Skip to content

Commit

Permalink
Initial port to SDL3
Browse files Browse the repository at this point in the history
The HiDPI scaling is not working as expected and all textures now
default to SDL_SCALEMODE_NEAREST when loaded.
  • Loading branch information
LiquidityC committed Nov 2, 2024
1 parent b3a3fb3 commit ec83b7e
Show file tree
Hide file tree
Showing 50 changed files with 441 additions and 434 deletions.
109 changes: 55 additions & 54 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,61 +95,62 @@ ELSEIF ( WIN32 )
"${CMAKE_C_FLAGS_RELEASE} -mwindows"
)
ENDIF ()
#IF ( GCC )
#SET (
#CMAKE_C_FLAGS_DEBUG
#"${CMAKE_C_FLAGS_DEBUG} -fsanitize=address -fno-omit-frame-pointer"
#)
#ENDIF ( GCC )
IF ( GCC )
SET (
CMAKE_C_FLAGS_DEBUG
"${CMAKE_C_FLAGS_DEBUG} -fsanitize=address -fno-omit-frame-pointer"
)
ENDIF ( GCC )

if (STEAM)
include_directories(
lib/steamworks_c_wrapper/src
)
endif ()

# Include SDL2
# Include SDL
set(SDL_STATIC ON)
set(SDL_SHARED OFF)
set(SDL_TEST OFF)
add_subdirectory(lib/SDL)
set(SDL2_INCLUDE_DIR lib/SDL/include)
set(SDL2_LIBRARY SDL2-static)
set(SDL2_MAIN_LIBRARY SDL2main)
set(SDL_INCLUDE_DIR lib/SDL/include)
set(SDL_LIBRARY SDL3-static)
set(SDL_MAIN_LIBRARY SDLmain)

# Include SDL2_image/mixer/ttf
# Include SDL_image/mixer/ttf
set(BUILD_SHARED_LIBS OFF)

set(SDL2IMAGE_PNG ON)
set(SDL2IMAGE_JPG ON)
set(SDL2IMAGE_AVIF OFF)
set(SDL2IMAGE_BMP OFF)
set(SDL2IMAGE_GIF OFF)
set(SDL2IMAGE_JXL OFF)
set(SDL2IMAGE_LBM OFF)
set(SDL2IMAGE_PCX OFF)
set(SDL2IMAGE_PNM OFF)
set(SDL2IMAGE_QOI OFF)
set(SDL2IMAGE_SVG OFF)
set(SDL2IMAGE_TGA OFF)
set(SDL2IMAGE_TIF OFF)
set(SDL2IMAGE_WEBP OFF)
set(SDL2IMAGE_XCF OFF)
set(SDL2IMAGE_XPM OFF)
set(SDL2IMAGE_XV OFF)
set(SDLIMAGE_PNG ON)
set(SDLIMAGE_JPG ON)
set(SDLIMAGE_AVIF OFF)
set(SDLIMAGE_BMP OFF)
set(SDLIMAGE_GIF OFF)
set(SDLIMAGE_JXL OFF)
set(SDLIMAGE_LBM OFF)
set(SDLIMAGE_PCX OFF)
set(SDLIMAGE_PNM OFF)
set(SDLIMAGE_QOI OFF)
set(SDLIMAGE_SVG OFF)
set(SDLIMAGE_TGA OFF)
set(SDLIMAGE_TIF OFF)
set(SDLIMAGE_WEBP OFF)
set(SDLIMAGE_XCF OFF)
set(SDLIMAGE_XPM OFF)
set(SDLIMAGE_XV OFF)
add_subdirectory(lib/SDL_image)
set(SDL2_IMAGE_INCLUDE_DIR lib/SDL_image/include)
set(SDL2_IMAGE_LIBRARY SDL2_image)
set(SDL_IMAGE_INCLUDE_DIR lib/SDL_image/include)
set(SDL_IMAGE_LIBRARY SDL3_image-static)

# opusfile caused issues in macos build and we don't need it anyways
set(SDL2MIXER_OPUS OFF)
set(SDLMIXER_OPUS OFF)
add_subdirectory(lib/SDL_mixer)
set(SDL2_MIXER_INCLUDE_DIR lib/SDL_mixer/include)
set(SDL2_MIXER_LIBRARY SDL2_mixer)
set(SDL_MIXER_INCLUDE_DIR lib/SDL_mixer/include)
set(SDL_MIXER_LIBRARY SDL3_mixer-static)

set(SDLTTF_VENDORED ON)
add_subdirectory(lib/SDL_ttf)
set(SDL2_TTF_INCLUDE_DIR lib/SDL_ttf/include)
set(SDL2_TTF_LIBRARY SDL2_ttf)
set(SDL_TTF_INCLUDE_DIR lib/SDL_ttf/include)
set(SDL_TTF_LIBRARY SDL3_ttf-static)

# Include lua lib
add_subdirectory(lib/lua-5.3.5)
Expand All @@ -164,19 +165,6 @@ include_directories(lib/physfs-3.0/src)
set(PHYSFS_LIBRARY physfs-static)
set(PHYSFS_INCLUDE_DIR physfs-3.0/src)

include_directories(
${PROJECT_BINARY_DIR}
${SDL2_INCLUDE_DIR}
${SDL2_IMAGE_INCLUDE_DIR}
${SDL2_TTF_INCLUDE_DIR}
${SDL2_MIXER_INCLUDE_DIR}
${PHYSFS_INCLUDE_DIR}
${LUA_INCLUDE_DIR}
lib/sqlite3
lib/bh_random/src
lib/checksum/src
)

if (CMOCKA_FOUND)
include_directories(
${CMOCKA_INCLUDE_DIR}
Expand Down Expand Up @@ -278,6 +266,19 @@ add_executable(breakhack
${STEAM_SOURCES}
)

include_directories(
${PROJECT_BINARY_DIR}
${SDL_INCLUDE_DIR}
${SDL_IMAGE_INCLUDE_DIR}
${SDL_TTF_INCLUDE_DIR}
${SDL_MIXER_INCLUDE_DIR}
${PHYSFS_INCLUDE_DIR}
${LUA_INCLUDE_DIR}
lib/sqlite3
lib/bh_random/src
lib/checksum/src
)

if (MSVC)
source_group("Header files" REGULAR_EXPRESSION ".*\\.h")
source_group("Source files" REGULAR_EXPRESSION ".*\\.c")
Expand All @@ -288,10 +289,10 @@ set_source_files_properties(lib/sqlite3/sqlite3.c COMPILE_FLAGS -w)

target_link_libraries(breakhack
${CMAKE_DL_LIBS} # Sqlite needs DL libs
${SDL2_LIBRARY}
${SDL2_IMAGE_LIBRARY}
${SDL2_TTF_LIBRARY}
${SDL2_MIXER_LIBRARY}
${SDL_LIBRARY}
${SDL_IMAGE_LIBRARY}
${SDL_TTF_LIBRARY}
${SDL_MIXER_LIBRARY}
${LUA_LIBRARIES}
${PHYSFS_LIBRARY}
bh_random
Expand All @@ -300,7 +301,7 @@ target_link_libraries(breakhack

if (MSVC OR MINGW)
target_link_libraries(breakhack
${SDL2_MAIN_LIBRARY}
${SDL_MAIN_LIBRARY}
)
endif ()

Expand Down Expand Up @@ -339,7 +340,7 @@ IF (CMOCKA_FOUND AND NOT OSX AND NOT CLANG)
add_executable(test_input test/test_input.c src/input.c src/keyboard.c)
target_link_libraries(test_input
${CMOCKA_LIBRARY}
${SDL2_LIBRARY}
${SDL_LIBRARY}
)
#set_target_properties(test_input PROPERTIES
#LINK_FLAGS "-Wl,--wrap,keyboard_direction_press -Wl,--wrap,keyboard_press")
Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ install:

setup:
@mkdir -p build/debug
@mkdir -p build/release
@mkdir -p build/win-release
@cmake -B build/debug -DCMAKE_BUILD_TYPE=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS=YES -GNinja
@mkdir -p build/release
@cmake -B build/release -DCMAKE_BUILD_TYPE=Release -GNinja
@mkdir -p build/win-release
@cmake -B build/win-release \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_TOOLCHAIN_FILE=build_deps/toolchains/mingw-w64-i686.cmake \
-DSDL2MIXER_VENDORED=ON \
-DSDL2TTF_VENDORED=ON \
-DSDLMIXER_VENDORED=ON \
-DSDLTTF_VENDORED=ON \
-GNinja
@ln -fs build/debug/compile_commands.json
@echo "Setup complete"
Expand Down
2 changes: 1 addition & 1 deletion lib/SDL
Submodule SDL updated 2353 files
2 changes: 1 addition & 1 deletion lib/SDL_image
Submodule SDL_image updated 399 files
2 changes: 1 addition & 1 deletion lib/SDL_mixer
Submodule SDL_mixer updated 400 files
2 changes: 1 addition & 1 deletion lib/SDL_ttf
Submodule SDL_ttf updated 348 files
2 changes: 1 addition & 1 deletion src/actiontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#ifndef ACTIONTEXT_H_
#define ACTIONTEXT_H_

#include <SDL.h>
#include <SDL3/SDL.h>
#include <stdbool.h>

#include "position.h"
Expand Down
2 changes: 1 addition & 1 deletion src/actiontextbuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#ifndef ACTIONTEXTBUILDER_H_
#define ACTIONTEXTBUILDER_H_

#include <SDL.h>
#include <SDL3/SDL.h>
#include "actiontext.h"
#include "camera.h"
#include "update_data.h"
Expand Down
2 changes: 1 addition & 1 deletion src/animation.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#ifndef _ANIMATION_H
#define _ANIMATION_H

#include <SDL.h>
#include <SDL3/SDL.h>
#include <stdbool.h>

typedef struct Timer Timer;
Expand Down
2 changes: 1 addition & 1 deletion src/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#ifndef CAMERA_H_
#define CAMERA_H_

#include <SDL.h>
#include <SDL3/SDL.h>

#include "position.h"
#include "timer.h"
Expand Down
2 changes: 1 addition & 1 deletion src/collisions.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#ifndef COLLISIONS_H_
#define COLLISIONS_H_

#include <SDL.h>
#include <SDL3/SDL.h>
#include <stdbool.h>

#include "position.h"
Expand Down
26 changes: 12 additions & 14 deletions src/gamecontroller.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
#include "gamecontroller.h"
#include "util.h"

static SDL_GameController *controller = NULL;
static SDL_Gamepad *controller = NULL;
static SDL_Haptic *haptic = NULL;
static Uint8 controllerMode = 0;

void
gamecontroller_set(SDL_GameController *ctrler)
gamecontroller_set(SDL_Gamepad *ctrler)
{
controller = ctrler;

const char *ctrlName = SDL_GameControllerName(controller);
const char *ctrlName = SDL_GetGamepadName(controller);
info("Game controller connected: %s", ctrlName);

// Try to determine if this is a PS3/4 controller
Expand All @@ -39,17 +39,15 @@ gamecontroller_set(SDL_GameController *ctrler)
else
controllerMode = 1;

haptic = SDL_HapticOpenFromJoystick(SDL_GameControllerGetJoystick(controller));
haptic = SDL_OpenHapticFromJoystick(SDL_GetGamepadJoystick(controller));
if (haptic) {
info("Haptics are supported by controller: %s", ctrlName);
if (SDL_HapticRumbleInit(haptic) >= 0) {
if (SDL_InitHapticRumble(haptic)) {
info("Haptics enabled for controller: %s", ctrlName);
}
else {
} else {
info("Failed to enable haptics for: %s", ctrlName);
}
}
else {
} else {
info("Haptics not supported by controller: %s", ctrlName);
}
}
Expand All @@ -60,21 +58,21 @@ gamecontroller_rumble(float intensity, Uint32 duration)
if (!haptic)
return;

if (SDL_HapticRumblePlay(haptic, intensity, duration) != 0)
if (SDL_PlayHapticRumble(haptic, intensity, duration) != 0)
error("Failed to play rumble: %s", SDL_GetError());
}

Uint8
gamecontroller_mode()
gamecontroller_mode(void)
{
return controllerMode;
}

void
gamecontroller_close()
gamecontroller_close(void)
{
if (controller)
SDL_GameControllerClose(controller);
SDL_CloseGamepad(controller);
if (haptic)
SDL_HapticClose(haptic);
SDL_CloseHaptic(haptic);
}
6 changes: 3 additions & 3 deletions src/gamecontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@

#pragma once

#include <SDL.h>
#include <SDL3/SDL.h>

typedef struct GameController {
SDL_GameController *controller;
SDL_Gamepad *controller;
unsigned int mode;
} GameController;

void
gamecontroller_set(SDL_GameController *controller);
gamecontroller_set(SDL_Gamepad *controller);

void
gamecontroller_rumble(float intensity, Uint32 duration);
Expand Down
2 changes: 1 addition & 1 deletion src/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#ifndef GLOBALS_H_
#define GLOBALS_H_

#include <SDL_ttf.h>
#include <SDL3/SDL3/SDL_ttf.h>

TTF_Font *gFontLarge = NULL;
TTF_Font *gFontSmall = NULL;
Expand Down
8 changes: 4 additions & 4 deletions src/gui.c
Original file line number Diff line number Diff line change
Expand Up @@ -457,20 +457,20 @@ gui_render_minimap(Gui *gui, Map *map, Camera *cam)
{
sprite_render(gui->miniMapFrame, cam);

SDL_Rect box = { 0, 0, 12, 8 };
SDL_FRect box = { 0.0f, 0.0f, 12.0f, 8.0f };
for (Uint8 i = 0; i < MAP_H_ROOM_COUNT; ++i) {
for (Uint8 j = 0; j < MAP_V_ROOM_COUNT; ++j) {
Room *room = map->rooms[i][j];
box.x = i*14 + 10;
box.y = j*10 + 14;
box.x = (float) i*14 + 10;
box.y = (float) j*10 + 14;
if (room && room->visited) {
if (map->currentRoom.x == i && map->currentRoom.y == j)
SDL_SetRenderDrawColor(cam->renderer, 0, 255, 255, 255);
else
SDL_SetRenderDrawColor(cam->renderer, 255, 255, 255, 255);
SDL_RenderFillRect(cam->renderer, &box);
SDL_SetRenderDrawColor(cam->renderer, 60, 134, 252, 255);
SDL_RenderDrawRect(cam->renderer, &box);
SDL_RenderRect(cam->renderer, &box);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/gui_button.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#ifndef GUI_BUTTON_H_
#define GUI_BUTTON_H_

#include <SDL.h>
#include <SDL3/SDL.h>
#include "pointer.h"
#include "sprite.h"
#include "linkedlist.h"
Expand Down
Loading

0 comments on commit ec83b7e

Please sign in to comment.