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

本文介绍了一个简单的SDL应用程序,用于实时显示鼠标光标的位置坐标。通过不断创建和销毁TextSurface对象来更新显示的文本,该程序演示了如何在屏幕上动态显示鼠标位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

作者:龙飞

注意事项:
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;
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值