我是SDL的新手,正在制作一个可以做一些基本图像blitting的程序。
程序运行没有错误,但图像没有显示在我创建的窗口中。 这是代码:
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <string>
bool quit = false;
SDL_Event xOut;
const int screenW = 640;
const int screenH = 480;
const int screenBPP = 32;
SDL_Surface *window = NULL;
SDL_Surface *background = NULL;
SDL_Surface *backgroundOPT = NULL;
SDL_Surface *loadIMG(std::string filename, SDL_Surface *image, SDL_Surface *imageOPT)
{
image = IMG_Load(filename.c_str());
imageOPT = SDL_DisplayFormat(image);
SDL_FreeSurface(image);
return imageOPT;
}
void applyIMG(int x, int y, SDL_Surface *screen, SDL_Surface *imageBlit)
{
SDL_Rect imgPosition;
imgPosition.x = x;
imgPosition.y = y;
SDL_BlitSurface(imageBlit, NULL, screen, &imgPosition);
}
int main(int argc, char* args[])
{
SDL_Init(SDL_INIT_EVERYTHING);
window = SDL_SetVideoMode(screenW, screenH, screenBPP, SDL_SWSURFACE);
while(quit == false)
{
loadIMG("arena.png", background, backgroundOPT);
applyIMG(0, 0, window, backgroundOPT);
SDL_Flip(window);
SDL_PollEvent(&xOut);
if(xOut.type == SDL_QUIT)
{
quit = true;
}
}
SDL_FreeSurface(backgroundOPT);
SDL_Quit();
return 0;
}
任何帮助将不胜感激。
答案 0 :(得分:0)
在你的功能中
SDL_Surface *loadIMG(std::string filename, SDL_Surface *image, SDL_Surface *imageOPT);
指针imageOPT正在函数调用中被复制:
loadIMG("arena.png", background, backgroundOPT);
这意味着您没有设置backgroundOPT
,而是设置它的副本。
简单地做这样的事情:
backgroundOPT = loadIMG("arena.png", background, backgroundOPT);
或者将您的函数原型更改为:
SDL_Surface *loadIMG(std::string filename, SDL_Surface *image, SDL_Surface **imageOPT);
并称之为:
loadIMG("arena.png", background, &backgroundOPT);