Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Problem with code? Compiles, but doesn't run.

Credits go to Kris Asick and LennyLen for helping out!
This thread is locked; no one can reply to it. rss feed Print
Problem with code? Compiles, but doesn't run.
Durnus
Member #7,997
November 2006
avatar

Mind if I post the code here? If not:

1#include <allegro.h>
2 
3 
4int particles[10][10];
5 
6int xcursor;
7int ycursor;
8 
9 
10BITMAP *buffer;
11 
12int main(){
13
14 allegro_init();
15 install_keyboard();
16
17 set_color_depth(8);
18 set_gfx_mode( GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0);
19
20 set_window_title("The World of Sand - Durnus's Falling Sand Game");
21
22 xcursor = 0;
23 ycursor = 0;
24
25 while ( !key[KEY_ESC] ){
26
27 clear_keybuf();
28 acquire_screen();
29
30
31 if(key[KEY_UP])ycursor--;
32 if(key[KEY_DOWN]) ycursor++;
33 if(key[KEY_LEFT]) xcursor--;
34 if(key[KEY_RIGHT]) xcursor++;
35
36 if(key[KEY_SPACE]) particles[xcursor][ycursor] = 1;
37
38 for(int i = 0; i < 10; i++)
39 {
40 for(int j = 0; i < 10; i++)
41 {
42 if(particles[j]<i>==1)
43 {
44 rectfill( buffer, j*2, j*2, i*2, i*2+1, makecol( 100, 100, 100));
45 }
46 }
47 }
48
49 rectfill( buffer, xcursor*2, xcursor*2+1, ycursor*2, ycursor*2+1, makecol( 255, 255, 255));
50
51
52 draw_sprite(screen, buffer, 0, 0);
53
54
55 release_screen();
56
57
58 rest(10);
59
60
61 
62 }
63
64 return 0;
65
66}
67END_OF_MAIN();

Sorry about all the spaces, that just helps me keep the code organized. :P

I'm not quite sure what is making errors, but when I make all the controls and rectfills comments, it runs perfectly.

Could someone tell me what's wrong? I'm not asking for new code, just a way to fix my problems. ???

Kris Asick
Member #1,424
July 2001

Remove your acquire_screen() and release_screen() commands. They are only needed when you want to make multiple reads and writes to video memory, and even then, when you do call these functions you should ONLY be drawing to video memory between them and nothing else.

Since Allegro will automatically call these commands when needed, and you're only making one write to video memory (the "screen" bitmap), there's no need to have them.

--- Kris Asick (Gemini)
--- http://www.pixelships.com

--- Kris Asick (Gemini)
--- http://www.pixelships.com

LennyLen
Member #5,313
December 2004
avatar

You need to create the buffer BITMAP before you can use it. For example:
buffer = create_bitmap(width, height)

Durnus
Member #7,997
November 2006
avatar

Thanks! It works now! (I'm saying 'works' as in it draws something.) ;D

Kris Asick
Member #1,424
July 2001

Whoops! I missed that! ::)

Yeah, do what LennyLen said too. Add that particular line of code anywhere before your while loop! You may also want to call clear_bitmap(buffer) after you create it too to be sure it's nice and empty.

--- Kris Asick (Gemini)
--- http://www.pixelships.com

--- Kris Asick (Gemini)
--- http://www.pixelships.com

Go to: