随笔 - 96  文章 - 255  trackbacks - 0
<2011年1月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
303112345

E-mail:zbln426@163.com QQ:85132383 长期寻找对战略游戏感兴趣的合作伙伴。

常用链接

留言簿(21)

随笔分类

随笔档案

SDL相关网站

我的个人网页

我的小游戏

资源下载

搜索

  •  

积分与排名

  • 积分 - 484564
  • 排名 - 37

最新评论

阅读排行榜

评论排行榜

作者:龙飞

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;
}
posted on 2008-03-02 22:02 lf426 阅读(3267) 评论(6)  编辑 收藏 引用 所属分类: SDL入门教程

FeedBack:
# re: SDL入门教程(五):7、鼠标事件演示,代码重用 2008-06-12 16:10 huahua
为什么我调试的时候,定义的类#include "SurfaceClass.h"调用是成功的,但是
ScreenSurface screen(SCREEN_WIDTH, SCREEN_HEIGHT);时却说无法识别 undefined reference to 'ScreenSurface ::..........................  回复  更多评论
  
# re: SDL入门教程(五):7、鼠标事件演示,代码重用[未登录] 2008-06-12 18:46 lf426
没有和SurfaceClass.cpp一起编译?  回复  更多评论
  
# re: SDL入门教程(五):7、鼠标事件演示,代码重用 2009-06-15 17:57 georangel
int game(int argc ,char* argv[])

//mouse event
if ( gameEvent.type == SDL_MOUSEMOTION ) {
// delta_x, delta_y 的默认值如果设置为0,
// 可以在需要刷新的时候,
// 在xpos, ypos没有改变之前擦除“变动目标”原来的区域
backGround.blit(xpos, ypos, xpos, ypos, IMG_WIDTH, IMG_HEIGHT);
px = gameEvent.motion.x;
py = gameEvent.motion.y;
}
  回复  更多评论
  
# re: SDL入门教程(五):7、鼠标事件演示,代码重用 2011-01-13 10:21 笑傲暖壶
您好,我想问一下,在SDL中能不能控制光标的大小?  回复  更多评论
  
# re: SDL入门教程(五):7、鼠标事件演示,代码重用[未登录] 2011-01-15 13:23 lf426
简单的说,不能。光标属于操作系统的GUI元素@笑傲暖壶
  回复  更多评论
  
# re: SDL入门教程(五):7、鼠标事件演示,代码重用 2013-04-07 17:30 ylguo
可以的,SDL里的光标是自己画的。并不是用的系统gui自带的那个鼠标,找找源码@笑傲暖壶
  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理