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