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

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

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

-------
Sweden: Free from the shackles of Democracy since 2008-06-18!

a b
Member #8,092
December 2006

text output<<eye it doesn't run

TotalControl
Member #8,078
December 2006

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

Archon
Member #4,195
January 2004
avatar

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

But in Allegro, use textprintf_ex

a b
Member #8,092
December 2006

OH noooo !!!

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

Jonatan Hedborg
Member #4,886
July 2004
avatar

What does that even mean?

Allegro is a C library.

-------
Sweden: Free from the shackles of Democracy since 2008-06-18!

Archon
Member #4,195
January 2004
avatar

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

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

C++ is backwards compatible with C.

GullRaDriel
Member #3,861
September 2003
avatar

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

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

a b
Member #8,092
December 2006

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

Jonatan Hedborg
Member #4,886
July 2004
avatar

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

-------
Sweden: Free from the shackles of Democracy since 2008-06-18!

a b
Member #8,092
December 2006

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

Archon
Member #4,195
January 2004
avatar

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
Member #1,565
October 2001
avatar

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

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

a b
Member #8,092
December 2006

thanks you

GullRaDriel
Member #3,861
September 2003
avatar

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

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

a b
Member #8,092
December 2006

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

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.

(\ /)_____#_____####__#
(O.o)¯¯¯#¯¯¯¯¯#¯¯¯#¯¯#
(> <)__####__####__####
Megabytes are where I keep my Data.

a b
Member #8,092
December 2006

thanks, but can't I clearing screen easier ?

m c
Member #5,337
December 2004
avatar

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.

(\ /)_____#_____####__#
(O.o)¯¯¯#¯¯¯¯¯#¯¯¯#¯¯#
(> <)__####__####__####
Megabytes are where I keep my Data.

Archon
Member #4,195
January 2004
avatar

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

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
Member #856
December 2000
avatar

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

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

 1   2 


Go to: