Maelstrom: Use SDL functions instead of stdlib

From c0e952bb5b5d7fc7d6c137a397f2ff99e28da59b Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Sat, 22 Nov 2025 23:45:58 -0800
Subject: [PATCH] Use SDL functions instead of stdlib

---
 game/controls.cpp |  3 ---
 game/gameover.cpp |  4 ++--
 game/init.cpp     |  2 --
 game/lobby.cpp    | 10 ++--------
 game/netplay.cpp  |  2 --
 game/scores.cpp   |  4 +---
 utils/prefs.cpp   |  2 +-
 7 files changed, 6 insertions(+), 21 deletions(-)

diff --git a/game/controls.cpp b/game/controls.cpp
index 9e66e5bb..3952e136 100644
--- a/game/controls.cpp
+++ b/game/controls.cpp
@@ -23,9 +23,6 @@
 /* This file handles the controls configuration and updating the keystrokes 
 */
 
-#include <string.h>
-#include <ctype.h>
-
 #include "Maelstrom_Globals.h"
 #include "player.h"
 #include "../screenlib/UIDialog.h"
diff --git a/game/gameover.cpp b/game/gameover.cpp
index 2884685a..cee9e12f 100644
--- a/game/gameover.cpp
+++ b/game/gameover.cpp
@@ -70,9 +70,9 @@ void GameOverPanelDelegate::OnShow()
 		final[i].Frags = gPlayers[i]->GetFrags();
 	}
 	if ( gGameInfo.IsDeathmatch() )
-		qsort(final,MAX_PLAYERS,sizeof(struct FinalScore),cmp_byfrags);
+		SDL_qsort(final,MAX_PLAYERS,sizeof(struct FinalScore),cmp_byfrags);
 	else
-		qsort(final,MAX_PLAYERS,sizeof(struct FinalScore),cmp_byscore);
+		SDL_qsort(final,MAX_PLAYERS,sizeof(struct FinalScore),cmp_byscore);
 
 	m_panel->HideAll();
 
diff --git a/game/init.cpp b/game/init.cpp
index 1347d800..b4b5ca83 100644
--- a/game/init.cpp
+++ b/game/init.cpp
@@ -23,8 +23,6 @@
 #ifdef COMPUTE_VELTABLE
 #include <math.h>
 #endif
-#include <signal.h>
-#include <stdlib.h>
 
 #include "Maelstrom_Globals.h"
 #include "load.h"
diff --git a/game/lobby.cpp b/game/lobby.cpp
index 6e228379..3bb28f55 100644
--- a/game/lobby.cpp
+++ b/game/lobby.cpp
@@ -20,9 +20,6 @@
     slouken@libsdl.org
 */
 
-#include <stdlib.h>
-#include <time.h>
-
 #include "SDL_net.h"
 #include "Maelstrom_Globals.h"
 #include "../screenlib/UIElement.h"
@@ -245,9 +242,6 @@ LobbyDialogDelegate::GetElement(const char *name, UIElement *&element)
 void
 LobbyDialogDelegate::OnShow()
 {
-	// Seed the random number generator for our unique ID
-	srand(time(NULL)+SDL_GetTicks());
-
 	// Start up networking
 	SetHostOrJoin(0, m_hostOrJoin->GetValue());
 }
@@ -321,9 +315,9 @@ LobbyDialogDelegate::SetHostOrJoin(void*, int value)
 			return;
 		}
 
-		Uint32 localID = rand();
+		Uint32 localID = SDL_rand_bits();
 		while (localID <= 1) {
-			localID = rand();
+			localID = SDL_rand_bits();
 		}
 		m_game.SetLocalID(localID);
 
diff --git a/game/netplay.cpp b/game/netplay.cpp
index 2f05d7f0..aec31016 100644
--- a/game/netplay.cpp
+++ b/game/netplay.cpp
@@ -22,8 +22,6 @@
 
 /* This contains the network play functions and data */
 
-#include <stdlib.h>
-
 #include "Maelstrom_Globals.h"
 #include "netplay.h"
 #include "protocol.h"
diff --git a/game/scores.cpp b/game/scores.cpp
index 43b0ddca..d3d9218d 100644
--- a/game/scores.cpp
+++ b/game/scores.cpp
@@ -24,8 +24,6 @@
    This file handles the cheat dialogs and the high score file
 */
 
-#include <stdlib.h>	// for qsort()
-
 #include "Maelstrom_Globals.h"
 #include "scores.h"
 #include "../utils/array.h"
@@ -88,7 +86,7 @@ void LoadScores(void)
 
 	// Take the top 10
 	if (scores.length() > 0) {
-		qsort(&scores[0], scores.length(), sizeof(scores[0]), SortScores);
+		SDL_qsort(&scores[0], scores.length(), sizeof(scores[0]), SortScores);
 	}
 	for (i = 0; i < scores.length() && i < NUM_SCORES; ++i) {
 		hScores[i] = scores[i];
diff --git a/utils/prefs.cpp b/utils/prefs.cpp
index bd103636..8f5de9ff 100644
--- a/utils/prefs.cpp
+++ b/utils/prefs.cpp
@@ -121,7 +121,7 @@ Prefs::Save()
 	while (hash_iter(m_values, (const void **)&key, (const void **)&value, &iter)) {
 		keys.add(key);
 	}
-	qsort(&keys[0], keys.length(), sizeof(key), sort_keys);
+	SDL_qsort(&keys[0], keys.length(), sizeof(key), sort_keys);
 	
 	for (unsigned int i = 0; i < keys.length(); ++i) {
 		hash_find(m_values, keys[i], (const void **)&value);