1 #include "RendererApp.h"
3 #include <MpqArchive.h>
6 #include "DrawSprite.h"
7 #include "FileBrowser.h"
8 #include "imgui/imgui_bgfx.h"
10 RendererApp::RendererApp() =
default;
11 RendererApp::~RendererApp() =
default;
13 bool RendererApp::initAppThread()
15 if (!BaseApp::initAppThread())
return false;
17 imguiCreate(imguiAllocator);
19 ImGuiIO& io = ImGui::GetIO();
20 io.KeyMap[ImGuiKey_Tab] = SDL_SCANCODE_TAB;
21 io.KeyMap[ImGuiKey_LeftArrow] = SDL_SCANCODE_LEFT;
22 io.KeyMap[ImGuiKey_RightArrow] = SDL_SCANCODE_RIGHT;
23 io.KeyMap[ImGuiKey_UpArrow] = SDL_SCANCODE_UP;
24 io.KeyMap[ImGuiKey_DownArrow] = SDL_SCANCODE_DOWN;
25 io.KeyMap[ImGuiKey_PageUp] = SDL_SCANCODE_PAGEUP;
26 io.KeyMap[ImGuiKey_PageDown] = SDL_SCANCODE_PAGEDOWN;
27 io.KeyMap[ImGuiKey_Home] = SDL_SCANCODE_HOME;
28 io.KeyMap[ImGuiKey_End] = SDL_SCANCODE_END;
29 io.KeyMap[ImGuiKey_Insert] = SDL_SCANCODE_INSERT;
30 io.KeyMap[ImGuiKey_Delete] = SDL_SCANCODE_DELETE;
31 io.KeyMap[ImGuiKey_Backspace] = SDL_SCANCODE_BACKSPACE;
32 io.KeyMap[ImGuiKey_Space] = SDL_SCANCODE_SPACE;
33 io.KeyMap[ImGuiKey_Enter] = SDL_SCANCODE_RETURN;
34 io.KeyMap[ImGuiKey_Escape] = SDL_SCANCODE_ESCAPE;
35 io.KeyMap[ImGuiKey_A] = SDL_SCANCODE_A;
36 io.KeyMap[ImGuiKey_C] = SDL_SCANCODE_C;
37 io.KeyMap[ImGuiKey_V] = SDL_SCANCODE_V;
38 io.KeyMap[ImGuiKey_X] = SDL_SCANCODE_X;
39 io.KeyMap[ImGuiKey_Y] = SDL_SCANCODE_Y;
40 io.KeyMap[ImGuiKey_Z] = SDL_SCANCODE_Z;
42 io.ConfigFlags = 0 | ImGuiConfigFlags_NavEnableKeyboard | ImGuiConfigFlags_NavEnableGamepad;
44 io.SetClipboardTextFn = [](
void*,
const char* text) { SDL_SetClipboardText(text); };
45 io.GetClipboardTextFn = [](
void*) ->
const char* {
return SDL_GetClipboardText(); };
47 io.IniFilename =
"imgui.ini";
48 io.LogFilename =
nullptr;
51 if (!pal.decode(
"palettes/pal.dat"))
return false;
52 spriteRenderer.init(pal);
54 fileBrowser = std::make_unique<FileBrowser>();
58 void RendererApp::shutdownAppThread()
60 fileBrowser.reset(
nullptr);
61 spriteRenderer.shutdown();
63 BaseApp::shutdownAppThread();
66 void RendererApp::executeAppLoopOnce()
69 updateMouseAccordingToImgui();
71 const Inputs inputs = inputsManager.receiveAndProcessEvents();
75 ImGuiIO& imguiIO = ImGui::GetIO();
76 for (
const SDL_Event& event : inputs.events)
80 case SDL_QUIT: requireExit();
break;
82 if (event.key.keysym.sym == SDLK_F1) {
83 showBgfxStats = !showBgfxStats;
88 int key =
event.key.keysym.scancode;
89 IM_ASSERT(key >= 0 && key < IM_ARRAYSIZE(imguiIO.KeysDown));
90 imguiIO.KeysDown[key] = (
event.type == SDL_KEYDOWN);
91 imguiIO.KeyShift = ((SDL_GetModState() & KMOD_SHIFT) != 0);
92 imguiIO.KeyCtrl = ((SDL_GetModState() & KMOD_CTRL) != 0);
93 imguiIO.KeyAlt = ((SDL_GetModState() & KMOD_ALT) != 0);
94 imguiIO.KeySuper = ((SDL_GetModState() & KMOD_GUI) != 0);
97 case SDL_MOUSEWHEEL: {
float mouseWheelY = float(event.wheel.y);
98 #if SDL_VERSION_ATLEAST(2, 0, 4)
99 if (event.wheel.direction == SDL_MOUSEWHEEL_FLIPPED) mouseWheelY *= -1.f;
102 imguiIO.MouseWheel += mouseWheelY;
105 case SDL_TEXTINPUT: imguiIO.AddInputCharactersUTF8(event.text.text);
break;
114 int32_t(mouseState.x), int32_t(mouseState.y),
115 0 | ((mouseState.buttonsMask & SDL_BUTTON(SDL_BUTTON_LEFT)) ? IMGUI_MBUT_LEFT : 0)
116 | ((mouseState.buttonsMask & SDL_BUTTON(SDL_BUTTON_RIGHT)) ? IMGUI_MBUT_RIGHT : 0)
117 | ((mouseState.buttonsMask & SDL_BUTTON(SDL_BUTTON_MIDDLE)) ? IMGUI_MBUT_MIDDLE : 0),
118 uint16_t(windowWidth), uint16_t(windowHeight));
120 ImGui::ShowDemoWindow();
123 ImGui::Begin(
"Debug");
124 ImGui::Checkbox(
"Display bgfx stats", &showBgfxStats);
125 bgfx::setDebug(BGFX_DEBUG_TEXT | (showBgfxStats ? BGFX_DEBUG_STATS : 0));
129 fileBrowser->display(spriteRenderer);
134 bgfx::setViewRect(0, 0, 0, uint16_t(windowWidth), uint16_t(windowHeight));
140 spriteRenderer.draw(windowWidth, windowHeight);
148 static SDL_SystemCursor ImGuiMouseCursorToSDL(ImGuiMouseCursor imguiCursor)
152 case ImGuiMouseCursor_Arrow:
return SDL_SYSTEM_CURSOR_ARROW;
153 case ImGuiMouseCursor_TextInput:
return SDL_SYSTEM_CURSOR_IBEAM;
154 case ImGuiMouseCursor_ResizeAll:
return SDL_SYSTEM_CURSOR_SIZEALL;
155 case ImGuiMouseCursor_ResizeNS:
return SDL_SYSTEM_CURSOR_SIZENS;
156 case ImGuiMouseCursor_ResizeEW:
return SDL_SYSTEM_CURSOR_SIZEWE;
157 case ImGuiMouseCursor_ResizeNESW:
return SDL_SYSTEM_CURSOR_SIZENESW;
158 case ImGuiMouseCursor_ResizeNWSE:
return SDL_SYSTEM_CURSOR_SIZENWSE;
159 case ImGuiMouseCursor_Hand:
return SDL_SYSTEM_CURSOR_HAND;
161 case ImGuiMouseCursor_None:
162 case ImGuiMouseCursor_COUNT:
163 default: assert(
false);
return SDL_SYSTEM_CURSOR_ARROW;
167 void RendererApp::updateMouseAccordingToImgui()
170 const ImGuiIO& io = ImGui::GetIO();
174 if (io.WantSetMousePos)
175 SDL_WarpMouseInWindow(mainWindow, (
int)io.MousePos.x, (
int)io.MousePos.y);
177 if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange)
return;
179 const ImGuiMouseCursor imguiCursor = ImGui::GetMouseCursor();
180 if (io.MouseDrawCursor || imguiCursor == ImGuiMouseCursor_None) {
182 SDL_ShowCursor(SDL_FALSE);
187 SDL_SetCursor(systemCursors[ImGuiMouseCursorToSDL(imguiCursor)]);
188 SDL_ShowCursor(SDL_TRUE);
Implementation of a DCC file decoder.
Helper to load a Diablo 2 palette (.pal/.dat format)