diff --git a/CMakeLists.txt b/CMakeLists.txt index c0c9fdc..7577a71 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,8 +10,8 @@ set(CMAKE_C_STANDARD_REQUIRED ON) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules) if(NOT DEFINED DUSK_TARGET_SYSTEM) - set(DUSK_TARGET_SYSTEM "linux") - # set(DUSK_TARGET_SYSTEM "psp") + # set(DUSK_TARGET_SYSTEM "linux") + set(DUSK_TARGET_SYSTEM "psp") endif() # Prep cache diff --git a/assets/CMakeLists.txt b/assets/CMakeLists.txt index 1ab8aeb..7136e37 100644 --- a/assets/CMakeLists.txt +++ b/assets/CMakeLists.txt @@ -7,4 +7,5 @@ add_asset(PALETTE first.palette.png) add_asset(IMAGE font_minogram.png type=ALPHA) add_asset(IMAGE entities.png type=PALETTIZED) add_asset(CONFIG init.dcf) +add_asset(CONFIG init_psp.dcf) # add_asset(TILESET entities.tsx) \ No newline at end of file diff --git a/assets/font_minogram.png b/assets/font_minogram.png index 6aa3b65..c41f917 100644 Binary files a/assets/font_minogram.png and b/assets/font_minogram.png differ diff --git a/assets/init.dcf b/assets/init.dcf index 5e13f81..714d2c7 100644 --- a/assets/init.dcf +++ b/assets/init.dcf @@ -1,4 +1,5 @@ echo " = Dawn Init = "; + bind ` toggleconsole; bind w up; bind s down; diff --git a/assets/init_psp.dcf b/assets/init_psp.dcf new file mode 100644 index 0000000..18162c5 --- /dev/null +++ b/assets/init_psp.dcf @@ -0,0 +1,17 @@ +echo " = Dawn PSP = "; + +bind up up; +bind down down; +bind left left; +bind right right; +bind cross accept; +bind circle cancel; +bind select toggleconsole; +bind start toggleconsole; + +bind lstick_negative_y up; +bind lstick_positive_y down; +bind lstick_negative_x left; +bind lstick_positive_x right; + +fps 1; \ No newline at end of file diff --git a/src/console/consoledefs.h b/src/console/consoledefs.h index 6c66c92..3919046 100644 --- a/src/console/consoledefs.h +++ b/src/console/consoledefs.h @@ -14,7 +14,7 @@ #define CONSOLE_VARIABLES_MAX 128 #define CONSOLE_LINE_MAX 256 #define CONSOLE_HISTORY_MAX 32 -#define CONSOLE_EXEC_BUFFER_MAX 16 +#define CONSOLE_EXEC_BUFFER_MAX 32 #define CONSOLE_ALIAS_MAX 32 #define CONSOLE_VAR_NAME_MAX 32 diff --git a/src/display/display.c b/src/display/display.c index 371df97..8e3315d 100644 --- a/src/display/display.c +++ b/src/display/display.c @@ -18,8 +18,8 @@ display_t DISPLAY; errorret_t displayInit(void) { #if DISPLAY_SDL2 uint32_t flags = SDL_INIT_VIDEO; - #if INPUT_SUPPORT_GAMEPAD - flags |= SDL_INIT_GAMECONTROLLER; + #if INPUT_GAMEPAD == 1 + flags |= SDL_INIT_GAMECONTROLLER | SDL_INIT_JOYSTICK; #endif if(SDL_Init(flags) != 0) { errorThrow("SDL Failed to Initialize: %s", SDL_GetError()); diff --git a/src/display/scene/overworld/sceneoverworld.c b/src/display/scene/overworld/sceneoverworld.c index 4069dbb..3a9d955 100644 --- a/src/display/scene/overworld/sceneoverworld.c +++ b/src/display/scene/overworld/sceneoverworld.c @@ -63,8 +63,8 @@ void sceneOverworldRenderMap(const map_t *map) { // Draw base layer // Draw entities - entity_t *start = &map->entities[0]; - entity_t *end = &map->entities[map->entityCount]; + const entity_t *start = &map->entities[0]; + const entity_t *end = &map->entities[map->entityCount]; while(start < end) { // Render entity here. sceneOverworldRenderEntity(start); diff --git a/src/display/ui/ui.c b/src/display/ui/ui.c index 83d26be..675da34 100644 --- a/src/display/ui/ui.c +++ b/src/display/ui/ui.c @@ -54,7 +54,7 @@ void uiRender(void) { cameraPushMatrix(&UI.camera); uiConsoleRender(); - uiFPSRender(); + // uiFPSRender(); spriteBatchFlush(); cameraPopMatrix(); diff --git a/src/display/ui/uiconsole.c b/src/display/ui/uiconsole.c index 220c858..d28ce3f 100644 --- a/src/display/ui/uiconsole.c +++ b/src/display/ui/uiconsole.c @@ -11,16 +11,17 @@ void uiConsoleRender(void) { if(!CONSOLE.visible) return; + consolePrint("Test\n"); - int32_t i = 0; + int32_t i = CONSOLE_HISTORY_MAX - 1; char_t *line; do { line = CONSOLE.line[i]; if(line[0] == '\0') { - i++; + i--; continue; } uiTextDraw(0, i * UI_TEXT_TILE_HEIGHT, line, COLOR_WHITE); - i++; - } while(i < CONSOLE_HISTORY_MAX); + i--; + } while(i > 0); } \ No newline at end of file diff --git a/src/display/ui/uifps.c b/src/display/ui/uifps.c index 2fa8119..251989e 100644 --- a/src/display/ui/uifps.c +++ b/src/display/ui/uifps.c @@ -12,16 +12,15 @@ #include "util/string.h" void uiFPSInit(void) { - consoleRegVar("fps", "1", NULL); + consoleRegVar("fps", "0", NULL); } void uiFPSRender(void) { - float_t fps = TIME.delta > 0.0f ? (1.0f / TIME.delta) : 0.0f; - if(stringCompare(consoleVarGet("fps")->value, "0") == 0) { return; } + float_t fps = TIME.delta > 0.0f ? (1.0f / TIME.delta) : 0.0f; char_t buffer[64]; snprintf( buffer, diff --git a/src/engine/engine.c b/src/engine/engine.c index d252da4..f89312d 100644 --- a/src/engine/engine.c +++ b/src/engine/engine.c @@ -31,7 +31,12 @@ errorret_t engineInit(void) { errorChain(displayInit()); rpgInit(); - consoleExec("exec init.dcf"); + // Init scripts + #if PSP + consoleExec("exec init_psp.dcf"); + #else + consoleExec("exec init.dcf"); + #endif errorOk(); } diff --git a/src/input/CMakeLists.txt b/src/input/CMakeLists.txt index 6054d57..1426f89 100644 --- a/src/input/CMakeLists.txt +++ b/src/input/CMakeLists.txt @@ -22,7 +22,7 @@ if(DUSK_TARGET_SYSTEM STREQUAL "linux") elseif(DUSK_TARGET_SYSTEM STREQUAL "psp") target_compile_definitions(${DUSK_TARGET_NAME} PRIVATE - INPUT_SDL=1 + INPUT_SDL2=1 INPUT_GAMEPAD=1 ) endif() \ No newline at end of file diff --git a/src/input/input.c b/src/input/input.c index 06b002b..a9b6ce2 100644 --- a/src/input/input.c +++ b/src/input/input.c @@ -22,16 +22,23 @@ void inputInit(void) { INPUT.actions[i].currentValue = 0.0f; } - stringCopy(&INPUT_BUTTON_DATA[0].command[0], "echo \"test\";", CONSOLE_LINE_MAX); - INPUT_BUTTON_DATA[0].actionType = INPUT_BUTTON_ACTION_TYPE_COMMAND; - - INPUT_BUTTON_DATA[1].actionType = INPUT_BUTTON_ACTION_TYPE_ACTION; - INPUT_BUTTON_DATA[1].action = INPUT_ACTION_UP; + INPUT.deadzone = 0.1f; } void inputUpdate(void) { #if INPUT_SDL2 == 1 - INPUT.keyboardState = SDL_GetKeyboardState(NULL); + #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 #endif // Reset all actions diff --git a/src/input/input.h b/src/input/input.h index e66608b..a00747e 100644 --- a/src/input/input.h +++ b/src/input/input.h @@ -12,7 +12,15 @@ typedef struct { inputactiondata_t actions[INPUT_ACTION_COUNT]; + #if INPUT_GAMEPAD == 1 + float_t deadzone; + #endif + #if INPUT_SDL2 == 1 + #if INPUT_GAMEPAD == 1 + SDL_GameController *controller; + #endif + #if INPUT_KEYBOARD == 1 const uint8_t *keyboardState; #endif diff --git a/src/input/inputbutton.c b/src/input/inputbutton.c index a9e7523..99070b8 100644 --- a/src/input/inputbutton.c +++ b/src/input/inputbutton.c @@ -14,6 +14,23 @@ inputbuttondata_t INPUT_BUTTON_DATA[] = { #if INPUT_SDL2 == 1 #if INPUT_GAMEPAD == 1 #if PSP + { .name = "triangle", { .type = INPUT_BUTTON_TYPE_GAMEPAD, .gpButton = SDL_CONTROLLER_BUTTON_Y } }, + { .name = "cross", { .type = INPUT_BUTTON_TYPE_GAMEPAD, .gpButton = SDL_CONTROLLER_BUTTON_A } }, + { .name = "circle", { .type = INPUT_BUTTON_TYPE_GAMEPAD, .gpButton = SDL_CONTROLLER_BUTTON_B } }, + { .name = "square", { .type = INPUT_BUTTON_TYPE_GAMEPAD, .gpButton = SDL_CONTROLLER_BUTTON_X } }, + { .name = "start", { .type = INPUT_BUTTON_TYPE_GAMEPAD, .gpButton = SDL_CONTROLLER_BUTTON_START } }, + { .name = "select", { .type = INPUT_BUTTON_TYPE_GAMEPAD, .gpButton = SDL_CONTROLLER_BUTTON_BACK } }, + { .name = "up", { .type = INPUT_BUTTON_TYPE_GAMEPAD, .gpButton = SDL_CONTROLLER_BUTTON_DPAD_UP } }, + { .name = "down", { .type = INPUT_BUTTON_TYPE_GAMEPAD, .gpButton = SDL_CONTROLLER_BUTTON_DPAD_DOWN } }, + { .name = "left", { .type = INPUT_BUTTON_TYPE_GAMEPAD, .gpButton = SDL_CONTROLLER_BUTTON_DPAD_LEFT } }, + { .name = "right", { .type = INPUT_BUTTON_TYPE_GAMEPAD, .gpButton = SDL_CONTROLLER_BUTTON_DPAD_RIGHT } }, + { .name = "l", { .type = INPUT_BUTTON_TYPE_GAMEPAD, .gpButton = SDL_CONTROLLER_BUTTON_LEFTSHOULDER } }, + { .name = "r", { .type = INPUT_BUTTON_TYPE_GAMEPAD, .gpButton = SDL_CONTROLLER_BUTTON_RIGHTSHOULDER } }, + + { .name = "lstick_positive_x", { .type = INPUT_BUTTON_TYPE_GAMEPAD_AXIS, .gpAxis = { .axis = SDL_CONTROLLER_AXIS_LEFTX, .positive = true } } }, + { .name = "lstick_negative_x", { .type = INPUT_BUTTON_TYPE_GAMEPAD_AXIS, .gpAxis = { .axis = SDL_CONTROLLER_AXIS_LEFTX, .positive = false } } }, + { .name = "lstick_positive_y", { .type = INPUT_BUTTON_TYPE_GAMEPAD_AXIS, .gpAxis = { .axis = SDL_CONTROLLER_AXIS_LEFTY, .positive = true } } }, + { .name = "lstick_negative_y", { .type = INPUT_BUTTON_TYPE_GAMEPAD_AXIS, .gpAxis = { .axis = SDL_CONTROLLER_AXIS_LEFTY, .positive = false } } }, #else #endif #endif @@ -145,11 +162,12 @@ inputbuttondata_t INPUT_BUTTON_DATA[] = { inputbutton_t inputButtonGetByName(const char_t *name) { assertNotNull(name, "name must not be NULL"); - uint32_t len = sizeof(INPUT_BUTTON_DATA) / sizeof(inputbuttondata_t); - - for(uint32_t i = 0; i < len; i++) { - if(stringCompareInsensitive(INPUT_BUTTON_DATA[i].name, name) != 0) continue; - return INPUT_BUTTON_DATA[i].button; + inputbuttondata_t *data = INPUT_BUTTON_DATA; + while(data->name != NULL) { + if(stringCompareInsensitive(data->name, name) == 0) { + return data->button; + } + data++; } return (inputbutton_t){ .type = INPUT_BUTTON_TYPE_NONE }; @@ -170,12 +188,23 @@ float_t inputButtonGetValue(const inputbutton_t button) { #if INPUT_GAMEPAD == 1 case INPUT_BUTTON_TYPE_GAMEPAD: { #if INPUT_SDL2 == 1 - if(SDL_GameControllerGetButton( - SDL_GameControllerFromInstanceID(0), button.gpButton - )) return 1.0f; + if(SDL_GameControllerGetButton(INPUT.controller, button.gpButton)) { + return 1.0f; + } #endif return 0.0f; } + + case INPUT_BUTTON_TYPE_GAMEPAD_AXIS: { + #if INPUT_SDL2 == 1 + Sint16 axis = SDL_GameControllerGetAxis(INPUT.controller, button.gpAxis.axis); + if(!button.gpAxis.positive) axis = -axis; + float_t value = (float_t)axis / 32767.0f; + // Deadzone + if(value < INPUT.deadzone) return 0.0f; + return value; + #endif + } #endif default: { diff --git a/src/input/inputbutton.h b/src/input/inputbutton.h index 06eafa3..09f82b1 100644 --- a/src/input/inputbutton.h +++ b/src/input/inputbutton.h @@ -26,6 +26,7 @@ #if INPUT_GAMEPAD == 1 #if INPUT_SDL2 == 1 typedef SDL_GameControllerButton inputgamepadbutton_t; + typedef SDL_GameControllerAxis inputgamepadaxis_t; #endif #endif @@ -37,6 +38,7 @@ typedef enum { #endif #if INPUT_GAMEPAD == 1 INPUT_BUTTON_TYPE_GAMEPAD, + INPUT_BUTTON_TYPE_GAMEPAD_AXIS, #endif INPUT_BUTTON_TYPE_COUNT @@ -48,6 +50,10 @@ typedef struct { union { #if INPUT_GAMEPAD == 1 inputgamepadbutton_t gpButton; + struct { + inputgamepadaxis_t axis; + bool_t positive; + } gpAxis; #endif #if INPUT_KEYBOARD == 1