Display
This commit is contained in:
@@ -14,11 +14,10 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/mod
|
|||||||
|
|
||||||
if(NOT DEFINED DAWN_TARGET_SYSTEM)
|
if(NOT DEFINED DAWN_TARGET_SYSTEM)
|
||||||
set(DAWN_TARGET_SYSTEM "linux")
|
set(DAWN_TARGET_SYSTEM "linux")
|
||||||
# set(DAWN_TARGET_SYSTEM "psp")
|
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
# Prep cache
|
# Prep cache
|
||||||
set(DAWN_CACHE_TARGET "dusk-target")
|
set(DAWN_CACHE_TARGET "dawn-target")
|
||||||
|
|
||||||
# Build variables
|
# Build variables
|
||||||
set(DAWN_ROOT_DIR "${CMAKE_SOURCE_DIR}")
|
set(DAWN_ROOT_DIR "${CMAKE_SOURCE_DIR}")
|
||||||
@@ -30,8 +29,8 @@ set(DAWN_DATA_DIR "${DAWN_ROOT_DIR}/data")
|
|||||||
set(DAWN_ASSETS_DIR "${DAWN_ROOT_DIR}/assets")
|
set(DAWN_ASSETS_DIR "${DAWN_ROOT_DIR}/assets")
|
||||||
set(DAWN_BUILT_ASSETS_DIR "${DAWN_BUILD_DIR}/built_assets" CACHE INTERNAL ${DAWN_CACHE_TARGET})
|
set(DAWN_BUILT_ASSETS_DIR "${DAWN_BUILD_DIR}/built_assets" CACHE INTERNAL ${DAWN_CACHE_TARGET})
|
||||||
set(DAWN_GENERATED_HEADERS_DIR "${DAWN_BUILD_DIR}/generated")
|
set(DAWN_GENERATED_HEADERS_DIR "${DAWN_BUILD_DIR}/generated")
|
||||||
set(DAWN_TARGET_NAME "Dusk" CACHE INTERNAL ${DAWN_CACHE_TARGET})
|
set(DAWN_TARGET_NAME "Dawn" CACHE INTERNAL ${DAWN_CACHE_TARGET})
|
||||||
set(DAWN_BUILD_BINARY ${DAWN_BUILD_DIR}/Dusk CACHE INTERNAL ${DAWN_CACHE_TARGET})
|
set(DAWN_BUILD_BINARY ${DAWN_BUILD_DIR}/Dawn CACHE INTERNAL ${DAWN_CACHE_TARGET})
|
||||||
set(DAWN_ASSETS "" CACHE INTERNAL ${DAWN_CACHE_TARGET})
|
set(DAWN_ASSETS "" CACHE INTERNAL ${DAWN_CACHE_TARGET})
|
||||||
|
|
||||||
# Create directories
|
# Create directories
|
||||||
@@ -64,7 +63,8 @@ add_executable(${DAWN_TARGET_NAME})
|
|||||||
if(DAWN_TARGET_SYSTEM STREQUAL "linux")
|
if(DAWN_TARGET_SYSTEM STREQUAL "linux")
|
||||||
find_package(SDL2 REQUIRED)
|
find_package(SDL2 REQUIRED)
|
||||||
find_package(OpenGL REQUIRED)
|
find_package(OpenGL REQUIRED)
|
||||||
target_link_libraries(${DAWN_TARGET_NAME} PRIVATE
|
target_link_libraries(${DAWN_TARGET_NAME}
|
||||||
|
PRIVATE
|
||||||
SDL2
|
SDL2
|
||||||
OpenGL::GL
|
OpenGL::GL
|
||||||
GL
|
GL
|
||||||
@@ -73,7 +73,8 @@ if(DAWN_TARGET_SYSTEM STREQUAL "linux")
|
|||||||
elseif(DAWN_TARGET_SYSTEM STREQUAL "psp")
|
elseif(DAWN_TARGET_SYSTEM STREQUAL "psp")
|
||||||
find_package(SDL2 REQUIRED)
|
find_package(SDL2 REQUIRED)
|
||||||
find_package(OpenGL REQUIRED)
|
find_package(OpenGL REQUIRED)
|
||||||
target_link_libraries(${DAWN_TARGET_NAME} PUBLIC
|
target_link_libraries(${DAWN_TARGET_NAME}
|
||||||
|
PUBLIC
|
||||||
${SDL2_LIBRARIES}
|
${SDL2_LIBRARIES}
|
||||||
SDL2
|
SDL2
|
||||||
OpenGL::GL
|
OpenGL::GL
|
||||||
|
@@ -29,5 +29,21 @@ target_sources(${DAWN_TARGET_NAME}
|
|||||||
# Subdirs
|
# Subdirs
|
||||||
add_subdirectory(assert)
|
add_subdirectory(assert)
|
||||||
add_subdirectory(console)
|
add_subdirectory(console)
|
||||||
|
add_subdirectory(display)
|
||||||
add_subdirectory(engine)
|
add_subdirectory(engine)
|
||||||
add_subdirectory(time)
|
add_subdirectory(time)
|
||||||
|
|
||||||
|
# Platform-specific settings
|
||||||
|
if(DAWN_TARGET_SYSTEM STREQUAL "linux")
|
||||||
|
target_compile_definitions(${DAWN_TARGET_NAME}
|
||||||
|
PRIVATE
|
||||||
|
DAWN_SDL2=1
|
||||||
|
DAWN_SDL2_GAMEPAD=1
|
||||||
|
)
|
||||||
|
elseif(DAWN_TARGET_SYSTEM STREQUAL "psp")
|
||||||
|
target_compile_definitions(${DAWN_TARGET_NAME}
|
||||||
|
PRIVATE
|
||||||
|
DAWN_SDL2=1
|
||||||
|
DAWN_SDL2_GAMEPAD=1
|
||||||
|
)
|
||||||
|
endif()
|
@@ -4,7 +4,7 @@
|
|||||||
# https://opensource.org/licenses/MIT
|
# https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
# Sources
|
# Sources
|
||||||
target_sources(${DUSK_TARGET_NAME}
|
target_sources(${DAWN_TARGET_NAME}
|
||||||
PRIVATE
|
PRIVATE
|
||||||
Console.cpp
|
Console.cpp
|
||||||
)
|
)
|
@@ -37,6 +37,14 @@ extern "C" {
|
|||||||
typedef char char_t;
|
typedef char char_t;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if DAWN_SDL2
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
|
||||||
|
#define GL_GLEXT_PROTOTYPES
|
||||||
|
#include <GL/gl.h>
|
||||||
|
#include <GL/glext.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
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);
|
||||||
|
};
|
||||||
|
}
|
@@ -4,7 +4,7 @@
|
|||||||
# https://opensource.org/licenses/MIT
|
# https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
# Sources
|
# Sources
|
||||||
target_sources(${DUSK_TARGET_NAME}
|
target_sources(${DAWN_TARGET_NAME}
|
||||||
PRIVATE
|
PRIVATE
|
||||||
Engine.cpp
|
Engine.cpp
|
||||||
)
|
)
|
@@ -7,25 +7,33 @@
|
|||||||
|
|
||||||
using namespace Dawn;
|
using namespace Dawn;
|
||||||
|
|
||||||
|
std::shared_ptr<Engine> Engine::instance = nullptr;
|
||||||
|
|
||||||
|
std::shared_ptr<Engine> Engine::getInstance() {
|
||||||
|
if(!Engine::instance) {
|
||||||
|
Engine::instance = std::make_shared<Engine>();
|
||||||
|
}
|
||||||
|
return Engine::instance;
|
||||||
|
}
|
||||||
|
|
||||||
Engine::Engine() :
|
Engine::Engine() :
|
||||||
time(),
|
time(),
|
||||||
console()
|
console(),
|
||||||
|
display()
|
||||||
{
|
{
|
||||||
// console.registerCommand("echo", [](std::vector<std::string> &args) {
|
console.registerCommand("exit", [this](std::vector<std::string> &args) {
|
||||||
// if(args.size() == 0) {
|
this->exitRequested = true;
|
||||||
// std::cout << "Usage: echo <message>" << std::endl;
|
});
|
||||||
// return;
|
}
|
||||||
// }
|
|
||||||
|
|
||||||
// std::cout << args[0] << std::endl;
|
bool_t Engine::isExitRequested() const {
|
||||||
// });
|
return this->exitRequested;
|
||||||
|
|
||||||
// console.exec("echo");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Engine::update(void) {
|
void Engine::update(void) {
|
||||||
time.update();
|
time.update();
|
||||||
console.update();
|
console.update();
|
||||||
|
display.update();
|
||||||
}
|
}
|
||||||
|
|
||||||
Engine::~Engine() {
|
Engine::~Engine() {
|
||||||
|
@@ -6,18 +6,39 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "time/Time.hpp"
|
#include "time/Time.hpp"
|
||||||
#include "console/Console.hpp"
|
#include "console/Console.hpp"
|
||||||
|
#include "display/Display.hpp"
|
||||||
|
|
||||||
namespace Dawn {
|
namespace Dawn {
|
||||||
struct Engine {
|
struct Engine {
|
||||||
|
private:
|
||||||
|
static std::shared_ptr<Engine> instance;
|
||||||
|
|
||||||
|
bool_t exitRequested = false;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Time time;
|
Time time;
|
||||||
Console console;
|
Console console;
|
||||||
|
Display display;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the singleton instance of the engine.
|
||||||
|
*
|
||||||
|
* @return A shared pointer to the engine instance.
|
||||||
|
*/
|
||||||
|
static std::shared_ptr<Engine> getInstance();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor for the Dawn engine.
|
* Constructor for the Dawn engine.
|
||||||
*/
|
*/
|
||||||
Engine();
|
Engine();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if an exit has been requested.
|
||||||
|
*
|
||||||
|
* @return True if an exit has been requested, false otherwise.
|
||||||
|
*/
|
||||||
|
bool_t isExitRequested() const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update the engine state.
|
* Update the engine state.
|
||||||
*/
|
*/
|
||||||
|
@@ -8,16 +8,12 @@
|
|||||||
|
|
||||||
using namespace Dawn;
|
using namespace Dawn;
|
||||||
|
|
||||||
static std::shared_ptr<Engine> engine;
|
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
engine = std::make_shared<Engine>();
|
auto engine = Engine::getInstance();
|
||||||
|
|
||||||
while(1) {
|
while(!engine->isExitRequested()) {
|
||||||
engine->update();
|
engine->update();
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
engine = nullptr;
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
@@ -20,10 +20,10 @@ Time::Time(void) :
|
|||||||
void Time::update(void) {
|
void Time::update(void) {
|
||||||
float_t delta;
|
float_t delta;
|
||||||
|
|
||||||
#if TIME_SDL2
|
#if DAWN_SDL2
|
||||||
delta = (float_t)SDL_GetTicks() / 1000.0f - this->time;
|
delta = (float_t)SDL_GetTicks() / 1000.0f - this->time;
|
||||||
#else
|
#else
|
||||||
|
#error "No time platform defined"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
this->delta = delta;
|
this->delta = delta;
|
||||||
|
Reference in New Issue
Block a user