Begin Repo
This commit is contained in:
58
.vscode/settings.json
vendored
Normal file
58
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,58 @@
|
||||
{
|
||||
"C_Cpp.errorSquiggles": "disabled",
|
||||
"files.associations": {
|
||||
"array": "cpp",
|
||||
"atomic": "cpp",
|
||||
"bit": "cpp",
|
||||
"*.tcc": "cpp",
|
||||
"cctype": "cpp",
|
||||
"chrono": "cpp",
|
||||
"clocale": "cpp",
|
||||
"cmath": "cpp",
|
||||
"compare": "cpp",
|
||||
"concepts": "cpp",
|
||||
"cstdarg": "cpp",
|
||||
"cstddef": "cpp",
|
||||
"cstdint": "cpp",
|
||||
"cstdio": "cpp",
|
||||
"cstdlib": "cpp",
|
||||
"cstring": "cpp",
|
||||
"ctime": "cpp",
|
||||
"cwchar": "cpp",
|
||||
"cwctype": "cpp",
|
||||
"deque": "cpp",
|
||||
"set": "cpp",
|
||||
"string": "cpp",
|
||||
"unordered_map": "cpp",
|
||||
"vector": "cpp",
|
||||
"exception": "cpp",
|
||||
"algorithm": "cpp",
|
||||
"functional": "cpp",
|
||||
"iterator": "cpp",
|
||||
"memory": "cpp",
|
||||
"memory_resource": "cpp",
|
||||
"numeric": "cpp",
|
||||
"optional": "cpp",
|
||||
"random": "cpp",
|
||||
"ratio": "cpp",
|
||||
"string_view": "cpp",
|
||||
"system_error": "cpp",
|
||||
"tuple": "cpp",
|
||||
"type_traits": "cpp",
|
||||
"utility": "cpp",
|
||||
"fstream": "cpp",
|
||||
"initializer_list": "cpp",
|
||||
"iosfwd": "cpp",
|
||||
"iostream": "cpp",
|
||||
"istream": "cpp",
|
||||
"limits": "cpp",
|
||||
"new": "cpp",
|
||||
"numbers": "cpp",
|
||||
"ostream": "cpp",
|
||||
"sstream": "cpp",
|
||||
"stdexcept": "cpp",
|
||||
"streambuf": "cpp",
|
||||
"cinttypes": "cpp",
|
||||
"typeinfo": "cpp"
|
||||
}
|
||||
}
|
||||
16053
assets/viking_room.obj
Normal file
16053
assets/viking_room.obj
Normal file
File diff suppressed because it is too large
Load Diff
BIN
assets/viking_room.png
Normal file
BIN
assets/viking_room.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 940 KiB |
9
events/event.cpp
Normal file
9
events/event.cpp
Normal file
@@ -0,0 +1,9 @@
|
||||
#include "event.h"
|
||||
|
||||
Event::Event() {
|
||||
|
||||
}
|
||||
|
||||
Event::~Event()
|
||||
{
|
||||
}
|
||||
20
events/event.h
Normal file
20
events/event.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include "terminal.h"
|
||||
|
||||
enum class EventType {
|
||||
None = 0,
|
||||
WindowClose, WindowResize, WindowFocus, WindowLostFocus, WindowMoved,
|
||||
KeyPressed, KeyReleased, KeyTyped,
|
||||
MouseButtonPressed, MouseButtonReleased, MouseMoved, MouseScrolled
|
||||
};
|
||||
|
||||
class Event {
|
||||
|
||||
public:
|
||||
Event();
|
||||
virtual ~Event();
|
||||
|
||||
virtual EventType getType() const = 0;
|
||||
|
||||
};
|
||||
39
events/eventHandler.cpp
Normal file
39
events/eventHandler.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
#include "eventHandler.h"
|
||||
#include "application.h"
|
||||
|
||||
EventHandler::EventHandler() {
|
||||
|
||||
}
|
||||
|
||||
bool EventHandler::handleEvent(const Event* e, Layer* layer) {
|
||||
//std::cout << (int)e->getType() << '\n';
|
||||
switch(e->getType()) {
|
||||
case EventType::KeyPressed:
|
||||
return layer->onKeyPressed(static_cast<const KeyEvent*>(e));
|
||||
case EventType::KeyReleased:
|
||||
return layer->onKeyReleased(static_cast<const KeyEvent*>(e));
|
||||
case EventType::KeyTyped:
|
||||
return layer->onKeyTyped(static_cast<const KeyTypedEvent*>(e));
|
||||
case EventType::MouseButtonPressed:
|
||||
return layer->onMouseButtonPressed(static_cast<const MouseButtonEvent*>(e));
|
||||
case EventType::MouseButtonReleased:
|
||||
return layer->onMouseButtonReleased(static_cast<const MouseButtonEvent*>(e));
|
||||
case EventType::MouseScrolled:
|
||||
return layer->onMouseScrolled(static_cast<const MouseScrolledEvent*>(e));
|
||||
case EventType::MouseMoved:
|
||||
return layer->onMouseMoved(static_cast<const MouseMovedEvent*>(e));
|
||||
case EventType::WindowClose:
|
||||
return layer->onWindowClose(static_cast<const WindowEvent*>(e));
|
||||
case EventType::WindowFocus:
|
||||
return layer->onWindowFocus(static_cast<const WindowEvent*>(e));
|
||||
case EventType::WindowLostFocus:
|
||||
return layer->onWindowFocusLost(static_cast<const WindowEvent*>(e));
|
||||
case EventType::WindowMoved:
|
||||
return layer->onWindowMoved(static_cast<const WindowEvent*>(e));
|
||||
case EventType::WindowResize:
|
||||
return layer->onWindowResize(static_cast<const WindowEvent*>(e));
|
||||
//default:
|
||||
|
||||
}
|
||||
return false;
|
||||
}
|
||||
14
events/eventHandler.h
Normal file
14
events/eventHandler.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include "terminal.h"
|
||||
|
||||
#include "event.h"
|
||||
|
||||
|
||||
class EventHandler {
|
||||
|
||||
public:
|
||||
EventHandler();
|
||||
|
||||
bool handleEvent(const Event*, Layer*);
|
||||
};
|
||||
10
events/keyEvent.cpp
Normal file
10
events/keyEvent.cpp
Normal file
@@ -0,0 +1,10 @@
|
||||
#include "keyEvent.h"
|
||||
|
||||
KeyEvent::KeyEvent(int k, int s, int m, int r, bool p) : key(k), scancode(s), mod(m), repeat(r), pressed(p) {
|
||||
|
||||
}
|
||||
|
||||
KeyTypedEvent::KeyTypedEvent(int w, char* t) : windowID(w), text(t)
|
||||
{
|
||||
|
||||
}
|
||||
28
events/keyEvent.h
Normal file
28
events/keyEvent.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#include "terminal.h"
|
||||
#include "event.h"
|
||||
|
||||
class KeyEvent : public Event {
|
||||
const int key;
|
||||
const int scancode;
|
||||
const int mod;
|
||||
const int repeat;
|
||||
const bool pressed;
|
||||
public:
|
||||
KeyEvent(int, int, int, int, bool);
|
||||
inline int getKey() const { return key; }
|
||||
inline int getScancode() const { return scancode; }
|
||||
inline int getMod() const { return mod; }
|
||||
inline bool getPressed() const { return pressed; }
|
||||
EventType getType() const { return pressed ? EventType::KeyPressed : EventType::KeyReleased; }
|
||||
};
|
||||
|
||||
class KeyTypedEvent : public Event {
|
||||
const std::string text;
|
||||
const int windowID;
|
||||
|
||||
public:
|
||||
KeyTypedEvent(int, char*);
|
||||
inline int getWindowID() const { return windowID; }
|
||||
inline const char* getText() const { return text.c_str(); }
|
||||
EventType getType() const { return EventType::KeyTyped; }
|
||||
};
|
||||
13
events/mouseEvent.cpp
Normal file
13
events/mouseEvent.cpp
Normal file
@@ -0,0 +1,13 @@
|
||||
#include "mouseEvent.h"
|
||||
|
||||
MouseButtonEvent::MouseButtonEvent(int b, bool p) : button(b), pressed(p) {
|
||||
|
||||
}
|
||||
|
||||
MouseMovedEvent::MouseMovedEvent(int _x, int _y, double _dx, double _dy) : x(_x), y(_y), dx(_dx), dy(_dy) {
|
||||
|
||||
}
|
||||
|
||||
MouseScrolledEvent::MouseScrolledEvent(int _x, int _y) : x(_x), y(_y) {
|
||||
|
||||
}
|
||||
33
events/mouseEvent.h
Normal file
33
events/mouseEvent.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#include "terminal.h"
|
||||
#include "event.h"
|
||||
|
||||
class MouseButtonEvent : public Event {
|
||||
const int button;
|
||||
const bool pressed;
|
||||
public:
|
||||
MouseButtonEvent(int, bool);
|
||||
inline int getButton() const { return button; }
|
||||
inline bool getPressed() const { return pressed; }
|
||||
EventType getType() const { return pressed ? EventType::MouseButtonPressed : EventType::MouseButtonReleased; }
|
||||
};
|
||||
|
||||
class MouseMovedEvent : public Event {
|
||||
const int x, y;
|
||||
const double dx, dy;
|
||||
public:
|
||||
MouseMovedEvent(int, int, double, double);
|
||||
inline int getX() const { return x; }
|
||||
inline int getY() const { return y; }
|
||||
inline double getDeltaX() const { return dx; }
|
||||
inline double getDeltaY() const { return dy; }
|
||||
EventType getType() const { return EventType::MouseMoved; }
|
||||
};
|
||||
|
||||
class MouseScrolledEvent : public Event {
|
||||
const int x, y;
|
||||
public:
|
||||
MouseScrolledEvent(int, int);
|
||||
inline int getX() const { return x; }
|
||||
inline int getY() const { return y; }
|
||||
EventType getType() const { return EventType::MouseScrolled; }
|
||||
};
|
||||
5
events/windowEvent.cpp
Normal file
5
events/windowEvent.cpp
Normal file
@@ -0,0 +1,5 @@
|
||||
#include "windowEvent.h"
|
||||
|
||||
WindowEvent::WindowEvent(int id, int e) : windowID(id), event(e) {
|
||||
|
||||
}
|
||||
27
events/windowEvent.h
Normal file
27
events/windowEvent.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include "terminal.h"
|
||||
#include "event.h"
|
||||
|
||||
|
||||
class WindowEvent : public Event {
|
||||
const int windowID, event;
|
||||
public:
|
||||
WindowEvent(int, int);
|
||||
|
||||
inline int getWindowID() const { return windowID; }
|
||||
|
||||
EventType getType() const {
|
||||
switch(event) {
|
||||
case SDL_WINDOWEVENT_CLOSE:
|
||||
return EventType::WindowClose;
|
||||
case SDL_WINDOWEVENT_RESIZED:
|
||||
return EventType::WindowResize;
|
||||
case SDL_WINDOWEVENT_FOCUS_GAINED:
|
||||
return EventType::WindowFocus;
|
||||
case SDL_WINDOWEVENT_FOCUS_LOST:
|
||||
return EventType::WindowLostFocus;
|
||||
}
|
||||
return EventType::None;
|
||||
}
|
||||
};
|
||||
45
flake.lock
generated
Normal file
45
flake.lock
generated
Normal file
@@ -0,0 +1,45 @@
|
||||
{
|
||||
"nodes": {
|
||||
"imgui": {
|
||||
"flake": false,
|
||||
"locked": {
|
||||
"lastModified": 1714484256,
|
||||
"narHash": "sha256-75OH+6v6w/dH5D/3WFOPAmGpyggjbc++SX7dTu56qf8=",
|
||||
"owner": "ocornut",
|
||||
"repo": "imgui",
|
||||
"rev": "ebb8d781020a77d1d5185009e208068e3bf8753b",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "ocornut",
|
||||
"ref": "docking",
|
||||
"repo": "imgui",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1714635257,
|
||||
"narHash": "sha256-4cPymbty65RvF1DWQfc+Bc8B233A1BWxJnNULJKQ1EY=",
|
||||
"owner": "nixos",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "63c3a29ca82437c87573e4c6919b09a24ea61b0f",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "nixos",
|
||||
"ref": "nixos-unstable",
|
||||
"repo": "nixpkgs",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"imgui": "imgui",
|
||||
"nixpkgs": "nixpkgs"
|
||||
}
|
||||
}
|
||||
},
|
||||
"root": "root",
|
||||
"version": 7
|
||||
}
|
||||
134
flake.nix
Normal file
134
flake.nix
Normal file
@@ -0,0 +1,134 @@
|
||||
{
|
||||
description = "Build Project Terminal";
|
||||
|
||||
inputs = {
|
||||
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
|
||||
imgui.url = "github:ocornut/imgui?ref=docking";
|
||||
imgui.flake = false;
|
||||
};
|
||||
|
||||
outputs = { self, nixpkgs, imgui }@inputs:
|
||||
let
|
||||
|
||||
system = "x86_64-linux";
|
||||
|
||||
pkgs = nixpkgs.legacyPackages.${system};
|
||||
|
||||
renderer = "vulkan";
|
||||
|
||||
window = "sdl2";
|
||||
|
||||
in {
|
||||
|
||||
packages.${system}.default = pkgs.stdenv.mkDerivation {
|
||||
|
||||
name = "Terminal";
|
||||
|
||||
inherit system;
|
||||
|
||||
src = ./.;
|
||||
|
||||
imgui = inputs.imgui;
|
||||
|
||||
|
||||
|
||||
buildInputs = with pkgs; if renderer == "vulkan" && window == "sdl2" then [
|
||||
tinyobjloader
|
||||
stb
|
||||
glm
|
||||
shaderc
|
||||
SDL2
|
||||
vulkan-headers
|
||||
vulkan-loader
|
||||
] else if renderer == "vulkan" && window == "glfw" then [
|
||||
stb
|
||||
glm
|
||||
shaderc
|
||||
glfw
|
||||
vulkan-headers
|
||||
vulkan-loader
|
||||
] else if renderer == "opengl3" && window == "sdl2" then [
|
||||
stb
|
||||
glm
|
||||
SDL2
|
||||
|
||||
] else if renderer == "opengl3" && window == "glfw" then [
|
||||
stb
|
||||
glm
|
||||
glfw
|
||||
] else [];
|
||||
|
||||
buildPhase = if renderer == "vulkan" && window == "sdl2" then
|
||||
''
|
||||
glslc shaders/*.vert -o vert.spv
|
||||
glslc shaders/*.frag -o frag.spv
|
||||
g++ src/*.cpp renderer/vulkanRenderer.cpp window/sdl2Window.cpp layers/*.cpp events/*.cpp \
|
||||
-I src -I renderer -I window -I layers -I events \
|
||||
-I $imgui -I $imgui/backends -I ${pkgs.stb}/include/stb $(sdl2-config --cflags) \
|
||||
$imgui/*.cpp $imgui/backends/*sdl2.cpp $imgui/backends/*vulkan.cpp \
|
||||
-lSDL2 -lvulkan -o $name -DIMGUI_ENABLE -DSHDR_PATH=$out/bin
|
||||
''
|
||||
else if renderer == "vulkan" && window == "glfw" then
|
||||
''
|
||||
glslc shaders/*.vert -o vert.spv
|
||||
glslc shaders/*.frag -o frag.spv
|
||||
g++ src/*.cpp renderer/vulkanRenderer.cpp window/sdl2Window.cpp layers/*.cpp events/*.cpp \
|
||||
-I src -I renderer -I window -I layers -I events \
|
||||
-I $imgui -I $imgui/backends -I ${pkgs.stb}/include/stb $(sdl2-config --cflags) \
|
||||
$imgui/*.cpp $imgui/backends/*sdl2.cpp $imgui/backends/*vulkan.cpp \
|
||||
-lSDL2 -lvulkan -o $name -DIMGUI_ENABLE -DSHDR_PATH=$out/bin
|
||||
''
|
||||
else if renderer == "opengl3" && window == "sdl2" then
|
||||
''
|
||||
glslc shaders/*.vert -o vert.spv
|
||||
glslc shaders/*.frag -o frag.spv
|
||||
g++ src/*.cpp renderer/vulkanRenderer.cpp window/sdl2Window.cpp layers/*.cpp events/*.cpp \
|
||||
-I src -I renderer -I window -I layers -I events \
|
||||
-I $imgui -I $imgui/backends -I ${pkgs.stb}/include/stb $(sdl2-config --cflags) \
|
||||
$imgui/*.cpp $imgui/backends/*sdl2.cpp $imgui/backends/*vulkan.cpp \
|
||||
-lSDL2 -lvulkan -o $name -DIMGUI_ENABLE -DSHDR_PATH=$out/bin
|
||||
''
|
||||
else if renderer == "opengl3" && window == "glfw" then
|
||||
''
|
||||
glslc shaders/*.vert -o vert.spv
|
||||
glslc shaders/*.frag -o frag.spv
|
||||
g++ src/*.cpp renderer/vulkanRenderer.cpp window/sdl2Window.cpp layers/*.cpp events/*.cpp \
|
||||
-I src -I renderer -I window -I layers -I events \
|
||||
-I $imgui -I $imgui/backends -I ${pkgs.stb}/include/stb $(sdl2-config --cflags) \
|
||||
$imgui/*.cpp $imgui/backends/*sdl2.cpp $imgui/backends/*vulkan.cpp \
|
||||
-lSDL2 -lvulkan -o $name -DIMGUI_ENABLE -DSHDR_PATH=$out/bin
|
||||
'' else '''';
|
||||
|
||||
installPhase = ''
|
||||
mkdir -p $out/bin
|
||||
cp *.spv $out/bin
|
||||
cp $name $out/bin
|
||||
'';
|
||||
|
||||
};
|
||||
|
||||
shaders = pkgs.stdenv.mkDerivation {
|
||||
|
||||
name = "Terminal";
|
||||
|
||||
inherit system;
|
||||
|
||||
src = ./.;
|
||||
|
||||
buildInputs = with pkgs; [
|
||||
shaderc
|
||||
];
|
||||
|
||||
buildPhase = ''
|
||||
glslc shaders/*.vert -o vert.spv
|
||||
glslc shaders/*.frag -o frag.spv
|
||||
'';
|
||||
|
||||
installPhase = ''
|
||||
mkdir -p $out/bin
|
||||
cp *.spv $out/bin
|
||||
'';
|
||||
|
||||
};
|
||||
};
|
||||
}
|
||||
36
imgui.ini
Normal file
36
imgui.ini
Normal file
@@ -0,0 +1,36 @@
|
||||
[Window][Debug##Default]
|
||||
Pos=60,60
|
||||
Size=400,400
|
||||
Collapsed=0
|
||||
|
||||
[Window][Dear ImGui Demo]
|
||||
Pos=62,29
|
||||
Size=464,257
|
||||
Collapsed=0
|
||||
DockId=0x00000001,0
|
||||
|
||||
[Window][Example: Custom rendering]
|
||||
Pos=13,20
|
||||
Size=806,486
|
||||
Collapsed=0
|
||||
DockId=0x00000001,1
|
||||
|
||||
[Window][User Window]
|
||||
Pos=48,325
|
||||
Size=73,132
|
||||
Collapsed=0
|
||||
|
||||
[Window][Example: Documents]
|
||||
ViewportPos=380,100
|
||||
ViewportId=0x1FD64BEB
|
||||
Size=621,714
|
||||
Collapsed=0
|
||||
|
||||
[Window][Main Editor]
|
||||
Pos=120,307
|
||||
Size=363,292
|
||||
Collapsed=0
|
||||
|
||||
[Docking][Data]
|
||||
DockNode ID=0x00000001 Pos=382,69 Size=464,257 Selected=0xAAB01F94
|
||||
|
||||
92
layers/baseLayer.cpp
Normal file
92
layers/baseLayer.cpp
Normal 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
32
layers/baseLayer.h
Normal 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
8
layers/layer.cpp
Normal file
@@ -0,0 +1,8 @@
|
||||
#include "layer.h"
|
||||
|
||||
#include "application.h"
|
||||
|
||||
|
||||
Layer::Layer() {}
|
||||
|
||||
Layer::~Layer() {}
|
||||
35
layers/layer.h
Normal file
35
layers/layer.h
Normal 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
77
layers/layerstack.cpp
Normal 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
32
layers/layerstack.h
Normal 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
271
layers/overlay.cpp
Normal 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
209
layers/overlay.h
Normal 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);
|
||||
}
|
||||
0
renderer/openglRenderer.cpp
Normal file
0
renderer/openglRenderer.cpp
Normal file
0
renderer/openglRenderer.h
Normal file
0
renderer/openglRenderer.h
Normal file
0
renderer/renderer.h
Normal file
0
renderer/renderer.h
Normal file
1469
renderer/vulkanRenderer.cpp
Normal file
1469
renderer/vulkanRenderer.cpp
Normal file
File diff suppressed because it is too large
Load Diff
296
renderer/vulkanRenderer.h
Normal file
296
renderer/vulkanRenderer.h
Normal file
@@ -0,0 +1,296 @@
|
||||
#pragma once
|
||||
|
||||
#include "terminal.h"
|
||||
|
||||
#include "sdl2Window.h"
|
||||
|
||||
const std::string MODEL_PATH = "/home/nathan/Projects/terminal/assets/viking_room.obj";
|
||||
const std::string TEXTURE_PATH = "/home/nathan/Pictures/Mountain.png";
|
||||
|
||||
struct QueueFamilyIndices {
|
||||
std::optional<uint32_t> graphicsFamily;
|
||||
std::optional<uint32_t> presentFamily;
|
||||
|
||||
bool isComplete() {
|
||||
return graphicsFamily.has_value() && presentFamily.has_value();
|
||||
}
|
||||
};
|
||||
|
||||
struct SwapChainSupportDetails {
|
||||
VkSurfaceCapabilitiesKHR capabilities;
|
||||
std::vector<VkSurfaceFormatKHR> formats;
|
||||
std::vector<VkPresentModeKHR> presentModes;
|
||||
};
|
||||
|
||||
static std::vector<char> readFile(const std::string& filename) {
|
||||
std::ifstream file(filename, std::ios::ate | std::ios::binary);
|
||||
|
||||
if (!file.is_open()) {
|
||||
throw std::runtime_error("failed to open file!");
|
||||
}
|
||||
|
||||
size_t fileSize = (size_t) file.tellg();
|
||||
std::vector<char> buffer(fileSize);
|
||||
|
||||
file.seekg(0);
|
||||
file.read(buffer.data(), fileSize);
|
||||
|
||||
file.close();
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
struct Vertex {
|
||||
glm::vec3 pos;
|
||||
glm::vec3 color;
|
||||
glm::vec2 texCoord;
|
||||
|
||||
static VkVertexInputBindingDescription getBindingDescription() {
|
||||
VkVertexInputBindingDescription bindingDescription{};
|
||||
bindingDescription.binding = 0;
|
||||
bindingDescription.stride = sizeof(Vertex);
|
||||
bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
|
||||
|
||||
return bindingDescription;
|
||||
}
|
||||
|
||||
static std::array<VkVertexInputAttributeDescription, 3> getAttributeDescriptions() {
|
||||
std::array<VkVertexInputAttributeDescription, 3> attributeDescriptions{};
|
||||
|
||||
attributeDescriptions[0].binding = 0;
|
||||
attributeDescriptions[0].location = 0;
|
||||
attributeDescriptions[0].format = VK_FORMAT_R32G32B32_SFLOAT;
|
||||
attributeDescriptions[0].offset = offsetof(Vertex, pos);
|
||||
|
||||
attributeDescriptions[1].binding = 0;
|
||||
attributeDescriptions[1].location = 1;
|
||||
attributeDescriptions[1].format = VK_FORMAT_R32G32B32_SFLOAT;
|
||||
attributeDescriptions[1].offset = offsetof(Vertex, color);
|
||||
|
||||
attributeDescriptions[2].binding = 0;
|
||||
attributeDescriptions[2].location = 2;
|
||||
attributeDescriptions[2].format = VK_FORMAT_R32G32_SFLOAT;
|
||||
attributeDescriptions[2].offset = offsetof(Vertex, texCoord);
|
||||
|
||||
return attributeDescriptions;
|
||||
}
|
||||
};
|
||||
|
||||
struct UniformBufferObject {
|
||||
alignas(16) glm::mat4 model;
|
||||
alignas(16) glm::mat4 view;
|
||||
alignas(16) glm::mat4 proj;
|
||||
};
|
||||
|
||||
const std::vector<Vertex> vertices = {
|
||||
{{-0.5f, -0.5f, 0.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}},
|
||||
{{0.5f, -0.5f, 0.0f}, {0.0f, 1.0f, 0.0f}, {1.0f, 0.0f}},
|
||||
{{0.5f, 0.5f, 0.0f}, {0.0f, 0.0f, 1.0f}, {1.0f, 1.0f}},
|
||||
{{-0.5f, 0.5f, 0.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}},
|
||||
|
||||
{{-0.5f, -0.5f, -0.5f}, {1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}},
|
||||
{{0.5f, -0.5f, -0.5f}, {0.0f, 1.0f, 0.0f}, {1.0f, 0.0f}},
|
||||
{{0.5f, 0.5f, -0.5f}, {0.0f, 0.0f, 1.0f}, {1.0f, 1.0f}},
|
||||
{{-0.5f, 0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}}
|
||||
};
|
||||
|
||||
const std::vector<uint32_t> indices = {
|
||||
0, 1, 2, 2, 3, 0,
|
||||
4, 5, 6, 6, 7, 4
|
||||
};
|
||||
|
||||
class Renderer {
|
||||
|
||||
public:
|
||||
|
||||
Renderer();
|
||||
|
||||
~Renderer();
|
||||
|
||||
inline VkInstance getInstance() { return instance; }
|
||||
inline VkPhysicalDevice getPhysicalDevice() { return physicalDevice; }
|
||||
inline VkDevice getDevice() { return device; }
|
||||
uint32_t getQueueFamily();
|
||||
inline VkQueue getGraphicsQueue() { return graphicsQueue; }
|
||||
inline VkQueue getPresentQueue() { return presentQueue; }
|
||||
inline VkSurfaceKHR getSurface() { return surface; }
|
||||
inline VkRenderPass getRenderPass() { return renderPass; }
|
||||
inline VkDescriptorPool getDescriptorPool() { return descriptorPool; }
|
||||
inline VkCommandBuffer getCommandBuffer(int frame) { return commandBuffers[frame]; }
|
||||
inline VkPipeline getPipeline() { return graphicsPipeline; }
|
||||
inline int getNumImages() { return MAX_FRAMES_IN_FLIGHT; }
|
||||
inline int getFrame() { return currentFrame; }
|
||||
inline VkSampleCountFlagBits getMSAASamples() { return msaaSamples; }
|
||||
|
||||
private:
|
||||
const int MAX_FRAMES_IN_FLIGHT = 2;
|
||||
|
||||
VkInstance instance;
|
||||
VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
|
||||
VkDevice device;
|
||||
VkQueue graphicsQueue;
|
||||
VkQueue presentQueue;
|
||||
VkSurfaceKHR surface;
|
||||
VkSwapchainKHR swapChain;
|
||||
std::vector<VkImage> swapChainImages;
|
||||
VkFormat swapChainImageFormat;
|
||||
VkExtent2D swapChainExtent;
|
||||
VkRenderPass renderPass;
|
||||
VkDescriptorPool descriptorPool;
|
||||
std::vector<VkDescriptorSet> descriptorSets;
|
||||
VkDescriptorSetLayout descriptorSetLayout;
|
||||
VkPipelineLayout pipelineLayout;
|
||||
VkPipeline graphicsPipeline;
|
||||
std::vector<VkFramebuffer> swapChainFramebuffers;
|
||||
std::vector<VkImageView> swapChainImageViews;
|
||||
VkCommandPool commandPool;
|
||||
std::vector<VkCommandBuffer> commandBuffers;
|
||||
std::vector<VkSemaphore> imageAvailableSemaphores;
|
||||
std::vector<VkSemaphore> renderFinishedSemaphores;
|
||||
std::vector<VkFence> inFlightFences;
|
||||
uint32_t currentFrame = 0;
|
||||
|
||||
VkBuffer vertexBuffer;
|
||||
VkDeviceMemory vertexBufferMemory;
|
||||
VkBuffer indexBuffer;
|
||||
VkDeviceMemory indexBufferMemory;
|
||||
std::vector<VkBuffer> uniformBuffers;
|
||||
std::vector<VkDeviceMemory> uniformBuffersMemory;
|
||||
std::vector<void*> uniformBuffersMapped;
|
||||
VkImage textureImage;
|
||||
VkDeviceMemory textureImageMemory;
|
||||
VkImageView textureImageView;
|
||||
VkSampler textureSampler;
|
||||
VkImage depthImage;
|
||||
VkDeviceMemory depthImageMemory;
|
||||
VkImageView depthImageView;
|
||||
|
||||
|
||||
VkSampleCountFlagBits msaaSamples = VK_SAMPLE_COUNT_1_BIT;
|
||||
VkImage colorImage;
|
||||
VkDeviceMemory colorImageMemory;
|
||||
VkImageView colorImageView;
|
||||
|
||||
|
||||
|
||||
|
||||
VkResult err;
|
||||
|
||||
bool enableValidationLayers = false;
|
||||
|
||||
const std::vector<const char*> deviceExtensions = {
|
||||
VK_KHR_SWAPCHAIN_EXTENSION_NAME
|
||||
};
|
||||
|
||||
|
||||
public:
|
||||
|
||||
void init();
|
||||
|
||||
void shutdown();
|
||||
|
||||
void draw();
|
||||
|
||||
void createInstance();
|
||||
|
||||
void pickPhysicalDevice();
|
||||
|
||||
void createLogicalDevice();
|
||||
|
||||
void createSurface();
|
||||
|
||||
void createSwapChain();
|
||||
|
||||
void createImageViews();
|
||||
|
||||
void createDescriptorPool();
|
||||
|
||||
void createDescriptorSets();
|
||||
|
||||
void createDescriptorSetLayout();
|
||||
|
||||
void createGraphicsPipeline();
|
||||
|
||||
void createRenderPass();
|
||||
|
||||
void createFramebuffers();
|
||||
|
||||
void createCommandPool();
|
||||
|
||||
void createVertexBuffer();
|
||||
|
||||
void createIndexBuffer();
|
||||
|
||||
void createUniformBuffers();
|
||||
|
||||
void updateUniformBuffer(uint32_t);
|
||||
|
||||
void createBuffer(VkDeviceSize, VkBufferUsageFlags, VkMemoryPropertyFlags, VkBuffer&, VkDeviceMemory&);
|
||||
|
||||
void createCommandBuffer();
|
||||
|
||||
void recordCommandBuffer(VkCommandBuffer, uint32_t);
|
||||
|
||||
void createSyncObjects();
|
||||
|
||||
|
||||
|
||||
void loadModel();
|
||||
|
||||
bool hasStencilComponent(VkFormat);
|
||||
|
||||
VkFormat findDepthFormat();
|
||||
|
||||
VkFormat findSupportedFormat(const std::vector<VkFormat>&, VkImageTiling, VkFormatFeatureFlags);
|
||||
|
||||
void createDepthResources();
|
||||
|
||||
VkImageView createImageView(VkImage, VkFormat, VkImageAspectFlags);
|
||||
|
||||
void copyBufferToImage(VkBuffer, VkImage, uint32_t, uint32_t);
|
||||
|
||||
void createTextureSampler();
|
||||
|
||||
void createTextureImageView();
|
||||
|
||||
void transitionImageLayout(VkImage, VkFormat, VkImageLayout, VkImageLayout);
|
||||
|
||||
VkCommandBuffer beginSingleTimeCommands();
|
||||
|
||||
void endSingleTimeCommands(VkCommandBuffer);
|
||||
|
||||
void createImage(uint32_t, uint32_t, VkFormat, VkImageTiling, VkImageUsageFlags, VkMemoryPropertyFlags, VkImage&, VkDeviceMemory&);
|
||||
|
||||
void createTextureImage();
|
||||
|
||||
VkSampleCountFlagBits getMaxUsableSampleCount();
|
||||
|
||||
void copyBuffer(VkBuffer, VkBuffer, VkDeviceSize);
|
||||
|
||||
uint32_t findMemoryType(uint32_t, VkMemoryPropertyFlags);
|
||||
|
||||
void recreateSwapChain();
|
||||
|
||||
void cleanupSwapChain();
|
||||
|
||||
VkShaderModule createShaderModule(const std::vector<char>&);
|
||||
|
||||
VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR&);
|
||||
|
||||
VkPresentModeKHR chooseSwapPresentMode(const std::vector<VkPresentModeKHR>&);
|
||||
|
||||
VkSurfaceFormatKHR chooseSwapSurfaceFormat(const std::vector<VkSurfaceFormatKHR>&);
|
||||
|
||||
SwapChainSupportDetails querySwapChainSupport(VkPhysicalDevice);
|
||||
|
||||
bool checkDeviceExtensionSupport(VkPhysicalDevice);
|
||||
|
||||
QueueFamilyIndices findQueueFamilies(VkPhysicalDevice);
|
||||
|
||||
bool isDeviceSuitable(VkPhysicalDevice);
|
||||
|
||||
bool isExtensionAvailable(const std::vector<VkExtensionProperties>&, const char*);
|
||||
|
||||
void check_vk_result();
|
||||
|
||||
};
|
||||
14
shaders/s.frag
Normal file
14
shaders/s.frag
Normal file
@@ -0,0 +1,14 @@
|
||||
#version 450
|
||||
|
||||
layout(binding = 1) uniform sampler2D texSampler;
|
||||
|
||||
layout(location = 0) in vec3 fragColor;
|
||||
layout(location = 1) in vec2 fragTexCoord;
|
||||
|
||||
layout(location = 0) out vec4 outColor;
|
||||
|
||||
|
||||
void main() {
|
||||
outColor = texture(texSampler, fragTexCoord);
|
||||
//outColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);
|
||||
}
|
||||
21
shaders/s.vert
Normal file
21
shaders/s.vert
Normal file
@@ -0,0 +1,21 @@
|
||||
#version 450
|
||||
|
||||
|
||||
layout(binding = 0) uniform UniformBufferObject {
|
||||
mat4 model;
|
||||
mat4 view;
|
||||
mat4 proj;
|
||||
} ubo;
|
||||
|
||||
layout(location = 0) in vec3 inPosition;
|
||||
layout(location = 1) in vec3 inColor;
|
||||
layout(location = 2) in vec2 inTexCoord;
|
||||
|
||||
layout(location = 0) out vec3 fragColor;
|
||||
layout(location = 1) out vec2 fragTexCoord;
|
||||
|
||||
void main() {
|
||||
gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 1.0);
|
||||
fragColor = inColor;
|
||||
fragTexCoord = inTexCoord;
|
||||
}
|
||||
47
src/application.cpp
Normal file
47
src/application.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#include "application.h"
|
||||
|
||||
App* App::instance = nullptr;
|
||||
|
||||
App::App() {
|
||||
|
||||
if(instance != nullptr) {
|
||||
std::cout << "App already exists\nThere can only be one!\n";
|
||||
std::abort();
|
||||
}
|
||||
|
||||
std::cout << "Initializing...\n";
|
||||
|
||||
instance = this;
|
||||
|
||||
window.init("test", 640, 640);
|
||||
|
||||
renderer.init();
|
||||
|
||||
}
|
||||
|
||||
App::~App() {
|
||||
|
||||
lStack.shutdown();
|
||||
|
||||
renderer.shutdown();
|
||||
|
||||
window.shutdown();
|
||||
|
||||
std::cout << "\nExiting...";
|
||||
}
|
||||
|
||||
void App::run() {
|
||||
std::cout << "\nTesting...\n";
|
||||
|
||||
lStack.pushLayer(new BaseLayer());
|
||||
lStack.pushOverlay(new Overlay());
|
||||
|
||||
// Main loop
|
||||
while (!done) {
|
||||
|
||||
lStack.propagateEvent(window.pollEvents());
|
||||
|
||||
renderer.draw();
|
||||
}
|
||||
}
|
||||
|
||||
49
src/application.h
Normal file
49
src/application.h
Normal file
@@ -0,0 +1,49 @@
|
||||
#pragma once
|
||||
|
||||
#include "terminal.h"
|
||||
|
||||
#include "sdl2Window.h"
|
||||
#include "vulkanRenderer.h"
|
||||
#include "event.h"
|
||||
#include "eventHandler.h"
|
||||
#include "layer.h"
|
||||
#include "layerstack.h"
|
||||
#include "baseLayer.h"
|
||||
#include "overlay.h"
|
||||
|
||||
|
||||
|
||||
|
||||
class App {
|
||||
|
||||
public:
|
||||
|
||||
App();
|
||||
|
||||
~App();
|
||||
|
||||
void run();
|
||||
|
||||
inline Window& getWindow() { return window; }
|
||||
|
||||
inline Renderer& getRenderer() { return renderer; }
|
||||
|
||||
inline LayerStack& getLayerStack() { return lStack; }
|
||||
|
||||
inline static App& Get() { return *instance; }
|
||||
|
||||
bool done = false;
|
||||
|
||||
bool show = true;
|
||||
|
||||
private:
|
||||
|
||||
static App* instance;
|
||||
|
||||
Window window;
|
||||
|
||||
Renderer renderer;
|
||||
|
||||
LayerStack lStack;
|
||||
|
||||
};
|
||||
9
src/terminal.cpp
Normal file
9
src/terminal.cpp
Normal file
@@ -0,0 +1,9 @@
|
||||
#include "application.h"
|
||||
|
||||
int main() {
|
||||
|
||||
App app;
|
||||
|
||||
app.run();
|
||||
|
||||
}
|
||||
58
src/terminal.h
Normal file
58
src/terminal.h
Normal file
@@ -0,0 +1,58 @@
|
||||
#pragma once
|
||||
|
||||
|
||||
//macros
|
||||
#ifdef SHDR_PATH
|
||||
#define STRINGIZE(x) #x
|
||||
#define STRINGIZE_VALUE_OF(x) STRINGIZE(x)
|
||||
#endif
|
||||
|
||||
|
||||
#include <iostream>
|
||||
#include <stdio.h> // printf, fprintf
|
||||
#include <stdlib.h> // abort
|
||||
|
||||
#include <vector>
|
||||
#include <optional>
|
||||
#include <set>
|
||||
#include <cstdint> // Necessary for uint32_t
|
||||
#include <limits> // Necessary for std::numeric_limits
|
||||
#include <algorithm> // Necessary for std::clamp
|
||||
#include <fstream>
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
|
||||
#define GLM_FORCE_RADIANS
|
||||
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
|
||||
|
||||
#include <vulkan/vulkan.h>
|
||||
|
||||
#include <SDL.h>
|
||||
#include <SDL_video.h>
|
||||
#include <SDL_vulkan.h>
|
||||
|
||||
|
||||
|
||||
#include "imgui.h"
|
||||
#include "imconfig.h"
|
||||
#include "imgui_internal.h"
|
||||
#include "imgui_impl_sdl2.h"
|
||||
#include "imgui_impl_vulkan.h"
|
||||
|
||||
|
||||
|
||||
//Forward declarations
|
||||
class App;
|
||||
class MouseButtonEvent;
|
||||
class MouseMovedEvent;
|
||||
class MouseScrolledEvent;
|
||||
class KeyEvent;
|
||||
class KeyTypedEvent;
|
||||
class WindowEvent;
|
||||
class Layer;
|
||||
|
||||
|
||||
|
||||
87
window/glfwWindow.cpp
Normal file
87
window/glfwWindow.cpp
Normal file
@@ -0,0 +1,87 @@
|
||||
#include "glfwWindow.h"
|
||||
|
||||
|
||||
|
||||
WindowGLFW::WindowGLFW() {
|
||||
|
||||
}
|
||||
|
||||
WindowGLFW::~WindowGLFW() {
|
||||
|
||||
glfwTerminate();
|
||||
}
|
||||
|
||||
void WindowGLFW::init( std::string title, int x, int y) : title(title), x(x), y(y) {
|
||||
|
||||
if(!glfwInit()) {
|
||||
std::cout << "glfwInit failed!\n";
|
||||
error = true;
|
||||
return;
|
||||
}
|
||||
|
||||
window = glfwCreateWindow(x, y, title.c_str(), NULL, NULL);
|
||||
if(!window) {
|
||||
glfwTerminate();
|
||||
std::cout << "glfwCreateWindow failed!";
|
||||
error = true;
|
||||
return;
|
||||
}
|
||||
|
||||
glfwMakeContextCurrent(window);
|
||||
}
|
||||
|
||||
void WindowGLFW::shutdown() {
|
||||
~WindowGLFW();
|
||||
}
|
||||
|
||||
void WindowGLFW::getSize() {
|
||||
|
||||
SDL_GetWindowSize(window, &x, &y);
|
||||
}
|
||||
|
||||
const Event* WindowGLFW::createEvent() {
|
||||
switch(event.type) {
|
||||
case SDL_KEYDOWN:
|
||||
return new KeyEvent(event.key.keysym.sym, event.key.keysym.scancode,
|
||||
event.key.keysym.mod, event.key.repeat, true);
|
||||
|
||||
case SDL_KEYUP:
|
||||
return new KeyEvent(event.key.keysym.sym, event.key.keysym.scancode,
|
||||
event.key.keysym.mod, event.key.repeat, false);
|
||||
|
||||
case SDL_TEXTINPUT:
|
||||
return new KeyTypedEvent(event.text.windowID, event.text.text);
|
||||
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
case SDL_MOUSEBUTTONUP:
|
||||
|
||||
switch(event.button.button) {
|
||||
case SDL_BUTTON_LEFT: return new MouseButtonEvent(0, (event.type == SDL_MOUSEBUTTONDOWN));
|
||||
case SDL_BUTTON_RIGHT: return new MouseButtonEvent(1, (event.type == SDL_MOUSEBUTTONDOWN));
|
||||
case SDL_BUTTON_MIDDLE: return new MouseButtonEvent(2, (event.type == SDL_MOUSEBUTTONDOWN));
|
||||
case SDL_BUTTON_X1: return new MouseButtonEvent(3, (event.type == SDL_MOUSEBUTTONDOWN));
|
||||
case SDL_BUTTON_X2: return new MouseButtonEvent(4, (event.type == SDL_MOUSEBUTTONDOWN));
|
||||
}
|
||||
|
||||
|
||||
case SDL_MOUSEMOTION:
|
||||
return new MouseMovedEvent(event.motion.x, event.motion.y, event.motion.xrel, event.motion.yrel);
|
||||
|
||||
case SDL_MOUSEWHEEL:
|
||||
return new MouseScrolledEvent(event.wheel.x, event.wheel.y);
|
||||
|
||||
case SDL_WINDOWEVENT:
|
||||
return new WindowEvent(event.window.windowID, event.window.event);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
const Event* WindowGLFW::pollEvents() {
|
||||
|
||||
while (SDL_PollEvent(&event)) {
|
||||
|
||||
return createEvent();
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
42
window/glfwWindow.h
Normal file
42
window/glfwWindow.h
Normal file
@@ -0,0 +1,42 @@
|
||||
#pragma once
|
||||
|
||||
#include "terminal.h"
|
||||
|
||||
#include "event.h"
|
||||
#include "mouseEvent.h"
|
||||
#include "keyEvent.h"
|
||||
#include "windowEvent.h"
|
||||
|
||||
|
||||
class WindowGLFW {
|
||||
|
||||
public:
|
||||
|
||||
WindowGLFW();
|
||||
|
||||
~WindowGLFW();
|
||||
|
||||
void init( std::string, int, int);
|
||||
|
||||
void shutdown();
|
||||
|
||||
void getSize();
|
||||
|
||||
const Event* pollEvents();
|
||||
|
||||
operator GLFWwindow*() { return window; }
|
||||
|
||||
//private:
|
||||
|
||||
const Event* createEvent();
|
||||
|
||||
int x, y;
|
||||
|
||||
std::string title;
|
||||
|
||||
GLFWwindow* window = nullptr;
|
||||
|
||||
SDL_Event event;
|
||||
|
||||
bool error = false;
|
||||
};
|
||||
94
window/sdl2Window.cpp
Normal file
94
window/sdl2Window.cpp
Normal file
@@ -0,0 +1,94 @@
|
||||
#include "sdl2Window.h"
|
||||
|
||||
|
||||
|
||||
WindowSDL2::WindowSDL2() {
|
||||
|
||||
|
||||
}
|
||||
|
||||
WindowSDL2::~WindowSDL2() {
|
||||
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
void WindowSDL2::init( std::string title, int x, int y) : title(title), x(x), y(y) {
|
||||
|
||||
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0)
|
||||
{
|
||||
printf("Error: %s\n", SDL_GetError());
|
||||
error = true;
|
||||
return;
|
||||
}
|
||||
|
||||
SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_VULKAN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
|
||||
window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, x, y, window_flags);
|
||||
if (window == nullptr)
|
||||
{
|
||||
printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError());
|
||||
error = true;
|
||||
return;
|
||||
}
|
||||
|
||||
// int test = SDL_SetWindowOpacity(this->window, 0.1f);
|
||||
// float t;
|
||||
// SDL_GetWindowOpacity(this->window, &t);
|
||||
// std::cout << test << " " << t << std::endl;
|
||||
}
|
||||
|
||||
void WindowSDL2::shutdown() {
|
||||
~WindowSDL2();
|
||||
}
|
||||
|
||||
void WindowSDL2::getSize() {
|
||||
|
||||
SDL_GetWindowSize(window, &x, &y);
|
||||
}
|
||||
|
||||
const Event* WindowSDL2::createEvent() {
|
||||
switch(event.type) {
|
||||
case SDL_KEYDOWN:
|
||||
return new KeyEvent(event.key.keysym.sym, event.key.keysym.scancode,
|
||||
event.key.keysym.mod, event.key.repeat, true);
|
||||
|
||||
case SDL_KEYUP:
|
||||
return new KeyEvent(event.key.keysym.sym, event.key.keysym.scancode,
|
||||
event.key.keysym.mod, event.key.repeat, false);
|
||||
|
||||
case SDL_TEXTINPUT:
|
||||
return new KeyTypedEvent(event.text.windowID, event.text.text);
|
||||
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
case SDL_MOUSEBUTTONUP:
|
||||
|
||||
switch(event.button.button) {
|
||||
case SDL_BUTTON_LEFT: return new MouseButtonEvent(0, (event.type == SDL_MOUSEBUTTONDOWN));
|
||||
case SDL_BUTTON_RIGHT: return new MouseButtonEvent(1, (event.type == SDL_MOUSEBUTTONDOWN));
|
||||
case SDL_BUTTON_MIDDLE: return new MouseButtonEvent(2, (event.type == SDL_MOUSEBUTTONDOWN));
|
||||
case SDL_BUTTON_X1: return new MouseButtonEvent(3, (event.type == SDL_MOUSEBUTTONDOWN));
|
||||
case SDL_BUTTON_X2: return new MouseButtonEvent(4, (event.type == SDL_MOUSEBUTTONDOWN));
|
||||
}
|
||||
|
||||
|
||||
case SDL_MOUSEMOTION:
|
||||
return new MouseMovedEvent(event.motion.x, event.motion.y, event.motion.xrel, event.motion.yrel);
|
||||
|
||||
case SDL_MOUSEWHEEL:
|
||||
return new MouseScrolledEvent(event.wheel.x, event.wheel.y);
|
||||
|
||||
case SDL_WINDOWEVENT:
|
||||
return new WindowEvent(event.window.windowID, event.window.event);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
const Event* WindowSDL2::pollEvents() {
|
||||
|
||||
while (SDL_PollEvent(&event)) {
|
||||
|
||||
return createEvent();
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
42
window/sdl2Window.h
Normal file
42
window/sdl2Window.h
Normal file
@@ -0,0 +1,42 @@
|
||||
#pragma once
|
||||
|
||||
#include "terminal.h"
|
||||
|
||||
#include "event.h"
|
||||
#include "mouseEvent.h"
|
||||
#include "keyEvent.h"
|
||||
#include "windowEvent.h"
|
||||
|
||||
|
||||
class WindowSDL2 {
|
||||
|
||||
public:
|
||||
|
||||
WindowSDL2();
|
||||
|
||||
~WindowSDL2();
|
||||
|
||||
void init( std::string, int, int);
|
||||
|
||||
void shutdown();
|
||||
|
||||
void getSize();
|
||||
|
||||
const Event* pollEvents();
|
||||
|
||||
operator SDL_Window*() { return window; }
|
||||
|
||||
//private:
|
||||
|
||||
const Event* createEvent();
|
||||
|
||||
int x, y;
|
||||
|
||||
std::string title;
|
||||
|
||||
SDL_Window* window = nullptr;
|
||||
|
||||
SDL_Event event;
|
||||
|
||||
bool error = false;
|
||||
};
|
||||
31
window/window.cpp
Normal file
31
window/window.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#include "window.h"
|
||||
|
||||
Window::Window()
|
||||
{
|
||||
}
|
||||
|
||||
Window::~Window()
|
||||
{
|
||||
}
|
||||
|
||||
void Window::init(std::string, int, int)
|
||||
{
|
||||
}
|
||||
|
||||
void Window::shutdown()
|
||||
{
|
||||
}
|
||||
|
||||
void Window::getSize()
|
||||
{
|
||||
}
|
||||
|
||||
const Event *Window::pollEvents()
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const Event *Window::createEvent()
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
42
window/window.h
Normal file
42
window/window.h
Normal file
@@ -0,0 +1,42 @@
|
||||
#pragma once
|
||||
|
||||
#include "terminal.h"
|
||||
|
||||
#include "event.h"
|
||||
#include "mouseEvent.h"
|
||||
#include "keyEvent.h"
|
||||
#include "windowEvent.h"
|
||||
|
||||
|
||||
class Window {
|
||||
|
||||
public:
|
||||
|
||||
Window();
|
||||
|
||||
~Window();
|
||||
|
||||
void init( std::string, int, int);
|
||||
|
||||
void shutdown();
|
||||
|
||||
void getSize();
|
||||
|
||||
const Event* pollEvents();
|
||||
|
||||
operator SDL_Window*() { return window; }
|
||||
|
||||
//private:
|
||||
|
||||
const Event* createEvent();
|
||||
|
||||
int x, y;
|
||||
|
||||
std::string title;
|
||||
|
||||
SDL_Window* window = nullptr;
|
||||
|
||||
SDL_Event event;
|
||||
|
||||
bool error = false;
|
||||
};
|
||||
Reference in New Issue
Block a user