145 lines
3.4 KiB
C++
145 lines
3.4 KiB
C++
// 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;
|
|
|
|
MeshVertex vertices[3] = {
|
|
{ {255, 0, 0, 255}, glm::vec2(0.5f, 1.0f), glm::vec3(0.0f, 0.5f, 0.0f) },
|
|
{ {0, 255, 0, 255}, glm::vec2(0.0f, 0.0f), glm::vec3(-0.5f, -0.5f, 0.0f) },
|
|
{ {0, 0, 255, 255}, glm::vec2(1.0f, 0.0f), glm::vec3(0.5f, -0.5f, 0.0f) }
|
|
};
|
|
|
|
Display::Display(Engine &engine) :
|
|
#if DAWN_SDL2
|
|
glContext(nullptr),
|
|
window(nullptr),
|
|
#endif
|
|
engine(engine),
|
|
mesh(3, PrimitiveType::TRIANGLES, vertices)
|
|
{
|
|
|
|
#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.console.exec("exit");
|
|
break;
|
|
}
|
|
|
|
case SDL_WINDOWEVENT: {
|
|
switch(event.window.event) {
|
|
case SDL_WINDOWEVENT_CLOSE: {
|
|
engine.console.exec("exit");
|
|
break;
|
|
}
|
|
|
|
default: {
|
|
break;
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
|
|
default: {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
// SDL_GL_MakeCurrent(this->window, this->glContext);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
glClearColor(
|
|
Color4F::CORNFLOWER_BLUE.r,
|
|
Color4F::CORNFLOWER_BLUE.g,
|
|
Color4F::CORNFLOWER_BLUE.b,
|
|
Color4F::CORNFLOWER_BLUE.a
|
|
);
|
|
|
|
mesh.draw(0, -1);
|
|
|
|
// glViewport(0, 0, DEFAULT_WIDTH, DEFAULT_HEIGHT);
|
|
|
|
GLenum err = glGetError();
|
|
if (err != GL_NO_ERROR) {
|
|
printf("OpenGL error: %d\n", err);
|
|
}
|
|
|
|
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
|
|
} |