From 0f513c88dfbeb8d6c7a3b858796463763b621bb1 Mon Sep 17 00:00:00 2001 From: Paul Demers Date: Wed, 20 May 2026 18:56:04 -0400 Subject: [PATCH] Add SDL_net support --- .gitmodules | 4 +++ README.md | 4 +-- lib/pkgconfig/SDL2_net.pc | 6 +++++ lib/sdl/Makefile | 22 +++++++++++++++ lib/sdl/SDL_net | 1 + samples/sdl_net/Makefile | 7 +++++ samples/sdl_net/main.c | 57 +++++++++++++++++++++++++++++++++++++++ 7 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 lib/pkgconfig/SDL2_net.pc create mode 160000 lib/sdl/SDL_net create mode 100644 samples/sdl_net/Makefile create mode 100644 samples/sdl_net/main.c diff --git a/.gitmodules b/.gitmodules index db49e0a56..3f20b64ea 100644 --- a/.gitmodules +++ b/.gitmodules @@ -42,3 +42,7 @@ [submodule "lib/usb/libusbohci"] path = lib/usb/libusbohci url = https://github.com/XboxDev/libusbohci.git +[submodule "lib/sdl/SDL_net"] + path = lib/sdl/SDL_net + url = https://github.com/PaulMDemers/SDL_net.git + branch = nxdk-sdl-net diff --git a/README.md b/README.md index da55de532..8deaab9f3 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Notable features: - No complicated cross-compiling or big library dependencies! Builds with `make` and just needs standard tools and llvm. - Modern C / C++ standards and compiler features. - Supports popular APIs like Windows API and BSD sockets. -- SDL2 support for input, audio and 2D graphics. +- SDL2 support for input, audio, networking, and 2D graphics. - Custom API for 3D graphics using NVIDIA-designed shader-languages (with additional Xbox extensions). - Open-Source drivers which can be modified to get the most out of the hardware. - Modifiable startup code, for as much system control as necessary. @@ -64,7 +64,7 @@ Code Overview * `lib/net` - Network stack for the Xbox based on lwIP. * `lib/pdclib` - Xbox port of PDCLib, a CC0-licensed C standard library. * `lib/pbkit` - A low level library for interfacing with the Xbox GPU. -* `lib/sdl` - Xbox ports of SDL2 and SDL_ttf. +* `lib/sdl` - Xbox ports of SDL2, SDL_image, SDL_net, and SDL_ttf. * `lib/usb` - USB support from OpenXDK. Hacked together parts of an old Linux OHCI stack. * `lib/winapi` - Xbox specific implementations of common useful WinAPI-functions. * `lib/xboxkrnl` - Header and import library for interfacing with the Xbox kernel. diff --git a/lib/pkgconfig/SDL2_net.pc b/lib/pkgconfig/SDL2_net.pc new file mode 100644 index 000000000..27ade3db0 --- /dev/null +++ b/lib/pkgconfig/SDL2_net.pc @@ -0,0 +1,6 @@ +Name: SDL2_net +Description: networking library for Simple DirectMedia Layer +Version: 2.0.1 +Requires: sdl2 >= 2.0.9 +Libs: -l${NXDK_DIR}/lib/libSDL2_net.lib +Cflags: -I${NXDK_DIR}/lib/sdl/SDL_net diff --git a/lib/sdl/Makefile b/lib/sdl/Makefile index b0328c326..ec74ff331 100644 --- a/lib/sdl/Makefile +++ b/lib/sdl/Makefile @@ -74,3 +74,25 @@ CLEANRULES += clean-libsdlimage .PHONY: clean-libsdlimage clean-libsdlimage: $(VE)rm -f $(LIBSDLIMAGE_OBJS) $(NXDK_DIR)/lib/libSDL2_image.lib + +SDLNET_DIR = $(NXDK_DIR)/lib/sdl/SDL_net + +SDLNET_SRCS = $(SDLNET_DIR)/SDLnet.c \ + $(SDLNET_DIR)/SDLnetselect.c \ + $(SDLNET_DIR)/SDLnetTCP.c \ + $(SDLNET_DIR)/SDLnetUDP.c + +SDLNET_OBJS = $(addsuffix .obj, $(basename $(SDLNET_SRCS))) + +NXDK_CFLAGS += -I$(SDLNET_DIR) +NXDK_CXXFLAGS += -I$(SDLNET_DIR) + +$(NXDK_DIR)/lib/libSDL2_net.lib: $(SDLNET_OBJS) + +main.exe: $(NXDK_DIR)/lib/libSDL2_net.lib + +CLEANRULES += clean-sdlnet + +.PHONY: clean-sdlnet +clean-sdlnet: + $(VE)rm -f $(SDLNET_OBJS) $(NXDK_DIR)/lib/libSDL2_net.lib diff --git a/lib/sdl/SDL_net b/lib/sdl/SDL_net new file mode 160000 index 000000000..7c47cf80a --- /dev/null +++ b/lib/sdl/SDL_net @@ -0,0 +1 @@ +Subproject commit 7c47cf80a2c70e7542886a2a25d7a7fc7b4d4dcd diff --git a/samples/sdl_net/Makefile b/samples/sdl_net/Makefile new file mode 100644 index 000000000..8bbeadef1 --- /dev/null +++ b/samples/sdl_net/Makefile @@ -0,0 +1,7 @@ +XBE_TITLE = nxdk\ sample\ -\ SDL2_net +GEN_XISO = $(XBE_TITLE).iso +SRCS = $(CURDIR)/main.c +NXDK_DIR ?= $(CURDIR)/../.. +NXDK_SDL = y + +include $(NXDK_DIR)/Makefile diff --git a/samples/sdl_net/main.c b/samples/sdl_net/main.c new file mode 100644 index 000000000..f5adab244 --- /dev/null +++ b/samples/sdl_net/main.c @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include +#include +#include + +int main(void) +{ + IPaddress local; + UDPsocket udp; + + XVideoSetMode(640, 480, 32, REFRESH_DEFAULT); + debugPrint("nxdk SDL_net sample\n"); + + if (SDL_Init(0) < 0) { + debugPrint("SDL_Init failed: %s\n", SDL_GetError()); + return 1; + } + + nxNetInit(NULL); + + if (SDLNet_Init() < 0) { + debugPrint("SDLNet_Init failed: %s\n", SDLNet_GetError()); + SDL_Quit(); + return 1; + } + + if (SDLNet_ResolveHost(&local, "127.0.0.1", 12345) == 0) { + debugPrint("Resolved 127.0.0.1:%u\n", SDLNet_Read16(&local.port)); + } else { + debugPrint("Resolve failed: %s\n", SDLNet_GetError()); + } + + udp = SDLNet_UDP_Open(0); + if (udp) { + IPaddress *bound = SDLNet_UDP_GetPeerAddress(udp, -1); + if (bound) { + ip4_addr_t addr; + addr.addr = bound->host; + debugPrint("UDP bound to %s:%u\n", ip4addr_ntoa(&addr), SDLNet_Read16(&bound->port)); + } + SDLNet_UDP_Close(udp); + } else { + debugPrint("UDP open failed: %s\n", SDLNet_GetError()); + } + + SDLNet_Quit(); + SDL_Quit(); + + while (1) { + Sleep(1000); + } + + return 0; +}