I think this is a good place for a "RTFM". I suggest looking under "text output".
text output<<eye it doesn't run
You could use something like:
textprintf_ex(screen, font, 100, 100, makecol(255, 255, 255), -1, "Number: %i", eye);
Don't use iostream anyway ( << and >> and cout and cin), instead, use stdio (printf("hello"); )
But in Allegro, use textprintf_ex
OH noooo !!!
this is similar to language c but this is not similar to language c ++ - why ?
What does that even mean?
Allegro is a C library.
this is similar to language c but this is not similar to language c ++ - why ?
Because iostream style stinks, and I use C++.
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........
C++ is backwards compatible with C.
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...
I know c++ very well but library allegro is strange
Has the definition of "very well" changed since i last checked?
Why can't I use cin and cout with using library allegro - it is strange ?
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.
You can, but they output to the console, not to the main window.
thanks you
I know how to ask very well but my knowledge about programming is strange
Do not take it hard, I am just joking
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> |
4 | using namespace std; |
5 | |
6 | int main() |
7 | { |
8 | BITMAP *plan = NULL; |
9 | BITMAP *pawn = NULL; |
10 | |
11 | allegro_init(); |
12 | install_keyboard(); |
13 | set_color_depth(32); //32-bitowa glebia kolorow |
14 | set_gfx_mode(GFX_AUTODETECT,1280,1024,0,0); |
15 | set_palette(default_palette); |
16 | clear_to_color(screen,15); //wyczyszczenie struktury bitmap i zamalowanie jej danym kolorem |
17 | |
18 | |
19 | plan = load_bitmap("plan.bmp",default_palette); |
20 | pawn = load_bitmap("pawn.bmp",default_palette); |
21 | |
22 | |
23 | |
24 | blit(plan,screen,0,0,0,0,plan->w,plan->h); |
25 | blit(pawn,screen,0,0,0,0,pawn->w,pawn->h); |
26 | |
27 | textout_ex(screen,font,"You are starting from field number 6",10,740,2,-1); |
28 | textout_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]; |
37 | int eye; |
38 | srand (time(0)); |
39 | |
40 | |
41 | |
42 | |
43 | while(!key[KEY_ESC]) //program bedzie dzialac dopoki nie wcisne ESC |
44 | { |
45 | |
46 | eye = 1+rand()%6; |
47 | textprintf_ex(screen, font, 10, 820, makecol(0, 0, 0), -1, "You throw number: %d", eye); |
48 | textout_ex(screen,font,"r Go on right, l Go on left",10,840,2,-1); |
49 | readkey(); |
50 | if (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 | |
85 | destroy_bitmap(plan); |
86 | destroy_bitmap(pawn); |
87 | |
88 | allegro_exit(); |
89 | return 0; |
90 | |
91 | |
92 | |
93 | } |
94 | END_OF_MAIN() |
maybe this is (just a little bit) better?
1 | #include<iostream> |
2 | #include<ctime> |
3 | #include <allegro.h> |
4 | using namespace std; |
5 | |
6 | inline 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 | |
16 | int main() |
17 | { |
18 | BITMAP *plan = NULL; |
19 | BITMAP *pawn = NULL; |
20 | BITMAP *backbuffer = NULL; |
21 | allegro_init(); |
22 | install_keyboard(); |
23 | set_color_depth(32); //32-bitowa glebia kolorow |
24 | set_gfx_mode(GFX_AUTODETECT_WINDOWED,800,600,0,0); |
25 | clear_to_color(screen,makecol(255,255,255)); //wyczyszczenie struktury bitmap i zamalowanie jej danym kolorem |
26 | |
27 | backbuffer=create_bitmap(SCREEN_W,SCREEN_H); |
28 | if(NULL==backbuffer) |
29 | { |
30 | exit(1); //not enough computer memory |
31 | } |
32 | |
33 | |
34 | clear_to_color(backbuffer,makecol(255,255,255)); |
35 | |
36 | plan = load_bitmap("plan.bmp",NULL); |
37 | pawn = load_bitmap("pawn.bmp",NULL); |
38 | |
39 | |
40 | |
41 | blit(plan,backbuffer,0,0,0,0,plan->w,plan->h); |
42 | blit(pawn,backbuffer,0,0,0,0,pawn->w,pawn->h); |
43 | |
44 | textout_ex(backbuffer,font,"You are starting from field number 6",10,250,2,-1); |
45 | textout_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]; |
54 | int eye; |
55 | srand (time(0)); |
56 | update_eye(&eye,backbuffer); |
57 | blit(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 | |
98 | destroy_bitmap(plan); |
99 | destroy_bitmap(pawn); |
100 | destroy_bitmap(backbuffer); |
101 | allegro_exit(); |
102 | return 0; |
103 | |
104 | |
105 | |
106 | } |
107 | END_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.
thanks, but can't I clearing screen easier ?
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.
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.
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.
Guys, this is a lost cause. Just... do something better.
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> |
4 | using namespace std; |
5 | |
6 | int main() |
7 | { |
8 | BITMAP *plan = NULL; |
9 | BITMAP *pawn = NULL; |
10 | |
11 | allegro_init(); |
12 | install_keyboard(); |
13 | set_color_depth(32); //32-bitowa glebia kolorow |
14 | set_gfx_mode(GFX_AUTODETECT,1280,1024,0,0); |
15 | set_palette(default_palette); |
16 | clear_to_color(screen,15); //wyczyszczenie struktury bitmapy i zamalowanie jej danym kolorem |
17 | |
18 | |
19 | plan = load_bitmap("plan.bmp",default_palette); |
20 | pawn = load_bitmap("pawn.bmp",default_palette); |
21 | |
22 | |
23 | int tab[11]={30,130,230,330,450,550,690,820,900,1000,1150}; // <- "x" in pixels |
24 | int *index=&tab[5]; |
25 | int *start=&tab[0]; |
26 | int *end=&tab[10]; |
27 | int eye; |
28 | srand (time(0)); |
29 | |
30 | |
31 | |
32 | |
33 | blit(plan,screen,0,0,0,0,plan->w,plan->h); |
34 | blit(pawn,screen,0,0,*index,400,pawn->w,pawn->h); |
35 | |
36 | |
37 | textout_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 | |
45 | eye = 1+rand()%6; |
46 | textprintf_ex(screen, font, 10, 820, makecol(0, 0, 0), -1, "You throw number: %d", eye); |
47 | textout_ex(screen,font,"r Go on right, l Go on left",10,840,2,-1); |
48 | readkey(); |
49 | |
50 | if (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 | |
83 | clear_bitmap(screen); |
84 | blit(plan,screen,0,0,0,0,plan->w,plan->h); |
85 | blit(pawn,screen,0,0,*index,400,pawn->w,pawn->h); |
86 | textout_ex(screen,font,"ESC - The end of program",10,990,2,-1); |
87 | } |
88 | |
89 | destroy_bitmap(plan); |
90 | destroy_bitmap(pawn); |
91 | |
92 | allegro_exit(); |
93 | return 0; |
94 | |
95 | |
96 | |
97 | } |
98 | END_OF_MAIN() |
You should be passing NULL to load_bitmap, NOT default_palette.
blit(pawn,screen,0,0,*index,400,pawn->w,pawn->h);
WTF is that? *index? Is that even valid code?
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
Ah, okay.
THANK YOU A LOT - it is my code:
1 | #include<iostream> |
2 | #include<ctime> |
3 | #include <allegro.h> |
4 | using namespace std; |
5 | |
6 | int main() |
7 | { |
8 | BITMAP *plan = NULL; |
9 | BITMAP *pawn = NULL; |
10 | |
11 | allegro_init(); |
12 | install_keyboard(); |
13 | set_color_depth(32); //32-bitowa glebia kolorow |
14 | set_gfx_mode(GFX_AUTODETECT,1280,1024,0,0); |
15 | set_palette(default_palette); |
16 | clear_to_color(screen,15); //wyczyszczenie struktury bitmapy i zamalowanie jej danym kolorem |
17 | |
18 | |
19 | plan = load_bitmap("plan.bmp",NULL); |
20 | pawn = load_bitmap("pawn.bmp",NULL); |
21 | |
22 | |
23 | int tab1[11]={30,130,230,330,450,550,690,820,900,1000,1150}; // <- "x" in pixels |
24 | int tab2[11]={1,2,3,4,5,6,7,8,9,10,11}; |
25 | |
26 | int *index=&tab2[5]; |
27 | int *start=&tab2[0]; |
28 | int *end=&tab2[10]; |
29 | int eye; |
30 | srand (time(0)); |
31 | |
32 | |
33 | |
34 | |
35 | blit(plan,screen,0,0,0,0,plan->w,plan->h); |
36 | blit(pawn,screen,0,0,tab1[*index-1],400,pawn->w,pawn->h); |
37 | |
38 | |
39 | textout_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 | |
47 | eye = 1+rand()%6; |
48 | textprintf_ex(screen, font, 10, 820, makecol(0, 0, 0), -1, "You throw number: %d", eye); |
49 | textout_ex(screen,font,"r Go on right, l Go on left",10,840,2,-1); |
50 | readkey(); |
51 | |
52 | if (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 | |
85 | clear_bitmap(screen); |
86 | blit(plan,screen,0,0,0,0,plan->w,plan->h); |
87 | blit(pawn,screen,0,0,tab1[*index-1],400,pawn->w,pawn->h); |
88 | textout_ex(screen,font,"ESC - The end of program",10,990,2,-1); |
89 | } |
90 | |
91 | destroy_bitmap(plan); |
92 | destroy_bitmap(pawn); |
93 | |
94 | allegro_exit(); |
95 | return 0; |
96 | |
97 | |
98 | |
99 | } |
100 | END_OF_MAIN() |
My code is yet full mess and chaos, but it runs - this my first game !
YESSSSSS !!!!!!!!!!! hurrrrra
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:
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:
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.
LennyLen thank You very much for your advices - I will use it - thank you
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.
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 ?
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)
I change my code according with your hands WHAT ARE YOU THINKING NOW ABOUT MY CODE
1 | |
2 | |
3 | |
4 | #include<iostream> |
5 | #include<ctime> |
6 | #include <allegro.h> |
7 | using namespace std; |
8 | |
9 | int main() |
10 | { |
11 | BITMAP *chart = NULL; |
12 | BITMAP *pawn = NULL; |
13 | BITMAP *buffer = NULL; |
14 | |
15 | allegro_init(); |
16 | install_keyboard(); |
17 | set_color_depth(32); |
18 | set_gfx_mode(GFX_AUTODETECT,1280,1024,0,0); |
19 | buffer=create_bitmap(SCREEN_W,SCREEN_H); |
20 | |
21 | |
22 | chart = load_bitmap("chart.bmp",NULL); |
23 | pawn = load_bitmap("pawn.bmp",NULL); |
24 | |
25 | |
26 | int 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 | }; |
30 | int tab2[10]={1,2,3,4,5,6,7,8,9,10}; |
31 | int *index=&tab2[5]; |
32 | int *start=&tab2[0]; |
33 | int *end=&tab2[9]; |
34 | int eye; |
35 | srand (time(0)); |
36 | |
37 | |
38 | while(!key[KEY_ESC]) |
39 | { |
40 | eye = 1+rand()%4; |
41 | blit(chart,buffer,0,0,0,0,chart->w,chart->h); |
42 | blit(pawn,buffer,0,0,tab1[0][*index-1],tab1[1][*index-1],pawn->w,pawn->h); |
43 | textprintf_ex(buffer, font, 10, 820, makecol(0, 0, 0), -1, "You throw: %d", eye); |
44 | textprintf_ex(buffer, font, 10, 840, makecol(0, 0, 0), -1, "-> - right, <- - left"); |
45 | textprintf_ex(buffer, font, 10, 990, makecol(0, 0, 0), -1, "ESC - the end of game"); |
46 | blit(buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H); |
47 | readkey(); |
48 | |
49 | abc: |
50 | if (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 | |
63 | else 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 | |
75 | else |
76 | { |
77 | if (!key[KEY_ESC]) |
78 | { |
79 | goto abc; |
80 | } |
81 | |
82 | } |
83 | |
84 | clear_to_color(buffer,makecol(255,255,255)); |
85 | |
86 | } |
87 | |
88 | destroy_bitmap(chart); |
89 | destroy_bitmap(pawn); |
90 | destroy_bitmap(buffer); |
91 | allegro_exit(); |
92 | return 0; |
93 | } |
94 | END_OF_MAIN() |
Don't use "goto" statements.
Use "break"s and while(...) loops.
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.
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
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.
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> |
4 | using namespace std; |
5 | |
6 | void timer_tick(); |
7 | void setup_timer(); |
8 | int timer; |
9 | |
10 | int 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 | } |
115 | END_OF_MAIN(); |
116 | |
117 | void timer_tick() { |
118 | |
119 | timer++; |
120 | |
121 | }END_OF_FUNCTION(timer_tick); |
122 | |
123 | void 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 | } |
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...
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")
1 | eye = 1+rand()%4; |
2 | blit(chart,buffer,0,0,0,0,chart->w,chart->h); |
3 | blit(pawn,buffer,0,0,tab1[0][*index-1],tab1[1][*index-1],pawn->w,pawn->h); |
4 | textprintf_ex(buffer, font, 10, 820, makecol(0, 0, 0), -1, "You throw: %d", eye); |
5 | textprintf_ex(buffer, font, 10, 840, makecol(0, 0, 0), -1, "-> - right, <- - left"); |
6 | textprintf_ex(buffer, font, 10, 990, makecol(0, 0, 0), -1, "ESC - the end of game"); |
7 | blit(buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H); |
8 | readkey(); |
9 | |
10 | abc: |
11 | if (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 | |
24 | else 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 | |
36 | else |
37 | { |
38 | if (!key[KEY_ESC]) |
39 | { |
40 | goto abc; |
41 | } |
42 | |
43 | } |
44 | |
45 | clear_to_color(buffer,makecol(255,255,255)); |
46 | |
47 | } |
48 | |
49 | destroy_bitmap(chart); |
50 | destroy_bitmap(pawn); |
51 | destroy_bitmap(buffer); |
52 | allegro_exit(); |
53 | return 0; |
54 | } |
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.