SDL动画没有显示任何内容

时间:2016-03-22 04:49:50

标签: c++ animation sdl c++14 sdl-2

这是我的代码,它什么都没显示。这是为了做一个简单的动画,但我的屏幕仍然是完全空白,我似乎无法弄清楚出了什么问题。我有所有需要的库,代码能够编译,但正如我之前所说,它的屏幕只是空白。

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>  
#include <iostream>
#include <stdlib.h>

using namespace std;
SDL_Texture *LoadTexture(std::string filepath, SDL_Renderer *renderTarget){
SDL_Texture *texture = nullptr;
SDL_Surface *surface = SDL_LoadBMP(filepath.c_str());
if (surface == NULL)
    std::cout << "Error1"<< std::endl; 
else{
    texture = SDL_CreateTextureFromSurface(renderTarget, surface);
    if(texture == NULL)
        std::cout << "Error2" << std::endl;
}

SDL_FreeSurface(surface);
return texture; 



}
int main( int argc, char *argv[] ){

const int FPS = 60;

SDL_Window *window = nullptr;
SDL_Texture *currentImage = nullptr; 
SDL_Renderer *renderTarget = nullptr; 
SDL_Rect playerRect;
SDL_Rect playerPosition;
int frameWidth, frameHeight;
int textureWidth, textureHeight; 
playerPosition.x = playerPosition.y = 0; 
playerPosition.w = playerPosition.h = 30; 
int frameTime = 0; 
SDL_Init( SDL_INIT_VIDEO );

int imgFlags = IMG_INIT_JPG|IMG_INIT_PNG;
if(!(IMG_Init(imgFlags) != imgFlags)){
    cout<< "Error: " << IMG_GetError() <<endl;
}


window = SDL_CreateWindow( "Lab 7 or whatever ", 
            SDL_WINDOWPOS_CENTERED,
            SDL_WINDOWPOS_CENTERED,
            800,
            600,
            SDL_WINDOW_RESIZABLE  );

renderTarget = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
currentImage = LoadTexture("naruto.png", renderTarget); 

if ( window == NULL){
    cout << "There is a problem"
    << SDL_GetError() << endl;

}

SDL_QueryTexture(currentImage, NULL, NULL, &textureWidth, &textureHeight);

frameWidth = textureWidth /4;
frameHeight = textureHeight /11; 

playerRect.x = playerRect.y = 0;
playerRect.w = frameWidth;
playerRect.h = frameHeight; 

//SDL_SetRenderDrawColor(renderTarget, 255, 255, 255, 0); 
SDL_Event event;
bool running = true;

while ( running ){

    while ( SDL_PollEvent ( &event ) ){

        if ( event.type == SDL_QUIT ){
            running = false;
            break;
        }

    }


    frameTime++;

    if(FPS / frameTime == 4) {
        frameTime = 0;
        playerRect.x += frameWidth;
        if(playerRect.x >= textureWidth) {
            playerRect.x = 0;
            //playerRect.y += frameHeight; 
        }
    }
    SDL_RenderClear(renderTarget);
    SDL_RenderCopy(renderTarget, currentImage, &playerRect, &playerPosition);
    SDL_RenderPresent(renderTarget); 

}

SDL_DestroyWindow( window );
SDL_DestroyTexture( currentImage );
SDL_DestroyRenderer( renderTarget);
window = nullptr;
currentImage = nullptr;
renderTarget = nullptr; 
SDL_Quit();
return 0;

}

1 个答案:

答案 0 :(得分:2)

SDL_LoadBMP只能加载BMP文件。使用SDL_image函数IMG_Load加载PNG文件。