throw bone
a b

How can I see number with using library Allegro ?
I have got code in c++ but I don't know how can I see it using allegro?

int eye;
srand (time(0));
eye = 1+rand()%6;
cout<<eye;

Jonatan Hedborg

I think this is a good place for a "RTFM". I suggest looking under "text output".

a b

text output<<eye it doesn't run

TotalControl

You could use something like:
textprintf_ex(screen, font, 100, 100, makecol(255, 255, 255), -1, "Number: %i", eye);

Archon

Don't use iostream anyway ( << and >> and cout and cin), instead, use stdio (printf("hello"); )

But in Allegro, use textprintf_ex

a b

OH noooo !!!

this is similar to language c but this is not similar to language c ++ - why ?

Jonatan Hedborg

What does that even mean?

Allegro is a C library.

Archon
Quote:

this is similar to language c but this is not similar to language c ++ - why ?

Because iostream style stinks, and I use C++.

a b

So I have to use language C and I can not use c ++ ? :( So I will not can use classes, heirdom etc. :( It is so sad........

Archon

C++ is backwards compatible with C.

GullRaDriel

a b, you really need to go watch what is C and what is C++, after what go back to allegro library.

Tons of people here use allegro with C++ or C.

You can use all C functions in a C++ program.

What ? hahem ! Weird...

a b

I know c++ very well but library allegro is strange

Jonatan Hedborg

Has the definition of "very well" changed since i last checked?

a b

Why can't I use cin and cout with using library allegro - it is strange ?

Archon
Quote:

Why can't I use cin and cout with using library allegro - it is strange ?

Those are for the CONSOLE and not for the Allegro window.

Fladimir da Gorf

You can, but they output to the console, not to the main window.

a b

thanks you

GullRaDriel
a b-corrected- said:

I know how to ask very well but my knowledge about programming is strange

Do not take it hard, I am just joking :P

a b

My english isn't so good.

Could you say me what is wrong :

I have got table of size= 11. I am on field number 6, automically throw bone for example 4 and press l (left) so I am on field number 2, automically throw bone for example 3 and press l (left) so I am on field number 10, automically throw bone for example 4 and press r (right) so I am on field number 3 - my code in c++ is good but programm print very unattractively text - so what is vrong ??:

1#include<iostream>
2#include<ctime>
3#include <allegro.h>
4using namespace std;
5 
6int main()
7{
8BITMAP *plan = NULL;
9BITMAP *pawn = NULL;
10 
11allegro_init();
12install_keyboard();
13set_color_depth(32); //32-bitowa glebia kolorow
14set_gfx_mode(GFX_AUTODETECT,1280,1024,0,0);
15set_palette(default_palette);
16clear_to_color(screen,15); //wyczyszczenie struktury bitmap i zamalowanie jej danym kolorem
17 
18 
19plan = load_bitmap("plan.bmp",default_palette);
20pawn = load_bitmap("pawn.bmp",default_palette);
21 
22 
23 
24blit(plan,screen,0,0,0,0,plan->w,plan->h);
25blit(pawn,screen,0,0,0,0,pawn->w,pawn->h);
26 
27textout_ex(screen,font,"You are starting from field number 6",10,740,2,-1);
28textout_ex(screen,font,"ESC - The end of program",10,990,2,-1);
29 
30 
31 
32 
33 int tab[11]={1,2,3,4,5,6,7,8,9,10,11};
34 int *index=&tab[5];
35 int *start=&tab[0];
36 int *end=&tab[10];
37int eye;
38srand (time(0));
39 
40 
41 
42 
43 while(!key[KEY_ESC]) //program bedzie dzialac dopoki nie wcisne ESC
44{
45 
46eye = 1+rand()%6;
47textprintf_ex(screen, font, 10, 820, makecol(0, 0, 0), -1, "You throw number: %d", eye);
48textout_ex(screen,font,"r Go on right, l Go on left",10,840,2,-1);
49readkey();
50if (key[KEY_L])
51 {
52 if ((index-eye)>=start)
53 {
54 index=index-eye;
55 textprintf_ex(screen, font, 10, 860, makecol(0, 0, 0), -1, "You are now on field number: %d", *index);
56
57 }
58 else
59 {
60 index=end-(eye-*index);
61 textprintf_ex(screen, font, 10, 860, makecol(0, 0, 0), -1, "You are now on field number: %d", *index);
62 }
63
64 }
65
66 else if (key[KEY_R])
67 {
68
69 if ((index+eye)<=end)
70 {
71 index=index+eye;
72 textprintf_ex(screen, font, 10, 860, makecol(0, 0, 0), -1, "You are now on field number: %d", *index);
73 }
74 else
75 {
76 index=start+(eye-(*end-*index))-1;
77 textprintf_ex(screen, font, 10, 860, makecol(0, 0, 0), -1, "You are now on field number: %d", *index);
78 }
79 }
80 
81 
82 
83}
84 
85destroy_bitmap(plan);
86destroy_bitmap(pawn);
87 
88allegro_exit();
89return 0;
90 
91 
92 
93}
94END_OF_MAIN()

m c

maybe this is (just a little bit) better?

1#include<iostream>
2#include<ctime>
3#include <allegro.h>
4using namespace std;
5 
6inline void update_eye(int *eye, BITMAP *backbuffer)
7{
8 *eye = 1+rand()%6;
9 printf("%d\n",*eye);
10 textprintf_ex(backbuffer, font, 10, 320, makecol(0, 0, 0), -1, "You throw number: %d", *eye);
11 textout_ex(backbuffer,font,"r Go on right, l Go on left",10,340,2,-1);
12
13 return;
14}
15 
16int main()
17{
18BITMAP *plan = NULL;
19BITMAP *pawn = NULL;
20BITMAP *backbuffer = NULL;
21allegro_init();
22install_keyboard();
23set_color_depth(32); //32-bitowa glebia kolorow
24set_gfx_mode(GFX_AUTODETECT_WINDOWED,800,600,0,0);
25clear_to_color(screen,makecol(255,255,255)); //wyczyszczenie struktury bitmap i zamalowanie jej danym kolorem
26 
27backbuffer=create_bitmap(SCREEN_W,SCREEN_H);
28if(NULL==backbuffer)
29{
30 exit(1); //not enough computer memory
31}
32 
33 
34clear_to_color(backbuffer,makecol(255,255,255));
35 
36plan = load_bitmap("plan.bmp",NULL);
37pawn = load_bitmap("pawn.bmp",NULL);
38 
39 
40 
41blit(plan,backbuffer,0,0,0,0,plan->w,plan->h);
42blit(pawn,backbuffer,0,0,0,0,pawn->w,pawn->h);
43 
44textout_ex(backbuffer,font,"You are starting from field number 6",10,250,2,-1);
45textout_ex(backbuffer,font,"ESC - The end of program",10,490,2,-1);
46 
47 
48 
49 
50 int tab[11]={1,2,3,4,5,6,7,8,9,10,11};
51 int *index=&tab[5];
52 int *start=&tab[0];
53 int *end=&tab[10];
54int eye;
55srand (time(0));
56update_eye(&eye,backbuffer);
57blit(backbuffer,screen,0,0,0,0,SCREEN_W,SCREEN_H);
58 
59 
60 while(!key[KEY_ESC]) //program bedzie dzialac dopoki nie wcisne ESC
61{
62
63 update_eye(&eye,backbuffer);
64 readkey();
65 if (key[KEY_L])
66 {
67 if ((index-eye)>=start)
68 {
69 index=index-eye;
70 textprintf_ex(backbuffer, font, 10, 360, makecol(0, 0, 0), -1, "You are now on field number: %d", *index);
71
72 }
73 else
74 {
75 index=end-(eye-*index);
76 textprintf_ex(backbuffer, font, 10, 360, makecol(0, 0, 0), -1, "You are now on field number: %d", *index);
77 }
78
79 }
80 else if (key[KEY_R])
81 {
82 if ((index+eye)<=end)
83 {
84 index=index+eye;
85 textprintf_ex(backbuffer, font, 10, 360, makecol(0, 0, 0), -1, "You are now on field number: %d", *index);
86 }
87 else
88 {
89 index=start+(eye-(*end-*index))-1;
90 textprintf_ex(backbuffer, font, 10, 360, makecol(0, 0, 0), -1, "You are now on field number: %d", *index);
91 }
92 }
93 
94 blit(backbuffer,screen,0,0,0,0,SCREEN_W,SCREEN_H);
95 clear_to_color(backbuffer,makecol(255,255,255)); //Clear the buffer for next time
96}
97 
98destroy_bitmap(plan);
99destroy_bitmap(pawn);
100destroy_bitmap(backbuffer);
101allegro_exit();
102return 0;
103 
104 
105 
106}
107END_OF_MAIN()

It is still quite ugly.

what i think that you were forgetting is that you were NOT clearing the screen before you wrote to it again.

So every time through that while loop, you were printing on top of the text that was still on there from last time.

a b

thanks, but can't I clearing screen easier ?

m c

yes, you could just
clear_bitmap(screen);
i suppose. It's just that people normally use a double buffer to negate any chances of tearing, which usually looks pretty bad.

Archon
Quote:

thanks, but can't I clearing screen easier ?

You don't clear the 'screen', you should clear the 'backbuffer' which should be as big as the screen.

BAF

Why are you clearing the backbuffer to white? Usually you just clear_bitmap and put it to black.

And, as Archon said, you don't need to clear the screen.

X-G

Guys, this is a lost cause. Just... do something better.

a b

THANKS !! I study library from one day (today) and I almost write my first game :) But something in run my pawn (checker ?) is wrong - please see - it is my board (chart ?):
http://img474.imageshack.us/img474/7083/planst5.png

and it is my pawn (checker?):
http://img381.imageshack.us/img381/121/pawnjg7.png

Could you tell me what is wrong - I am so excited :)

1#include<iostream>
2#include<ctime>
3#include <allegro.h>
4using namespace std;
5 
6int main()
7{
8BITMAP *plan = NULL;
9BITMAP *pawn = NULL;
10 
11allegro_init();
12install_keyboard();
13set_color_depth(32); //32-bitowa glebia kolorow
14set_gfx_mode(GFX_AUTODETECT,1280,1024,0,0);
15set_palette(default_palette);
16clear_to_color(screen,15); //wyczyszczenie struktury bitmapy i zamalowanie jej danym kolorem
17 
18 
19plan = load_bitmap("plan.bmp",default_palette);
20pawn = load_bitmap("pawn.bmp",default_palette);
21 
22 
23int tab[11]={30,130,230,330,450,550,690,820,900,1000,1150}; // <- "x" in pixels
24int *index=&tab[5];
25int *start=&tab[0];
26int *end=&tab[10];
27int eye;
28srand (time(0));
29 
30 
31 
32 
33blit(plan,screen,0,0,0,0,plan->w,plan->h);
34blit(pawn,screen,0,0,*index,400,pawn->w,pawn->h);
35 
36 
37textout_ex(screen,font,"ESC - The end of program",10,990,2,-1);
38 
39 
40 
41 
42 while(!key[KEY_ESC]) //program bedzie dzialac dopoki nie wcisne ESC
43{
44 
45eye = 1+rand()%6;
46textprintf_ex(screen, font, 10, 820, makecol(0, 0, 0), -1, "You throw number: %d", eye);
47textout_ex(screen,font,"r Go on right, l Go on left",10,840,2,-1);
48readkey();
49 
50if (key[KEY_L])
51 {
52 if ((index-eye)>=start)
53 {
54 index=index-eye;
55
56
57
58 }
59 else
60 {
61 index=end-(eye-*index);
62
63
64 }
65
66 }
67
68 else if (key[KEY_R])
69 {
70
71 if ((index+eye)<=end)
72 {
73 index=index+eye;
74
75 }
76 else
77 {
78 index=start+(eye-(*end-*index))-1;
79
80 }
81 }
82 
83clear_bitmap(screen);
84blit(plan,screen,0,0,0,0,plan->w,plan->h);
85blit(pawn,screen,0,0,*index,400,pawn->w,pawn->h);
86textout_ex(screen,font,"ESC - The end of program",10,990,2,-1);
87}
88 
89destroy_bitmap(plan);
90destroy_bitmap(pawn);
91 
92allegro_exit();
93return 0;
94 
95 
96 
97}
98END_OF_MAIN()

BAF

You should be passing NULL to load_bitmap, NOT default_palette.

Quote:

blit(pawn,screen,0,0,*index,400,pawn->w,pawn->h);

WTF is that? *index? Is that even valid code?

a b

BAF 0,0,*index,400

*index is my "x" and "400" is my "y" because I have
int tab[11]={30,130,230,330,450,550,690,820,900,1000,1150}; // <- "x" in pixels
int *index=&tab[5];

so my pawn is starting (x,y) (550, 400) so it is field number 6 on board (chart ?) :
http://img474.imageshack.us/img474/7083/planst5.png

BAF

Ah, okay.

a b

THANK YOU A LOT - it is my code:

1#include<iostream>
2#include<ctime>
3#include <allegro.h>
4using namespace std;
5 
6int main()
7{
8BITMAP *plan = NULL;
9BITMAP *pawn = NULL;
10 
11allegro_init();
12install_keyboard();
13set_color_depth(32); //32-bitowa glebia kolorow
14set_gfx_mode(GFX_AUTODETECT,1280,1024,0,0);
15set_palette(default_palette);
16clear_to_color(screen,15); //wyczyszczenie struktury bitmapy i zamalowanie jej danym kolorem
17 
18 
19plan = load_bitmap("plan.bmp",NULL);
20pawn = load_bitmap("pawn.bmp",NULL);
21 
22 
23int tab1[11]={30,130,230,330,450,550,690,820,900,1000,1150}; // <- "x" in pixels
24int tab2[11]={1,2,3,4,5,6,7,8,9,10,11};
25 
26int *index=&tab2[5];
27int *start=&tab2[0];
28int *end=&tab2[10];
29int eye;
30srand (time(0));
31 
32 
33 
34 
35blit(plan,screen,0,0,0,0,plan->w,plan->h);
36blit(pawn,screen,0,0,tab1[*index-1],400,pawn->w,pawn->h);
37 
38 
39textout_ex(screen,font,"ESC - The end of program",10,990,2,-1);
40 
41 
42 
43 
44 while(!key[KEY_ESC]) //program bedzie dzialac dopoki nie wcisne ESC
45{
46 
47eye = 1+rand()%6;
48textprintf_ex(screen, font, 10, 820, makecol(0, 0, 0), -1, "You throw number: %d", eye);
49textout_ex(screen,font,"r Go on right, l Go on left",10,840,2,-1);
50readkey();
51 
52if (key[KEY_L])
53 {
54 if ((index-eye)>=start)
55 {
56 index=index-eye;
57
58
59
60 }
61 else
62 {
63 index=end-(eye-*index);
64
65
66 }
67
68 }
69
70 else if (key[KEY_R])
71 {
72
73 if ((index+eye)<=end)
74 {
75 index=index+eye;
76
77 }
78 else
79 {
80 index=start+(eye-(*end-*index))-1;
81
82 }
83 }
84 
85clear_bitmap(screen);
86blit(plan,screen,0,0,0,0,plan->w,plan->h);
87blit(pawn,screen,0,0,tab1[*index-1],400,pawn->w,pawn->h);
88textout_ex(screen,font,"ESC - The end of program",10,990,2,-1);
89}
90 
91destroy_bitmap(plan);
92destroy_bitmap(pawn);
93 
94allegro_exit();
95return 0;
96 
97 
98 
99}
100END_OF_MAIN()

My code is yet full mess and chaos, but it runs - this my first game ! ;D :P
YESSSSSS !!!!!!!!!!! ;D hurrrrra :)

LennyLen

A couple of things.

Why are you using a pointer to point to an array of the numbers 1..11 when you can just use the numbers 1..11?

Instead of:

Quote:

int tab2[11]={1,2,3,4,5,6,7,8,9,10,11};

int *index=&tab2[5];
int *start=&tab2[0];
int *end=&tab2[10];

You can use:

int index = 5;
int start = 0;
int end = 10;

And then replace any instances of *index, *start and *end in your program with index, start and end. This is simpler and will not effect the way your game works.

It's also not a good idea to hardcode in pixel values like this:

Quote:

int tab1[11]={30,130,230,330,450,550,690,820,900,1000,1150};

If you change the width of your bitmap, you need to rewrite every value. Try to encode a formula instead. Something like:

for (n = 0; n < 11; n++)
    tab[n] = START + ( width * (n - 1) ) + (GAP * n);

where START and GAP are #DEFINE values.

a b

LennyLen thank You very much for your advices - I will use it - thank you

Steve Terry

I have some more advice... read a god damn C book! Learn proper code indentation. Lean the difference between C and C++ (your code is not C++ besides the fact you included some C++ headers and a namespace which are completely unused anyway). Once you grasp those concepts you can begin to learn the difference between 8-bit and high color depth concepts, they do work in very different ways and you should not mix the two. Lastly learn what a double buffer is and use it.

a b

Steve Terry:
"I have some more advice... read a god damn C book! Learn proper code indentation. Lean the difference between C and C++ (your code is not C++ besides the fact you included some C++ headers and a namespace which are completely unused anyway)."

I know c++, I don't use classes because I wanted only to see use library allegro.

"Once you grasp those concepts you can begin to learn the difference between 8-bit and high color depth concepts, they do work in very different ways and you should not mix the two."

Where in my code is mix two ways ?

Jonatan Hedborg
a b said:

Where in my code is mix two ways ?

Well, if you knew the difference, you wouldn't have to ask that ;)

set_color_depth(32); //High bit-depth.
set_palette(default_palette); //Only useful in 8bpp mode...
...
textout_ex(screen,font,"ESC - The end of program",10,990,2,-1); //You can't really use "2" as a color. Use makecol(redpart,bluepart,greenpart)

a b

I change my code according with your hands ;D;D;D WHAT ARE YOU THINKING NOW ABOUT MY CODE ?????? ;D;D;D;D

1 
2 
3 
4#include<iostream>
5#include<ctime>
6#include <allegro.h>
7using namespace std;
8 
9int main()
10{
11BITMAP *chart = NULL;
12BITMAP *pawn = NULL;
13BITMAP *buffer = NULL;
14 
15allegro_init();
16install_keyboard();
17set_color_depth(32);
18set_gfx_mode(GFX_AUTODETECT,1280,1024,0,0);
19buffer=create_bitmap(SCREEN_W,SCREEN_H);
20 
21 
22chart = load_bitmap("chart.bmp",NULL);
23pawn = load_bitmap("pawn.bmp",NULL);
24 
25 
26int tab1[2][10]={
27 {30,130,230,330,450,550,690,820,900,1000},
28 {400,400,400,400,400,400,400,400,400,400},
29 };
30int tab2[10]={1,2,3,4,5,6,7,8,9,10};
31int *index=&tab2[5];
32int *start=&tab2[0];
33int *end=&tab2[9];
34int eye;
35srand (time(0));
36 
37 
38while(!key[KEY_ESC])
39{
40eye = 1+rand()%4;
41blit(chart,buffer,0,0,0,0,chart->w,chart->h);
42blit(pawn,buffer,0,0,tab1[0][*index-1],tab1[1][*index-1],pawn->w,pawn->h);
43textprintf_ex(buffer, font, 10, 820, makecol(0, 0, 0), -1, "You throw: %d", eye);
44textprintf_ex(buffer, font, 10, 840, makecol(0, 0, 0), -1, "-> - right, <- - left");
45textprintf_ex(buffer, font, 10, 990, makecol(0, 0, 0), -1, "ESC - the end of game");
46blit(buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H);
47readkey();
48 
49abc:
50if (key[KEY_LEFT])
51 {
52 if ((index-eye)>=start)
53 {
54 index=index-eye;
55 }
56 else
57 {
58 index=end-(eye-*index);
59 }
60
61 }
62
63else if (key[KEY_RIGHT])
64 {
65 if ((index+eye)<=end)
66 {
67 index=index+eye;
68 }
69 else
70 {
71 index=start+(eye-(*end-*index))-1;
72 }
73 }
74
75else
76{
77 if (!key[KEY_ESC])
78 {
79 goto abc;
80 }
81
82}
83 
84clear_to_color(buffer,makecol(255,255,255));
85 
86}
87 
88destroy_bitmap(chart);
89destroy_bitmap(pawn);
90destroy_bitmap(buffer);
91allegro_exit();
92return 0;
93}
94END_OF_MAIN()

Archon

Don't use "goto" statements. :-/

Use "break"s and while(...) loops.

Steve Terry

a b: if you are comfortable with classes there is nothing that is stopping you from writing classes. C++ is an extension of C and is backwards compatible. You just can't use some C/C++ functionality like cin/cout/printf/scanf/etc. These only work on console windows. Your indentation needs some work as well.

a b

I will do this with classes but first I would like you check my code if everything is ok because it is the most important (if everything is ok I will wrote this and other thing using classes) I am starting write with allegro and you are masters in using allegro so :D

Tobias Dammers
Quote:

I know c++, I don't use classes because I wanted only to see use library allegro.

C++ is more than just "C with classes".
Here are some key features of C++ over C:
- Stronger type checking.
- Templates.
- Exception handling ( try { ... } catch (exception) { ... } )
- Function overloading
- Operator overloading
- new and delete replacing the tedious and error-prone malloc()
- "smart" cast operators (e.g. dynamic_cast<>() )
- References
- STL (though not a core language feature)

Nonetheless, a proper C program usually passes C++ compilation just fine.

ngiacomelli

a b: You'll get better responses if you present your code properly. I went through with Notepad and did some basic indentation. I also added a timer in, and separated your logic from your drawing. I got rid of that strange goto statement.

People have suggested reading up more on C/C++. I suggest you do that, and also stop copy and pasting code without considering it. You had a while loop checking: !key[KEY_ESC] and then an if statement checking for the exact same thing, later on.

I haven't tested this code, so I've probably made errors. But I hope it helps.

EDIT: I also added a check for problems when setting the graphics mode. Check for errors!

1#include <iostream>
2#include <ctime>
3#include <allegro.h>
4using namespace std;
5 
6void timer_tick();
7void setup_timer();
8int timer;
9 
10int main()
11{
12 
13 BITMAP *chart = NULL;
14 BITMAP *pawn = NULL;
15 BITMAP *buffer = NULL;
16 
17 allegro_init();
18 install_keyboard();
19 set_color_depth( 32 );
20 
21 if (set_gfx_mode(GFX_AUTODETECT, 1280, 1024, 0, 0) != 0)
22 {
23 if (set_gfx_mode(GFX_SAFE, 1280, 1024, 0, 0) != 0)
24 {
25
26 set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
27 allegro_message("Unable to set any graphic mode\n%s\n", allegro_error);
28 return 1;
29 }
30 }
31 
32 buffer = create_bitmap(SCREEN_W,SCREEN_H);
33 
34 chart = load_bitmap("chart.bmp",NULL);
35 pawn = load_bitmap("pawn.bmp",NULL);
36 
37 int tab1[2][10]={
38 {30,130,230,330,450,550,690,820,900,1000},
39 {400,400,400,400,400,400,400,400,400,400},
40 };
41 int tab2[10]={1,2,3,4,5,6,7,8,9,10};
42 int *index=&tab2[5];
43 int *start=&tab2[0];
44 int *end=&tab2[9];
45 int eye=0;
46 
47 srand (time(0));
48 
49 setup_timer();
50 
51 while ( !key[KEY_ESC] )
52 {
53 
54 while ( timer > 0 ) {
55
56 timer--;
57 
58 if ( key[KEY_LEFT] )
59 {
60 
61 if ((index-eye)>=start)
62 {
63 
64 index = index - eye;
65 
66 } else {
67 
68 index = end - ( eye-*index );
69 
70 }
71 
72 } else if ( key[KEY_RIGHT] ) {
73 
74 if ( ( index + eye ) <= end ) {
75 
76 index = index + eye;
77 
78 } else {
79 
80 index = start + ( eye-( *end-*index ) )-1;
81 
82 }
83 }
84
85 }
86 
87 eye = 1+rand()%4;
88 
89 /* I moved this call to clear_to_color into the rendering loop.
90 I assume you want it to clear the buffer bitmap before each render,
91 rather than when the user decides to quit?*/
92 
93 clear_to_color(buffer,makecol(255,255,255));
94 
95 blit(chart,buffer,0,0,0,0,chart->w,chart->h);
96 blit(pawn,buffer,0,0,tab1[0][*index-1],tab1[1][*index-1],pawn->w,pawn->h);
97 
98 textprintf_ex(buffer, font, 10, 820, makecol(0, 0, 0), -1, "You throw: %d", eye);
99 textprintf_ex(buffer, font, 10, 840, makecol(0, 0, 0), -1, "-> - right, <- - left");
100 textprintf_ex(buffer, font, 10, 990, makecol(0, 0, 0), -1, "ESC - the end of game");
101
102 blit(buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H);
103
104 }
105 
106 destroy_bitmap(chart);
107 destroy_bitmap(pawn);
108 destroy_bitmap(buffer);
109 
110 allegro_exit();
111 
112 return 0;
113 
114}
115END_OF_MAIN();
116 
117void timer_tick() {
118
119 timer++;
120
121}END_OF_FUNCTION(timer_tick);
122 
123void setup_timer()
124{
125
126 LOCK_FUNCTION(timer_tick);
127 LOCK_VARIABLE(timer);
128 
129 install_int_ex(timer_tick, BPS_TO_TIMER(60));
130
131}

Tobias Dammers

Another tip: Modularize your code. Split code into dedicated functions. I suggest:
init() to set up everything: allegro, globals, the game itself
logic_update() to update the game logic
draw() to update the screen
main_loop() for, well, the main loop; this should call logic_update() and draw()
cleanup() to clean up everything you have allocated, exit allegro, etc.
...and of course the ones you already have.
Ideally, each function should be no longer than 25 lines. If it is, divide some more.

This is useful for several reasons:
1) You can clearly separate different parts of the program, like logic and drawing code; each goes into a separate function, and if the logic function touches any drawing routines, you know instantly something is wrong.
2) You may end up re-using code before you know
3) The style is easy on the eye, and it's easier to see what each function does.
4) The compiler has an easier time optimizing your code when your functions are shorter.

Oh, and clearing the buffer bitmap right before you destroy_bitmap() it is pretty useless...

a b

Tobias and Nial thank you for help my. VARY THANK !!;D

I don't agree with one thing - you wrote:
"Oh, and clearing the buffer bitmap right before you destroy_bitmap() it is pretty useless..."
" /* I moved this call to clear_to_color into the rendering loop.
I assume you want it to clear the buffer bitmap before each render,
rather than when the user decides to quit?*/"

I want that: first player see map (not black screen), throw bone, player decide if he want to go on left or on right and he press <- or ->, map is cleared and player see new map with pawn on definite field after moving about definite number of fields on left or right, throw bone, press <- or ->, clearing screen and again .... so I think it is good:

(before this code I didn't use "blit" so my screen is black so I first must use "blit")

1eye = 1+rand()%4;
2blit(chart,buffer,0,0,0,0,chart->w,chart->h);
3blit(pawn,buffer,0,0,tab1[0][*index-1],tab1[1][*index-1],pawn->w,pawn->h);
4textprintf_ex(buffer, font, 10, 820, makecol(0, 0, 0), -1, "You throw: %d", eye);
5textprintf_ex(buffer, font, 10, 840, makecol(0, 0, 0), -1, "-> - right, <- - left");
6textprintf_ex(buffer, font, 10, 990, makecol(0, 0, 0), -1, "ESC - the end of game");
7blit(buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H);
8readkey();
9 
10abc:
11if (key[KEY_LEFT])
12 {
13 if ((index-eye)>=start)
14 {
15 index=index-eye;
16 }
17 else
18 {
19 index=end-(eye-*index);
20 }
21
22 }
23
24else if (key[KEY_RIGHT])
25 {
26 if ((index+eye)<=end)
27 {
28 index=index+eye;
29 }
30 else
31 {
32 index=start+(eye-(*end-*index))-1;
33 }
34 }
35
36else
37{
38 if (!key[KEY_ESC])
39 {
40 goto abc;
41 }
42
43}
44 
45clear_to_color(buffer,makecol(255,255,255));
46 
47}
48 
49destroy_bitmap(chart);
50destroy_bitmap(pawn);
51destroy_bitmap(buffer);
52allegro_exit();
53return 0;
54}

ngiacomelli

I don't understand what you're asking here. As far as I can see: you render everything each frame (chart, etc, etc). By adding the call to clear_to_color before you do your rendering, you basically make the buffer bitmap totally white. Imagine starting a painting with a blank canvas. Once you've made the buffer totally white, you can then render everything onto it.

As far as having the clear_to_color call where you initially placed it: there is no point. You destroy the bitmap right after making it white.

EDIT: Of course, if your chart bitmap is >= the set resolution, you wouldn't need to clear the bitmap. It is still good practice, though.

Thread #589033. Printed from Allegro.cc