sdl back
This commit is contained in:
@@ -6,5 +6,12 @@
|
||||
# Sources
|
||||
target_sources(${DUSK_TARGET_NAME}
|
||||
PRIVATE
|
||||
render.c
|
||||
)
|
||||
display.c
|
||||
)
|
||||
|
||||
if(DUSK_TARGET_SYSTEM STREQUAL "linux")
|
||||
target_compile_definitions(${DUSK_TARGET_NAME}
|
||||
PRIVATE
|
||||
DUSK_DISPLAY_SDL2=1
|
||||
)
|
||||
endif()
|
96
src/display/display.c
Normal file
96
src/display/display.c
Normal file
@@ -0,0 +1,96 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "display/display.h"
|
||||
#include "console/console.h"
|
||||
|
||||
display_t DISPLAY;
|
||||
|
||||
errorret_t displayInit(void) {
|
||||
#if DUSK_DISPLAY_SDL2
|
||||
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER) != 0) {
|
||||
errorThrow("SDL Failed to Initialize: %s", SDL_GetError());
|
||||
}
|
||||
|
||||
// Set OpenGL attributes (Needs to be done now or later?)
|
||||
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
||||
|
||||
// Create window with OpenGL flag.
|
||||
DISPLAY.window = SDL_CreateWindow(
|
||||
"DuskSDL2",
|
||||
SDL_WINDOWPOS_UNDEFINED,
|
||||
SDL_WINDOWPOS_UNDEFINED,
|
||||
DISPLAY_WINDOW_WIDTH_DEFAULT,
|
||||
DISPLAY_WINDOW_HEIGHT_DEFAULT,
|
||||
SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI |
|
||||
SDL_WINDOW_OPENGL
|
||||
);
|
||||
if(!DISPLAY.window) {
|
||||
errorThrow("SDL_CreateWindow failed: %s", SDL_GetError());
|
||||
}
|
||||
|
||||
// Create OpenGL context
|
||||
DISPLAY.glContext = SDL_GL_CreateContext(DISPLAY.window);
|
||||
if(!DISPLAY.glContext) {
|
||||
errorThrow("SDL_GL_CreateContext failed: %s", SDL_GetError());
|
||||
}
|
||||
|
||||
SDL_GL_SetSwapInterval(1);
|
||||
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_UNPACK_ALIGNMENT, 1);
|
||||
|
||||
glEnableClientState(GL_COLOR_ARRAY);// To confirm: every frame on PSP?
|
||||
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||
glEnableClientState(GL_VERTEX_ARRAY);
|
||||
#endif
|
||||
|
||||
// For now, we just return an OK error.
|
||||
errorOk();
|
||||
}
|
||||
|
||||
errorret_t displayUpdate(void) {
|
||||
#if DUSK_DISPLAY_SDL2
|
||||
SDL_Event event;
|
||||
while(SDL_PollEvent(&event)) {
|
||||
switch(event.type) {
|
||||
case SDL_QUIT:
|
||||
consoleExec("quit");
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
SDL_GL_SwapWindow(DISPLAY.window);
|
||||
#endif
|
||||
|
||||
// For now, we just return an OK error.
|
||||
errorOk();
|
||||
}
|
||||
|
||||
errorret_t displayDispose(void) {
|
||||
#if DUSK_DISPLAY_SDL2
|
||||
if(DISPLAY.glContext) {
|
||||
SDL_GL_DeleteContext(DISPLAY.glContext);
|
||||
DISPLAY.glContext = NULL;
|
||||
}
|
||||
if(DISPLAY.window) {
|
||||
SDL_DestroyWindow(DISPLAY.window);
|
||||
DISPLAY.window = NULL;
|
||||
}
|
||||
SDL_Quit();
|
||||
#endif
|
||||
|
||||
// For now, we just return an OK error.
|
||||
errorOk();
|
||||
}
|
54
src/display/display.h
Normal file
54
src/display/display.h
Normal file
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "error/error.h"
|
||||
|
||||
#if DUSK_DISPLAY_SDL2
|
||||
#include <SDL2/SDL.h>
|
||||
#define GL_GLEXT_PROTOTYPES
|
||||
#include <GL/gl.h>
|
||||
#include <GL/glext.h>
|
||||
#endif
|
||||
|
||||
#ifndef DISPLAY_WIDTH
|
||||
#define DISPLAY_WIDTH 320
|
||||
#endif
|
||||
#ifndef DISPLAY_HEIGHT
|
||||
#define DISPLAY_HEIGHT 240
|
||||
#endif
|
||||
|
||||
#ifndef DISPLAY_WINDOW_WIDTH_DEFAULT
|
||||
#define DISPLAY_WINDOW_WIDTH_DEFAULT DISPLAY_WIDTH
|
||||
#endif
|
||||
#ifndef DISPLAY_WINDOW_HEIGHT_DEFAULT
|
||||
#define DISPLAY_WINDOW_HEIGHT_DEFAULT DISPLAY_HEIGHT
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
#if DUSK_DISPLAY_SDL2
|
||||
SDL_Window *window;
|
||||
SDL_GLContext glContext;
|
||||
#endif
|
||||
} display_t;
|
||||
|
||||
extern display_t DISPLAY;
|
||||
|
||||
/**
|
||||
* Initializes the display system.
|
||||
*/
|
||||
errorret_t displayInit(void);
|
||||
|
||||
/**
|
||||
* Tells the display system to actually draw the frame.
|
||||
*/
|
||||
errorret_t displayUpdate(void);
|
||||
|
||||
/**
|
||||
* Disposes of the display system.
|
||||
*/
|
||||
errorret_t displayDispose(void);
|
@@ -1,8 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "display/render.h"
|
@@ -1,31 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "error/error.h"
|
||||
|
||||
#ifndef RENDER_WIDTH
|
||||
#define RENDER_WIDTH 320
|
||||
#endif
|
||||
#ifndef RENDER_HEIGHT
|
||||
#define RENDER_HEIGHT 240
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Initializes the rendering system.
|
||||
*/
|
||||
errorret_t renderInit(void);
|
||||
|
||||
/**
|
||||
* Tells the rendering system to actually draw the frame.
|
||||
*/
|
||||
errorret_t renderDraw(void);
|
||||
|
||||
/**
|
||||
* Disposes of the rendering system.
|
||||
*/
|
||||
errorret_t renderDispose(void);
|
Reference in New Issue
Block a user