test/testrelative.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 28 Nov 2014 04:51:33 -0800
changeset 9246 a761913e5e91
parent 8724 9fa8abe556d7
child 9278 8900afb78a19
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).
slouken@6311
     1
/*
slouken@8149
     2
  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
slouken@6311
     3
slouken@6311
     4
  This software is provided 'as-is', without any express or implied
slouken@6311
     5
  warranty.  In no event will the authors be held liable for any damages
slouken@6311
     6
  arising from the use of this software.
slouken@6311
     7
slouken@6311
     8
  Permission is granted to anyone to use this software for any purpose,
slouken@6311
     9
  including commercial applications, and to alter it and redistribute it
slouken@6311
    10
  freely.
slouken@6311
    11
*/
slouken@6311
    12
slouken@6311
    13
/* Simple program:  Test relative mouse motion */
slouken@6311
    14
slouken@6311
    15
#include <stdlib.h>
slouken@6311
    16
#include <stdio.h>
slouken@6311
    17
#include <time.h>
slouken@6311
    18
slouken@6785
    19
#include "SDL_test_common.h"
slouken@6311
    20
slouken@6311
    21
slouken@6785
    22
static SDLTest_CommonState *state;
slouken@6311
    23
slouken@6311
    24
static void
philipp@8724
    25
DrawRects(SDL_Renderer * renderer, SDL_Rect * rect)
slouken@6311
    26
{
slouken@6311
    27
    SDL_SetRenderDrawColor(renderer, 255, 127, 0, 255);
philipp@8724
    28
    SDL_RenderFillRect(renderer, rect);
slouken@6311
    29
}
slouken@6311
    30
slouken@6311
    31
int
slouken@6311
    32
main(int argc, char *argv[])
slouken@6311
    33
{
slouken@6311
    34
    int i, done;
philipp@8724
    35
    SDL_Rect rect;
slouken@6311
    36
    SDL_Event event;
slouken@6311
    37
philipp@8724
    38
    /* Enable standard application logging */
aschiffler@7639
    39
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
slouken@7191
    40
slouken@6311
    41
    /* Initialize test framework */
slouken@6785
    42
    state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
slouken@6311
    43
    if (!state) {
slouken@6311
    44
        return 1;
slouken@6311
    45
    }
philipp@8724
    46
    for (i = 1; i < argc; ++i) {
slouken@6785
    47
        SDLTest_CommonArg(state, i);
dimitris@6318
    48
    }
slouken@6785
    49
    if (!SDLTest_CommonInit(state)) {
slouken@6311
    50
        return 2;
slouken@6311
    51
    }
slouken@6311
    52
slouken@6311
    53
    /* Create the windows and initialize the renderers */
slouken@6311
    54
    for (i = 0; i < state->num_windows; ++i) {
slouken@6311
    55
        SDL_Renderer *renderer = state->renderers[i];
slouken@6311
    56
        SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
slouken@6311
    57
        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
slouken@6311
    58
        SDL_RenderClear(renderer);
slouken@6311
    59
    }
slouken@6311
    60
slouken@6311
    61
    srand((unsigned int)time(NULL));
dimitris@6316
    62
    if(SDL_SetRelativeMouseMode(SDL_TRUE) < 0) {
dimitris@6316
    63
        return 3;
dimitris@6316
    64
    };
slouken@6311
    65
slouken@6311
    66
    rect.x = DEFAULT_WINDOW_WIDTH / 2;
slouken@6311
    67
    rect.y = DEFAULT_WINDOW_HEIGHT / 2;
slouken@6311
    68
    rect.w = 10;
slouken@6311
    69
    rect.h = 10;
slouken@6311
    70
    /* Main render loop */
slouken@6311
    71
    done = 0;
slouken@6311
    72
    while (!done) {
slouken@6311
    73
        /* Check for events */
slouken@6311
    74
        while (SDL_PollEvent(&event)) {
slouken@6785
    75
            SDLTest_CommonEvent(state, &event, &done);
slouken@6311
    76
            switch(event.type) {
slouken@6311
    77
                case SDL_MOUSEMOTION:
slouken@6311
    78
                {
slouken@6311
    79
                    rect.x += event.motion.xrel;
slouken@6311
    80
                    rect.y += event.motion.yrel;
slouken@6311
    81
                }
slouken@6311
    82
                break;
slouken@6311
    83
            }
slouken@6311
    84
        }
slouken@6311
    85
        for (i = 0; i < state->num_windows; ++i) {
slouken@6311
    86
            SDL_Renderer *renderer = state->renderers[i];
slouken@7787
    87
            if (state->windows[i] == NULL)
slouken@7787
    88
                continue;
slouken@6311
    89
            SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
slouken@6311
    90
            SDL_RenderClear(renderer);
slouken@6311
    91
philipp@8724
    92
            DrawRects(renderer, &rect);
slouken@6311
    93
slouken@6311
    94
            SDL_RenderPresent(renderer);
slouken@6311
    95
        }
slouken@6311
    96
    }
slouken@6311
    97
slouken@6785
    98
    SDLTest_CommonQuit(state);
slouken@6311
    99
    return 0;
slouken@6311
   100
}
slouken@6311
   101
slouken@6311
   102
/* vi: set ts=4 sw=4 expandtab: */