main.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <stdbool.h>
  5. #include <SDL2/SDL.h>
  6. #pragma GCC diagnostic push
  7. #pragma GCC diagnostic ignored "-Wall"
  8. #pragma GCC diagnostic ignored "-Wsign-conversion"
  9. #pragma GCC diagnostic ignored "-Wdouble-promotion"
  10. #pragma GCC diagnostic ignored "-Wconversion"
  11. #define STB_IMAGE_IMPLEMENTATION
  12. #include "stb_image.h"
  13. #pragma GCC diagnostic pop
  14. #define IMP
  15. #include "app.h"
  16. #include "vec2.h"
  17. #include "da.h"
  18. #include "unwrap.h"
  19. #include "file.h"
  20. #include "keybind.h"
  21. #include "config.h"
  22. struct app handle_events(struct app app);
  23. int32_t
  24. main(int32_t argc, char **argv)
  25. {
  26. (void) argc; (void) argv;
  27. SCE(SDL_Init(SDL_INIT_VIDEO));
  28. const char *font_path = "./charmap-oldschool_white.png";
  29. struct app app = app_create("ged");
  30. app.font = font_create(app.rdr, font_path, 7, 18);
  31. app.font.scale = 2.0;
  32. const char *filepath = "./src/main.c";
  33. enum buffer_err err;
  34. RET_UNWRAP2(app.buf, err,
  35. struct ret_buffer_err,
  36. buffer_load_from_file(app.buf, filepath));
  37. (void) err;
  38. uint64_t i = 0;
  39. while ( app.running == true ) {
  40. struct buffer *c_buf = &app.buf;
  41. app = handle_events(app);
  42. SCE(SDL_SetRenderDrawColor(app.rdr, i & 0xFF, 0, 0, 1));
  43. SCE(SDL_RenderClear(app.rdr));
  44. app_render_text(app, c_buf->data.items, c_buf->data.size,
  45. vec2(0, 0), 0xFFFFFFFF, app.font.scale);
  46. /* app_render_cursor(app, 0x00FF000F); */
  47. SDL_RenderPresent(app.rdr);
  48. ++i;
  49. }
  50. SDL_DestroyWindow(app.win.ptr);
  51. DA_DESTROY(app.buf.data);
  52. SDL_Quit();
  53. exit(EXIT_SUCCESS);
  54. }
  55. struct app
  56. handle_events(struct app app)
  57. {
  58. SDL_Event e = {0};
  59. bool done = false;
  60. while ( SDL_PollEvent(&e) ) {
  61. switch ( e.type ) {
  62. case SDL_QUIT: {
  63. app.running = false;
  64. } break;
  65. case SDL_KEYDOWN: {
  66. SDL_KeyboardEvent key = e.key;
  67. SDL_Keysym ks = key.keysym;
  68. struct keybinds kb = keybinds[ks.sym % KEYBINDS_MAX_SIZE];
  69. for ( size_t i = 0; i < kb.size; ++i ) {
  70. struct bind b = kb.binds[i];
  71. if ( b.func == NULL ) {
  72. continue;
  73. }
  74. if ( (! (b.mod == KMOD_NONE
  75. && b.mod == ks.mod))
  76. && (! (b.mod & ks.mod)) ) {
  77. continue;
  78. }
  79. app = b.func(app, b.args);
  80. done = true;
  81. }
  82. } break;
  83. case SDL_TEXTINPUT: {
  84. if ( done == true ) {
  85. break;
  86. }
  87. char *t = e.text.text;
  88. while( *t != '\0' ) {
  89. app.buf = buffer_insert_char(
  90. app.buf, app.buf.cur, *t++);
  91. }
  92. } break;
  93. case SDL_WINDOWEVENT: {
  94. WINDOW_UP_SIZE(app.win);
  95. } break;
  96. }
  97. }
  98. return app;
  99. }