Im using DevCPP with Allegro and C++. I have a program Im making and so far the coding went excellent no errors at all, then when I ran it it was black for maybe 4 seconds and then said "Would you like to send an error report" and when I click dont send it just exits the program. Idk what is wrong but if you know that would be appreciated here is my code
1 | //This is my first attempt at a game using the allegro |
2 | //sdk or whatever |
3 | // by GuitarGod1134 |
4 | // Im going for some kind of rpg like thing here |
5 | |
6 | //lets begin with the header |
7 | #include <allegro.h> |
8 | |
9 | //declare our pics |
10 | //these are pics for our charactor |
11 | BITMAP *charupSprite; |
12 | BITMAP *chardownSprite; |
13 | BITMAP *charleftSprite; |
14 | BITMAP *charrightSprite; |
15 | |
16 | //our player coordinates on the screen |
17 | int playerx = 10; |
18 | int playery = 10; |
19 | |
20 | //players direction to correspond with the sprite |
21 | int dir = 1; |
22 | |
23 | //main function |
24 | int main() |
25 | { |
26 | //initiate the usual allegro stuff |
27 | allegro_init(); |
28 | install_keyboard(); |
29 | set_color_depth(16); |
30 | set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0); |
31 | |
32 | //clear the key buffer so its free of memory |
33 | clear_keybuf(); |
34 | |
35 | //lets get a loop going so when the person presses |
36 | // esc he/she can quit the game |
37 | while ( !key[KEY_ESC]) |
38 | { |
39 | |
40 | //charactor pics |
41 | charupSprite = load_bitmap("charup.bmp", NULL); |
42 | chardownSprite = load_bitmap("chardown.bmp", NULL); |
43 | charleftSprite = load_bitmap("charleft.bmp", NULL); |
44 | charrightSprite = load_bitmap("charright.bmp", NULL); |
45 | |
46 | |
47 | //acquire the screen |
48 | acquire_screen(); |
49 | |
50 | //lets set up the movement for the player |
51 | if (key[KEY_UP]) dir = 1, --playery; |
52 | else if (key[KEY_DOWN]) dir = 2, ++playery; |
53 | else if (key[KEY_RIGHT]) dir = 3, ++playerx; |
54 | else if (key[KEY_LEFT]) dir = 4, --playerx; |
55 | |
56 | //now the actual blitting of the picture to the screen |
57 | //all the statements are for directions |
58 | if (dir == 1) |
59 | { |
60 | draw_sprite( screen, charupSprite, playerx, playery); |
61 | } |
62 | else if (dir == 2) |
63 | { |
64 | draw_sprite( screen, chardownSprite, playerx, playery); |
65 | } |
66 | else if (dir == 3) |
67 | { |
68 | draw_sprite( screen, charrightSprite, playerx, playery); |
69 | } |
70 | else if (dir == 4) |
71 | { |
72 | draw_sprite( screen, charleftSprite, playerx, playery); |
73 | } |
74 | |
75 | //end the while |
76 | } |
77 | |
78 | //finish it off |
79 | release_screen(); |
80 | return 0; |
81 | } |
82 | |
83 | //end the main |
84 | END_OF_MAIN(); |
And here is everything if you want to test it yourself (which i recommend)
The biggest and probably what's causing the problem is that you're loading bitmaps inside the while loop. This means that it will be constantly trying to reload your bitmaps every cycle of the while.
Your code should instead be something like this:
1 | #include <allegro.h> |
2 | |
3 | int main() |
4 | { |
5 | int playerx = 10; |
6 | int playery = 10; |
7 | int dir = 1; |
8 | |
9 | allegro_init(); |
10 | install_keyboard(); |
11 | set_color_depth(16); |
12 | set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0); |
13 | |
14 | BITMAP *charupSprite = load_bitmap("charup.bmp", NULL); |
15 | BITMAP *chardownSprite = load_bitmap("chardown.bmp", NULL); |
16 | BITMAP *charleftSprite = load_bitmap("charleft.bmp", NULL); |
17 | BITMAP *charrightSprite = load_bitmap("charright.bmp", NULL); |
18 | |
19 | BITMAP *buffer = create_bimap(640, 480); |
20 | |
21 | while ( !key[KEY_ESC]) |
22 | { |
23 | clear_to_color(buffer, makecol(0, 0, 0)); |
24 | |
25 | if (key[KEY_UP]) |
26 | { |
27 | dir = 1; |
28 | --playery; |
29 | } |
30 | if (key[KEY_DOWN]) |
31 | { |
32 | dir = 2; |
33 | ++playery; |
34 | } |
35 | if (key[KEY_RIGHT]) |
36 | { |
37 | dir = 3; |
38 | ++playerx; |
39 | } |
40 | if (key[KEY_LEFT]) |
41 | { |
42 | dir = 4; |
43 | --playerx; |
44 | } |
45 | |
46 | if (dir == 1) |
47 | { |
48 | draw_sprite(buffer, charupSprite, playerx, playery); |
49 | } |
50 | else if (dir == 2) |
51 | { |
52 | draw_sprite(buffer, chardownSprite, playerx, playery); |
53 | } |
54 | else if (dir == 3) |
55 | { |
56 | draw_sprite(buffer, charrightSprite, playerx, playery); |
57 | } |
58 | else if (dir == 4) |
59 | { |
60 | draw_sprite(buffer, charleftSprite, playerx, playery); |
61 | } |
62 | |
63 | blit(buffer, screen, 0, 0, 0, 0, 640, 480); |
64 | } |
65 | |
66 | destroy_bitmap(charupSprite); |
67 | destroy_bitmap(chardownSprite); |
68 | destroy_bitmap(charrightSprite); |
69 | destroy_bitmap(charleftSprite); |
70 | destroy_bitmap(buffer); |
71 | |
72 | return 0; |
73 | } |
74 | END_OF_MAIN() |
If you must mess with acquire/release screen, it should be directly around the drawing calls, not around the logic too. You should also have a timer on your logic.
And you should be drawing to a buffer, the blitting the buffer to the screen.
OK i loaded the images outside of the while loop and im still getting the error report
Chances are that the BITMAPs are failing to load. Try checking the return values of the load_bitmap() calls to see if they are successful or not.
Remove the acquire_bitmap and release_bitmap things entirely. Just make a buffer and draw to that (as people have said).
Considering how many people misuse acquire_bitmap, maybe we should consider changing whatever it says about it in the manual?
You assume that people read the manual:
Warning: This function can be very dangerous to use, since the whole program may get locked while the bitmap is locked. So the lock should only be held for a short time, and you should not call anything but drawing operations onto the locked video bitmap while a lock is in place. Especially don't call things like show_mouse (or scare_mouse which calls that) or readkey, since it will most likely deadlock your entire program.
But where does people learn of that evil function?
Tutorials, other code, and (perhaps) skimming the table of contents.
LennyLen: Chances are that the BITMAPs are failing to load. Try checking the return values of the load_bitmap() calls to see if they are successful or not.
Ya how do i Do that?
Anyone else have an idea whats wrong?
Ya how do i Do that?
Something like this:
if (!(charupSprite = load_bitmap("charup.bmp", NULL))) { allegro_message("Error loading charup.bmp"); exit(-1); }
In C++, you might get away with declaring and testing the value all in line, but in C, you can't, which is why I removed the dclaration. I always prefer to declare all variables in one place rather than when I first use them anyway.
If you feel you absolutely MUST declare them when you first use them, then you can use this method of testing:
BITMAP *charupSprite = load_bitmap("charup.bmp", NULL); if (!charupSprite) { allegro_message("Error loading charup.bmp"); exit(-1); }
Ya it turns out they are failing to load. I tried that error message and sure enough it returned the error message. I even tried renaming the bmp's to 1,2,3,4 to make sure that they are not misspelled but they still failed to load.
EDIt; ok i got it my pics arent valid for some reason. I used different pics and they worked so I now know its the pics thanks for the help!
You probably have the .bmp files in the wrong directory. IIRC, Dev-C++ creates a directory for your project, then it creates a DEBUG and RELEASE subdirectory within that where the executables will be. Put the .bmp files in the project directory, not in the subdirectory with the .exe.
I used to put my bitmap files in the wrong directory to. None of the tutorials I read said anything about it. Finally I found out that with Dev-c++, you put it in the folder called Dev-c++ where all your c++ files are saved automatically saved to.
IIRC, Dev-C++ creates a directory for your project, then it creates a DEBUG and RELEASE subdirectory within that where the executables will be.
You don't recall correctly.:-X You're describing Visual Studio's behavior.
You don't recall correctly. You're describing Visual Studio's behavior.
I think my mind is trying to block all memory of Dev-C++. The only IDE I've liked less is Visual-MinGW, which somehow managed to have even more bugs than Dev-C++.