一段时间后,SDL2程序停止响应

时间:2015-08-16 22:58:09

标签: c windows gcc sdl-2

所以我试着做一个简单的精灵动画。 我使用此图像作为精灵:http://answers.unity3d.com/storage/temp/5358-1123_01_01.jpg

这是代码:

#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>


//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;


#define PATH_TO_IMAGE "sprite.jpg"

int main(int argc, char* args[]) {

    const int WALKING_ANIMATION_FRAMES = 8;

    SDL_Rect gSpriteClips[ 8 ] = {
        (SDL_Rect) {.h = 112, .w = 88, .x = 16, .y = 16},
        (SDL_Rect) {.h = 112, .w = 88, .x = 133, .y = 16},
        (SDL_Rect) {.h = 112, .w = 88, .x = 265, .y = 16},
        (SDL_Rect) {.h = 112, .w = 88, .x = 398, .y = 16},
        (SDL_Rect) {.h = 112, .w = 88, .x = 16, .y = 139},
        (SDL_Rect) {.h = 112, .w = 88, .x = 132, .y = 139},
        (SDL_Rect) {.h = 112, .w = 88, .x = 264, .y = 139},
        (SDL_Rect) {.h = 112, .w = 88, .x = 397, .y = 139},
    };

    //The window renderer
    SDL_Renderer *renderer = NULL;

    //The window we'll be rendering to
    SDL_Window *gWindow;

    //Initialize SDL
    if (SDL_Init(SDL_INIT_VIDEO) == 0) {
        //Create window
        gWindow = SDL_CreateWindow("Character animation", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);

        if (gWindow != NULL) {
            //Get window surface
            renderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
            int imgFlags = IMG_INIT_JPG;

            if (IMG_Init(imgFlags) & imgFlags) {
                bool quit = false;


                SDL_Texture *texture = IMG_LoadTexture(renderer, PATH_TO_IMAGE);
                int frame = 0;
                int selected_frame = 0;

                while (!quit) {         

                    SDL_RenderClear(renderer);

                    selected_frame = (frame / WALKING_ANIMATION_FRAMES);

                    SDL_Rect *currentClip = &gSpriteClips[selected_frame];
                    SDL_RenderCopy(renderer, texture, currentClip, &((SDL_Rect){ .x = 16, .y = 16, .h = 112, .w = 88}) );
                    SDL_RenderPresent(renderer);

                    printf("Selected frame: %d -- %d - %d\n", selected_frame, frame, WALKING_ANIMATION_FRAMES);

                    ++frame;
                    if ((frame / WALKING_ANIMATION_FRAMES) >= WALKING_ANIMATION_FRAMES) {
                        printf("Entrei aqui\n");
                        frame = 0;                        
                    }
                }
            }


        } else {
            printf("SDL_Init failed ON WINDOW: %s\n", SDL_GetError());
        }
    }

    //Destroy window
    SDL_DestroyRenderer(renderer);
    renderer = NULL;
    SDL_DestroyWindow(gWindow);
    gWindow = NULL;

    //Quit SDL subsystems
    IMG_Quit();
    SDL_Quit();


    return (EXIT_SUCCESS);
}

代码编译没有任何问题。 这是基于Lazy Foo第14课。

我遇到的问题是,在10或15秒后程序停止响应(窗口灰显了精灵动画停止)。 让我说清楚,动画确实有效,并且在程序冻结之前它会工作几次(我看它至少循环3到4次)。

我有一个调试printf,即使在程序窗口停止响应后也可以继续工作。

我最初认为我可能会查看gSpriteClips但是在printf中我总是看到等于或低于7的数字。

有没有人看到我遗漏的任何明显问题?

感谢。

1 个答案:

答案 0 :(得分:3)

您的主循环不会刷新SDL事件队列。从SDL教程中复制粘贴:

while( !quit ){
    // optionally your code comes here...

    // The following inner loop flushes the event queue.
    // (All events that have been queued up between two frames.)

    // declare an event struct somewhere
    SDL_Event event;

    /* Poll for events */
    while( SDL_PollEvent( &event ) ){   
        switch( event.type ){
            /* Keyboard event */
            /* Pass the event data onto PrintKeyInfo() */
            case SDL_KEYDOWN:
            case SDL_KEYUP:
                PrintKeyInfo( &event.key );
                break;

            /* SDL_QUIT event (window close) */
            case SDL_QUIT:
                quit = 1;
                break;

            default:
                break;
        }
    }

    // optionally your code comes here...
}

SDL事件循环基本上等同于典型的本机Windows GUI应用程序的Windows GetMessage()循环。不在Windows上的GUI应用程序中使用消息通常会导致灰色和卡住的窗口症状。