Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Sprite animation question  (Read 3279 times)

0 Members and 1 Guest are viewing this topic.

mhouse777

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Sprite animation question
« on: July 25, 2016, 12:34:47 am »
HI,
 I am not a programmer but I enjoy animating my 3d characters ( I work in 3ds Max). I used SDL but I couldnt get my program to work in MSV 2015  C++ after upgrading from MSV express 2010, SFML works fine so far. I found out how to crop images off one of my sprite sheets...... 
#include <SFML/Graphics.Hpp>
#include <string>
#include <iostream>

int main()
{
        sf::RenderWindow Window(sf::VideoMode(640, 480), "sfml test");
        Window.setKeyRepeatEnabled(false);
        sf::Texture pTexure;
        sf::Sprite playerImage;

        if (!pTexure.loadFromFile("run_jump.bmp" , sf::IntRect(0,0 , 640 ,480 )))
                std::cout << "cant load image" << std::endl;
        playerImage.setTexture(pTexure);
        while (Window.isOpen())
        {
                sf::Event Event;
                while (Window.pollEvent(Event))
                {  
                        switch(Event.type)
                        case sf::Event::Closed:
                                Window.close();
                                break;
                }
                Window.draw(playerImage);
                Window.display();
        }
}
 

...... I would like to run four or more sprite sheets ( with their associated  wav files ) randomly. I'm not sure how to set up the IntRect() to include four or more sprite sheets. Also,  I would like to learn how to associate their wav files so they play when the sprite sheet is called. The sprites are the same dimension as my window , but the sprite sheets are 10 across 5 down , 6400 x  2400. Any help would be appreciated.

Thx

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Re: Sprite animation question
« Reply #1 on: July 25, 2016, 08:37:03 am »
Just create one sf::Texture and sf::Sprite per texture. Also I'm not really sure about your frame layout. Do you  have just one frame per file? Or multiple frames in one file? Showing an example image would help a lot.

mhouse777

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Sprite animation question
« Reply #2 on: July 25, 2016, 11:23:45 am »
Thanks Mario,

 I have 50 frames per sprite sheet , each 640 X 480, 10 across and  5 down. I have attached one of the sprite sheets. If you have time could you show some example code? Thanks so much. I have to use the quick reply b/c the normal reply times out. I will load the sprite sheet example soon hopefully.

mhouse777

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Sprite animation question
« Reply #3 on: July 25, 2016, 11:32:22 am »
here's the sprite sheet example.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Sprite animation question
« Reply #4 on: July 25, 2016, 12:51:54 pm »
The sprite sheet that you attached is a lot bigger than 640 x 480; it's 6400 × 2400, in fact.

Assuming your sprite sheets are really 640x480 as you say, I think the best option would be to add all the spritesheets into one image file and load it as a single texture. It's should be simple enough to 'switch frame groups' using a single texture.

If you need something to help with animating, may I suggest Plinth. Its Animation Tracks were inspired by my own love of 3DS Max, in fact :o
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

mhouse777

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Sprite animation question
« Reply #5 on: July 25, 2016, 11:22:39 pm »
Hi Hapax,

 Yes the sprite sheet is 6400 x 2400 , and the actual sprites on the sheet are 640 x 480, so it's 10  across on the x and 5 down on the y. The actual rect window is 640 x 480 , so the sprites animation fit exactly into that window........

here's the code from my previous SDL program ( many link errors  in VS C++ 2015 I couldn't fix , but compiled ok)
#include "SDL.h"
#include "SDL_mixer.h"
#include <time.h>
#include <ctime>
#include <cstdlib>
#include <windows.h>

using namespace std;

void setrects(SDL_Rect* clip)
{

clip[0].x=0;
clip[0].y=0;
clip[0].w=640;  
clip[0].h=480;

clip[1].x=640;
clip[1].y=0;
clip[1].w=640;  
clip[1].h=480;

clip[2].x=1280;
clip[2].y=0;
clip[2].w=640;  
clip[2].h=480;

clip[3].x=1920;
clip[3].y=0;
clip[3].w=640;  
clip[3].h=480;

clip[4].x=2560;
clip[4].y=0;
clip[4].w=640;  
clip[4].h=480;

clip[5].x=3200;
clip[5].y=0;
clip[5].w=640;  
clip[5].h=480;

clip[6].x=3840;
clip[6].y=0;
clip[6].w=640;  
clip[6].h=480;

clip[7].x=4480;
clip[7].y=0;
clip[7].w=640;  
clip[7].h=480;

clip[8].x=5120;
clip[8].y=0;
clip[8].w=640;  
clip[8].h=480;

clip[9].x=5760;
clip[9].y=0;
clip[9].w=640;  
clip[9].h=480;  // row 1

clip[10].x=0;
clip[10].y=480;
clip[10].w=640;  
clip[10].h=480;

clip[11].x=640;
clip[11].y=480;
clip[11].w=640;  
clip[11].h=480;

clip[12].x=1280;
clip[12].y=480;
clip[12].w=640;  
clip[12].h=480;

clip[13].x=1920;
clip[13].y=480;
clip[13].w=640;  
clip[13].h=480;

clip[14].x=2560;
clip[14].y=480;
clip[14].w=640;  
clip[14].h=480;

clip[15].x=3200;
clip[15].y=480;
clip[15].w=640;  
clip[15].h=480;

clip[16].x=3840;
clip[16].y=480;
clip[16].w=640;  
clip[16].h=480;

clip[17].x=4480;
clip[17].y=480;
clip[17].w=640;  
clip[17].h=480;

clip[18].x=5120;
clip[18].y=480;
clip[18].w=640;  
clip[18].h=480;

clip[19].x=5760;
clip[19].y=480;
clip[19].w=640;  
clip[19].h=480; // end row 2

clip[20].x=0;
clip[20].y=960;
clip[20].w=640;  
clip[20].h=480;

clip[21].x=640;
clip[21].y=960;
clip[21].w=640;  
clip[21].h=480;

clip[22].x=1280;
clip[22].y=960;
clip[22].w=640;  
clip[22].h=480;

clip[23].x=1920;
clip[23].y=960;
clip[23].w=640;  
clip[23].h=480;

clip[24].x=2560;
clip[24].y=960;
clip[24].w=640;  
clip[24].h=480;

clip[25].x=3200;
clip[25].y=960;
clip[25].w=640;  
clip[25].h=480;

clip[26].x=3840;
clip[26].y=960;
clip[26].w=640;  
clip[26].h=480;

clip[27].x=4480;
clip[27].y=960;
clip[27].w=640;  
clip[27].h=480;

clip[28].x=5120;
clip[28].y=960;
clip[28].w=640;  
clip[28].h=480;

clip[29].x=5760;
clip[29].y=960;
clip[29].w=640;  
clip[29].h=480; // end row 3

 clip[30].x=0;
clip[30].y=1440;
clip[30].w=640;  
clip[30].h=480;
 
 clip[31].x=640;
clip[31].y=1440;
clip[31].w=640;  
clip[31].h=480;

clip[32].x=1280;
clip[32].y=1440;
clip[32].w=640;  
clip[32].h=480;

clip[33].x=1920;
clip[33].y=1440;
clip[33].w=640;  
clip[33].h=480;

clip[34].x=2560;
clip[34].y=1440;
clip[34].w=640;  
clip[34].h=480;

clip[35].x=3200;
clip[35].y=1440;
clip[35].w=640;  
clip[35].h=480;

clip[36].x=3840;
clip[36].y=1440;
clip[36].w=640;  
clip[36].h=480;

clip[37].x=4480;
clip[37].y=1440;
clip[37].w=640;  
clip[37].h=480;

clip[38].x=5120;
clip[38].y=1440;
clip[38].w=640;  
clip[38].h=480;

clip[39].x=5760;
clip[39].y=1440;
clip[39].w=640;  
clip[39].h=480; // end row 4

clip[40].x=0;
clip[40].y=1920;
clip[40].w=640;  
clip[40].h=480;

clip[41].x=640;
clip[41].y=1920;
clip[41].w=640;  
clip[41].h=480;

clip[42].x=1280;
clip[42].y=1920;
clip[42].w=640;  
clip[42].h=480;

clip[43].x=1920;
clip[43].y=1920;
clip[43].w=640;  
clip[43].h=480;

clip[44].x=2560;
clip[44].y=1920;
clip[44].w=640;  
clip[44].h=480;

clip[45].x=3200;
clip[45].y=1920;
clip[45].w=640;  
clip[45].h=480;

clip[46].x=3840;
clip[46].y=1920;
clip[46].w=640;  
clip[46].h=480;

clip[47].x=4480;
clip[47].y=1920;
clip[47].w=640;  
clip[47].h=480;

clip[48].x=5120;
clip[48].y=1920;
clip[48].w=640;  
clip[48].h=480;

clip[49].x=5760;
clip[49].y=1920;
clip[49].w=640;  
clip[49].h=480; // end row 5

}


int main( int argc, char** argv )
{
        SDL_Init(SDL_INIT_EVERYTHING);
        Mix_OpenAudio(22050,MIX_DEFAULT_FORMAT,2,4096);
        Mix_Music* music , *music2 , *music5 , *music6 , *music7 , *music8 ,  *music9 ,  *music10, *music11, *music12 , *music13, *music14 , *music15, *music16 ;
        music=Mix_LoadMUS("a/tank_buster.wav");
        music2=Mix_LoadMUS("b/tank_buster.wav");
        music5=Mix_LoadMUS("a/airship_buster.wav");
        music6=Mix_LoadMUS("b/bunker_buster.wav");
        music7=Mix_LoadMUS("a/bunker_buster_2.wav");
        music8=Mix_LoadMUS("b/airship_buster_2.wav");
        music9=Mix_LoadMUS("a/bunker_buster.wav");
        music10=Mix_LoadMUS("b/bunker_buster_2.wav");
        music11=Mix_LoadMUS("a/tank_buster_2.wav");
        music12=Mix_LoadMUS("b/tank_buster_2.wav");
        music13=Mix_LoadMUS("a/airship_buster_2.wav");
        music14=Mix_LoadMUS("b/airship_buster.wav");
        music15=Mix_LoadMUS("b/knock_down.wav");
        music16=Mix_LoadMUS("a/knock_down.wav");
       
        SDL_Surface* screen,*image, *image2, *image3 , *image4 ,*image5,*image6 ,*image7 ,*image8,*image9 , *image10, *image11, *image12, *image13, *image14;
        screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
        bool a_knock_down_animation = false;
        bool b_knock_down_animation = false;
        bool running=true;
        const int FPS=30;
                    Uint32 start;
                    SDL_Rect rect;
                    int frame=0;
                    Uint32 color=SDL_MapRGB(screen->format,0,0,0);

        image=SDL_DisplayFormat(SDL_LoadBMP("A/tank_buster.BMP"));
        image2=SDL_DisplayFormat(SDL_LoadBMP("B/tank_buster.BMP"));
        image3=SDL_DisplayFormat(SDL_LoadBMP("A/knock_down.bmp"));
        image4=SDL_DisplayFormat(SDL_LoadBMP("B/knock_down.bmp"));
        image5=SDL_DisplayFormat(SDL_LoadBMP("A/airship_buster.bmp"));
        image6=SDL_DisplayFormat(SDL_LoadBMP("B/bunker_buster.bmp"));
        image7=SDL_DisplayFormat(SDL_LoadBMP("A/bunker_buster_2.bmp"));
        image8=SDL_DisplayFormat(SDL_LoadBMP("B/airship_buster_2.bmp"));
        image9=SDL_DisplayFormat(SDL_LoadBMP("A/bunker_buster.bmp"));
        image10=SDL_DisplayFormat(SDL_LoadBMP("B/bunker_buster_2.bmp"));
        image11=SDL_DisplayFormat(SDL_LoadBMP("A/tank_buster_2.BMP"));
        image12=SDL_DisplayFormat(SDL_LoadBMP("B/tank_buster_2.bmp"));
        image13=SDL_DisplayFormat(SDL_LoadBMP("A/airship_buster_2.BMP"));
        image14=SDL_DisplayFormat(SDL_LoadBMP("B/airship_buster.bmp"));
       
       
       
        SDL_FillRect(screen,&rect,color);
        SDL_SetColorKey(image,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,255,0,255));
        SDL_SetColorKey(image2,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,255,0,255));
        SDL_SetColorKey(image3,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,255,0,255));
        SDL_SetColorKey(image4,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,255,0,255));
        SDL_SetColorKey(image5,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,255,0,255));
        SDL_SetColorKey(image6,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,255,0,255));
        SDL_SetColorKey(image7,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,255,0,255));
        SDL_SetColorKey(image8,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,255,0,255));
        SDL_SetColorKey(image9,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,255,0,255));
        SDL_SetColorKey(image10,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,255,0,255));
        SDL_SetColorKey(image11,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,255,0,255));
        SDL_SetColorKey(image12,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,255,0,255));
        SDL_SetColorKey(image13,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,255,0,255));
        SDL_SetColorKey(image14,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,255,0,255));
       
        SDL_Rect rects[50];
        setrects(rects);
        int a_pax_health =6;
        int a_sam_health= 28;
        int a_baz_health= 33;
         

        int b_pax_health= 5;
        int b_sam_health= 10;
        int b_baz_health= 15;
       

                   
                   
       
        int i;
        int random;
        srand((unsigned)time(0));

        for( i=0; i<5 ; i++)
        {
                random = (rand() % 4) +1 ;
        }



while(running)
                {

                        SDL_WM_SetCaption("Domination!!",NULL);
                        start=SDL_GetTicks();
                        SDL_Event event;
                        while(SDL_PollEvent(&event))
                        {

                                switch(event.type)
                                {
                                case SDL_QUIT:
                                running=false;
                                        break;
                                }

                               
                         }


 

switch(random)
{

       

case 1:

             if(b_pax_health<=0 && b_sam_health<=0 && b_baz_health<= 0)
         {
                 SDL_BlitSurface(image4,&rects[frame],screen,&rect) ;
                            b_knock_down_animation=true;
         }
         else if( b_baz_health > 0 && b_pax_health <= 0 && b_sam_health <= 0){
                 SDL_BlitSurface(image6,&rects[frame],screen,&rect) ;  // b_bunker_buster_1
                 
         }

     
         else if( b_sam_health > 0 && b_pax_health <= 0){
                 SDL_BlitSurface(image14,&rects[frame],screen,&rect) ;  // b_airship_buster_1
                 
         }
         
         else if( b_pax_health > 0 ){
                SDL_BlitSurface(image2,&rects[frame],screen,&rect) ;  //  b_tank_buster_1
         
         }
         
         
 

break;



case 2:

             if(a_pax_health <=0 && a_sam_health <=0 && a_baz_health <= 0)
         {
                 SDL_BlitSurface(image3,&rects[frame],screen,&rect) ;
                            a_knock_down_animation=true;
                                                         
         }
         else if( a_baz_health > 0 && a_pax_health <=0  &&  a_sam_health <= 0){
                 SDL_BlitSurface(image9,&rects[frame],screen,&rect) ;  // a_bunker_buster_1
                 
         }
                 
         else if( a_sam_health > 0 && a_pax_health <=0  ){
                 SDL_BlitSurface(image5,&rects[frame],screen,&rect) ;  // a_airship_buster_1
                 
         }
         else if( a_pax_health > 0 ){
                SDL_BlitSurface(image,&rects[frame],screen,&rect) ;  //  a_tank_buster_1
         
         }
         
         
       

break;



case 3:

           if(b_pax_health<=0 && b_sam_health<=0 && b_baz_health <= 0)
         {
                 SDL_BlitSurface(image4,&rects[frame],screen,&rect) ;
                            b_knock_down_animation=true;
         }

         else if( b_baz_health > 0  &&  b_sam_health <= 0 && b_pax_health<=0 ){
                 SDL_BlitSurface(image10,&rects[frame],screen,&rect) ;  // b_bunker_buster_2
                 
         }
         else if( b_sam_health > 0 && b_pax_health <= 0 ){
                 SDL_BlitSurface(image8,&rects[frame],screen,&rect) ;  // b_airship_buster_2
                 
         }
         else if( b_pax_health > 0 ){
                SDL_BlitSurface(image12,&rects[frame],screen,&rect) ;  //  b_tank_buster_2
       
         }

 

break;



case 4:


       
         

             if(a_pax_health<=0 && a_sam_health<=0 && a_baz_health<= 0)
         {
                 SDL_BlitSurface(image3,&rects[frame],screen,&rect) ;   // knock_down
                            a_knock_down_animation=true;
                                                       

         }
         else if( a_baz_health > 0 && a_sam_health <= 0  &&  a_pax_health <= 0){
                 SDL_BlitSurface(image7,&rects[frame],screen,&rect) ;  // a_bunker_buster_2
                 
         }
       
         else if( a_sam_health > 0 &&  a_pax_health <= 0 ){
                 SDL_BlitSurface(image13,&rects[frame],screen,&rect) ;  // a_airship_buster_2
                 
         }
         
         else if( a_pax_health > 0 ){
                SDL_BlitSurface(image11,&rects[frame],screen,&rect) ;  //  a_tank_buster_2
       
         }
       
break;





}     // end switch



  SDL_Flip(screen);
  frame++;
  if(frame==50)
{
  frame=0;

  if (a_knock_down_animation  ||  b_knock_down_animation)
      running = false;
                   
      random= rand() % 4 +1;
      Mix_HaltMusic();  
      if(random==1)
{
     
   if( b_pax_health<=0 && b_sam_health<0 && b_baz_health <= 0)
      {
        Mix_PlayMusic(music15,0);

      }

        else if( b_baz_health > 0 && b_pax_health <= 0  &&  b_sam_health <= 0){      // b_baz_buster_1
                 Mix_PlayMusic(music6,0);
                 b_baz_health = b_baz_health - 1;
                // b_baz_health = b_baz_health + 1;    // bunker
                }
                                                                                 
        else if( b_sam_health > 0 && b_pax_health <= 0) {        // b_airship_buster_1
                Mix_PlayMusic(music14,0);
                b_sam_health = b_sam_health - 1;
                }

        else if(  b_pax_health > 0){
                                                                           
                Mix_PlayMusic(music2,0);                 // b/tank_buster_1
                b_pax_health = b_pax_health - 1;
                                                                                                         
                }
                                                                                                                                                                 
                                                                                                                                                                 
                }



                if(random==2)
                {
                                                                                               
                   if(a_pax_health<=0 && a_sam_health<=0 && a_baz_health <= 0){
                        Mix_PlayMusic(music16,0);

                        }
                else if( a_baz_health > 0 && a_pax_health <= 0  &&  a_sam_health <= 0){      // a_baz_buster_1
                        Mix_PlayMusic(music9,0);
                        a_baz_health = a_baz_health - 1;          //  +6 power_bunker , -0 UNQ (block),  PWRbaz_UNQ +6 and blocks (unbeatable), -6 normal
                        }
                else if(a_sam_health > 0 && a_pax_health <= 0 ){            // a_airship_buster_1
                        Mix_PlayMusic(music5,0);
                        a_sam_health = a_sam_health - 1;
                         }
                else if(  a_pax_health > 0){                            
                        Mix_PlayMusic(music,0);                 // a/tank_buster_1
                        a_pax_health = a_pax_health - 1;
                                                                                                         
                        }
                                                                                                                                                                 
                                                                                                                                                               
                        }



                if(random==3)
                {
                  if(b_pax_health<=0 && b_sam_health<=0 && b_baz_health <= 0){
                        Mix_PlayMusic(music15,0);

                        }
                                                                                                                                                                 
                else if(b_baz_health > 0 && b_pax_health <= 0  &&  b_sam_health <= 0){       // b_baz_buster_2
                        Mix_PlayMusic(music10,0);
                        b_baz_health = b_baz_health - 1;  //normal
               //       b_baz_health = b_baz_health - 0;  //  UNQ
                        }
                                                                                                                                                                 
                else if( b_sam_health > 0 && b_pax_health <= 0 ){                            // b_airship_buster_2
                        Mix_PlayMusic(music8,0);                                                                                                                   
              //        b_sam_health = b_sam_health - 0;   // UNQ
                        b_sam_health = b_sam_health - 1;   // normal
                                                                                                                                                       
                         }
                else if(  b_pax_health > 0){
                                                                           
                        Mix_PlayMusic(music12,0);                                           // b/tank_buster_2
                        b_pax_health = b_pax_health - 1;    // normal
             //         b_pax_health = b_pax_health - 0;    // UNQ
                                                                                                         
                        }

                        }


                        if(random==4)
                        {
                           if(a_pax_health<=0 && a_sam_health<=0 && a_baz_health <= 0){
                                 Mix_PlayMusic(music16,0);

                                }
                                else if( a_baz_health > 0 && a_pax_health <= 0  &&  a_sam_health <= 0){      // a_baz_buster_2
                                        Mix_PlayMusic(music7,0);
                                        a_baz_health = a_baz_health - 1; // normal
                             //         a_baz_health = a_baz_health - 0; // UNQ  , equals a block
                                        }

                                else if( a_sam_health > 0 && a_pax_health <= 0 ){                  // a_airship_buster_2
                                        Mix_PlayMusic(music13,0);
                                        a_sam_health = a_sam_health - 1; // normal
                             //         a_sam_health = a_sam_health - 0; // UNQ
                                        }

                                else if(  a_pax_health > 0){
                                                                           
                                         Mix_PlayMusic(music11,0);                 // a/tank_buster_2
                                         a_pax_health = a_pax_health - 1; // normal
                            //           a_pax_health = a_pax_health - 0; // UNQ
                                                                                                         
                                         }
                                                                                                                                                                 
                                         }
                                                                                                                                               
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

                                                                                                                                                         

                                                                                                       
                                                                                                               
                                                                                                                                                         
        }

         

        if(1000/FPS>SDL_GetTicks()-start )
    SDL_Delay(1000/FPS-(SDL_GetTicks()-start));

        }  


        Mix_FreeMusic(music);
    Mix_FreeMusic(music2);
        Mix_FreeMusic(music5);
        Mix_FreeMusic(music6);
        Mix_FreeMusic(music7);
        Mix_FreeMusic(music8);
        Mix_FreeMusic(music9);
        Mix_FreeMusic(music10);
        Mix_FreeMusic(music11);
        Mix_FreeMusic(music12);
        Mix_FreeMusic(music13);
        Mix_FreeMusic(music14);
        Mix_FreeMusic(music15);
        Mix_FreeMusic(music16);
        Mix_CloseAudio();



        SDL_FreeSurface(image);
        SDL_FreeSurface(image2);
        SDL_FreeSurface(image3);
        SDL_FreeSurface(image4);
        SDL_FreeSurface(image5);
        SDL_FreeSurface(image6);
        SDL_FreeSurface(image7);
        SDL_FreeSurface(image8);
        SDL_FreeSurface(image9);
        SDL_FreeSurface(image10);
        SDL_FreeSurface(image11);
        SDL_FreeSurface(image12);
        SDL_FreeSurface(image13);
        SDL_FreeSurface(image14);
       
        system("PAUSE");
        SDL_Quit();
    return 0;


}

 


...... this randomly selects a sprite sheet and it's associated wav file and health decrements , so the first to reach health zero, game ends. I need some assistance to transfer this over to SFML , and in a more efficient way If possible. Thanks so much

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Sprite animation question
« Reply #6 on: July 26, 2016, 12:08:42 am »
Is there a reason that you're storing the entire window worth of texture per sprite frame? I assume you're clipping this off when displaying it; the images themselves are already cropped and there is a list of clipping in your SDL code.
If so, it would most definitely much better to compact those frames in the sprite sheet. The compacted frames can then be set to the sprite by adjusting the texture rectangle to match. Compacting saves storage space, which could be used to fit other sheets if required.
Saying that, a texture with a width of over 6k is really quite wide; you can't rely on graphics cards being able to handle this size.

p.s. Still, you can load multiple textures and switch between them on the same sprite if you have to!
« Last Edit: July 26, 2016, 12:24:53 am by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

mhouse777

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Sprite animation question
« Reply #7 on: July 26, 2016, 08:38:15 am »
Hi Hapax,
 Yes, the display is clipped, so all you see is the sprite moving in the window, 640 x 480. I do like the idea of putting all the textures on one sprite sheet for a character , but wouldn't that be a massive texture with 200 sprites at 640 x 480 each? I just put this code together from bits and pieces I learned from SDL on the internet being that I'm not a programmer. That being said , could you lay some sample code out with the idea you had about switching between multiple textures on the same sprite sheet?  Thanks Hapax.
 

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Sprite animation question
« Reply #8 on: July 26, 2016, 12:30:48 pm »
wouldn't that be a massive texture with 200 sprites at 640 x 480 each?
That's the point. You can crop the unused space around the actual character in the image. Then, just display it in the correct position.

'Switching groups' on the same texture could be as simple as having the other sets being arranged in the same way but offset in the image. Then you could just apply the offset depending on the set.
For example, if each spritesheet 'set' was 1000 x 200, your second one would be at (0, 200), third at (0, 400), fourth at (0, 600) and fifth at (0, 800). The texture itself would be a single 1000x1000 texture.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

mhouse777

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Sprite animation question
« Reply #9 on: July 28, 2016, 04:47:08 am »
Cool . Any chance for some sample code?

Thanks Hapax.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Sprite animation question
« Reply #10 on: July 28, 2016, 02:58:09 pm »
Sure. Assuming you have the sprites cropped and have all the texture rectangles stored for each frame, switching groups in the way I suggested above would be something like:
unsigned int selectedGroup = 3; // which group to use, with the first one being zero

const unsigned int groupHeight = 200u;
sf::IntRect frameRect = getCurrentFrameTextureRectangle(); // you'd need to implement this for one group
frameRect.top += groupHeight * selectedGroup;
sprite.setTextureRectangle(frameRect);
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

mhouse777

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Sprite animation question
« Reply #11 on: August 01, 2016, 10:34:08 pm »
... cool. I'll try this out and get back to you. Thanks Hapax.