merge default here sdl2transition
authorunc0rr
Tue, 17 Dec 2013 00:02:52 +0400
branchsdl2transition
changeset 9798 f2b18754742f
parent 9711 7d0329f37181 (diff)
parent 9797 1fdc1507e42d (current diff)
child 11360 7a7611adf715
merge default here
hedgewars/CMakeLists.txt
hedgewars/uChat.pas
hedgewars/uInputHandler.pas
hedgewars/uLandGraphics.pas
hedgewars/uLandObjects.pas
hedgewars/uStore.pas
hedgewars/uWorld.pas
project_files/hedgewars.pro
--- a/CMakeLists.txt	Sun Dec 15 14:05:42 2013 -0500
+++ b/CMakeLists.txt	Tue Dec 17 00:02:52 2013 +0400
@@ -15,6 +15,7 @@
 option(NOSERVER "Disable gameServer build (off)]" OFF)
 option(NOPNG "Disable screenshoot compression (off)" OFF)
 option(NOVIDEOREC "Disable video recording (off)" OFF)
+option(NOSDL2 "Disable SDL2+ and use SDL 1.2 (off)" OFF)
 
 #libraries are built shared unless explicitly added as a static
 option(BUILD_SHARED_LIBS "Build libraries as shared modules (on)" ON)
@@ -49,11 +50,12 @@
 
 message(STATUS "Building ${HEDGEWARS_VERSION}-r${HEDGEWARS_REVISION} (${HEDGEWARS_HASH})")
 
+#io library paths
+include(${CMAKE_MODULE_PATH}/paths.cmake)
 #general utilities
 include(${CMAKE_MODULE_PATH}/utils.cmake)
 #platform specific init code
 include(${CMAKE_MODULE_PATH}/platform.cmake)
-include(${CMAKE_MODULE_PATH}/paths.cmake)
 
 
 #when build type is not specified, assume Debug/Release according to build version information
--- a/QTfrontend/CMakeLists.txt	Sun Dec 15 14:05:42 2013 -0500
+++ b/QTfrontend/CMakeLists.txt	Tue Dec 17 00:02:52 2013 +0400
@@ -19,8 +19,17 @@
     message(FATAL_ERROR "This version of QT is known *not* to work, please update or use a lower version")
 endif()
 
-find_package(SDL1or2)            #video in SDLInteraction
-find_package(SDL_mixer REQUIRED) #audio in SDLInteraction
+if(${NOSDL2})
+    find_package(SDL REQUIRED)            #video in SDLInteraction
+    find_package(SDL_mixer REQUIRED) #audio in SDLInteraction
+    include_directories(${SDL_INCLUDE_DIR})
+    include_directories(${SDLMIXER_INCLUDE_DIR})
+else(${NOSDL2})
+    find_package(SDL2 REQUIRED)
+    find_package(SDL2_mixer REQUIRED) #audio in SDLInteraction
+    include_directories(${SDL2_INCLUDE_DIR})
+    include_directories(${SDL2MIXER_INCLUDE_DIR})
+endif(${NOSDL2})
 
 if(${FFMPEG_FOUND})
     add_definitions(-DVIDEOREC -D__STDC_CONSTANT_MACROS)
@@ -59,8 +68,6 @@
 include_directories(${CMAKE_CURRENT_SOURCE_DIR}/ui/widget)
 include_directories(${CMAKE_CURRENT_SOURCE_DIR}/util)
 include_directories(${CMAKE_CURRENT_SOURCE_DIR}/util/platform)
-include_directories(${SDL_INCLUDE_DIR})
-include_directories(${SDLMIXER_INCLUDE_DIR})
 include_directories(${PHYSFS_INCLUDE_DIR})
 include_directories(${PHYSLAYER_INCLUDE_DIR})
 
@@ -198,10 +205,20 @@
 list(APPEND HW_LINK_LIBS
     physfs physlayer
     ${QT_LIBRARIES}
-    ${SDL_LIBRARY}
-    ${SDLMIXER_LIBRARY}
     )
 
+if(NOSDL2)
+    list(APPEND HW_LINK_LIBS
+        ${SDL_LIBRARY}
+        ${SDLMIXER_LIBRARY}
+        )
+else()
+    list(APPEND HW_LINK_LIBS
+        ${SDL2_LIBRARY}
+        ${SDL2MIXER_LIBRARY}
+        )
+endif()
+
 if(WIN32 AND NOT UNIX)
     if(NOT SDL_LIBRARY)
         list(APPEND HW_LINK_LIBS SDL)
--- a/QTfrontend/gameuiconfig.cpp	Sun Dec 15 14:05:42 2013 -0500
+++ b/QTfrontend/gameuiconfig.cpp	Tue Dec 17 00:02:52 2013 +0400
@@ -86,10 +86,11 @@
     else Form->ui.pageOptions->CBResolution->setCurrentIndex(t);
 
     // Default the windowed resolution to 5/6 of the screen size
-    int screenWidth = SDL_GetVideoInfo()->current_w * 5 / 6;
-    int screenHeight = SDL_GetVideoInfo()->current_h * 5 / 6;
-    QString widthStr; widthStr.setNum(screenWidth);
-    QString heightStr; heightStr.setNum(screenHeight);
+    QSize screenSize = SDLInteraction::instance().getCurrentResolution();
+    screenSize *= 5.0 / 6;
+
+    QString widthStr = QString::number(screenSize.width());
+    QString heightStr = QString::number(screenSize.height());
     QString wWidth = value("video/windowedWidth", widthStr).toString();
     QString wHeight = value("video/windowedHeight", heightStr).toString();
     // If left blank reset the resolution to the default
--- a/QTfrontend/main.cpp	Sun Dec 15 14:05:42 2013 -0500
+++ b/QTfrontend/main.cpp	Tue Dec 17 00:02:52 2013 +0400
@@ -37,6 +37,8 @@
 #include "FileEngine.h"
 #include "MessageDialog.h"
 
+#include "SDLInteraction.h"
+
 #ifdef _WIN32
 #include <Shlobj.h>
 #elif defined __APPLE__
@@ -132,6 +134,8 @@
     cocoaInit = new CocoaInitializer(); // Creates the autoreleasepool preventing cocoa object leaks on OS X.
 #endif
 
+    SDLInteraction::instance();
+
     HWApplication app(argc, argv);
 
     QLabel *splash = NULL;
--- a/QTfrontend/ui/widget/about.cpp	Sun Dec 15 14:05:42 2013 -0500
+++ b/QTfrontend/ui/widget/about.cpp	Tue Dec 17 00:02:52 2013 +0400
@@ -99,7 +99,14 @@
     libinfo.append(QString(tr("Unknown Compiler")).arg(__VERSION__) + QString("<br>"));
 #endif
 
-    const SDL_version *sdl_ver = SDL_Linked_Version();
+    const SDL_version *sdl_ver;
+    SDL_version sdl_version;
+#if SDL_MAJOR_VERSION == 2
+    SDL_GetVersion(&sdl_version);
+    sdl_ver = &sdl_version;
+#else
+    sdl_ver = SDL_Linked_Version();
+#endif
     libinfo.append(QString("<a href=\"http://www.libsdl.org/\">SDL</a> version: %1.%2.%3<br>")
         .arg(sdl_ver->major)
         .arg(sdl_ver->minor)
--- a/QTfrontend/util/SDLInteraction.cpp	Sun Dec 15 14:05:42 2013 -0500
+++ b/QTfrontend/util/SDLInteraction.cpp	Tue Dec 17 00:02:52 2013 +0400
@@ -84,6 +84,18 @@
 {
     QStringList result;
 
+#if SDL_VERSION_ATLEAST(2, 0, 0)
+    int modesNumber = SDL_GetNumDisplayModes(0);
+    SDL_DisplayMode mode;
+
+    for(int i = 0; i < modesNumber; ++i)
+    {
+        SDL_GetDisplayMode(0, i, &mode);
+
+        if ((mode.w >= 640) && (mode.h >= 480))
+            result << QString("%1x%2").arg(mode.w).arg(mode.h);
+    }
+#else
     SDL_Rect **modes;
 
     modes = SDL_ListModes(NULL, SDL_FULLSCREEN);
@@ -98,6 +110,7 @@
             if ((modes[i]->w >= 640) && (modes[i]->h >= 480))
                 result << QString("%1x%2").arg(modes[i]->w).arg(modes[i]->h);
     }
+#endif
 
     return result;
 }
@@ -107,6 +120,9 @@
 {
     QStringList result;
 
+#if SDL_VERSION_ATLEAST(2, 0, 0)
+
+#else
     int i = 0;
     while(i < 1024 && sdlkeys[i][1][0] != '\0')
         i++;
@@ -177,7 +193,8 @@
 
     // Terminate the list
     sdlkeys[i][0][0] = '\0';
-    sdlkeys[i][1][0] = '\0';
+    sdlkeys[i][1][0] = '\0';   
+#endif
 }
 
 
@@ -239,7 +256,7 @@
     if (!m_audioInitialized) return;
 
     if (m_music == NULL)
-        m_music = Mix_LoadMUS_RW(PHYSFSRWOPS_openRead(m_musicTrack.toLocal8Bit().constData()));
+        m_music = Mix_LoadMUS_RW(PHYSFSRWOPS_openRead(m_musicTrack.toLocal8Bit().constData()), 0);
 
     Mix_VolumeMusic(MIX_MAX_VOLUME - 28);
     Mix_FadeInMusic(m_music, -1, 1750);
@@ -260,3 +277,17 @@
     m_isPlayingMusic = false;
 }
 
+
+QSize SDLInteraction::getCurrentResolution()
+{
+#if SDL_VERSION_ATLEAST(2, 0, 0)
+    SDL_DisplayMode mode;
+
+    SDL_GetDesktopDisplayMode(0, &mode);
+
+    return QSize(mode.w, mode.h);
+#else
+    SDL_VideoInfo * vi = SDL_GetVideoInfo();
+    return QSize(vi->current_w, vi->current_h);
+#endif
+}
--- a/QTfrontend/util/SDLInteraction.h	Sun Dec 15 14:05:42 2013 -0500
+++ b/QTfrontend/util/SDLInteraction.h	Tue Dec 17 00:02:52 2013 +0400
@@ -27,7 +27,15 @@
 
 #include <QMap>
 #include <QStringList>
+#include <QSize>
 
+// workaround some strange Qt and SLD2 interaction
+#ifdef Q_OS_MAC
+#  ifdef MAC_OS_X_VERSION_MIN_REQUIRED
+#    undef MAC_OS_X_VERSION_MIN_REQUIRED
+#    define MAC_OS_X_VERSION_MIN_REQUIRED MAC_OS_X_VERSION_10_6
+#  endif
+#endif
 #include "SDL_mixer.h"
 
 /**
@@ -103,6 +111,8 @@
 
         /// Fades out and stops the background music (if playing).
         void stopMusic();
+
+        QSize getCurrentResolution();
 };
 
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cmake_modules/FindSDL2_image.cmake	Tue Dec 17 00:02:52 2013 +0400
@@ -0,0 +1,88 @@
+# - Locate SDL2_image library
+# This module defines:
+#  SDL2_IMAGE_LIBRARIES, the name of the library to link against
+#  SDL2_IMAGE_INCLUDE_DIRS, where to find the headers
+#  SDL2_IMAGE_FOUND, if false, do not try to link against
+#  SDL2_IMAGE_VERSION_STRING - human-readable string containing the version of SDL2_image
+#
+# For backward compatiblity the following variables are also set:
+#  SDL2IMAGE_LIBRARY (same value as SDL2_IMAGE_LIBRARIES)
+#  SDL2IMAGE_INCLUDE_DIR (same value as SDL2_IMAGE_INCLUDE_DIRS)
+#  SDL2IMAGE_FOUND (same value as SDL2_IMAGE_FOUND)
+#
+# $SDL2DIR is an environment variable that would
+# correspond to the ./configure --prefix=$SDL2DIR
+# used in building SDL2.
+#
+# Created by Eric Wing. This was influenced by the FindSDL2.cmake
+# module, but with modifications to recognize OS X frameworks and
+# additional Unix paths (FreeBSD, etc).
+
+#=============================================================================
+# Copyright 2005-2009 Kitware, Inc.
+# Copyright 2012 Benjamin Eikel
+#
+# Distributed under the OSI-approved BSD License (the "License");
+# see accompanying file Copyright.txt for details.
+#
+# This software is distributed WITHOUT ANY WARRANTY; without even the
+# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+# See the License for more information.
+#=============================================================================
+# (To distribute this file outside of CMake, substitute the full
+#  License text for the above reference.)
+
+if(NOT SDL2_IMAGE_INCLUDE_DIR AND SDL2IMAGE_INCLUDE_DIR)
+  set(SDL2_IMAGE_INCLUDE_DIR ${SDL2IMAGE_INCLUDE_DIR} CACHE PATH "directory cache
+entry initialized from old variable name")
+endif()
+find_path(SDL2_IMAGE_INCLUDE_DIR SDL_image.h
+  HINTS
+    ENV SDL2IMAGEDIR
+    ENV SDL2DIR
+  PATH_SUFFIXES include/SDL2 include
+)
+
+if(NOT SDL2_IMAGE_LIBRARY AND SDL2IMAGE_LIBRARY)
+  set(SDL2_IMAGE_LIBRARY ${SDL2IMAGE_LIBRARY} CACHE FILEPATH "file cache entry
+initialized from old variable name")
+endif()
+find_library(SDL2_IMAGE_LIBRARY
+  NAMES SDL2_image
+  HINTS
+    ENV SDL2IMAGEDIR
+    ENV SDL2DIR
+  PATH_SUFFIXES lib
+)
+
+if(SDL2_IMAGE_INCLUDE_DIR AND EXISTS "${SDL2_IMAGE_INCLUDE_DIR}/SDL2_image.h")
+  file(STRINGS "${SDL2_IMAGE_INCLUDE_DIR}/SDL2_image.h" SDL2_IMAGE_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SDL2_IMAGE_MAJOR_VERSION[ \t]+[0-9]+$")
+  file(STRINGS "${SDL2_IMAGE_INCLUDE_DIR}/SDL2_image.h" SDL2_IMAGE_VERSION_MINOR_LINE REGEX "^#define[ \t]+SDL2_IMAGE_MINOR_VERSION[ \t]+[0-9]+$")
+  file(STRINGS "${SDL2_IMAGE_INCLUDE_DIR}/SDL2_image.h" SDL2_IMAGE_VERSION_PATCH_LINE REGEX "^#define[ \t]+SDL2_IMAGE_PATCHLEVEL[ \t]+[0-9]+$")
+  string(REGEX REPLACE "^#define[ \t]+SDL2_IMAGE_MAJOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_IMAGE_VERSION_MAJOR "${SDL2_IMAGE_VERSION_MAJOR_LINE}")
+  string(REGEX REPLACE "^#define[ \t]+SDL2_IMAGE_MINOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_IMAGE_VERSION_MINOR "${SDL2_IMAGE_VERSION_MINOR_LINE}")
+  string(REGEX REPLACE "^#define[ \t]+SDL2_IMAGE_PATCHLEVEL[ \t]+([0-9]+)$" "\\1" SDL2_IMAGE_VERSION_PATCH "${SDL2_IMAGE_VERSION_PATCH_LINE}")
+  set(SDL2_IMAGE_VERSION_STRING ${SDL2_IMAGE_VERSION_MAJOR}.${SDL2_IMAGE_VERSION_MINOR}.${SDL2_IMAGE_VERSION_PATCH})
+  unset(SDL2_IMAGE_VERSION_MAJOR_LINE)
+  unset(SDL2_IMAGE_VERSION_MINOR_LINE)
+  unset(SDL2_IMAGE_VERSION_PATCH_LINE)
+  unset(SDL2_IMAGE_VERSION_MAJOR)
+  unset(SDL2_IMAGE_VERSION_MINOR)
+  unset(SDL2_IMAGE_VERSION_PATCH)
+endif()
+
+set(SDL2_IMAGE_LIBRARIES ${SDL2_IMAGE_LIBRARY})
+set(SDL2_IMAGE_INCLUDE_DIRS ${SDL2_IMAGE_INCLUDE_DIR})
+
+include(FindPackageHandleStandardArgs)
+
+FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2_image
+                                  REQUIRED_VARS SDL2_IMAGE_LIBRARIES SDL2_IMAGE_INCLUDE_DIRS
+                                  VERSION_VAR SDL2_IMAGE_VERSION_STRING)
+
+# for backward compatiblity
+set(SDL2IMAGE_LIBRARY ${SDL2_IMAGE_LIBRARIES})
+set(SDL2IMAGE_INCLUDE_DIR ${SDL2_IMAGE_INCLUDE_DIRS})
+set(SDL2IMAGE_FOUND ${SDL2_IMAGE_FOUND})
+
+mark_as_advanced(SDL2_IMAGE_LIBRARY SDL2_IMAGE_INCLUDE_DIR)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cmake_modules/FindSDL2_mixer.cmake	Tue Dec 17 00:02:52 2013 +0400
@@ -0,0 +1,88 @@
+# - Locate SDL2_mixer library
+# This module defines:
+#  SDL2_MIXER_LIBRARIES, the name of the library to link against
+#  SDL2_MIXER_INCLUDE_DIRS, where to find the headers
+#  SDL2_MIXER_FOUND, if false, do not try to link against
+#  SDL2_MIXER_VERSION_STRING - human-readable string containing the version of SDL2_mixer
+#
+# For backward compatiblity the following variables are also set:
+#  SDL2MIXER_LIBRARY (same value as SDL2_MIXER_LIBRARIES)
+#  SDL2MIXER_INCLUDE_DIR (same value as SDL2_MIXER_INCLUDE_DIRS)
+#  SDL2MIXER_FOUND (same value as SDL2_MIXER_FOUND)
+#
+# $SDL2DIR is an environment variable that would
+# correspond to the ./configure --prefix=$SDL2DIR
+# used in building SDL2.
+#
+# Created by Eric Wing. This was influenced by the FindSDL2.cmake
+# module, but with modifications to recognize OS X frameworks and
+# additional Unix paths (FreeBSD, etc).
+
+#=============================================================================
+# Copyright 2005-2009 Kitware, Inc.
+# Copyright 2012 Benjamin Eikel
+#
+# Distributed under the OSI-approved BSD License (the "License");
+# see accompanying file Copyright.txt for details.
+#
+# This software is distributed WITHOUT ANY WARRANTY; without even the
+# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+# See the License for more information.
+#=============================================================================
+# (To distribute this file outside of CMake, substitute the full
+#  License text for the above reference.)
+
+if(NOT SDL2_MIXER_INCLUDE_DIR AND SDL2MIXER_INCLUDE_DIR)
+  set(SDL2_MIXER_INCLUDE_DIR ${SDL2MIXER_INCLUDE_DIR} CACHE PATH "directory cache
+entry initialized from old variable name")
+endif()
+find_path(SDL2_MIXER_INCLUDE_DIR SDL_mixer.h
+  HINTS
+    ENV SDL2MIXERDIR
+    ENV SDL2DIR
+  PATH_SUFFIXES include/SDL2 include
+)
+
+if(NOT SDL2_MIXER_LIBRARY AND SDL2MIXER_LIBRARY)
+  set(SDL2_MIXER_LIBRARY ${SDL2MIXER_LIBRARY} CACHE FILEPATH "file cache entry
+initialized from old variable name")
+endif()
+find_library(SDL2_MIXER_LIBRARY
+  NAMES SDL2_mixer
+  HINTS
+    ENV SDL2MIXERDIR
+    ENV SDL2DIR
+  PATH_SUFFIXES lib
+)
+
+if(SDL2_MIXER_INCLUDE_DIR AND EXISTS "${SDL2_MIXER_INCLUDE_DIR}/SDL2_mixer.h")
+  file(STRINGS "${SDL2_MIXER_INCLUDE_DIR}/SDL2_mixer.h" SDL2_MIXER_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SDL2_MIXER_MAJOR_VERSION[ \t]+[0-9]+$")
+  file(STRINGS "${SDL2_MIXER_INCLUDE_DIR}/SDL2_mixer.h" SDL2_MIXER_VERSION_MINOR_LINE REGEX "^#define[ \t]+SDL2_MIXER_MINOR_VERSION[ \t]+[0-9]+$")
+  file(STRINGS "${SDL2_MIXER_INCLUDE_DIR}/SDL2_mixer.h" SDL2_MIXER_VERSION_PATCH_LINE REGEX "^#define[ \t]+SDL2_MIXER_PATCHLEVEL[ \t]+[0-9]+$")
+  string(REGEX REPLACE "^#define[ \t]+SDL2_MIXER_MAJOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_MIXER_VERSION_MAJOR "${SDL2_MIXER_VERSION_MAJOR_LINE}")
+  string(REGEX REPLACE "^#define[ \t]+SDL2_MIXER_MINOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_MIXER_VERSION_MINOR "${SDL2_MIXER_VERSION_MINOR_LINE}")
+  string(REGEX REPLACE "^#define[ \t]+SDL2_MIXER_PATCHLEVEL[ \t]+([0-9]+)$" "\\1" SDL2_MIXER_VERSION_PATCH "${SDL2_MIXER_VERSION_PATCH_LINE}")
+  set(SDL2_MIXER_VERSION_STRING ${SDL2_MIXER_VERSION_MAJOR}.${SDL2_MIXER_VERSION_MINOR}.${SDL2_MIXER_VERSION_PATCH})
+  unset(SDL2_MIXER_VERSION_MAJOR_LINE)
+  unset(SDL2_MIXER_VERSION_MINOR_LINE)
+  unset(SDL2_MIXER_VERSION_PATCH_LINE)
+  unset(SDL2_MIXER_VERSION_MAJOR)
+  unset(SDL2_MIXER_VERSION_MINOR)
+  unset(SDL2_MIXER_VERSION_PATCH)
+endif()
+
+set(SDL2_MIXER_LIBRARIES ${SDL2_MIXER_LIBRARY})
+set(SDL2_MIXER_INCLUDE_DIRS ${SDL2_MIXER_INCLUDE_DIR})
+
+include(FindPackageHandleStandardArgs)
+
+FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2_mixer
+                                  REQUIRED_VARS SDL2_MIXER_LIBRARIES SDL2_MIXER_INCLUDE_DIRS
+                                  VERSION_VAR SDL2_MIXER_VERSION_STRING)
+
+# for backward compatiblity
+set(SDL2MIXER_LIBRARY ${SDL2_MIXER_LIBRARIES})
+set(SDL2MIXER_INCLUDE_DIR ${SDL2_MIXER_INCLUDE_DIRS})
+set(SDL2MIXER_FOUND ${SDL2_MIXER_FOUND})
+
+mark_as_advanced(SDL2_MIXER_LIBRARY SDL2_MIXER_INCLUDE_DIR)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cmake_modules/FindSDL2_net.cmake	Tue Dec 17 00:02:52 2013 +0400
@@ -0,0 +1,88 @@
+# - Locate SDL2_net library
+# This module defines:
+#  SDL2_NET_LIBRARIES, the name of the library to link against
+#  SDL2_NET_INCLUDE_DIRS, where to find the headers
+#  SDL2_NET_FOUND, if false, do not try to link against
+#  SDL2_NET_VERSION_STRING - human-readable string containing the version of SDL2_net
+#
+# For backward compatiblity the following variables are also set:
+#  SDL2NET_LIBRARY (same value as SDL2_NET_LIBRARIES)
+#  SDL2NET_INCLUDE_DIR (same value as SDL2_NET_INCLUDE_DIRS)
+#  SDL2NET_FOUND (same value as SDL2_NET_FOUND)
+#
+# $SDL2DIR is an environment variable that would
+# correspond to the ./configure --prefix=$SDL2DIR
+# used in building SDL2.
+#
+# Created by Eric Wing. This was influenced by the FindSDL2.cmake
+# module, but with modifications to recognize OS X frameworks and
+# additional Unix paths (FreeBSD, etc).
+
+#=============================================================================
+# Copyright 2005-2009 Kitware, Inc.
+# Copyright 2012 Benjamin Eikel
+#
+# Distributed under the OSI-approved BSD License (the "License");
+# see accompanying file Copyright.txt for details.
+#
+# This software is distributed WITHOUT ANY WARRANTY; without even the
+# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+# See the License for more information.
+#=============================================================================
+# (To distribute this file outside of CMake, substitute the full
+#  License text for the above reference.)
+
+if(NOT SDL2_NET_INCLUDE_DIR AND SDL2NET_INCLUDE_DIR)
+  set(SDL2_NET_INCLUDE_DIR ${SDL2NET_INCLUDE_DIR} CACHE PATH "directory cache
+entry initialized from old variable name")
+endif()
+find_path(SDL2_NET_INCLUDE_DIR SDL_net.h
+  HINTS
+    ENV SDL2NETDIR
+    ENV SDL2DIR
+  PATH_SUFFIXES include/SDL2 include
+)
+
+if(NOT SDL2_NET_LIBRARY AND SDL2NET_LIBRARY)
+  set(SDL2_NET_LIBRARY ${SDL2NET_LIBRARY} CACHE FILEPATH "file cache entry
+initialized from old variable name")
+endif()
+find_library(SDL2_NET_LIBRARY
+  NAMES SDL2_net
+  HINTS
+    ENV SDL2NETDIR
+    ENV SDL2DIR
+  PATH_SUFFIXES lib
+)
+
+if(SDL2_NET_INCLUDE_DIR AND EXISTS "${SDL2_NET_INCLUDE_DIR}/SDL2_net.h")
+  file(STRINGS "${SDL2_NET_INCLUDE_DIR}/SDL2_net.h" SDL2_NET_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SDL2_NET_MAJOR_VERSION[ \t]+[0-9]+$")
+  file(STRINGS "${SDL2_NET_INCLUDE_DIR}/SDL2_net.h" SDL2_NET_VERSION_MINOR_LINE REGEX "^#define[ \t]+SDL2_NET_MINOR_VERSION[ \t]+[0-9]+$")
+  file(STRINGS "${SDL2_NET_INCLUDE_DIR}/SDL2_net.h" SDL2_NET_VERSION_PATCH_LINE REGEX "^#define[ \t]+SDL2_NET_PATCHLEVEL[ \t]+[0-9]+$")
+  string(REGEX REPLACE "^#define[ \t]+SDL2_NET_MAJOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_NET_VERSION_MAJOR "${SDL2_NET_VERSION_MAJOR_LINE}")
+  string(REGEX REPLACE "^#define[ \t]+SDL2_NET_MINOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_NET_VERSION_MINOR "${SDL2_NET_VERSION_MINOR_LINE}")
+  string(REGEX REPLACE "^#define[ \t]+SDL2_NET_PATCHLEVEL[ \t]+([0-9]+)$" "\\1" SDL2_NET_VERSION_PATCH "${SDL2_NET_VERSION_PATCH_LINE}")
+  set(SDL2_NET_VERSION_STRING ${SDL2_NET_VERSION_MAJOR}.${SDL2_NET_VERSION_MINOR}.${SDL2_NET_VERSION_PATCH})
+  unset(SDL2_NET_VERSION_MAJOR_LINE)
+  unset(SDL2_NET_VERSION_MINOR_LINE)
+  unset(SDL2_NET_VERSION_PATCH_LINE)
+  unset(SDL2_NET_VERSION_MAJOR)
+  unset(SDL2_NET_VERSION_MINOR)
+  unset(SDL2_NET_VERSION_PATCH)
+endif()
+
+set(SDL2_NET_LIBRARIES ${SDL2_NET_LIBRARY})
+set(SDL2_NET_INCLUDE_DIRS ${SDL2_NET_INCLUDE_DIR})
+
+include(FindPackageHandleStandardArgs)
+
+FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2_net
+                                  REQUIRED_VARS SDL2_NET_LIBRARIES SDL2_NET_INCLUDE_DIRS
+                                  VERSION_VAR SDL2_NET_VERSION_STRING)
+
+# for backward compatiblity
+set(SDL2NET_LIBRARY ${SDL2_NET_LIBRARIES})
+set(SDL2NET_INCLUDE_DIR ${SDL2_NET_INCLUDE_DIRS})
+set(SDL2NET_FOUND ${SDL2_NET_FOUND})
+
+mark_as_advanced(SDL2_NET_LIBRARY SDL2_NET_INCLUDE_DIR)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cmake_modules/FindSDL2_ttf.cmake	Tue Dec 17 00:02:52 2013 +0400
@@ -0,0 +1,88 @@
+# - Locate SDL2_ttf library
+# This module defines:
+#  SDL2_TTF_LIBRARIES, the name of the library to link against
+#  SDL2_TTF_INCLUDE_DIRS, where to find the headers
+#  SDL2_TTF_FOUND, if false, do not try to link against
+#  SDL2_TTF_VERSION_STRING - human-readable string containing the version of SDL2_ttf
+#
+# For backward compatiblity the following variables are also set:
+#  SDL2TTF_LIBRARY (same value as SDL2_TTF_LIBRARIES)
+#  SDL2TTF_INCLUDE_DIR (same value as SDL2_TTF_INCLUDE_DIRS)
+#  SDL2TTF_FOUND (same value as SDL2_TTF_FOUND)
+#
+# $SDL2DIR is an environment variable that would
+# correspond to the ./configure --prefix=$SDL2DIR
+# used in building SDL2.
+#
+# Created by Eric Wing. This was influenced by the FindSDL2.cmake
+# module, but with modifications to recognize OS X frameworks and
+# additional Unix paths (FreeBSD, etc).
+
+#=============================================================================
+# Copyright 2005-2009 Kitware, Inc.
+# Copyright 2012 Benjamin Eikel
+#
+# Distributed under the OSI-approved BSD License (the "License");
+# see accompanying file Copyright.txt for details.
+#
+# This software is distributed WITHOUT ANY WARRANTY; without even the
+# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+# See the License for more information.
+#=============================================================================
+# (To distribute this file outside of CMake, substitute the full
+#  License text for the above reference.)
+
+if(NOT SDL2_TTF_INCLUDE_DIR AND SDL2TTF_INCLUDE_DIR)
+  set(SDL2_TTF_INCLUDE_DIR ${SDL2TTF_INCLUDE_DIR} CACHE PATH "directory cache
+entry initialized from old variable name")
+endif()
+find_path(SDL2_TTF_INCLUDE_DIR SDL_ttf.h
+  HINTS
+    ENV SDL2TTFDIR
+    ENV SDL2DIR
+  PATH_SUFFIXES include/SDL2 include
+)
+
+if(NOT SDL2_TTF_LIBRARY AND SDL2TTF_LIBRARY)
+  set(SDL2_TTF_LIBRARY ${SDL2TTF_LIBRARY} CACHE FILEPATH "file cache entry
+initialized from old variable name")
+endif()
+find_library(SDL2_TTF_LIBRARY
+  NAMES SDL2_ttf
+  HINTS
+    ENV SDL2TTFDIR
+    ENV SDL2DIR
+  PATH_SUFFIXES lib
+)
+
+if(SDL2_TTF_INCLUDE_DIR AND EXISTS "${SDL2_TTF_INCLUDE_DIR}/SDL2_ttf.h")
+  file(STRINGS "${SDL2_TTF_INCLUDE_DIR}/SDL2_ttf.h" SDL2_TTF_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SDL2_TTF_MAJOR_VERSION[ \t]+[0-9]+$")
+  file(STRINGS "${SDL2_TTF_INCLUDE_DIR}/SDL2_ttf.h" SDL2_TTF_VERSION_MINOR_LINE REGEX "^#define[ \t]+SDL2_TTF_MINOR_VERSION[ \t]+[0-9]+$")
+  file(STRINGS "${SDL2_TTF_INCLUDE_DIR}/SDL2_ttf.h" SDL2_TTF_VERSION_PATCH_LINE REGEX "^#define[ \t]+SDL2_TTF_PATCHLEVEL[ \t]+[0-9]+$")
+  string(REGEX REPLACE "^#define[ \t]+SDL2_TTF_MAJOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_TTF_VERSION_MAJOR "${SDL2_TTF_VERSION_MAJOR_LINE}")
+  string(REGEX REPLACE "^#define[ \t]+SDL2_TTF_MINOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_TTF_VERSION_MINOR "${SDL2_TTF_VERSION_MINOR_LINE}")
+  string(REGEX REPLACE "^#define[ \t]+SDL2_TTF_PATCHLEVEL[ \t]+([0-9]+)$" "\\1" SDL2_TTF_VERSION_PATCH "${SDL2_TTF_VERSION_PATCH_LINE}")
+  set(SDL2_TTF_VERSION_STRING ${SDL2_TTF_VERSION_MAJOR}.${SDL2_TTF_VERSION_MINOR}.${SDL2_TTF_VERSION_PATCH})
+  unset(SDL2_TTF_VERSION_MAJOR_LINE)
+  unset(SDL2_TTF_VERSION_MINOR_LINE)
+  unset(SDL2_TTF_VERSION_PATCH_LINE)
+  unset(SDL2_TTF_VERSION_MAJOR)
+  unset(SDL2_TTF_VERSION_MINOR)
+  unset(SDL2_TTF_VERSION_PATCH)
+endif()
+
+set(SDL2_TTF_LIBRARIES ${SDL2_TTF_LIBRARY})
+set(SDL2_TTF_INCLUDE_DIRS ${SDL2_TTF_INCLUDE_DIR})
+
+include(FindPackageHandleStandardArgs)
+
+FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2_ttf
+                                  REQUIRED_VARS SDL2_TTF_LIBRARIES SDL2_TTF_INCLUDE_DIRS
+                                  VERSION_VAR SDL2_TTF_VERSION_STRING)
+
+# for backward compatiblity
+set(SDL2TTF_LIBRARY ${SDL2_TTF_LIBRARIES})
+set(SDL2TTF_INCLUDE_DIR ${SDL2_TTF_INCLUDE_DIRS})
+set(SDL2TTF_FOUND ${SDL2_TTF_FOUND})
+
+mark_as_advanced(SDL2_TTF_LIBRARY SDL2_TTF_INCLUDE_DIR)
--- a/cmake_modules/platform.cmake	Sun Dec 15 14:05:42 2013 -0500
+++ b/cmake_modules/platform.cmake	Tue Dec 17 00:02:52 2013 +0400
@@ -36,6 +36,12 @@
         message(FATAL_ERROR "Hedgewars is not supported on Mac OS X pre-10.4")
     endif()
 
+    #gcc is EOL on these systems
+    if (current_macosx_version VERSION_GREATER "10.8")
+        set(CMAKE_C_COMPILER clang)
+        set(CMAKE_CXX_COMPILER clang++)
+    endif()
+
     #workaround for http://playcontrol.net/ewing/jibberjabber/big_behind-the-scenes_chang.html#SDL_mixer (Update 2)
     if(current_macosx_version VERSION_EQUAL "10.4")
         find_package(SDL_mixer REQUIRED)
@@ -97,6 +103,9 @@
 
     #add user framework directory
     add_flag_append(CMAKE_Pascal_FLAGS "-Ff~/Library/Frameworks")
+
+    #workaround most of the -Fl settings getting lost
+    add_flag_append(CMAKE_Pascal_FLAGS "-k-L${LIBRARY_OUTPUT_PATH}")
 endif(APPLE)
 
 if(MINGW)
--- a/hedgewars/CMakeLists.txt	Sun Dec 15 14:05:42 2013 -0500
+++ b/hedgewars/CMakeLists.txt	Tue Dec 17 00:02:52 2013 +0400
@@ -1,13 +1,22 @@
-find_package(SDL1or2)
-find_package(SDL_image)
-find_package(SDL_net)
-find_package(SDL_ttf)
-find_package(SDL_mixer)
+enable_language(Pascal)
+
+if(${NOSDL2})
+    find_package(SDL REQUIRED)
+    find_package(SDL_image REQUIRED)
+    find_package(SDL_net REQUIRED)
+    find_package(SDL_ttf REQUIRED)
+    find_package(SDL_mixer REQUIRED)
+else(${NOSDL2})
+    find_package(SDL2 REQUIRED)
+    find_package(SDL2_image REQUIRED)
+    find_package(SDL2_net REQUIRED)
+    find_package(SDL2_ttf REQUIRED)
+    find_package(SDL2_mixer REQUIRED)
+    add_definitions(-dSDL2)
+endif(${NOSDL2})
 
 include (CheckLibraryExists)
 
-
-enable_language(Pascal)
 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config.inc.in ${CMAKE_CURRENT_BINARY_DIR}/config.inc)
 include_directories(${CMAKE_CURRENT_BINARY_DIR})
 
@@ -99,7 +108,7 @@
 
 
 #DEPENDECIES AND EXECUTABLES SECTION
-if(NOT ${BUILD_ENGINE_LIBRARY} AND APPLE)
+if(NOT ${BUILD_ENGINE_LIBRARY} AND APPLE AND ${NOSDL2})
     #on OSX we need to provide the SDL_main() function when building as executable
     add_subdirectory(sdlmain)
     list(APPEND HW_LINK_LIBS SDLmain)
@@ -111,8 +120,11 @@
     list(APPEND HW_LINK_LIBS avwrapper)
     add_definitions(-dUSE_VIDEO_RECORDING)
     add_flag_append(CMAKE_Pascal_FLAGS -Fl${LIBRARY_OUTPUT_PATH})
-    #only for SDL < 2, linking carried out by fpc
-    find_package_or_disable_msg(GLUT NOVIDEOREC "Video recording will not be built")
+    
+    if(${NOSDL2})
+        #only for SDL < 2, linking carried out by fpc
+        find_package_or_disable_msg(GLUT NOVIDEOREC "Video recording will not be built")
+    endif(${NOSDL2})
 endif()
 
 find_package_or_disable_msg(PNG NOPNG "Screenshots will be saved in BMP")
@@ -148,20 +160,18 @@
 list(APPEND HW_LINK_LIBS physlayer)
 
 #Mix_Init/Mix_Quit from SDL_mixer 1.2.10
-check_library_exists(${SDLMIXER_LIBRARY} Mix_Init "" HAVE_MIXINIT)
-if(HAVE_MIXINIT)
-    add_definitions(-dSDL_MIXER_NEWER)
-endif(HAVE_MIXINIT)
+if(${NOSDL2})
+    check_library_exists(${SDLMIXER_LIBRARY} Mix_Init "" HAVE_MIXINIT)
+    if(HAVE_MIXINIT)
+        add_definitions(-dSDL_MIXER_NEWER)
+    endif(HAVE_MIXINIT)
 
-#IMG_Init/IMG_Quit from SDL_image 1.2.8
-check_library_exists(${SDLIMAGE_LIBRARY} IMG_Init "" HAVE_IMGINIT)
-if(HAVE_IMGINIT)
-    add_definitions(-dSDL_IMAGE_NEWER)
-endif(HAVE_IMGINIT)
-
-if(NOT (SDL_VERSION VERSION_LESS 2.0))
-    add_definitions(-dSDL2)
-endif()
+    #IMG_Init/IMG_Quit from SDL_image 1.2.8
+    check_library_exists(${SDLIMAGE_LIBRARY} IMG_Init "" HAVE_IMGINIT)
+    if(HAVE_IMGINIT)
+        add_definitions(-dSDL_IMAGE_NEWER)
+    endif(HAVE_IMGINIT)
+endif(${NOSDL2})
 
 #needs to be last
 add_definitions(-dDEBUGFILE)
--- a/hedgewars/SDLh.pas	Sun Dec 15 14:05:42 2013 -0500
+++ b/hedgewars/SDLh.pas	Tue Dec 17 00:02:52 2013 +0400
@@ -55,33 +55,57 @@
 
 {$IFDEF DARWIN}
     {$IFNDEF HWLIBRARY}
-        {$linklib SDLmain}
-        {$PASCALMAINNAME SDL_main}
-        {$linkframework Cocoa}
-        {$linkframework SDL}
-        {$linkframework SDL_net}
-        {$linkframework SDL_image}
-        {$linkframework SDL_ttf}
-        {$linkframework SDL_mixer}
-        {$linkframework OpenGL}
+        {$IFDEF SDL2}
+            {$linkframework SDL2}
+            {$linkframework SDL2_net}
+            {$linkframework SDL2_image}
+            {$linkframework SDL2_ttf}
+            {$linkframework SDL2_mixer}
+        {$ELSE}
+            {$linklib SDLmain}
+            {$PASCALMAINNAME SDL_main}
+            {$linkframework Cocoa}
+            {$linkframework OpenGL}
+            {$linkframework SDL}
+            {$linkframework SDL_net}
+            {$linkframework SDL_image}
+            {$linkframework SDL_ttf}
+            {$linkframework SDL_mixer}
+        {$ENDIF}
     {$ENDIF}
 {$ENDIF}
 
 
 (*  SDL  *)
 const
-{$IFDEF WIN32}
-    SDLLibName = 'SDL.dll';
-    SDL_TTFLibName = 'SDL_ttf.dll';
-    SDL_MixerLibName = 'SDL_mixer.dll';
-    SDL_ImageLibName = 'SDL_image.dll';
-    SDL_NetLibName = 'SDL_net.dll';
+{$IFDEF SDL2}
+    {$IFDEF WIN32}
+        SDLLibName = 'SDL2.dll';
+        SDL_TTFLibName = 'SDL2_ttf.dll';
+        SDL_MixerLibName = 'SDL2_mixer.dll';
+        SDL_ImageLibName = 'SDL2_image.dll';
+        SDL_NetLibName = 'SDL2_net.dll';
+    {$ELSE}
+        SDLLibName = 'libSDL2';
+        SDL_TTFLibName = 'libSDL2_ttf';
+        SDL_MixerLibName = 'libSDL2_mixer';
+        SDL_ImageLibName = 'libSDL2_image';
+        SDL_NetLibName = 'libSDL2_net';
+    {$ENDIF}
 {$ELSE}
-    SDLLibName = 'libSDL';
-    SDL_TTFLibName = 'libSDL_ttf';
-    SDL_MixerLibName = 'libSDL_mixer';
-    SDL_ImageLibName = 'libSDL_image';
-    SDL_NetLibName = 'libSDL_net';
+    {$IFDEF WIN32}
+        SDLLibName = 'SDL.dll';
+        SDL_TTFLibName = 'SDL_ttf.dll';
+        SDL_MixerLibName = 'SDL_mixer.dll';
+        SDL_ImageLibName = 'SDL_image.dll';
+        SDL_NetLibName = 'SDL_net.dll';
+    {$ELSE}
+        SDLLibName = 'libSDL';
+        SDL_TTFLibName = 'libSDL_ttf';
+        SDL_MixerLibName = 'libSDL_mixer';
+        SDL_ImageLibName = 'libSDL_image';
+        SDL_NetLibName = 'libSDL_net';
+    {$ENDIF}
 {$ENDIF}
 
 /////////////////////////////////////////////////////////////////
@@ -115,6 +139,9 @@
 
 
 {$IFDEF SDL2}
+    SDL_TEXTEDITINGEVENT_TEXT_SIZE = 32;
+    SDL_TEXTINPUTEVENT_TEXT_SIZE   = 32;
+
     // SDL_Event types
     // pascal does not support unions as is, so we list here every possible event
     // and later associate a struct type each
@@ -519,21 +546,20 @@
         data1, data2: LongInt;
         end;
 
-    // available in sdl12 but not exposed
     TSDL_TextEditingEvent = record
-        type_: LongWord;
-        timestamp: LongWord;
-        windowID: LongWord;
-        text: array[0..31] of Byte;
-        start, lenght: LongInt;
+        type_: Longword;
+        timestamp: Longword;
+        windowID: Longword;
+        text: array [0..SDL_TEXTEDITINGEVENT_TEXT_SIZE - 1] of char;
+        start: LongInt;
+        length: LongInt;
         end;
 
-    // available in sdl12 but not exposed
     TSDL_TextInputEvent = record
-        type_: LongWord;
-        timestamp: LongWord;
-        windowID: LongWord;
-        text: array[0..31] of Byte;
+        type_: Longword;
+        timestamp: Longword;
+        windowID: Longword;
+        text: array [0..SDL_TEXTINPUTEVENT_TEXT_SIZE - 1] of char;
         end;
 
     TSDL_TouchFingerEvent = record
@@ -772,7 +798,7 @@
             SDL_KEYDOWN,
             SDL_KEYUP: (key: TSDL_KeyboardEvent);
             SDL_TEXTEDITING: (edit: TSDL_TextEditingEvent);
-            SDL_TEXTINPUT: (tedit: TSDL_TextInputEvent);
+            SDL_TEXTINPUT: (text: TSDL_TextInputEvent);
             SDL_MOUSEMOTION: (motion: TSDL_MouseMotionEvent);
             SDL_MOUSEBUTTONDOWN,
             SDL_MOUSEBUTTONUP: (button: TSDL_MouseButtonEvent);
@@ -986,7 +1012,7 @@
 
 function  SDL_GL_CreateContext(window: PSDL_Window): PSDL_GLContext; cdecl; external SDLLibName;
 procedure SDL_GL_DeleteContext(context: PSDL_GLContext); cdecl; external SDLLibName;
-function  SDL_GL_SwapWindow(window: PSDL_Window): LongInt; cdecl; external SDLLibName;
+procedure SDL_GL_SwapWindow(window: PSDL_Window); cdecl; external SDLLibName;
 function  SDL_GL_SetSwapInterval(interval: LongInt): LongInt; cdecl; external SDLLibName;
 
 procedure SDL_VideoQuit; cdecl; external SDLLibName;
@@ -1007,6 +1033,7 @@
 procedure SDL_WarpMouseInWindow(window: PSDL_Window; x, y: LongInt); cdecl; external SDLLibName;
 function  SDL_SetHint(name, value: PChar): Boolean; cdecl; external SDLLibName;
 procedure SDL_StartTextInput; cdecl; external SDLLibName;
+procedure SDL_StopTextInput; cdecl; external SDLLibName;
 
 function  SDL_PeepEvents(event: PSDL_Event; numevents: LongInt; action: TSDL_eventaction; minType, maxType: LongWord): LongInt; cdecl; external SDLLibName;
 
--- a/hedgewars/hwengine.pas	Sun Dec 15 14:05:42 2013 -0500
+++ b/hedgewars/hwengine.pas	Tue Dec 17 00:02:52 2013 +0400
@@ -154,17 +154,28 @@
             case event.type_ of
 {$IFDEF SDL2}
                 SDL_KEYDOWN:
-                    if GameState = gsChat then
-                        begin
-                    // sdl on iphone supports only ashii keyboards and the unicode field is deprecated in sdl 1.3
-                        KeyPressChat(SDL_GetKeyFromScancode(event.key.keysym.sym), event.key.keysym.sym); //TODO correct for keymodifiers
-                        end
-                    else
-                        if GameState >= gsGame then ProcessKey(event.key);
+                    if (GameState = gsChat) then
+                        KeyPressChat(event.key.keysym.sym)
+                    else if (GameState >= gsGame) then
+                        ProcessKey(event.key);
                 SDL_KEYUP:
                     if (GameState <> gsChat) and (GameState >= gsGame) then
                         ProcessKey(event.key);
 
+                SDL_MOUSEBUTTONDOWN:
+                    if GameState = gsConfirm then
+                        ParseCommand('quit', true)
+                    else
+                        if (GameState >= gsGame) then ProcessMouse(event.button, true);
+
+                SDL_MOUSEBUTTONUP:
+                    if (GameState >= gsGame) then ProcessMouse(event.button, false);
+
+                SDL_MOUSEWHEEL:
+                    ProcessMouseWheel(event.wheel.x, event.wheel.y);
+
+                SDL_TEXTINPUT: uChat.TextInput(event.text);
+
                 SDL_WINDOWEVENT:
                     if event.window.event = SDL_WINDOWEVENT_SHOWN then
                     begin
@@ -336,18 +347,18 @@
         AddFileLog(inttostr(i) + ': ' + ParamStr(i));
 
     WriteToConsole('Init SDL... ');
-    if not cOnlyStats then SDLTry(SDL_Init(SDL_INIT_VIDEO or SDL_INIT_NOPARACHUTE) >= 0, true);
+    if not cOnlyStats then SDLTry(SDL_Init(SDL_INIT_VIDEO or SDL_INIT_NOPARACHUTE) >= 0, 'SDL_Init', true);
     WriteLnToConsole(msgOK);
 
 {$IFDEF SDL2}
-    SDL_StartTextInput();
+    //SDL_StartTextInput();
 {$ELSE}
     SDL_EnableUNICODE(1);
 {$ENDIF}
     SDL_ShowCursor(0);
 
     WriteToConsole('Init SDL_ttf... ');
-    SDLTry(TTF_Init() <> -1, true);
+    SDLTry(TTF_Init() <> -1, 'TTF_Init', true);
     WriteLnToConsole(msgOK);
 
 {$IFDEF USE_VIDEO_RECORDING}
--- a/hedgewars/uChat.pas	Sun Dec 15 14:05:42 2013 -0500
+++ b/hedgewars/uChat.pas	Tue Dec 17 00:02:52 2013 +0400
@@ -21,6 +21,7 @@
 unit uChat;
 
 interface
+uses SDLh;
 
 procedure initModule;
 procedure freeModule;
@@ -28,11 +29,17 @@
 procedure CleanupInput;
 procedure AddChatString(s: shortstring);
 procedure DrawChat;
-procedure KeyPressChat(Key, Sym: Longword);
 procedure SendHogSpeech(s: shortstring);
 
+{$IFDEF SDL2}
+procedure KeyPressChat(Sym: Longword);
+procedure TextInput(var event: TSDL_TextInputEvent);
+{$ELSE}
+procedure KeyPressChat(Key, Sym: Longword);
+{$ENDIF}
+
 implementation
-uses SDLh, uInputHandler, uTypes, uVariables, uCommands, uUtils, uTextures, uRender, uIO;
+uses uInputHandler, uTypes, uVariables, uCommands, uUtils, uTextures, uRender, uIO;
 
 const MaxStrIndex = 27;
 
@@ -309,14 +316,38 @@
 begin
     FreezeEnterKey;
     history:= 0;
-{$IFNDEF SDL2}
+{$IFDEF SDL2}
+    SDL_StopTextInput();
+{$ELSE}
     SDL_EnableKeyRepeat(0,0);
 {$ENDIF}
     GameState:= gsGame;
     ResetKbd;
 end;
 
+{$IFDEF SDL2}
+procedure TextInput(var event: TSDL_TextInputEvent);
+var s: shortstring;
+    l: byte;
+begin
+    l:= 0;
+    while event.text[l] <> #0 do
+        begin
+        s[l + 1]:= event.text[l];
+        inc(l)
+        end;
+    s[0]:= char(l);
+
+    if byte(InputStr.s[0]) + l > 240 then exit;
+
+    InputStrL[byte(InputStr.s[0]) + l]:= InputStr.s[0];
+    SetLine(InputStr, InputStr.s + s, true)
+end;
+
+procedure KeyPressChat(Sym: Longword);
+{$ELSE}
 procedure KeyPressChat(Key, Sym: Longword);
+{$ENDIF}
 const firstByteMark: array[0..3] of byte = (0, $C0, $E0, $F0);
 var i, btw, index: integer;
     utf8: shortstring;
@@ -365,6 +396,8 @@
         else
             action:= false;
         end;
+
+{$IFNDEF SDL2}
     if not action and (Key <> 0) then
         begin
         if (Key < $80) then
@@ -392,6 +425,7 @@
         InputStrL[byte(InputStr.s[0]) + btw]:= InputStr.s[0];
         SetLine(InputStr, InputStr.s + utf8, true)
         end
+{$ENDIF}
 end;
 
 procedure chChatMessage(var s: shortstring);
@@ -434,7 +468,9 @@
 begin
     s:= s; // avoid compiler hint
     GameState:= gsChat;
-{$IFNDEF SDL2}
+{$IFDEF SDL2}
+    SDL_StartTextInput();
+{$ELSE}
     SDL_EnableKeyRepeat(200,45);
 {$ENDIF}
     if length(s) = 0 then
--- a/hedgewars/uDebug.pas	Sun Dec 15 14:05:42 2013 -0500
+++ b/hedgewars/uDebug.pas	Tue Dec 17 00:02:52 2013 +0400
@@ -24,7 +24,7 @@
 
 procedure OutError(Msg: shortstring; isFatalError: boolean);
 procedure TryDo(Assert: boolean; Msg: shortstring; isFatal: boolean); inline;
-procedure SDLTry(Assert: boolean; isFatal: boolean);
+procedure SDLTry(Assert: boolean; Msg: shortstring; isFatal: boolean);
 
 implementation
 uses SDLh, uConsole, uCommands;
@@ -42,13 +42,13 @@
     OutError(Msg, isFatal)
 end;
 
-procedure SDLTry(Assert: boolean; isFatal: boolean);
+procedure SDLTry(Assert: boolean; Msg: shortstring; isFatal: boolean);
 var s: shortstring;
 begin
 if not Assert then
     begin
     s:= SDL_GetError();
-    OutError(s, isFatal)
+    OutError(Msg + ': ' + s, isFatal)
     end
 end;
 
--- a/hedgewars/uIO.pas	Sun Dec 15 14:05:42 2013 -0500
+++ b/hedgewars/uIO.pas	Tue Dec 17 00:02:52 2013 +0400
@@ -106,16 +106,16 @@
 var ipaddr: TIPAddress;
 begin
     WriteToConsole('Init SDL_Net... ');
-    SDLTry(SDLNet_Init = 0, true);
+    SDLTry(SDLNet_Init = 0, 'SDLNet_Init', true);
     fds:= SDLNet_AllocSocketSet(1);
-    SDLTry(fds <> nil, true);
+    SDLTry(fds <> nil, 'SDLNet_AllocSocketSet', true);
     WriteLnToConsole(msgOK);
     WriteToConsole('Establishing IPC connection to tcp 127.0.0.1:' + IntToStr(ipcPort) + ' ');
     {$HINTS OFF}
-    SDLTry(SDLNet_ResolveHost(ipaddr, PChar('127.0.0.1'), ipcPort) = 0, true);
+    SDLTry(SDLNet_ResolveHost(ipaddr, PChar('127.0.0.1'), ipcPort) = 0, 'SDLNet_ResolveHost', true);
     {$HINTS ON}
     IPCSock:= SDLNet_TCP_Open(ipaddr);
-    SDLTry(IPCSock <> nil, true);
+    SDLTry(IPCSock <> nil, 'SDLNet_TCP_Open', true);
     WriteLnToConsole(msgOK)
 end;
 
--- a/hedgewars/uInputHandler.pas	Sun Dec 15 14:05:42 2013 -0500
+++ b/hedgewars/uInputHandler.pas	Tue Dec 17 00:02:52 2013 +0400
@@ -30,6 +30,7 @@
 //procedure MaskModifier(var code: LongInt; modifier: LongWord);
 procedure MaskModifier(Modifier: shortstring; var code: LongInt);
 procedure ProcessMouse(event: TSDL_MouseButtonEvent; ButtonDown: boolean);
+procedure ProcessMouseWheel(x, y: LongInt);
 procedure ProcessKey(event: TSDL_KeyboardEvent); inline;
 procedure ProcessKey(code: LongInt; KeyDown: boolean);
 
@@ -82,8 +83,8 @@
 var code: LongInt;
 begin
     name:= LowerCase(name);
-    code:= cKeyMaxIndex;
-    while (code > 0) and (KeyNames[code] <> name) do dec(code);
+    code:= 0;
+    while (code <= cKeyMaxIndex) and (KeyNames[code] <> name) do inc(code);
 
     MaskModifier(Modifier, code);
     KeyNameToCode:= code;
@@ -168,7 +169,13 @@
 
 if CurrentBinds[code][0] <> #0 then
     begin
-    if (code > 3) and KeyDown and (not ((CurrentBinds[code] = 'put')) or (CurrentBinds[code] = 'ammomenu') or (CurrentBinds[code] = '+cur_u') or (CurrentBinds[code] = '+cur_d') or (CurrentBinds[code] = '+cur_l') or (CurrentBinds[code] = '+cur_r')) and (CurrentTeam <> nil) and (not CurrentTeam^.ExtDriven) then bShowAmmoMenu:= false;
+    if (code < cKeyMaxIndex - 2) // means not mouse buttons
+        and KeyDown
+        and (not ((CurrentBinds[code] = 'put')) or (CurrentBinds[code] = 'ammomenu') or (CurrentBinds[code] = '+cur_u') or (CurrentBinds[code] = '+cur_d') or (CurrentBinds[code] = '+cur_l') or (CurrentBinds[code] = '+cur_r')) 
+        and (CurrentTeam <> nil) 
+        and (not CurrentTeam^.ExtDriven) 
+        then bShowAmmoMenu:= false;
+
     if KeyDown then
         begin
         if CurrentBinds[code] = 'switch' then
@@ -198,6 +205,15 @@
     end
 end;
 
+{$IFDEF SDL2}
+procedure ProcessKey(event: TSDL_KeyboardEvent); inline;
+var code: LongInt;
+begin
+    code:= event.keysym.scancode;
+    //writelntoconsole('[KEY] '+inttostr(code)+ ' -> ''' +KeyNames[code] + ''', type = '+inttostr(event.type_));
+    ProcessKey(code, event.type_ = SDL_KEYDOWN);
+end;
+{$ELSE}
 procedure ProcessKey(event: TSDL_KeyboardEvent); inline;
 var code: LongInt;
 begin
@@ -205,21 +221,32 @@
     //MaskModifier(code, event.keysym.modifier);
     ProcessKey(code, event.type_ = SDL_KEYDOWN);
 end;
+{$ENDIF}
 
 procedure ProcessMouse(event: TSDL_MouseButtonEvent; ButtonDown: boolean);
 begin
-case event.button of
-    SDL_BUTTON_LEFT:
-        ProcessKey(KeyNameToCode('mousel'), ButtonDown);
-    SDL_BUTTON_MIDDLE:
-        ProcessKey(KeyNameToCode('mousem'), ButtonDown);
-    SDL_BUTTON_RIGHT:
-        ProcessKey(KeyNameToCode('mouser'), ButtonDown);
-    SDL_BUTTON_WHEELDOWN:
-        ProcessKey(KeyNameToCode('wheeldown'), ButtonDown);
-    SDL_BUTTON_WHEELUP:
-        ProcessKey(KeyNameToCode('wheelup'), ButtonDown);
-    end;
+    //writelntoconsole('[MOUSE] '+inttostr(event.button));
+    case event.button of
+        SDL_BUTTON_LEFT:
+            ProcessKey(KeyNameToCode('mousel'), ButtonDown);
+        SDL_BUTTON_MIDDLE:
+            ProcessKey(KeyNameToCode('mousem'), ButtonDown);
+        SDL_BUTTON_RIGHT:
+            ProcessKey(KeyNameToCode('mouser'), ButtonDown);
+        SDL_BUTTON_WHEELDOWN:
+            ProcessKey(KeyNameToCode('wheeldown'), ButtonDown);
+        SDL_BUTTON_WHEELUP:
+            ProcessKey(KeyNameToCode('wheelup'), ButtonDown);
+        end;
+end;
+
+procedure ProcessMouseWheel(x, y: LongInt);
+begin
+    //writelntoconsole('[MOUSEWHEEL] '+inttostr(x)+', '+inttostr(y));
+    if y > 0 then
+        ProcessKey(KeyNameToCode('wheelup'), true)
+    else if y < 0 then
+        ProcessKey(KeyNameToCode('wheeldown'), true);
 end;
 
 procedure ResetKbd;
@@ -230,6 +257,112 @@
         ProcessKey(t, False);
 end;
 
+
+procedure InitDefaultBinds;
+var i: Longword;
+begin
+    DefaultBinds[KeyNameToCode('escape')]:= 'quit';
+    DefaultBinds[KeyNameToCode(_S'`')]:= 'history';
+    DefaultBinds[KeyNameToCode('delete')]:= 'rotmask';
+
+    //numpad
+    //DefaultBinds[265]:= '+volup';
+    //DefaultBinds[256]:= '+voldown';
+
+    DefaultBinds[KeyNameToCode(_S'0')]:= '+volup';
+    DefaultBinds[KeyNameToCode(_S'9')]:= '+voldown';
+    DefaultBinds[KeyNameToCode(_S'8')]:= 'mute';
+    DefaultBinds[KeyNameToCode(_S'c')]:= 'capture';
+    DefaultBinds[KeyNameToCode(_S'r')]:= 'record';
+    DefaultBinds[KeyNameToCode(_S'h')]:= 'findhh';
+    DefaultBinds[KeyNameToCode(_S'p')]:= 'pause';
+    DefaultBinds[KeyNameToCode(_S's')]:= '+speedup';
+    DefaultBinds[KeyNameToCode(_S't')]:= 'chat';
+    DefaultBinds[KeyNameToCode(_S'y')]:= 'confirm';
+
+    DefaultBinds[KeyNameToCode('mousem')]:= 'zoomreset';
+    DefaultBinds[KeyNameToCode('wheelup')]:= 'zoomin';
+    DefaultBinds[KeyNameToCode('wheeldown')]:= 'zoomout';
+
+    DefaultBinds[KeyNameToCode('f12')]:= 'fullscr';
+
+
+    DefaultBinds[KeyNameToCode('mousel')]:= '/put';
+    DefaultBinds[KeyNameToCode('mouser')]:= 'ammomenu';
+    DefaultBinds[KeyNameToCode('backspace')]:= 'hjump';
+    DefaultBinds[KeyNameToCode('tab')]:= 'switch';
+    DefaultBinds[KeyNameToCode('return')]:= 'ljump';
+    DefaultBinds[KeyNameToCode('space')]:= '+attack';
+    DefaultBinds[KeyNameToCode('up')]:= '+up';
+    DefaultBinds[KeyNameToCode('down')]:= '+down';
+    DefaultBinds[KeyNameToCode('left')]:= '+left';
+    DefaultBinds[KeyNameToCode('right')]:= '+right';
+    DefaultBinds[KeyNameToCode('left_shift')]:= '+precise';
+
+
+    DefaultBinds[KeyNameToCode('j0a0u')]:= '+left';
+    DefaultBinds[KeyNameToCode('j0a0d')]:= '+right';
+    DefaultBinds[KeyNameToCode('j0a1u')]:= '+up';
+    DefaultBinds[KeyNameToCode('j0a1d')]:= '+down';
+    for i:= 1 to 10 do DefaultBinds[KeyNameToCode('f'+IntToStr(i))]:= 'slot '+IntToStr(i);
+    for i:= 1 to 5  do DefaultBinds[KeyNameToCode(IntToStr(i))]:= 'timer '+IntToStr(i);
+
+    loadBinds('dbind', cPathz[ptData] + '/settings.ini');
+end;
+
+
+{$IFDEF SDL2}
+procedure InitKbdKeyTable;
+var i, j, k, t: LongInt;
+    s: string[15];
+begin
+    KeyNames[cKeyMaxIndex    ]:= 'mousel';
+    KeyNames[cKeyMaxIndex - 1]:= 'mousem';
+    KeyNames[cKeyMaxIndex - 2]:= 'mouser';
+    KeyNames[cKeyMaxIndex - 3]:= 'wheelup';
+    KeyNames[cKeyMaxIndex - 4]:= 'wheeldown';
+
+    for i:= 0 to cKeyMaxIndex - 5 do
+        begin
+        s:= shortstring(SDL_GetScancodeName(i));
+
+        for t:= 1 to Length(s) do
+            if s[t] = ' ' then
+                s[t]:= '_';
+        KeyNames[i]:= LowerCase(s)
+        end;
+
+
+    // get the size of keyboard array
+    SDL_GetKeyState(@k);
+
+    // Controller(s)
+    for j:= 0 to Pred(ControllerNumControllers) do
+        begin
+        for i:= 0 to Pred(ControllerNumAxes[j]) do
+            begin
+            KeyNames[k + 0]:= 'j' + IntToStr(j) + 'a' + IntToStr(i) + 'u';
+            KeyNames[k + 1]:= 'j' + IntToStr(j) + 'a' + IntToStr(i) + 'd';
+            inc(k, 2);
+            end;
+        for i:= 0 to Pred(ControllerNumHats[j]) do
+            begin
+            KeyNames[k + 0]:= 'j' + IntToStr(j) + 'h' + IntToStr(i) + 'u';
+            KeyNames[k + 1]:= 'j' + IntToStr(j) + 'h' + IntToStr(i) + 'r';
+            KeyNames[k + 2]:= 'j' + IntToStr(j) + 'h' + IntToStr(i) + 'd';
+            KeyNames[k + 3]:= 'j' + IntToStr(j) + 'h' + IntToStr(i) + 'l';
+            inc(k, 4);
+            end;
+        for i:= 0 to Pred(ControllerNumButtons[j]) do
+            begin
+            KeyNames[k]:= 'j' + IntToStr(j) + 'b' + IntToStr(i);
+            inc(k, 1);
+            end;
+        end;
+
+        InitDefaultBinds
+end;
+{$ELSE}
 procedure InitKbdKeyTable;
 var i, j, k, t: LongInt;
     s: string[15];
@@ -244,7 +377,7 @@
 for i:= 6 to cKeyMaxIndex do
     begin
     s:= shortstring(sdl_getkeyname(i));
-    //WriteLnToConsole('uInputHandler - ' + IntToStr(i) + ': ' + s + ' ' + IntToStr(cKeyMaxIndex));
+    //AddFileLog('uInputHandler - ' + IntToStr(i) + ': ' + s + ' ' + IntToStr(cKeyMaxIndex));
     if s = 'unknown key' then KeyNames[i]:= ''
     else 
         begin
@@ -283,72 +416,30 @@
         end;
     end;
 
-DefaultBinds[KeyNameToCode('escape')]:= 'quit';
-DefaultBinds[KeyNameToCode(_S'`')]:= 'history';
-DefaultBinds[KeyNameToCode('delete')]:= 'rotmask';
-
-//numpad
-//DefaultBinds[265]:= '+volup';
-//DefaultBinds[256]:= '+voldown';
+    InitDefaultBinds
+end;
+{$ENDIF}
 
-DefaultBinds[KeyNameToCode(_S'0')]:= '+volup';
-DefaultBinds[KeyNameToCode(_S'9')]:= '+voldown';
-DefaultBinds[KeyNameToCode(_S'8')]:= 'mute';
-DefaultBinds[KeyNameToCode(_S'c')]:= 'capture';
-DefaultBinds[KeyNameToCode(_S'r')]:= 'record';
-DefaultBinds[KeyNameToCode(_S'h')]:= 'findhh';
-DefaultBinds[KeyNameToCode(_S'p')]:= 'pause';
-DefaultBinds[KeyNameToCode(_S's')]:= '+speedup';
-DefaultBinds[KeyNameToCode(_S't')]:= 'chat';
-DefaultBinds[KeyNameToCode(_S'y')]:= 'confirm';
-
-DefaultBinds[KeyNameToCode('mousem')]:= 'zoomreset';
-DefaultBinds[KeyNameToCode('wheelup')]:= 'zoomin';
-DefaultBinds[KeyNameToCode('wheeldown')]:= 'zoomout';
-
-DefaultBinds[KeyNameToCode('f12')]:= 'fullscr';
 
 
-DefaultBinds[KeyNameToCode('mousel')]:= '/put';
-DefaultBinds[KeyNameToCode('mouser')]:= 'ammomenu';
-DefaultBinds[KeyNameToCode('backspace')]:= 'hjump';
-DefaultBinds[KeyNameToCode('tab')]:= 'switch';
-DefaultBinds[KeyNameToCode('return')]:= 'ljump';
-DefaultBinds[KeyNameToCode('space')]:= '+attack';
-DefaultBinds[KeyNameToCode('up')]:= '+up';
-DefaultBinds[KeyNameToCode('down')]:= '+down';
-DefaultBinds[KeyNameToCode('left')]:= '+left';
-DefaultBinds[KeyNameToCode('right')]:= '+right';
-DefaultBinds[KeyNameToCode('left_shift')]:= '+precise';
-
-
-DefaultBinds[KeyNameToCode('j0a0u')]:= '+left';
-DefaultBinds[KeyNameToCode('j0a0d')]:= '+right';
-DefaultBinds[KeyNameToCode('j0a1u')]:= '+up';
-DefaultBinds[KeyNameToCode('j0a1d')]:= '+down';
-for i:= 1 to 10 do DefaultBinds[KeyNameToCode('f'+IntToStr(i))]:= 'slot '+char(i+48);
-for i:= 1 to 5  do DefaultBinds[KeyNameToCode(IntToStr(i))]:= 'timer '+IntToStr(i);
-
-loadBinds('dbind', cPathz[ptData] + '/settings.ini');
-end;
-
+{$IFNDEF MOBILE}
 procedure SetBinds(var binds: TBinds);
-{$IFNDEF MOBILE}
 var
     t: LongInt;
-{$ENDIF}
 begin
-{$IFDEF MOBILE}
-    binds:= binds; // avoid hint
-    CurrentBinds:= DefaultBinds;
-{$ELSE}
     for t:= 0 to cKbdMaxIndex do
         if (CurrentBinds[t] <> binds[t]) and tkbd[t] then
             ProcessKey(t, False);
 
     CurrentBinds:= binds;
+end;
+{$ELSE}
+procedure SetBinds(var binds: TBinds);
+begin
+    binds:= binds; // avoid hint
+    CurrentBinds:= DefaultBinds;
+end;
 {$ENDIF}
-end;
 
 procedure SetDefaultBinds;
 begin
--- a/hedgewars/uLand.pas	Sun Dec 15 14:05:42 2013 -0500
+++ b/hedgewars/uLand.pas	Tue Dec 17 00:02:52 2013 +0400
@@ -398,7 +398,7 @@
 TryDo(Surface <> nil, 'Assert (LandSurface <> nil) failed', true);
 
 if SDL_MustLock(Surface) then
-    SDLTry(SDL_LockSurface(Surface) >= 0, true);
+    SDLTry(SDL_LockSurface(Surface) >= 0, 'SDL_LockSurface', true);
 
 p:= Surface^.pixels;
 for y:= 0 to LAND_HEIGHT - 1 do
@@ -578,7 +578,7 @@
     cpX:= (LAND_WIDTH - tmpsurf^.w) div 2;
     cpY:= LAND_HEIGHT - tmpsurf^.h;
     if SDL_MustLock(tmpsurf) then
-        SDLTry(SDL_LockSurface(tmpsurf) >= 0, true);
+        SDLTry(SDL_LockSurface(tmpsurf) >= 0, 'SDL_LockSurface', true);
 
         p:= tmpsurf^.pixels;
         for y:= 0 to Pred(tmpsurf^.h) do
--- a/hedgewars/uLandGraphics.pas	Sun Dec 15 14:05:42 2013 -0500
+++ b/hedgewars/uLandGraphics.pas	Tue Dec 17 00:02:52 2013 +0400
@@ -607,7 +607,7 @@
 col:= Frame div numFramesFirstCol;
 
 if SDL_MustLock(Image) then
-    SDLTry(SDL_LockSurface(Image) >= 0, true);
+    SDLTry(SDL_LockSurface(Image) >= 0, 'SDL_LockSurface', true);
 
 bpp:= Image^.format^.BytesPerPixel;
 TryDo(bpp = 4, 'It should be 32 bpp sprite', true);
--- a/hedgewars/uLandObjects.pas	Sun Dec 15 14:05:42 2013 -0500
+++ b/hedgewars/uLandObjects.pas	Tue Dec 17 00:02:52 2013 +0400
@@ -103,7 +103,7 @@
 WriteToConsole('Generating collision info... ');
 
 if SDL_MustLock(Image) then
-    SDLTry(SDL_LockSurface(Image) >= 0, true);
+    SDLTry(SDL_LockSurface(Image) >= 0, 'SDL_LockSurface', true);
 
 bpp:= Image^.format^.BytesPerPixel;
 TryDo(bpp = 4, 'Land object should be 32bit', true);
@@ -146,7 +146,7 @@
 WriteToConsole('Generating collision info... ');
 
 if SDL_MustLock(Image) then
-    SDLTry(SDL_LockSurface(Image) >= 0, true);
+    SDLTry(SDL_LockSurface(Image) >= 0, 'SDL_LockSurface', true);
 
 bpp:= Image^.format^.BytesPerPixel;
 TryDo(bpp = 4, 'Land object should be 32bit', true);
@@ -194,8 +194,8 @@
 
 procedure InitRects;
 begin
-RectCount:= 0;
-New(Rects)
+    RectCount:= 0;
+    New(Rects)
 end;
 
 procedure FreeRects;
@@ -349,7 +349,7 @@
 CheckCanPlace:= bRes;
 end;
 
-function TryPut(var Obj: TThemeObject): boolean; overload;
+function TryPut(var Obj: TThemeObject): boolean;
 const MaxPointsIndex = 2047;
 var x, y: Longword;
     ar: array[0..MaxPointsIndex] of TPoint;
@@ -370,12 +370,12 @@
                 begin
                 ar[cnt].x:= x;
                 ar[cnt].y:= y;
-                inc(cnt);
-                if cnt > MaxPointsIndex then // buffer is full, do not check the rest land
+                if cnt >= MaxPointsIndex then // buffer is full, do not check the rest land
                     begin
-                    y:= 5000;
-                    x:= 5000;
+                    y:= $FF000000;
+                    x:= $FF000000;
                     end
+                    else inc(cnt);
                 end;
             inc(y, 3);
         until y >= LAND_HEIGHT - Height;
@@ -396,7 +396,7 @@
 TryPut:= bRes;
 end;
 
-function TryPut(var Obj: TSprayObject; Surface: PSDL_Surface): boolean; overload;
+function TryPut2(var Obj: TSprayObject; Surface: PSDL_Surface): boolean;
 const MaxPointsIndex = 8095;
 var x, y: Longword;
     ar: array[0..MaxPointsIndex] of TPoint;
@@ -404,7 +404,7 @@
     r: TSDL_Rect;
     bRes: boolean;
 begin
-TryPut:= false;
+TryPut2:= false;
 cnt:= 0;
 with Obj do
     begin
@@ -423,18 +423,19 @@
                 begin
                 ar[cnt].x:= x;
                 ar[cnt].y:= y;
-                inc(cnt);
-                if cnt > MaxPointsIndex then // buffer is full, do not check the rest land
+                if cnt >= MaxPointsIndex then // buffer is full, do not check the rest land
                     begin
-                    y:= 5000;
-                    x:= 5000;
+                    y:= $FF000000;
+                    x:= $FF000000;
                     end
+                    else inc(cnt);
                 end;
             inc(y, 12);
         until y >= LAND_HEIGHT - Height - 8;
         inc(x, getrandom(12) + 12)
     until x >= LAND_WIDTH - Width;
     bRes:= cnt <> 0;
+AddFileLog('CHECKPOINT 004');
     if bRes then
         begin
         i:= getrandom(cnt);
@@ -448,7 +449,7 @@
         end
     else Maxcnt:= 0
     end;
-TryPut:= bRes;
+TryPut2:= bRes;
 end;
 
 
@@ -814,13 +815,13 @@
         exit;
     WriteLnToConsole('Adding theme objects...');
 
-    for i:=0 to ThemeObjects.Count do
+    for i:=0 to Pred(ThemeObjects.Count) do
         ThemeObjects.objs[i].Maxcnt := max(1, (ThemeObjects.objs[i].Maxcnt * MaxHedgehogs) div 18); // Maxcnt is proportional to map size, but allow objects to span even if we're on a tiny map
 
     repeat
         t := getrandom(ThemeObjects.Count);
         b := false;
-        for i:=0 to ThemeObjects.Count do
+        for i:= 0 to Pred(ThemeObjects.Count) do
             begin
             ii := (i+t) mod ThemeObjects.Count;
 
@@ -838,18 +839,18 @@
         exit;
     WriteLnToConsole('Adding spray objects...');
 
-    for i:=0 to SprayObjects.Count do
+    for i:= 0 to Pred(SprayObjects.Count) do
         SprayObjects.objs[i].Maxcnt := max(1, (SprayObjects.objs[i].Maxcnt * MaxHedgehogs) div 18); // Maxcnt is proportional to map size, but allow objects to span even if we're on a tiny map
 
     repeat
         t := getrandom(SprayObjects.Count);
         b := false;
-        for i:=0 to SprayObjects.Count do
+        for i:= 0 to Pred(SprayObjects.Count) do
             begin
             ii := (i+t) mod SprayObjects.Count;
 
             if SprayObjects.objs[ii].Maxcnt <> 0 then
-                b := b or TryPut(SprayObjects.objs[ii], Surface)
+                b := b or TryPut2(SprayObjects.objs[ii], Surface)
             end;
     until not b;
 end;
@@ -876,7 +877,6 @@
 procedure AddOnLandObjects(Surface: PSDL_Surface);
 begin
 InitRects;
-//AddSprayObjects(Surface, SprayObjects, 12);
 AddSprayObjects(Surface, SprayObjects);
 FreeRects
 end;
--- a/hedgewars/uRenderUtils.pas	Sun Dec 15 14:05:42 2013 -0500
+++ b/hedgewars/uRenderUtils.pas	Tue Dec 17 00:02:52 2013 +0400
@@ -99,7 +99,7 @@
     tmpsurf:= TTF_RenderUTF8_Blended(Fontz[Font].Handle, Str2PChar(s), clr);
     finalRect.x:= X + cFontBorder + 2;
     finalRect.y:= Y + cFontBorder;
-    SDLTry(tmpsurf <> nil, true);
+    SDLTry(tmpsurf <> nil, 'TTF_RenderUTF8_Blended', true);
     SDL_UpperBlit(tmpsurf, @textRect, Surface, @finalRect);
     SDL_FreeSurface(tmpsurf);
     finalRect.x:= X;
@@ -449,7 +449,7 @@
                 rect.x:= edgeHeight + 1 + ((i - w) div 2);
                 // trying to more evenly position the text, vertically
                 rect.y:= edgeHeight + ((j-(numLines*h)) div 2) + line * h;
-                SDLTry(tmpsurf <> nil, true);
+                SDLTry(tmpsurf <> nil, 'TTF_RenderUTF8_Blended', true);
                 SDL_UpperBlit(tmpsurf, nil, finalSurface, @rect);
                 SDL_FreeSurface(tmpsurf);
                 inc(line);
--- a/hedgewars/uSound.pas	Sun Dec 15 14:05:42 2013 -0500
+++ b/hedgewars/uSound.pas	Tue Dec 17 00:02:52 2013 +0400
@@ -311,7 +311,7 @@
     end;
 
     WriteToConsole('Init SDL_mixer... ');
-    SDLTry(Mix_Init(MIX_INIT_OGG) <> 0, true);
+    SDLTry(Mix_Init(MIX_INIT_OGG) <> 0, 'Mix_Init', true);
     WriteLnToConsole(msgOK);
 
     Mix_AllocateChannels(Succ(chanTPU));
@@ -413,7 +413,7 @@
             s:= cPathz[Soundz[snd].Path] + '/' + Soundz[snd].FileName;
             WriteToConsole(msgLoading + s + ' ');
             defVoicepack^.chunks[snd]:= Mix_LoadWAV_RW(rwopsOpenRead(s), 1);
-            SDLTry(defVoicepack^.chunks[snd] <> nil, true);
+            SDLTry(defVoicepack^.chunks[snd] <> nil, 'Mix_LoadWAV_RW', true);
             WriteLnToConsole(msgOK);
             end;
         lastChan[snd]:= Mix_PlayChannelTimed(-1, defVoicepack^.chunks[snd], 0, -1)
@@ -509,7 +509,7 @@
             s:= cPathz[Soundz[snd].Path] + '/' + Soundz[snd].FileName;
             WriteToConsole(msgLoading + s + ' ');
             defVoicepack^.chunks[snd]:= Mix_LoadWAV_RW(rwopsOpenRead(s), 1);
-            SDLTry(defVoicepack^.chunks[snd] <> nil, true);
+            SDLTry(defVoicepack^.chunks[snd] <> nil, 'Mix_LoadWAV_RW', true);
             WriteLnToConsole(msgOK);
             end;
         if fadems > 0 then
@@ -559,10 +559,10 @@
     WriteToConsole(msgLoading + s + ' ');
 
     Mus:= Mix_LoadMUS_RW(rwopsOpenRead(s));
-    SDLTry(Mus <> nil, false);
+    SDLTry(Mus <> nil, 'Mix_LoadMUS_RW', false);
     WriteLnToConsole(msgOK);
 
-    SDLTry(Mix_FadeInMusic(Mus, -1, 3000) <> -1, false)
+    SDLTry(Mix_FadeInMusic(Mus, -1, 3000) <> -1, 'Mix_FadeInMusic', false)
 end;
 
 procedure SetVolume(vol: LongInt);
--- a/hedgewars/uStore.pas	Sun Dec 15 14:05:42 2013 -0500
+++ b/hedgewars/uStore.pas	Tue Dec 17 00:02:52 2013 +0400
@@ -100,7 +100,7 @@
 clr.b:= Color and $FF;
 tmpsurf:= TTF_RenderUTF8_Blended(Fontz[Font].Handle, Str2PChar(s), clr);
 tmpsurf:= doSurfaceConversion(tmpsurf);
-SDLTry(tmpsurf <> nil, true);
+SDLTry(tmpsurf <> nil, 'TTF_RenderUTF8_Blended, doSurfaceConversion', true);
 SDL_UpperBlit(tmpsurf, nil, Surface, @finalRect);
 SDL_FreeSurface(tmpsurf);
 finalRect.x:= X;
@@ -328,7 +328,7 @@
             s:= cPathz[ptFonts] + '/' + Name;
             WriteToConsole(msgLoading + s + ' (' + inttostr(Height) + 'pt)... ');
             Handle:= TTF_OpenFontRW(rwopsOpenRead(s), true, Height);
-            SDLTry(Handle <> nil, true);
+            SDLTry(Handle <> nil, 'TTF_OpenFontRW', true);
             TTF_SetFontStyle(Handle, style);
             WriteLnToConsole(msgOK)
             end;
@@ -717,15 +717,15 @@
 
     AuxBufNum:= AuxBufNum;
 
-{$IFDEF MOBILE}
+{$IFDEF SDL2}
     // TODO: this function creates an opengles1.1 context
     // un-comment below and add proper logic to support opengles2.0
     //SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
     //SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
     if SDLGLcontext = nil then
         SDLGLcontext:= SDL_GL_CreateContext(SDLwindow);
-    SDLTry(SDLGLcontext <> nil, true);
-    SDL_GL_SetSwapInterval(1);
+    SDLTry(SDLGLcontext <> nil, 'SDLGLcontext', true);
+    SDLTry(SDL_GL_SetSwapInterval(1) = 0, 'SDL_GL_SetSwapInterval', true);
 {$ENDIF}
 
     // get the max (h and v) size for textures that the gpu can support
@@ -1092,7 +1092,7 @@
                                  SDL_WINDOWPOS_CENTERED_MASK, SDL_WINDOWPOS_CENTERED_MASK,
                                  cScreenWidth, cScreenHeight,
                                  SDL_WINDOW_HIDDEN or SDL_WINDOW_OPENGL);
-    SDLTry(SDLwindow <> nil, true);
+    SDLTry(SDLwindow <> nil, 'SDL_CreateWindow', true);
     SetupOpenGL();
 end;
 {$ELSE}
@@ -1152,10 +1152,11 @@
         SDL_WM_SetCaption(_P'Hedgewars', nil);
     {$ENDIF}
         WriteToConsole('Init SDL_image... ');
-        SDLTry(IMG_Init(IMG_INIT_PNG) <> 0, true);
+        SDLTry(IMG_Init(IMG_INIT_PNG) <> 0, 'IMG_Init', true);
         WriteLnToConsole(msgOK);
         // load engine icon
     {$IFNDEF DARWIN}
+    {$IFNDEF SDL2}
         ico:= LoadDataImage(ptGraphics, 'hwengine', ifIgnoreCaps);
         if ico <> nil then
             begin
@@ -1163,6 +1164,7 @@
             SDL_FreeSurface(ico)
             end;
     {$ENDIF}
+    {$ENDIF}
         end
     else
         begin
@@ -1224,7 +1226,7 @@
 
     if SDLwindow = nil then
         SDLwindow:= SDL_CreateWindow('Hedgewars', x, y, cScreenWidth, cScreenHeight, flags);
-    SDLTry(SDLwindow <> nil, true);
+    SDLTry(SDLwindow <> nil, 'SDL_CreateWindow', true);
 {$ELSE}
     flags:= SDL_OPENGL or SDL_RESIZABLE;
     if cFullScreen then
--- a/hedgewars/uTextures.pas	Sun Dec 15 14:05:42 2013 -0500
+++ b/hedgewars/uTextures.pas	Tue Dec 17 00:02:52 2013 +0400
@@ -154,7 +154,7 @@
 glBindTexture(GL_TEXTURE_2D, Surface2Tex^.id);
 
 if SDL_MustLock(surf) then
-    SDLTry(SDL_LockSurface(surf) >= 0, true);
+    SDLTry(SDL_LockSurface(surf) >= 0, 'Lock surface', true);
 
 fromP4:= Surf^.pixels;
 
--- a/misc/libphyslayer/CMakeLists.txt	Sun Dec 15 14:05:42 2013 -0500
+++ b/misc/libphyslayer/CMakeLists.txt	Tue Dec 17 00:02:52 2013 +0400
@@ -1,7 +1,14 @@
 
-find_package(SDL1or2)
+if(${NOSDL2})
+    find_package(SDL REQUIRED)
+    include_directories(${SDL_INCLUDE_DIR})
+else(${NOSDL2})
+    find_package(SDL2 REQUIRED)
+    include_directories(${SDL2_INCLUDE_DIR})
+    set(SDL_LIBRARY ${SDL2_LIBRARY})
+endif(${NOSDL2})
+
 include_directories(${PHYSFS_INCLUDE_DIR})
-include_directories(${SDL_INCLUDE_DIR})
 include_directories(${LUA_INCLUDE_DIR})
 
 ## extra functions needed by Hedgewars
--- a/project_files/hedgewars.pro	Sun Dec 15 14:05:42 2013 -0500
+++ b/project_files/hedgewars.pro	Tue Dec 17 00:02:52 2013 +0400
@@ -274,8 +274,8 @@
 }
 
 !macx {
-    LIBS += -lSDL -lSDL_mixer -lSDL_net
+    LIBS += -lSDL2 -lSDL2_mixer -lSDL2_net
     !win32 {
-        INCLUDEPATH += /usr/local/include/SDL /usr/include/SDL
+        INCLUDEPATH += /usr/local/include/SDL2 /usr/include/SDL2
     }
 }
--- a/tools/CMakeLists.txt	Sun Dec 15 14:05:42 2013 -0500
+++ b/tools/CMakeLists.txt	Tue Dec 17 00:02:52 2013 +0400
@@ -8,12 +8,21 @@
 
 if (APPLE)
     find_package(Qt4 REQUIRED QUIET)
-    find_package(SDL REQUIRED)
-    find_package(SDL_image REQUIRED)
-    find_package(SDL_net REQUIRED)
-    find_package(SDL_ttf REQUIRED)
-    find_package(SDL_mixer REQUIRED)
-    find_package(OggVorbis REQUIRED)
+    if(${NOSDL2})
+        find_package(SDL REQUIRED)
+        find_package(SDL_image REQUIRED)
+        find_package(SDL_net REQUIRED)
+        find_package(SDL_ttf REQUIRED)
+        find_package(SDL_mixer REQUIRED)
+        find_package(OggVorbis REQUIRED)
+    else(${NOSDL2})
+        find_package(SDL2 REQUIRED)
+        find_package(SDL2_image REQUIRED)
+        find_package(SDL2_net REQUIRED)
+        find_package(SDL2_ttf REQUIRED)
+        find_package(SDL2_mixer REQUIRED)
+    endif(${NOSDL2})
+
     if(NOT NOAUTOUPDATE)
         find_package(Sparkle) #needed for SPARKLE_FOUND variable
         #needed because the 'if' clause in the script prints silly policy warnings