[已解决]我正在关注本教程(视频),我就在此时此刻。我在编译时没有问题,但是在运行程序时它只是闪烁一秒钟然后关闭。所以我运行了调试器,发现SDL_DisplayFormat
我的load_image
函数中有一个分段错误,当我注释掉加载图像的行时,它工作正常,但我找不到问题的原因。
[解决方案] 我没有填写 .bmp 文件,这就是 SDL_DisplayFormat 不起作用的原因。提交图纸后,一切都开始工作了。
这是我的代码:
#include "game.h"
/*TODO
*stworzyć plik z blokami "blocks.bmp"
*stworzyć plik z tlem "background.bmp"
*uzywac jako tla do obrazkow 000,255,255
*
*/
game::game()
{
//zaladowanie ekranu
SDL_Init(SDL_INIT_EVERYTHING);
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGTH, 32, SDL_SWSURFACE);
//ladowanei obrazkow
//ustawianie polozenia i rozmiaru kamery
camera.x = camera.y = 0;
camera.w = SCREEN_WIDTH;
camera.h = SCREEN_HEIGTH;
//inicjalizacja kierunku w kotrym sie poruszamy
direction[0] = direction[1] = 0;
running = true;
block = (load_image("blocks.bmp"));
background = (load_image("background.bmp"));
}
game::~game()
{
SDL_FreeSurface(block);
SDL_FreeSurface(background);
SDL_Quit();
}
SDL_Surface* game::load_image (const char* filename)
{
SDL_Surface* tmp = SDL_LoadBMP(filename);
SDL_Surface* tmp2 = SDL_DisplayFormat(tmp);
//do odkomentowania kolorkey na razie zeby sprawdzac kolizje
// SDL_SetColorKey(tmp2, SDL_SRCCOLORKEY, SDL_MapRGB(screen->format, 0x00, 0xff, 0xff)
SDL_FreeSurface(tmp);
return tmp2;
}
void game::handleEvents()
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
running = false;
return;
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_LEFT:
direction[0] = 1;
break;
case SDLK_RIGHT:
direction[1] = 1;
break;
}
break;
case SDL_KEYUP:
switch(event.key.keysym.sym)
{
case SDLK_LEFT:
direction[0] = 0;
break;
case SDLK_RIGHT:
direction[1] = 0;
break;
}
break;
}
}
}
void game::start()
{
while(1)
{
handleEvents();
SDL_Flip(screen);
}
}