This commit is contained in:
2025-09-19 23:20:55 -05:00
parent 96fcddea30
commit 1d16c0ae68
12 changed files with 256 additions and 39 deletions

121
src/display/Display.cpp Normal file
View File

@@ -0,0 +1,121 @@
// Copyright (c) 2025 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "Display.hpp"
#include "engine/Engine.hpp"
using namespace Dawn;
Display::Display(void) :
#if DAWN_SDL2
glContext(nullptr),
window(nullptr)
#endif
{
#if DAWN_SDL2
uint32_t flags = SDL_INIT_VIDEO;
#if DAWN_SDL2_GAMEPAD
flags |= SDL_INIT_GAMECONTROLLER | SDL_INIT_JOYSTICK;
#endif
if(SDL_Init(flags) != 0) {
throw "Failed to initialize SDL2";
}
// Set OpenGL Attributes
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
// Create window
this->window = SDL_CreateWindow(
"Dawn",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
DEFAULT_WIDTH,
DEFAULT_HEIGHT,
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI |
SDL_WINDOW_SHOWN
);
if(!this->window) {
throw "Failed to create SDL2 window";
}
// Create OpenGL context
this->glContext = SDL_GL_CreateContext(this->window);
if(!this->glContext) {
throw "Failed to create OpenGL context";
}
// Setup GL
SDL_GL_SetSwapInterval(1);// Enable vsync
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glDisable(GL_LIGHTING);// PSP defaults this on?
glShadeModel(GL_SMOOTH);// Fixes color on PSP?
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glEnableClientState(GL_COLOR_ARRAY);// TODO: every frame on PSP?
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
#else
#error "No display platform defined"
#endif
}
void Display::update(void) {
#if DAWN_SDL2
SDL_Event event;
while(SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_QUIT: {
Engine::getInstance()->console.exec("exit");
break;
}
case SDL_WINDOWEVENT: {
switch(event.window.event) {
case SDL_WINDOWEVENT_CLOSE: {
Engine::getInstance()->console.exec("exit");
break;
}
default: {
break;
}
}
break;
}
default: {
break;
}
}
}
SDL_GL_MakeCurrent(this->window, this->glContext);
glViewport(0, 0, DEFAULT_WIDTH, DEFAULT_HEIGHT);
SDL_GL_SwapWindow(this->window);
#endif
}
Display::~Display(void) {
#if DAWN_SDL2
if(this->glContext) {
SDL_GL_DeleteContext(this->glContext);
this->glContext = nullptr;
}
if(this->window) {
SDL_DestroyWindow(this->window);
this->window = nullptr;
}
SDL_Quit();
#endif
}