Started Yaze implementation using ImGui and improving workflow from previous projects

This commit is contained in:
scawful
2022-06-08 23:22:01 -04:00
parent accf24c244
commit bdafdcf069
11 changed files with 476 additions and 0 deletions

View File

@@ -0,0 +1,100 @@
#include "Controller.h"
namespace yaze {
namespace Application {
namespace Core {
bool Controller::isActive() const { return active; }
void Controller::onEntry() noexcept(false) {
window.Create();
renderer.Create(window.Get());
active = true;
}
void Controller::onInput() {
int wheel = 0;
SDL_Event event;
ImGuiIO& io = ImGui::GetIO();
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_UP:
case SDLK_DOWN:
case SDLK_RETURN:
case SDLK_BACKSPACE:
case SDLK_TAB:
io.KeysDown[event.key.keysym.scancode] =
(event.type == SDL_KEYDOWN);
break;
default:
break;
}
break;
case SDL_KEYUP: {
int key = event.key.keysym.scancode;
IM_ASSERT(key >= 0 && key < IM_ARRAYSIZE(io.KeysDown));
io.KeysDown[key] = (event.type == SDL_KEYDOWN);
io.KeyShift = ((SDL_GetModState() & KMOD_SHIFT) != 0);
io.KeyCtrl = ((SDL_GetModState() & KMOD_CTRL) != 0);
io.KeyAlt = ((SDL_GetModState() & KMOD_ALT) != 0);
io.KeySuper = ((SDL_GetModState() & KMOD_GUI) != 0);
break;
}
case SDL_WINDOWEVENT:
switch (event.window.event) {
case SDL_WINDOWEVENT_CLOSE:
active = false;
break;
case SDL_WINDOWEVENT_SIZE_CHANGED:
io.DisplaySize.x = static_cast<float>(event.window.data1);
io.DisplaySize.y = static_cast<float>(event.window.data2);
break;
default:
break;
}
break;
case SDL_TEXTINPUT:
io.AddInputCharactersUTF8(event.text.text);
break;
case SDL_MOUSEWHEEL:
wheel = event.wheel.y;
break;
default:
break;
}
}
int mouseX;
int mouseY;
const int buttons = SDL_GetMouseState(&mouseX, &mouseY);
io.DeltaTime = 1.0f / 60.0f;
io.MousePos = ImVec2(static_cast<float>(mouseX), static_cast<float>(mouseY));
io.MouseDown[0] = buttons & SDL_BUTTON(SDL_BUTTON_LEFT);
io.MouseDown[1] = buttons & SDL_BUTTON(SDL_BUTTON_RIGHT);
io.MouseWheel = static_cast<float>(wheel);
}
void Controller::onLoad() const {
editor->UpdateScreen();
}
void Controller::doRender() {
renderer.Render();
}
void Controller::onExit() {
ImGui_ImplSDLRenderer_Shutdown();
ImGui_ImplSDL2_Shutdown();
ImGui::DestroyContext();
window.Destroy();
renderer.Destroy();
SDL_Quit();
}
} // namespace Core
} // namespace Application
} // namespace yaze

View File

@@ -0,0 +1,50 @@
#ifndef YAZE_APPLICATION_CORE_CONTROLLER_H
#define YAZE_APPLICATION_CORE_CONTROLLER_H
#include <memory>
#include <SDL2/SDL.h>
#include "imgui/imgui.h"
#include "imgui/imgui_internal.h"
#include "imgui/backends/imgui_impl_sdl.h"
#include "imgui/backends/imgui_impl_sdlrenderer.h"
#include "imgui/misc/cpp/imgui_stdlib.h"
#include "Window.h"
#include "Renderer.h"
#include "Events/Event.h"
#include "View/Editor.h"
int main(int argc, char** argv);
namespace yaze {
namespace Application {
namespace Core {
class Controller {
public:
Controller() = default;
bool isActive() const;
void onEntry();
void onInput();
void onLoad() const;
void doRender();
void onExit();
private:
Window window;
Renderer renderer;
std::unique_ptr<View::Editor> editor;
bool active = false;
void quit() { active = false; }
friend int ::main(int argc, char** argv);
};
} // namespace Core
} // namespace Application
} // namespace yaze
#endif // YAZE_APPLICATION_CORE_CONTROLLER_H

View File

@@ -0,0 +1,23 @@
#ifndef YAZE_APPLICATION_CONTROLLER_ENTRYPOINT_H
#define YAZE_APPLICATION_CONTROLLER_ENTRYPOINT_H
#include "Controller.h"
using namespace yaze::Application::Core;
int main(int argc, char** argv)
{
Controller controller;
controller.onEntry();
while (controller.isActive()) {
controller.onInput();
controller.onLoad();
controller.doRender();
}
controller.onExit();
return EXIT_SUCCESS;
}
#endif // YAZE_APPLICATION_CONTROLLER_ENTRYPOINT_H

View File

@@ -0,0 +1,50 @@
#include "Renderer.h"
namespace yaze {
namespace Application {
namespace Core {
void Renderer::Create(SDL_Window* window) {
if (window == nullptr) {
SDL_Log("SDL_CreateWindow: %s\n", SDL_GetError());
SDL_Quit();
} else {
renderer = SDL_CreateRenderer(
window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (renderer == nullptr) {
SDL_Log("SDL_CreateRenderer: %s\n", SDL_GetError());
SDL_Quit();
} else {
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00);
}
}
// Create the ImGui and ImPlot contexts
ImGui::CreateContext();
// Initialize ImGui for SDL
ImGui_ImplSDL2_InitForSDLRenderer(window, renderer);
ImGui_ImplSDLRenderer_Init(renderer);
// Build a new ImGui frame
ImGui_ImplSDLRenderer_NewFrame();
ImGui_ImplSDL2_NewFrame(window);
}
void Renderer::Render() {
SDL_RenderClear(renderer);
ImGui::Render();
ImGui_ImplSDLRenderer_RenderDrawData(ImGui::GetDrawData());
SDL_RenderPresent(renderer);
}
void Renderer::Destroy() {
SDL_DestroyRenderer(renderer);
renderer = nullptr;
}
} // namespace Core
} // namespace Application
} // namespace yaze

View File

@@ -0,0 +1,30 @@
#ifndef YAZE_APPLICATION_CORE_RENDERER_H
#define YAZE_APPLICATION_CORE_RENDERER_H
#include <SDL2/SDL.h>
#include "imgui/imgui.h"
#include "imgui/imgui_internal.h"
#include "imgui/backends/imgui_impl_sdl.h"
#include "imgui/backends/imgui_impl_sdlrenderer.h"
#include "imgui/misc/cpp/imgui_stdlib.h"
namespace yaze {
namespace Application {
namespace Core {
class Renderer {
public:
void Create(SDL_Window* window);
void Render();
void Destroy();
private:
SDL_Renderer* renderer = nullptr;
};
} // namespace Core
} // namespace Application
} // namespace yaze
#endif // YAZE_APPLICATION_CORE_RENDERER_H

View File

@@ -0,0 +1,33 @@
#include "Window.h"
namespace yaze {
namespace Application {
namespace Core {
// TODO: pass in the size of the window as argument
void Window::Create() {
if (SDL_Init(SDL_INIT_EVERYTHING)) {
SDL_Log("SDL_Init: %s\n", SDL_GetError());
} else {
window = SDL_CreateWindow("Yet Another Zelda3 Editor", // window title
SDL_WINDOWPOS_UNDEFINED, // initial x position
SDL_WINDOWPOS_UNDEFINED, // initial y position
800, // width, in pixels
600, // height, in pixels
SDL_WINDOW_RESIZABLE // window flags
);
}
}
SDL_Window* Window::Get() {
return window;
}
void Window::Destroy() {
SDL_DestroyWindow(window);
window = nullptr;
}
} // namespace Core
} // namespace Application
} // namespace yaze

View File

@@ -0,0 +1,24 @@
#ifndef YAZE_APPLICATION_CORE_WINDOW_H
#define YAZE_APPLICATION_CORE_WINDOW_H
#include <SDL2/SDL.h>
namespace yaze {
namespace Application {
namespace Core {
class Window {
public:
void Create();
SDL_Window* Get();
void Destroy();
private:
SDL_Window* window = nullptr;
};
} // namespace Core
} // namespace Application
} // namespace yaze
#endif

View File

@@ -0,0 +1,12 @@
#include "Event.h"
namespace yaze {
namespace Application {
namespace Events {
void Event::Assign(const std::function<void()>& event) { event_ = event; }
void Event::Trigger() const { event_(); }
} // namespace Events
} // namespace Application
} // namespace yaze

View File

@@ -0,0 +1,23 @@
#ifndef YAZE_APPLICATION_EVENTS_EVENT_H
#define YAZE_APPLICATION_EVENTS_EVENT_H
#include <functional>
namespace yaze {
namespace Application {
namespace Events {
class Event {
public:
void Assign(const std::function<void()> & event);
void Trigger() const;
private:
std::function<void()> event_;
};
} // namespace Events
} // namespace Application
} // namespace yaze
#endif // YAZE_APPLICATION_EVENTS_EVENT_H

View File

@@ -0,0 +1,103 @@
#include "Editor.h"
namespace yaze {
namespace Application {
namespace View {
void Editor::UpdateScreen() const {
const ImGuiIO& io = ImGui::GetIO();
ImGui::NewFrame();
ImGui::SetNextWindowPos(ImVec2(0, 0));
ImVec2 dimensions(io.DisplaySize.x, io.DisplaySize.y);
ImGui::SetNextWindowSize(dimensions, ImGuiCond_Always);
ImGuiWindowFlags flags =
ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoCollapse |
ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoScrollbar |
ImGuiWindowFlags_MenuBar;
if (!ImGui::Begin("Main", nullptr, flags)) {
ImGui::End();
return;
}
DrawYazeMenu();
ImGui::End();
}
void Editor::DrawYazeMenu() const {
if (ImGui::BeginMenuBar()) {
if (ImGui::BeginMenu("File")) {
DrawFileMenu();
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Edit")) {
DrawEditMenu();
ImGui::EndMenu();
}
ImGui::EndMenuBar();
}
}
void Editor::DrawFileMenu() const {
if (ImGui::MenuItem("Open", "Ctrl+O")) {
// TODO: Add the ability to open ALTTP ROM
}
if (ImGui::BeginMenu("Open Recent")) {
ImGui::MenuItem("alttp.sfc");
// TODO: Display recently accessed files here
ImGui::EndMenu();
}
if (ImGui::MenuItem("Save", "Ctrl+S")) {
// TODO: Implement this
}
if (ImGui::MenuItem("Save As..")) {
// TODO: Implement this
}
ImGui::Separator();
// TODO: Make these options matter
if (ImGui::BeginMenu("Options")) {
static bool enabled = true;
ImGui::MenuItem("Enabled", "", &enabled);
ImGui::BeginChild("child", ImVec2(0, 60), true);
for (int i = 0; i < 10; i++)
ImGui::Text("Scrolling Text %d", i);
ImGui::EndChild();
static float f = 0.5f;
static int n = 0;
ImGui::SliderFloat("Value", &f, 0.0f, 1.0f);
ImGui::InputFloat("Input", &f, 0.1f);
ImGui::Combo("Combo", &n, "Yes\0No\0Maybe\0\0");
ImGui::EndMenu();
}
}
void Editor::DrawEditMenu() const {
if (ImGui::MenuItem("Undo", "Ctrl+O")) {
// TODO: Implement this
}
if (ImGui::MenuItem("Undo", "Ctrl+O")) {
// TODO: Implement this
}
ImGui::Separator();
if (ImGui::MenuItem("Cut", "Ctrl+X")) {
// TODO: Implement this
}
if (ImGui::MenuItem("Copy", "Ctrl+C")) {
// TODO: Implement this
}
if (ImGui::MenuItem("Paste", "Ctrl+V")) {
// TODO: Implement this
}
ImGui::Separator();
if (ImGui::MenuItem("Find Tiles", "Ctrl+F")) {
// TODO: Implement this
}
}
} // namespace View
} // namespace Application
} // namespace yaze

View File

@@ -0,0 +1,28 @@
#ifndef YAZE_APPLICATION_VIEW_EDITOR_H
#define YAZE_APPLICATION_VIEW_EDITOR_H
#include "imgui/imgui.h"
#include "imgui/imgui_internal.h"
#include "imgui/backends/imgui_impl_sdl.h"
#include "imgui/backends/imgui_impl_sdlrenderer.h"
#include "imgui/misc/cpp/imgui_stdlib.h"
namespace yaze {
namespace Application {
namespace View {
class Editor {
public:
void UpdateScreen() const;
private:
void DrawYazeMenu() const;
void DrawFileMenu() const;
void DrawEditMenu() const;
};
} // namespace View
} // namespace Application
} // namespace yaze
#endif // YAZE_APPLICATION_VIEW_EDITOR_H