test/testnativew32.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 28 Nov 2014 04:51:33 -0800
changeset 9246 a761913e5e91
parent 8149 681eb46b8ac4
child 9619 b94b6d0bff0f
permissions -rw-r--r--
Fixed bug 2786 - "UCS-2-INTERNAL" iconv encoding is not supported everywhere, use UTF-16LE instead

Jonas Kulla

src/main/windows/SDL_windows_main.c:137:
cmdline = SDL_iconv_string("UTF-8", "UCS-2-INTERNAL", (char *)(text), (SDL_wcslen(text)+1)*sizeof(WCHAR));

I'm trying to compile an SDL2 application for windows using the mingw-w64 32bit toolchain provided by my distro (Fedora 19). However, even the simplest test program that does nothing at all fails to startup with a "Fatal error - out of memory" message because the mingw iconv library provided by my distro does not support the "UCS-2-INTERNAL" encoding and the conversion returns null.

From my little bit of research, it turns out that even though this encoding is supported by the external GNU libiconv library, some glibc versions (?) don't support it with their internal iconv routines, and will instead provide the native endian encoding when "UCS-2" is specified.

Nonetheless, I wonder why the native endianness is considered in the first place when Windows doesn't even run on any big endian archs (to my knowledge). And true enough, 'WIN_StringToUTF8' from core/windows/SDL_windows.h is used everywhere else in the windows backend, which is just a macro to iconv with "UTF-16LE" as source. Therefore it would IMO make sense to use this macro here as well, which would solve my problem (patch attached).
     1 /*
     2   Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
     3 
     4   This software is provided 'as-is', without any express or implied
     5   warranty.  In no event will the authors be held liable for any damages
     6   arising from the use of this software.
     7 
     8   Permission is granted to anyone to use this software for any purpose,
     9   including commercial applications, and to alter it and redistribute it
    10   freely.
    11 */
    12 
    13 #include "testnative.h"
    14 
    15 #ifdef TEST_NATIVE_WINDOWS
    16 
    17 static void *CreateWindowNative(int w, int h);
    18 static void DestroyWindowNative(void *window);
    19 
    20 NativeWindowFactory WindowsWindowFactory = {
    21     "windows",
    22     CreateWindowNative,
    23     DestroyWindowNative
    24 };
    25 
    26 LRESULT CALLBACK
    27 WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    28 {
    29     switch (msg) {
    30     case WM_CLOSE:
    31         DestroyWindow(hwnd);
    32         break;
    33     case WM_DESTROY:
    34         PostQuitMessage(0);
    35         break;
    36     default:
    37         return DefWindowProc(hwnd, msg, wParam, lParam);
    38     }
    39     return 0;
    40 }
    41 
    42 static void *
    43 CreateWindowNative(int w, int h)
    44 {
    45     HWND hwnd;
    46     WNDCLASS wc;
    47 
    48     wc.style = 0;
    49     wc.lpfnWndProc = WndProc;
    50     wc.cbClsExtra = 0;
    51     wc.cbWndExtra = 0;
    52     wc.hInstance = GetModuleHandle(NULL);
    53     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    54     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    55     wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
    56     wc.lpszMenuName = NULL;
    57     wc.lpszClassName = "SDL Test";
    58 
    59     if (!RegisterClass(&wc)) {
    60         MessageBox(NULL, "Window Registration Failed!", "Error!",
    61                    MB_ICONEXCLAMATION | MB_OK);
    62         return 0;
    63     }
    64 
    65     hwnd =
    66         CreateWindow("SDL Test", "", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
    67                      CW_USEDEFAULT, w, h, NULL, NULL, GetModuleHandle(NULL),
    68                      NULL);
    69     if (hwnd == NULL) {
    70         MessageBox(NULL, "Window Creation Failed!", "Error!",
    71                    MB_ICONEXCLAMATION | MB_OK);
    72         return 0;
    73     }
    74 
    75     ShowWindow(hwnd, SW_SHOW);
    76 
    77     return hwnd;
    78 }
    79 
    80 static void
    81 DestroyWindowNative(void *window)
    82 {
    83     DestroyWindow((HWND) window);
    84 }
    85 
    86 #endif