Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial SDL2 port #241

Merged
merged 12 commits into from
May 6, 2024
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ jobs:
run: |
sudo apt update
sudo apt install -y --no-install-recommends \
ninja-build cmake g++ libgtest-dev libsdl1.2-dev libsdl-image1.2-dev libncurses-dev zlib1g-dev libspdlog-dev
ninja-build cmake g++ libgtest-dev libsdl2-dev libncurses-dev zlib1g-dev libspdlog-dev

- name: Configure CMake
env:
Expand Down
3 changes: 1 addition & 2 deletions Brewfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# Homebrew dependencies to build Descent3

# SDL
brew "sdl12-compat"
brew "sdl2_image"
brew "sdl2"

brew "cmake"
brew "googletest"
Expand Down
16 changes: 5 additions & 11 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,27 +76,21 @@ if(UNIX)
add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:-Wno-multichar;${BITS};${EXTRA_CXX_FLAGS}>")
add_compile_options("$<$<COMPILE_LANGUAGE:C>:${BITS}>")

find_package(SDL REQUIRED)
if(APPLE)
# Provide FIND_PACKAGE( SDL_image ) below with an include dir and library that work with brew-installed sdl2_image
find_path(SDL_IMAGE_INCLUDE_DIR NAMES SDL_image.h PATH_SUFFIXES include/SDL2)
find_library(SDL_IMAGE_LIBRARY NAMES SDL2_image)
endif()

find_package(SDL_image REQUIRED)
find_package(SDL2 REQUIRED)
find_package(Curses REQUIRED)
find_package(OpenGL REQUIRED)
message("SDL Include Dir is " ${SDL_INCLUDE_DIR})
# Some versions of the SDL2 find_package set SDL2_INCLUDE_DIR and some set a plural SDL2_INCLUDE_DIRS. Check both.
message("SDL2 Include Dir is ${SDL2_INCLUDE_DIR} ${SDL2_INCLUDE_DIRS}")
endif()

if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
message("Building for Linux")
add_compile_definitions(__LINUX__ LINUX _MAX_PATH=260 _MAX_FNAME=256 _REENRANT __32BIT__ HAVEALLOCA_H _USE_OGL_ACTIVE_TEXTURES)
set(PLATFORM_INCLUDES "lib/linux" ${SDL_INCLUDE_DIR})
set(PLATFORM_INCLUDES "lib/linux" ${SDL2_INCLUDE_DIR} ${SDL2_INCLUDE_DIRS})
elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
message("Building for MAC OSX")
add_compile_definitions(__LINUX__ LINUX _MAX_PATH=260 _MAX_FNAME=256 _REENRANT MACOSX=1 _USE_OGL_ACTIVE_TEXTURES)
set(PLATFORM_INCLUDES "lib/linux" ${SDL_INCLUDE_DIR})
set(PLATFORM_INCLUDES "lib/linux" ${SDL2_INCLUDE_DIR} ${SDL2_INCLUDE_DIRS})
elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows")
list(APPEND CMAKE_LIBRARY_PATH "lib/win" "lib/win/directx")
add_compile_options("$<$<AND:$<CONFIG:Debug>,$<COMPILE_LANGUAGE:CXX>>:/EHsc;/RTC1;/W3;/nologo;/c;/Zi;/TP;/errorReport:prompt>")
Expand Down
6 changes: 3 additions & 3 deletions Descent3/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -282,13 +282,13 @@ if(WIN32)
endif()

if(UNIX AND NOT APPLE)
set(PLATFORM_LIBS linux dd_lnxsound ddvid_lnx lnxcontroller ddio_lnx SDL::SDL m ${CMAKE_DL_LIBS} ${CURSES_LIBRARIES})
set(PLATFORM_LIBS linux dd_lnxsound ddvid_lnx lnxcontroller ddio_lnx SDL2::SDL2 m ${CMAKE_DL_LIBS} ${CURSES_LIBRARIES})
set(PLATFORM_CPPS loki_utils.c lnxmain.cpp)
endif()

if(APPLE)
set(PLATFORM_LIBS linux dd_lnxsound ddvid_lnx lnxcontroller ddio_lnx SDL::SDL ${CURSES_LIBRARIES})
set(PLATFORM_CPPS loki_utils.c lnxmain.cpp SDLMain.m)
set(PLATFORM_LIBS linux dd_lnxsound ddvid_lnx lnxcontroller ddio_lnx SDL2::SDL2 ${CURSES_LIBRARIES})
set(PLATFORM_CPPS loki_utils.c lnxmain.cpp)
set(CMAKE_EXE_LINKER_FLAGS "-framework IOKit -framework Cocoa -framework OpenGL -framework Carbon")
endif()

Expand Down
16 changes: 12 additions & 4 deletions Descent3/lnxmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ int sdlMouseButtonUpFilter(const SDL_Event *event);
int sdlMouseButtonDownFilter(const SDL_Event *event);
int sdlMouseMotionFilter(const SDL_Event *event);

int d3SDLEventFilter(const SDL_Event *event) {
int SDLCALL d3SDLEventFilter(void *userdata, SDL_Event *event) {
switch (event->type) {
case SDL_KEYUP:
case SDL_KEYDOWN:
Expand All @@ -322,6 +322,11 @@ int d3SDLEventFilter(const SDL_Event *event) {
return (sdlMouseButtonUpFilter(event));
case SDL_MOUSEBUTTONDOWN:
return (sdlMouseButtonDownFilter(event));
case SDL_QUIT:
SDL_Quit();
_exit(0);
break;
default: break;
icculus marked this conversation as resolved.
Show resolved Hide resolved
} // switch

return (1);
Expand Down Expand Up @@ -614,8 +619,11 @@ int main(int argc, char *argv[]) {
} // if
*/

SDL_ClearError();
int rc = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_CDROM);
#ifdef DEDICATED
setenv("SDL_VIDEODRIVER", "dummy", 1);
#endif

int rc = SDL_Init(SDL_INIT_VIDEO);
if (rc != 0) {
fprintf(stderr, "SDL: SDL_Init() failed! rc == (%d).\n", rc);
fprintf(stderr, "SDL_GetError() reports \"%s\".\n", SDL_GetError());
Expand All @@ -625,7 +633,7 @@ int main(int argc, char *argv[]) {
// atexit(SDL_Quit);

// !!! FIXME: Don't use an event filter!
SDL_SetEventFilter(d3SDLEventFilter);
SDL_SetEventFilter(d3SDLEventFilter, NULL);
install_signal_handlers();

// build the command line as one long string, seperated by spaces...
Expand Down