|
Bitmap behind mappy map |
PCwizard200
Member #16,127
December 2015
|
Hy everyone. When drawing my tile map I made in mappy and then drawing my player image on the screen also, I relized that the player image was behind the tile map. So in order to fix this problem is there a command in allegro 5 that I can use to fix this problem. The tutorial series in which I learned allegro from on fixbyproximity.com only showed the command to draw the map really. source code just in case just keep in mind the project is def not finished 1//This is the source code for my game in allegro 5, The game is called MENACER and It is a work in progress
2//I want this game to look decent and be fun to play, so it will probably take a while for me to get it finished
3//don't use this source code without my permission, Thanks.
4
5#include <allegro5/allegro.h>
6#include <allegro5/allegro_image.h> //including our allegro headers here
7#include <allegro5/allegro_ttf.h>
8#include <allegro5/allegro_font.h>
9#include <allegro5/allegro_primitives.h>
10#include <allegro5/allegro_acodec.h>
11#include <allegro5/allegro_audio.h>
12#include <allegro5/allegro_native_dialog.h>
13#include <iostream>
14#include <math.h>
15#include "mappy_A5.h"
16
17const int WIDTH = 800; //Defining my width of the game window and height
18const int HEIGHT = 600;
19
20bool keys[] = {false, false, false, false, false, false, false, false}; //declare the controls we are using for my game
21enum KEYS{UP, DOWN, LEFT, RIGHT, SPACE, C, F, ESCAPE};
22
23enum STATE{MENU, PLAY, GAMEOVER, QUIT}; //game state declaration
24
25void ChangeState(int &state, int newState); //change state void declaration
26
27//THE MAIN PROGRAM IS HERE!
28int main(void){
29
30//Game variables!
31bool done = false;
32bool render = false;
33
34float gameTime = 0;
35int frames = 0;
36int gameFPS = 0;
37
38int state = -1;
39
40int xOff = 0;
41int yOff = 0;
42
43//ALLEGRO VARIABLES!
44ALLEGRO_DISPLAY *display = NULL;
45ALLEGRO_EVENT_QUEUE *event_queue = NULL;
46ALLEGRO_TIMER *timer;
47ALLEGRO_FONT *font10;
48ALLEGRO_BITMAP *MenuImage = NULL;
49ALLEGRO_BITMAP *MenacerTank = NULL;
50ALLEGRO_SAMPLE *MenuTheme = NULL;
51ALLEGRO_SAMPLE *LV1music = NULL;
52ALLEGRO_SAMPLE_INSTANCE *instance1 = NULL;
53ALLEGRO_SAMPLE_INSTANCE *instance2 = NULL;
54
55if (!al_init())
56return -1;
57
58display = al_create_display(WIDTH, HEIGHT); //initiate the display
59
60
61if(!display)
62return -1;
63
64//ADDON INSTALL
65al_install_keyboard();
66al_init_image_addon();
67al_init_font_addon();
68al_init_ttf_addon();
69al_init_primitives_addon();
70al_install_audio();
71al_init_acodec_addon();
72
73al_reserve_samples(2);
74
75if (MapLoad("level 1.FMP",1))
76return -5;
77
78//loading game assets
79font10 = al_load_font("atari.ttf", 24, 0);
80MenuImage = al_load_bitmap("menu art.png");
81MenacerTank = al_load_bitmap("tank.png");
82MenuTheme = al_load_sample("Robotech - Rick Hunter's Theme.ogg");
83LV1music = al_load_sample("raiden 1.ogg");
84
85instance1 = al_create_sample_instance(MenuTheme);
86instance2 = al_create_sample_instance(LV1music);
87
88al_attach_sample_instance_to_mixer(instance1, al_get_default_mixer());
89al_attach_sample_instance_to_mixer(instance2, al_get_default_mixer());
90
91ChangeState(state, MENU); //THE FIRST STATE MY GAME WILL GO TO IS THE MENU STATE
92
93
94event_queue = al_create_event_queue();
95timer = al_create_timer(1.0 / 60);
96
97al_register_event_source(event_queue, al_get_timer_event_source(timer));
98al_register_event_source(event_queue, al_get_keyboard_event_source());
99
100
101al_start_timer(timer);
102gameTime = al_current_time();
103while(!done){
104
105 ALLEGRO_EVENT ev;
106 al_wait_for_event(event_queue, &ev);
107
108 //INPUT FOR MY GAME IS HERE
109 if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
110{
111 switch (ev.keyboard.keycode)
112 {
113 case ALLEGRO_KEY_ESCAPE:
114 keys[ESCAPE] = true;
115 break;
116 case ALLEGRO_KEY_SPACE:
117 keys[SPACE] = true;
118 break;
119 case ALLEGRO_KEY_C:
120 keys[C] = true;
121 break;
122 case ALLEGRO_KEY_F:
123 keys[F] = true;
124 break;
125 case ALLEGRO_KEY_UP:
126 keys[UP] = true;
127 break;
128 case ALLEGRO_KEY_DOWN:
129 keys[DOWN] = true;
130 break;
131 case ALLEGRO_KEY_LEFT:
132 keys[LEFT] = true;
133 break;
134 case ALLEGRO_KEY_RIGHT:
135 keys[RIGHT] = true;
136 break;
137 }
138
139
140}
141
142if (ev.type == ALLEGRO_EVENT_KEY_UP)
143{
144 switch(ev.keyboard.keycode)
145 {
146 case ALLEGRO_KEY_ESCAPE:
147 keys[ESCAPE] = false;
148 break;
149 case ALLEGRO_KEY_SPACE:
150 keys[SPACE] = false;
151 break;
152 case ALLEGRO_KEY_C:
153 keys[C] = false;
154 break;
155 case ALLEGRO_KEY_F:
156 keys[F] = false;
157 break;
158 case ALLEGRO_KEY_UP:
159 keys[UP] = false;
160 break;
161 case ALLEGRO_KEY_DOWN:
162 keys[DOWN] = false;
163 break;
164 case ALLEGRO_KEY_LEFT:
165 keys[LEFT] = false;
166 break;
167 case ALLEGRO_KEY_RIGHT:
168 keys[RIGHT] = false;
169 break;
170 }
171
172
173}
174
175else if (ev.type == ALLEGRO_EVENT_TIMER)
176{
177 render = true;
178 //UPDATE FPS
179 frames++;
180 if(al_current_time() - gameTime >= 1)
181 {
182 gameTime = al_current_time();
183 gameFPS = frames;
184 frames = 0;
185 }
186
187if (state == MENU) //Game State Controls
188{
189 if(keys[SPACE])
190 ChangeState(state, PLAY);
191 else if(keys[ESCAPE])
192 ChangeState(state, QUIT);
193}
194
195
196
197else if (state == PLAY)
198{
199if(keys[ESCAPE])
200 ChangeState(state, GAMEOVER);
201}
202
203else if(state == GAMEOVER)
204{
205 if(keys[SPACE])
206 ChangeState(state, MENU);
207}
208
209xOff += keys[RIGHT] * 10;
210xOff -= keys[LEFT] * 10;
211yOff += keys[DOWN] * 10;
212yOff -= keys[UP] * 10;
213
214if(xOff < 0)
215xOff = 0;
216if(yOff < 0)
217 yOff = 0;
218if(xOff > (mapwidth * mapblockwidth - WIDTH))
219 xOff = mapwidth * mapblockwidth - WIDTH;
220if(yOff > (mapheight * mapblockheight - HEIGHT))
221 yOff = mapheight * mapblockheight - HEIGHT;
222}
223
224
225//Here we render everything
226
227if(render && al_is_event_queue_empty(event_queue))
228{
229
230render = true;
231
232//BEGIN!
233
234 if (state == MENU)
235{
236al_draw_bitmap(MenuImage, 0,0,0);
237al_set_sample_instance_playmode(instance1, ALLEGRO_PLAYMODE_LOOP);
238al_play_sample_instance(instance1);
239}
240
241else if(state == PLAY)
242{
243
244 al_draw_bitmap(MenacerTank,200,100,0);
245 al_stop_sample_instance(instance1);
246 MapDrawBG(xOff,yOff,0,0, WIDTH, HEIGHT);
247
248
249}
250
251
252
253
254
255
256
257
258else if(state == QUIT)
259{
260 done = true;
261}
262
263
264
265
266
267
268
269
270
271
272al_flip_display();
273al_clear_to_color(al_map_rgb(0,0,0));
274}
275
276
277
278
279
280
281
282
283
284}
285
286//DESTROY PROJECT OBJECTS
287
288al_destroy_font(font10);
289al_destroy_bitmap(MenuImage);
290al_destroy_timer(timer);
291al_destroy_display(display);
292al_destroy_event_queue(event_queue);
293al_destroy_sample(MenuTheme);
294al_destroy_sample(LV1music);
295al_destroy_sample_instance(instance1);
296al_destroy_sample_instance(instance2);
297
298MapFreeMem();
299
300return 0;
301}
302
303void ChangeState(int &state, int newState)
304{
305
306 if (state == MENU)
307 {
308 std::cout << "Leaving the MENU state \n";
309 }
310 else if (state == PLAY)
311{
312 std::cout <<"Leaving the PLAY state \n";
313}
314else if (state == GAMEOVER)
315{
316 std::cout << "Leaving the GAMEOVER state \n";
317}
318else if (state == QUIT)
319{
320 std::cout << "Leaving the QUIT state \n";
321}
322
323state = newState;
324
325
326 if (state == MENU)
327 {
328 std::cout << "Entering the MENU state \n";
329 }
330 else if (state == PLAY)
331{
332 std::cout <<"Entering the PLAY state \n";
333}
334else if (state == GAMEOVER)
335{
336 std::cout << "Entering the GAMEOVER state \n";
337}
338else if (state == QUIT)
339{
340 std::cout << "Entering the QUIT state \n";
341}
342
343
344}
|
Eric Johnson
Member #14,841
January 2013
|
Call the function to draw the map before drawing anything else, then draw the player. else if(state == PLAY) { MapDrawBG(xOff,yOff,0,0, WIDTH, HEIGHT); // Draw map first. al_draw_bitmap(MenacerTank,200,100,0); al_stop_sample_instance(instance1); } Every time you draw something, it goes above whatever was previously drawn. So think of it as going from the background to the foreground. If you were to draw bitmaps A, B, and then C, you would have A in the very back, followed by B, and then C in the very front.
|
PCwizard200
Member #16,127
December 2015
|
person said: Eric Johnson: Call the function to draw the map before drawing anything else, then draw the player. else if(state == PLAY) MapDrawBG(xOff,yOff,0,0, WIDTH, HEIGHT); // Draw map first. } Every time you draw something, it goes above whatever was previously drawn. So think of it as going from the background to the foreground. If you were to draw bitmaps A, B, and then C, you would have A in the very back, followed by B, and then C in the very front. Thanks Eric Johnson I honestly did not know that you have to put images in a certain order according to how you want it to look. Ya learn something every day!;D
|
Eric Johnson
Member #14,841
January 2013
|
You're welcome. I'm glad I could help.
|
|