您的位置:首页 > 其它

SDL入门教程(九):5、文本反馈鼠标位置坐标信息

2012-09-11 13:02 513 查看
作者:龙飞

注意事项:

1、times.ttf文件请到C:/WINDOWS/Fonts下寻找并拷贝到资源目录下。

2、如果您使用VC2008,请用Release编译。原因是,似乎涉及到vector的操作,Runtime Library在debug的时候必须用Multi-theaded Debug DLL (MDd),而Release时候才用Multi-theaded DLL (MD)。而我们亲爱的SDL必须始终用MD,所以,请Release吧。

这是一个检验TextSurface构造函数和析构函数的好例子。我们每一次的鼠标移动,都会导致一个新的TextSurface对象被建立,然后很快的又消亡。

源代码:

//UVi Soft (2008)

//Long Fei (lf426), E-mail: zbln426@163.com

#include "SurfaceClass.h"

#include "int_to_string.h"

int game(int argc, char* argv[]);

int main(int argc ,char* argv[])

{

int mainRtn = 0;

try {

mainRtn = game(argc, argv);

}

catch ( const ErrorInfo& info ) {

info.show();

return -1;

}

return mainRtn;

}

int game(int argc ,char* argv[])

{

//Create a SDL screen.

const int SCREEN_WIDTH = 640;

const int SCREEN_HEIGHT = 480;

const std::string WINDOW_NAME = "Mouse Motion";

ScreenSurface screen(SCREEN_WIDTH, SCREEN_HEIGHT, WINDOW_NAME);

//Fill background.(default is black)

screen.fillColor();

screen.flip();

//Main loop.Press ESC or click X to quit.

bool gameOver = false;

SDL_Event gameEvent;

int x;

int y;

while( gameOver == false ){

while ( SDL_PollEvent(&gameEvent) != 0 ){

if ( gameEvent.type == SDL_MOUSEMOTION ) {

x = gameEvent.motion.x;

y = gameEvent.motion.y;

std::string mouse_at = "Mouse at: ";

mouse_at += ("x = " + int_to_string(x) + "; y = " + int_to_string(y) + ";");

TextSurface text("text", mouse_at, screen);

screen.fillColor();

text.blit();

screen.flip();

}

if ( gameEvent.type == SDL_QUIT ){

gameOver = true;

}

if ( gameEvent.type == SDL_KEYUP ){

if ( gameEvent.key.keysym.sym == SDLK_ESCAPE ){

gameOver = true;

}

}

}

}

return 0;

}

此文章来自于【/article/10941201.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: