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】
注意事项:
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】
相关文章推荐
- SDL入门教程(九):5、文本反馈鼠标位置坐标信息
- SDL入门教程(九):3、文本反馈“按键”信息
- SDL入门教程(九):3、文本反馈“按键”信息
- 使用SDL动态显示鼠标的位置信息(四)
- SDL入门教程(九):1、在SDL图形窗口显示文本
- SDL入门教程(九):1、在SDL图形窗口显示文本
- SDL入门教程(九):2、显示文本的完整代码
- 在PV3D中获取鼠标点击位置的空间坐标信息
- SDL入门教程(二):3、中途装载与退出,SDL错误信息
- SDL入门教程(三):3、视频属性信息(VideoInfo)
- SDL入门教程(五):7、鼠标事件演示,代码重用
- SDL入门教程(五):7、鼠标事件演示,代码重用
- SDL入门教程(二):3、中途装载与退出,SDL错误信息
- SDL入门教程(三):3、视频属性信息(VideoInfo)
- SDL入门教程(八):1、裁剪精灵图片(Clip Blitting and Sprite Sheets)
- 伟景行 citymaker 从入门到精通(3)——点击地图获取坐标,点击模型获取模型信息和属性信息
- 控制台上识别鼠标点击位置坐标
- SDL入门教程(九):4、int转换为std::string
- 用 javascript 获取鼠标(光标)的坐标位置 兼容IE/Firefox等浏览器
- SDL入门教程(十一):3、做一个对话框