yaze 0.2.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
controller.cc
Go to the documentation of this file.
1#include "controller.h"
2
3#include <SDL.h>
4
5#include <filesystem>
6#include <memory>
7
8#include "absl/status/status.h"
9#include "absl/strings/str_format.h"
13#include "app/gui/style.h"
14#include "imgui/backends/imgui_impl_sdl2.h"
15#include "imgui/backends/imgui_impl_sdlrenderer2.h"
16#include "imgui/imgui.h"
17
18namespace yaze {
19namespace core {
20
21namespace {
22// Helper function to draw the main window without docking enabled
23// Should be followed by ImGui::End() to close the window
25 constexpr ImGuiWindowFlags kMainEditorFlags =
26 ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoCollapse |
27 ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_MenuBar |
28 ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoTitleBar;
29
30 const ImGuiIO &io = ImGui::GetIO();
31 ImGui_ImplSDLRenderer2_NewFrame();
32 ImGui_ImplSDL2_NewFrame();
33 ImGui::NewFrame();
34 ImGui::SetNextWindowPos(gui::kZeroPos);
35 ImVec2 dimensions(io.DisplaySize.x, io.DisplaySize.y);
36 ImGui::SetNextWindowSize(dimensions, ImGuiCond_Always);
37
38 if (!ImGui::Begin("##YazeMain", nullptr, kMainEditorFlags)) {
39 ImGui::End();
40 }
41}
42} // namespace
43
44absl::Status Controller::OnEntry(std::string filename) {
48 backend_.init_audio();
49 // cast to Sdl2backend to access audio buffer and device
50 auto &sdl2_backend = static_cast<Sdl2Backend &>(backend_);
51 editor_manager_.emulator().set_audio_buffer(
52 sdl2_backend.audio_buffer().get());
53 editor_manager_.emulator().set_audio_device_id(sdl2_backend.audio_device());
54 Initialize(filename);
55 return absl::OkStatus();
56}
57
58void Controller::Initialize(std::string filename) {
59 editor_manager_.Initialize(filename);
60 active_ = true;
61}
62
64 ImGuiIO &io = ImGui::GetIO();
65 SDL_Event event;
66
67 while (SDL_PollEvent(&event)) {
68 ImGui_ImplSDL2_ProcessEvent(&event);
69 switch (event.type) {
70 case SDL_KEYDOWN:
71 case SDL_KEYUP: {
72 io.KeyShift = ((SDL_GetModState() & KMOD_SHIFT) != 0);
73 io.KeyCtrl = ((SDL_GetModState() & KMOD_CTRL) != 0);
74 io.KeyAlt = ((SDL_GetModState() & KMOD_ALT) != 0);
75 io.KeySuper = ((SDL_GetModState() & KMOD_GUI) != 0);
76 break;
77 }
78 case SDL_WINDOWEVENT:
79 switch (event.window.event) {
80 case SDL_WINDOWEVENT_CLOSE:
81 active_ = false;
82 break;
83 case SDL_WINDOWEVENT_SIZE_CHANGED:
84 io.DisplaySize.x = static_cast<float>(event.window.data1);
85 io.DisplaySize.y = static_cast<float>(event.window.data2);
86 break;
87 default:
88 break;
89 }
90 break;
91 default:
92 break;
93 }
94 }
95
96 int mouseX;
97 int mouseY;
98 const int buttons = SDL_GetMouseState(&mouseX, &mouseY);
99
100 io.DeltaTime = 1.0f / 60.0f;
101 io.MousePos = ImVec2(static_cast<float>(mouseX), static_cast<float>(mouseY));
102 io.MouseDown[0] = buttons & SDL_BUTTON(SDL_BUTTON_LEFT);
103 io.MouseDown[1] = buttons & SDL_BUTTON(SDL_BUTTON_RIGHT);
104 io.MouseDown[2] = buttons & SDL_BUTTON(SDL_BUTTON_MIDDLE);
105
106 int wheel = 0;
107 io.MouseWheel = static_cast<float>(wheel);
108}
109
110absl::Status Controller::OnLoad() {
111 if (editor_manager_.quit()) {
112 active_ = false;
113 }
114
115#if TARGET_OS_IPHONE != 1
116 ImGui_ImplSDLRenderer2_NewFrame();
117 ImGui_ImplSDL2_NewFrame();
118 ImGui::NewFrame();
119
120 const ImGuiViewport *viewport = ImGui::GetMainViewport();
121 ImGui::SetNextWindowPos(viewport->WorkPos);
122 ImGui::SetNextWindowSize(viewport->WorkSize);
123 ImGui::SetNextWindowViewport(viewport->ID);
124
125 ImGuiWindowFlags window_flags =
126 ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_NoDocking;
127 window_flags |= ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse |
128 ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove;
129 window_flags |=
130 ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoNavFocus;
131
132 ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
133 ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
134 ImGui::Begin("DockSpaceWindow", nullptr, window_flags);
135 ImGui::PopStyleVar(2);
136
137 // Create DockSpace
138 ImGuiID dockspace_id = ImGui::GetID("MyDockSpace");
139 ImGui::DockSpace(dockspace_id, ImVec2(0.0f, 0.0f),
140 ImGuiDockNodeFlags_PassthruCentralNode);
141
142 editor_manager_.DrawMenuBar(); // Draw the fixed menu bar at the top
143
144 ImGui::End();
145#endif
147 return absl::OkStatus();
148}
149
151 ImGui::Render();
152 SDL_RenderClear(Renderer::GetInstance().renderer());
153 ImGui_ImplSDLRenderer2_RenderDrawData(ImGui::GetDrawData(),
154 Renderer::GetInstance().renderer());
155 SDL_RenderPresent(Renderer::GetInstance().renderer());
156}
157
159 backend_.shutdown_audio();
160 ImGui_ImplSDLRenderer2_Shutdown();
161 ImGui_ImplSDL2_Shutdown();
162 ImGui::DestroyContext();
163 SDL_Quit();
164}
165
167 auto sdl_flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER;
168
169 if (SDL_Init(sdl_flags) != 0) {
170 return absl::InternalError(
171 absl::StrFormat("SDL_Init: %s\n", SDL_GetError()));
172 }
173
174 SDL_DisplayMode display_mode;
175 SDL_GetCurrentDisplayMode(0, &display_mode);
176 int screen_width = display_mode.w * 0.8;
177 int screen_height = display_mode.h * 0.8;
178
179 window_ = std::unique_ptr<SDL_Window, core::SDL_Deleter>(
180 SDL_CreateWindow("Yet Another Zelda3 Editor", // window title
181 SDL_WINDOWPOS_UNDEFINED, // initial x position
182 SDL_WINDOWPOS_UNDEFINED, // initial y position
183 screen_width, // width, in pixels
184 screen_height, // height, in pixels
185 SDL_WINDOW_RESIZABLE),
187 if (window_ == nullptr) {
188 return absl::InternalError(
189 absl::StrFormat("SDL_CreateWindow: %s\n", SDL_GetError()));
190 }
191
192 return absl::OkStatus();
193}
194
198
200 IMGUI_CHECKVERSION();
201 ImGui::CreateContext();
202
203 ImGuiIO &io = ImGui::GetIO();
204 io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
205 io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
206
207 // Initialize ImGui based on the backend
208 ImGui_ImplSDL2_InitForSDLRenderer(window_.get(),
209 Renderer::GetInstance().renderer());
210 ImGui_ImplSDLRenderer2_Init(Renderer::GetInstance().renderer());
211
213
214 // Set the default style
216
217 // Build a new ImGui frame
218 backend_.new_frame();
219
220 return absl::OkStatus();
221}
222
224 // Create and load a dotfile for the application
225 // This will store the user's preferences and settings
226 std::string config_directory = GetConfigDirectory();
227
228 // Create the directory if it doesn't exist
229 if (!std::filesystem::exists(config_directory)) {
230 if (!std::filesystem::create_directory(config_directory)) {
231 return absl::InternalError(absl::StrFormat(
232 "Failed to create config directory %s", config_directory));
233 }
234 }
235
236 // Check if the config file exists
237 std::string config_file = config_directory + "yaze.cfg";
238 if (!std::filesystem::exists(config_file)) {
239 // Create the file if it doesn't exist
240 std::ofstream file(config_file);
241 if (!file.is_open()) {
242 return absl::InternalError(
243 absl::StrFormat("Failed to create config file %s", config_file));
244 }
245 file.close();
246 }
247
248 return absl::OkStatus();
249}
250
251} // namespace core
252} // namespace yaze
absl::Status OnLoad()
absl::Status CreateWindow()
core::PlatformBackend< Sdl2Backend > backend_
Definition controller.h:54
void Initialize(std::string filename="")
Definition controller.cc:58
absl::Status CreateGuiContext()
absl::Status OnEntry(std::string filename="")
Definition controller.cc:44
editor::EditorManager editor_manager_
Definition controller.h:52
std::shared_ptr< SDL_Window > window_
Definition controller.h:53
absl::Status CreateRenderer()
absl::Status LoadConfigFiles()
static Renderer & GetInstance()
Definition renderer.h:26
absl::Status CreateRenderer(SDL_Window *window)
Definition renderer.h:31
#define RETURN_IF_ERROR(expression)
Definition macro.h:51
absl::Status LoadPackageFonts()
std::string GetConfigDirectory()
constexpr ImVec2 kZeroPos
Definition input.h:23
void ColorsYaze()
Definition style.cc:132
Main namespace for the application.
Definition controller.cc:18
Deleter for SDL_Window and SDL_Renderer.
Definition sdl_deleter.h:12