SDL演示程序,SDL1.2,显示一个图片并随鼠标移动

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

code::block下设置工程properties/build options中 compiler和linker设置好
加(SDL)\include\
加(SDL)\lib\
加(SDL)\lib\libsdl.a 两个.a都加上

建一CPP控制台程序代码如下
工程目录下放helloworld.bmp和SDL.dll

编译,运行
------------------

#include
using namespace std;
#include "SDL/SDL.h"
void pressESCtoQuit();
void InitialEvents();
void doSomeLoopThings();

SDL_Surface* pScreen;

SDL_Surface* limg;

SDL_Rect rec0;

int mouse_x = 0;
int mouse_y = 0;

int main(int argc,char* argv[]) {
try {
if ( SDL_Init(SDL_INIT_VIDEO == -1 ))
throw SDL_GetError();
} catch ( const char* s ) {
std::cerr << s << std::endl;
return -1;
}
atexit(SDL_Quit);

pScreen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
InitialEvents();
pressESCtoQuit();
return 0;
}

void pressESCtoQuit() {
bool gameOver = false;
while( gameOver == false ){
SDL_Event gameEvent;
SDL_PollEvent(&gameEvent);
if ( &gameEvent != 0 ){
if ( gameEvent.type == SDL_QUIT ){ gameOver = true; }
if ( gameEvent.type == SDL_KEYDOWN ){
if ( gameEvent.key.keysym.sym == SDLK_ESCAPE ){ gameOver = true; }
}if ( gameEvent.type == SDL_MOUSEMOTION ){
mouse_x = gameEvent.motion.x;
mouse_y = gameEvent.motion.y;
cout<
//If doSomeLoopThings() is here, the program will pause right on time the mouse leave the window

}
}
doSomeLoopThings();
}
return;
}

void InitialEvents(){
limg = SDL_LoadBMP("helloworld.bmp");
}

void doSomeLoopThings(){
SDL_FillRect(pScreen, 0, SDL_MapRGB(pScreen->format, 1, 1, 1));
rec0.x = mouse_x - limg->w / 2;
rec0.y = mouse_y - limg->h / 2;

SDL_BlitSurface(limg, 0, pScreen, &rec0);
SDL_Flip(pScreen);
//cout << "."; return;

}

相关文档
最新文档