main.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Dear ImGui: standalone example application for Allegro 5
  2. // If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp.
  3. // Read online: https://github.com/ocornut/imgui/tree/master/docs
  4. #include <stdint.h>
  5. #include <allegro5/allegro.h>
  6. #include <allegro5/allegro_primitives.h>
  7. #include "imgui.h"
  8. #include "imgui_impl_allegro5.h"
  9. int main(int, char**)
  10. {
  11. // Setup Allegro
  12. al_init();
  13. al_install_keyboard();
  14. al_install_mouse();
  15. al_init_primitives_addon();
  16. al_set_new_display_flags(ALLEGRO_RESIZABLE);
  17. ALLEGRO_DISPLAY* display = al_create_display(1280, 720);
  18. al_set_window_title(display, "Dear ImGui Allegro 5 example");
  19. ALLEGRO_EVENT_QUEUE* queue = al_create_event_queue();
  20. al_register_event_source(queue, al_get_display_event_source(display));
  21. al_register_event_source(queue, al_get_keyboard_event_source());
  22. al_register_event_source(queue, al_get_mouse_event_source());
  23. // Setup Dear ImGui context
  24. IMGUI_CHECKVERSION();
  25. ImGui::CreateContext();
  26. ImGuiIO& io = ImGui::GetIO(); (void)io;
  27. //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
  28. io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
  29. // Setup Dear ImGui style
  30. ImGui::StyleColorsDark();
  31. //ImGui::StyleColorsClassic();
  32. // Setup Platform/Renderer backends
  33. ImGui_ImplAllegro5_Init(display);
  34. // Load Fonts
  35. // - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
  36. // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
  37. // - If the file cannot be loaded, the function will return NULL. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
  38. // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
  39. // - Read 'docs/FONTS.md' for more instructions and details.
  40. // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
  41. //io.Fonts->AddFontDefault();
  42. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
  43. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
  44. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
  45. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f);
  46. //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
  47. //IM_ASSERT(font != NULL);
  48. bool show_demo_window = true;
  49. bool show_another_window = false;
  50. ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
  51. // Main loop
  52. bool running = true;
  53. while (running)
  54. {
  55. // Poll and handle events (inputs, window resize, etc.)
  56. // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
  57. // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
  58. // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
  59. // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
  60. ALLEGRO_EVENT ev;
  61. while (al_get_next_event(queue, &ev))
  62. {
  63. ImGui_ImplAllegro5_ProcessEvent(&ev);
  64. if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
  65. running = false;
  66. if (ev.type == ALLEGRO_EVENT_DISPLAY_RESIZE)
  67. {
  68. ImGui_ImplAllegro5_InvalidateDeviceObjects();
  69. al_acknowledge_resize(display);
  70. ImGui_ImplAllegro5_CreateDeviceObjects();
  71. }
  72. }
  73. // Start the Dear ImGui frame
  74. ImGui_ImplAllegro5_NewFrame();
  75. ImGui::NewFrame();
  76. // 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!).
  77. if (show_demo_window)
  78. ImGui::ShowDemoWindow(&show_demo_window);
  79. // 2. Show a simple window that we create ourselves. We use a Begin/End pair to created a named window.
  80. {
  81. static float f = 0.0f;
  82. static int counter = 0;
  83. ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
  84. ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
  85. ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
  86. ImGui::Checkbox("Another Window", &show_another_window);
  87. ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
  88. ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
  89. if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
  90. counter++;
  91. ImGui::SameLine();
  92. ImGui::Text("counter = %d", counter);
  93. ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
  94. ImGui::End();
  95. }
  96. // 3. Show another simple window.
  97. if (show_another_window)
  98. {
  99. ImGui::Begin("Another Window", &show_another_window); // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked)
  100. ImGui::Text("Hello from another window!");
  101. if (ImGui::Button("Close Me"))
  102. show_another_window = false;
  103. ImGui::End();
  104. }
  105. // Rendering
  106. ImGui::Render();
  107. al_clear_to_color(al_map_rgba_f(clear_color.x, clear_color.y, clear_color.z, clear_color.w));
  108. ImGui_ImplAllegro5_RenderDrawData(ImGui::GetDrawData());
  109. al_flip_display();
  110. }
  111. // Cleanup
  112. ImGui_ImplAllegro5_Shutdown();
  113. ImGui::DestroyContext();
  114. al_destroy_event_queue(queue);
  115. al_destroy_display(display);
  116. return 0;
  117. }