Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [A5] Odds and Ends

This thread is locked; no one can reply to it. rss feed Print
[A5] Odds and Ends
Specter Phoenix
Member #1,425
July 2001
avatar

Just seeking help and advice for these last three issues.

I've hit some issues I'm trying to figure out and needed some advice on. I can't seem to do a splash screen that will display until space bar is pressed, at which point the game starts.

Trying to get a screenshot function working based off the A5 Wiki source and wanted to see if anyone had advice on that. ( http://wiki.allegro.cc/index.php?title=Allegro_5_Screenshot )

Trying to implement a high score and debating on the format and needing advice. Should I just do the custom 3 initials with high score with top 10 to 20 high scores?

I realize my code is terrible and rand() obsessed, so try not to rip me a new one too much:

#SelectExpand
1#include "init.h" 2#include "sprite.h" 3#include "scrnshot.h" 4#include <fstream> 5#include <cstdio> 6#include <cstdlib> 7#include <ctime> 8 9const int SCREEN_H = 480; 10const int SCREEN_W = 640; 11#define SCREEN_WIDTH 640 12#define SCREEN_HEIGHT 480 13#define BLOCKSIZE 32 14 15enum MYKEYS{ KEY_SPACE, KEY_F1, KEY_F12 }; 16bool key[3] = { false, false, false }; 17 18int main() 19{ 20 /* initialize random seed: */ 21 srand(time(NULL)); 22 23 // int iSecret = rand() % 608 + 16; 24 int aiSprite = rand() % 5; 25 int stragSpr = rand() % 5; 26 int wolverSpr = rand() % 5; 27 int mcbSprite = rand() % 5; 28 int dpoolSpr = rand() % 5; 29 // int randYVel = rand() % 6 + 1; 30 31 int ticks = 0; 32 int score = 0; 33 int misses = 0; 34 35 GAME game; 36 init(&game); 37 38 SPRITE playerShip; 39 SPRITE splashScreen; 40 SPRITE help; 41 SPRITE info; 42 SPRITE aiShip, straggler, wolverine, mcbride, dpool; 43 SPRITE explosion; 44 45 splashScreen.loadSpriteSheets("data/splash.png"); 46 splashScreen.setCoord(0, 0); 47 splashScreen.setData(640, 480, 0, 0, 0); 48 49 help.loadSpriteSheets("data/help.png"); 50 help.setCoord(0, 0); 51 help.setData(640, 480, 0, 0, 0); 52 53 info.loadSpriteSheets("data/dirSheet.png"); 54 info.setCoord(512, 448); 55 info.setData(160, 32, 0, 0, 2); 56 57 playerShip.loadSpriteSheets("data/player.png"); 58 playerShip.setData(32, 32, 4, 4, 0); 59 60 61 explosion.loadSpriteSheets("data/explSheet.png"); 62 explosion.setData(32, 32, 0, 0, 3); 63 64 aiShip.loadSpriteSheets("data/shipSheet.png"); 65 straggler.loadSpriteSheets("data/shipSheet.png"); 66 wolverine.loadSpriteSheets("data/shipSheet.png"); 67 mcbride.loadSpriteSheets("data/shipSheet.png"); 68 dpool.loadSpriteSheets("data/shipSheet.png"); 69 70 aiShip.setCoord(rand() % 608 + 16, -20); 71 straggler.setCoord(rand() % 608 + 16, -20); 72 wolverine.setCoord(rand() % 608 + 16, -20); 73 mcbride.setCoord(rand() % 608 + 16, -20); 74 dpool.setCoord(rand() % 608 + 16, -20); 75 76 aiShip.setData(32, 32, 0, rand() % 6 + 1, 4); 77 straggler.setData(32, 32, 0, rand() % 6 + 1, 4); 78 wolverine.setData(32, 32, 0, rand() % 6 + 1, 4); 79 mcbride.setData(32, 32, 0, rand() % 6 + 1, 4); 80 dpool.setData(32, 32, 0, rand() % 6 + 1, 4); 81 82 aiShip.points(5); 83 straggler.points(10); 84 wolverine.points(15); 85 mcbride.points(20); 86 dpool.points(25); 87 88 al_hide_mouse_cursor(game.display); 89 90 bool redraw = true; 91 bool doexit = false; 92 93 al_set_window_title(game.display,"Global Destruction"); 94 al_set_window_position(game.display, 20, 20); 95 96 al_register_event_source(game.event_queue, al_get_display_event_source(game.display)); 97 al_register_event_source(game.event_queue, al_get_timer_event_source(game.timer)); 98 al_register_event_source(game.event_queue, al_get_keyboard_event_source()); 99 al_register_event_source(game.event_queue, al_get_mouse_event_source()); 100 101 al_clear_to_color(al_map_rgb(0, 0, 0)); 102 al_flip_display(); 103 al_start_timer(game.timer); 104 105 while (!doexit) 106 { 107 ALLEGRO_EVENT ev; 108 al_wait_for_event(game.event_queue, &ev); 109 110 111 if(ev.type == ALLEGRO_EVENT_TIMER) 112 { 113 ticks++; 114 aiShip.movement(); 115 straggler.movement(); 116 wolverine.movement(); 117 mcbride.movement(); 118 dpool.movement(); 119 120 if(Collision(playerShip, aiShip)) 121 { 122 explosion.setCoord(aiShip.getX(), aiShip.getY()); 123 explosion.animate(ticks, 4.5); 124 aiShip.setData(32, 32, 0, (rand() % 6 + 1), 4); 125 aiShip.setCoord((rand() % 608 + 16), -20); 126 aiSprite = rand() % 5; 127 al_flip_display(); 128 score = score + aiShip.getScore(); 129 } 130 131 if(aiShip.getY() > SCREEN_H) 132 { 133 aiShip.setData(32, 32, 0, (rand() % 6 + 1), 4); 134 aiShip.setCoord((rand() % 608 + 16), -20); 135 al_flip_display(); 136 aiSprite = rand() % 5; 137 misses++; 138 } 139 140 if(Collision(playerShip, straggler)) 141 { 142 explosion.setCoord(straggler.getX(), straggler.getY()); 143 explosion.animate(ticks, 4.5); 144 straggler.setData(32, 32, 0, (rand() % 6 + 1), 4); 145 straggler.setCoord((rand() % 608 + 16), -20); 146 stragSpr = rand() % 5; 147 al_flip_display(); 148 score = score + straggler.getScore(); 149 } 150 151 if(straggler.getY() > SCREEN_H) 152 { 153 straggler.setData(32, 32, 0, (rand() % 6 + 1), 4); 154 straggler.setCoord((rand() % 608 + 16), -20); 155 al_flip_display(); 156 stragSpr = rand() % 5; 157 misses++; 158 } 159 160 if(Collision(playerShip, wolverine)) 161 { 162 explosion.setCoord(wolverine.getX(), wolverine.getY()); 163 explosion.animate(ticks, 4.5); 164 wolverine.setData(32, 32, 0, (rand() % 6 + 1), 4); 165 wolverine.setCoord((rand() % 608 + 16), -20); 166 wolverSpr = rand() % 5; 167 al_flip_display(); 168 score = score + wolverine.getScore(); 169 } 170 171 if(wolverine.getY() > SCREEN_H) 172 { 173 wolverine.setData(32, 32, 0, (rand() % 6 + 1), 4); 174 wolverine.setCoord((rand() % 608 + 16), -20); 175 al_flip_display(); 176 wolverSpr = rand() % 5; 177 misses++; 178 } 179 180 if(Collision(playerShip, mcbride)) 181 { 182 explosion.setCoord(mcbride.getX(), mcbride.getY()); 183 explosion.animate(ticks, 4.5); 184 mcbride.setData(32, 32, 0, (rand() % 6 + 1), 4); 185 mcbride.setCoord((rand() % 608 + 16), -20); 186 mcbSprite = rand() % 5; 187 al_flip_display(); 188 score = score + mcbride.getScore(); 189 } 190 191 if(mcbride.getY() > SCREEN_H) 192 { 193 mcbride.setData(32, 32, 0, (rand() % 6 + 1), 4); 194 mcbride.setCoord((rand() % 608 + 16), -20); 195 al_flip_display(); 196 mcbSprite = rand() % 5; 197 misses++; 198 } 199 200 if(Collision(playerShip, dpool)) 201 { 202 explosion.setCoord(dpool.getX(), dpool.getY()); 203 explosion.animate(ticks, 4.5); 204 dpool.setData(32, 32, 0, (rand() % 6 + 1), 4); 205 dpool.setCoord((rand() % 608 + 16), -20); 206 dpoolSpr = rand() % 5; 207 al_flip_display(); 208 score = score + dpool.getScore(); 209 } 210 211 if(dpool.getY() > SCREEN_H) 212 { 213 dpool.setData(32, 32, 0, (rand() % 6 + 1), 4); 214 dpool.setCoord((rand() % 608 + 16), -20); 215 al_flip_display(); 216 dpoolSpr = rand() % 5; 217 misses++; 218 } 219 220 if(misses == 10) 221 doexit = true; 222 223 redraw = true; 224 } 225 else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 226 { 227 break; 228 } 229 else if(ev.type == ALLEGRO_EVENT_MOUSE_AXES || 230 ev.type == ALLEGRO_EVENT_MOUSE_ENTER_DISPLAY) 231 { 232 playerShip.setCoord(ev.mouse.x - 16, ev.mouse.y - 16); 233 } 234 else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) 235 { 236 switch(ev.keyboard.keycode) 237 { 238 case ALLEGRO_KEY_SPACE: 239 key[KEY_SPACE] = true; 240 break; 241 case ALLEGRO_KEY_F1: 242 key[KEY_F1] = true; 243 break; 244 case ALLEGRO_KEY_F12: 245 key[KEY_F12] = true; 246 break; 247 } 248 } 249 else if(ev.type == ALLEGRO_EVENT_KEY_UP) 250 { 251 switch(ev.keyboard.keycode) 252 { 253 case ALLEGRO_KEY_SPACE: 254 key[KEY_SPACE] = false; 255 break; 256 case ALLEGRO_KEY_F1: 257 key[KEY_F1] = false; 258 break; 259 case ALLEGRO_KEY_F12: 260 key[KEY_F12] = false; 261 break; 262 case ALLEGRO_KEY_ESCAPE: 263 doexit = true; 264 break; 265 } 266 } 267 268 if(redraw && al_is_event_queue_empty(game.event_queue)) 269 { 270 271 redraw = false; 272 al_clear_to_color(al_map_rgb(0, 0, 0)); 273 274 playerShip.draw(); 275 aiShip.randDraw(aiSprite); 276 straggler.randDraw(stragSpr); 277 wolverine.randDraw(wolverSpr); 278 mcbride.randDraw(mcbSprite); 279 dpool.randDraw(dpoolSpr); 280 281 // draw score over everything!!! 282 al_draw_textf(game.font, al_map_rgb(255, 255, 255), SCREEN_W / 2, SCREEN_H - 20, ALLEGRO_ALIGN_CENTER, "%i", score); 283 284 al_flip_display(); 285 } 286 } 287 288 al_clear_to_color(al_map_rgb(0, 0, 0)); 289 al_flip_display(); 290 al_destroy_display(game.display); 291 292 return 0; 293}

"Can't a man even talk to himself without being interrupted?" -Krull(1983)
"Through vengence I was born. Through war I was trained. Through love I was found. Through death I was released. Through release I was given a purpose." -- Specter Phoenix
"Programming == AWESOME the rest is just tools to accomplish it."
END OF LINE

Edgar Reynaldo
Member #8,592
May 2007
avatar

Trent Gamblin
Member #261
April 2000
avatar

The simplest possible way to do a splash screen like that is like this (Oh come on, best practices don't really matter for such a small issue! But if you wanted to extend this somehow it would be bad...)

for (;;) {
   al_draw_bitmap(splash, 0, 0, 0);
   al_flip_display();
   ALLEGRO_KEYBOARD_STATE kbstate;
   al_get_keyboard_state(&kbstate);
   if (al_key_down(&kbstate, ALLEGRO_KEY_SPACE)) {
      break;
   }
   al_rest(1.0/60.0);
}

Kris Asick
Member #1,424
July 2001

Hmm... well, the way I code my games is to have a "system" variable which tracks which primary system to run code through. If the system variable changes, then the next time there's a system loop it will run completely different code. For a splash screen, the simplest thing to do is create an entirely separate code section just for it, then jump to your game code following. At the moment, everything you do is in main(), whereas my main() simply calls init code then runs a while() + switch() loop, accessing various system functions depending on the system variable state until I set the system variable to a shutdown state, which exits the while() loop and calls all the shutdown functions.

Kinda like this pseudocode:

while (system_state != SYS_SHUTDOWN) switch (system_state)
{
    case SYS_SPLASHSCREEN: RunSplashScreen(); break;
    case SYS_MAINMENU:     RunMainMenu(); break;
    case SYS_OPTIONSMENU:  RunOptions(); break;
    case SYS_RUNGAME:      RunGame(); break;
}

For a screenshot function, just keep a numerical variable going to add onto your screenshot's filename and advance it every time you save a screen. Before you actually save the screenshot, check if the file exists. If it does, add the variable up by 1 and check again. Most screenshot functions are attached to the F12 Key, though the Print Screen key works too, keeping in mind that Windows will take a screenshot itself and put it in the clipboard when you press that key.

As for high scores, this is going to depend on a number of factors. If you want your game to have joystick/gamepad support you should show a screen of letter selectors that you can move through with the joystick/gamepad, or even the arrow keys on the keyboard, though you should still detect actual alphanumeric keypresses. I'd only go with initials if you intend this game to feel like an arcade game, otherwise have at least 8 characters allowed. Top 20 takes a lot of screen space and can actually look less impressive than a Top 10, but this is more a matter of preference really. ;)

--- Kris Asick (Gemini)
--- http://www.pixelships.com

Specter Phoenix
Member #1,425
July 2001
avatar

Well I tried this method to no avail:

for(;;)
{
     splashScreen.draw();
     info.animate(ticks, 2);
     al_flip_display();
     if(key[KEY_SPACE])
           break;
}

playerShip.draw();
// ...draw other ships

Tried Trentg's code and it just made it so the button had to be held to play so it flashed. Could be putting the code in the wrong spot or missing something logic wise.

"Can't a man even talk to himself without being interrupted?" -Krull(1983)
"Through vengence I was born. Through war I was trained. Through love I was found. Through death I was released. Through release I was given a purpose." -- Specter Phoenix
"Programming == AWESOME the rest is just tools to accomplish it."
END OF LINE

Go to: