• Domingo 15 de Diciembre de 2024, 07:08

Autor Tema:  manejo de sprite a partir de hojas de sprite en sdl en c++  (Leído 1721 veces)

luchojimenez

  • Miembro activo
  • **
  • Mensajes: 56
    • Ver Perfil
manejo de sprite a partir de hojas de sprite en sdl en c++
« en: Miércoles 15 de Diciembre de 2010, 04:38 »
0
este ejemplo  lo consegui de la pagina lazy foo  el jemplo tomaron  unos dibujos para  la  animacion  una hoja sprite , a esta animacion se llama  sprite me gustaria  que  una alma  caritativa  me explicase  mucho para poder  impplementar  el codigo  en mis propias  hojas de sprite

Código: C++
  1.  
  2. //The headers
  3. #include "SDL/SDL.h"
  4. #include "SDL/SDL_image.h"
  5. #include <string>
  6.  
  7. //Screen attributes
  8. const int SCREEN_WIDTH = 640;
  9. const int SCREEN_HEIGHT = 480;
  10. const int SCREEN_BPP = 32;
  11.  
  12. //The frames per second
  13. const int FRAMES_PER_SECOND = 10;
  14.  
  15. //The dimenstions of the stick figure
  16. const int FOO_WIDTH = 64;
  17. const int FOO_HEIGHT = 205;
  18.  
  19. //The direction status of the stick figure
  20. const int FOO_RIGHT = 0;
  21. const int FOO_LEFT = 1;
  22.  
  23. //The surfaces
  24. SDL_Surface *foo = NULL;
  25. SDL_Surface *screen = NULL;
  26.  
  27. //The event structure
  28. SDL_Event event;
  29.  
  30. //The areas of the sprite sheet
  31. SDL_Rect clipsRight[ 4 ];
  32. SDL_Rect clipsLeft[ 4 ];
  33.  
  34. //The stick figure
  35. class Foo
  36. {
  37.     private:
  38.     //The offset
  39.     int offSet;
  40.  
  41.     //Its rate of movement
  42.     int velocity;
  43.  
  44.     //Its current frame
  45.     int frame;
  46.  
  47.     //Its animation status
  48.     int status;
  49.  
  50.     public:
  51.     //Initializes the variables
  52.     Foo();
  53.  
  54.     //Handles input
  55.     void handle_events();
  56.  
  57.     //Moves the stick figure
  58.     void move();
  59.  
  60.     //Shows the stick figure
  61.     void show();
  62. };
  63.  
  64. //The timer
  65. class Timer
  66. {
  67.     private:
  68.     //The clock time when the timer started
  69.     int startTicks;
  70.  
  71.     //The ticks stored when the timer was paused
  72.     int pausedTicks;
  73.  
  74.     //The timer status
  75.     bool paused;
  76.     bool started;
  77.  
  78.     public:
  79.     //Initializes variables
  80.     Timer();
  81.  
  82.     //The various clock actions
  83.     void start();
  84.     void stop();
  85.     void pause();
  86.     void unpause();
  87.  
  88.     //Gets the timer's time
  89.     int get_ticks();
  90.  
  91.     //Checks the status of the timer
  92.     bool is_started();
  93.     bool is_paused();
  94. };
  95.  
  96. SDL_Surface *load_image( std::string filename )
  97. {
  98.     //The image that's loaded
  99.     SDL_Surface* loadedImage = NULL;
  100.  
  101.     //The optimized surface that will be used
  102.     SDL_Surface* optimizedImage = NULL;
  103.  
  104.     //Load the image
  105.     loadedImage = IMG_Load( filename.c_str() );
  106.  
  107.     //If the image loaded
  108.     if( loadedImage != NULL )
  109.     {
  110.         //Create an optimized surface
  111.         optimizedImage = SDL_DisplayFormat( loadedImage );
  112.  
  113.         //Free the old surface
  114.         SDL_FreeSurface( loadedImage );
  115.  
  116.         //If the surface was optimized
  117.         if( optimizedImage != NULL )
  118.         {
  119.             //Color key surface
  120.             SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) );
  121.         }
  122.     }
  123.  
  124.     //Return the optimized surface
  125.     return optimizedImage;
  126. }
  127.  
  128. void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
  129. {
  130.     //Holds offsets
  131.     SDL_Rect offset;
  132.  
  133.     //Get offsets
  134.     offset.x = x;
  135.     offset.y = y;
  136.  
  137.     //Blit
  138.     SDL_BlitSurface( source, clip, destination, &offset );
  139. }
  140.  
  141. void set_clips()
  142. {
  143.     //Clip the sprites
  144.     clipsRight[ 0 ].x = 0;
  145.     clipsRight[ 0 ].y = 0;
  146.     clipsRight[ 0 ].w = FOO_WIDTH;
  147.     clipsRight[ 0 ].h = FOO_HEIGHT;
  148.  
  149.     clipsRight[ 1 ].x = FOO_WIDTH;
  150.     clipsRight[ 1 ].y = 0;
  151.     clipsRight[ 1 ].w = FOO_WIDTH;
  152.     clipsRight[ 1 ].h = FOO_HEIGHT;
  153.  
  154.     clipsRight[ 2 ].x = FOO_WIDTH * 2;
  155.     clipsRight[ 2 ].y = 0;
  156.     clipsRight[ 2 ].w = FOO_WIDTH;
  157.     clipsRight[ 2 ].h = FOO_HEIGHT;
  158.  
  159.     clipsRight[ 3 ].x = FOO_WIDTH * 3;
  160.     clipsRight[ 3 ].y = 0;
  161.     clipsRight[ 3 ].w = FOO_WIDTH;
  162.     clipsRight[ 3 ].h = FOO_HEIGHT;
  163.  
  164.     clipsLeft[ 0 ].x = 0;
  165.     clipsLeft[ 0 ].y = FOO_HEIGHT;
  166.     clipsLeft[ 0 ].w = FOO_WIDTH;
  167.     clipsLeft[ 0 ].h = FOO_HEIGHT;
  168.  
  169.     clipsLeft[ 1 ].x = FOO_WIDTH;
  170.     clipsLeft[ 1 ].y = FOO_HEIGHT;
  171.     clipsLeft[ 1 ].w = FOO_WIDTH;
  172.     clipsLeft[ 1 ].h = FOO_HEIGHT;
  173.  
  174.     clipsLeft[ 2 ].x = FOO_WIDTH * 2;
  175.     clipsLeft[ 2 ].y = FOO_HEIGHT;
  176.     clipsLeft[ 2 ].w = FOO_WIDTH;
  177.     clipsLeft[ 2 ].h = FOO_HEIGHT;
  178.  
  179.     clipsLeft[ 3 ].x = FOO_WIDTH * 3;
  180.     clipsLeft[ 3 ].y = FOO_HEIGHT;
  181.     clipsLeft[ 3 ].w = FOO_WIDTH;
  182.     clipsLeft[ 3 ].h = FOO_HEIGHT;
  183. }
  184.  
  185. bool init()
  186. {
  187.     //Initialize all SDL subsystems
  188.     if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
  189.     {
  190.         return false;
  191.     }
  192.  
  193.     //Set up the screen
  194.     screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
  195.  
  196.     //If there was an error in setting up the screen
  197.     if( screen == NULL )
  198.     {
  199.         return false;
  200.     }
  201.  
  202.     //Set the window caption
  203.     SDL_WM_SetCaption( "Animation Test", NULL );
  204.  
  205.     //If everything initialized fine
  206.     return true;
  207. }
  208.  
  209. bool load_files()
  210. {
  211.     //Load the sprite sheet
  212.     foo = load_image( "foo.png" );
  213.  
  214.     //If there was a problem in loading the sprite
  215.     if( foo == NULL )
  216.     {
  217.         return false;
  218.     }
  219.  
  220.     //If everything loaded fine
  221.     return true;
  222. }
  223.  
  224. void clean_up()
  225. {
  226.     //Free the surface
  227.     SDL_FreeSurface( foo );
  228.  
  229.     //Quit SDL
  230.     SDL_Quit();
  231. }
  232.  
  233. Foo::Foo()
  234. {
  235.     //Initialize movement variables
  236.     offSet = 0;
  237.     velocity = 0;
  238.  
  239.     //Initialize animation variables
  240.     frame = 0;
  241.     status = FOO_RIGHT;
  242. }
  243.  
  244. void Foo::handle_events()
  245. {
  246.     //If a key was pressed
  247.     if( event.type == SDL_KEYDOWN )
  248.     {
  249.         //Set the velocity
  250.         switch( event.key.keysym.sym )
  251.         {
  252.             case SDLK_RIGHT: velocity += FOO_WIDTH / 4; break;
  253.             case SDLK_LEFT: velocity -= FOO_WIDTH / 4; break;
  254.         }
  255.     }
  256.     //If a key was released
  257.     else if( event.type == SDL_KEYUP )
  258.     {
  259.         //Set the velocity
  260.         switch( event.key.keysym.sym )
  261.         {
  262.             case SDLK_RIGHT: velocity -= FOO_WIDTH / 4; break;
  263.             case SDLK_LEFT: velocity += FOO_WIDTH / 4; break;
  264.         }
  265.     }
  266. }
  267.  
  268. void Foo::move()
  269. {
  270.     //Move
  271.     offSet += velocity;
  272.  
  273.     //Keep the stick figure in bounds
  274.     if( ( offSet < 0 ) || ( offSet + FOO_WIDTH > SCREEN_WIDTH ) )
  275.     {
  276.         offSet -= velocity;
  277.     }
  278. }
  279.  
  280. void Foo::show()
  281. {
  282.     //If Foo is moving left
  283.     if( velocity < 0 )
  284.     {
  285.         //Set the animation to left
  286.         status = FOO_LEFT;
  287.  
  288.         //Move to the next frame in the animation
  289.         frame++;
  290.     }
  291.     //If Foo is moving right
  292.     else if( velocity > 0 )
  293.     {
  294.         //Set the animation to right
  295.         status = FOO_RIGHT;
  296.  
  297.         //Move to the next frame in the animation
  298.         frame++;
  299.     }
  300.     //If Foo standing
  301.     else
  302.     {
  303.         //Restart the animation
  304.         frame = 0;
  305.     }
  306.  
  307.     //Loop the animation
  308.     if( frame >= 4 )
  309.     {
  310.         frame = 0;
  311.     }
  312.  
  313.     //Show the stick figure
  314.     if( status == FOO_RIGHT )
  315.     {
  316.         apply_surface( offSet, SCREEN_HEIGHT - FOO_HEIGHT, foo, screen, &clipsRight[ frame ] );
  317.     }
  318.     else if( status == FOO_LEFT )
  319.     {
  320.         apply_surface( offSet, SCREEN_HEIGHT - FOO_HEIGHT, foo, screen, &clipsLeft[ frame ] );
  321.     }
  322. }
  323.  
  324. Timer::Timer()
  325. {
  326.     //Initialize the variables
  327.     startTicks = 0;
  328.     pausedTicks = 0;
  329.     paused = false;
  330.     started = false;
  331. }
  332.  
  333. void Timer::start()
  334. {
  335.     //Start the timer
  336.     started = true;
  337.  
  338.     //Unpause the timer
  339.     paused = false;
  340.  
  341.     //Get the current clock time
  342.     startTicks = SDL_GetTicks();
  343. }
  344.  
  345. void Timer::stop()
  346. {
  347.     //Stop the timer
  348.     started = false;
  349.  
  350.     //Unpause the timer
  351.     paused = false;
  352. }
  353.  
  354. void Timer::pause()
  355. {
  356.     //If the timer is running and isn't already paused
  357.     if( ( started == true ) && ( paused == false ) )
  358.     {
  359.         //Pause the timer
  360.         paused = true;
  361.  
  362.         //Calculate the paused ticks
  363.         pausedTicks = SDL_GetTicks() - startTicks;
  364.     }
  365. }
  366.  
  367. void Timer::unpause()
  368. {
  369.     //If the timer is paused
  370.     if( paused == true )
  371.     {
  372.         //Unpause the timer
  373.         paused = false;
  374.  
  375.         //Reset the starting ticks
  376.         startTicks = SDL_GetTicks() - pausedTicks;
  377.  
  378.         //Reset the paused ticks
  379.         pausedTicks = 0;
  380.     }
  381. }
  382.  
  383. int Timer::get_ticks()
  384. {
  385.     //If the timer is running
  386.     if( started == true )
  387.     {
  388.         //If the timer is paused
  389.         if( paused == true )
  390.         {
  391.             //Return the number of ticks when the timer was paused
  392.             return pausedTicks;
  393.         }
  394.         else
  395.         {
  396.             //Return the current time minus the start time
  397.             return SDL_GetTicks() - startTicks;
  398.         }
  399.     }
  400.  
  401.     //If the timer isn't running
  402.     return 0;
  403. }
  404.  
  405. bool Timer::is_started()
  406. {
  407.     return started;
  408. }
  409.  
  410. bool Timer::is_paused()
  411. {
  412.     return paused;
  413. }
  414.  
  415. int main( int argc, char* args[] )
  416. {
  417.     //Quit flag
  418.     bool quit = false;
  419.  
  420.     //Initialize
  421.     if( init() == false )
  422.     {
  423.         return 1;
  424.     }
  425.  
  426.     //Load the files
  427.     if( load_files() == false )
  428.     {
  429.         return 1;
  430.     }
  431.  
  432.     //Clip the sprite sheet
  433.     set_clips();
  434.  
  435.     //The frame rate regulator
  436.     Timer fps;
  437.  
  438.     //The stick figure
  439.     Foo walk;
  440.  
  441.     //While the user hasn't quit
  442.     while( quit == false )
  443.     {
  444.         //Start the frame timer
  445.         fps.start();
  446.  
  447.         //While there's events to handle
  448.         while( SDL_PollEvent( &event ) )
  449.         {
  450.             //Handle events for the stick figure
  451.             walk.handle_events();
  452.  
  453.             //If the user has Xed out the window
  454.             if( event.type == SDL_QUIT )
  455.             {
  456.                 //Quit the program
  457.                 quit = true;
  458.             }
  459.         }
  460.  
  461.         //Move the stick figure
  462.         walk.move();
  463.  
  464.         //Fill the screen white
  465.         SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
  466.  
  467.         //Show the stick figure on the screen
  468.         walk.show();
  469.  
  470.         //Update the screen
  471.         if( SDL_Flip( screen ) == -1 )
  472.         {
  473.             return 1;
  474.         }
  475.  
  476.         //Cap the frame rate
  477.         if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
  478.         {
  479.             SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
  480.         }
  481.     }
  482.  
  483.     //Clean up
  484.     clean_up();
  485.  
  486.     return 0;
  487. }
  488.  
  489.  
  490.  
  491.  
  492.  

este  es el sprite

lo que me  gustaria saber  es  como hace sdl para procesar el orden de las posiciones  en la hoja de  sprite
El mensaje contiene 1 archivo adjunto. Debes ingresar o registrarte para poder verlo y descargarlo.