Skip to content

Instantly share code, notes, and snippets.

@aneury1
Created September 28, 2017 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aneury1/c29b16dba13ef75cf4ff3d0fd61d3181 to your computer and use it in GitHub Desktop.
Save aneury1/c29b16dba13ef75cf4ff3d0fd61d3181 to your computer and use it in GitHub Desktop.
Button.c++
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
int main(int argc, char *argv[])
{
SDL_Window *w;
SDL_Renderer *render;
bool r= true;
SDL_Rect Button={150,150, 200, 48};
SDL_Rect text_pos={Button.x + 48 ,151, 100,32};
SDL_Color c1 = {66, 197, 244, 255};
SDL_Color c2 = {60, 180, 280, 255};
SDL_Color c4 = {51, 51, 51, 255};
bool mouse_clicked = false;
SDL_Color c3 = c1;
SDL_Init(SDL_INIT_EVERYTHING);
TTF_Init();
TTF_Font *font = TTF_OpenFont("C:\\Users\\aperez\\FONT\\Roboto-Black.ttf" ,16);
SDL_CreateWindowAndRenderer(500,500,SDL_WINDOW_SHOWN, &w,&render);
SDL_Color white ={0xff,0xff,0xff,0xff};
SDL_Color black = {0,0,0,0};
SDL_Surface * noclicked = TTF_RenderText_Solid(font, "Prueba ", white);
SDL_Surface * clicked = TTF_RenderText_Solid(font, "Prueba ", black);
SDL_Texture * text = NULL;
while(r){
SDL_Rect mouse;
SDL_Event e;
while(SDL_PollEvent(&e)){
if(e.type == SDL_QUIT)
r=false;
SDL_GetMouseState(&mouse.x, &mouse.y);
mouse.w = 1;
mouse.h = 1;
if(e.type == SDL_MOUSEBUTTONDOWN)
mouse_clicked = true;
else
mouse_clicked = false;
}
/* if(SDL_HasIntersection(&mouse, &Button)==SDL_TRUE)
c3 = c2;
else
c3 = c1;*/
if(SDL_HasIntersection(&mouse, &Button)==SDL_TRUE && mouse_clicked)
{
text = SDL_CreateTextureFromSurface(render, clicked );
c3 = c4;
}
else
{
text = SDL_CreateTextureFromSurface(render, noclicked );
if(SDL_HasIntersection(&mouse, &Button)==SDL_TRUE)
c3 = c2;
else
c3 = c1;
}
SDL_SetRenderDrawColor(render,0xff,0xff,0xff,255);
SDL_RenderClear(render);
SDL_SetRenderDrawColor(render, c3.r, c3.g, c3.b, 255);
SDL_RenderFillRect(render, &Button);
SDL_RenderCopy(render, text, NULL, &text_pos);
SDL_RenderPresent(render);
SDL_DestroyTexture(text);
text = NULL;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment