Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Problem creating a bitmap, and a class

Credits go to CGamesPlay and X-G for helping out!
This thread is locked; no one can reply to it. rss feed Print
Problem creating a bitmap, and a class
Paul Hoey
Member #7,189
May 2006

DATAFILE *data = NULL;
!**!BITMAP *bg = NULL;
    
//a function to initalise some resources
void init_data()
{
    data = load_datafile("bboy.dat");
    bg = (BITMAP*)data[bg].dat; 
}

The line with the !**! in front of it gives the following error:
expected unqualified-id before numeric constant
expected init-declarator before numeric constant
expected `,' or `;' before numeric constant

Same with this code:

1//the ball class
2class ball
3{
4 public:
5 int ballX;
6 int ballY;
7 BITMAP *ballImage;
8 public:
9 void init(int x, int y);
10 void drawBall(BITMAP *method_buffer);
11};
12 
13void ball::init(int x, int y)
14{
15 ballX = x;
16 ballY = y;
17 ballImage = (BITMAP*)data[ball].dat;
18};

Any ideas on the problem?

Jonatan Hedborg
Member #4,886
July 2004
avatar

Post the whole file. Probably some syntactical error earlier.

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

X-G
Member #856
December 2000
avatar

ballImage = (BITMAP*)data[ball].dat;

... and you also have a class named "ball". Name conflict. :P

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

Paul Hoey
Member #7,189
May 2006

Why would there be a conflict if I'm loading it form a data file, though?

Here's the whole file:

1//this is to use allegro
2#include <allegro.h>
3#include ".\bboy_head.h"
4 
5volatile long speed_counter = 0; // A long integer which will store the value of the
6 // speed counter.
7 
8void increment_speed_counter() // A function to increment the speed counter
9{
10 speed_counter++; // This will just increment the speed counter by one. :)
11}
12END_OF_FUNCTION(increment_speed_counter); // Make sure you tell it that it's the end of the
13 // function
14DATAFILE *data = NULL;
15//BITMAP *bg = NULL;
16 
17//a function to initalise some resources
18void init_data()
19{
20 data = load_datafile("bboy.dat");
21 //bg = (BITMAP*)data[bg].dat;
22};
23
24//the player class
25class player
26{
27 //to store the image and the cooards
28 public:
29 int playerX;
30 int playerY;
31 int dir; //1 = right, 2 = left
32 int state; //0 = idle, 1 = run, 2 = jump
33 int constantY;
34 int velocity;
35 bool maxJump;
36 BITMAP *player_image;
37 BITMAP *player_idle_r;
38 BITMAP *player_run_r;
39 BITMAP *player_jump_r;
40
41 BITMAP *player_idle_l;
42 BITMAP *player_run_l;
43 BITMAP *player_jump_l;
44
45 //a few methods we'll be needing
46 public:
47 void move_left(); //moves the player left
48 void move_right(); //moves the player right
49 void draw_player(BITMAP *method_buffer); //just takes in a buffer that th epalyer is drawn to
50 void init(int x, int y); //initilises the player values
51 //void throwBall(int x, int y, ball temp);
52 void jump();
53 //void throwBall(int x, int y, ball temp);
54};
55 
56void player::init(int x, int y)
57{
58 playerX = x;
59 playerY = y;
60 constantY = playerY;
61 velocity = 2;
62 maxJump = false;
63 player_idle_r = (BITMAP*)data[idle_r].dat;
64 player_run_r = (BITMAP*)data[run_r].dat;
65 player_jump_r = (BITMAP*)data[jump_r].dat;
66
67 player_idle_l = (BITMAP*)data[idle_l].dat;
68 player_run_l = (BITMAP*)data[run_l].dat;
69 player_jump_l = (BITMAP*)data[jump_l].dat;
70
71 dir = 1;
72 state = 0;
73
74 player_image = player_idle_r;
75};
76 
77void player::draw_player(BITMAP *method_buffer)
78{
79 if(dir == 1)
80 {
81 //direction is right, so we'll be using the right images
82 if(state == 0)
83 {
84 player_image = player_idle_r;
85 }
86 else
87 if(state == 1)
88 {
89 player_image = player_run_r;
90 }
91 else
92 {
93 player_image = player_jump_r;
94 }
95 }
96 else
97 if(dir == 2)
98 {
99 //direction is left, so we'll be using the left images
100 if(state == 0)
101 {
102 player_image = player_idle_l;
103 }
104 else
105 if(state == 1)
106 {
107 player_image = player_run_l;
108 }
109 else
110 {
111 player_image = player_jump_l;
112 }
113 }
114 draw_sprite(method_buffer, player_image, playerX, playerY);
115};
116 
117void player::move_left()
118{
119 playerX--;
120 dir = 2;
121 state = 1;
122};
123 
124void player::move_right()
125{
126 playerX++;
127 dir = 1;
128 state = 1;
129};
130 
131void player::jump()
132{
133 state = 2;
134 if(maxJump == false)
135 {
136 playerY--;
137
138 if(playerY <= constantY - 40)
139 {
140 maxJump = true;
141 }
142 }
143 else
144 if(maxJump == true)
145 {
146 playerY++;
147
148 if(playerY >= constantY)
149 {
150 maxJump = false;
151 }
152 }
153
154};
155
156/*
157void player::throwBall(int x, int y, ball temp)
158{
159
160};
161
162//the ball class
163class ball
164{
165 public:
166 int ballX;
167 int ballY;
168 BITMAP *ballImage;
169 public:
170 void init(int x, int y);
171 void drawBall(BITMAP *method_buffer);
172};
173
174void ball::init(int x, int y)
175{
176 ballX = x;
177 ballY = y;
178 ballImage = (BITMAP*)data[ball].dat;
179};
180*/
181//our main method
182int main(void)
183{
184 //this initilises Allegro
185 if(allegro_init() != 0)
186 {
187 return 1;
188 }
189
190 //set the colour depth
191 set_color_depth(24);
192
193 //here we set up a new window to use
194 //the first parameter is the driver we're using
195 //the next 2 specify height and width
196 //the last 2 are for memory allocation
197 if (set_gfx_mode(GFX_AUTODETECT, 800, 600, 0, 0) != 0)
198 {
199 //should te above fail, we want to try and set it using SAFE mode
200 if(set_gfx_mode(GFX_SAFE, 800, 600, 0, 0) != 0)
201 {
202 //and if that fails we want to tell the suer why the program will not work
203 set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
204 allegro_message("Unable to initiate any graphics mode!\n$s\n", allegro_error);
205 return 1;
206 }
207 }
208
209 //clear the screen to be entirely white
210 clear_to_color(screen, makecol(255, 255, 255));
211
212 //this installs the keyboard driver so we can use it
213 install_keyboard();
214
215 //this installs the mouse
216 install_mouse();
217
218 //installing the timer routines
219 install_timer();
220
221 //creating a timer
222 install_int_ex(increment_speed_counter, BPS_TO_TIMER(60));
223
224 //declaring & creating the buffer
225 BITMAP *buffer = create_bitmap(800, 600);
226
227 //init our data
228 init_data();
229
230 player bboy;
231
232 bboy.init(400, 200);
233
234 //the mosue cursor
235 //BITMAP *cursor;
236
237 //cursor = (BITMAP*)data[mouse_cursor].dat;
238
239 while(!key[KEY_ESC])
240 {
241 if(key[KEY_A])
242 {
243 bboy.move_left();
244 }
245 else
246 if(key[KEY_D])
247 {
248 bboy.move_right();
249 }
250 else
251 {
252 bboy.state = 0;
253 }
254 
255 if(key[KEY_SPACE])
256 {
257 bboy.jump();
258 }
259 else
260 {
261 bboy.velocity = 2;
262 }
263
264 //draw_sprite(buffer, bg, 0, 0);
265 bboy.draw_player(buffer);
266
267 textout_ex(buffer, font, "WASD to move", 10, 10, makecol(255, 255, 255), -1);
268
269 //drawing the mouse cursor to the screen
270 //draw_sprite(buffer, cursor, mouse_x, mouse_y);
271 //drawing the buffers
272 blit(buffer, screen, 0, 0, 0, 0, 800, 600);
273 //clearing the buffer
274 clear_bitmap(buffer);
275 }
276
277 //now we wait for a key press
278 readkey();
279 destroy_bitmap(buffer);
280 //unload_datafile(falling);
281 return 0;
282}
283 
284//we have to call this when using Allegro
285END_OF_MAIN()

CGamesPlay
Member #2,559
July 2002
avatar

When you name a datafile object like that, grabber makes a header file like this:

#define ball 1

That means that wherever the word "ball" appears in your source code, the compiler changes it to a "1". So, your code looks like this:

class 1
{
...

Well, obviously that isn't valid. That is why we typically name datafile objects in all capital letters, because we don't name variables and classes in all capital letters.

So, the answer is to change the name of the object in the datafile to something like "DATAFILE_BALL" instead of "ball".

[append]
The same goes for bg and, well, anything else in the data file.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Paul Hoey
Member #7,189
May 2006

Ah, okay. Thank you.

Audric
Member #907
January 2001

You can automate half of the changes:

allegro manual said:

To prevent name conflicts, you can specify a prefix string for these definitions by typing it into the "Prefix:" field in the grabber or using the '-p' option to dat

ie: if you use a prefix like "datafile_item_", the generated header file will automatically be #define datafile_item_ball 1
You'll still have to change the "calls" yourself, anyway: ballImage = (BITMAP*)data[datafile_item_ball].dat;

Tobias Dammers
Member #2,604
August 2002
avatar

Quote:

So, the answer is to change the name of the object in the datafile to something like "DATAFILE_BALL" instead of "ball".

Another option (which I tend to use) is keeping the file extensions when adding objects to the datafile. The dat tool does this by default, so the following command:

dat mydatafile.dat -a foo.bmp -t BMP

...will add a bitmap object called "FOO_BMP" to the datafile (letters are automatically uppercased, and all non-alphanumeric characters are converted to underscores). Since your identifiers don't usually end with _BMP or _PCX or _WAV or whatever extension you use, this works rather well.

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

Dustin Dettmer
Member #3,935
October 2003
avatar

Now try naming the new entry something besides foo.bmp

Ya, ya thats right, you can't do it!

Go to: