This post is a portion of Part 6 in my on going series about 2D Game Development using the Allegro 5 library. These posts are in course order. You can see all of the posts in this course by clicking the “2D Game Development” button at the top of this site.
- 6.0 – Bitmaps
- 6.1 – Creating Bitmaps
- 6.2 – Loading Premade Bitmaps
- 6.3 – Rotating, Scaling, Tinting Bitmaps
In the first video of Part 6, we are looking at how to create bitmaps inside of our program.
Full source can be found here.
Notice that the source code is slightly different from the video? Here is why.
Hi,
I got a problem that i could not solve. I search a lot in the internet, but i haven’t found.
This is my code:
#include
#include
#include
// Variáveis Globais
int height=600,width=400;
const int FPS = 60;
int sair = false;
int reDraw = true;
// Protótipos
bool inicializacao(ALLEGRO_DISPLAY *display);
void createPlayer(ALLEGRO_BITMAP *p, ALLEGRO_DISPLAY *display);
int main(){
// Variáveis Allegro
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT_QUEUE *events = NULL;
ALLEGRO_TIMER *timer = NULL;
ALLEGRO_BITMAP *player = NULL;
ALLEGRO_BITMAP *image = NULL;
// Inicialização do Allegro
sair = !inicializacao(display);
// Outras Inicializações
player = al_create_bitmap(60,60);
al_set_target_bitmap(player);
al_clear_to_color(al_map_rgb(255,255,255));
al_draw_filled_circle(30,30,20,al_map_rgb(55,0,166));
al_draw_filled_circle(15,15,3,al_map_rgb(111,111,166));
al_draw_filled_circle(45,15,3,al_map_rgb(111,111,166));
al_set_target_bitmap(al_get_backbuffer(display));
events = al_create_event_queue();
timer = al_create_timer(1.0 / FPS);
al_start_timer(timer);
al_register_event_source(events,al_get_keyboard_event_source());
al_register_event_source(events,al_get_mouse_event_source());
al_register_event_source(events,al_get_timer_event_source(timer));
while(!sair){
ALLEGRO_EVENT ev;
al_wait_for_event(events,&ev);
if(ev.type==ALLEGRO_EVENT_KEY_DOWN){
switch(ev.keyboard.keycode){
case ALLEGRO_KEY_ESCAPE:
sair = true;
break;
}
}else if(ev.type==ALLEGRO_EVENT_TIMER){
// Aqui ocorrem as operações que devem ser sincronizadas
reDraw = true;
}else if(ev.type==ALLEGRO_EVENT_MOUSE_AXES){
}
if(reDraw && al_is_event_queue_empty(events)){
// Redesenhar Grtáficos
al_draw_bitmap(player,(width/2)-30,(height/2)-30,0);
al_flip_display();
al_clear_to_color(al_map_rgb(255,255,255));
reDraw = false;
}
}
// Destruir Objetos
al_destroy_bitmap(player);
al_destroy_display(display);
al_destroy_timer(timer);
return 0;
}
bool inicializacao(ALLEGRO_DISPLAY *display){
if(!al_init()) return false;
ALLEGRO_MONITOR_INFO info;
if(al_get_monitor_info(0,&info)){
width = info.x2 – info.x1;
height = info.y2 – info.y1;
}
al_set_new_display_flags(ALLEGRO_FULLSCREEN);
display = al_create_display(width,height);
if(display==NULL) return false;
if(!al_install_keyboard()) return false;
if(!al_install_mouse()) return false;
if(!al_init_primitives_addon()) return false;
if(!al_init_image_addon()) return false;
return true;
}
The problem says: al_copy_tranform: Assertion ‘src’ failed.
Sorry, I solved the problem. It needs a pointer to a pointer, because in sometimes is not changing the pointer.
Glad you solved it!
WHY CAN I ONLY SEE THE TURRETS?????????
this is my code
//
//GLOBALS=================================================================
//
const int WIDTH = 800;
const int HEIGHT = 600;
//
//prototypes==============================================================
//
int main()
{
//
//Variablen===========================================================
//
int screen_w;
int screen_h;
bool done = false;
int ImageRadius = 20;
//
//Objektvariablen=====================================================
//
//
// ALLEGRO Variablen==================================================
//
ALLEGRO_DISPLAY *display=NULL;
ALLEGRO_COLOR black,red,green,background,brown,yellow,blue;
ALLEGRO_BITMAP *image = NULL;
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
ALLEGRO_EVENT ev;
//
// COLORS=============================================================
//
black = al_map_rgb(0,0,0);
red = al_map_rgb(255,0,0);
green = al_map_rgb(0,255,0);
blue = al_map_rgb(0,0,255);
brown = al_map_rgb(200,200,0);
background = al_map_rgb(80,80,255);
yellow = al_map_rgb(200,200,0);
//
//INITIALISIERUNG=====================================================
//
if(!al_init())
{
al_show_native_message_box(NULL,”ERROR”,NULL,”failed to initialize Allegro”,NULL,NULL);
return -1;
}
display=al_create_display(WIDTH,HEIGHT);
if(!display)
{
al_show_native_message_box(NULL,”ERROR”,NULL,”failed to create the display”,NULL,NULL);
return -1;
}
al_init_primitives_addon();
if(!al_install_keyboard() || !al_install_mouse())
{
al_show_native_message_box(NULL,”ERROR”,NULL,”failed to install Controls”,NULL,NULL);
return -1;
}
image = al_create_bitmap(40, 40);
al_set_target_bitmap(image);
al_draw_filled_rectangle(ImageRadius, ImageRadius – 9, ImageRadius + 10, ImageRadius – 7, al_map_rgb(255,0,0));
al_draw_filled_rectangle(ImageRadius, ImageRadius + 9, ImageRadius + 10, ImageRadius + 7, al_map_rgb(255,0,0));
al_draw_filled_triangle(ImageRadius – 12, ImageRadius – 17, ImageRadius + 12, ImageRadius, ImageRadius – 12, ImageRadius – 17, al_map_rgb(0,0,200));
al_draw_filled_rectangle(ImageRadius – 12, ImageRadius – 2, ImageRadius + 15, ImageRadius + 2, green);
al_set_target_bitmap(al_get_backbuffer(display));
//
//DEFINITION==========================================================
//
screen_w = al_get_display_width(display);
screen_h =al_get_display_height(display);
//al_hide_mouse_cursor(display);
event_queue = al_create_event_queue();
al_register_event_source(event_queue,al_get_keyboard_event_source());
//al_register_event_source(event_queue,al_get_display_event_source(display));
//al_register_event_source(event_queue,al_get_mouse_event_source());
//
//CODE================================================================
//
while(!done)
{
al_wait_for_event(event_queue,&ev);
if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
{
switch(ev.keyboard.keycode)
{
case ALLEGRO_KEY_ESCAPE:
done=true;
break;
}
}
al_clear_to_color(al_map_rgb(255,255,255));
al_draw_bitmap(image, 100,100,0);
al_flip_display();
}
al_destroy_event_queue(event_queue);
al_destroy_display(display);
return 0;
}
display is a kind of bitmap right? so when we are changing from *image to our original back buffer we aare writing this –>
al_set_target_bitmap(al_get_backbuffer(display));
but instead cant we write this? –>
al_set_target_bitmap(display);
???
No, the display is a data structure that contains more than just the pixel data.
hi how would you go about creating a jump command if you were creating a platformer