From c375d27daa7ec121deb2caa09f1749dd4914f3df Mon Sep 17 00:00:00 2001
From: chosenofbear <chosenofbear>
Date: Wed, 17 Jun 2015 09:12:19 -0400
Subject: [PATCH 1/4] Fix build in Visual Studio (2013)

Tested with Visual Studio 2013.
---
 binding-mri/binding-mri.cpp        | 4 ++--
 binding-mri/binding-util.cpp       | 4 ++--
 binding-mri/etc-binding.cpp        | 4 ++++
 binding-mri/filesystem-binding.cpp | 5 ++---
 binding-mri/font-binding.cpp       | 4 ++++
 binding-mri/input-binding.cpp      | 4 ++++
 binding-mri/table-binding.cpp      | 4 ++++
 patches/SDL_sound/ima-adpcm.patch  | 4 ++--
 src/alstream.cpp                   | 1 +
 src/crossplatform.h                | 7 +++++++
 src/eventthread.cpp                | 1 +
 src/exception.h                    | 3 +++
 src/filesystem.cpp                 | 1 +
 src/graphics.cpp                   | 2 ++
 src/keybindings.cpp                | 1 +
 src/main.cpp                       | 5 ++++-
 src/sdl-util.h                     | 4 ++++
 src/settingsmenu.cpp               | 1 +
 src/sharedstate.cpp                | 2 ++
 src/soundemitter.cpp               | 1 +
 src/util.h                         | 3 +++
 src/vorbissource.cpp               | 3 +++
 22 files changed, 58 insertions(+), 10 deletions(-)
 create mode 100644 src/crossplatform.h

diff --git a/binding-mri/binding-mri.cpp b/binding-mri/binding-mri.cpp
index 5cb518e..85884cd 100644
--- a/binding-mri/binding-mri.cpp
+++ b/binding-mri/binding-mri.cpp
@@ -20,11 +20,11 @@
 */
 
 #include "binding.h"
-#include "binding-util.h"
+#include "util.h"
+#include "binding-util.h"  //fatal error C1189 if this is before "util.h"
 #include "sharedstate.h"
 #include "eventthread.h"
 #include "filesystem.h"
-#include "util.h"
 #include "sdl-util.h"
 #include "debugwriter.h"
 #include "graphics.h"
diff --git a/binding-mri/binding-util.cpp b/binding-mri/binding-util.cpp
index e73d999..f5edb44 100644
--- a/binding-mri/binding-util.cpp
+++ b/binding-mri/binding-util.cpp
@@ -19,11 +19,11 @@
 ** along with mkxp.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-#include "binding-util.h"
+#include "util.h"
+#include "binding-util.h" //fatal error C1189 if this is before "util.h"
 
 #include "sharedstate.h"
 #include "exception.h"
-#include "util.h"
 
 #include <stdarg.h>
 #include <string.h>
diff --git a/binding-mri/etc-binding.cpp b/binding-mri/etc-binding.cpp
index 2c2cdd2..931f182 100644
--- a/binding-mri/etc-binding.cpp
+++ b/binding-mri/etc-binding.cpp
@@ -19,6 +19,10 @@
 ** along with mkxp.  If not, see <http://www.gnu.org/licenses/>.
 */
 
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_DEPRECATE
+#endif
+
 #include "etc.h"
 #include "binding-util.h"
 #include "serializable-binding.h"
diff --git a/binding-mri/filesystem-binding.cpp b/binding-mri/filesystem-binding.cpp
index 17cf50d..c4f5c54 100644
--- a/binding-mri/filesystem-binding.cpp
+++ b/binding-mri/filesystem-binding.cpp
@@ -19,11 +19,10 @@
 ** along with mkxp.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-#include "binding-util.h"
-
+#include "util.h"
+#include "binding-util.h" //fatal error C1189 if this is before "util.h"
 #include "sharedstate.h"
 #include "filesystem.h"
-#include "util.h"
 
 #include "ruby/encoding.h"
 #include "ruby/intern.h"
diff --git a/binding-mri/font-binding.cpp b/binding-mri/font-binding.cpp
index 2a8856a..df1efbc 100644
--- a/binding-mri/font-binding.cpp
+++ b/binding-mri/font-binding.cpp
@@ -19,6 +19,10 @@
 ** along with mkxp.  If not, see <http://www.gnu.org/licenses/>.
 */
 
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_DEPRECATE
+#endif
+
 #include "font.h"
 #include "binding-util.h"
 #include "binding-types.h"
diff --git a/binding-mri/input-binding.cpp b/binding-mri/input-binding.cpp
index f20b06a..5c00dd3 100644
--- a/binding-mri/input-binding.cpp
+++ b/binding-mri/input-binding.cpp
@@ -19,6 +19,10 @@
 ** along with mkxp.  If not, see <http://www.gnu.org/licenses/>.
 */
 
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_DEPRECATE
+#endif
+
 #include "input.h"
 #include "sharedstate.h"
 #include "exception.h"
diff --git a/binding-mri/table-binding.cpp b/binding-mri/table-binding.cpp
index 87a9b58..17b64a3 100644
--- a/binding-mri/table-binding.cpp
+++ b/binding-mri/table-binding.cpp
@@ -19,6 +19,10 @@
 ** along with mkxp.  If not, see <http://www.gnu.org/licenses/>.
 */
 
+#ifdef _MSC_VER
+#define NOMINMAX
+#endif
+
 #include <algorithm>
 #include "table.h"
 #include "binding-util.h"
diff --git a/patches/SDL_sound/ima-adpcm.patch b/patches/SDL_sound/ima-adpcm.patch
index bf6ba73..73b5dd4 100644
--- a/patches/SDL_sound/ima-adpcm.patch
+++ b/patches/SDL_sound/ima-adpcm.patch
@@ -186,7 +186,7 @@ diff -r 719dade41745 decoders/wav.c
 +                break;
 +
 +            rc = read_ima_block_headers(internal->rw, fmt->fmt.ima.d,
-+                                        fmt->wChannels, (Sint16*) (out_buf+bw));
++                                        fmt->wChannels, (Sint16*)out_buf+bw);
 +                                        
 +            if (!rc)
 +            {
@@ -210,7 +210,7 @@ diff -r 719dade41745 decoders/wav.c
 +        if (bw+fmt->fmt.ima.dec_frameset_size > internal->buffer_size)
 +            break;
 +
-+        rc = read_ima_frameset(internal->rw, w, (Sint16*) (out_buf+bw));
++        rc = read_ima_frameset(internal->rw, w, (Sint16*)out_buf+bw);
 +
 +        if (!rc)
 +        {
diff --git a/src/alstream.cpp b/src/alstream.cpp
index 18799ff..3220fd4 100644
--- a/src/alstream.cpp
+++ b/src/alstream.cpp
@@ -19,6 +19,7 @@
 ** along with mkxp.  If not, see <http://www.gnu.org/licenses/>.
 */
 
+#include "crossplatform.h"
 #include "alstream.h"
 
 #include "sharedstate.h"
diff --git a/src/crossplatform.h b/src/crossplatform.h
new file mode 100644
index 0000000..9f4bef6
--- /dev/null
+++ b/src/crossplatform.h
@@ -0,0 +1,7 @@
+#ifdef _MSC_VER
+#define snprintf _snprintf
+
+#include <direct.h>
+
+#define chdir _chdir
+#endif
\ No newline at end of file
diff --git a/src/eventthread.cpp b/src/eventthread.cpp
index df381e8..864de9b 100644
--- a/src/eventthread.cpp
+++ b/src/eventthread.cpp
@@ -19,6 +19,7 @@
 ** along with mkxp.  If not, see <http://www.gnu.org/licenses/>.
 */
 
+#include "crossplatform.h"
 #include "eventthread.h"
 
 #include <SDL_events.h>
diff --git a/src/exception.h b/src/exception.h
index 47d9c78..5746df7 100644
--- a/src/exception.h
+++ b/src/exception.h
@@ -22,6 +22,9 @@
 #ifndef EXCEPTION_H
 #define EXCEPTION_H
 
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_DEPRECATE
+#endif
 #include <string>
 #include <stdio.h>
 #include <stdarg.h>
diff --git a/src/filesystem.cpp b/src/filesystem.cpp
index 6642d3b..3dc2119 100644
--- a/src/filesystem.cpp
+++ b/src/filesystem.cpp
@@ -19,6 +19,7 @@
 ** along with mkxp.  If not, see <http://www.gnu.org/licenses/>.
 */
 
+#include "crossplatform.h"
 #include "filesystem.h"
 
 #include "rgssad.h"
diff --git a/src/graphics.cpp b/src/graphics.cpp
index 1862629..a5ae7bc 100644
--- a/src/graphics.cpp
+++ b/src/graphics.cpp
@@ -43,7 +43,9 @@
 #include <SDL_image.h>
 
 #include <time.h>
+#ifndef _MSC_VER
 #include <sys/time.h>
+#endif
 #include <errno.h>
 #include <algorithm>
 
diff --git a/src/keybindings.cpp b/src/keybindings.cpp
index 49d2a71..4a89fb6 100644
--- a/src/keybindings.cpp
+++ b/src/keybindings.cpp
@@ -19,6 +19,7 @@
 ** along with mkxp.  If not, see <http://www.gnu.org/licenses/>.
 */
 
+#include "crossplatform.h"
 #include "keybindings.h"
 
 #include "config.h"
diff --git a/src/main.cpp b/src/main.cpp
index eb07970..0c34a68 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -26,11 +26,14 @@
 #include <SDL_ttf.h>
 #include <SDL_sound.h>
 
+#ifndef _MSC_VER
 #include <unistd.h>
+#endif
 #include <string.h>
 #include <assert.h>
 #include <string>
 
+#include "crossplatform.h"
 #include "sharedstate.h"
 #include "eventthread.h"
 #include "gl-debug.h"
@@ -357,4 +360,4 @@ int main(int argc, char *argv[])
 	SDL_Quit();
 
 	return 0;
-}
+}
\ No newline at end of file
diff --git a/src/sdl-util.h b/src/sdl-util.h
index 1ed615d..c6044b4 100644
--- a/src/sdl-util.h
+++ b/src/sdl-util.h
@@ -44,7 +44,11 @@ int __sdlThreadFun(void *obj)
 template<class C, void (C::*func)()>
 SDL_Thread *createSDLThread(C *obj, const std::string &name = std::string())
 {
+#ifdef _MSC_VER
+	return SDL_CreateThread((__sdlThreadFun<C, func>), name.c_str(), obj);
+#else
 	return SDL_CreateThread(__sdlThreadFun<C, func>, name.c_str(), obj);
+#endif
 }
 
 /* On Android, SDL_RWFromFile always opens files from inside
diff --git a/src/settingsmenu.cpp b/src/settingsmenu.cpp
index a14b488..fe95674 100644
--- a/src/settingsmenu.cpp
+++ b/src/settingsmenu.cpp
@@ -19,6 +19,7 @@
 ** along with mkxp.  If not, see <http://www.gnu.org/licenses/>.
 */
 
+#include "crossplatform.h"
 #include "settingsmenu.h"
 
 #include <SDL_video.h>
diff --git a/src/sharedstate.cpp b/src/sharedstate.cpp
index 023ff6b..4c29f87 100644
--- a/src/sharedstate.cpp
+++ b/src/sharedstate.cpp
@@ -38,7 +38,9 @@
 #include "exception.h"
 #include "sharedmidistate.h"
 
+#ifndef _MSC_VER
 #include <unistd.h>
+#endif
 #include <stdio.h>
 #include <string>
 
diff --git a/src/soundemitter.cpp b/src/soundemitter.cpp
index 3850c0b..c27ef15 100644
--- a/src/soundemitter.cpp
+++ b/src/soundemitter.cpp
@@ -19,6 +19,7 @@
 ** along with mkxp.  If not, see <http://www.gnu.org/licenses/>.
 */
 
+#include "crossplatform.h"
 #include "soundemitter.h"
 
 #include "sharedstate.h"
diff --git a/src/util.h b/src/util.h
index ad9141a..f2461cd 100644
--- a/src/util.h
+++ b/src/util.h
@@ -22,6 +22,9 @@
 #ifndef UTIL_H
 #define UTIL_H
 
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_DEPRECATE
+#endif
 #include <stdio.h>
 #include <string>
 #include <algorithm>
diff --git a/src/vorbissource.cpp b/src/vorbissource.cpp
index 07dcdb4..1498dbc 100644
--- a/src/vorbissource.cpp
+++ b/src/vorbissource.cpp
@@ -19,6 +19,9 @@
 ** along with mkxp.  If not, see <http://www.gnu.org/licenses/>.
 */
 
+#ifdef _MSC_VER
+#include <algorithm>
+#endif
 #include "aldatasource.h"
 #include "exception.h"
 

From 0a6ede669b1f092d559968345b56fbcc228c699c Mon Sep 17 00:00:00 2001
From: chosenofbear <chosenofbear>
Date: Thu, 18 Jun 2015 15:41:32 -0400
Subject: [PATCH 2/4] Use a better (still bad) way to fix VC's compile error
 from Ruby's #define inline __inline

---
 binding-mri/binding-mri.cpp        | 4 ++--
 binding-mri/binding-util.cpp       | 4 ++--
 binding-mri/binding-util.h         | 1 +
 binding-mri/filesystem-binding.cpp | 5 +++--
 4 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/binding-mri/binding-mri.cpp b/binding-mri/binding-mri.cpp
index 85884cd..5cb518e 100644
--- a/binding-mri/binding-mri.cpp
+++ b/binding-mri/binding-mri.cpp
@@ -20,11 +20,11 @@
 */
 
 #include "binding.h"
-#include "util.h"
-#include "binding-util.h"  //fatal error C1189 if this is before "util.h"
+#include "binding-util.h"
 #include "sharedstate.h"
 #include "eventthread.h"
 #include "filesystem.h"
+#include "util.h"
 #include "sdl-util.h"
 #include "debugwriter.h"
 #include "graphics.h"
diff --git a/binding-mri/binding-util.cpp b/binding-mri/binding-util.cpp
index f5edb44..e73d999 100644
--- a/binding-mri/binding-util.cpp
+++ b/binding-mri/binding-util.cpp
@@ -19,11 +19,11 @@
 ** along with mkxp.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-#include "util.h"
-#include "binding-util.h" //fatal error C1189 if this is before "util.h"
+#include "binding-util.h"
 
 #include "sharedstate.h"
 #include "exception.h"
+#include "util.h"
 
 #include <stdarg.h>
 #include <string.h>
diff --git a/binding-mri/binding-util.h b/binding-mri/binding-util.h
index 83589f7..2d4a218 100644
--- a/binding-mri/binding-util.h
+++ b/binding-mri/binding-util.h
@@ -23,6 +23,7 @@
 #define BINDING_UTIL_H
 
 #include <ruby.h>
+#undef inline //ruby's config.h has #define inline __inline, which cause error in xkeycheck.h
 
 #include "exception.h"
 
diff --git a/binding-mri/filesystem-binding.cpp b/binding-mri/filesystem-binding.cpp
index c4f5c54..17cf50d 100644
--- a/binding-mri/filesystem-binding.cpp
+++ b/binding-mri/filesystem-binding.cpp
@@ -19,10 +19,11 @@
 ** along with mkxp.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-#include "util.h"
-#include "binding-util.h" //fatal error C1189 if this is before "util.h"
+#include "binding-util.h"
+
 #include "sharedstate.h"
 #include "filesystem.h"
+#include "util.h"
 
 #include "ruby/encoding.h"
 #include "ruby/intern.h"

From 55315a5258ef3a47b65c5b74c9c9dc7cd8a7f380 Mon Sep 17 00:00:00 2001
From: chosenofbear <chosenofbear>
Date: Thu, 18 Jun 2015 16:25:43 -0400
Subject: [PATCH 3/4] update crossplatform.h

---
 binding-mri/etc-binding.cpp   |  4 ----
 binding-mri/font-binding.cpp  |  4 ----
 binding-mri/input-binding.cpp |  4 ----
 src/crossplatform.h           | 14 ++++++++++++--
 src/exception.h               |  5 ++---
 src/util.h                    |  5 ++---
 6 files changed, 16 insertions(+), 20 deletions(-)

diff --git a/binding-mri/etc-binding.cpp b/binding-mri/etc-binding.cpp
index 931f182..2c2cdd2 100644
--- a/binding-mri/etc-binding.cpp
+++ b/binding-mri/etc-binding.cpp
@@ -19,10 +19,6 @@
 ** along with mkxp.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-#ifdef _MSC_VER
-#define _CRT_SECURE_NO_DEPRECATE
-#endif
-
 #include "etc.h"
 #include "binding-util.h"
 #include "serializable-binding.h"
diff --git a/binding-mri/font-binding.cpp b/binding-mri/font-binding.cpp
index df1efbc..2a8856a 100644
--- a/binding-mri/font-binding.cpp
+++ b/binding-mri/font-binding.cpp
@@ -19,10 +19,6 @@
 ** along with mkxp.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-#ifdef _MSC_VER
-#define _CRT_SECURE_NO_DEPRECATE
-#endif
-
 #include "font.h"
 #include "binding-util.h"
 #include "binding-types.h"
diff --git a/binding-mri/input-binding.cpp b/binding-mri/input-binding.cpp
index 5c00dd3..f20b06a 100644
--- a/binding-mri/input-binding.cpp
+++ b/binding-mri/input-binding.cpp
@@ -19,10 +19,6 @@
 ** along with mkxp.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-#ifdef _MSC_VER
-#define _CRT_SECURE_NO_DEPRECATE
-#endif
-
 #include "input.h"
 #include "sharedstate.h"
 #include "exception.h"
diff --git a/src/crossplatform.h b/src/crossplatform.h
index 9f4bef6..26f4993 100644
--- a/src/crossplatform.h
+++ b/src/crossplatform.h
@@ -1,7 +1,17 @@
+#ifndef MKXP_CROSSPLATFORM_H
+#define MKXP_CROSSPLATFORM_H
+
 #ifdef _MSC_VER
-#define snprintf _snprintf
+//#define _CRT_SECURE_NO_DEPRECATE //should be defined in binding-mri's preprocessor for vsnprintf, fopen
 
 #include <direct.h>
 
+#ifndef snprintf
+#define snprintf _snprintf
+#endif
+#ifndef chdir
 #define chdir _chdir
-#endif
\ No newline at end of file
+#endif
+#endif
+
+#endif //MKXP_CROSSPLATFORM_H
diff --git a/src/exception.h b/src/exception.h
index 5746df7..02db28e 100644
--- a/src/exception.h
+++ b/src/exception.h
@@ -22,9 +22,8 @@
 #ifndef EXCEPTION_H
 #define EXCEPTION_H
 
-#ifdef _MSC_VER
-#define _CRT_SECURE_NO_DEPRECATE
-#endif
+#include "crossplatform.h"
+
 #include <string>
 #include <stdio.h>
 #include <stdarg.h>
diff --git a/src/util.h b/src/util.h
index f2461cd..214d057 100644
--- a/src/util.h
+++ b/src/util.h
@@ -22,9 +22,8 @@
 #ifndef UTIL_H
 #define UTIL_H
 
-#ifdef _MSC_VER
-#define _CRT_SECURE_NO_DEPRECATE
-#endif
+#include "crossplatform.h"
+
 #include <stdio.h>
 #include <string>
 #include <algorithm>

From 99dfe731e12d8f8e0c20190c723bbdcd742cbd01 Mon Sep 17 00:00:00 2001
From: chosenofbear <chosenofbear>
Date: Tue, 23 Jun 2015 09:25:15 -0400
Subject: [PATCH 4/4] fix compile in VC++ with INI_ENCODING option

---
 src/config.cpp | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/src/config.cpp b/src/config.cpp
index cbbbe99..5db26f5 100644
--- a/src/config.cpp
+++ b/src/config.cpp
@@ -348,7 +348,11 @@ void Config::readGameINI()
 			size_t inLen = game.title.size();
 			size_t outLen = inLen * 4;
 			std::string buf(outLen, '\0');
+#ifdef _MSC_VER
+			const char *inPtr = game.title.c_str();
+#else
 			char *inPtr = const_cast<char*>(game.title.c_str());
+#endif
 			char *outPtr = const_cast<char*>(buf.c_str());
 
 			errno = 0;