图像未显示在SDL中

时间:2015-04-23 05:10:09

标签: c++ image sdl

当我运行它时它只显示一个黑屏,如果我把SDL_GetError()放在最后它会打印一个空白行.......

有关如何解决此问题的任何想法?

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

class character {

public: SDL_Rect src, cur;
public: SDL_Texture *image;

void setSrc(int x, int y, int w, int h) {

    src.x = x;
    src.y = y;
    src.w = w;
    src.h = h;

}

void setCur(int x, int y, int w, int h) {

    src.x = x;
    src.y = y;
    src.w = w;
    src.h = h;

}

};  

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

bool in = true;
character p1, p2, ball; 
SDL_Window *window = 0;
SDL_Renderer *renderer = 0;
SDL_Surface *screen, *imageLoader;

SDL_Init(SDL_INIT_VIDEO);

window = SDL_CreateWindow("Pong",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_SHOWN);

renderer = SDL_CreateRenderer(window, -1, 0);

p1.setSrc(0, 0, 100, 500);
p1.setCur(0, 0, 100, 500);

imageLoader = IMG_Load("/home/donaldo/Documents/Games/Images/player.bmp");

p1.image = SDL_CreateTextureFromSurface(renderer, imageLoader);

SDL_RenderCopy(renderer, p1.image, &p1.src, &p1.cur);

SDL_RenderPresent(renderer);

SDL_Delay(3000);

return 0;

}

1 个答案:

答案 0 :(得分:1)

您的setCur()功能中有拼写错误。我假设您要设置cur矩形,而不是src

此外,没有相关性,但是当你完成它们时,释放你分配的表面和纹理是个好习惯。