main.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*================================================================
  2. * Copyright: 2020 John Jackson
  3. * imgui
  4. * Simple c++ imgui/glfw example
  5. Press `esc` to exit the application.
  6. =================================================================*/
  7. #include <gs.h>
  8. #include <imgui/imgui_impl_glfw.h>
  9. #include <imgui/imgui_impl_opengl3.h>
  10. #include <GLFW/glfw3.h>
  11. // Forward Decls.
  12. gs_result app_init();
  13. gs_result app_update(); // Use to update your application
  14. void imgui_init();
  15. void imgui_new_frame();
  16. void imgui_render();
  17. // Globals
  18. b8 g_show_demo_window = true;
  19. int main(int argc, char** argv)
  20. {
  21. // This is our app description. It gives internal hints to our engine for various things like
  22. // window size, title, as well as update, init, and shutdown functions to be run.
  23. gs_application_desc_t app = {0};
  24. app.window_title = "ImGui";
  25. app.window_width = 800;
  26. app.window_height = 600;
  27. app.init = &app_init;
  28. app.update = &app_update;
  29. // Construct internal instance of our engine
  30. gs_engine_t* engine = gs_engine_construct(app);
  31. // Run the internal engine loop until completion
  32. gs_result res = engine->run();
  33. // Check result of engine after exiting loop
  34. if (res != gs_result_success)
  35. {
  36. gs_println("Error: Engine did not successfully finish running.");
  37. return -1;
  38. }
  39. gs_println("Gunslinger exited successfully.");
  40. return 0;
  41. }
  42. gs_result app_init()
  43. {
  44. imgui_init();
  45. return gs_result_success;
  46. }
  47. // Update your application here
  48. gs_result app_update()
  49. {
  50. // Grab global instance of engine
  51. gs_engine_t* engine = gs_engine_instance();
  52. // If we press the escape key, exit the application
  53. if (engine->ctx.platform->key_pressed(gs_keycode_esc))
  54. {
  55. return gs_result_success;
  56. }
  57. imgui_new_frame();
  58. // 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!).
  59. ImGui::ShowDemoWindow(&g_show_demo_window);
  60. // Draw all imgui data
  61. imgui_render();
  62. // Otherwise, continue
  63. return gs_result_in_progress;
  64. }
  65. void imgui_init()
  66. {
  67. gs_platform_i* platform = gs_engine_instance()->ctx.platform;
  68. // Get main window from platform
  69. void* win = platform->raw_window_handle(platform->main_window());
  70. // Setup Dear ImGui context
  71. IMGUI_CHECKVERSION();
  72. ImGui::CreateContext();
  73. ImGuiIO& io = ImGui::GetIO();
  74. (void)io;
  75. // Setup Dear ImGui style
  76. ImGui::StyleColorsDark();
  77. // Setup Platform/Renderer bindings
  78. ImGui_ImplGlfw_InitForOpenGL(win, true);
  79. ImGui_ImplOpenGL3_Init();
  80. }
  81. void imgui_new_frame()
  82. {
  83. // Start the Dear ImGui frame
  84. ImGui_ImplOpenGL3_NewFrame();
  85. ImGui_ImplGlfw_NewFrame();
  86. ImGui::NewFrame();
  87. }
  88. void imgui_render()
  89. {
  90. gs_platform_i* platform = gs_engine_instance()->ctx.platform;
  91. // TODO(john): abstract this all away to be rendered via command buffer system
  92. ImGui::Render();
  93. gs_vec2 fbs = platform->frame_buffer_size(platform->main_window());
  94. glViewport(0, 0, fbs.x, fbs.y);
  95. glClearColor(0.1f, 0.1f, 0.1f, 1.f);
  96. glClear(GL_COLOR_BUFFER_BIT);
  97. ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
  98. }