image.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include <stdlib.h>
  2. #include <SDL2/SDL.h>
  3. #include "gfx/SDL_easy.h"
  4. #include "gfx/image.h"
  5. image_t *__create_image(SDL_Texture *texture, int x, int y, int w, int h)
  6. {
  7. image_t *image = malloc(sizeof(image_t));
  8. image->texture = texture;
  9. int curr_w;
  10. int curr_h;
  11. SDL_QueryTexture(image->texture, NULL, NULL, &curr_w, &curr_h);
  12. if (w) curr_w = w;
  13. if (h) curr_h = h;
  14. image->rect.x = x; image->rect.y = y; image->rect.w = curr_w; image->rect.h = curr_h;
  15. return image;
  16. }
  17. image_t *create_image_from_file(const char *file, int x, int y, int w, int h)
  18. {
  19. SDL_Texture * texture = SDL_ImageLoad(file);
  20. return __create_image(texture, x, y, w, h);
  21. }
  22. image_t *create_image_from_mem(void *mem, size_t mem_size, int x, int y, int w, int h)
  23. {
  24. SDL_Texture * texture = SDL_ImageLoadMem(mem, mem_size);
  25. return __create_image(texture, x, y, w, h);
  26. }
  27. void draw_image2(image_t *image)
  28. {
  29. if (!image) return;
  30. SDL_DrawImageScale(image->texture, image->rect.x, image->rect.y, image->rect.w, image->rect.h);
  31. }
  32. void draw_image_scale(image_t *image, int w, int h)
  33. {
  34. if (!image) return;
  35. SDL_DrawImageScale(image->texture, image->rect.x, image->rect.y, w, h);
  36. }
  37. void draw_image_position(image_t *image, int x, int y)
  38. {
  39. if (!image) return;
  40. SDL_DrawImageScale(image->texture, x, y, image->rect.w, image->rect.h);
  41. }
  42. void draw_image_set(image_t *image, int x, int y, int w, int h)
  43. {
  44. if (!image) return;
  45. SDL_DrawImageScale(image->texture, x, y, w, h);
  46. }
  47. void set_image(image_t *image, int x, int y, int w, int h)
  48. {
  49. if (!image) return;
  50. image->rect.x = x; image->rect.y = y; image->rect.w = w; image->rect.h = h;
  51. }
  52. void free_image(image_t *image)
  53. {
  54. if (!image) return;
  55. SDL_DestroyTexture(image->texture);
  56. free(image);
  57. }