yaze 0.3.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 <string>
6
7#include "absl/status/status.h"
12#include "app/gfx/resource/arena.h" // Add include for Arena
13#include "app/gfx/backend/sdl2_renderer.h" // Add include for new renderer
16#include "imgui/backends/imgui_impl_sdl2.h"
17#include "imgui/backends/imgui_impl_sdlrenderer2.h"
18#include "imgui/imgui.h"
19
20namespace yaze {
21
22absl::Status Controller::OnEntry(std::string filename) {
23 // Create renderer FIRST
24 renderer_ = std::make_unique<gfx::SDL2Renderer>();
25
26 // Call CreateWindow with our renderer
27 RETURN_IF_ERROR(CreateWindow(window_, renderer_.get(), SDL_WINDOW_RESIZABLE));
28
29 // Initialize the graphics Arena with the renderer
31
32 // Set up audio for emulator
33 editor_manager_.emulator().set_audio_buffer(window_.audio_buffer_.get());
34 editor_manager_.emulator().set_audio_device_id(window_.audio_device_);
35
36 // Initialize editor manager with renderer
37 editor_manager_.Initialize(renderer_.get(), filename);
38
39 active_ = true;
40 return absl::OkStatus();
41}
42
43void Controller::SetStartupEditor(const std::string& editor_name,
44 const std::string& cards) {
45 // Process command-line flags for editor and cards
46 // Example: --editor=Dungeon --cards="Rooms List,Room 0,Room 105"
47 if (!editor_name.empty()) {
49 }
50}
51
53 PRINT_IF_ERROR(HandleEvents(window_));
54}
55
56absl::Status Controller::OnLoad() {
58 active_ = false;
59 return absl::OkStatus();
60 }
61
62#if TARGET_OS_IPHONE != 1
63 ImGui_ImplSDLRenderer2_NewFrame();
64 ImGui_ImplSDL2_NewFrame();
65 ImGui::NewFrame();
66
67 const ImGuiViewport* viewport = ImGui::GetMainViewport();
68 ImGui::SetNextWindowPos(viewport->WorkPos);
69 ImGui::SetNextWindowSize(viewport->WorkSize);
70 ImGui::SetNextWindowViewport(viewport->ID);
71
72 ImGuiWindowFlags window_flags =
73 ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_NoDocking;
74 window_flags |= ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse |
75 ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove;
76 window_flags |=
77 ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoNavFocus;
78
79 ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
80 ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
81 ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
82 ImGui::Begin("DockSpaceWindow", nullptr, window_flags);
83 ImGui::PopStyleVar(3);
84
85 // Create DockSpace first
86 ImGuiID dockspace_id = ImGui::GetID("MyDockSpace");
88 dockspace_id, ImVec2(0.0f, 0.0f), ImGuiDockNodeFlags_PassthruCentralNode);
89
90 editor_manager_.DrawMenuBar(); // Draw the fixed menu bar at the top
91
92 ImGui::End();
93#endif
95 absl::Status update_status = editor_manager_.Update();
97 RETURN_IF_ERROR(update_status);
98 return absl::OkStatus();
99}
100
102 // Process all pending texture commands (batched to max 8 per frame).
104
105 ImGui::Render();
106 renderer_->Clear();
107 ImGui_ImplSDLRenderer2_RenderDrawData(
108 ImGui::GetDrawData(),
109 static_cast<SDL_Renderer*>(renderer_->GetBackendRenderer()));
110 renderer_->Present();
111
112 // Use TimingManager for accurate frame timing in sync with SDL
113 float delta_time = TimingManager::Get().Update();
114
115 // Gentle frame rate cap to prevent excessive CPU usage
116 // Only delay if we're rendering faster than 144 FPS (< 7ms per frame)
117 if (delta_time < 0.007f) {
118 SDL_Delay(1); // Tiny delay to yield CPU without affecting ImGui timing
119 }
120}
121
123 renderer_->Shutdown();
124 PRINT_IF_ERROR(ShutdownWindow(window_));
125}
126
127} // namespace yaze
editor::EditorManager editor_manager_
Definition controller.h:50
absl::Status OnEntry(std::string filename="")
Definition controller.cc:22
core::Window window_
Definition controller.h:49
void SetStartupEditor(const std::string &editor_name, const std::string &cards)
Definition controller.cc:43
absl::Status OnLoad()
Definition controller.cc:56
void DoRender() const
std::unique_ptr< gfx::IRenderer > renderer_
Definition controller.h:51
static TimingManager & Get()
Definition timing.h:19
float Update()
Update the timing manager (call once per frame)
Definition timing.h:28
void DrawMenuBar()
Draw the main menu bar.
void Initialize(gfx::IRenderer *renderer, const std::string &filename="")
void OpenEditorAndCardsFromFlags(const std::string &editor_name, const std::string &cards_str)
absl::Status Update()
Main update loop for the editor application.
auto emulator() -> emu::Emulator &
void Initialize(IRenderer *renderer)
Definition arena.cc:13
void ProcessTextureQueue(IRenderer *renderer)
Definition arena.cc:36
static Arena & Get()
Definition arena.cc:15
static void BeginEnhancedDockSpace(ImGuiID dockspace_id, const ImVec2 &size=ImVec2(0, 0), ImGuiDockNodeFlags flags=0)
static WidgetIdRegistry & Instance()
#define PRINT_IF_ERROR(expression)
Definition macro.h:27
#define RETURN_IF_ERROR(expression)
Definition macro.h:53
Main namespace for the application.
Definition controller.cc:20
std::shared_ptr< int16_t > audio_buffer_
Definition window.h:20
SDL_AudioDeviceID audio_device_
Definition window.h:19