SDL鼠标处理机制续

来源:互联网 发布:谷歌输入法64位 linux 编辑:程序博客网 时间:2024/06/06 06:57

SDL鼠标处理机制续

 (2010-06-04 10:01:38)
转载
标签: 

鼠标处理

 

sdl

 

鼠标事件

 

杂谈

分类: IT技术
在C语言 + SDL编写俄罗斯方块(九)----鼠标处理机制中发现了一个比较普遍的问题,VS2005+SDL无法让SDL窗口和控制台窗口同时显示,造成无法显示屏幕坐标,凡是出现此种情况的环境可以采用下面代码:

#include <stdio.h>
#include <stdlib.h>
#include <sdl/sdl.h>

#define WIDTH   600
#define HEIGHT  400
#define CPP     32

SDL_Surface* screen = NULL;

//用于接收系统事件消息
SDL_Event event;

//function name: mouseEvent

//parameter: none

//return: void

//function: test mouse event


void mouseEvent(void)
{
         int quit = 1;
         int i = 0;

         SDL_Rect rect;
      
        rect.x = 0;
        rect.y = 0;
        rect.w = WIDTH;
        rect.h = HIEGHT;


         while(quit)
         {
                   //获取所有的系统事件消息

                   while(SDL_PollEvent(&event))
                   {

                            //通过switch语句处理不同的事件消息

                            switch(event.type)

                            {

                            //获取鼠标按下的事件消息

                            case SDL_MOUSEBUTTONDOWN:

                                     //通过switch语句确定按下的是哪个鼠标键

                                     switch(event.button.button)
                                     {

                                     //按下的是左健

                                     case SDL_BUTTON_LEFT:                                              
                                               SDL_FillRect(screen, &rect, i+=20);
                                               SDL_Flip(screen);
                                               break; 

                                     //按下的是右键
                                     case SDL_BUTTON_RIGHT:
                                             
 SDL_FillRect(screen, &rect, i-=20);    
                                             
 SDL_Flip(screen);                                        
                                              break;


                                     //按下的是中键
                                     case SDL_BUTTON_MIDDLE:
                                              
 SDL_FillRect(screen, &rect, 0);    
                                              
 SDL_Flip(screen);                                          
                                               break;

                                     }

                             //获取鼠标松开的事件消息
                            case SDL_MOUSEBUTTONUP:

                                     //通过switch语句确定松开的是哪个鼠标键
                                     switch(event.button.button)
                                     {

                                     //松开的是左健

                                     case SDL_BUTTON_LEFT:

                                              //松开左键后的代码在这里执行

                                               break;
 

                                     //松开的是右键

                                     case SDL_BUTTON_RIGHT:

                                              //松开右键后的代码在这里执行

                                               break;

 

                                     //按下的是中键

                                     case SDL_BUTTON_MIDDLE:

                                              //松开中键后的代码在这里执行

                                               break;

                                     }

                            }

                   }

         }
}


//function name: initSDL
//parameter: void
//return: int
//1: initial SDL normally
//0: initial SDL abnormally
//function: initial SDL

int initSDL(void)
{
  
    //init all SDL sub-system
    if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
    {
        printf( "SDL init failed...\n" );
        return 0;    //init failed
    }
  
    //init screen
    screen = SDL_SetVideoMode(WIDTH,HEIGHT,CPP,SDL_SWSURFACE|SDL_DOUBLEBUF);
    if(NULL == screen)
    {
        printf("init screen failed, please check your display card.\n");
        return 0;    //init screen failed  
    }

    SDL_WM_SetCaption("SDL keyboard",NULL);
    return 1;
}


//function name: quitSDL
//parameter: none
//return: void
//function: close SDL

void quitSDL(void)
{
    SDL_Quit();
}


int main(int argc , char *argv[])
{

    //initial SDL
    initSDL();

    //testing mouse
    mouseEvent();

    //close SDL
    quitSDL();

    system("pause");
    return 0;
}