我最近开始使用SDL编程,我一直在关注gamedevgeek和lazyfoo的一些教程。我必须在屏幕上显示图像,但当我尝试移动它们时,我意识到屏幕没有正确更新,使精灵留下痕迹,就像在这张图片中一样:
正如您所看到的,精灵从左上角开始,并在其右下方的路径上留下了痕迹。
一些可以提供帮助的信息:
我尝试了什么:
这是我的代码。
#include "SDL.h"
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
int main ( int argc, char *argv[] )
{
SDL_Surface *screen, *temp, *sprite;
SDL_Rect rcSprite;
SDL_Event event;
Uint8 *keystate;
int colorkey, gameover;
SDL_Init(SDL_INIT_VIDEO);
// screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SDL_DOUBLEBUF | SDL_FULLSCREEN);
// screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SDL_HWSURFACE);
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SDL_SWSURFACE);
temp = SDL_LoadBMP("sprite.bmp");
sprite = SDL_DisplayFormat(temp);
SDL_FreeSurface(temp);
colorkey = SDL_MapRGB(screen->format, 255, 0, 255);
SDL_SetColorKey(sprite, SDL_SRCCOLORKEY | SDL_RLEACCEL, colorkey);
rcSprite.x = 0;
rcSprite.y = 0;
gameover = 0;
while (!gameover)
{
if (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_ESCAPE:
gameover = 1;
break;
}
break;
}
}
keystate = SDL_GetKeyState(NULL);
if (keystate[SDLK_LEFT] )
rcSprite.x -= 2;
if (keystate[SDLK_RIGHT] )
rcSprite.x += 2;
if (keystate[SDLK_UP] )
rcSprite.y -= 2;
if (keystate[SDLK_DOWN] )
rcSprite.y += 2;
SDL_BlitSurface(sprite, NULL, screen, &rcSprite);
// SDL_Flip(screen);
SDL_UpdateRect(screen, 0, 0, 0, 0);
}
SDL_FreeSurface(sprite);
SDL_FreeSurface(grass);
SDL_Quit();
return 0;
}
我做错了什么?毋庸置疑,我对linux世界以及覆盆子pi以及SDL都非常陌生:)
答案 0 :(得分:2)
您调用SDL_SetVideoMode()不正确,您的代码不应编译,启用编译器警告。
该函数有四个参数:
SDL_Surface *SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags);
所以它应该是这样的:
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32 , SDL_SWSURFACE);
if( !screen )
//print your warning and exit.
此外,您的屏幕表面永远不会被清除,精灵仍然存在。用精灵重绘整个屏幕或清除表面
SDL_FillRect( screen , NULL , 0x0 ) ; //fills the entire surface with 0x0 .