7 KB、152行 生 Blame 履歴
1// Dear ImGui: standalone example application for Allegro 5
2
3// Learn about Dear ImGui:
4// - FAQ https://dearimgui.com/faq
5// - Getting Started https://dearimgui.com/getting-started
6// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
7// - Introduction, links and more at the top of imgui.cpp
8
9// On Windows, you can install Allegro5 using vcpkg:
10// git clone https://github.com/Microsoft/vcpkg
11// cd vcpkg
12// bootstrap - vcpkg.bat
13// vcpkg install allegro5 --triplet=x86-windows ; for win32
14// vcpkg install allegro5 --triplet=x64-windows ; for win64
15// vcpkg integrate install ; register include and libs in Visual Studio
16
17#include <stdint.h>
18#include <allegro5/allegro.h>
19#include <allegro5/allegro_primitives.h>
20#include "imgui.h"
21#include "imgui_impl_allegro5.h"
22
23int main(int, char**)
24{
25 // Setup Allegro
26 al_init();
27 al_install_keyboard();
28 al_install_mouse();
29 al_init_primitives_addon();
30 al_set_new_display_flags(ALLEGRO_RESIZABLE);
31 ALLEGRO_DISPLAY* display = al_create_display(1280, 800);
32 al_set_window_title(display, "Dear ImGui Allegro 5 example");
33 ALLEGRO_EVENT_QUEUE* queue = al_create_event_queue();
34 al_register_event_source(queue, al_get_display_event_source(display));
35 al_register_event_source(queue, al_get_keyboard_event_source());
36 al_register_event_source(queue, al_get_mouse_event_source());
37
38 // Setup Dear ImGui context
39 IMGUI_CHECKVERSION();
40 ImGui::CreateContext();
41 ImGuiIO& io = ImGui::GetIO(); (void)io;
42 io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
43 io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
44
45 // Setup Dear ImGui style
46 ImGui::StyleColorsDark();
47 //ImGui::StyleColorsLight();
48
49 // Setup Platform/Renderer backends
50 ImGui_ImplAllegro5_Init(display);
51
52 // Load Fonts
53 // - If fonts are not explicitly loaded, Dear ImGui will select an embedded font: either AddFontDefaultVector() or AddFontDefaultBitmap().
54 // This selection is based on (style.FontSizeBase * style.FontScaleMain * style.FontScaleDpi) reaching a small threshold.
55 // - You can load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
56 // - If a file cannot be loaded, AddFont functions will return a nullptr. Please handle those errors in your code (e.g. use an assertion, display an error and quit).
57 // - Read 'docs/FONTS.md' for more instructions and details.
58 // - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use FreeType for higher quality font rendering.
59 // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
60 //style.FontSizeBase = 20.0f;
61 //io.Fonts->AddFontDefaultVector();
62 //io.Fonts->AddFontDefaultBitmap();
63 //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf");
64 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf");
65 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf");
66 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf");
67 //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf");
68 //IM_ASSERT(font != nullptr);
69
70 bool show_demo_window = true;
71 bool show_another_window = false;
72 ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
73
74 // Main loop
75 bool running = true;
76 while (running)
77 {
78 // Poll and handle events (inputs, window resize, etc.)
79 // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
80 // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
81 // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
82 // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
83 ALLEGRO_EVENT ev;
84 while (al_get_next_event(queue, &ev))
85 {
86 ImGui_ImplAllegro5_ProcessEvent(&ev);
87 if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
88 running = false;
89 if (ev.type == ALLEGRO_EVENT_DISPLAY_RESIZE)
90 {
91 ImGui_ImplAllegro5_InvalidateDeviceObjects();
92 al_acknowledge_resize(display);
93 ImGui_ImplAllegro5_CreateDeviceObjects();
94 }
95 }
96
97 // Start the Dear ImGui frame
98 ImGui_ImplAllegro5_NewFrame();
99 ImGui::NewFrame();
100
101 // 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!).
102 if (show_demo_window)
103 ImGui::ShowDemoWindow(&show_demo_window);
104
105 // 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window.
106 {
107 static float f = 0.0f;
108 static int counter = 0;
109
110 ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
111
112 ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
113 ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
114 ImGui::Checkbox("Another Window", &show_another_window);
115
116 ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
117 ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
118
119 if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
120 counter++;
121 ImGui::SameLine();
122 ImGui::Text("counter = %d", counter);
123
124 ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
125 ImGui::End();
126 }
127
128 // 3. Show another simple window.
129 if (show_another_window)
130 {
131 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)
132 ImGui::Text("Hello from another window!");
133 if (ImGui::Button("Close Me"))
134 show_another_window = false;
135 ImGui::End();
136 }
137
138 // Rendering
139 ImGui::Render();
140 al_clear_to_color(al_map_rgba_f(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w));
141 ImGui_ImplAllegro5_RenderDrawData(ImGui::GetDrawData());
142 al_flip_display();
143 }
144
145 // Cleanup
146 ImGui_ImplAllegro5_Shutdown();
147 ImGui::DestroyContext();
148 al_destroy_event_queue(queue);
149 al_destroy_display(display);
150
151 return 0;
152}