button.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include <SDL2/SDL.h>
  2. #include "gfx/button.h"
  3. #include "gfx/font.h"
  4. #include "gfx/SDL_easy.h"
  5. button_t *create_button(font_t *font, int x, int y, uint8_t colour, uint16_t button)
  6. {
  7. button_t *t = malloc(sizeof(button_t));
  8. t->rect.x = x;
  9. t->rect.y = y;
  10. SDL_Colour col = SDL_GetColour(colour);
  11. SDL_Surface *surface = TTF_RenderGlyph_Blended(font->fnt, button, col);
  12. t->tex = SDL_CreateTextureFromSurface(SDL_GetRenderer(SDL_GetWindow()), surface);
  13. SDL_QueryTexture(t->tex, NULL, NULL, &t->rect.w, &t->rect.h);
  14. SDL_FreeSurface(surface);
  15. return t;
  16. }
  17. void draw_button(button_t *button)
  18. {
  19. SDL_RenderCopy(SDL_GetRenderer(SDL_GetWindow()), button->tex, NULL, &button->rect);
  20. }
  21. void draw_button_position(button_t *button, int x, int y)
  22. {
  23. SDL_Rect pos = { x, y, button->rect.w, button->rect.h };
  24. SDL_RenderCopy(SDL_GetRenderer(SDL_GetWindow()), button->tex, NULL, &pos);
  25. }
  26. void draw_button_scale(button_t *button, int w, int h)
  27. {
  28. SDL_Rect pos = { button->rect.x, button->rect.y, w, h };
  29. SDL_RenderCopy(SDL_GetRenderer(SDL_GetWindow()), button->tex, NULL, &pos);
  30. }
  31. void draw_button_set(button_t *button, int x, int y, int w, int h)
  32. {
  33. button->rect.x = x;
  34. button->rect.y = y;
  35. button->rect.w = w;
  36. button->rect.h = h;
  37. draw_button(button);
  38. }
  39. void position_button(button_t *button, int x, int y)
  40. {
  41. button->rect.x = x;
  42. button->rect.y = y;
  43. }
  44. void set_button()
  45. {}
  46. void free_button(button_t *t)
  47. {
  48. SDL_DestroyTexture(t->tex);
  49. free(t);
  50. t = NULL;
  51. }