test/testviewport.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 28 Nov 2014 04:51:33 -0800
changeset 9246 a761913e5e91
parent 8149 681eb46b8ac4
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).
     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 /* Simple program:  Check viewports */
    13 
    14 #include <stdlib.h>
    15 #include <stdio.h>
    16 #include <time.h>
    17 
    18 #include "SDL_test.h"
    19 #include "SDL_test_common.h"
    20 
    21 
    22 static SDLTest_CommonState *state;
    23 
    24 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    25 static void
    26 quit(int rc)
    27 {
    28     SDLTest_CommonQuit(state);
    29     exit(rc);
    30 }
    31 
    32 void
    33 DrawOnViewport(SDL_Renderer * renderer, SDL_Rect viewport)
    34 {    
    35     SDL_Rect rect;
    36 
    37     /* Set the viewport */
    38     SDL_RenderSetViewport(renderer, &viewport);
    39     
    40     /* Draw a gray background */
    41     SDL_SetRenderDrawColor(renderer, 0x80, 0x80, 0x80, 0xFF);
    42     SDL_RenderClear(renderer);
    43 
    44     /* Test inside points */
    45     SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xF, 0xFF);
    46     SDL_RenderDrawPoint(renderer, viewport.h/2 + 10, viewport.w/2);
    47     SDL_RenderDrawPoint(renderer, viewport.h/2 - 10, viewport.w/2);
    48     SDL_RenderDrawPoint(renderer, viewport.h/2     , viewport.w/2 - 10);
    49     SDL_RenderDrawPoint(renderer, viewport.h/2     , viewport.w/2 + 10);
    50 
    51     /* Test horizontal and vertical lines */
    52     SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
    53     SDL_RenderDrawLine(renderer, 1, 0, viewport.w-2, 0);
    54     SDL_RenderDrawLine(renderer, 1, viewport.h-1, viewport.w-2, viewport.h-1);
    55     SDL_RenderDrawLine(renderer, 0, 1, 0, viewport.h-2);
    56     SDL_RenderDrawLine(renderer, viewport.w-1, 1, viewport.w-1, viewport.h-2);
    57 
    58     /* Test diagonal lines */
    59     SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0xFF, 0xFF);
    60     SDL_RenderDrawLine(renderer, 0, 0,
    61                        viewport.w-1, viewport.h-1);
    62     SDL_RenderDrawLine(renderer, viewport.w-1, 0,
    63                        0, viewport.h-1);                      
    64 
    65     /* Test outside points */
    66     SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xF, 0xFF);
    67     SDL_RenderDrawPoint(renderer, viewport.h/2 + viewport.h, viewport.w/2);
    68     SDL_RenderDrawPoint(renderer, viewport.h/2 - viewport.h, viewport.w/2);
    69     SDL_RenderDrawPoint(renderer, viewport.h/2     , viewport.w/2 - viewport.w);
    70     SDL_RenderDrawPoint(renderer, viewport.h/2     , viewport.w/2 + viewport.w);
    71 
    72     /* Add a box at the top */
    73     rect.w = 8;
    74     rect.h = 8;
    75     rect.x = (viewport.w - rect.w) / 2;
    76     rect.y = 0;
    77     SDL_RenderFillRect(renderer, &rect);
    78 }
    79 
    80 int
    81 main(int argc, char *argv[])
    82 {
    83     int i, j, done;
    84     SDL_Event event;
    85     Uint32 then, now, frames;
    86     SDL_Rect viewport;
    87     SDL_bool use_target = SDL_FALSE;
    88 
    89     /* Initialize test framework */
    90     state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
    91     if (!state) {
    92         return 1;
    93     }
    94 
    95     for (i = 1; i < argc;) {
    96         int consumed;
    97 
    98         consumed = SDLTest_CommonArg(state, i);
    99         if (consumed == 0) {
   100             consumed = -1;
   101             if (SDL_strcasecmp(argv[i], "--target") == 0) {
   102                 use_target = SDL_TRUE;
   103                 consumed = 1;
   104             }
   105         }
   106         if (consumed < 0) {
   107             SDL_Log("Usage: %s %s [--target]\n",
   108                     argv[0], SDLTest_CommonUsage(state));
   109             quit(1);
   110         }
   111         i += consumed;
   112     }
   113     if (!SDLTest_CommonInit(state)) {
   114         quit(2);
   115     }
   116 
   117     if (use_target) {
   118         int w, h;
   119 
   120         for (i = 0; i < state->num_windows; ++i) {
   121             SDL_GetWindowSize(state->windows[i], &w, &h);
   122             state->targets[i] = SDL_CreateTexture(state->renderers[i], SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h);
   123             SDL_SetRenderTarget(state->renderers[i], state->targets[i]);
   124         }
   125     }
   126 
   127     for (i = 0; i < state->num_windows; ++i) {
   128         SDL_Renderer *renderer = state->renderers[i];
   129         SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
   130         SDL_RenderClear(renderer);
   131     }
   132 
   133     /* Main render loop */
   134     frames = 0;
   135     then = SDL_GetTicks();
   136     done = 0;
   137     j = 0;
   138     while (!done) {
   139         /* Check for events */
   140         ++frames;
   141         while (SDL_PollEvent(&event)) {
   142             SDLTest_CommonEvent(state, &event, &done);
   143         }
   144         
   145         /* Move a viewport box in steps around the screen */                
   146         viewport.x = j * 100;
   147         viewport.y = viewport.x;
   148         viewport.w = 100 + j * 50;
   149         viewport.h = 100 + j * 50;
   150         j = (j + 1) % 4;            
   151         SDL_Log("Current Viewport x=%i y=%i w=%i h=%i", viewport.x, viewport.y, viewport.w, viewport.h);
   152         
   153         for (i = 0; i < state->num_windows; ++i) {
   154             if (state->windows[i] == NULL)
   155                 continue;
   156                 
   157             /* Draw using viewport */        
   158             DrawOnViewport(state->renderers[i], viewport);
   159 
   160             /* Update the screen! */
   161             if (use_target) {
   162                 SDL_SetRenderTarget(state->renderers[i], NULL);
   163                 SDL_RenderCopy(state->renderers[i], state->targets[i], NULL, NULL);
   164                 SDL_RenderPresent(state->renderers[i]);
   165                 SDL_SetRenderTarget(state->renderers[i], state->targets[i]);
   166             } else {
   167                 SDL_RenderPresent(state->renderers[i]);
   168             }
   169         }
   170         
   171         SDL_Delay(1000);
   172     }
   173 
   174     /* Print out some timing information */
   175     now = SDL_GetTicks();
   176     if (now > then) {
   177         double fps = ((double) frames * 1000) / (now - then);
   178         SDL_Log("%2.2f frames per second\n", fps);
   179     }
   180     quit(0);
   181     return 0;
   182 }
   183 
   184 /* vi: set ts=4 sw=4 expandtab: */