コード例 #1
0
int main( int argc, char* args[] )
{
	//Start up SDL and create window
	if( !init() )
	{
		printf( "Failed to initialize!\n" );
	}
	else
	{
		//Load media
		if( !loadMedia() )
		{
			printf( "Failed to load media!\n" );
		}
		else
		{	
			//Main loop flag
			bool quit = false;

			//Event handler
			SDL_Event e;

			//The dot that will be moving around on the screen
			Dot dot;

			//Keeps track of time between steps
			LTimer stepTimer;

			//While application is running
			while( !quit )
			{
				//Handle events on queue
				while( SDL_PollEvent( &e ) != 0 )
				{
					//User requests quit
					if( e.type == SDL_QUIT )
					{
						quit = true;
					}

					//Handle input for the dot
					dot.handleEvent( e );
				}

				//Calculate time step
				float timeStep = stepTimer.getTicks() / 1000.f;

				//Move for time step
				dot.move( timeStep );

				//Restart step timer
				stepTimer.start();

				//Clear screen
				SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
				SDL_RenderClear( gRenderer );

				//Render dot
				dot.render();

				//Update screen
				SDL_RenderPresent( gRenderer );
			}
		}
	}

	//Free resources and close SDL
	close();

	return 0;
}
コード例 #2
0
ファイル: sdl26.cpp プロジェクト: osom8979/example
int sdl26Main()
{
    using namespace SDL26ExampleNamespace;

    //Start up SDL and create window
    if( !init() )
    {
        printf( "Failed to initialize!\n" );
    }
    else
    {
        //Load media
        if( !loadMedia() )
        {
            printf( "Failed to load media!\n" );
        }
        else
        {
            //Main loop flag
            bool quit = false;

            //Event handler
            SDL_Event e;

            //The dot that will be moving around on the screen
            Dot dot;

            //While application is running
            while( !quit )
            {
                //Handle events on queue
                while( SDL_PollEvent( &e ) != 0 )
                {
                    //User requests quit
                    if( e.type == SDL_QUIT )
                    {
                        quit = true;
                    }

                    //Handle input for the dot
                    dot.handleEvent( e );
                }

                //Move the dot
                dot.move();

                //Clear screen
                SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
                SDL_RenderClear( gRenderer );

                //Render objects
                dot.render();

                //Update screen
                SDL_RenderPresent( gRenderer );
            }
        }
    }

    //Free resources and close SDL
    close();

    return 0;
}
コード例 #3
0
int main( int argc, char* args[] ) {
  //Start up SDL and create window
  if( !init() ) {
    printf( "Failed to initialize!\n" );
  } else {
    //Load media
    if( !loadMedia() ) {
      printf( "Failed to load media!\n" );
    } else {
      //Main loop flag
      bool quit = false;

      //Event handler
      SDL_Event e;

      //The dot that will be moving around on the screen
      Dot dot;

      //The camera area
      SDL_Rect camera = { 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT };

      //While application is running
      while( !quit ) {
        //Handle events on queue
        while( SDL_PollEvent( &e ) != 0 ) {
          //User requests quit
          if( e.type == SDL_QUIT ) {
            quit = true;
          }

          //Handle input for the dot
          dot.handleEvent( e );
        }

        //Move the dot
        dot.move();

        //Center the camera over the dot
        camera.x = ( dot.getPosX() + Dot::DOT_WIDTH / 2 ) - SCREEN_WIDTH / 2;
        camera.y = ( dot.getPosY() + Dot::DOT_HEIGHT / 2 ) - SCREEN_HEIGHT / 2;

        //Keep the camera in bounds
        if( camera.x < 0 ) {
          camera.x = 0;
        }
        if( camera.y < 0 ) {
          camera.y = 0;
        }
        if( camera.x > LEVEL_WIDTH - camera.w ) {
          camera.x = LEVEL_WIDTH - camera.w;
        }
        if( camera.y > LEVEL_HEIGHT - camera.h ) {
          camera.y = LEVEL_HEIGHT - camera.h;
        }

        //Clear screen
        SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
        SDL_RenderClear( gRenderer );

        //Render background
        gBGTexture.render( 0, 0, &camera );

        //Render objects
        dot.render( camera.x, camera.y );

        //Update screen
        SDL_RenderPresent( gRenderer );
      }
    }
  }

  //Free resources and close SDL
  close();

  return 0;
}