Display
This commit is contained in:
10
src/display/CMakeLists.txt
Normal file
10
src/display/CMakeLists.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
# Copyright (c) 2025 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Sources
|
||||
target_sources(${DAWN_TARGET_NAME}
|
||||
PRIVATE
|
||||
Display.cpp
|
||||
)
|
||||
121
src/display/Display.cpp
Normal file
121
src/display/Display.cpp
Normal 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
|
||||
}
|
||||
36
src/display/Display.hpp
Normal file
36
src/display/Display.hpp
Normal file
@@ -0,0 +1,36 @@
|
||||
// Copyright (c) 2025 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "dawn.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
struct Display {
|
||||
private:
|
||||
#if DAWN_SDL2
|
||||
SDL_Window* window;
|
||||
SDL_GLContext glContext;
|
||||
#endif
|
||||
|
||||
public:
|
||||
static constexpr uint32_t DEFAULT_WIDTH = 800;
|
||||
static constexpr uint32_t DEFAULT_HEIGHT = 600;
|
||||
|
||||
/**
|
||||
* Display constructor
|
||||
*/
|
||||
Display(void);
|
||||
|
||||
/**
|
||||
* Update the display (swap buffers, etc.)
|
||||
*/
|
||||
void update(void);
|
||||
|
||||
/**
|
||||
* Display destructor
|
||||
*/
|
||||
~Display(void);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user