test/testhittesting.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 28 Nov 2014 04:51:33 -0800
changeset 9246 a761913e5e91
parent 8947 14d3e9f93c16
child 11272 d60ede5a0c2e
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 #include <stdio.h>
     2 #include "SDL.h"
     3 
     4 /* !!! FIXME: rewrite this to be wired in to test framework. */
     5 
     6 #define RESIZE_BORDER 20
     7 
     8 const SDL_Rect drag_areas[] = {
     9     { 20, 20, 100, 100 },
    10     { 200, 70, 100, 100 },
    11     { 400, 90, 100, 100 }
    12 };
    13 
    14 static const SDL_Rect *areas = drag_areas;
    15 static int numareas = SDL_arraysize(drag_areas);
    16 
    17 static SDL_HitTestResult
    18 hitTest(SDL_Window *window, const SDL_Point *pt, void *data)
    19 {
    20     int i;
    21     int w, h;
    22 
    23     for (i = 0; i < numareas; i++) {
    24         if (SDL_PointInRect(pt, &areas[i])) {
    25             SDL_Log("HIT-TEST: DRAGGABLE\n");
    26             return SDL_HITTEST_DRAGGABLE;
    27         }
    28     }
    29 
    30     SDL_GetWindowSize(window, &w, &h);
    31 
    32     #define REPORT_RESIZE_HIT(name) { \
    33         SDL_Log("HIT-TEST: RESIZE_" #name "\n"); \
    34         return SDL_HITTEST_RESIZE_##name; \
    35     }
    36 
    37     if (pt->x < RESIZE_BORDER && pt->y < RESIZE_BORDER) {
    38         REPORT_RESIZE_HIT(TOPLEFT);
    39     } else if (pt->x > RESIZE_BORDER && pt->x < w - RESIZE_BORDER && pt->y < RESIZE_BORDER) {
    40         REPORT_RESIZE_HIT(TOP);
    41     } else if (pt->x > w - RESIZE_BORDER && pt->y < RESIZE_BORDER) {
    42         REPORT_RESIZE_HIT(TOPRIGHT);
    43     } else if (pt->x > w - RESIZE_BORDER && pt->y > RESIZE_BORDER && pt->y < h - RESIZE_BORDER) {
    44         REPORT_RESIZE_HIT(RIGHT);
    45     } else if (pt->x > w - RESIZE_BORDER && pt->y > h - RESIZE_BORDER) {
    46         REPORT_RESIZE_HIT(BOTTOMRIGHT);
    47     } else if (pt->x < w - RESIZE_BORDER && pt->x > RESIZE_BORDER && pt->y > h - RESIZE_BORDER) {
    48         REPORT_RESIZE_HIT(BOTTOM);
    49     } else if (pt->x < RESIZE_BORDER && pt->y > h - RESIZE_BORDER) {
    50         REPORT_RESIZE_HIT(BOTTOMLEFT);
    51     } else if (pt->x < RESIZE_BORDER && pt->y < h - RESIZE_BORDER && pt->y > RESIZE_BORDER) {
    52         REPORT_RESIZE_HIT(LEFT);
    53     }
    54 
    55     SDL_Log("HIT-TEST: NORMAL\n");
    56     return SDL_HITTEST_NORMAL;
    57 }
    58 
    59 
    60 int main(int argc, char **argv)
    61 {
    62     int done = 0;
    63     SDL_Window *window;
    64     SDL_Renderer *renderer;
    65 
    66     /* !!! FIXME: check for errors. */
    67     SDL_Init(SDL_INIT_VIDEO);
    68     window = SDL_CreateWindow("Drag the red boxes", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_BORDERLESS | SDL_WINDOW_RESIZABLE);
    69     renderer = SDL_CreateRenderer(window, -1, 0);
    70 
    71     if (SDL_SetWindowHitTest(window, hitTest, NULL) == -1) {
    72         SDL_Log("Enabling hit-testing failed!\n");
    73         SDL_Quit();
    74         return 1;
    75     }
    76 
    77     while (!done)
    78     {
    79         SDL_Event e;
    80         int nothing_to_do = 1;
    81 
    82         SDL_SetRenderDrawColor(renderer, 0, 0, 127, 255);
    83         SDL_RenderClear(renderer);
    84         SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
    85         SDL_RenderFillRects(renderer, areas, SDL_arraysize(drag_areas));
    86         SDL_RenderPresent(renderer);
    87 
    88         while (SDL_PollEvent(&e)) {
    89             nothing_to_do = 0;
    90 
    91             switch (e.type)
    92             {
    93                 case SDL_MOUSEBUTTONDOWN:
    94                     SDL_Log("button down!\n");
    95                     break;
    96 
    97                 case SDL_MOUSEBUTTONUP:
    98                     SDL_Log("button up!\n");
    99                     break;
   100 
   101                 case SDL_WINDOWEVENT:
   102                     if (e.window.event == SDL_WINDOWEVENT_MOVED) {
   103                         SDL_Log("Window event moved to (%d, %d)!\n", (int) e.window.data1, (int) e.window.data2);
   104                     }
   105                     break;
   106 
   107                 case SDL_KEYDOWN:
   108                     if (e.key.keysym.sym == SDLK_ESCAPE) {
   109                         done = 1;
   110                     } else if (e.key.keysym.sym == SDLK_x) {
   111                         if (!areas) {
   112                             areas = drag_areas;
   113                             numareas = SDL_arraysize(drag_areas);
   114                         } else {
   115                             areas = NULL;
   116                             numareas = 0;
   117                         }
   118                     }
   119                     break;
   120 
   121                 case SDL_QUIT:
   122                     done = 1;
   123                     break;
   124             }
   125         }
   126 
   127         if (nothing_to_do) {
   128             SDL_Delay(50);
   129         }
   130     }
   131 
   132     SDL_Quit();
   133     return 0;
   134 }