Technically working
This commit is contained in:
@@ -11,4 +11,5 @@ target_include_directories(${DUSK_LIBRARY_TARGET_NAME}
|
||||
|
||||
# Subdirs
|
||||
add_subdirectory(display)
|
||||
add_subdirectory(input)
|
||||
add_subdirectory(input)
|
||||
add_subdirectory(time)
|
||||
@@ -8,10 +8,11 @@
|
||||
#include "display/display.h"
|
||||
#include "engine/engine.h"
|
||||
#include "display/displaygl.h"
|
||||
#include "error/errorgl.h"
|
||||
|
||||
erroret_t displaySDL2Init(void) {
|
||||
errorret_t displaySDL2Init(void) {
|
||||
uint32_t flags = SDL_INIT_VIDEO;
|
||||
#if INPUT_GAMEPAD == 1
|
||||
#ifdef DUSK_INPUT_GAMEPAD
|
||||
flags |= SDL_INIT_GAMECONTROLLER | SDL_INIT_JOYSTICK;
|
||||
#endif
|
||||
if(SDL_Init(flags) != 0) {
|
||||
@@ -26,8 +27,8 @@ erroret_t displaySDL2Init(void) {
|
||||
"Dusk",
|
||||
SDL_WINDOWPOS_UNDEFINED,
|
||||
SDL_WINDOWPOS_UNDEFINED,
|
||||
DUSK_WINDOW_WIDTH_DEFAULT,
|
||||
DUSK_WINDOW_HEIGHT_DEFAULT,
|
||||
DUSK_DISPLAY_WIDTH_DEFAULT,
|
||||
DUSK_DISPLAY_HEIGHT_DEFAULT,
|
||||
SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI |
|
||||
SDL_WINDOW_OPENGL
|
||||
);
|
||||
@@ -48,12 +49,12 @@ erroret_t displaySDL2Init(void) {
|
||||
|
||||
errorChain(displayOpenGLInit());
|
||||
|
||||
|
||||
// #if DUSK_PSP
|
||||
// errorChain(displayPSPInit());
|
||||
// #endif
|
||||
|
||||
errorChain(errorGLCheck());
|
||||
errorOk();
|
||||
}
|
||||
|
||||
errorret_t displaySDL2Update(void) {
|
||||
@@ -90,11 +91,7 @@ errorret_t displaySDL2Update(void) {
|
||||
|
||||
errorret_t displaySDL2Swap(void) {
|
||||
SDL_GL_SwapWindow(DISPLAY.window);
|
||||
|
||||
GLenum err;
|
||||
while((err = glGetError()) != GL_NO_ERROR) {
|
||||
debugPrint("GL Error: %d\n", err);
|
||||
}
|
||||
errorChain(errorGLCheck());
|
||||
errorOk();
|
||||
}
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
typedef inputpointeraxissdl2_t inputpointeraxisplatform_t;
|
||||
#endif
|
||||
|
||||
#define inputUpdatePlatform inputUpdateSDL2
|
||||
#define inputButtonGetValuePlatform inputButtonGetValueSDL2
|
||||
|
||||
typedef inputsdl2_t inputplatform_t;
|
||||
@@ -6,12 +6,40 @@
|
||||
*/
|
||||
|
||||
#include "input/input.h"
|
||||
#include "assert/assert.h"
|
||||
|
||||
void inputUpdateSDL2(void) {
|
||||
#if INPUT_GAMEPAD == 1
|
||||
INPUT.controller = NULL;
|
||||
|
||||
for(int32_t i = 0; i < SDL_NumJoysticks(); i++) {
|
||||
if(!SDL_IsGameController(i)) continue;
|
||||
INPUT.controller = SDL_GameControllerOpen(i);
|
||||
if(INPUT.controller) break;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if INPUT_KEYBOARD == 1
|
||||
INPUT.keyboardState = SDL_GetKeyboardState(NULL);
|
||||
#endif
|
||||
|
||||
#if INPUT_POINTER == 1
|
||||
int pointerX, pointerY;
|
||||
SDL_GetMouseState(&pointerX, &pointerY);
|
||||
|
||||
int windowWidth, windowHeight;
|
||||
SDL_GetWindowSize(DISPLAY.window, &windowWidth, &windowHeight);
|
||||
|
||||
INPUT.mouseX = (float_t)pointerX / (float_t)windowWidth;
|
||||
INPUT.mouseY = (float_t)pointerY / (float_t)windowHeight;
|
||||
#endif
|
||||
}
|
||||
|
||||
float_t inputButtonGetValue(const inputbutton_t button) {
|
||||
switch(button.type) {
|
||||
#ifdef DUSK_INPUT_KEYBOARD
|
||||
case INPUT_BUTTON_TYPE_KEYBOARD: {
|
||||
return INPUT.keyboardState[button.scancode] ? 1.0f : 0.0f;
|
||||
return INPUT.platform.keyboardState[button.scancode] ? 1.0f : 0.0f;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -19,10 +47,10 @@ float_t inputButtonGetValue(const inputbutton_t button) {
|
||||
case INPUT_BUTTON_TYPE_POINTER: {
|
||||
switch(button.pointerAxis) {
|
||||
case INPUT_POINTER_AXIS_X:
|
||||
return INPUT.mouseX;
|
||||
return INPUT.platform.mouseX;
|
||||
|
||||
case INPUT_POINTER_AXIS_Y:
|
||||
return INPUT.mouseY;
|
||||
return INPUT.platform.mouseY;
|
||||
|
||||
default:
|
||||
assertUnreachable("Unknown pointer axis");
|
||||
@@ -33,7 +61,9 @@ float_t inputButtonGetValue(const inputbutton_t button) {
|
||||
|
||||
#ifdef DUSK_INPUT_GAMEPAD
|
||||
case INPUT_BUTTON_TYPE_GAMEPAD: {
|
||||
if(SDL_GameControllerGetButton(INPUT.controller, button.gpButton)) {
|
||||
if(SDL_GameControllerGetButton(
|
||||
INPUT.platform.controller, button.gpButton
|
||||
)) {
|
||||
return 1.0f;
|
||||
}
|
||||
return 0.0f;
|
||||
@@ -42,7 +72,9 @@ float_t inputButtonGetValue(const inputbutton_t button) {
|
||||
case INPUT_BUTTON_TYPE_GAMEPAD_AXIS: {
|
||||
float_t value = 0.0f;
|
||||
|
||||
Sint16 axis = SDL_GameControllerGetAxis(INPUT.controller, button.gpAxis.axis);
|
||||
Sint16 axis = SDL_GameControllerGetAxis(
|
||||
INPUT.platform.controller, button.gpAxis.axis
|
||||
);
|
||||
value = (float_t)axis / 32767.0f;
|
||||
|
||||
if(!button.gpAxis.positive) value = -value;
|
||||
|
||||
@@ -30,21 +30,24 @@ typedef struct inputbutton_s inputbutton_t;
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
#if INPUT_GAMEPAD == 1
|
||||
#ifdef DUSK_INPUT_GAMEPAD
|
||||
SDL_GameController *controller;
|
||||
#endif
|
||||
|
||||
#if INPUT_KEYBOARD == 1
|
||||
#ifdef DUSK_INPUT_KEYBOARD
|
||||
const uint8_t *keyboardState;
|
||||
#endif
|
||||
|
||||
#if INPUT_POINTER == 1
|
||||
#if INPUT_SDL2 == 1
|
||||
float_t mouseX, mouseY;
|
||||
#endif
|
||||
#ifdef DUSK_INPUT_POINTER
|
||||
float_t mouseX, mouseY;
|
||||
#endif
|
||||
} inputsdl2_t;
|
||||
|
||||
/**
|
||||
* Updates the input state for SDL2.
|
||||
*/
|
||||
void inputUpdateSDL2(void);
|
||||
|
||||
/**
|
||||
* Returns the input value (between 0 and 1) of the given button.
|
||||
*
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
# Copyright (c) 2026 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Sources
|
||||
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
||||
PUBLIC
|
||||
timesdl2.c
|
||||
)
|
||||
|
||||
# Subdirs
|
||||
@@ -0,0 +1,12 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "timesdl2.h"
|
||||
|
||||
#define timeTickPlatform timeTickSDL2
|
||||
#define timeGetDeltaPlatform timeGetDeltaSDL2
|
||||
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "timesdl2.h"
|
||||
|
||||
void timeTickSDL2(void) {
|
||||
TIME_TICKS_LAST_SDL2 = TIME_TICKS_SDL2;
|
||||
TIME_TICKS_SDL2 = SDL_GetTicks64();
|
||||
}
|
||||
|
||||
float_t timeGetDeltaSDL2(void) {
|
||||
return (float_t)(TIME_TICKS_SDL2 - TIME_TICKS_LAST_SDL2) / 1000.0f;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "dusk.h"
|
||||
|
||||
static uint64_t TIME_TICKS_SDL2 = 0;
|
||||
static uint64_t TIME_TICKS_LAST_SDL2 = 0;
|
||||
|
||||
/**
|
||||
* Requests SDL2 to update its internal game ticks.
|
||||
*/
|
||||
void timeTickSDL2(void);
|
||||
|
||||
/**
|
||||
* Gets the time delta for this tick in seconds.
|
||||
*
|
||||
* @return The time delta for this tick in seconds.
|
||||
*/
|
||||
float_t timeGetDeltaSDL2(void);
|
||||
Reference in New Issue
Block a user