标签:
是的,按钮控件很常见,几乎在每一个Windows窗体内都能找到它的身影。SDL作为一套“一套开放源代码的跨平台多媒体开发库”,自然可以实现按钮。而按钮实现的重点,就是SDL的鼠标响应事件。
SDL的鼠标事件包括鼠标移动事件、按下鼠标键、松开鼠标键,和键盘一样,当你移动鼠标时发生鼠标移动事件,按下一个鼠标键,比如说左键时,发生按下鼠标键事件,松开鼠标键时会发生松开鼠标键事件。可以通过
SDL_PollEvent(&Event);
来把上一个事件从事件队列中取出。接下来可以进行判断,如果其type为SDL_MOUSEBUTTONDOWN则为鼠标按下,这时可以进行按下按钮后该做的事情,并把按钮的图片更新为按下后的样式。如果type为SDL_MOUSEBUTTONUP则为鼠标松开,则把按钮图片样式恢复未按下的情况。SDL_MOUSEMOTION表示鼠标在窗口表面移动,这时候可以把按钮更新到被指向的样式。当然,以上的一切更新,都是在鼠标的xy坐标位于按钮上才进行的。
懂了鼠标事件,SDL实现按钮就很容易了。下面我直接贴上源代码,如果要背景图片和按钮图片可以点击下载。这个程序还有一些缺陷,主要是按钮图片有渐变色,去背景不彻底导致不美观。大家可以自己修改按钮图片。当然修改时要把程序中原来的图片宽、高的信息修改。
#include <iostream> #include <cstdlib> #include <SDL2/SDL.h> #include <SDL2/SDL_image.h> const Uint32 H=650,W=1000; const Uint32 FPS=20; const char* BuFile[]={"B1.png","B2.png","B3.png"}; SDL_Window *MainWin=NULL; SDL_Surface *BackGround=NULL,*ButtonSur=NULL; SDL_Renderer *Render=NULL; SDL_Texture *BackText=NULL,*ButtonText=NULL; SDL_Rect ButtRect={0,0,64,64}; SDL_Event Mouse; using namespace std; bool System_Init(); bool DrawBackGround(); bool UpdateButton(Uint32); bool isOnButton(Uint32,Uint32); void halt(); void Destroy(); int main(int argc,char *argv[]){ if (!System_Init()) halt(); if (!DrawBackGround()) halt(); bool quit=false; Uint32 _FPS_Timer=0,Buttonnow=0; if (!UpdateButton(0)) halt(); while (!quit){ while (SDL_PollEvent(&Mouse)){ switch (Mouse.type){ case SDL_QUIT: quit=true; break; case SDL_MOUSEBUTTONDOWN: if (Mouse.button.button==SDL_BUTTON_LEFT) if (isOnButton(Mouse.button.x,Mouse.button.y)) if (Buttonnow!=2){ if (!UpdateButton(2)) halt(); Buttonnow=2; } break; case SDL_MOUSEMOTION: if (isOnButton(Mouse.button.x,Mouse.button.y)){ if (Buttonnow!=1){ if (!UpdateButton(1)) halt(); Buttonnow=1; }}else if (Buttonnow!=0){ if (!UpdateButton(0)) halt(); Buttonnow=0; } break; case SDL_MOUSEBUTTONUP: if (Buttonnow!=0){ if (!UpdateButton(0)) halt(); Buttonnow=0; } } } if(SDL_GetTicks()-_FPS_Timer<1000/FPS) SDL_Delay(1000/FPS-SDL_GetTicks()+_FPS_Timer); _FPS_Timer=SDL_GetTicks(); } return 0; } void halt(){ cerr<<SDL_GetError()<<endl; Destroy(); exit(-1); } bool System_Init(){ if (SDL_Init(SDL_INIT_VIDEO)==-1) return false; if (IMG_Init(IMG_INIT_PNG)==-1) return false; MainWin=SDL_CreateWindow("Button Test",540,270,W,H,SDL_WINDOW_SHOWN); if (MainWin==NULL) return false; Render=SDL_CreateRenderer(MainWin,-1,SDL_RENDERER_ACCELERATED); if (Render==NULL) return false; return true; } bool DrawBackGround(){ BackGround=IMG_Load("BG.png"); if (BackGround==NULL) return false; BackText=SDL_CreateTextureFromSurface(Render,BackGround); if (BackText==NULL) return false; SDL_RenderClear(Render); SDL_RenderCopy(Render,BackText,NULL,NULL); SDL_RenderPresent(Render); return true; } void Destroy(){ SDL_FreeSurface(BackGround); SDL_DestroyWindow(MainWin); SDL_DestroyRenderer(Render); SDL_DestroyTexture(BackText); IMG_Quit(); SDL_Quit(); } bool UpdateButton(Uint32 ButtonMode){ DrawBackGround(); const char* OpenButton=BuFile[ButtonMode]; ButtonSur=IMG_Load(OpenButton); Uint32 color_key=SDL_MapRGB(ButtonSur->format,255,255,255); SDL_SetColorKey(ButtonSur,SDL_TRUE,color_key); if (ButtonSur==NULL) return false; ButtonText=SDL_CreateTextureFromSurface(Render,ButtonSur); if (BackText==NULL) return false; SDL_RenderCopy(Render,ButtonText,NULL,&ButtRect); SDL_RenderPresent(Render); return true; } bool isOnButton(Uint32 x,Uint32 y){ if (x>=0&&x<=64) if (y>=0&&y<=64) return true; return false; }
标签:
原文地址:http://www.cnblogs.com/Darksun/p/4320974.html