sdl2.0的按钮控件实现原理小程序

10 篇文章 0 订阅

#include "SDL.h"
#include "SDL_thread.h"
#include"SDL_stdinc.h"
#include<stdio.h>
#include<string.h>

#define W 400
#define H 240
#define false 0
#define SHAPE_SIZE 150

SDL_Color color = { 178, 34, 34 };

float x = 0, y = 0;

SDL_Thread*th=NULL;
SDL_Event event; 

Uint32 window_id1;
Uint32 window_id2;
Uint32 window_id3;
Uint32 window_id4;
unsigned char touch_down_flag=0;
unsigned char touch_count=0;

void render_display(SDL_Renderer *ren_window,SDL_Texture *tex_window,const SDL_Rect* srcrect,const SDL_Rect* dstrect)
{
SDL_RenderCopy(ren_window, tex_window, NULL, NULL); 
SDL_RenderPresent(ren_window);
}
SDL_Texture *texture_creatfrom_surface(SDL_Renderer *ren_window,SDL_Surface *sur_window)
{
SDL_Texture *re_texture;
re_texture = SDL_CreateTextureFromSurface(ren_window,sur_window);
SDL_FreeSurface(sur_window);
return re_texture;

}

int getTouch(void*data)
{
Uint32 windowid;
while (1)
{

while (SDL_PollEvent(&event))
{
 if (event.type == SDL_MOUSEMOTION || event.type == SDL_MOUSEBUTTONDOWN)
  {
  windowid=event.motion.windowID;
  x = event.motion.x;
  y = event.motion.y;
  printf("X: %f Y: %f\n",x,y);
  printf("relative motion x:%f\n",event.motion.xrel);
  printf("relative motion y:%f\n",event.motion.yrel);
 }
      if(event.type == SDL_MOUSEBUTTONDOWN)
      { 
         while(event.type == SDL_MOUSEBUTTONDOWN){
         SDL_PollEvent(&event);
        touch_down_flag=1;
  

          if(event.type == SDL_MOUSEBUTTONUP){
               touch_down_flag=0;
             break;
          }
        }
     touch_count+=1;
     if(touch_count==2)touch_count=0;
    }
touch_down_flag=0;
}

}
}


int main(int argc, char *argv[])
{
SDL_Window *window_1 = NULL;
SDL_Window *window_2 = NULL;
SDL_Window *window_test_bmp = NULL;


SDL_Surface *sur_window1= NULL;
SDL_Surface *sur_window2= NULL;
SDL_Surface *sur_button_down= NULL;
SDL_Surface *sur_test_bmp= NULL;

SDL_Texture *tex_window1 = NULL;
SDL_Texture *tex_window2 = NULL;
SDL_Texture *tex_button_down = NULL;
SDL_Texture *tex_test_bmp = NULL;

SDL_Renderer *ren_window1 = NULL;
SDL_Renderer *ren_window2 = NULL;
SDL_Renderer *ren_test_bmp = NULL;
SDL_Renderer *ren_window4 = NULL;
char q = false;
  SDL_Surface* Loading_Surf;
  SDL_Texture* BlueShapes;

unsigned char con_flag=0;


  SDL_Rect SrcR;
  SDL_Rect DestR;

  SrcR.x = 0;
  SrcR.y = 0;
  SrcR.w = SHAPE_SIZE;
  SrcR.h = SHAPE_SIZE;

  DestR.x = 200 - SHAPE_SIZE / 2;
  DestR.y = 150 - SHAPE_SIZE / 2;
  DestR.w = SHAPE_SIZE;
  DestR.h = SHAPE_SIZE;

/*   creat window 1*/
window_1 = SDL_CreateWindow("Touch Test",0,0,800,480,0);
ren_window1 = SDL_CreateRenderer(window_1, -1, SDL_RENDERER_ACCELERATED);
sur_window1 = SDL_CreateRGBSurface(0, 800, 480, 32, 0, 0, 0, 0); 
  if(sur_window1==NULL){
fprintf(stderr,"creatrgbsurface failed :%s\n",SDL_GetError());
exit(1);
}
SDL_FillRect(sur_window1, NULL, 0xff0cc0);
tex_window1=texture_creatfrom_surface(ren_window1,sur_window1);
render_display(ren_window1,tex_window1,NULL,NULL);

/*   creat window 2*/
window_2 = SDL_CreateWindow("Touch Test",60,350,200,100,0);
ren_window2 = SDL_CreateRenderer(window_2, -1, SDL_RENDERER_ACCELERATED);
sur_window2 = SDL_CreateRGBSurface(0, W, H, 32, 0, 0, 0, 0); 
  if(sur_window2==NULL){
fprintf(stderr,"creatrgbsurface failed :%s\n",SDL_GetError());
exit(1);
}
SDL_FillRect(sur_window2, NULL, 0x230cc5);
tex_window2=texture_creatfrom_surface(ren_window2,sur_window2);



window_id1=SDL_GetWindowID(window_1);
window_id2=SDL_GetWindowID(window_2);


th=SDL_CreateThread(getTouch,NULL,NULL);

sur_button_down = SDL_CreateRGBSurface(0, W, H, 32, 0, 0, 0, 0); 
SDL_FillRect(sur_button_down, NULL, 0x03fcc5);
tex_button_down=texture_creatfrom_surface(ren_window2,sur_button_down);

window_test_bmp = SDL_CreateWindow("test_picture",200,0, 600, 300, 0);
ren_test_bmp = SDL_CreateRenderer(window_test_bmp, -1, SDL_RENDERER_ACCELERATED);
sur_test_bmp = SDL_LoadBMP("test.bmp");
tex_test_bmp =texture_creatfrom_surface(ren_test_bmp,sur_test_bmp);
render_display(ren_test_bmp,tex_test_bmp,NULL,NULL);

 Loading_Surf = SDL_LoadBMP("Blueshapes.bmp");
  BlueShapes = texture_creatfrom_surface(ren_test_bmp,Loading_Surf);


while (1)
{


//SDL_RenderClear(ren_window2);
//SDL_RenderClear(ren_test_bmp);


if(touch_down_flag==1)
render_display(ren_window2,tex_button_down,NULL,NULL);
else
render_display(ren_window2,tex_window2,NULL,NULL);

if(touch_count==1){
if(con_flag!=touch_count){
 SrcR.x = 0;
 SrcR.y = 0;
con_flag=touch_count;
}
}
if(touch_count==0){
if(con_flag!=touch_count){
SrcR.x = 0;
 SrcR.y = SHAPE_SIZE;
con_flag=touch_count;
}
}//*/
SDL_RenderCopy(ren_test_bmp,tex_test_bmp,NULL,NULL);
SDL_RenderCopy(ren_test_bmp, BlueShapes, &SrcR, &DestR);  
SDL_RenderPresent(ren_test_bmp);

printf("%d\n",touch_count);
}//*/


  SDL_DestroyTexture(tex_window1);
  SDL_DestroyRenderer(ren_window1);
  SDL_DestroyWindow(window_1);
  SDL_DestroyTexture(tex_window2);
  SDL_DestroyRenderer(ren_window2);
  SDL_DestroyWindow(window_2);
  SDL_DestroyTexture(tex_test_bmp);
  SDL_DestroyRenderer(ren_test_bmp);
  SDL_DestroyWindow(window_test_bmp);

  SDL_Quit();
 //\CAͷ\C5\E4\D6Ⱦ\C6\F7\BAʹ\B0\BFڲ\A2\CD˳\F6
}

本人亲调,有问题随便问,挺简单的,SDL做按钮控件就是这原理,把上面用到的函数封装到C++类里,就行了,明天我再做按钮类的封装,,
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值