我爱代码 - 专业游戏安全与逆向论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 4575|回复: 0

SDL入门教程(五):7、鼠标事件演示,代码重用

[复制链接]

2381

主题

53

回帖

9145

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
9145
QQ
发表于 2017-12-19 09:51:38 | 显示全部楼层 |阅读模式

想要查看内容赶紧注册登陆吧!

您需要 登录 才可以下载或查看,没有账号?立即注册

x
7.1:演示程序源代码

        今天因为一个网上的朋友的请求,做个一个关于鼠标事件的演示程序。实际上,可以完全用到前面我们构造的类和类方法,这里送上主程序,供大家参考。其他两个文件和图片文件均不需要任何改变。
#include "SurfaceClass.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;
    ScreenSurface screen(SCREEN_WIDTH, SCREEN_HEIGHT);

   
//Create 2 SDL surface for the screen that just created.
    DisplaySurface backGround("bg.bmp", screen);
    DisplaySurface frontImage(
"image.bmp", screen);

   
//Blit backGround surface to screen and flip the screen.
    backGround.blit();
    screen.flip();

   
//moving image's coordinate.
    int xpos = 0;
   
int ypos = 0;
   
//mouse's coordinate.
    int px = 0;
   
int py = 0;
   
//moving image's size.
    const int IMG_WIDTH = 128;
   
const int IMG_HEIGHT = 128;
   
//main loop.
    bool gameOver = false;
   
while( gameOver == false ){
        
//press ESC or click X to quit.
        SDL_Event gameEvent;
        
while ( SDL_PollEvent(&gameEvent) != 0 ){
            
if ( gameEvent.type == SDL_QUIT ){
                gameOver
= true;
            }
            
if ( gameEvent.type == SDL_KEYUP ){
               
if ( gameEvent.key.keysym.sym == SDLK_ESCAPE ){
                    gameOver
= true;
                }
            }
            
//mouse event
            if ( gameEvent.type == SDL_MOUSEMOTION ) {
                px
= gameEvent.motion.x;
                py
= gameEvent.motion.y;
            }
        }

        
if ( xpos < px )
            xpos
++;
        
if ( xpos > px )
            xpos
--;
        
if ( ypos < py )
            ypos
++;
        
if ( ypos > py )
            ypos
--;

        backGround.blit(xpos, ypos, xpos, ypos, IMG_WIDTH, IMG_HEIGHT);
        frontImage.blit(xpos, ypos);
        screen.flip();
    }

   
return 0;
}

                     
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|手机版|Archiver|我爱代码 - 专业游戏安全与逆向论坛 ( 陇ICP备17000105号-1 )

GMT+8, 2025-2-24 05:37 , Processed in 0.047758 second(s), 21 queries .

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表