VC++ C++ C# Algorithm

C++博客 首页 新随笔 联系 聚合 管理
  21 Posts :: 3 Stories :: 31 Comments :: 0 Trackbacks

很多游戏都是用鼠标控制的,所以说处理鼠标事件也是非常重要的,鼠标事件和键盘事件处理方式差的不太多,所以我就直接给出了一个小程序,该程序把窗口一分为二,当在左半部分移动时,左面部分就变绿色,右面部分变黑色,在右半部分移动时,该部分就变蓝色,左半部分就变成了黑色,当鼠标左击时,对应的部分将会变红色。

#include  " SDL.h "
#include 
" SDL_ttf.h "
SDL_Surface 
* screen = NULL;

TTF_Font 
* font =
NULL;
// screen to show on window

const   int  SCREEN_BPP = 32 ;



int  main(  int  argc,  char *
 args[] )
{
    
// Start SDL

     bool  quit = false ;
    SDL_Rect rectLeft;
    SDL_Rect rectRight;
    rectLeft.x
= 0
;
    rectLeft.y
= 0
;
    rectLeft.w
= 320
;
    rectLeft.h
= 480
;
    rectRight.x
= 320
;
    rectRight.y
= 0
;
    rectRight.w
= 640
;
    rectRight.h
= 480
;
    SDL_Init( SDL_INIT_EVERYTHING );
    
if (TTF_Init() ==- 1
)
        
return   false
;
    
    screen 
=  SDL_SetVideoMode(  600 480
, SCREEN_BPP, SDL_SWSURFACE );
    
if (screen ==
NULL)
        
return   false
;

    Uint32 colorBlue
= SDL_MapRGB(screen -> format, 0 , 0 , 255
);
    Uint32 colorGreen
= SDL_MapRGB(screen -> format, 0 , 255 , 0
);
    Uint32 colorRed
= SDL_MapRGB(screen -> format, 255 , 0 , 0
);
    Uint32 colorBlack
= SDL_MapRGB(screen -> format, 0 , 0 , 0
);
    SDL_Event 
event
;
    
while ( !
quit)
    
{
        
if (SDL_PollEvent( & event
))
        
{
            
if ( event .type  ==
 SDL_MOUSEMOTION)
            
{
                Uint16 x
= event
.motion.x;
                Uint16 y
= event
.motion.y;


                
if (x > 0   &&  x < 320   &&  y > 0   &&  y < 480
 )
                
{
                    SDL_FillRect(screen,
&
rectLeft,colorBlue);
                    SDL_FillRect(screen,
&
rectRight,colorBlack);
                }

                
if (x > 320   &&  x < 640   &&  y > 0   &&  y < 480  )
                
{
                    SDL_FillRect(screen,
&
rectRight,colorGreen);
                    SDL_FillRect(screen,
&
rectLeft,colorBlack);
                }

            }

            
if ( event .type  == SDL_MOUSEBUTTONDOWN)
            
{
                Uint16 x
= event
.motion.x;
                Uint16 y
= event
.motion.y;
                
if ( event .button.button  ==
 SDL_BUTTON_LEFT)
                
{
                    
if (x > 0   &&  x < 320   &&  y > 0   &&  y < 480
 )
                    
{
                        SDL_FillRect(screen,
&
rectLeft,colorRed);
                    }

                    
if (x > 320   &&  x < 640   &&  y > 0   &&  y < 480  )
                    
{
                        SDL_FillRect(screen,
&
rectRight,colorRed);
                    }

                }

            }

            
if ( event .type  ==  SDL_QUIT)
                quit
= true
;
        }

        
if (SDL_Flip(screen)  ==   - 1 )
        
{
            
return   false
;
        }

    }

    SDL_FreeSurface(screen);
    SDL_Quit();

    
return   0 ;    
}

mousedown.jpg
posted on 2007-03-12 20:10 大熊猫 阅读(1716) 评论(3)  编辑 收藏 引用

Feedback

# re: SDL游戏编程(8)鼠标事件 2007-04-17 17:48 brick
Orz...
正在学习SDL,受益。。。  回复  更多评论
  

# re: SDL游戏编程(8)鼠标事件 2007-07-12 12:34 溺水的鱼
有个问题,一般情况下我们用的都是主surface(即程序启动时由SDL_SetVideoMode创建的surface),此时往这个surface上用SDL_BlitSurface来blit一个虚拟surface(即由SDL_CreateRGBSurface创建的surface)时是可以的,但是如果把一个虚拟surface用SDL_BlitSurface来blit另一个虚拟surface上时为什么显示不出来  回复  更多评论
  

# re: SDL游戏编程(8)鼠标事件 2007-12-23 21:42 秦歌
顶  回复  更多评论
  


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