folders
This commit is contained in:
@@ -281,6 +281,7 @@
|
|||||||
curl
|
curl
|
||||||
glm
|
glm
|
||||||
nlohmann_json
|
nlohmann_json
|
||||||
|
stb
|
||||||
|
|
||||||
gamenetworkingsockets
|
gamenetworkingsockets
|
||||||
|
|
||||||
|
|||||||
@@ -2,32 +2,35 @@
|
|||||||
#define RENDERER_H
|
#define RENDERER_H
|
||||||
|
|
||||||
#include "pch.hpp"
|
#include "pch.hpp"
|
||||||
#include "RendererOpenGL/RendererOpenGL.h"
|
|
||||||
#include "RendererSDL3/RendererSDL3.h"
|
|
||||||
|
|
||||||
namespace Archimedes {
|
namespace Archimedes {
|
||||||
|
|
||||||
|
|
||||||
|
class VertexArray {};
|
||||||
|
|
||||||
|
class IndexArray {};
|
||||||
|
|
||||||
|
class Shader {};
|
||||||
|
|
||||||
class Renderer {
|
class Renderer {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
int w, h;
|
int w, h;
|
||||||
|
|
||||||
~Renderer() {}
|
Renderer() {}
|
||||||
|
virtual ~Renderer() = 0;
|
||||||
|
|
||||||
bool init(void* ptr) { return r.init(ptr); }
|
virtual bool init() = 0;
|
||||||
|
|
||||||
void render() {
|
virtual void render() = 0;
|
||||||
r.render(rc, w, h);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::list<std::function<void()>>& getCmdList() {
|
std::list<std::function<void()>>& getCmdList() {
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
RendererImpl& getRendererImpl() { return r; }
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::list<std::function<void()>> rc;
|
std::list<std::function<void()>> rc;
|
||||||
RendererImpl r;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,13 +3,15 @@
|
|||||||
|
|
||||||
#include "pch.hpp"
|
#include "pch.hpp"
|
||||||
|
|
||||||
|
#include "utils/Renderer/Renderer.h"
|
||||||
|
|
||||||
#define GLEW_STATIC
|
#define GLEW_STATIC
|
||||||
#include <GL/glew.h>
|
#include <GL/glew.h>
|
||||||
|
|
||||||
|
|
||||||
namespace Archimedes {
|
namespace Archimedes {
|
||||||
|
|
||||||
class RendererOpenGL {
|
class RendererOpenGL : public Renderer {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
typedef void renderCmd();
|
typedef void renderCmd();
|
||||||
@@ -17,7 +19,7 @@ namespace Archimedes {
|
|||||||
RendererOpenGL() {};
|
RendererOpenGL() {};
|
||||||
~RendererOpenGL() {};
|
~RendererOpenGL() {};
|
||||||
|
|
||||||
bool init(void* p) {
|
bool init() {
|
||||||
return glewInit() == GLEW_OK;
|
return glewInit() == GLEW_OK;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -27,9 +29,9 @@ namespace Archimedes {
|
|||||||
glClearColor(0.2, 0.2, 0.4, 1);
|
glClearColor(0.2, 0.2, 0.4, 1);
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
for(auto f : cmdList)
|
|
||||||
f();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void draw() {}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,44 +0,0 @@
|
|||||||
#if RENDERER == 1
|
|
||||||
|
|
||||||
#ifndef RENDERER_OPENGL
|
|
||||||
#define RENDERER_OPENGL
|
|
||||||
|
|
||||||
#include "pch.hpp"
|
|
||||||
|
|
||||||
#define GLEW_STATIC
|
|
||||||
#include <GL/glew.h>
|
|
||||||
|
|
||||||
|
|
||||||
namespace Archimedes {
|
|
||||||
|
|
||||||
class RendererOpenGL {
|
|
||||||
|
|
||||||
public:
|
|
||||||
typedef void renderCmd();
|
|
||||||
|
|
||||||
RendererOpenGL() {};
|
|
||||||
~RendererOpenGL() {};
|
|
||||||
|
|
||||||
bool init(void* p) {
|
|
||||||
return glewInit() == GLEW_OK;
|
|
||||||
};
|
|
||||||
|
|
||||||
void render(std::list<std::function<void()>> cmdList, int& w, int& h) {
|
|
||||||
|
|
||||||
glViewport(0, 0, w, h);
|
|
||||||
glClearColor(0.2, 0.2, 0.4, 1);
|
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
|
||||||
|
|
||||||
for(auto f : cmdList)
|
|
||||||
f();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef RendererOpenGL RendererImpl;
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
#if RENDERER == 2
|
|
||||||
|
|
||||||
#ifndef RENDERER_SDL3
|
|
||||||
#define RENDERER_SDL3
|
|
||||||
|
|
||||||
#include "pch.hpp"
|
|
||||||
|
|
||||||
#include <SDL3/SDL.h>
|
|
||||||
|
|
||||||
|
|
||||||
namespace Archimedes {
|
|
||||||
|
|
||||||
class RendererSDL3 {
|
|
||||||
|
|
||||||
public:
|
|
||||||
typedef void renderCmd();
|
|
||||||
|
|
||||||
RendererSDL3() {};
|
|
||||||
~RendererSDL3() { SDL_DestroyRenderer(renderer); };
|
|
||||||
|
|
||||||
bool init(void* window) {
|
|
||||||
renderer = SDL_CreateRenderer((SDL_Window*) window, nullptr);
|
|
||||||
SDL_SetRenderVSync(renderer, 1);
|
|
||||||
return renderer != nullptr;
|
|
||||||
};
|
|
||||||
|
|
||||||
void render(std::list<std::function<void()>> cmdList, int& w, int& h) {
|
|
||||||
|
|
||||||
//SDL_SetRenderScale(renderer, w, h);
|
|
||||||
|
|
||||||
SDL_RenderClear(renderer);
|
|
||||||
|
|
||||||
for(auto f : cmdList)
|
|
||||||
f();
|
|
||||||
|
|
||||||
SDL_RenderPresent(renderer);
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_Renderer* renderer = nullptr;
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef RendererSDL3 RendererImpl;
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,145 +0,0 @@
|
|||||||
#if WINDOW == 1
|
|
||||||
|
|
||||||
#ifndef WINDOW_GLFW
|
|
||||||
#define WINDOW_GLFW
|
|
||||||
|
|
||||||
#include "pch.hpp"
|
|
||||||
|
|
||||||
#include "utils/Window/WindowEvents.h"
|
|
||||||
|
|
||||||
#define GLFW_INCLUDE_NONE
|
|
||||||
#include <GLFW/glfw3.h>
|
|
||||||
|
|
||||||
|
|
||||||
namespace Archimedes {
|
|
||||||
|
|
||||||
class Window;
|
|
||||||
|
|
||||||
class WindowGLFW {
|
|
||||||
|
|
||||||
public:
|
|
||||||
|
|
||||||
WindowGLFW(Window* p, const std::function<void(Event*)>& sendEvent) {
|
|
||||||
|
|
||||||
/*if(glfwPlatformSupported(GLFW_PLATFORM_WAYLAND)) {
|
|
||||||
glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_WAYLAND);
|
|
||||||
}*/
|
|
||||||
|
|
||||||
|
|
||||||
data.window = p;
|
|
||||||
data.sendEvent = sendEvent;
|
|
||||||
|
|
||||||
glfwSetErrorCallback([](int e, const char* m){
|
|
||||||
std::cout << "GLFW Error " << e << ": " << m << std::endl;
|
|
||||||
});
|
|
||||||
|
|
||||||
if(!glfwInit()) {
|
|
||||||
std::cout << "glfwInit failed!\n";
|
|
||||||
std::abort();
|
|
||||||
}
|
|
||||||
#if RENDRER == 1
|
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
|
||||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|
||||||
#endif
|
|
||||||
w = glfwCreateWindow(640, 480, "Archimedes", NULL, NULL);
|
|
||||||
|
|
||||||
if(!w) {
|
|
||||||
std::cout << "glfwCreateWindow failed!\n";
|
|
||||||
glfwTerminate();
|
|
||||||
std::abort();
|
|
||||||
}
|
|
||||||
glfwMakeContextCurrent(w);
|
|
||||||
glfwSwapInterval(1);
|
|
||||||
|
|
||||||
glfwSetWindowUserPointer(w, &data);
|
|
||||||
|
|
||||||
glfwSetWindowSizeCallback(w, [](GLFWwindow* window, int w, int h){
|
|
||||||
WindowData& d = *(WindowData*) glfwGetWindowUserPointer(window);
|
|
||||||
|
|
||||||
d.sendEvent(new ResizeWindowEvent(w, h));
|
|
||||||
});
|
|
||||||
|
|
||||||
glfwSetWindowCloseCallback(w, [](GLFWwindow* window){
|
|
||||||
WindowData& d = *(WindowData*) glfwGetWindowUserPointer(window);
|
|
||||||
|
|
||||||
d.sendEvent(new CloseWindowEvent(d.window));
|
|
||||||
});
|
|
||||||
|
|
||||||
glfwSetKeyCallback(w, [](GLFWwindow* window, int key, int scancode, int action, int mods){
|
|
||||||
WindowData& d = *(WindowData*) glfwGetWindowUserPointer(window);
|
|
||||||
|
|
||||||
switch(action) {
|
|
||||||
case GLFW_PRESS:
|
|
||||||
d.sendEvent(new KeyPressedWindowEvent(key, 0));
|
|
||||||
break;
|
|
||||||
case GLFW_RELEASE:
|
|
||||||
d.sendEvent(new KeyReleasedWindowEvent(key));
|
|
||||||
break;
|
|
||||||
case GLFW_REPEAT:
|
|
||||||
d.sendEvent(new KeyPressedWindowEvent(key, 1));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
glfwSetMouseButtonCallback(w, [](GLFWwindow* window, int button, int action, int mods){
|
|
||||||
WindowData& d = *(WindowData*) glfwGetWindowUserPointer(window);
|
|
||||||
|
|
||||||
switch(action) {
|
|
||||||
case GLFW_PRESS:
|
|
||||||
d.sendEvent(new MouseButtonPressedWindowEvent(button));
|
|
||||||
break;
|
|
||||||
case GLFW_RELEASE:
|
|
||||||
d.sendEvent(new MouseButtonReleasedWindowEvent(button));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
glfwSetScrollCallback(w, [](GLFWwindow* window, double dx, double dy){
|
|
||||||
WindowData& d = *(WindowData*) glfwGetWindowUserPointer(window);
|
|
||||||
|
|
||||||
d.sendEvent(new ScrollWindowEvent(dx, dy));
|
|
||||||
});
|
|
||||||
|
|
||||||
glfwSetCursorPosCallback(w, [](GLFWwindow* window, double dx, double dy){
|
|
||||||
WindowData& d = *(WindowData*) glfwGetWindowUserPointer(window);
|
|
||||||
|
|
||||||
d.sendEvent(new MouseMovedWindowEvent(dx, dy));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
~WindowGLFW() {
|
|
||||||
glfwTerminate();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
bool shouldClose() {
|
|
||||||
return glfwWindowShouldClose(w);
|
|
||||||
}
|
|
||||||
|
|
||||||
void doFrame() { restoreContext(); glfwSwapBuffers(w); }
|
|
||||||
|
|
||||||
void pollEvents() { glfwPollEvents(); }
|
|
||||||
|
|
||||||
void restoreContext() { glfwMakeContextCurrent(w); }
|
|
||||||
|
|
||||||
void getSize(int& w, int& h) {
|
|
||||||
glfwGetFramebufferSize(this->w, &w, &h);
|
|
||||||
}
|
|
||||||
|
|
||||||
GLFWwindow* getWindow() { return w; }
|
|
||||||
|
|
||||||
WindowData data;
|
|
||||||
|
|
||||||
private:
|
|
||||||
GLFWwindow* w;
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef WindowGLFW WindowImpl;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
#if WINDOW == 1
|
||||||
|
|
||||||
#ifndef WINDOW_GLFW
|
#ifndef WINDOW_GLFW
|
||||||
#define WINDOW_GLFW
|
#define WINDOW_GLFW
|
||||||
|
|
||||||
@@ -11,17 +13,13 @@
|
|||||||
|
|
||||||
namespace Archimedes {
|
namespace Archimedes {
|
||||||
|
|
||||||
template <class WindowImpl, class RendererImpl>
|
|
||||||
requires allowed_windows<WindowImpl, RendererImpl>
|
|
||||||
&& allowed_renderers<RendererImpl>
|
|
||||||
class Window;
|
class Window;
|
||||||
|
|
||||||
template <class R>
|
|
||||||
class WindowGLFW {
|
class WindowGLFW {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
WindowGLFW(Window<WindowGLFW<R>, R>* p, const std::function<void(Event*)>& sendEvent) {
|
WindowGLFW(Window* p, const std::function<void(Event*)>& sendEvent) {
|
||||||
|
|
||||||
/*if(glfwPlatformSupported(GLFW_PLATFORM_WAYLAND)) {
|
/*if(glfwPlatformSupported(GLFW_PLATFORM_WAYLAND)) {
|
||||||
glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_WAYLAND);
|
glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_WAYLAND);
|
||||||
@@ -57,19 +55,19 @@ namespace Archimedes {
|
|||||||
glfwSetWindowUserPointer(w, &data);
|
glfwSetWindowUserPointer(w, &data);
|
||||||
|
|
||||||
glfwSetWindowSizeCallback(w, [](GLFWwindow* window, int w, int h){
|
glfwSetWindowSizeCallback(w, [](GLFWwindow* window, int w, int h){
|
||||||
WindowData<WindowGLFW<R>, R>& d = *(WindowData<WindowGLFW<R>, R>*) glfwGetWindowUserPointer(window);
|
WindowData& d = *(WindowData*) glfwGetWindowUserPointer(window);
|
||||||
|
|
||||||
d.sendEvent(new ResizeWindowEvent(w, h));
|
d.sendEvent(new ResizeWindowEvent(w, h));
|
||||||
});
|
});
|
||||||
|
|
||||||
glfwSetWindowCloseCallback(w, [](GLFWwindow* window){
|
glfwSetWindowCloseCallback(w, [](GLFWwindow* window){
|
||||||
WindowData<WindowGLFW<R>, R>& d = *(WindowData<WindowGLFW<R>, R>*) glfwGetWindowUserPointer(window);
|
WindowData& d = *(WindowData*) glfwGetWindowUserPointer(window);
|
||||||
|
|
||||||
d.sendEvent(new CloseWindowEvent(d.window));
|
d.sendEvent(new CloseWindowEvent(d.window));
|
||||||
});
|
});
|
||||||
|
|
||||||
glfwSetKeyCallback(w, [](GLFWwindow* window, int key, int scancode, int action, int mods){
|
glfwSetKeyCallback(w, [](GLFWwindow* window, int key, int scancode, int action, int mods){
|
||||||
WindowData<WindowGLFW<R>, R>& d = *(WindowData<WindowGLFW<R>, R>*) glfwGetWindowUserPointer(window);
|
WindowData& d = *(WindowData*) glfwGetWindowUserPointer(window);
|
||||||
|
|
||||||
switch(action) {
|
switch(action) {
|
||||||
case GLFW_PRESS:
|
case GLFW_PRESS:
|
||||||
@@ -85,7 +83,7 @@ namespace Archimedes {
|
|||||||
});
|
});
|
||||||
|
|
||||||
glfwSetMouseButtonCallback(w, [](GLFWwindow* window, int button, int action, int mods){
|
glfwSetMouseButtonCallback(w, [](GLFWwindow* window, int button, int action, int mods){
|
||||||
WindowData<WindowGLFW<R>, R>& d = *(WindowData<WindowGLFW<R>, R>*) glfwGetWindowUserPointer(window);
|
WindowData& d = *(WindowData*) glfwGetWindowUserPointer(window);
|
||||||
|
|
||||||
switch(action) {
|
switch(action) {
|
||||||
case GLFW_PRESS:
|
case GLFW_PRESS:
|
||||||
@@ -98,13 +96,13 @@ namespace Archimedes {
|
|||||||
});
|
});
|
||||||
|
|
||||||
glfwSetScrollCallback(w, [](GLFWwindow* window, double dx, double dy){
|
glfwSetScrollCallback(w, [](GLFWwindow* window, double dx, double dy){
|
||||||
WindowData<WindowGLFW<R>, R>& d = *(WindowData<WindowGLFW<R>, R>*) glfwGetWindowUserPointer(window);
|
WindowData& d = *(WindowData*) glfwGetWindowUserPointer(window);
|
||||||
|
|
||||||
d.sendEvent(new ScrollWindowEvent(dx, dy));
|
d.sendEvent(new ScrollWindowEvent(dx, dy));
|
||||||
});
|
});
|
||||||
|
|
||||||
glfwSetCursorPosCallback(w, [](GLFWwindow* window, double dx, double dy){
|
glfwSetCursorPosCallback(w, [](GLFWwindow* window, double dx, double dy){
|
||||||
WindowData<WindowGLFW<R>, R>& d = *(WindowData<WindowGLFW<R>, R>*) glfwGetWindowUserPointer(window);
|
WindowData& d = *(WindowData*) glfwGetWindowUserPointer(window);
|
||||||
|
|
||||||
d.sendEvent(new MouseMovedWindowEvent(dx, dy));
|
d.sendEvent(new MouseMovedWindowEvent(dx, dy));
|
||||||
});
|
});
|
||||||
@@ -131,13 +129,17 @@ namespace Archimedes {
|
|||||||
|
|
||||||
GLFWwindow* getWindow() { return w; }
|
GLFWwindow* getWindow() { return w; }
|
||||||
|
|
||||||
WindowData<WindowGLFW<R>, R> data;
|
WindowData data;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
GLFWwindow* w;
|
GLFWwindow* w;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
typedef WindowGLFW WindowImpl;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
#if WINDOW == 2
|
||||||
|
|
||||||
#ifndef WINDOW_SDL3
|
#ifndef WINDOW_SDL3
|
||||||
#define WINDOW_SDL3
|
#define WINDOW_SDL3
|
||||||
|
|
||||||
@@ -15,15 +17,11 @@
|
|||||||
|
|
||||||
namespace Archimedes {
|
namespace Archimedes {
|
||||||
|
|
||||||
template <class WindowImpl, class RendererImpl>
|
|
||||||
requires allowed_windows<WindowImpl, RendererImpl>
|
|
||||||
&& allowed_renderers<RendererImpl>
|
|
||||||
class Window;
|
class Window;
|
||||||
|
|
||||||
template <class W, class R>
|
|
||||||
static bool EventCallback(void* ptr, SDL_Event* e) {
|
static bool EventCallback(void* ptr, SDL_Event* e) {
|
||||||
|
|
||||||
WindowData<W, R>& data = *(WindowData<W, R>*) ptr;
|
WindowData& data = *(WindowData*) ptr;
|
||||||
|
|
||||||
switch(e->type) {
|
switch(e->type) {
|
||||||
|
|
||||||
@@ -68,12 +66,11 @@ namespace Archimedes {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class R>
|
|
||||||
class WindowSDL3 {
|
class WindowSDL3 {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
WindowSDL3(Window<WindowSDL3<R>, R>* p, const std::function<void(Event*)>& sendEvent) {
|
WindowSDL3(Window* p, const std::function<void(Event*)>& sendEvent) {
|
||||||
|
|
||||||
data.window = p;
|
data.window = p;
|
||||||
data.sendEvent = sendEvent;
|
data.sendEvent = sendEvent;
|
||||||
@@ -157,7 +154,7 @@ namespace Archimedes {
|
|||||||
#if RENDERER == 1
|
#if RENDERER == 1
|
||||||
SDL_GLContext getContext() { return gl_context; }
|
SDL_GLContext getContext() { return gl_context; }
|
||||||
#endif
|
#endif
|
||||||
WindowData<WindowSDL3<R>, R> data;
|
WindowData data;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
SDL_Window* w;
|
SDL_Window* w;
|
||||||
@@ -168,6 +165,9 @@ namespace Archimedes {
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
typedef WindowSDL3 WindowImpl;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|||||||
@@ -1,173 +0,0 @@
|
|||||||
#if WINDOW == 2
|
|
||||||
|
|
||||||
#ifndef WINDOW_SDL3
|
|
||||||
#define WINDOW_SDL3
|
|
||||||
|
|
||||||
#include "pch.hpp"
|
|
||||||
|
|
||||||
#include "utils/Window/WindowEvents.h"
|
|
||||||
|
|
||||||
#include <SDL3/SDL.h>
|
|
||||||
|
|
||||||
#if RENDERER == 2
|
|
||||||
|
|
||||||
#include <SDL3/SDL_opengl.h>
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace Archimedes {
|
|
||||||
|
|
||||||
class Window;
|
|
||||||
|
|
||||||
static bool EventCallback(void* ptr, SDL_Event* e) {
|
|
||||||
|
|
||||||
WindowData& data = *(WindowData*) ptr;
|
|
||||||
|
|
||||||
switch(e->type) {
|
|
||||||
|
|
||||||
case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
|
|
||||||
case SDL_EVENT_QUIT:
|
|
||||||
data.sendEvent(new CloseWindowEvent(data.window, e));
|
|
||||||
break;
|
|
||||||
case SDL_EVENT_WINDOW_RESIZED:
|
|
||||||
data.sendEvent(new ResizeWindowEvent(e->window.data1, e->window.data2, e));
|
|
||||||
break;
|
|
||||||
case SDL_EVENT_WINDOW_MOUSE_ENTER:
|
|
||||||
case SDL_EVENT_WINDOW_FOCUS_GAINED:
|
|
||||||
data.sendEvent(new FocusedWindowEvent(data.window, e));
|
|
||||||
break;
|
|
||||||
case SDL_EVENT_WINDOW_MOUSE_LEAVE:
|
|
||||||
case SDL_EVENT_WINDOW_FOCUS_LOST:
|
|
||||||
data.sendEvent(new FocusLostWindowEvent(data.window, e));
|
|
||||||
break;
|
|
||||||
case SDL_EVENT_KEY_DOWN:
|
|
||||||
data.sendEvent(new KeyPressedWindowEvent(e->key.key, e->key.repeat ? 1 : 0, e));
|
|
||||||
break;
|
|
||||||
case SDL_EVENT_KEY_UP:
|
|
||||||
data.sendEvent(new KeyReleasedWindowEvent(e->key.key, e));
|
|
||||||
break;
|
|
||||||
case SDL_EVENT_MOUSE_MOTION:
|
|
||||||
data.sendEvent(new MouseMovedWindowEvent(e->motion.x, e->motion.y, e));
|
|
||||||
break;
|
|
||||||
case SDL_EVENT_MOUSE_BUTTON_DOWN:
|
|
||||||
data.sendEvent(new MouseButtonPressedWindowEvent(e->button.button, e));
|
|
||||||
break;
|
|
||||||
case SDL_EVENT_MOUSE_BUTTON_UP:
|
|
||||||
data.sendEvent(new MouseButtonReleasedWindowEvent(e->button.button, e));
|
|
||||||
break;
|
|
||||||
case SDL_EVENT_MOUSE_WHEEL:
|
|
||||||
data.sendEvent(new ScrollWindowEvent(e->wheel.x, e->wheel.y, e));
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
data.sendEvent(new AnonymousEvent(e));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
class WindowSDL3 {
|
|
||||||
|
|
||||||
public:
|
|
||||||
|
|
||||||
WindowSDL3(Window* p, const std::function<void(Event*)>& sendEvent) {
|
|
||||||
|
|
||||||
data.window = p;
|
|
||||||
data.sendEvent = sendEvent;
|
|
||||||
|
|
||||||
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD)) {
|
|
||||||
std::cerr << "Error: SDL_Init(): " << SDL_GetError() << std::flush;
|
|
||||||
std::abort();
|
|
||||||
}
|
|
||||||
|
|
||||||
#if RENDERER == 1
|
|
||||||
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
|
||||||
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
|
|
||||||
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
|
|
||||||
|
|
||||||
SDL_WindowFlags window_flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN;
|
|
||||||
#elif RENDERER == 2
|
|
||||||
SDL_WindowFlags window_flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN | SDL_WINDOW_HIGH_PIXEL_DENSITY;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
w = SDL_CreateWindow("Archimedes", 1280, 720, window_flags);
|
|
||||||
if (w == nullptr)
|
|
||||||
{
|
|
||||||
std::cerr << "Error: SDL_CreateWindow(): " << SDL_GetError() << std::endl;
|
|
||||||
std::abort();
|
|
||||||
}
|
|
||||||
SDL_SetWindowPosition(w, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
|
|
||||||
|
|
||||||
#if RENDERER == 1
|
|
||||||
gl_context = SDL_GL_CreateContext(w);
|
|
||||||
|
|
||||||
if (gl_context == nullptr)
|
|
||||||
{
|
|
||||||
std::cerr << "Error: SDL_GL_CreateContext(): " << SDL_GetError() << std::endl;
|
|
||||||
std::abort();
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_GL_MakeCurrent(w, gl_context);
|
|
||||||
SDL_GL_SetSwapInterval(1); // Enable vsync
|
|
||||||
#endif
|
|
||||||
SDL_AddEventWatch(EventCallback, &data);
|
|
||||||
|
|
||||||
SDL_ShowWindow(w);
|
|
||||||
}
|
|
||||||
|
|
||||||
~WindowSDL3() {
|
|
||||||
#if RENDERER == 1
|
|
||||||
SDL_GL_DestroyContext(gl_context);
|
|
||||||
#endif
|
|
||||||
SDL_RemoveEventWatch(EventCallback, &data);
|
|
||||||
SDL_DestroyWindow(w);
|
|
||||||
SDL_Quit();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool shouldClose() { return false; }
|
|
||||||
|
|
||||||
void doFrame() {
|
|
||||||
#if RENDERER == 1
|
|
||||||
restoreContext();
|
|
||||||
SDL_GL_SwapWindow(w);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
void pollEvents() {
|
|
||||||
static SDL_Event e;
|
|
||||||
while(SDL_PollEvent(&e)) {}
|
|
||||||
}
|
|
||||||
#if RENDERER == 1
|
|
||||||
void restoreContext() { SDL_GL_MakeCurrent(w, gl_context); }
|
|
||||||
#endif
|
|
||||||
void getSize(int& w, int& h) {
|
|
||||||
w = this->width;
|
|
||||||
h = this->height;
|
|
||||||
}
|
|
||||||
|
|
||||||
void setSize(int& w, int& h) {
|
|
||||||
this->width = w;
|
|
||||||
this->height = h;
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_Window* getWindow() { return w; }
|
|
||||||
#if RENDERER == 1
|
|
||||||
SDL_GLContext getContext() { return gl_context; }
|
|
||||||
#endif
|
|
||||||
WindowData data;
|
|
||||||
|
|
||||||
private:
|
|
||||||
SDL_Window* w;
|
|
||||||
int width = 0, height = 0;
|
|
||||||
#if RENDERER == 1
|
|
||||||
SDL_GLContext gl_context;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef WindowSDL3 WindowImpl;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif
|
|
||||||
Reference in New Issue
Block a user