Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » throw bone

This thread is locked; no one can reply to it. rss feed Print
 1   2 
throw bone
a b
Member #8,092
December 2006

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
Member #2,981
December 2002
avatar

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
Member #8,092
December 2006

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
Member #2,981
December 2002
avatar

Ah, okay.

a b
Member #8,092
December 2006

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
Member #5,313
December 2004
avatar

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
Member #8,092
December 2006

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

Steve Terry
Member #1,989
March 2002
avatar

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.

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

a b
Member #8,092
December 2006

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
Member #4,886
July 2004
avatar

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
Member #8,092
December 2006

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
Member #4,195
January 2004
avatar

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

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

Steve Terry
Member #1,989
March 2002
avatar

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.

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

a b
Member #8,092
December 2006

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
Member #2,604
August 2002
avatar

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.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

ngiacomelli
Member #5,114
October 2004

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
Member #2,604
August 2002
avatar

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...

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

a b
Member #8,092
December 2006

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
Member #5,114
October 2004

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.

 1   2 


Go to: