First game engine I made
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ui.c 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <SDL2/SDL.h>
  4. #include <SDL2/SDL_image.h>
  5. #include "ui.h"
  6. int rows, columns, lengthpix, heightpix;
  7. int *rowptr = &rows, *columnptr = &columns;
  8. void setupui(SDL_Window* win, SDL_Renderer* ren){
  9. printf("Set up UI handler.\n");
  10. window = win;//THESE TWO ARE CORRECT. THE RENDERER AND WINDOW EQUAL THE ABBREVIATED VERSIONS
  11. renderer = ren;
  12. IMG_Init(IMG_INIT_PNG);
  13. flippy = SDL_FLIP_NONE;
  14. }
  15. void destroyui(){
  16. printf("Destroyed UI handler.\n");
  17. SDL_FreeSurface(loadingsurface);
  18. SDL_DestroyTexture(loadingtexture);
  19. }
  20. void drawstring(char *message, int posx, int posy, int scale, int spacing){//scale, if in pixels and square, is what the value is.
  21. int i;
  22. int len = (strlen(message) - 1);
  23. int currow = 0;
  24. int curcolumn = 0;
  25. int value;
  26. //both of the rects below will be updated in a loop. THis just starts them out.
  27. //the internal selector rect
  28. letter.x = 0;
  29. letter.y = 0;
  30. letter.w = lengthpix / rows;//rows = 12
  31. letter.h = heightpix / columns;//colums = 40
  32. //the representation of the internal selector
  33. letterfinal.x = posx;
  34. letterfinal.y = posy;
  35. letterfinal.w = scale;
  36. letterfinal.h = scale;
  37. for(i = 0; i <= len; i++){
  38. //printf("\n%c\n:", message[i]);
  39. currow = 0;
  40. curcolumn = 0;
  41. value = 0;
  42. //get the ascii value and convert it to a value from the font sheet
  43. switch (message[i]){
  44. case 'A':
  45. value = 0;
  46. break;
  47. case 'B':
  48. value = 1;
  49. break;
  50. case 'C':
  51. value = 2;
  52. break;
  53. case 'D':
  54. value = 3;
  55. break;
  56. case 'E':
  57. value = 4;
  58. break;
  59. case 'F':
  60. value = 5;
  61. break;
  62. case 'G':
  63. value = 6;
  64. break;
  65. case 'H':
  66. value = 7;
  67. break;
  68. case 'I':
  69. value = 8;
  70. break;
  71. case 'J':
  72. value = 9;
  73. break;
  74. case 'K':
  75. value = 10;
  76. break;
  77. case 'L':
  78. value = 11;
  79. break;
  80. case 'M':
  81. value = 12;
  82. break;
  83. case 'N':
  84. value = 13;
  85. break;
  86. case 'O':
  87. value = 14;
  88. break;
  89. case 'P':
  90. value = 15;
  91. break;
  92. case 'Q':
  93. value = 16;
  94. break;
  95. case 'R':
  96. value = 17;
  97. break;
  98. case 'S':
  99. value = 18;
  100. break;
  101. case 'T':
  102. value = 19;
  103. break;
  104. case 'U':
  105. value = 20;
  106. break;
  107. case 'V':
  108. value = 21;
  109. break;
  110. case 'W':
  111. value = 22;
  112. break;
  113. case 'X':
  114. value = 23;
  115. break;
  116. case 'Y':
  117. value = 24;
  118. break;
  119. case 'Z':
  120. value = 25;
  121. break;
  122. case ' ':
  123. value = 26;
  124. break;
  125. case 'a':
  126. value = 27;
  127. break;
  128. case 'b':
  129. value = 28;
  130. break;
  131. case 'c':
  132. value = 29;
  133. break;
  134. case 'd':
  135. value = 30;
  136. break;
  137. case 'e':
  138. value = 31;
  139. break;
  140. case 'f':
  141. value = 32;
  142. break;
  143. case 'g':
  144. value = 33;
  145. break;
  146. case 'h':
  147. value = 34;
  148. break;
  149. case 'i':
  150. value = 35;
  151. break;
  152. case 'j':
  153. value = 36;
  154. break;
  155. case 'k':
  156. value = 37;
  157. break;
  158. case 'l':
  159. value = 38;
  160. break;
  161. case 'm':
  162. value = 39;
  163. break;
  164. case 'n':
  165. value = 40;
  166. break;
  167. case 'o':
  168. value = 41;
  169. break;
  170. case 'p':
  171. value = 42;
  172. break;
  173. case 'q':
  174. value = 43;
  175. break;
  176. case 'r':
  177. value = 44;
  178. break;
  179. case 's':
  180. value = 45;
  181. break;
  182. case 't':
  183. value = 46;
  184. break;
  185. case 'u':
  186. value = 47;
  187. break;
  188. case 'v':
  189. value = 48;
  190. break;
  191. case 'w':
  192. value = 49;
  193. break;
  194. case 'x':
  195. value = 50;
  196. break;
  197. case 'y':
  198. value = 51;
  199. break;
  200. case 'z':
  201. value = 52;
  202. break;
  203. case '1':
  204. value = 53;
  205. break;
  206. case '2':
  207. value = 54;
  208. break;
  209. case '3':
  210. value = 55;
  211. break;
  212. case '4':
  213. value = 56;
  214. break;
  215. case '5':
  216. value = 57;
  217. break;
  218. case '6':
  219. value = 58;
  220. break;
  221. case '7':
  222. value = 59;
  223. break;
  224. case '8':
  225. value = 60;
  226. break;
  227. case '9':
  228. value = 61;
  229. break;
  230. case '0':
  231. value = 62;
  232. break;
  233. case '.':
  234. value = 63;
  235. break;
  236. case ',':
  237. value = 64;
  238. break;
  239. case '!':
  240. value = 65;
  241. break;
  242. case 0x3F:
  243. value = 66;
  244. break;
  245. }
  246. //end of switch statement
  247. //THE LAST THING YOU WERE WORKING ON GOT A FLOATING POINT EXCEPTION, YOU DIVIDED BY ZERO SOMEWHERE :/
  248. //highly unstable, written at night, needs to betested tomorrow, reading loop?:
  249. //REMEMBER, ALL OF THIS IS ALREADY IN A LOOP
  250. //printf("\n\n\n this is rows before: %d. \n\n\n", rows);
  251. //int colcount;
  252. //int rowcount;
  253. int num = 0;
  254. for(curcolumn = 0; curcolumn <= columns; curcolumn++){
  255. //printf(" COLUMN: %d.", colcount);
  256. for(currow = 0; currow <= rows; currow++, num++){
  257. //printf(" ROW: %d.", currow);
  258. if(num == value){
  259. //printf("broken in row");
  260. break;
  261. }
  262. //num++;
  263. //currow++;
  264. }
  265. if(num == value){
  266. //printf("broken in column");
  267. break;
  268. }
  269. //curcolumn++;
  270. }
  271. letter.x = 5 * currow;
  272. letter.y = 5 * curcolumn;
  273. //printf(".ROW: %d, COLUMN: %d.", currow, curcolumn);
  274. //letter.w = lengthpix / rows;//rows = 12
  275. //letter.h = heightpix / columns;//colums = 40
  276. SDL_RenderCopyEx(renderer, fonttexture, &letter, &letterfinal, 0.0f, NULL, flippy);
  277. //END OF LOOP
  278. //the representation of the internal selector
  279. letterfinal.x += spacing * (scale /2);//USE THIS ONLY
  280. //letterfinal.y = posy + 0;
  281. //letterfinal.w += (scale + 3);
  282. //letterfinal.h = posy + scale;
  283. }
  284. }
  285. void setupfont(char *url, int rows, int columns){
  286. //printf("got to here\n");
  287. loadingsurface = IMG_Load(url);
  288. *rowptr = rows - 1;
  289. *columnptr = columns - 1;
  290. fonttexture = SDL_CreateTextureFromSurface(renderer, loadingsurface);
  291. SDL_QueryTexture(fonttexture, NULL, NULL, &lengthpix, &heightpix);
  292. }
  293. void drawwindow(char *title, char *message, int ifbuttons, int iftitle, int xpos, int ypos, int timeonscreen){//the title and buttons, if both gone, will just make the window with the backdrop window
  294. }