Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Image Keyboard

This thread is locked; no one can reply to it. rss feed Print
Image Keyboard
motyas
Member #16,305
April 2016

Hello guys, i need your help, i'm doing an allegro player (to move it with keyboard arrows) but i can't, i don't know why... (My platform is Xcode, for dudes) I attached the error to the post.

This is my code:

#SelectExpand
1#include <allegro5/allegro.h> 2#include <allegro5/allegro_image.h> 3#include <allegro5/allegro_native_dialog.h> 4#include <allegro5/allegro_primitives.h> 5#include <allegro5/allegro_font.h> 6#include <allegro5/allegro_ttf.h> 7 8const int screenWidth = 640, screenHeight = 480; 9const float FPS = 60.0; 10enum direction { 11 UP, DOWN, LEFT, RIGHT 12}; 13 14int main(int argc, char *argv[]){ 15 16 ALLEGRO_DISPLAY * display = NULL; 17 ALLEGRO_EVENT_QUEUE * event_queue = NULL; 18 ALLEGRO_TIMER * timer = NULL; 19 ALLEGRO_KEYBOARD_STATE keyState; 20 21 float x = 10, y = 10, moveSpeed = 5; 22 23 int dir = DOWN; 24 25 bool done = false; 26 27 if(!al_init()){ 28 al_show_native_message_box(NULL, "Error", NULL, "Allegro couldn't initialize successfully", NULL, ALLEGRO_MESSAGEBOX_ERROR); 29 return -1; 30 } 31 32 al_install_keyboard(); 33 if(!al_install_keyboard()){ 34 al_show_native_message_box(NULL, "Error", NULL, "couldn't initialize keyboard successfully", NULL, ALLEGRO_MESSAGEBOX_ERROR); 35 return -1; 36 } 37 38 display = al_create_display(screenWidth, screenHeight); 39 al_set_window_title(display, "Keyboard image"); 40 41 al_clear_to_color(al_map_rgb(0, 0, 0)); 42 43 if(!display){ 44 al_show_native_message_box(NULL, "Error", NULL, "Window wasn't created successfully", NULL, ALLEGRO_MESSAGEBOX_ERROR); 45 return -1; 46 } 47 48 event_queue = al_create_event_queue(); 49 if(!event_queue){ 50 al_show_native_message_box(NULL, "Error", NULL, "Event queue wasn't created successfully", NULL, ALLEGRO_MESSAGEBOX_ERROR); 51 return -1; 52 } 53 54 timer = al_create_timer(1.0 / FPS); 55 if(!timer){ 56 al_show_native_message_box(NULL, "Error", NULL, "Timer wasn't created successfully", NULL, ALLEGRO_MESSAGEBOX_ERROR); 57 return -1; 58 } 59 60 ALLEGRO_BITMAP * player = NULL; 61 player = al_load_bitmap("/Users/matias/Desktop/Matías/C++/Programas/AllegroFiles/Tutorial/Allegro_Keyboard/Allegro_Keyboard/player.png"); 62 63 al_init_image_addon(); 64 65 al_register_event_source(event_queue, al_get_display_event_source(display)); 66 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 67 al_register_event_source(event_queue, al_get_keyboard_event_source()); 68 69 al_start_timer(timer); 70 71 while(!done){ 72 ALLEGRO_EVENT ev; 73 al_wait_for_event(event_queue, &ev); 74 al_get_keyboard_state(&keyState); 75 76 if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE){ 77 done = true; 78 } 79 else if(ev.type == ALLEGRO_EVENT_TIMER){ 80 81 if(al_key_down(&keyState, ALLEGRO_KEY_DOWN)){ 82 y += moveSpeed; 83 dir = DOWN; 84 } 85 else if(al_key_down(&keyState, ALLEGRO_KEY_UP)){ 86 y -= moveSpeed; 87 dir = UP; 88 } 89 else if(al_key_down(&keyState, ALLEGRO_KEY_RIGHT)){ 90 x += moveSpeed; 91 dir = RIGHT; 92 } 93 else if(al_key_down(&keyState, ALLEGRO_KEY_LEFT)){ 94 x -= moveSpeed; 95 dir = LEFT; 96 } 97 } 98 99 al_clear_to_color(al_map_rgb(0, 0, 0)); 100 al_draw_bitmap(player, x, y, NULL); 101 al_flip_display(); 102 103 } 104 105 al_destroy_bitmap(player); 106 al_destroy_timer(timer); 107 al_destroy_display(display); 108 al_destroy_event_queue(event_queue); 109 110 return 0; 111}

Someone can help me?

Thanks ;D

jmasterx
Member #11,410
October 2009

Sorry but my platform is XCode, for chicks.

Elias
Member #358
May 2000

Apple only provides the for chicks version... where can you even get the for dudes one?

--
"Either help out or stop whining" - Evert

Mark Oates
Member #1,146
March 2001
avatar

motyas
Member #16,305
April 2016

I said something weird? I think I misspoke (I don't speak english very well as you can see xD). And... What you refer with "for chicks" ?? ???

Chris Katko
Member #1,881
January 2002
avatar

You wrote Xcode for dudes (a casual word for men) and, not sure what you meant by that, people are jokingly using the word "chicks" (the equivalent casual word for women).

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs

Audric
Member #907
January 2001

This kind of crash is a called a "null pointer dereferencing"
See in the bottom window, "bitmap = (ALLEGRO_BITMAP *) null"
The line of code which crashes tries to do ->w and ->h on something which is null - it will always crash.

The left column shows you the call stack : which function calls which other.
al_draw_tinted_bitmap() which crashed, was called by al_draw_bitmap, which was called by al_mangled_main() - this one is your own "main" function, Allegro renames it internally.
Ideally, When clicking "2 :: _al_mangled_main", in XCode, it should show you exactly which line of your code called allegro's function with a null bitmap.
In this case, it's easy anyway because your code has only one call to al_draw_bitmap().
You pass the bitmap "player", so look up how you initialized it :

 ALLEGRO_BITMAP * player = NULL;
player = al_load_bitmap("/Users/matias/Desktop/Matías/C++/Programas/AllegroFiles/Tutorial/Allegro_Keyboard/Allegro_Keyboard/player.png");
al_init_image_addon();

Here I'm pretty sure that you should call al_init_image_addon() first, because it "plugs" the decoding functions for many file formats, like PNG.
For safety, after loading "player" image, you should still check if it's null, and if it is, stop the program.

motyas
Member #16,305
April 2016

Ahahahaha, is an english expression, i didn't understand :'c

And... Thanks men, you saved me, i didn't realize that allegro_init_image_addon(); was under my bitmap hahah ;D

Mark Oates
Member #1,146
March 2001
avatar

"dudes" and "chicks" are very informal words. You don't ever hear them outside of pop culture. If you have ever heard the words "bro" or "homies" it's similar.

motyas
Member #16,305
April 2016

Ahhh, homies sounds like Homer hahaha...
Also, can i do you a question? I solve my first problem, but I have other (I want to create a RPG player. My console is Xcode, for chicks) ;D

There's my code:

#SelectExpand
1#include <allegro5/allegro.h> 2#include <allegro5/allegro_image.h> 3#include <allegro5/allegro_native_dialog.h> 4#include <allegro5/allegro_primitives.h> 5#include <allegro5/allegro_font.h> 6#include <allegro5/allegro_ttf.h> 7 8const int screenWidth = 640, screenHeight = 480; 9const float FPS = 60.0; 10enum direction { 11 UP, DOWN, LEFT, RIGHT 12}; 13 14int main(int argc, char *argv[]){ 15 16 ALLEGRO_DISPLAY * display = NULL; 17 ALLEGRO_EVENT_QUEUE * event_queue = NULL; 18 ALLEGRO_TIMER * timer = NULL; 19 ALLEGRO_KEYBOARD_STATE keyState; 20 21 float x = 320, y = 240, moveSpeed = 5; 22 23 int dir = DOWN, sourceX = 32, sourceY = 0; 24 25 bool done = false, draw = true, active = false; 26 27 if(!al_init()){ 28 al_show_native_message_box(NULL, "Error", NULL, "Allegro couldn't initialize successfully", NULL, ALLEGRO_MESSAGEBOX_ERROR); 29 return -1; 30 } 31 32 al_install_keyboard(); 33 if(!al_install_keyboard()){ 34 al_show_native_message_box(NULL, "Error", NULL, "couldn't initialize keyboard successfully", NULL, ALLEGRO_MESSAGEBOX_ERROR); 35 return -1; 36 } 37 38 display = al_create_display(screenWidth, screenHeight); 39 al_set_window_title(display, "Keyboard image"); 40 41 al_clear_to_color(al_map_rgb(0, 0, 0)); 42 43 if(!display){ 44 al_show_native_message_box(NULL, "Error", NULL, "Window wasn't created successfully", NULL, ALLEGRO_MESSAGEBOX_ERROR); 45 return -1; 46 } 47 48 event_queue = al_create_event_queue(); 49 if(!event_queue){ 50 al_show_native_message_box(NULL, "Error", NULL, "Event queue wasn't created successfully", NULL, ALLEGRO_MESSAGEBOX_ERROR); 51 return -1; 52 } 53 54 timer = al_create_timer(1.0 / FPS); 55 if(!timer){ 56 al_show_native_message_box(NULL, "Error", NULL, "Timer wasn't created successfully", NULL, ALLEGRO_MESSAGEBOX_ERROR); 57 return -1; 58 } 59 al_init_image_addon(); 60 61 ALLEGRO_BITMAP * player = NULL; 62 player = al_load_bitmap("/Users/matias/Desktop/Matías/C++/Programas/AllegroFiles/Tutorial/Allegro_Keyboard/Allegro_Keyboard/resources/player.png"); 63 64 al_init_primitives_addon(); 65 66 al_register_event_source(event_queue, al_get_display_event_source(display)); 67 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 68 al_register_event_source(event_queue, al_get_keyboard_event_source()); 69 70 al_start_timer(timer); 71 72 while(!done){ 73 ALLEGRO_EVENT ev; 74 al_wait_for_event(event_queue, &ev); 75 al_get_keyboard_state(&keyState); 76 77 if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE){ 78 done = true; 79 } 80 else if(ev.type == ALLEGRO_EVENT_TIMER){ 81 82 active = true; 83 84 if(al_key_down(&keyState, ALLEGRO_KEY_DOWN)){ 85 y += moveSpeed; 86 dir = DOWN; 87 } 88 else if(al_key_down(&keyState, ALLEGRO_KEY_UP)){ 89 y -= moveSpeed; 90 dir = UP; 91 } 92 else if(al_key_down(&keyState, ALLEGRO_KEY_RIGHT)){ 93 x += moveSpeed; 94 dir = RIGHT; 95 } 96 else if(al_key_down(&keyState, ALLEGRO_KEY_LEFT)){ 97 x -= moveSpeed; 98 dir = LEFT; 99 } 100 else{ 101 active = false; 102 } 103 104 if(active){ 105 sourceX += al_get_bitmap_width(player) / 3; 106 } 107 else{ 108 sourceX = 32; 109 } 110 111 if(sourceX >= al_get_bitmap_width(player)){ 112 sourceX = 0; 113 } 114 115 sourceY = dir; 116 117 draw = true; 118 } 119 120 if(draw){ 121 al_draw_bitmap_region(player, sourceX, sourceY * al_get_bitmap_height(player) / 4, 32, 32, x, y, NULL); 122 al_flip_display(); 123 al_clear_to_color(al_map_rgb(0, 0, 0)); 124 } 125 } 126 127 al_destroy_bitmap(player); 128 al_destroy_timer(timer); 129 al_destroy_display(display); 130 al_destroy_event_queue(event_queue); 131 132 return 0; 133}

I attached an image of my console to this post.
One more time, thanks! ;D

I feel that this post is going to be left behind :'(

Go to: