Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Ummm,I think I have trouble

This thread is locked; no one can reply to it. rss feed Print
Ummm,I think I have trouble
keprast
Member #16,794
January 2018

Hello, anyone.

This is the complete structure of the code.
The content is incomplete.

The structure is yes.but“al_load_bitmap”so error.

If you run it, it will prompt "error, error, allegro load bitmap".

Why?I checked the name of the picture,I checked the path of the picture.

But,error???
The path of the picture is right.Picture and source code in the same folder.
In windows system.

What happened on earth?!
--------------------------------------------
(I fix the text content)

#SelectExpand
1#include"allegro5\allegro.h" 2#include"allegro5\allegro_image.h" 3#include"allegro5\allegro_native_dialog.h" 4#include"stdio.h" 5#include"cmath" 6 7enum MYKEYS 8{ 9 W,S,A,D 10}; 11 12const float pi = 3.14; 13const float Vx_min = 4.2; 14const float Vy_min = 4.2; 15const float Vx_max = 8.4; 16const float Vy_max = 8.4; 17const float Vx_a = 1.05; 18const float Vy_a = 1.05; 19 20float Vx = 0.0; 21float Vy = 0.0; 22float hudu = 0.0; 23float angle = 0.0; 24 25//const char * const mybitmap = "shibing.png"; 26const int pmW = 720; 27const int pmH = 480; 28 29int main(int argc, char * argv[]) 30{ 31 int wtW, wtH; 32 ALLEGRO_DISPLAY *pm = NULL; 33 ALLEGRO_BITMAP *wt = NULL; 34 ALLEGRO_EVENT_QUEUE *move = NULL; 35 ALLEGRO_EVENT ev; 36 ALLEGRO_TIMER *timer = NULL; 37 38 bool key[4]{ false,false,false,false }; 39 bool key1 = true; 40 bool key2 = false; 41 42 const float FPS = 60.0; 43 44 if (!al_init()) { 45 al_show_native_message_box(pm, "Error", "Error", "allegro system!", 46 NULL, ALLEGRO_MESSAGEBOX_ERROR); 47 return 1; 48 } 49 50 if (!al_install_keyboard()) { 51 al_show_native_message_box(pm, "Error", "Error", "allegro key!", 52 NULL, ALLEGRO_MESSAGEBOX_ERROR); 53 return 1; 54 } 55 56 pm = al_create_display(pmW, pmH); 57 58 if (pm == NULL) { 59 al_show_native_message_box(pm, "Error", "Error", "allegro display!", 60 NULL, ALLEGRO_MESSAGEBOX_ERROR); 61 return 1; 62 } 63 64 timer = al_create_timer(1 / FPS); 65 66 if (timer == NULL) { 67 al_show_native_message_box(pm, "Error", "Error", "allegro timer!", 68 NULL, ALLEGRO_MESSAGEBOX_ERROR); 69 al_destroy_display(pm); 70 return 1; 71 } 72 73 move = al_create_event_queue(); 74 75 if (move == NULL) { 76 al_show_native_message_box(pm, "Error", "Error", "allegro event!", 77 NULL, ALLEGRO_MESSAGEBOX_ERROR); 78 al_destroy_display(pm); 79 al_destroy_timer(timer); 80 return 1; 81 } 82 83 al_register_event_source(move, al_get_display_event_source(pm)); 84 al_register_event_source(move, al_get_keyboard_event_source()); 85 al_register_event_source(move, al_get_timer_event_source(timer)); 86 87 if (!al_init_image_addon) { 88 al_show_native_message_box(pm, "Error", "Error", "allegro image system!", 89 NULL, ALLEGRO_MESSAGEBOX_ERROR); 90 al_destroy_display(pm); 91 al_destroy_timer(timer); 92 al_destroy_event_queue(move); 93 return 1; 94 } 95 96 wt = al_load_bitmap("shibing.png"); 97 98 if (wt == NULL) { 99 al_show_native_message_box(pm, "Error", "Error", "allegro load bitmap!", 100 NULL, ALLEGRO_MESSAGEBOX_ERROR); 101 al_destroy_display(pm); 102 al_destroy_timer(timer); 103 al_destroy_event_queue(move); 104 return 1; 105 } 106 107 wtW = al_get_bitmap_width(wt); 108 wtH = al_get_bitmap_height(wt); 109 110 float x = (pmW / 2) - (wtW / 2); 111 float y = (pmH / 2) - (wtH / 2); 112 113 al_start_timer(timer); 114 115 while (!key2) 116 { 117 al_wait_for_event(move, &ev); 118 if (ALLEGRO_EVENT_DISPLAY_CLOSE){ 119 key2 = true; 120 } 121 else 122 if (ev.type == ALLEGRO_EVENT_KEY_DOWN) 123 { 124 if (ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { 125 key2 = true; 126 } 127 if (ev.keyboard.keycode == ALLEGRO_KEY_W) { 128 key[W] = true; 129 Vx = Vx_min; 130 Vy = Vy_min; 131 Vx = Vx*sin(hudu); 132 Vy = Vy*cos(hudu); 133 } 134 if (ev.keyboard.keycode == ALLEGRO_KEY_S) { 135 key[S] = true; 136 Vx = Vx_min; 137 Vy = Vy_min; 138 Vx = Vx*sin(hudu); 139 Vy = Vy*cos(hudu); 140 } 141 if (ev.keyboard.keycode == ALLEGRO_KEY_A) { 142 key[A] = true; 143 Vx = Vx_min; 144 Vy = Vy_min; 145 Vx = Vx*sin(hudu); 146 Vy = Vy*cos(hudu); 147 } 148 if (ev.keyboard.keycode == ALLEGRO_KEY_D) { 149 key[D] = true; 150 Vx = Vx_min; 151 Vy = Vy_min; 152 Vx = Vx*sin(hudu); 153 Vy = Vy*cos(hudu); 154 } 155 } 156 else 157 if (ev.type == ALLEGRO_KEY_UP) { 158 if (ev.keyboard.keycode == ALLEGRO_KEY_W) { 159 key[W] = false; 160 } 161 if (ev.keyboard.keycode == ALLEGRO_KEY_S) { 162 key[S] = false; 163 } 164 if (ev.keyboard.keycode == ALLEGRO_KEY_A) { 165 key[A] = false; 166 } 167 if (ev.keyboard.keycode == ALLEGRO_KEY_D) { 168 key[D] = false; 169 } 170 } 171 if (ev.type == ALLEGRO_EVENT_TIMER) { 172 al_set_target_bitmap(al_get_backbuffer(pm)); 173 al_clear_to_color(al_map_rgb(255, 255, 255)); 174 175 al_draw_bitmap(wt, x, y, 0); 176 177 al_flip_display(); 178 } 179 } 180 al_destroy_display(pm); 181 al_destroy_timer(timer); 182 al_destroy_event_queue(move); 183 al_destroy_bitmap(wt); 184 185 return 0; 186}

Chris Katko
Member #1,881
January 2002
avatar

Don't forget code tags! :)

#SelectExpand
1#include"allegro5\allegro.h" 2#include"allegro5\allegro_image.h" 3#include"allegro5\allegro_native_dialog.h" 4#include"stdio.h" 5#include"cmath" 6 7enum MYKEYS 8{ 9W,S,A,D 10}; 11 12const float pi = 3.14; 13const float Vx_min = 4.2; 14const float Vy_min = 4.2; 15const float Vx_max = 8.4; 16const float Vy_max = 8.4; 17const float Vx_a = 1.05; 18const float Vy_a = 1.05; 19 20float Vx = 0.0; 21float Vy = 0.0; 22float hudu = 0.0; 23float angle = 0.0; 24 25//const char * const mybitmap = "shibing.png"; 26const int pmW = 720; 27const int pmH = 480; 28 29int main(int argc, char * argv[]) 30{ 31int wtW, wtH; 32ALLEGRO_DISPLAY *pm = NULL; 33ALLEGRO_BITMAP *wt = NULL; 34ALLEGRO_EVENT_QUEUE *move = NULL; 35ALLEGRO_EVENT ev; 36ALLEGRO_TIMER *timer = NULL; 37 38bool key[4]{ false,false,false,false }; 39bool key1 = true; 40bool key2 = false; 41 42const float FPS = 60.0; 43 44if (!al_init()) { 45al_show_native_message_box(pm, "Error", "Error", "allegro system!", 46NULL, ALLEGRO_MESSAGEBOX_ERROR); 47return 1; 48} 49 50if (!al_install_keyboard()) { 51al_show_native_message_box(pm, "Error", "Error", "allegro key!", 52NULL, ALLEGRO_MESSAGEBOX_ERROR); 53return 1; 54} 55 56pm = al_create_display(pmW, pmH); 57 58if (pm == NULL) { 59al_show_native_message_box(pm, "Error", "Error", "allegro display!", 60NULL, ALLEGRO_MESSAGEBOX_ERROR); 61return 1; 62} 63 64timer = al_create_timer(1 / FPS); 65 66if (timer == NULL) { 67al_show_native_message_box(pm, "Error", "Error", "allegro timer!", 68NULL, ALLEGRO_MESSAGEBOX_ERROR); 69al_destroy_display(pm); 70return 1; 71} 72 73move = al_create_event_queue(); 74 75if (move == NULL) { 76al_show_native_message_box(pm, "Error", "Error", "allegro event!", 77NULL, ALLEGRO_MESSAGEBOX_ERROR); 78al_destroy_display(pm); 79al_destroy_timer(timer); 80return 1; 81} 82 83al_register_event_source(move, al_get_display_event_source(pm)); 84al_register_event_source(move, al_get_keyboard_event_source()); 85al_register_event_source(move, al_get_timer_event_source(timer)); 86 87if (!al_init_image_addon) { 88al_show_native_message_box(pm, "Error", "Error", "allegro image system!", 89NULL, ALLEGRO_MESSAGEBOX_ERROR); 90al_destroy_display(pm); 91al_destroy_timer(timer); 92al_destroy_event_queue(move); 93return 1; 94} 95 96wt = al_load_bitmap("shibing.png"); 97 98if (wt == NULL) { 99al_show_native_message_box(pm, "Error", "Error", "allegro load bitmap!", 100NULL, ALLEGRO_MESSAGEBOX_ERROR); 101al_destroy_display(pm); 102al_destroy_timer(timer); 103al_destroy_event_queue(move); 104return 1; 105} 106 107wtW = al_get_bitmap_width(wt); 108wtH = al_get_bitmap_height(wt); 109 110float x = (pmW / 2) - (wtW / 2); 111float y = (pmH / 2) - (wtH / 2); 112 113al_start_timer(timer); 114 115while (!key2) 116{ 117al_wait_for_event(move, &ev); 118if (ALLEGRO_EVENT_DISPLAY_CLOSE){ 119key2 = true; 120} 121else 122if (ev.type == ALLEGRO_EVENT_KEY_DOWN) 123{ 124if (ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { 125key2 = true; 126} 127if (ev.keyboard.keycode == ALLEGRO_KEY_W) { 128key[W] = true; 129Vx = Vx_min; 130Vy = Vy_min; 131Vx = Vx*sin(hudu); 132Vy = Vy*cos(hudu); 133} 134if (ev.keyboard.keycode == ALLEGRO_KEY_S) { 135key[S] = true; 136Vx = Vx_min; 137Vy = Vy_min; 138Vx = Vx*sin(hudu); 139Vy = Vy*cos(hudu); 140} 141if (ev.keyboard.keycode == ALLEGRO_KEY_A) { 142key[A] = true; 143Vx = Vx_min; 144Vy = Vy_min; 145Vx = Vx*sin(hudu); 146Vy = Vy*cos(hudu); 147} 148if (ev.keyboard.keycode == ALLEGRO_KEY_D) { 149key[D] = true; 150Vx = Vx_min; 151Vy = Vy_min; 152Vx = Vx*sin(hudu); 153Vy = Vy*cos(hudu); 154} 155} 156else 157if (ev.type == ALLEGRO_KEY_UP) { 158if (ev.keyboard.keycode == ALLEGRO_KEY_W) { 159key[W] = false; 160} 161if (ev.keyboard.keycode == ALLEGRO_KEY_S) { 162key[S] = false; 163} 164if (ev.keyboard.keycode == ALLEGRO_KEY_A) { 165key[A] = false; 166} 167if (ev.keyboard.keycode == ALLEGRO_KEY_D) { 168key[D] = false; 169} 170} 171if (ev.type == ALLEGRO_EVENT_TIMER) { 172al_set_target_bitmap(al_get_backbuffer(pm)); 173al_clear_to_color(al_map_rgb(255, 255, 255)); 174 175al_draw_bitmap(wt, x, y, 0); 176 177al_flip_display(); 178} 179} 180al_destroy_display(pm); 181al_destroy_timer(timer); 182al_destroy_event_queue(move); 183al_destroy_bitmap(wt); 184 185return 0; 186}

What operating system are you running? If it's Linux (and Mac OS X?), the filenames are case sensitive. If it's windows, make sure that the file is located in where ever you told Visual Studio/etc the "working directory" is.

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

keprast
Member #16,794
January 2018

@Chris Katko
I'm sorry, but I don't know Tags.

It's a window system.
The position of the picture is correct.

bamccaig
Member #7,536
July 2006
avatar

You can edit your post until a few hours (or days?) have passed by clicking the edit icon.

611202

Please add code tags to make it easier to read the code, and from now on whenever you post code try to use this mechanism (both here and on any other programming sites). You are more likely to get help that way.

Code tags refer to XHTML tags as follows:

<code>
int main(int argc, char * argv[])
{
return 0;
}
</code>

You cannot see it, but line 3 is 4 space characters (" ") followed by "return 0;".

That displays it as:

int main(int argc, char * argv[])
{
    return 0;
}

The syntax highlighting and monospaced font make it much easier to read the code. It will also preserve white-space so indentation is not lost. On some other sites they will use "BBCode" tags instead which use square-brackets "[]" instead of angle-brackets "<>" like so: [code]your code here[/code]. A few others use something like "markdown" (and allegro.cc supports some markdown syntax as well, but it's less reliable due to limitations with the syntax). You will eventually become familiar with all of these.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The code that you posted has an error in it, and if you enable warnings in your compiler (and your compiler is decent) it should show it to you. Here's what GCC says with -Wall (to enable "all" warnings):

main.cpp: In function ‘int main(int, char**)’:
main.cpp:87:10: warning: the address of ‘bool al_init_image_addon()’ will always evaluate as ‘true’ [-Waddress]
     if (!al_init_image_addon) {

The error message explains what's wrong, but since English isn't your first language and you're new to programming it might not be obvious. You're missing the parenthesis "()" to invoke the al_init_image_addon function. Instead, you're testing the memory address of the function which is guaranteed to be non-zero (i.e., true) on any system you would ever run this code on. Note that since you aren't actually calling the function the image addon is not actually initialized which explains why loading the sprite later on fails.

To fix it, add the parenthesis:

Once I fix that issue the program runs without error, but it terminates immediately so the window flashes and then disappears.

That's likely because of another similar error in the code:

        if (ALLEGRO_EVENT_DISPLAY_CLOSE){

This time instead of comparing this constant/enum value with the event variable you are just testing if the constant/enum itself is non-zero (and in this case it is so key2 is immediately set to true and the second iteration of the loop exits immediately).

Fix it by comparing the event type variable instead:

        if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE){

keprast
Member #16,794
January 2018

@bamccaig:
Thanks for your help!:)
The problem has been solved.;D

Go to: