apps/editor/src/eu/editor/main.cc
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "SDL.h" | ||
| 2 | |||
| 3 | #include <cassert> | ||
| 4 | #include <string> | ||
| 5 | |||
| 6 | #include "OpenSans-Regular.ttf.h" | ||
| 7 | |||
| 8 | #include "eu/log/log.h" | ||
| 9 | #include "eu/base/memorychunk.h" | ||
| 10 | |||
| 11 | #include "eu/render/canvas.h" | ||
| 12 | #include "eu/render/state.h" | ||
| 13 | #include "eu/render/font.h" | ||
| 14 | #include "eu/render/opengl_utils.h" | ||
| 15 | #include "eu/render/texture.io.h" | ||
| 16 | |||
| 17 | #include "eu/render/enable_high_performance_graphics.h" | ||
| 18 | |||
| 19 | #include "dear_imgui/imgui.h" | ||
| 20 | #include "dear_imgui/imgui_internal.h" | ||
| 21 | |||
| 22 | #include "dear_imgui/backends/imgui_impl_sdl2.h" | ||
| 23 | #include "dear_imgui/backends/imgui_impl_opengl3.h" | ||
| 24 | |||
| 25 | ENABLE_HIGH_PERFORMANCE_GRAPHICS | ||
| 26 | |||
| 27 | namespace imgui | ||
| 28 | { | ||
| 29 | ✗ | void label(const char* label) | |
| 30 | { | ||
| 31 | ✗ | ImGuiWindow* window = ImGui::GetCurrentWindow(); | |
| 32 | ✗ | float fullWidth = ImGui::GetContentRegionAvail().x; | |
| 33 | ✗ | float itemWidth = fullWidth * 0.6f; | |
| 34 | ✗ | ImVec2 textSize = ImGui::CalcTextSize(label); | |
| 35 | ✗ | ImRect textRect; | |
| 36 | ✗ | textRect.Min = ImGui::GetCursorScreenPos(); | |
| 37 | ✗ | textRect.Max = textRect.Min; | |
| 38 | ✗ | textRect.Max.x += fullWidth - itemWidth; | |
| 39 | ✗ | textRect.Max.y += textSize.y; | |
| 40 | |||
| 41 | ✗ | ImGui::AlignTextToFramePadding(); | |
| 42 | ✗ | textRect.Min.y += window->DC.CurrLineTextBaseOffset; | |
| 43 | ✗ | textRect.Max.y += window->DC.CurrLineTextBaseOffset; | |
| 44 | |||
| 45 | ✗ | ImGui::ItemSize(textRect); | |
| 46 | ✗ | if (ImGui::ItemAdd(textRect, window->GetID(label))) | |
| 47 | { | ||
| 48 | ✗ | const float ellipsis_max = 3.0f; | |
| 49 | ✗ | ImGui::RenderTextEllipsis(ImGui::GetWindowDrawList(), textRect.Min, textRect.Max, ellipsis_max, label, nullptr, &textSize); | |
| 50 | |||
| 51 | ✗ | if (textRect.GetWidth() < textSize.x && ImGui::IsItemHovered()) | |
| 52 | ✗ | ImGui::SetTooltip("%s", label); | |
| 53 | } | ||
| 54 | ✗ | ImGui::SameLine(); | |
| 55 | ✗ | ImGui::SetNextItemWidth(-1); | |
| 56 | ✗ | } | |
| 57 | |||
| 58 | ✗ | bool centered_button(const char* label) | |
| 59 | { | ||
| 60 | ✗ | const auto window_width = ImGui::GetContentRegionAvail().x; | |
| 61 | ✗ | const auto text_width = ImGui::CalcTextSize(label).x; | |
| 62 | ✗ | const auto padding_hor = ImGui::GetStyle().FramePadding.x * 2; // padding on both sides | |
| 63 | ✗ | const auto whitespace = window_width - text_width - padding_hor; | |
| 64 | ✗ | const float x = whitespace * 0.5f; | |
| 65 | ✗ | ImGui::SetCursorPosX(x); | |
| 66 | ✗ | return ImGui::Button(label); | |
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | ✗ | eu::MemoryChunk chunk_from_embed(const embedded_binary& binary) | |
| 71 | { | ||
| 72 | ✗ | return { .bytes = reinterpret_cast<const char*>(binary.data), .size = binary.size }; | |
| 73 | } | ||
| 74 | |||
| 75 | ✗ | int main(int, char**) | |
| 76 | { | ||
| 77 | ✗ | int window_width = 1280; | |
| 78 | ✗ | int window_height = 720; | |
| 79 | ✗ | const char* glsl_version = "#version 130"; | |
| 80 | |||
| 81 | ✗ | if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_EVENTS) < 0) { | |
| 82 | ✗ | LOG_ERR("Error initializing SDL: {}", SDL_GetError()); | |
| 83 | ✗ | return -1; | |
| 84 | } | ||
| 85 | |||
| 86 | #if defined(__APPLE__) | ||
| 87 | // GL 3.2 Core + GLSL 150 | ||
| 88 | const char* glsl_version = "#version 150"; | ||
| 89 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG); // Always required on Mac | ||
| 90 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); | ||
| 91 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); | ||
| 92 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); | ||
| 93 | #else | ||
| 94 | // GL 3.0 + GLSL 130 | ||
| 95 | // const char* glsl_version = "#version 130"; | ||
| 96 | ✗ | SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG); | |
| 97 | ✗ | SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); | |
| 98 | ✗ | SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4); | |
| 99 | ✗ | SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); // was 0 in dear imgui example?? | |
| 100 | #endif | ||
| 101 | |||
| 102 | ✗ | SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); | |
| 103 | ✗ | SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); | |
| 104 | |||
| 105 | ✗ | SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); | |
| 106 | ✗ | SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1); | |
| 107 | |||
| 108 | ✗ | SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); | |
| 109 | ✗ | SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); | |
| 110 | ✗ | SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); | |
| 111 | ✗ | SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); | |
| 112 | |||
| 113 | ✗ | SDL_Window* window = SDL_CreateWindow("Editor sample", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, | |
| 114 | window_width, window_height, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_RESIZABLE); | ||
| 115 | ✗ | if (!window) { | |
| 116 | ✗ | LOG_ERR("Error creating window: {}", SDL_GetError()); | |
| 117 | ✗ | return -1; | |
| 118 | } | ||
| 119 | |||
| 120 | ✗ | auto* glContext = SDL_GL_CreateContext(window); | |
| 121 | ✗ | SDL_GetWindowSize(window, &window_width, &window_height); | |
| 122 | ✗ | if (!glContext) { | |
| 123 | ✗ | LOG_ERR("Error creating gl context: {}", SDL_GetError()); | |
| 124 | ✗ | return -1; | |
| 125 | } | ||
| 126 | |||
| 127 | /* OpenGL setup */ | ||
| 128 | ✗ | const int glad_result = gladLoadGLLoader(SDL_GL_GetProcAddress); | |
| 129 | ✗ | if (glad_result == 0) | |
| 130 | { | ||
| 131 | ✗ | LOG_ERR("Failed to init glad, error: {0}", glad_result); | |
| 132 | ✗ | return -1; | |
| 133 | } | ||
| 134 | |||
| 135 | ✗ | IMGUI_CHECKVERSION(); | |
| 136 | ✗ | ImGui::CreateContext(); | |
| 137 | ✗ | ImGuiIO& io = ImGui::GetIO(); (void)io; | |
| 138 | ✗ | ImGui::StyleColorsDark(); | |
| 139 | ✗ | ImGui_ImplSDL2_InitForOpenGL(window, glContext); | |
| 140 | ✗ | ImGui_ImplOpenGL3_Init(glsl_version); | |
| 141 | |||
| 142 | ✗ | bool show_demo_window = true; | |
| 143 | |||
| 144 | { | ||
| 145 | ✗ | const std::string gl_vendor = reinterpret_cast<const char*>(glGetString(GL_VENDOR)); | |
| 146 | ✗ | const std::string gl_renderer = reinterpret_cast<const char*>(glGetString(GL_RENDERER)); | |
| 147 | ✗ | const std::string gl_version = reinterpret_cast<const char*>(glGetString(GL_VERSION)); | |
| 148 | ✗ | const std::string gl_shading_language_version = reinterpret_cast<const char*>(glGetString(GL_SHADING_LANGUAGE_VERSION)); | |
| 149 | |||
| 150 | ✗ | LOG_INFO("Vendor: {0}", gl_vendor); | |
| 151 | ✗ | LOG_INFO("Renderer: {0}", gl_renderer); | |
| 152 | ✗ | LOG_INFO("Version OpenGL: {0}", gl_version); | |
| 153 | ✗ | LOG_INFO("Version GLSL: {0}", gl_shading_language_version); | |
| 154 | ✗ | } | |
| 155 | |||
| 156 | ✗ | ImFontConfig config; | |
| 157 | ✗ | config.FontDataOwnedByAtlas = false; | |
| 158 | ✗ | ImFont* font = io.Fonts->AddFontFromMemoryTTF( | |
| 159 | const_cast<void*>(static_cast<const void*>(OPENSANS_REGULAR_TTF_data)), | ||
| 160 | OPENSANS_REGULAR_TTF_size, | ||
| 161 | 18.0f, | ||
| 162 | &config | ||
| 163 | ); | ||
| 164 | ✗ | IM_ASSERT(font != nullptr); | |
| 165 | |||
| 166 | ✗ | eu::render::State states; | |
| 167 | ✗ | eu::render::Render2 render{ &states }; | |
| 168 | |||
| 169 | ✗ | bool running = true; | |
| 170 | |||
| 171 | ✗ | LOG_INFO("Editor started"); | |
| 172 | ✗ | while (running) | |
| 173 | { | ||
| 174 | SDL_Event e; | ||
| 175 | ✗ | while (SDL_PollEvent(&e) != 0) | |
| 176 | { | ||
| 177 | ✗ | ImGui_ImplSDL2_ProcessEvent(&e); | |
| 178 | ✗ | switch (e.type) | |
| 179 | { | ||
| 180 | ✗ | case SDL_WINDOWEVENT: | |
| 181 | ✗ | if (e.window.event == SDL_WINDOWEVENT_RESIZED || e.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) | |
| 182 | { | ||
| 183 | ✗ | LOG_INFO("Resized"); | |
| 184 | ✗ | SDL_GetWindowSize(window, &window_width, &window_height); | |
| 185 | } | ||
| 186 | ✗ | break; | |
| 187 | ✗ | case SDL_QUIT: running = false; break; | |
| 188 | ✗ | default: | |
| 189 | // ignore other events | ||
| 190 | ✗ | break; | |
| 191 | } | ||
| 192 | } | ||
| 193 | |||
| 194 | ✗ | ImGui_ImplOpenGL3_NewFrame(); | |
| 195 | ✗ | ImGui_ImplSDL2_NewFrame(); | |
| 196 | ✗ | ImGui::NewFrame(); | |
| 197 | |||
| 198 | ✗ | if (show_demo_window) | |
| 199 | { | ||
| 200 | ✗ | ImGui::ShowDemoWindow(&show_demo_window); | |
| 201 | } | ||
| 202 | |||
| 203 | ✗ | if (ImGui::Begin("Properties")) | |
| 204 | { | ||
| 205 | static eu::v3 pos = {0,0,0}; | ||
| 206 | static eu::v3 rot = {0, 0, 0}; | ||
| 207 | static eu::v3 scale = {1, 1, 1}; | ||
| 208 | |||
| 209 | ✗ | imgui::label("Position"); | |
| 210 | ✗ | ImGui::DragFloat3("##Position", pos.get_data_ptr()); | |
| 211 | |||
| 212 | ✗ | imgui::label("Rotation"); | |
| 213 | ✗ | ImGui::DragFloat3("##Rotation", rot.get_data_ptr()); | |
| 214 | |||
| 215 | ✗ | imgui::label("Scale"); | |
| 216 | ✗ | ImGui::DragFloat3("##Scale", scale.get_data_ptr()); | |
| 217 | |||
| 218 | ✗ | imgui::centered_button("Add component"); | |
| 219 | } | ||
| 220 | ✗ | ImGui::End(); | |
| 221 | |||
| 222 | { | ||
| 223 | ✗ | eu::render::RenderCommand cmd {.states = &states, .render = &render, .size = {.width = window_width, .height = window_height} }; | |
| 224 | |||
| 225 | // todo(Gustav): provide a pixel layout | ||
| 226 | ✗ | const auto screen = eu::render::LayoutData{ .style = eu::render::ViewportStyle::extended, | |
| 227 | ✗ | .requested_width = static_cast<float>(window_width), .requested_height = static_cast<float>(window_height) }; | |
| 228 | |||
| 229 | ✗ | cmd.clear(eu::colors::blue_sky, screen); | |
| 230 | |||
| 231 | ✗ | auto layer = eu::render::with_layer2(cmd, screen); | |
| 232 | ✗ | eu::render::Quad{ .tint = eu::colors::green_bluish }.draw(layer.batch, layer.viewport_aabb_in_worldspace.get_bottom(50)); | |
| 233 | ✗ | } | |
| 234 | |||
| 235 | ✗ | ImGui::Render(); | |
| 236 | ✗ | ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); | |
| 237 | ✗ | SDL_GL_SwapWindow(window); | |
| 238 | } | ||
| 239 | |||
| 240 | ✗ | ImGui_ImplOpenGL3_Shutdown(); | |
| 241 | ✗ | ImGui_ImplSDL2_Shutdown(); | |
| 242 | ✗ | ImGui::DestroyContext(); | |
| 243 | |||
| 244 | ✗ | LOG_INFO("Shutting down"); | |
| 245 | ✗ | SDL_GL_DeleteContext(glContext); | |
| 246 | ✗ | SDL_DestroyWindow(window); | |
| 247 | ✗ | SDL_Quit(); | |
| 248 | |||
| 249 | ✗ | return 0; | |
| 250 | ✗ | } | |
| 251 |