app.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #ifndef APP_H
  2. #define APP_H
  3. #include <GL/glew.h>
  4. #include <SDL2/SDL.h>
  5. #include "da.h"
  6. #include "vec2.h"
  7. #include "window.h"
  8. #include "buffer.h"
  9. struct app {
  10. struct window win;
  11. SDL_Renderer *rdr;
  12. SDL_GLContext glctx;
  13. struct buffer buf;
  14. bool running;
  15. uint64_t target_fps;
  16. double dt;
  17. uint32_t last_press;
  18. struct {
  19. uint8_t tab_size;
  20. } cfg;
  21. bool show_fps;
  22. struct free_glyph_atlas *fga;
  23. struct cursor_render *cr;
  24. };
  25. #include "free_glyph.h"
  26. struct ret_app_vec2 {
  27. struct app f1;
  28. struct vec2 f2;
  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. struct ret_app_vec2 app_calc_buffer_cam(struct app app, double dt);
  48. #if defined(APP_IMP) || defined(IMP)
  49. #define UNHEX_COLOR(hex) \
  50. (uint8_t) ((hex >> 24) & 0xFF), \
  51. (uint8_t) ((hex >> 16) & 0xFF), \
  52. (uint8_t) ((hex >> 8 ) & 0xFF), \
  53. (uint8_t) ((hex >> 0 ) & 0xFF)
  54. #include <GL/glew.h>
  55. #include "sc.h"
  56. struct app
  57. app_create(const char *win_title)
  58. {
  59. SCE(SDL_Init(SDL_INIT_VIDEO));
  60. struct app app = {
  61. .win = window_create(win_title),
  62. .rdr = NULL,
  63. .running = true,
  64. .buf = buffer_create(),
  65. .cfg = {
  66. .tab_size = 8
  67. },
  68. .target_fps = 120,
  69. };
  70. app.dt = (1.0 / (double)app.target_fps) * 3;
  71. {
  72. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
  73. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
  74. SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
  75. SDL_GL_CONTEXT_PROFILE_CORE);
  76. int32_t major = 0;
  77. int32_t minor = 0;
  78. SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &major);
  79. SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &minor);
  80. printf("GL Version: %d.%d\n", major, minor);
  81. }
  82. /* SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, GL_DOUBLEBUFFER); */
  83. /* SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1); */
  84. /* SDL_GL_SetSwapInterval(0); */
  85. app.glctx = SDL_GL_CreateContext(app.win.ptr);
  86. SCP(app.glctx);
  87. if ( GLEW_OK != glewInit() ) {
  88. fprintf(stderr, "Could Not Initilize GLEW!\n");
  89. exit(EXIT_FAILURE);
  90. }
  91. if ( ! GLEW_ARB_draw_instanced ) {
  92. fprintf(stderr,
  93. "WARNING! GLEW_ARB_draw_instanced is not availible\n");
  94. exit(EXIT_FAILURE);
  95. }
  96. if ( ! GLEW_ARB_instanced_arrays ) {
  97. fprintf(stderr,
  98. "WARNING! GLEW_ARB_instanced_arrays"
  99. " is not availible\n");
  100. exit(EXIT_FAILURE);
  101. }
  102. if ( GLEW_ARB_debug_output ) {
  103. glEnable(GL_DEBUG_OUTPUT);
  104. glDebugMessageCallback(MessageCallback, 0);
  105. } else {
  106. fprintf(stderr,
  107. "WARNING! GLEW_ARB_debug_output is not availible\n");
  108. }
  109. glEnable(GL_BLEND);
  110. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  111. return app;
  112. }
  113. void
  114. app_destroy(struct app app)
  115. {
  116. SDL_DestroyWindow(app.win.ptr);
  117. SDL_GL_DeleteContext(app.glctx);
  118. free(app.buf.data.items);
  119. SDL_Quit();
  120. }
  121. #define BRANCHLESS_IF(cond, when_true, when_false) \
  122. (((cond)) * (when_true) + (!(cond)) * (when_false))
  123. struct vec2
  124. app_calc_cur_pos(struct app app)
  125. {
  126. size_t col = 0;
  127. size_t row = buffer_calc_cur_row(&app.buf);
  128. enum buffer_err err;
  129. /* RET_UNWRAP2(col, row, */
  130. /* struct ret_size_t_size_t, */
  131. /* buffer_calc_cur_pos(app.buf)); */
  132. size_t start_line;
  133. start_line = buffer_index_bw_char(&app.buf, '\n', app.buf.cur, &err);
  134. col = (app.buf.cur - start_line) - 1;
  135. if ( err == BUFFER_ERR_NOT_FOUND ) {
  136. col = app.buf.cur;
  137. }
  138. size_t tabs_n = 0;
  139. tabs_n = buffer_count_char_between(&app.buf, '\t', start_line,
  140. app.buf.cur, &err);
  141. col += ( tabs_n > 0 ) * ((tabs_n * app.cfg.tab_size) - tabs_n);
  142. double row_px = -(((double)row)
  143. * app.fga->atlas_dim.y);
  144. double col_px = (((double) col)
  145. * ((double) 1));
  146. return vec2(col_px, row_px);
  147. }
  148. void
  149. MessageCallback(GLenum source, GLenum type, GLuint id,
  150. GLenum severity, GLsizei length,
  151. const GLchar *msg, const void *up)
  152. {
  153. (void) source; (void) id; (void) length; (void) up;
  154. fprintf(stderr,
  155. "GL CALLBACK: %s type = 0x%x, severity = 0x%x, message = %s\n",
  156. (type == GL_DEBUG_TYPE_ERROR ? "*** GL ERROR ***" : ""),
  157. type, severity, msg);
  158. }
  159. struct vec2i
  160. app_calc_cursor(struct app app)
  161. {
  162. size_t row, col;
  163. RET_UNWRAP2(col, row,
  164. struct ret_size_t_size_t, buffer_calc_cur_pos(&app.buf));
  165. return vec2i((int32_t)col, (int32_t)row);
  166. }
  167. struct ret_app_vec2
  168. app_calc_buffer_cam(struct app app, double dt)
  169. {
  170. struct vec2 cur_pos = app_calc_cur_pos(app);
  171. struct vec2 cpos = app.buf.cam.pos;
  172. struct vec2 cvel = app.buf.cam.vel;
  173. cvel = vec2_sub(cur_pos, cpos);
  174. cpos = vec2_add(cpos, vec2_mul(cvel, vec2s(dt)));
  175. app.buf.cam.pos = cpos;
  176. app.buf.cam.vel = cvel;
  177. return (struct ret_app_vec2) {
  178. .f1 = app,
  179. .f2 = cpos
  180. };
  181. }
  182. #endif /* defined(APP_IMP) || defined(IMP) */
  183. #endif