Animation - 500 Millisecs Problem



  • Mein Animations Code kompiliert jetzt fehlerfrei, aber es wird nur der erste Frame dargestellt, und sinst bewegt sich nix.
    Igrnedwas stimmt mit meiner Zeit Schleife nicht, ich wollte es so machen, dass alle 500 Millisekunden ein neues Bild geladen wird.
    Vll. blickt ja von euch einer durch:

    #include "SDL/SDL.h"
    #include "SDL/SDL_image.h"
    #include <string>
    #include <ctime>
    
    using namespace std;
    
    SDL_Surface * LoadImage(const string &filename);
    void ApplySurface(short x, short y, SDL_Surface *source, SDL_Surface *destination, SDL_Rect *clip = NULL);
    
    int main(int args, char *argc[])
    {
        //Variablen (structs) für Bilder\Events
        SDL_Surface *screen = NULL;
        SDL_Surface *image = NULL;
        SDL_Event event;
        bool quit = false;
        short counter = 0;
        long time1 = 0;
    
        //SDL initialisieren
        if (SDL_Init(SDL_INIT_EVERYTHING) == -1) return 1;
        screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
        if(screen == NULL) return 1;
    
        //Frames festlegen
        SDL_Rect offset[6];
    
        //frame1                            OBERE REIHE
        offset[0].x = 0;
        offset[0].y = 0;
        offset[0].w = 130;
        offset[0].h = 136;
        //frame2
        offset[1].x = 131;
        offset[1].y = 0;
        offset[1].w = 130;
        offset[1].h = 136;
        //frame3
        offset[2].x = 262;
        offset[2].y = 0;
        offset[2].w = 130;
        offset[2].h = 136;
        //frame4
        offset[3].x = 393;
        offset[3].y = 0;
        offset[3].w = 130;
        offset[3].h = 136;
        //frame5                            UNTERE REIHE
        offset[4].x = 0;
        offset[4].y = 137;
        offset[4].w = 130;
        offset[4].h = 136;
        //frame6
        offset[5].x = 131;
        offset[5].y = 137;
        offset[5].w = 130;
        offset[5].h = 136;
    
        //Frame Bild laden
        image = LoadImage("char9.bmp");
    
        //Event Schleife
        while(quit == false)
        {
            if(time1 == 0) time1 = time(0); //?
            if(time(0) > (time1 + 500))     //?
            {                               //?
                ++counter;                  //?
                time1 = time(0);            //?
            }                               //?
    
            if(counter > 5) counter = 0;
    
            //Input überprüfen
            while(SDL_PollEvent(&event))
            {
                if(event.type == SDL_QUIT)
                {
                    quit = true;
                }
            }
    
            //Frames wiedergeben
            ApplySurface(0,0,image,screen,&offset[counter]);
    
            //Bildschirm flippen
            if(SDL_Flip(screen) == -1) return 1;
        }
    
        SDL_Quit();
    
        return 0;
    }
    
    SDL_Surface * LoadImage(const string &filename)
    {
        SDL_Surface *loadedImage = NULL;
        SDL_Surface *optimizedImage = NULL;
    
        loadedImage = IMG_Load(filename.c_str());
        if(loadedImage != NULL)
        {
            optimizedImage = SDL_DisplayFormat(loadedImage);
            SDL_FreeSurface(loadedImage);
        }
        return optimizedImage;
    }
    
    void ApplySurface(short x, short y, SDL_Surface *source, SDL_Surface *destination, SDL_Rect *clip)
    {
        SDL_Rect offset;
        offset.x = y;
        offset.y = y;
    
        SDL_BlitSurface(source, clip, destination, &offset);
    }
    

    MfG
    Stromberg



  • ich wollte es so machen, dass alle 500 Millisekunden ein neues Bild geladen wird

    if(time(0) > (time1 + 500)) 
            {                              
                ++counter;                 
                time1 = time(0);           
            }
    

    Warum hast Du denn nicht einfach mal nachgeguckt, was time(0) ueberhaupt macht?



  • hol dir die vergangenen ms seit programmstart mit SDL_GetTicks()


Anmelden zum Antworten