change windows backend to sdl2

This commit is contained in:
2021-10-31 10:52:43 +01:00
parent 02b3a88c9c
commit 8f0e0c48ed
12 changed files with 607 additions and 441 deletions

View File

@@ -7,11 +7,13 @@
#include "window/Display.h"
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
#include "render/gui/TowerGui.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_video.h>
#include <iostream>
#include "game/GameManager.h"
#include "imgui/imgui_impl_sdl.h"
#include "render/Renderer.h"
#include "render/gui/TowerGui.h"
#include "render/WorldRenderer.h"
#define WINDOW_NAME "Tower Defense"
@@ -20,24 +22,86 @@
namespace Display {
static GLFWwindow* window;
static SDL_Window* window;
static SDL_GLContext glContext;
std::unique_ptr<td::render::Renderer> renderer = std::make_unique<td::render::Renderer>();
static int lastWidth = 0, lastHeight = 0;
static float aspectRatio;
static bool shouldClose = false;
void error_callback(int error, const char* description) {
std::cerr << "GLFW Error : " << description << std::endl;
}
void windowResizeEvent(GLFWwindow* window, int width, int height) {
void windowResizeEvent(int width, int height) {
aspectRatio = (float)width / height;
renderer->resize(width, height);
lastWidth = width;
lastHeight = height;
}
void create() {
bool create() {
window = SDL_CreateWindow(WINDOW_NAME, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
// Prepare and create context
#ifdef __ANDROID__
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
#else
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
#endif
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
glContext = SDL_GL_CreateContext(window);
if (!glContext)
{
std::cerr << "Could not create context! SDL reports error: " << SDL_GetError() << std::endl;
return false;
}
int major, minor, mask;
int r, g, b, a, depth;
SDL_GL_GetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, &mask);
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &major);
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &minor);
SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &r);
SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &g);
SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &b);
SDL_GL_GetAttribute(SDL_GL_ALPHA_SIZE, &a);
SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &depth);
const char* mask_desc;
if (mask & SDL_GL_CONTEXT_PROFILE_CORE) {
mask_desc = "core";
} else if (mask & SDL_GL_CONTEXT_PROFILE_COMPATIBILITY) {
mask_desc = "compatibility";
} else if (mask & SDL_GL_CONTEXT_PROFILE_ES) {
mask_desc = "es";
} else {
mask_desc = "?";
}
std::cout << "Got context: " << major << "." << minor << mask_desc
<< ", R" << r << "G" << g << "B" << b << "A" << a << ", depth bits: " << depth << std::endl;
SDL_GL_MakeCurrent(window, glContext);
/*Log(LOG_INFO) << "Finished initialization";
return ctx;
glfwSetErrorCallback(&error_callback);
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
@@ -45,12 +109,13 @@ void create() {
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, true);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_NAME, nullptr, nullptr);
glfwMakeContextCurrent(window);
glfwMakeContextCurrent(window);*/
if (!renderer->init()) {
exit(1);
}
TowerGui::init(window, renderer.get());
windowResizeEvent(window, WINDOW_WIDTH, WINDOW_HEIGHT);
TowerGui::init(window, glContext, renderer.get());
windowResizeEvent(WINDOW_WIDTH, WINDOW_HEIGHT);
return true;
}
void render() {
@@ -59,32 +124,47 @@ void render() {
}
void update() {
glfwSwapBuffers(window);
int windowWidth, windowHeight;
glfwGetWindowSize(window, &windowWidth, &windowHeight);
if (windowWidth != lastWidth || windowHeight != lastHeight) {
windowResizeEvent(window, windowWidth, windowHeight);
}
SDL_GL_SwapWindow(window);
}
void destroy() {
renderer.reset(0);
TowerGui::destroy();
glfwDestroyWindow(window);
SDL_GL_DeleteContext(glContext);
SDL_DestroyWindow(window);
window = NULL;
glfwTerminate();
}
void pollEvents() {
glfwPollEvents();
SDL_Event event;
while(SDL_PollEvent(&event)){
if(event.type == SDL_WINDOWEVENT){
switch(event.window.event){
case SDL_WINDOWEVENT_CLOSE:{
shouldClose = true;
}
case SDL_WINDOWEVENT_RESIZED:{
int windowWidth, windowHeight;
SDL_GetWindowSize(window, &windowWidth, &windowHeight);
windowResizeEvent(windowWidth, windowHeight);
}
default:
break;
}
}
ImGui_ImplSDL2_ProcessEvent(&event);
}
}
bool isCloseRequested() {
return glfwWindowShouldClose(window);
return shouldClose;
}
bool isMouseDown(int button) {
return glfwGetMouseButton(window, button);
return ImGui::GetIO().MouseDown[button];
}
float getAspectRatio() {