Begin Repo

This commit is contained in:
2024-08-10 21:20:28 -05:00
commit 08115f90ce
41 changed files with 19581 additions and 0 deletions

92
layers/baseLayer.cpp Normal file
View File

@@ -0,0 +1,92 @@
#include "baseLayer.h"
#include "application.h"
BaseLayer::BaseLayer()
{
}
BaseLayer::~BaseLayer()
{
}
void BaseLayer::onAttach()
{
}
void BaseLayer::onDetach()
{
}
void BaseLayer::onUpdate()
{
}
bool BaseLayer::onEvent(const Event* e)
{
EventHandler eh;
return eh.handleEvent(e, this);
}
bool BaseLayer::onMouseButtonPressed(const MouseButtonEvent *e)
{
return true;
}
bool BaseLayer::onMouseButtonReleased(const MouseButtonEvent *e)
{
App::Get().show = !App::Get().show;
return true;
}
bool BaseLayer::onMouseMoved(const MouseMovedEvent *e)
{
return true;
}
bool BaseLayer::onMouseScrolled(const MouseScrolledEvent *e)
{
return true;
}
bool BaseLayer::onKeyPressed(const KeyEvent *e)
{
return true;
}
bool BaseLayer::onKeyReleased(const KeyEvent *e)
{
return true;
}
bool BaseLayer::onKeyTyped(const KeyTypedEvent *)
{
return true;
}
bool BaseLayer::onWindowClose(const WindowEvent *e)
{
if (e->getWindowID() == SDL_GetWindowID(App::Get().getWindow())) {
App::Get().done = true;
}
return true;
}
bool BaseLayer::onWindowFocus(const WindowEvent *e)
{
return true;
}
bool BaseLayer::onWindowFocusLost(const WindowEvent *e)
{
return true;
}
bool BaseLayer::onWindowMoved(const WindowEvent *e)
{
return true;
}
bool BaseLayer::onWindowResize(const WindowEvent *e)
{
return true;
}

32
layers/baseLayer.h Normal file
View File

@@ -0,0 +1,32 @@
#pragma once
#include "terminal.h"
#include "layer.h"
#include "eventHandler.h"
class BaseLayer : public Layer {
public:
BaseLayer();
~BaseLayer();
virtual void onAttach();
virtual void onDetach();
virtual void onUpdate();
virtual bool onEvent(const Event*);
protected:
virtual bool onMouseButtonPressed(const MouseButtonEvent*);
virtual bool onMouseButtonReleased(const MouseButtonEvent*);
virtual bool onMouseMoved(const MouseMovedEvent*);
virtual bool onMouseScrolled(const MouseScrolledEvent*);
virtual bool onKeyPressed(const KeyEvent*);
virtual bool onKeyReleased(const KeyEvent*);
virtual bool onKeyTyped(const KeyTypedEvent*);
virtual bool onWindowClose(const WindowEvent*);
virtual bool onWindowFocus(const WindowEvent*);
virtual bool onWindowFocusLost(const WindowEvent*);
virtual bool onWindowMoved(const WindowEvent*);
virtual bool onWindowResize(const WindowEvent*);
};

8
layers/layer.cpp Normal file
View File

@@ -0,0 +1,8 @@
#include "layer.h"
#include "application.h"
Layer::Layer() {}
Layer::~Layer() {}

35
layers/layer.h Normal file
View File

@@ -0,0 +1,35 @@
#pragma once
#include "terminal.h"
#include "event.h"
class Layer {
friend class EventHandler;
public:
Layer();
virtual ~Layer();
virtual void onAttach() = 0;
virtual void onDetach() = 0;
virtual void onUpdate() = 0;
virtual bool onEvent(const Event* event) = 0;
protected:
virtual bool onMouseButtonPressed(const MouseButtonEvent* event) = 0;
virtual bool onMouseButtonReleased(const MouseButtonEvent* event) = 0;
virtual bool onMouseMoved(const MouseMovedEvent* event) = 0;
virtual bool onMouseScrolled(const MouseScrolledEvent* event) = 0;
virtual bool onKeyPressed(const KeyEvent* event) = 0;
virtual bool onKeyReleased(const KeyEvent* event) = 0;
virtual bool onKeyTyped(const KeyTypedEvent* event) = 0;
virtual bool onWindowResize(const WindowEvent* event) = 0;
virtual bool onWindowFocus(const WindowEvent* event) = 0;
virtual bool onWindowFocusLost(const WindowEvent* event) = 0;
virtual bool onWindowClose(const WindowEvent* event) = 0;
virtual bool onWindowMoved(const WindowEvent* event) = 0;
};

77
layers/layerstack.cpp Normal file
View File

@@ -0,0 +1,77 @@
#include "layerstack.h"
#include "application.h"
LayerStack::LayerStack() {
insert = layers.begin();
}
LayerStack::~LayerStack() {
for(Layer* layer : layers) {
delete layer;
}
}
void LayerStack::pushLayer(Layer* layer) {
layers.emplace(insert, layer);
layer->onAttach();
}
void LayerStack::pushOverlay(Layer* layer) {
layers.emplace_back(layer);
layer->onAttach();
}
void LayerStack::popLayer(Layer* layer) {
auto it = find(layers.begin(), layers.end(), layer);
if(it != layers.end()) {
layers.erase(it);
insert--;
}
layer->onDetach();
}
void LayerStack::popOverlay(Layer* layer) {
auto it = find(layers.begin(), layers.end(), layer);
if(it != layers.end()) {
layers.erase(it);
}
layer->onDetach();
}
void LayerStack::propagateEvent(const Event* e) {
eventHandled = false;
if(e == nullptr)
return;
else {
if(layers.size() > 0) {
for(int i = layers.size() - 1; i >= 0; i--) {
if(!eventHandled)
eventHandled = layers.at(i)->onEvent(e);
else
break;
}
}
delete e;
}
}
void LayerStack::updateLayers() {
if(layers.size() > 0) {
for(Layer* layer : layers) {
layer->onUpdate();
}
}
}
void LayerStack::shutdown() {
if(layers.size() > 0) {
for(Layer* layer : layers) {
popLayer(layer);
layer->onDetach();
delete layer;
}
}
}

32
layers/layerstack.h Normal file
View File

@@ -0,0 +1,32 @@
#pragma once
#include "terminal.h"
#include "layer.h"
class LayerStack {
public:
LayerStack();
~LayerStack();
void pushLayer(Layer*);
void pushOverlay(Layer*);
void popLayer(Layer*);
void popOverlay(Layer*);
void propagateEvent(const Event*);
void updateLayers();
void shutdown();
inline Layer* getLayer(unsigned long i) { return layers.at(i); }
std::vector<Layer*>::iterator begin() {return layers.begin();}
std::vector<Layer*>::iterator end() {return layers.end();}
private:
std::vector<Layer*> layers;
std::vector<Layer*>::iterator insert;
bool eventHandled;
};

271
layers/overlay.cpp Normal file
View File

@@ -0,0 +1,271 @@
#include "overlay.h"
#include "application.h"
static void check_vk_result(VkResult err)
{
if (err == 0)
return;
fprintf(stderr, "[vulkan] Error: VkResult = %d\n", err);
if (err < 0)
abort();
}
Overlay::Overlay() {
}
Overlay::~Overlay() {
}
void Overlay::onAttach() {
init();
}
void Overlay::onDetach() {
}
void Overlay::onUpdate() {
newFrame();
define(nullptr, [](Overlay& overlay, void** data) {
if(App::Get().show)
ImGui::ShowDemoWindow();
{
static char filename[24] = "test.py";
static std::vector<char> text(2048);
static std::fstream file;
ImGui::Begin("Main Editor");
ImGui::InputText("File Name", filename, 24);
ImGui::SameLine();
if(ImGui::Button("load", ImVec2(48, 16))) {
file.open(filename, std::ios::in);
if(file.is_open()) {
for(int i = 0; i < text.size(); i++) {
if(!file.eof())
text.at(i) = file.get();
else
text.at(i) = '\r';
}
file.close();
} else {
std::cout << "file does not exist!\n";
}
}
ImGui::InputTextMultiline("Editor", text.data(), text.size(),
ImVec2(-FLT_MIN, ImGui::GetTextLineHeight() * 16),
ImGuiInputTextFlags_AllowTabInput);
if(ImGui::Button("save", ImVec2(48, 16))) {
file.open(filename, std::ios::out | std::ios::trunc);
if(file.is_open()){
for(char c : text)
file.put(c == '\0' ? ' ' : c);
file.close();
} else {
std::cout << "file could not be opened!\n";
}
}
ImGui::SameLine();
if(ImGui::Button("run", ImVec2(48, 16))) {
if (system(("python " + std::string(filename)).c_str()))
std::cout << "command failed!\n";
}
ImGui::End();
}
return 0;
});
render();
}
bool Overlay::onEvent(const Event* event) {
EventHandler eh;
return eh.handleEvent(event, this);
}
bool Overlay::onMouseButtonPressed(const MouseButtonEvent* event) {
ImGuiIO& io = ImGui::GetIO();
if(io.WantCaptureMouse)
io.AddMouseButtonEvent(event->getButton(), event->getPressed());
return io.WantCaptureMouse;
}
bool Overlay::onMouseButtonReleased(const MouseButtonEvent* event) {
ImGuiIO& io = ImGui::GetIO();
if(io.WantCaptureMouse)
io.AddMouseButtonEvent(event->getButton(), event->getPressed());
return io.WantCaptureMouse;
}
bool Overlay::onMouseMoved(const MouseMovedEvent* event) {
ImGuiIO& io = ImGui::GetIO();
if(io.WantCaptureMouse)
io.AddMousePosEvent(event->getX(), event->getY());
return io.WantCaptureMouse;
}
bool Overlay::onMouseScrolled(const MouseScrolledEvent* event) {
ImGuiIO& io = ImGui::GetIO();
if(io.WantCaptureMouse)
io.AddMouseWheelEvent(event->getX(), event->getY());
return io.WantCaptureMouse;
}
bool Overlay::onKeyPressed(const KeyEvent* event) {
ImGuiIO& io = ImGui::GetIO();
if(io.WantCaptureKeyboard) {
ImGui_ImplSDL2_UpdateKeyModifiers((SDL_Keymod)event->getMod());
io.AddKeyEvent(ImGui_ImplSDL2_KeyEventToImGuiKey(event->getKey(), (SDL_Scancode)event->getScancode()),
event->getPressed());
}
return io.WantCaptureKeyboard;
}
bool Overlay::onKeyReleased(const KeyEvent* event) {
ImGuiIO& io = ImGui::GetIO();
if(io.WantCaptureKeyboard) {
ImGui_ImplSDL2_UpdateKeyModifiers((SDL_Keymod)event->getMod());
io.AddKeyEvent(ImGui_ImplSDL2_KeyEventToImGuiKey(event->getKey(), (SDL_Scancode)event->getScancode()),
event->getPressed());
}
return io.WantCaptureKeyboard;
}
bool Overlay::onKeyTyped(const KeyTypedEvent *e)
{
ImGuiIO& io = ImGui::GetIO();
if(io.WantCaptureKeyboard)
io.AddInputCharactersUTF8(e->getText());
return io.WantCaptureKeyboard;
}
bool Overlay::onWindowClose(const WindowEvent *e)
{
return false;
}
bool Overlay::onWindowFocus(const WindowEvent *e)
{
return false;
}
bool Overlay::onWindowFocusLost(const WindowEvent *e)
{
return false;
}
bool Overlay::onWindowMoved(const WindowEvent *e)
{
return false;
}
bool Overlay::onWindowResize(const WindowEvent *e)
{
Window& window = App::Get().getWindow();
window.getSize();
ImGui::GetIO().DisplaySize = ImVec2(window.x, window.y);
return false;
}
void Overlay::init() {
App& app = App::Get();
Renderer& renderer = app.getRenderer();
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows
ImGui::StyleColorsDark();
ImGuiStyle& style = ImGui::GetStyle();
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
style.WindowRounding = 0.0f;
style.Colors[ImGuiCol_WindowBg].w = 1.0f;
}
ImGui_ImplSDL2_InitForVulkan(app.getWindow());
ImGui_ImplVulkan_InitInfo init_info = {};
init_info.Instance = renderer.getInstance();
init_info.Queue = renderer.getPresentQueue();
init_info.DescriptorPool = renderer.getDescriptorPool();
init_info.RenderPass = renderer.getRenderPass();
init_info.Device = renderer.getDevice();
init_info.PhysicalDevice = renderer.getPhysicalDevice();
// init_info.MSAASamples = renderer.getMSAASamples();
init_info.MinImageCount = renderer.getNumImages();
init_info.ImageCount = renderer.getNumImages();
ImGui_ImplVulkan_Init(&init_info);
ImGui_ImplVulkan_CreateFontsTexture();
}
void Overlay::shutdown() {
ImGui_ImplVulkan_Shutdown();
ImGui_ImplSDL2_Shutdown();
ImGui::DestroyContext();
}
void Overlay::newFrame() {
ImGui_ImplVulkan_NewFrame();
ImGui_ImplSDL2_NewFrame();
ImGui::NewFrame();
}
void Overlay::renderPlatformWindows() {
if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_ViewportsEnable) {
ImGui::UpdatePlatformWindows();
ImGui::RenderPlatformWindowsDefault();
}
}
void Overlay::render() {
Renderer& renderer = App::Get().getRenderer();
ImGui::Render();
main_draw_data = ImGui::GetDrawData();
mainMinimized = (main_draw_data->DisplaySize.x <= 0.0f || main_draw_data->DisplaySize.y <= 0.0f);
ImGui_ImplVulkan_RenderDrawData(main_draw_data, renderer.getCommandBuffer(renderer.getFrame()));
renderPlatformWindows();
}
bool Overlay::isMinimized() {
return mainMinimized;
}

209
layers/overlay.h Normal file
View File

@@ -0,0 +1,209 @@
#pragma once
#include "terminal.h"
#include "layer.h"
#include "sdl2Window.h"
#include "vulkanRenderer.h"
class Overlay : public Layer {
public:
Overlay();
~Overlay();
virtual void onAttach();
virtual void onDetach();
virtual void onUpdate();
virtual bool onEvent(const Event*);
protected:
virtual bool onMouseButtonPressed(const MouseButtonEvent*);
virtual bool onMouseButtonReleased(const MouseButtonEvent*);
virtual bool onMouseMoved(const MouseMovedEvent*);
virtual bool onMouseScrolled(const MouseScrolledEvent*);
virtual bool onKeyPressed(const KeyEvent*);
virtual bool onKeyReleased(const KeyEvent*);
virtual bool onKeyTyped(const KeyTypedEvent*);
virtual bool onWindowClose(const WindowEvent*);
virtual bool onWindowFocus(const WindowEvent*);
virtual bool onWindowFocusLost(const WindowEvent*);
virtual bool onWindowMoved(const WindowEvent*);
virtual bool onWindowResize(const WindowEvent*);
public:
void init();
void shutdown();
void newFrame();
void renderPlatformWindows();
//template<typename T>
inline int define(void** data, int(*func)(Overlay&, void**))
{ return func(*this, data); }
void render();
bool isMinimized();
//private:
ImDrawData* main_draw_data = nullptr;
bool mainMinimized;
};
/////////////////////////////////////////////
static ImGuiKey ImGui_ImplSDL2_KeyEventToImGuiKey(SDL_Keycode keycode, SDL_Scancode scancode)
{
IM_UNUSED(scancode);
switch (keycode)
{
case SDLK_TAB: return ImGuiKey_Tab;
case SDLK_LEFT: return ImGuiKey_LeftArrow;
case SDLK_RIGHT: return ImGuiKey_RightArrow;
case SDLK_UP: return ImGuiKey_UpArrow;
case SDLK_DOWN: return ImGuiKey_DownArrow;
case SDLK_PAGEUP: return ImGuiKey_PageUp;
case SDLK_PAGEDOWN: return ImGuiKey_PageDown;
case SDLK_HOME: return ImGuiKey_Home;
case SDLK_END: return ImGuiKey_End;
case SDLK_INSERT: return ImGuiKey_Insert;
case SDLK_DELETE: return ImGuiKey_Delete;
case SDLK_BACKSPACE: return ImGuiKey_Backspace;
case SDLK_SPACE: return ImGuiKey_Space;
case SDLK_RETURN: return ImGuiKey_Enter;
case SDLK_ESCAPE: return ImGuiKey_Escape;
case SDLK_QUOTE: return ImGuiKey_Apostrophe;
case SDLK_COMMA: return ImGuiKey_Comma;
case SDLK_MINUS: return ImGuiKey_Minus;
case SDLK_PERIOD: return ImGuiKey_Period;
case SDLK_SLASH: return ImGuiKey_Slash;
case SDLK_SEMICOLON: return ImGuiKey_Semicolon;
case SDLK_EQUALS: return ImGuiKey_Equal;
case SDLK_LEFTBRACKET: return ImGuiKey_LeftBracket;
case SDLK_BACKSLASH: return ImGuiKey_Backslash;
case SDLK_RIGHTBRACKET: return ImGuiKey_RightBracket;
case SDLK_BACKQUOTE: return ImGuiKey_GraveAccent;
case SDLK_CAPSLOCK: return ImGuiKey_CapsLock;
case SDLK_SCROLLLOCK: return ImGuiKey_ScrollLock;
case SDLK_NUMLOCKCLEAR: return ImGuiKey_NumLock;
case SDLK_PRINTSCREEN: return ImGuiKey_PrintScreen;
case SDLK_PAUSE: return ImGuiKey_Pause;
case SDLK_KP_0: return ImGuiKey_Keypad0;
case SDLK_KP_1: return ImGuiKey_Keypad1;
case SDLK_KP_2: return ImGuiKey_Keypad2;
case SDLK_KP_3: return ImGuiKey_Keypad3;
case SDLK_KP_4: return ImGuiKey_Keypad4;
case SDLK_KP_5: return ImGuiKey_Keypad5;
case SDLK_KP_6: return ImGuiKey_Keypad6;
case SDLK_KP_7: return ImGuiKey_Keypad7;
case SDLK_KP_8: return ImGuiKey_Keypad8;
case SDLK_KP_9: return ImGuiKey_Keypad9;
case SDLK_KP_PERIOD: return ImGuiKey_KeypadDecimal;
case SDLK_KP_DIVIDE: return ImGuiKey_KeypadDivide;
case SDLK_KP_MULTIPLY: return ImGuiKey_KeypadMultiply;
case SDLK_KP_MINUS: return ImGuiKey_KeypadSubtract;
case SDLK_KP_PLUS: return ImGuiKey_KeypadAdd;
case SDLK_KP_ENTER: return ImGuiKey_KeypadEnter;
case SDLK_KP_EQUALS: return ImGuiKey_KeypadEqual;
case SDLK_LCTRL: return ImGuiKey_LeftCtrl;
case SDLK_LSHIFT: return ImGuiKey_LeftShift;
case SDLK_LALT: return ImGuiKey_LeftAlt;
case SDLK_LGUI: return ImGuiKey_LeftSuper;
case SDLK_RCTRL: return ImGuiKey_RightCtrl;
case SDLK_RSHIFT: return ImGuiKey_RightShift;
case SDLK_RALT: return ImGuiKey_RightAlt;
case SDLK_RGUI: return ImGuiKey_RightSuper;
case SDLK_APPLICATION: return ImGuiKey_Menu;
case SDLK_0: return ImGuiKey_0;
case SDLK_1: return ImGuiKey_1;
case SDLK_2: return ImGuiKey_2;
case SDLK_3: return ImGuiKey_3;
case SDLK_4: return ImGuiKey_4;
case SDLK_5: return ImGuiKey_5;
case SDLK_6: return ImGuiKey_6;
case SDLK_7: return ImGuiKey_7;
case SDLK_8: return ImGuiKey_8;
case SDLK_9: return ImGuiKey_9;
case SDLK_a: return ImGuiKey_A;
case SDLK_b: return ImGuiKey_B;
case SDLK_c: return ImGuiKey_C;
case SDLK_d: return ImGuiKey_D;
case SDLK_e: return ImGuiKey_E;
case SDLK_f: return ImGuiKey_F;
case SDLK_g: return ImGuiKey_G;
case SDLK_h: return ImGuiKey_H;
case SDLK_i: return ImGuiKey_I;
case SDLK_j: return ImGuiKey_J;
case SDLK_k: return ImGuiKey_K;
case SDLK_l: return ImGuiKey_L;
case SDLK_m: return ImGuiKey_M;
case SDLK_n: return ImGuiKey_N;
case SDLK_o: return ImGuiKey_O;
case SDLK_p: return ImGuiKey_P;
case SDLK_q: return ImGuiKey_Q;
case SDLK_r: return ImGuiKey_R;
case SDLK_s: return ImGuiKey_S;
case SDLK_t: return ImGuiKey_T;
case SDLK_u: return ImGuiKey_U;
case SDLK_v: return ImGuiKey_V;
case SDLK_w: return ImGuiKey_W;
case SDLK_x: return ImGuiKey_X;
case SDLK_y: return ImGuiKey_Y;
case SDLK_z: return ImGuiKey_Z;
case SDLK_F1: return ImGuiKey_F1;
case SDLK_F2: return ImGuiKey_F2;
case SDLK_F3: return ImGuiKey_F3;
case SDLK_F4: return ImGuiKey_F4;
case SDLK_F5: return ImGuiKey_F5;
case SDLK_F6: return ImGuiKey_F6;
case SDLK_F7: return ImGuiKey_F7;
case SDLK_F8: return ImGuiKey_F8;
case SDLK_F9: return ImGuiKey_F9;
case SDLK_F10: return ImGuiKey_F10;
case SDLK_F11: return ImGuiKey_F11;
case SDLK_F12: return ImGuiKey_F12;
case SDLK_F13: return ImGuiKey_F13;
case SDLK_F14: return ImGuiKey_F14;
case SDLK_F15: return ImGuiKey_F15;
case SDLK_F16: return ImGuiKey_F16;
case SDLK_F17: return ImGuiKey_F17;
case SDLK_F18: return ImGuiKey_F18;
case SDLK_F19: return ImGuiKey_F19;
case SDLK_F20: return ImGuiKey_F20;
case SDLK_F21: return ImGuiKey_F21;
case SDLK_F22: return ImGuiKey_F22;
case SDLK_F23: return ImGuiKey_F23;
case SDLK_F24: return ImGuiKey_F24;
case SDLK_AC_BACK: return ImGuiKey_AppBack;
case SDLK_AC_FORWARD: return ImGuiKey_AppForward;
default: break;
}
return ImGuiKey_None;
}
static void ImGui_ImplSDL2_UpdateKeyModifiers(SDL_Keymod sdl_key_mods)
{
ImGuiIO& io = ImGui::GetIO();
io.AddKeyEvent(ImGuiMod_Ctrl, (sdl_key_mods & KMOD_CTRL) != 0);
io.AddKeyEvent(ImGuiMod_Shift, (sdl_key_mods & KMOD_SHIFT) != 0);
io.AddKeyEvent(ImGuiMod_Alt, (sdl_key_mods & KMOD_ALT) != 0);
io.AddKeyEvent(ImGuiMod_Super, (sdl_key_mods & KMOD_GUI) != 0);
}