青春不能永驻

人无远虑,必有近忧!

借助SDL图库画个按钮

  其实我们点击的按钮都是用坐标画上去的,当我们点击按钮就是触发里一个事件,好我们将按钮的上部分图片的坐标往一个方向移动一点距离,当我们松开鼠标时就让图片回到原来的位置,这样就是一个具有立体感的按钮!

 

主函数:

#include "Button.h"

Button::Button( SDL_Surface *src){
  screen=src;
 }
 
void Button::drawn()
{
 SDL_Event event;
 while(1){
  SDL_WaitEvent(&event);
  switch(event.type)
  {
   case SDL_MOUSEBUTTONDOWN:
    if(event.button.x<=100 && event.button.x>=40)
    if(event.button.y>=40 && event.button.y<= 100){
     
     boxRGBA(screen,44,44,104,104,100,100,63,0xff);
     filledTrigonRGBA(screen,55,55,85,70,55,85,70,77,235,0xff);
     boxRGBA(screen,85,55,90,85 ,70,77,235,0xff);
     display();
    }
    break;
   case SDL_MOUSEBUTTONUP:
     boxRGBA(screen,43,43,103,103,233,231,231,0xff);
     boxRGBA(screen,40,40,100,100,133,124,122,0xff);
     filledTrigonRGBA(screen,55,55,85,70,55,85 ,69,55,67,0xff);
     boxRGBA(screen,85,55,90,85 ,69,55,67,0xff);
     filledTrigonRGBA(screen, 100,40,104,44,100,44,233,231,231,0xff);
     display();
     SDL_FillRect(screen,NULL,0x00);
     break;
   case SDL_QUIT:
    exit(1);
  }
 }
}

void Button::drawn1()
{
 boxRGBA(screen,43,43,103,103,233,231,231,0xff);
 boxRGBA(screen,40,40,100,100,133,124,122,0xff);
 filledTrigonRGBA(screen,55,55,85,70,55,85 ,69,55,67,0xff);
 boxRGBA(screen,85,55,90,85 ,69,55,67,0xff);
 filledTrigonRGBA(screen, 100,40,103,43,100,43,233,231,231,0xff);
 display();
 SDL_FillRect(screen,NULL,0x00);

}
void Button::display()
{
 
 SDL_UpdateRect(screen,0,0,0,0);

}

 

头文件

#ifndef BUTTON_H
#define BUTTON_H

#include<iostream>
using namespace std;
#include<SDL/SDL.h>
#include<SDL/SDL_gfxPrimitives.h>
class Button
{
 public:
 Button( SDL_Surface *src);
 void drawn();
 void drawn1();
 void display();
 private:
 SDL_Surface *screen;

};
#endif

 

阅读更多
文章标签: button null
个人分类: SDL
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭