app.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. {
  60. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
  61. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
  62. SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
  63. SDL_GL_CONTEXT_PROFILE_CORE);
  64. int32_t major = 0;
  65. int32_t minor = 0;
  66. SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &major);
  67. SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &minor);
  68. printf("GL Version: %d.%d\n", major, minor);
  69. }
  70. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, GL_DOUBLEBUFFER);
  71. SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
  72. SDL_GL_SetSwapInterval(0);
  73. struct app app = {
  74. .win = window_create(win_title),
  75. .rdr = NULL,
  76. .running = true,
  77. .buf = buffer_create(),
  78. .cfg = {
  79. .tab_size = 8
  80. },
  81. .target_fps = 120,
  82. };
  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. glEnable(GL_BLEND);
  90. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  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. /* app.rdr = SDL_CreateRenderer(app.win.ptr, -1, SDL_RENDERER_ACCELERATED); */
  110. /* if ( app.rdr == NULL ) { */
  111. /* printf("Using software acceleration: %s\n", SDL_GetError()); */
  112. /* app.rdr = SDL_CreateRenderer(app.win.ptr, -1, */
  113. /* SDL_RENDERER_SOFTWARE); */
  114. /* } */
  115. /* SCP(app.rdr); */
  116. return app;
  117. }
  118. void
  119. app_destroy(struct app app)
  120. {
  121. SDL_DestroyWindow(app.win.ptr);
  122. SDL_GL_DeleteContext(app.glctx);
  123. DA_DESTROY(app.buf.data);
  124. SDL_Quit();
  125. }
  126. void
  127. app_render_char(struct app app, const char c, struct vec2 pos, double scale)
  128. {
  129. SDL_Rect dst = {
  130. .x = (int32_t) pos.x,
  131. .y = (int32_t) pos.y,
  132. .w = (int32_t)(app.font.ch_w * scale),
  133. .h = (int32_t)(app.font.ch_h * scale)
  134. };
  135. SCE(SDL_RenderCopy(app.rdr, app.font.tex,
  136. app.font.glyphs + (c - 32),
  137. &dst));
  138. }
  139. void
  140. app_set_text_color(struct app app, uint32_t color) {
  141. uint8_t r = (uint8_t) ((color >> 24) & 0xFF);
  142. uint8_t g = (uint8_t) ((color >> 16) & 0xFF);
  143. uint8_t b = (uint8_t) ((color >> 8 ) & 0xFF);
  144. uint8_t a = (uint8_t) ((color >> 0 ) & 0xFF);
  145. SCE(SDL_SetTextureColorMod(app.font.tex, r, g, b));
  146. SCE(SDL_SetTextureAlphaMod(app.font.tex, a));
  147. }
  148. uint32_t
  149. app_get_text_color(struct app app) {
  150. uint8_t r = 0;
  151. uint8_t g = 0;
  152. uint8_t b = 0;
  153. uint8_t a = 0;
  154. SCE(SDL_GetTextureColorMod(app.font.tex, &r, &g, &b));
  155. SCE(SDL_GetTextureAlphaMod(app.font.tex, &a));
  156. return (r << 24) | (g << 16) | (b << 8) | (a << 0);
  157. }
  158. #define BRANCHLESS_IF(cond, when_true, when_false) \
  159. (((cond)) * (when_true) + (!(cond)) * (when_false))
  160. void
  161. app_render_buffer(struct app app, struct vec2 pos,
  162. uint32_t color, double scale)
  163. {
  164. app_set_text_color(app, color);
  165. struct buffer buf = app.buf;
  166. DA_DEF_STRUCT_ITEM(char, da);
  167. DA_ASSIGN(da, buf.data);
  168. struct vec2 pen = pos;
  169. size_t i = buf.render_start;
  170. char c;
  171. for ( ; i < da.size; ++i ) {
  172. c = da.items[i];
  173. /* bool cond = (pen.x > app.win.w); */
  174. /* pen.x = BRANCHLESS_IF(cond, pos.x, pen.x); */
  175. /* pen.y += BRANCHLESS_IF(cond, */
  176. /* (double) (app.font.ch_h * scale), 0); */
  177. switch ( c ) {
  178. case '\n': {
  179. if ( i == app.buf.cur ) {
  180. app_render_cursor_in_pos(app, pen, 0xFF00FFFF);
  181. }
  182. pen.x = pos.x;
  183. pen.y += (double) (app.font.ch_h * scale);
  184. continue;
  185. } break;
  186. case '\t': {
  187. if ( i == app.buf.cur ) {
  188. app_render_cursor_in_pos(app, pen, 0xFF00FFFF);
  189. }
  190. pen.x += (double) ((app.font.ch_w * scale)
  191. * app.cfg.tab_size);
  192. continue;
  193. } break;
  194. }
  195. app_render_char(app, c, pen, scale);
  196. if ( i == app.buf.cur ) {
  197. app_render_cursor_in_pos(app, pen, 0xFF00FFFF);
  198. }
  199. pen.x += (double) (app.font.ch_w * scale);
  200. }
  201. if ( i == app.buf.cur ) {
  202. app_render_cursor_in_pos(app, pen, 0xFF00FFFF);
  203. }
  204. }
  205. void
  206. app_render_text(struct app app, const char *text, size_t text_size,
  207. struct vec2 pos, uint32_t color, double scale)
  208. {
  209. app_set_text_color(app, color);
  210. struct vec2 pen = pos;
  211. size_t i = 0;
  212. char c;
  213. for ( ; i < text_size; ++i ) {
  214. c = text[i];
  215. switch ( c ) {
  216. case '\n': {
  217. pen.x = pos.x;
  218. pen.y += (double) (app.font.ch_h * scale);
  219. continue;
  220. } break;
  221. case '\t': {
  222. pen.x += (double) ((app.font.ch_w * scale)
  223. * app.cfg.tab_size);
  224. continue;
  225. } break;
  226. }
  227. app_render_char(app, c, pen, scale);
  228. pen.x += (double) (app.font.ch_w * scale);
  229. }
  230. }
  231. void
  232. app_render_cursor_in_pos(struct app app, struct vec2 pos, uint32_t color)
  233. {
  234. SDL_Rect rect = {
  235. .x = (int32_t) pos.x,
  236. .y = (int32_t) pos.y,
  237. .w = (int32_t) (app.font.ch_w * app.font.scale),
  238. .h = (int32_t) (app.font.ch_h * app.font.scale)
  239. };
  240. SCE(SDL_SetRenderDrawColor(app.rdr, UNHEX_COLOR(color)));
  241. SCE(SDL_RenderFillRect(app.rdr, &rect));
  242. if ( app.buf.cur < app.buf.data.size ) {
  243. char c = app.buf.data.items[app.buf.cur];
  244. if ( c == '\n' ) return ;
  245. uint32_t old_color = app_get_text_color(app);
  246. app_set_text_color(app, ((~color) | 0xFF));
  247. app_render_char(app, c, pos, app.font.scale);
  248. app_set_text_color(app, old_color);
  249. }
  250. }
  251. void
  252. app_render_cursor(struct app app, uint32_t color)
  253. {
  254. struct vec2 pos = {
  255. .x = ((double)(app.buf.cur) * (app.font.ch_w * app.font.scale)),
  256. .y = ((double)(app.buf.cur) * (app.font.ch_h * app.font.scale))
  257. };
  258. app_render_cursor_in_pos(app, pos, color);
  259. }
  260. struct vec2
  261. app_calc_cur_pos(struct app app)
  262. {
  263. size_t col = 0;
  264. size_t row = buffer_calc_cur_row(app.buf);
  265. enum buffer_err err;
  266. /* RET_UNWRAP2(col, row, */
  267. /* struct ret_size_t_size_t, */
  268. /* buffer_calc_cur_pos(app.buf)); */
  269. size_t start_line;
  270. RET_UNWRAP2(start_line, err, struct ret_size_t_err,
  271. buffer_index_bw_char(app.buf, app.buf.cur, '\n'));
  272. col = (app.buf.cur - start_line) - 1;
  273. if ( err == BUFFER_ERR_NOT_FOUND ) {
  274. col = app.buf.cur;
  275. }
  276. size_t tabs_n = 0;
  277. RET_UNWRAP2(tabs_n, err,
  278. struct ret_size_t_err,
  279. buffer_count_char_between(app.buf, '\t',
  280. start_line, app.buf.cur));
  281. col += ( tabs_n > 0 ) * ((tabs_n * app.cfg.tab_size) - tabs_n);
  282. double row_px = (((double)row)
  283. * (app.font.ch_h * app.font.scale));
  284. double col_px = (((double) col) *
  285. (app.font.ch_w * app.font.scale));
  286. return vec2(col_px, row_px);
  287. }
  288. void
  289. MessageCallback(GLenum source, GLenum type, GLuint id,
  290. GLenum severity, GLsizei length,
  291. const GLchar *msg, const void *up)
  292. {
  293. (void) source; (void) id; (void) length; (void) up;
  294. fprintf(stderr,
  295. "GL CALLBACK: %s type = 0x%x, severity = 0x%x, message = %s\n",
  296. (type == GL_DEBUG_TYPE_ERROR ? "*** GL ERROR ***" : ""),
  297. type, severity, msg);
  298. }
  299. struct vec2i
  300. app_calc_cursor(struct app app)
  301. {
  302. size_t row, col;
  303. RET_UNWRAP2(col, row,
  304. struct ret_size_t_size_t, buffer_calc_cur_pos(app.buf));
  305. return vec2i((int32_t)col, (int32_t)row);
  306. }
  307. #endif /* defined(APP_IMP) || defined(IMP) */
  308. #endif