123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- #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<SDL_Rect> 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<NB_GAMERS; t++ )
- for ( int j(0); j < 5 && running; j++ )
- {
- x[t]++;
- if ( up[t] ) y[t]--;
- else if ( down[t] ) y[t]++;
- if ( y[t] < 0 ) y[t] = h-1;
- if ( y[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<NB_GAMERS; t++ )
- {
- x[t] = 0;
- y[t] = GAMERS_POS;
- }
- }
- /// -- Buffer
- /// calculs
- i++;
- for ( int t(0); t<NB_GAMERS; t++ )
- if ( !alive[t] )
- stringRGBA(buffer, x[t]+8, y[t]+8, "GAME OVER", r/3, g/3, b/3, 255);
- if ( i > 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<NB_GAMERS; t++ )
- if (x[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<NB_GAMERS; t++ )
- if ( !alive[t] )
- stringRGBA(buffer, x[t]+6, y[t]+6, "GAME OVER", 255, 4, 23, 255);
- /// past buffer
- SDL_BlitSurface( buffer, 0, screen, 0 );
- /// fps count
- fdis_count++;
- if ( SDL_GetTicks() - fdis_time > 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;
- }
|