app.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. #ifndef APP_H
  2. #define APP_H
  3. #include <GL/glew.h>
  4. #include <SDL2/SDL.h>
  5. #include "da.h"
  6. #include "font.h"
  7. #include "vec2.h"
  8. #include "window.h"
  9. #include "buffer.h"
  10. struct app {
  11. struct window win;
  12. SDL_Renderer *rdr;
  13. SDL_GLContext glctx;
  14. struct font font;
  15. struct buffer buf;
  16. bool running;
  17. uint64_t target_fps;
  18. struct {
  19. uint8_t tab_size;
  20. } cfg;
  21. bool show_fps;
  22. struct {
  23. int32_t time;
  24. int32_t resolution;
  25. int32_t scale;
  26. int32_t cursor;
  27. int32_t camera;
  28. } uniforms;
  29. };
  30. struct app app_create(const char *win_title);
  31. void app_destroy(struct app app);
  32. void app_set_text_color(struct app app, uint32_t color);
  33. uint32_t app_get_text_color(struct app app);
  34. void app_render_cursor_in_pos(struct app app, struct vec2 pos, uint32_t color);
  35. void app_render_cursor(struct app app, uint32_t color);
  36. void app_render_char(struct app app, const char c, struct vec2 pos,
  37. double scale);
  38. void app_render_buffer(struct app app, struct vec2 pos,
  39. uint32_t color, double scale);
  40. void app_render_text(struct app app, const char *text, size_t text_size,
  41. struct vec2 pos, uint32_t color, double scale);
  42. struct vec2 app_calc_cur_pos(struct app app);
  43. void MessageCallback(GLenum source, GLenum type, GLuint id,
  44. GLenum severity, GLsizei length,
  45. const GLchar *msg, const void *up);
  46. struct vec2i app_calc_cursor(struct app app);
  47. #if defined(APP_IMP) || defined(IMP)
  48. #define UNHEX_COLOR(hex) \
  49. (uint8_t) ((hex >> 24) & 0xFF), \
  50. (uint8_t) ((hex >> 16) & 0xFF), \
  51. (uint8_t) ((hex >> 8 ) & 0xFF), \
  52. (uint8_t) ((hex >> 0 ) & 0xFF)
  53. #include <GL/glew.h>
  54. #include "sc.h"
  55. struct app
  56. app_create(const char *win_title)
  57. {
  58. SCE(SDL_Init(SDL_INIT_VIDEO));
  59. struct app app = {
  60. .win = window_create(win_title),
  61. .rdr = NULL,
  62. .running = true,
  63. .buf = buffer_create(),
  64. .cfg = {
  65. .tab_size = 8
  66. },
  67. .target_fps = 120,
  68. };
  69. {
  70. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
  71. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
  72. SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
  73. SDL_GL_CONTEXT_PROFILE_CORE);
  74. int32_t major = 0;
  75. int32_t minor = 0;
  76. SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &major);
  77. SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &minor);
  78. printf("GL Version: %d.%d\n", major, minor);
  79. }
  80. /* SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, GL_DOUBLEBUFFER); */
  81. /* SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1); */
  82. /* SDL_GL_SetSwapInterval(0); */
  83. app.glctx = SDL_GL_CreateContext(app.win.ptr);
  84. SCP(app.glctx);
  85. if ( GLEW_OK != glewInit() ) {
  86. fprintf(stderr, "Could Not Initilize GLEW!\n");
  87. exit(EXIT_FAILURE);
  88. }
  89. if ( ! GLEW_ARB_draw_instanced ) {
  90. fprintf(stderr,
  91. "WARNING! GLEW_ARB_draw_instanced is not availible\n");
  92. exit(EXIT_FAILURE);
  93. }
  94. if ( ! GLEW_ARB_instanced_arrays ) {
  95. fprintf(stderr,
  96. "WARNING! GLEW_ARB_instanced_arrays"
  97. " is not availible\n");
  98. exit(EXIT_FAILURE);
  99. }
  100. if ( GLEW_ARB_debug_output ) {
  101. glEnable(GL_DEBUG_OUTPUT);
  102. glDebugMessageCallback(MessageCallback, 0);
  103. } else {
  104. fprintf(stderr,
  105. "WARNING! GLEW_ARB_debug_output is not availible\n");
  106. }
  107. glEnable(GL_BLEND);
  108. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  109. return app;
  110. }
  111. void
  112. app_destroy(struct app app)
  113. {
  114. SDL_DestroyWindow(app.win.ptr);
  115. SDL_GL_DeleteContext(app.glctx);
  116. DA_DESTROY(app.buf.data);
  117. SDL_Quit();
  118. }
  119. void
  120. app_render_char(struct app app, const char c, struct vec2 pos, double scale)
  121. {
  122. SDL_Rect dst = {
  123. .x = (int32_t) pos.x,
  124. .y = (int32_t) pos.y,
  125. .w = (int32_t)(app.font.ch_w * scale),
  126. .h = (int32_t)(app.font.ch_h * scale)
  127. };
  128. SCE(SDL_RenderCopy(app.rdr, app.font.tex,
  129. app.font.glyphs + (c - 32),
  130. &dst));
  131. }
  132. void
  133. app_set_text_color(struct app app, uint32_t color) {
  134. uint8_t r = (uint8_t) ((color >> 24) & 0xFF);
  135. uint8_t g = (uint8_t) ((color >> 16) & 0xFF);
  136. uint8_t b = (uint8_t) ((color >> 8 ) & 0xFF);
  137. uint8_t a = (uint8_t) ((color >> 0 ) & 0xFF);
  138. SCE(SDL_SetTextureColorMod(app.font.tex, r, g, b));
  139. SCE(SDL_SetTextureAlphaMod(app.font.tex, a));
  140. }
  141. uint32_t
  142. app_get_text_color(struct app app) {
  143. uint8_t r = 0;
  144. uint8_t g = 0;
  145. uint8_t b = 0;
  146. uint8_t a = 0;
  147. SCE(SDL_GetTextureColorMod(app.font.tex, &r, &g, &b));
  148. SCE(SDL_GetTextureAlphaMod(app.font.tex, &a));
  149. return (r << 24) | (g << 16) | (b << 8) | (a << 0);
  150. }
  151. #define BRANCHLESS_IF(cond, when_true, when_false) \
  152. (((cond)) * (when_true) + (!(cond)) * (when_false))
  153. void
  154. app_render_buffer(struct app app, struct vec2 pos,
  155. uint32_t color, double scale)
  156. {
  157. app_set_text_color(app, color);
  158. struct buffer buf = app.buf;
  159. DA_DEF_STRUCT_ITEM(char, da);
  160. DA_ASSIGN(da, buf.data);
  161. struct vec2 pen = pos;
  162. size_t i = buf.render_start;
  163. char c;
  164. for ( ; i < da.size; ++i ) {
  165. c = da.items[i];
  166. /* bool cond = (pen.x > app.win.w); */
  167. /* pen.x = BRANCHLESS_IF(cond, pos.x, pen.x); */
  168. /* pen.y += BRANCHLESS_IF(cond, */
  169. /* (double) (app.font.ch_h * scale), 0); */
  170. switch ( c ) {
  171. case '\n': {
  172. if ( i == app.buf.cur ) {
  173. app_render_cursor_in_pos(app, pen, 0xFF00FFFF);
  174. }
  175. pen.x = pos.x;
  176. pen.y += (double) (app.font.ch_h * scale);
  177. continue;
  178. } break;
  179. case '\t': {
  180. if ( i == app.buf.cur ) {
  181. app_render_cursor_in_pos(app, pen, 0xFF00FFFF);
  182. }
  183. pen.x += (double) ((app.font.ch_w * scale)
  184. * app.cfg.tab_size);
  185. continue;
  186. } break;
  187. }
  188. app_render_char(app, c, pen, scale);
  189. if ( i == app.buf.cur ) {
  190. app_render_cursor_in_pos(app, pen, 0xFF00FFFF);
  191. }
  192. pen.x += (double) (app.font.ch_w * scale);
  193. }
  194. if ( i == app.buf.cur ) {
  195. app_render_cursor_in_pos(app, pen, 0xFF00FFFF);
  196. }
  197. }
  198. void
  199. app_render_text(struct app app, const char *text, size_t text_size,
  200. struct vec2 pos, uint32_t color, double scale)
  201. {
  202. app_set_text_color(app, color);
  203. struct vec2 pen = pos;
  204. size_t i = 0;
  205. char c;
  206. for ( ; i < text_size; ++i ) {
  207. c = text[i];
  208. switch ( c ) {
  209. case '\n': {
  210. pen.x = pos.x;
  211. pen.y += (double) (app.font.ch_h * scale);
  212. continue;
  213. } break;
  214. case '\t': {
  215. pen.x += (double) ((app.font.ch_w * scale)
  216. * app.cfg.tab_size);
  217. continue;
  218. } break;
  219. }
  220. app_render_char(app, c, pen, scale);
  221. pen.x += (double) (app.font.ch_w * scale);
  222. }
  223. }
  224. void
  225. app_render_cursor_in_pos(struct app app, struct vec2 pos, uint32_t color)
  226. {
  227. SDL_Rect rect = {
  228. .x = (int32_t) pos.x,
  229. .y = (int32_t) pos.y,
  230. .w = (int32_t) (app.font.ch_w * app.font.scale),
  231. .h = (int32_t) (app.font.ch_h * app.font.scale)
  232. };
  233. SCE(SDL_SetRenderDrawColor(app.rdr, UNHEX_COLOR(color)));
  234. SCE(SDL_RenderFillRect(app.rdr, &rect));
  235. if ( app.buf.cur < app.buf.data.size ) {
  236. char c = app.buf.data.items[app.buf.cur];
  237. if ( c == '\n' ) return ;
  238. uint32_t old_color = app_get_text_color(app);
  239. app_set_text_color(app, ((~color) | 0xFF));
  240. app_render_char(app, c, pos, app.font.scale);
  241. app_set_text_color(app, old_color);
  242. }
  243. }
  244. void
  245. app_render_cursor(struct app app, uint32_t color)
  246. {
  247. struct vec2 pos = {
  248. .x = ((double)(app.buf.cur) * (app.font.ch_w * app.font.scale)),
  249. .y = ((double)(app.buf.cur) * (app.font.ch_h * app.font.scale))
  250. };
  251. app_render_cursor_in_pos(app, pos, color);
  252. }
  253. struct vec2
  254. app_calc_cur_pos(struct app app)
  255. {
  256. size_t col = 0;
  257. size_t row = buffer_calc_cur_row(app.buf);
  258. enum buffer_err err;
  259. /* RET_UNWRAP2(col, row, */
  260. /* struct ret_size_t_size_t, */
  261. /* buffer_calc_cur_pos(app.buf)); */
  262. size_t start_line;
  263. RET_UNWRAP2(start_line, err, struct ret_size_t_err,
  264. buffer_index_bw_char(app.buf, app.buf.cur, '\n'));
  265. col = (app.buf.cur - start_line) - 1;
  266. if ( err == BUFFER_ERR_NOT_FOUND ) {
  267. col = app.buf.cur;
  268. }
  269. size_t tabs_n = 0;
  270. RET_UNWRAP2(tabs_n, err,
  271. struct ret_size_t_err,
  272. buffer_count_char_between(app.buf, '\t',
  273. start_line, app.buf.cur));
  274. col += ( tabs_n > 0 ) * ((tabs_n * app.cfg.tab_size) - tabs_n);
  275. double row_px = -(((double)row)
  276. * (app.font.ch_h * app.font.scale));
  277. double col_px = (((double) col) *
  278. (app.font.ch_w * app.font.scale));
  279. return vec2(col_px, row_px);
  280. }
  281. void
  282. MessageCallback(GLenum source, GLenum type, GLuint id,
  283. GLenum severity, GLsizei length,
  284. const GLchar *msg, const void *up)
  285. {
  286. (void) source; (void) id; (void) length; (void) up;
  287. fprintf(stderr,
  288. "GL CALLBACK: %s type = 0x%x, severity = 0x%x, message = %s\n",
  289. (type == GL_DEBUG_TYPE_ERROR ? "*** GL ERROR ***" : ""),
  290. type, severity, msg);
  291. }
  292. struct vec2i
  293. app_calc_cursor(struct app app)
  294. {
  295. size_t row, col;
  296. RET_UNWRAP2(col, row,
  297. struct ret_size_t_size_t, buffer_calc_cur_pos(app.buf));
  298. return vec2i((int32_t)col, (int32_t)row);
  299. }
  300. #endif /* defined(APP_IMP) || defined(IMP) */
  301. #endif