我与SDL瞎搞之前,我决定把打开的图像为背景,以主“屏幕”的程序,并使其动态的,所以不同大小的图像将导致不同大小的屏幕。问题是,我不明白为什么它不让我。我可以编译,但是当我尝试创建屏幕时,程序会报错,因为背景图像因某些未知原因而被清空。任何想法为什么?SDL使影像画面不工作
程序是非常小:
#include <iostream>
#include "SDL/SDL.h"
#define bpp 32
int init(){
if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
return 1;
// will init ttf later
return 0;
}
SDL_Surface * open_image(std::string image_name){
SDL_Surface * loaded_image = SDL_LoadBMP(image_name.c_str());
if (loaded_image){
SDL_Surface * optimized_image = SDL_DisplayFormat(loaded_image); // becomes NULL
SDL_FreeSurface(loaded_image);
return optimized_image;
}
return loaded_image;
}
int main(int argc, char * argv[]){
if (init())
return 1;
SDL_Surface * background = open_image("hello.bmp");
SDL_Surface * screen = SDL_SetVideoMode(background -> w, background -> h, bpp, SDL_SWSURFACE); // erroring here
SDL_BlitSurface(background, NULL, screen, NULL);
if (SDL_Flip(screen) == -1)
return 1;
bool quit = false;
SDL_Event event;
while (!quit)
while (SDL_PollEvent(&event))
if (event.type == SDL_QUIT)
quit = true;
return 0;
}
我收到提示3,这显然是访问不存在的值。在这种情况下,背景为w和h,因为背景为NULL,即使它不应该是
是编译错误或运行时错误? –
我编辑了我的问题。它的运行时错误 – calccrypto
所以错误是在open_image?从哪里来? SDL_LoadBMP已经导致NULL还是SDL_DisplayFormat? – Bart