SDL_easy.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * I wrote this header to be as portable as possible for other homebrew switch projects
  3. * The only thing that needs to be changed will be the name / number of textures you want to load
  4. * If you decide to use this header and add functions, please consider opening a pr for said functions
  5. * I would greatly appreaciate it :)
  6. *
  7. * TotalJustice.
  8. */
  9. #include <SDL2/SDL.h>
  10. #include <SDL2/SDL_image.h>
  11. #include <SDL2/SDL_ttf.h>
  12. #include <SDL2/SDL2_gfxPrimitives.h>
  13. #include <stdio.h>
  14. #include <stdint.h>
  15. #include <stdbool.h>
  16. #include <stdarg.h>
  17. #include <stdlib.h>
  18. #include "gfx/SDL_easy.h"
  19. static SDL_Window *main_window;
  20. static SDL_Renderer *main_renderer;
  21. SDL_Colour colours[] =
  22. {
  23. { 255, 255, 255, 255 }, //white
  24. { 140, 140, 140, 255 }, //grey
  25. { 81, 81, 81, 255 }, //dark grey
  26. { 0, 0, 0, 255 }, //black
  27. { 255, 192, 203, 255 }, //pink
  28. { 253, 52, 131, 1 }, //neon pink
  29. { 255, 105, 180, 255 }, //hotpink
  30. { 255, 165, 0, 255 }, //orange
  31. { 255, 255, 0, 255 }, //yellow
  32. { 255, 215, 0, 255 }, //gold
  33. { 139, 69, 19, 255 }, //brown
  34. { 255, 0, 0, 255 }, //red
  35. { 139, 0, 0, 255 }, //dark red
  36. { 0, 128, 0, 255 }, //green
  37. { 50, 205, 50, 255 }, //lime green
  38. { 0, 255, 255, 255 }, //aqua
  39. { 0, 128, 128, 255 }, //teal
  40. { 0, 191, 255, 255 }, //light blue
  41. { 0, 0, 255, 255 }, //blue
  42. { 131, 177, 218, 255 }, //jordy blue
  43. { 97, 115, 255, 255 }, //faint blue
  44. { 28, 33, 73, 255 }, //dark blue
  45. { 160, 32, 240, 255 }, //purple
  46. { 75, 0, 130, 255 }, //indigo
  47. { 245, 245, 220, 255 }, //beige
  48. { 45, 45, 45, 255 }, //n_black
  49. { 50, 50, 50, 255 }, //n_light_black
  50. { 114, 114, 114, 255 }, //n_light_silver
  51. { 128, 128, 128, 255 }, //n_silver
  52. { 178, 178, 178, 255 }, //n_bright_Silver
  53. { 70, 70, 70, 255 }, //n_dark_grey
  54. { 77, 77, 77, 255 }, //n_grey
  55. { 251, 251, 251, 255 }, //n_white
  56. { 0, 255, 200, 255 }, //n_cyan
  57. { 143, 253, 252, 255 }, //n_teal
  58. { 36, 141, 199, 255 }, //n_blue
  59. { 27, 161, 255, 255 }, //n_lightblue
  60. { 255, 177, 66, 255 }, //n_yellow
  61. { 250, 90, 58, 255 }, //n_red
  62. { 207, 36, 33, 255 }, //glacia_red
  63. { 41, 30, 125, 255 }, //glacia_blue
  64. { 99, 32, 160, 255 }, //glacia_purple
  65. { 242, 0, 32, 255 }, //tomi_red
  66. { 40, 48, 76, 255 }, //law_blue
  67. { 136, 49, 57, 255 }, //law_red
  68. { 37, 103, 75, 255 }, //law_green
  69. { 4, 38, 37, 255 }, //law_darkgreen
  70. { 28, 23, 62, 255 }, //law_purple
  71. { 117, 3, 65, 255 }, //law_fuchsia
  72. { 171, 61, 12, 255 }, //law_orange
  73. };
  74. SDL_Colour SDL_GetColour(Colour colour_option)
  75. {
  76. return colours[colour_option];
  77. }
  78. SDL_Window *SDL_GetWindow(void)
  79. {
  80. return main_window;
  81. }
  82. SDL_Texture *__SDL_Image_Load(SDL_Surface *surface)
  83. {
  84. if (!surface) return NULL;
  85. SDL_Texture *texture = SDL_CreateTextureFromSurface(main_renderer, surface);
  86. SDL_FreeSurface(surface);
  87. return texture;
  88. }
  89. SDL_Texture *SDL_ImageLoad(const char *path)
  90. {
  91. return IMG_LoadTexture(main_renderer, path);
  92. }
  93. SDL_Texture *SDL_ImageLoadMem(void *data, int size)
  94. {
  95. return IMG_LoadTexture_RW(main_renderer, SDL_RWFromMem(data, size), 1);
  96. }
  97. void SDL_DrawText(TTF_Font *font, int x, int y, Colour colour, const char *text, ...)
  98. {
  99. char full_text[0x100] = {0};
  100. va_list argv;
  101. va_start(argv, text);
  102. vsnprintf(full_text, sizeof(full_text), text, argv);
  103. va_end(argv);
  104. SDL_Colour col = SDL_GetColour(colour);
  105. SDL_Surface *surface = TTF_RenderUTF8_Blended(font, full_text, col);
  106. SDL_Texture *tex = SDL_CreateTextureFromSurface(main_renderer, surface);
  107. SDL_Rect pos = { pos.x = x, pos.y = y, pos.w = surface->w, pos.h = surface->h };
  108. SDL_FreeSurface(surface);
  109. SDL_RenderCopy(main_renderer, tex, NULL, &pos);
  110. SDL_DestroyTexture(tex);
  111. }
  112. void SDL_DrawTextCenterX(TTF_Font *font, int y, int clip_x, int clip_w, Colour colour, const char *text, ...)
  113. {
  114. char full_text[0x100] = {0};
  115. va_list argv;
  116. va_start(argv, text);
  117. vsnprintf(full_text, sizeof(full_text), text, argv);
  118. va_end(argv);
  119. int t_w = 0;
  120. int t_h = 0;
  121. SDL_GetTextSize(font, &t_w, &t_h, full_text);
  122. SDL_DrawText(font, ((clip_w - t_w) / 2) + clip_x, y, colour, full_text);
  123. }
  124. void SDL_DrawButton(TTF_Font *font, uint16_t btn, int x, int y, Colour colour)
  125. {
  126. SDL_Colour col = SDL_GetColour(colour);
  127. SDL_Surface *surface = TTF_RenderGlyph_Blended(font, btn, col);
  128. SDL_Texture *tex = SDL_CreateTextureFromSurface(main_renderer, surface);
  129. SDL_Rect pos = { pos.x = x, pos.y = y, pos.w = surface->w, pos.h = surface->h };
  130. SDL_FreeSurface(surface);
  131. SDL_RenderCopy(main_renderer, tex, NULL, &pos);
  132. SDL_DestroyTexture(tex);
  133. }
  134. void SDL_DrawImage(SDL_Texture *texture, int x, int y)
  135. {
  136. SDL_Rect pos = { pos.x = x, pos.y = y };
  137. SDL_QueryTexture(texture, NULL, NULL, &pos.w, &pos.h);
  138. SDL_RenderCopy(main_renderer, texture, NULL, &pos);
  139. }
  140. void SDL_DrawImageRotate(SDL_Texture *texture, int x, int y, double rotate)
  141. {
  142. SDL_Rect pos;
  143. SDL_Point center;
  144. SDL_QueryTexture(texture, NULL, NULL, &pos.w, &pos.h);
  145. pos.x = 0;
  146. pos.y = 0;
  147. int halfwidth = pos.w / 2;
  148. center.x = halfwidth;
  149. center.y = pos.h - halfwidth;
  150. SDL_RenderCopyEx(main_renderer, texture, NULL, &pos, rotate, &center, SDL_FLIP_NONE);
  151. }
  152. void SDL_DrawImageScale(SDL_Texture *texture, int x, int y, int w, int h)
  153. {
  154. SDL_Rect pos = { pos.x = x, pos.y = y, pos.w = w, pos.h = h };
  155. SDL_RenderCopy(main_renderer, texture, NULL, &pos);
  156. }
  157. void SDL_DrawShape(Colour colour, int x, int y, int w, int h, bool filled)
  158. {
  159. SDL_Colour col = SDL_GetColour(colour);
  160. SDL_Rect pos = { pos.x = x, pos.y = y, pos.w = w, pos.h = h };
  161. SDL_SetRenderDrawColor(main_renderer, col.r, col.g, col.b, col.a);
  162. filled ? SDL_RenderFillRect(main_renderer, &pos) : SDL_RenderDrawRect(main_renderer, &pos);
  163. }
  164. void SDL_DrawShapeEX(uint8_t r, uint8_t g, uint8_t b, uint8_t a, int x, int y, int w, int h, bool filled)
  165. {
  166. SDL_Rect pos = { pos.x = x, pos.y = y, pos.w = w, pos.h = h };
  167. SDL_SetRenderDrawColor(main_renderer, r, g, b, a);
  168. filled ? SDL_RenderFillRect(main_renderer, &pos) : SDL_RenderDrawRect(main_renderer, &pos);
  169. }
  170. void SDL_DrawShapeOutline(Colour colour, int x, int y, int w, int h, int thicc)
  171. {
  172. // top, right, bottom, left.
  173. SDL_DrawShape(colour, x, y, w, thicc, true);
  174. SDL_DrawShape(colour, x + w, y, thicc, h + thicc, true);
  175. SDL_DrawShape(colour, x, y + h, w, thicc, true);
  176. SDL_DrawShape(colour, x, y, thicc, h + thicc, true);
  177. }
  178. void SDL_DrawShapeOutlineEX(uint8_t r, uint8_t g, uint8_t b, uint8_t a, int x, int y, int w, int h, int thicc)
  179. {
  180. // top, right, bottom, left.
  181. SDL_DrawShapeEX(r, g, b, a, x, y, w, thicc, true);
  182. SDL_DrawShapeEX(r, g, b, a, x + w, y, thicc, h + thicc, true);
  183. SDL_DrawShapeEX(r, g, b, a, x, y + h, w, thicc, true);
  184. SDL_DrawShapeEX(r, g, b, a, x, y, thicc, h + thicc, true);
  185. }
  186. void SDL_DrawShapeRounded(Colour colour, int x, int y, int w, int h, int r)
  187. {
  188. SDL_Colour col = SDL_GetColour(colour);
  189. roundedBoxRGBA(main_renderer, x, y,x + w, y + h, r, col.r, col.g, col.b, 255);
  190. }
  191. void SDL_DrawShapeRoundedOutline(Colour colour, int x, int y, int w, int h, int r)
  192. {
  193. SDL_Colour col = SDL_GetColour(colour);
  194. roundedRectangleRGBA(main_renderer, x, y,x + w, y + h, r, col.r, col.g, col.b, 255);
  195. }
  196. void SDL_DrawCircle(Colour colour, int x, int y, int r)
  197. {
  198. SDL_Colour col = SDL_GetColour(colour);
  199. filledCircleRGBA(main_renderer, x, y, r, col.r, col.g, col.b, 255);
  200. }
  201. void SDL_DrawCircleOutline(Colour colour, int x, int y, int r)
  202. {
  203. SDL_Colour col = SDL_GetColour(colour);
  204. circleRGBA(main_renderer, x, y, r, col.r, col.g, col.b, 255);
  205. }
  206. void SDL_SurfaceToFile(SDL_Surface *surface, const char *path, bool to_free)
  207. {
  208. SDL_SaveBMP(surface, path);
  209. SDL_FreeSurface(surface);
  210. }
  211. void SDL_TextureToFile(SDL_Texture *texture)
  212. {}
  213. SDL_Surface *__SDL_ScreenShot(void)
  214. {
  215. SDL_Surface *surface = SDL_CreateRGBSurface(0, SCREEN_W, SCREEN_H, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
  216. SDL_RenderReadPixels(main_renderer, NULL, SDL_PIXELFORMAT_ARGB8888, surface->pixels, surface->pitch);
  217. return surface;
  218. }
  219. SDL_Texture *SDL_ScreenShotToTexture(void)
  220. {
  221. return __SDL_Image_Load(__SDL_ScreenShot());
  222. }
  223. void SDL_ScreenShotToFile(const char *save_path)
  224. {
  225. SDL_SurfaceToFile(__SDL_ScreenShot(), save_path, true);
  226. }
  227. int SDL_GetTextureWidth(SDL_Texture *texture)
  228. {
  229. int w;
  230. SDL_QueryTexture(texture, NULL, NULL, &w, NULL);
  231. return w;
  232. }
  233. int SDL_GetTextureHeight(SDL_Texture *texture)
  234. {
  235. int h;
  236. SDL_QueryTexture(texture, NULL, NULL, NULL, &h);
  237. return h;
  238. }
  239. void SDL_GetTextSize(TTF_Font *font, int *w, int *h, const char *text, ...)
  240. {
  241. char full_text[0x100];
  242. va_list v;
  243. va_start(v, text);
  244. vsnprintf(full_text, sizeof(full_text), text, v);
  245. va_end(v);
  246. TTF_SizeUTF8(font, full_text, w, h);
  247. }
  248. void SDL_SetTextureColour(SDL_Texture *texture, Colour colour)
  249. {
  250. if (!texture) return;
  251. SDL_Colour col = SDL_GetColour(colour);
  252. SDL_SetTextureColorMod(texture, col.r, col.g, col.b);
  253. }
  254. void SDL_ClearRenderer(void)
  255. {
  256. SDL_SetRenderDrawColor(main_renderer, 0xff, 0xff, 0xff, 0xff);
  257. SDL_RenderClear(main_renderer);
  258. }
  259. void SDL_UpdateRenderer(void)
  260. {
  261. SDL_RenderPresent(main_renderer);
  262. }
  263. void SDL_EasyInit(Window_Flags w_flags, Renderer_Flags r_flags)
  264. {
  265. TTF_Init();
  266. SDL_Init(SDL_INIT_EVERYTHING);
  267. IMG_Init(IMG_INIT_PNG | IMG_INIT_JPG);
  268. main_window = SDL_CreateWindow("totaljustice", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_W, SCREEN_H, w_flags);
  269. main_renderer = SDL_CreateRenderer(main_window, -1, r_flags);
  270. SDL_SetRenderDrawBlendMode(SDL_GetRenderer(SDL_GetWindow()), SDL_BLENDMODE_BLEND);
  271. }
  272. void SDL_EasyExit(void)
  273. {
  274. SDL_DestroyRenderer(main_renderer);
  275. SDL_DestroyWindow(main_window);
  276. TTF_Quit();
  277. IMG_Quit();
  278. SDL_Quit();
  279. }