SDL_net: Allow library to init multiple times.

From ebe934a05294a86a2761130e24314beff50f553d Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Sun, 24 Sep 2023 11:18:59 -0400
Subject: [PATCH] Allow library to init multiple times.

---
 SDL_net.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/SDL_net.c b/SDL_net.c
index a5dd21e..44b5e3d 100644
--- a/SDL_net.c
+++ b/SDL_net.c
@@ -330,8 +330,14 @@ static SDLNet_Address *CreateSDLNetAddrFromSockAddr(struct sockaddr *saddr, Sock
     return SDLNet_RefAddress(addr);
 }
 
+static SDL_AtomicInt initialize_count;
+
 int SDLNet_Init(void)
 {
+    if (SDL_AtomicAdd(&initialize_count, 1) > 0) {
+        return 0;  // already initialized, call it a success.
+    }
+
     char *origerrstr = NULL;
 
     #ifdef __WINDOWS__
@@ -385,6 +391,14 @@ int SDLNet_Init(void)
 
 void SDLNet_Quit(void)
 {
+    const int prevcount = SDL_AtomicAdd(&initialize_count, -1);
+    if (prevcount <= 0) {
+        SDL_AtomicAdd(&initialize_count, 1);  // bump back up.
+        return;  // we weren't initialized!
+    } else if (prevcount > 1) {
+        return;  // need to quit more, to match previous init calls.
+    }
+
     if (resolver_lock && resolver_condition) {
         SDL_LockMutex(resolver_lock);
         SDL_AtomicSet(&resolver_shutdown, 1);