From 8382f38aef6c19c2a473a8a9c5781bcc35987e01 Mon Sep 17 00:00:00 2001
From: Tim Felgentreff <timfelgentreff@gmail.com>
Date: Sat, 2 May 2020 07:24:10 +0200
Subject: [PATCH] wip: compilation on windows

---
 CMakeLists.txt                | 10 ++++++++-
 cmake/modules/FindSDL2.cmake  |  9 +++++++-
 src/third_party/hqx/src/hqx.h | 14 +++++++------
 src/video/sdl.cpp             | 39 ++++++++++++++++++-----------------
 4 files changed, 45 insertions(+), 27 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index d02b72b98..f465653c4 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -711,6 +711,11 @@ else()
 	set(PIXMAPSDIRABS "${PIXMAPSDIR}")
 endif()
 
+if (WIN32)
+  	set(SDL2_LIBRARY ${SDL2_LIBRARY} ${SDL2main_LIBRARY})
+  	message(${SDL2_LIBRARY})
+endif()
+
 # Stratagus definitions
 
 add_definitions(-DUSE_ZLIB -DPIXMAPS=\"${PIXMAPSDIRABS}\")
@@ -807,6 +812,9 @@ endif()
 if(CMAKE_COMPILER_IS_GNUC)
 	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsigned-char")
 endif()
+if(WIN32 AND MSVC)
+	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++17")
+endif()
 
 if(CMAKE_BUILD_TYPE STREQUAL "Profile")
 	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pg")
@@ -1063,7 +1071,7 @@ if(WIN32 AND MINGW AND ENABLE_STATIC)
 	set_target_properties(stratagus PROPERTIES LINK_FLAGS "${LINK_FLAGS} -static-libgcc -static-libstdc++ -Wl,--stack,10485760")
 endif()
 
-if(APPLE)
+if(APPLE OR WIN32)
 	set_target_properties(stratagus PROPERTIES LINK_FLAGS "${LINK_FLAGS} -lSDL2main")
 endif()
 
diff --git a/cmake/modules/FindSDL2.cmake b/cmake/modules/FindSDL2.cmake
index 92e4501b8..926dd0526 100644
--- a/cmake/modules/FindSDL2.cmake
+++ b/cmake/modules/FindSDL2.cmake
@@ -13,6 +13,13 @@ find_library(SDL2_LIBRARY_TEMP
   PATH_SUFFIXES lib
 )
 
+find_library(SDL2main_LIBRARY
+  NAMES SDL2main
+  HINTS
+    ENV SDLDIR
+  PATH_SUFFIXES lib
+)
+
 # SDL may require threads on your system.
 # The Apple build may not need an explicit flag because one of the
 # frameworks may already provide it.
@@ -84,4 +91,4 @@ else (THEORA_FOUND)
   endif (SDL2_FIND_REQUIRED)
 endif (SDL2_FOUND)
 
-MARK_AS_ADVANCED(SDL2_INCLUDE_DIR SDL2_LIBRARY)
+MARK_AS_ADVANCED(SDL2_INCLUDE_DIR SDL2_LIBRARY SDL2main_LIBRARY)
diff --git a/src/third_party/hqx/src/hqx.h b/src/third_party/hqx/src/hqx.h
index ce8783eff..58cc5e449 100644
--- a/src/third_party/hqx/src/hqx.h
+++ b/src/third_party/hqx/src/hqx.h
@@ -34,12 +34,14 @@
 #endif
 
 #if defined(_WIN32)
-    #ifdef DLL_EXPORT
-        #define HQX_API __declspec(dllexport)
-    #else
-        #define HQX_API __declspec(dllimport)
-    #endif
-#else
+/* 
+ *     #ifdef DLL_EXPORT
+ *         #define HQX_API __declspec(dllexport)
+ *     #else
+ *         #define HQX_API __declspec(dllimport)
+ *     #endif
+ * #else
+ */
     #define HQX_API
 #endif
 
diff --git a/src/video/sdl.cpp b/src/video/sdl.cpp
index 7c4f1fc0a..47cd91ff1 100644
--- a/src/video/sdl.cpp
+++ b/src/video/sdl.cpp
@@ -282,25 +282,6 @@ void InitVideoSdl()
 #if defined(DEBUG) && !defined(USE_WIN32)
 		signal(SIGSEGV, CleanExit);
 		signal(SIGABRT, CleanExit);
-#endif
-#ifdef USE_WIN32
-		HWND hwnd = NULL;
-		HICON hicon = NULL;
-		SDL_SysWMinfo info;
-		SDL_VERSION(&info.version);
-
-		if (SDL_GetWMInfo(&info)) {
-			hwnd = info.window;
-		}
-
-		if (hwnd) {
-			hicon = ExtractIcon(GetModuleHandle(NULL), Parameters::Instance.applicationName.c_str(), 0);
-		}
-
-		if (hicon) {
-			SendMessage(hwnd, (UINT)WM_SETICON, ICON_SMALL, (LPARAM)hicon);
-			SendMessage(hwnd, (UINT)WM_SETICON, ICON_BIG, (LPARAM)hicon);
-		}
 #endif
 	}
 
@@ -352,6 +333,26 @@ void InitVideoSdl()
 	}
 	Video.ResizeScreen(Video.Width, Video.Height);
 
+// #ifdef USE_WIN32
+// 	HWND hwnd = NULL;
+// 	HICON hicon = NULL;
+// 	SDL_SysWMinfo info;
+// 	SDL_VERSION(&info.version);
+
+// 	if (SDL_GetWindowWMInfo(TheWindow, &info)) {
+// 		hwnd = info.win.window;
+// 	}
+
+// 	if (hwnd) {
+// 		hicon = ExtractIcon(GetModuleHandle(NULL), Parameters::Instance.applicationName.c_str(), 0);
+// 	}
+
+// 	if (hicon) {
+// 		SendMessage(hwnd, (UINT)WM_SETICON, ICON_SMALL, (LPARAM)hicon);
+// 		SendMessage(hwnd, (UINT)WM_SETICON, ICON_BIG, (LPARAM)hicon);
+// 	}
+// #endif
+
 #if ! defined(USE_WIN32) && ! defined(USE_MAEMO)
 
 		SDL_Surface *icon = NULL;