From 7903e0cdc470a67818376fd853d8e495701ee9f1 Mon Sep 17 00:00:00 2001 From: Justin Scofield Date: Thu, 4 Aug 2022 19:03:35 +0000 Subject: [PATCH 1/3] Convert to status codes for Controller init. --- src/app/core/controller.cc | 85 ++++---------------------------------- src/app/core/controller.h | 12 ++---- 2 files changed, 12 insertions(+), 85 deletions(-) diff --git a/src/app/core/controller.cc b/src/app/core/controller.cc index 5a8b7b63..4930d166 100644 --- a/src/app/core/controller.cc +++ b/src/app/core/controller.cc @@ -79,13 +79,14 @@ void HandleMouseMovement(int &wheel) { bool Controller::isActive() const { return active_; } -void Controller::onEntry() { - CreateWindow(); - CreateRenderer(); - CreateGuiContext(); +absl::Status Controller::onEntry() { + CHECK_STATUS(CreateWindow()) + CHECK_STATUS(CreateRenderer()) + CHECK_STATUS(CreateGuiContext()) InitializeKeymap(); master_editor_.SetupScreen(renderer_); active_ = true; + return absl::OkStatus(); } void Controller::onInput() { @@ -144,77 +145,7 @@ void Controller::onExit() const { SDL_Quit(); } -void Controller::CreateWindow() { - if (SDL_Init(SDL_INIT_EVERYTHING)) { - SDL_Log("SDL_Init: %s\n", SDL_GetError()); - } else { - window_ = std::unique_ptr( - SDL_CreateWindow("Yet Another Zelda3 Editor", // window title - SDL_WINDOWPOS_UNDEFINED, // initial x position - SDL_WINDOWPOS_UNDEFINED, // initial y position - 1200, // width, in pixels - 800, // height, in pixels - SDL_WINDOW_RESIZABLE), - sdl_deleter()); - } -} - -void Controller::CreateRenderer() { - if (window_ == nullptr) { - SDL_Log("SDL_CreateWindow: %s\n", SDL_GetError()); - SDL_Quit(); - } else { - renderer_ = std::unique_ptr( - SDL_CreateRenderer( - window_.get(), -1, - SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC), - sdl_deleter()); - if (renderer_ == nullptr) { - SDL_Log("SDL_CreateRenderer: %s\n", SDL_GetError()); - SDL_Quit(); - } else { - SDL_SetRenderDrawBlendMode(renderer_.get(), SDL_BLENDMODE_BLEND); - SDL_SetRenderDrawColor(renderer_.get(), 0x00, 0x00, 0x00, 0x00); - } - } -} - -void Controller::CreateGuiContext() const { - // Create the ImGui and ImPlot contexts - ImGui::CreateContext(); - - // Initialize ImGui for SDL - ImGui_ImplSDL2_InitForSDLRenderer(window_.get(), renderer_.get()); - ImGui_ImplSDLRenderer_Init(renderer_.get()); - - // Load available fonts - const ImGuiIO &io = ImGui::GetIO(); - io.Fonts->AddFontFromFileTTF("assets/font/Karla-Regular.ttf", 14.0f); - - // merge in icons from Google Material Design - static const ImWchar icons_ranges[] = {ICON_MIN_MD, 0xf900, 0}; - ImFontConfig icons_config; - icons_config.MergeMode = true; - icons_config.GlyphOffset.y = 5.0f; - icons_config.GlyphMinAdvanceX = 13.0f; - icons_config.PixelSnapH = true; - io.Fonts->AddFontFromFileTTF(FONT_ICON_FILE_NAME_MD, 18.0f, &icons_config, - icons_ranges); - io.Fonts->AddFontFromFileTTF("assets/font/Roboto-Medium.ttf", 14.0f); - io.Fonts->AddFontFromFileTTF("assets/font/Cousine-Regular.ttf", 14.0f); - io.Fonts->AddFontFromFileTTF("assets/font/DroidSans.ttf", 16.0f); - - // Set the default style - gui::ColorsYaze(); - - // Build a new ImGui frame - ImGui_ImplSDLRenderer_NewFrame(); - ImGui_ImplSDL2_NewFrame(window_.get()); -} - -// V2 functions --------------------------------------------------------------- - -absl::Status Controller::CreateWindowV2() { +absl::Status Controller::CreateWindow() { if (SDL_Init(SDL_INIT_EVERYTHING) != 0) { return absl::InternalError( absl::StrFormat("SDL_Init: %s\n", SDL_GetError())); @@ -235,7 +166,7 @@ absl::Status Controller::CreateWindowV2() { return absl::OkStatus(); } -absl::Status Controller::CreateRendererV2() { +absl::Status Controller::CreateRenderer() { renderer_ = std::unique_ptr( SDL_CreateRenderer(window_.get(), -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC), @@ -250,7 +181,7 @@ absl::Status Controller::CreateRendererV2() { return absl::OkStatus(); } -absl::Status Controller::CreateGuiContextV2() { +absl::Status Controller::CreateGuiContext() { ImGui::CreateContext(); // Initialize ImGui for SDL diff --git a/src/app/core/controller.h b/src/app/core/controller.h index 7c66b04f..d3bb255b 100644 --- a/src/app/core/controller.h +++ b/src/app/core/controller.h @@ -23,7 +23,7 @@ namespace core { class Controller { public: bool isActive() const; - void onEntry(); + absl::Status onEntry(); void onInput(); void onLoad(); void doRender() const; @@ -36,15 +36,11 @@ class Controller { void operator()(SDL_Texture *p) const { SDL_DestroyTexture(p); } }; - void CreateWindow(); - void CreateRenderer(); - void CreateGuiContext() const; + absl::Status CreateWindow(); + absl::Status CreateRenderer(); + absl::Status CreateGuiContext(); void CloseWindow() { active_ = false; } - absl::Status CreateWindowV2(); - absl::Status CreateRendererV2(); - absl::Status CreateGuiContextV2(); - friend int ::main(int argc, char **argv); bool active_; From c1ac8cd71c514c9a6dbd6681e1ca79f81e8d95b9 Mon Sep 17 00:00:00 2001 From: Justin Scofield Date: Thu, 4 Aug 2022 19:03:46 +0000 Subject: [PATCH 2/3] Add CHECK_STATUS macro --- src/app/core/constants.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/app/core/constants.h b/src/app/core/constants.h index 4986be5a..55a0e640 100644 --- a/src/app/core/constants.h +++ b/src/app/core/constants.h @@ -25,6 +25,11 @@ #define MENU_ITEM(w) if (ImGui::MenuItem(w)) #define MENU_ITEM2(w, v) if (ImGui::MenuItem(w, v)) +#define CHECK_STATUS(w) \ + if (!w.ok()) { \ + return w; \ + } + using ushort = unsigned short; using uint = unsigned int; using uchar = unsigned char; From 3b0dc9c195f43a1d19523dc8862ce2250f8ade9e Mon Sep 17 00:00:00 2001 From: Justin Scofield Date: Thu, 4 Aug 2022 19:04:09 +0000 Subject: [PATCH 3/3] Return Controller onEntry error in main. --- src/yaze.cc | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/yaze.cc b/src/yaze.cc index b1699c80..5c780550 100644 --- a/src/yaze.cc +++ b/src/yaze.cc @@ -11,7 +11,13 @@ int main(int argc, char** argv) { absl::InstallFailureSignalHandler(options); yaze::app::core::Controller controller; - controller.onEntry(); + + auto entry_status = controller.onEntry(); + if (!entry_status.ok()) { + // TODO(@scawful): log the specific error + return EXIT_FAILURE; + } + while (controller.isActive()) { controller.onInput(); controller.onLoad();