#include "spawn.h" #include "intToStr.h" #define NB_GAMERS 2 #define NB_GROW 6 #define NB_BASE 10 #define NEXT_ROUND spawn( &obs, NB_BASE+lvl*NB_GROW, w, h ) #define GAMERS_POS (1+t)*(h/(1+NB_GAMERS)) int main ( int argc, char** argv ) { /// initialize SDL video if ( SDL_Init( SDL_INIT_VIDEO ) < 0 ) { printf( "Unable to init SDL: %s\n", SDL_GetError() ); return 1; } SDL_ShowCursor( SDL_DISABLE ); /// make sure SDL cleans up before exit atexit(SDL_Quit); /// init random list srand( time(0) ); /// create a new window Uint16 w, h; SDL_Surface *buffer(0x0), *screen(0x0); { // spare useless variables const SDL_VideoInfo* fenetreInfo = SDL_GetVideoInfo(); w = fenetreInfo->current_w; h = fenetreInfo->current_h; } screen = SDL_SetVideoMode(w, h, 32, SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_FULLSCREEN); std::cout << "Set " << w << 'x' << h << " video." << std::endl; if ( !screen ) { std::cout << "Unable to set video " << SDL_GetError() << std::endl; return 1; } else { buffer = SDL_CreateRGBSurface( SDL_HWSURFACE, w, h, 32, 0, 0, 0, 0 ); } /// variables const Uint32 delay(18); Uint32 tps( SDL_GetTicks() ), tps_old(0); Uint32 fdis_time(0), fdis_count(0), fdis_rslt(0); bool alive[NB_GAMERS]; bool running; int lvl( 1 ); /// graph int i(0), x[NB_GAMERS], y[NB_GAMERS]; Uint8 r(0), g(255), b(255); bool up[NB_GAMERS], down[NB_GAMERS]; /// init for ( int t(0); t < NB_GAMERS; t++ ) { alive[t] = true; x[t] = -1; y[t] = GAMERS_POS; up[t] = down[t] = false; } /// obstacles std::vector obs(0); NEXT_ROUND; /// program main loop bool done = false; while (!done) { // message processing loop SDL_Event event; while (SDL_PollEvent(&event)) { // check for messages switch (event.type) { // exit if the window is closed case SDL_QUIT: done = true; break; // check for keypresses case SDL_KEYDOWN: // exit if ESCAPE is pressed switch ( event.key.keysym.sym ) { case SDLK_ESCAPE: done = true; break; case SDLK_UP: case SDLK_LEFT: up[0] = true; break; case SDLK_DOWN: case SDLK_RIGHT: down[0] = true; break; case SDLK_BACKSPACE: alive[0] = true; x[0] = w+1; lvl = 0; #if NB_GAMERS > 1 alive[1] = true; x[1] = w+1; break; case SDLK_w: case SDLK_a: up[1] = true; break; case SDLK_s: case SDLK_d: down[1] = true; break; #endif default: break; } break; case SDL_KEYUP: switch ( event.key.keysym.sym ) { case SDLK_UP: case SDLK_LEFT: up[0] = false; break; case SDLK_DOWN: case SDLK_RIGHT: down[0] = false; break; #if NB_GAMERS > 1 case SDLK_w: case SDLK_a: up[1] = false; break; case SDLK_s: case SDLK_d: down[1] = false; break; #endif default: break; } break; } // end switch event tye } // end of message processing // color computing if ( i < 256 ) { r = i; g = 255 - i%256; // b = 255 } else if ( i < 512 ) { //r = 255 g = i%256; b = 255 - i%256; } else if ( i < 768 ) { r = 255 - i%256; //g = 255 b = i%256; } // still run ? running = alive[0]; #if NB_GAMERS > 1 running &= alive[1]; #endif // NB_GAMERS // direction for ( int t(0); t= h ) y[t] = 0; if ( t == 0) pixelRGBA(buffer, x[0], y[0], r, g, b, 255); else pixelRGBA(buffer, x[1], y[1], g, b, r, 255); } // next round if ( x[0] > w ) { lvl++; obs.clear(); NEXT_ROUND; SDL_FillRect( buffer, 0, 0x000000 ); for ( int t(0); t 767 ) i = 0; /// draw obstacles for ( Uint16 j(0); j < obs.size(); j++ ) { SDL_FillRect( buffer, &obs[j], b + (g << 8) + (r << 16) ); for ( int t(0); t obs[j].x && x[t] < obs[j].x + obs[j].w && y[t] > obs[j].y && y[t] < obs[j].y + obs[j].h) alive[t] = false; } stringRGBA(buffer, 12, 12, ("Level "+intToStr( lvl )).c_str(), r, g, b, 255); for ( int t(0); t 1000) { fdis_rslt = fdis_count; fdis_count = 0; fdis_time = SDL_GetTicks(); } stringRGBA(screen, 12, 24, ("Fps: "+intToStr( fdis_rslt )).c_str(), r, g, b, 255); /// finally, update the screen :) SDL_Flip(screen); /// time management do { tps = SDL_GetTicks(); } while ( tps-tps_old < delay ); tps_old = tps; } // end main loop /// all is well ;) std::cout << "Exited cleanly." << std::endl; return 0; }