Skip to content

Commit e09bde0

Browse files
committed
cmake: support SDL2_net-on-SDL3 + build examples
1 parent 7a9d53e commit e09bde0

File tree

5 files changed

+65
-30
lines changed

5 files changed

+65
-30
lines changed

CMakeLists.txt

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
cmake_minimum_required(VERSION 3.16)
1+
cmake_minimum_required(VERSION 3.16...4.0)
22

33
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
44

5+
message(WARNING
6+
"SDL2_net on top of SDL3 is provided as a means to making porting easier.\n"
7+
"This library is completely unsupported.\n"
8+
"DO NOT SHIP THIS LIBRARY"
9+
)
10+
511
# See release_checklist.md
612
set(MAJOR_VERSION 2)
713
set(MINOR_VERSION 3)
814
set(MICRO_VERSION 0)
9-
set(SDL_REQUIRED_VERSION 2.0.4)
15+
set(SDL_REQUIRED_VERSION 3.1.0)
1016

1117
include(PrivateSdlFunctions)
1218
sdl_calculate_derived_version_variables()
@@ -15,7 +21,7 @@ if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_CURRENT_BINARY_DIR)
1521
message(FATAL_ERROR "Prevented in-tree built. Please create a build directory outside of the SDL_net source code and call cmake from there")
1622
endif()
1723

18-
project(SDL2_net
24+
project(SDL2On3_net
1925
LANGUAGES C
2026
VERSION "${FULL_VERSION}"
2127
)
@@ -28,10 +34,10 @@ else()
2834
set(SDL2NET_ROOTPROJECT OFF)
2935
endif()
3036

31-
if(TARGET SDL2::SDL2test)
32-
set(SDL2test_FOUND ON)
37+
if(TARGET SDL3::SDL3_test)
38+
set(SDL3test_FOUND ON)
3339
else()
34-
find_package(SDL2test)
40+
find_package(SDL3 CONFIG COMPONENTS SDL3_test)
3541
endif()
3642

3743
set(SDLNET_PKGCONFIG_PRIVATE_LIBS)
@@ -56,10 +62,10 @@ option(BUILD_SHARED_LIBS "Build the library as a shared library" ON)
5662

5763
cmake_dependent_option(SDL2NET_INSTALL "Enable SDL2_net install target" ${SDL2NET_ROOTPROJECT} ${sdl2net_install_enableable} OFF)
5864

59-
cmake_dependent_option(SDL2NET_SAMPLES "Build SDL2_net samples" ${SDL2NET_ROOTPROJECT} "${SDL2test_FOUND}" OFF)
65+
cmake_dependent_option(SDL2NET_SAMPLES "Build SDL2_net samples" ${SDL2NET_ROOTPROJECT} "TARGET SDL3::SDL3_test" OFF)
6066

6167
if(SDL2NET_SAMPLES)
62-
find_package(SDL2test REQUIRED)
68+
find_package(SDL3 CONFIG COMPONENTS SDL3_test)
6369
endif()
6470

6571
# Save BUILD_SHARED_LIBS variable
@@ -68,14 +74,18 @@ set(SDL2NET_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})
6874
if(SDL2NET_BUILD_SHARED_LIBS)
6975
set(sdl2_net_export_name SDL2_net)
7076
set(sdl2_net_install_name_infix shared)
71-
set(sdl2_target_name SDL2::SDL2)
77+
set(sdl3_target_name SDL3::SDL3)
78+
set(sdl3_component SDL3)
7279
else()
7380
set(sdl2_net_export_name SDL2_net-static)
7481
set(sdl2_net_install_name_infix static)
75-
set(sdl2_target_name SDL2::SDL2-static)
82+
set(sdl3_target_name SDL3::SDL3-static)
83+
set(sdl3_component SDL3-shared)
7684
endif()
7785

78-
sdl_find_sdl2(${sdl2_target_name} ${SDL_REQUIRED_VERSION})
86+
if(NOT TARGET ${sdl3_target_name})
87+
find_package(SDL3 REQUIRED CONFIG COMPONENTS ${sdl3_component})
88+
endif()
7989

8090
add_library(SDL2_net
8191
src/SDLnet.c
@@ -98,7 +108,7 @@ target_compile_definitions(SDL2_net PRIVATE
98108
SDL_BUILD_MINOR_VERSION=${MINOR_VERSION}
99109
SDL_BUILD_MICRO_VERSION=${MICRO_VERSION}
100110
)
101-
target_link_libraries(SDL2_net PRIVATE $<BUILD_INTERFACE:${sdl2_target_name}>)
111+
target_link_libraries(SDL2_net PRIVATE $<BUILD_INTERFACE:${sdl3_target_name}>)
102112
if(WIN32)
103113
if (MSVC)
104114
target_compile_options(SDL2_net PRIVATE /W3 /wd4244)
@@ -290,15 +300,15 @@ if(SDL2NET_SAMPLES)
290300

291301
add_executable(showinterfaces examples/showinterfaces.c)
292302
target_compile_definitions(showinterfaces PRIVATE SDL_MAIN_HANDLED)
293-
target_link_libraries(showinterfaces PRIVATE SDL2_net::${sdl2_net_export_name} ${sdl2_target_name})
303+
target_link_libraries(showinterfaces PRIVATE SDL2_net::${sdl2_net_export_name} ${sdl3_target_name})
294304

295305
add_executable(chat examples/chat.c examples/chat.h)
296-
if(TARGET SDL2::SDL2main)
297-
target_link_libraries(chat PRIVATE SDL2::SDL2main)
298-
endif()
299-
target_link_libraries(chat PRIVATE SDL2_net::${sdl2_net_export_name} SDL2::SDL2test ${sdl2_target_name})
306+
target_link_libraries(chat PRIVATE SDL2_net::${sdl2_net_export_name} SDL3::SDL3_test ${sdl3_target_name})
300307

301308
add_executable(chatd examples/chatd.c examples/chat.h)
302309
target_compile_definitions(chatd PRIVATE SDL_MAIN_HANDLED)
303-
target_link_libraries(chatd PRIVATE SDL2_net::${sdl2_net_export_name} ${sdl2_target_name})
310+
target_link_libraries(chatd PRIVATE SDL2_net::${sdl2_net_export_name} ${sdl3_target_name})
304311
endif()
312+
313+
add_library(SDL2_net_on_SDL3 INTERFACE)
314+
add_library(SDL2_net::on-SDL3 ALIAS SDL2_net_on_SDL3)

SDL2_netConfig.cmake.in

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
2+
message(WARNING
3+
"SDL2_net on top of SDL3 is provided as a means to making porting easier.\n"
4+
"This library is completely unsupported.\n"
5+
"DO NOT SHIP THIS LIBRARY"
6+
)
7+
8+
# Presence of a SDL2_net::on-SDL3 target signals SDL2_net on top of SDL3
9+
if(NOT TARGET SDL2_net::on-SDL3)
10+
add_library(SDL2_net::on-SDL3 UNKNOWN IMPORTED)
11+
endif()
12+
113
include(FeatureSummary)
214
set_package_properties(SDL2_net PROPERTIES
315
URL "https://www.libsdl.org/projects/SDL_net/"

examples/chat.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
*/
2626

2727
#include "SDL_net.h"
28-
#include "SDL_test.h"
28+
#include <SDL3/SDL_test.h>
2929
#include "chat.h"
3030

3131
#include <stdio.h>
@@ -97,12 +97,12 @@ static void TextWindowDisplay(TextWindow *textwin, SDL_Renderer *renderer)
9797
static void TextWindowAddTextWithLength(TextWindow *textwin, const char *text, size_t len)
9898
{
9999
size_t existing;
100-
SDL_bool newline = SDL_FALSE;
100+
bool newline = false;
101101
char *line;
102102

103103
if ( len > 0 && text[len - 1] == '\n' ) {
104104
--len;
105-
newline = SDL_TRUE;
105+
newline = true;
106106
}
107107

108108
if ( textwin->lines[textwin->current] ) {
@@ -261,8 +261,8 @@ int HandleServerData(Uint8 *data)
261261
newip.port, people[which].name);
262262

263263
/* Put the address back in network form */
264-
newip.host = SDL_SwapBE32(newip.host);
265-
newip.port = SDL_SwapBE16(newip.port);
264+
newip.host = SDL_Swap32BE(newip.host);
265+
newip.port = SDL_Swap16BE(newip.port);
266266

267267
/* Bind the address to the UDP socket */
268268
SDLNet_UDP_Bind(udpsock, which, &newip);
@@ -436,7 +436,7 @@ int main(int argc, char *argv[])
436436

437437

438438
/* Set a 640x480 video mode */
439-
if ( SDL_CreateWindowAndRenderer(640, 480, 0, &window, &renderer) < 0 ) {
439+
if ( SDL_CreateWindowAndRenderer("chat", 640, 480, 0, &window, &renderer) < 0 ) {
440440
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
441441
"Couldn't create window: %s\n",
442442
SDL_GetError());
@@ -504,16 +504,17 @@ int main(int argc, char *argv[])
504504
/* Run the GUI, handling network data */
505505
SendHello(argv[2]);
506506
done = 0;
507+
SDL_StartTextInput(window);
507508
while ( !done ) {
508509
HandleNet();
509510

510511
while ( SDL_PollEvent(&event) == 1 ) {
511512
switch ( event.type ) {
512-
case SDL_QUIT:
513+
case SDL_EVENT_QUIT:
513514
done = 1;
514515
break;
515-
case SDL_KEYDOWN:
516-
switch ( event.key.keysym.sym ) {
516+
case SDL_EVENT_KEY_DOWN:
517+
switch ( event.key.key ) {
517518
case SDLK_ESCAPE:
518519
done = 1;
519520
break;
@@ -533,7 +534,7 @@ int main(int argc, char *argv[])
533534
break;
534535
}
535536
break;
536-
case SDL_TEXTINPUT:
537+
case SDL_EVENT_TEXT_INPUT:
537538
{
538539
size_t textlen = SDL_strlen(event.text.text);
539540

@@ -557,6 +558,7 @@ int main(int argc, char *argv[])
557558

558559
DisplayGUI(renderer);
559560
}
561+
SDL_StopTextInput(window);
560562
cleanup(0);
561563

562564
/* Keep the compiler happy */

examples/chatd.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#include <stdio.h>
2929
#include <string.h>
3030

31-
#include "SDL.h"
31+
#include <SDL3/SDL.h>
3232
#include "SDL_net.h"
3333
#include "chat.h"
3434

@@ -199,7 +199,7 @@ int main(int argc, char *argv[])
199199
(void) argv;
200200

201201
/* Initialize SDL */
202-
if ( SDL_Init(0) < 0 ) {
202+
if ( ! SDL_Init(0) ){
203203
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
204204
"Couldn't initialize SDL: %s\n",
205205
SDL_GetError());

sdl2_net-config.cmake.in

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# sdl2_net cmake project-config input for ./configure scripts
22

3+
message(WARNING
4+
"SDL2_net on top of SDL3 is provided as a means to making porting easier.\n"
5+
"This library is completely unsupported.\n"
6+
"DO NOT SHIP THIS LIBRARY"
7+
)
8+
9+
# Presence of a SDL2_net::on-SDL3 target signals SDL2_net on top of SDL3
10+
if(NOT TARGET SDL2_net::on-SDL3)
11+
add_library(SDL2_net::on-SDL3 UNKNOWN IMPORTED)
12+
endif()
13+
314
include(FeatureSummary)
415
set_package_properties(SDL2_net PROPERTIES
516
URL "https://www.libsdl.org/projects/SDL_net/"

0 commit comments

Comments
 (0)