src/SDL_assert.c
changeset 5086 c2539ff054c8
parent 5064 eae20af0b983
child 5090 327f181542f1
equal deleted inserted replaced
5085:e7680e2c9f3c 5086:c2539ff054c8
    24 #include "SDL_atomic.h"
    24 #include "SDL_atomic.h"
    25 #include "SDL_assert.h"
    25 #include "SDL_assert.h"
    26 #include "SDL_assert_c.h"
    26 #include "SDL_assert_c.h"
    27 #include "video/SDL_sysvideo.h"
    27 #include "video/SDL_sysvideo.h"
    28 
    28 
    29 #ifdef __WINDOWS__
    29 #ifdef __WIN32__
    30 #define WIN32_LEAN_AND_MEAN
    30 #define WIN32_LEAN_AND_MEAN
    31 #include <windows.h>
    31 #include <windows.h>
       
    32 
       
    33 #ifndef WS_OVERLAPPEDWINDOW
       
    34 #define WS_OVERLAPPEDWINDOW 0
       
    35 #endif
       
    36 
       
    37 #ifdef UNICODE
       
    38 #define WIN_UTF8ToString(S) (WCHAR *)SDL_iconv_string("UCS-2", "UTF-8", (char *)S, SDL_strlen(S)+1)
       
    39 #else
       
    40 #define WIN_UTF8ToString(S) SDL_iconv_string("ASCII", "UTF-8", (char *)S, SDL_strlen(S)+1)
       
    41 #endif
    32 #else  /* fprintf, _exit(), etc. */
    42 #else  /* fprintf, _exit(), etc. */
    33 #include <stdio.h>
    43 #include <stdio.h>
    34 #include <stdlib.h>
    44 #include <stdlib.h>
    35 #include <unistd.h>
    45 #include <unistd.h>
    36 #endif
    46 #endif
    55 #endif
    65 #endif
    56 
    66 
    57 static void
    67 static void
    58 debug_print(const char *fmt, ...)
    68 debug_print(const char *fmt, ...)
    59 {
    69 {
    60 #ifdef __WINDOWS__
    70 #ifdef __WIN32__
    61     /* Format into a buffer for OutputDebugStringA(). */
    71     /* Format into a buffer for OutputDebugStringA(). */
    62     char buf[1024];
    72     char buf[1024];
    63     char *startptr;
    73     char *startptr;
    64     char *ptr;
    74     char *ptr;
       
    75     LPTSTR tstr;
    65     int len;
    76     int len;
    66     va_list ap;
    77     va_list ap;
    67     va_start(ap, fmt);
    78     va_start(ap, fmt);
    68     len = (int) SDL_vsnprintf(buf, sizeof (buf), fmt, ap);
    79     len = (int) SDL_vsnprintf(buf, sizeof (buf), fmt, ap);
    69     va_end(ap);
    80     va_end(ap);
    76     /* Write it, sorting out the Unix newlines... */
    87     /* Write it, sorting out the Unix newlines... */
    77     startptr = buf;
    88     startptr = buf;
    78     for (ptr = startptr; *ptr; ptr++) {
    89     for (ptr = startptr; *ptr; ptr++) {
    79         if (*ptr == '\n') {
    90         if (*ptr == '\n') {
    80             *ptr = '\0';
    91             *ptr = '\0';
    81             OutputDebugStringA(startptr);
    92             tstr = WIN_UTF8ToString(startptr);
    82             OutputDebugStringA("\r\n");
    93             OutputDebugString(tstr);
       
    94             SDL_free(tstr);
       
    95             OutputDebugString(TEXT("\r\n"));
    83             startptr = ptr+1;
    96             startptr = ptr+1;
    84         }
    97         }
    85     }
    98     }
    86 
    99 
    87     /* catch that last piece if it didn't have a newline... */
   100     /* catch that last piece if it didn't have a newline... */
    88     if (startptr != ptr) {
   101     if (startptr != ptr) {
    89         OutputDebugStringA(startptr);
   102         tstr = WIN_UTF8ToString(startptr);
       
   103         OutputDebugString(tstr);
       
   104         SDL_free(tstr);
    90     }
   105     }
    91 #else
   106 #else
    92     /* Unix has it easy. Just dump it to stderr. */
   107     /* Unix has it easy. Just dump it to stderr. */
    93     va_list ap;
   108     va_list ap;
    94     va_start(ap, fmt);
   109     va_start(ap, fmt);
    97     fflush(stderr);
   112     fflush(stderr);
    98 #endif
   113 #endif
    99 }
   114 }
   100 
   115 
   101 
   116 
   102 #ifdef __WINDOWS__
   117 #ifdef __WIN32__
   103 static SDL_assert_state SDL_Windows_AssertChoice = SDL_ASSERTION_ABORT;
   118 static SDL_assert_state SDL_Windows_AssertChoice = SDL_ASSERTION_ABORT;
   104 static const SDL_assert_data *SDL_Windows_AssertData = NULL;
   119 static const SDL_assert_data *SDL_Windows_AssertData = NULL;
   105 
   120 
   106 static LRESULT CALLBACK
   121 static LRESULT CALLBACK
   107 SDL_Assertion_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
   122 SDL_Assertion_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
   111         case WM_CREATE:
   126         case WM_CREATE:
   112         {
   127         {
   113             /* !!! FIXME: all this code stinks. */
   128             /* !!! FIXME: all this code stinks. */
   114             const SDL_assert_data *data = SDL_Windows_AssertData;
   129             const SDL_assert_data *data = SDL_Windows_AssertData;
   115             char buf[1024];
   130             char buf[1024];
       
   131             LPTSTR tstr;
   116             const int w = 100;
   132             const int w = 100;
   117             const int h = 25;
   133             const int h = 25;
   118             const int gap = 10;
   134             const int gap = 10;
   119             int x = gap;
   135             int x = gap;
   120             int y = 50;
   136             int y = 50;
   121             int len;
   137             int len;
   122             int i;
   138             int i;
   123             static const struct { 
   139             static const struct { 
   124                 const char *name;
   140                 LPCTSTR name;
   125                 SDL_assert_state state;
   141                 SDL_assert_state state;
   126             } buttons[] = {
   142             } buttons[] = {
   127                 {"Abort", SDL_ASSERTION_ABORT },
   143                 {TEXT("Abort"), SDL_ASSERTION_ABORT },
   128                 {"Break", SDL_ASSERTION_BREAK },
   144                 {TEXT("Break"), SDL_ASSERTION_BREAK },
   129                 {"Retry", SDL_ASSERTION_RETRY },
   145                 {TEXT("Retry"), SDL_ASSERTION_RETRY },
   130                 {"Ignore", SDL_ASSERTION_IGNORE },
   146                 {TEXT("Ignore"), SDL_ASSERTION_IGNORE },
   131                 {"Always Ignore", SDL_ASSERTION_ALWAYS_IGNORE },
   147                 {TEXT("Always Ignore"), SDL_ASSERTION_ALWAYS_IGNORE },
   132             };
   148             };
   133 
   149 
   134             len = (int) SDL_snprintf(buf, sizeof (buf), 
   150             len = (int) SDL_snprintf(buf, sizeof (buf), 
   135                          "Assertion failure at %s (%s:%d), triggered %u time%s:\r\n  '%s'",
   151                          "Assertion failure at %s (%s:%d), triggered %u time%s:\r\n  '%s'",
   136                          data->function, data->filename, data->linenum,
   152                          data->function, data->filename, data->linenum,
   138                          data->condition);
   154                          data->condition);
   139             if ((len < 0) || (len >= sizeof (buf))) {
   155             if ((len < 0) || (len >= sizeof (buf))) {
   140                 buf[sizeof (buf) - 1] = '\0';
   156                 buf[sizeof (buf) - 1] = '\0';
   141             }
   157             }
   142 
   158 
   143             CreateWindowA("STATIC", buf,
   159             tstr = WIN_UTF8ToString(buf);
       
   160             CreateWindow(TEXT("STATIC"), tstr,
   144                          WS_VISIBLE | WS_CHILD | SS_LEFT,
   161                          WS_VISIBLE | WS_CHILD | SS_LEFT,
   145                          x, y, 550, 100,
   162                          x, y, 550, 100,
   146                          hwnd, (HMENU) 1, NULL, NULL);
   163                          hwnd, (HMENU) 1, NULL, NULL);
       
   164             SDL_free(tstr);
   147             y += 110;
   165             y += 110;
   148 
   166 
   149             for (i = 0; i < (sizeof (buttons) / sizeof (buttons[0])); i++) {
   167             for (i = 0; i < (sizeof (buttons) / sizeof (buttons[0])); i++) {
   150                 CreateWindowA("BUTTON", buttons[i].name,
   168                 CreateWindow(TEXT("BUTTON"), buttons[i].name,
   151                          WS_VISIBLE | WS_CHILD,
   169                          WS_VISIBLE | WS_CHILD,
   152                          x, y, w, h,
   170                          x, y, w, h,
   153                          hwnd, (HMENU) buttons[i].state, NULL, NULL);
   171                          hwnd, (HMENU) buttons[i].state, NULL, NULL);
   154                 x += w + gap;
   172                 x += w + gap;
   155             }
   173             }
   246     }
   264     }
   247 }
   265 }
   248 
   266 
   249 static void SDL_ExitProcess(int exitcode)
   267 static void SDL_ExitProcess(int exitcode)
   250 {
   268 {
   251 #ifdef __WINDOWS__
   269 #ifdef __WIN32__
   252     ExitProcess(42);
   270     ExitProcess(42);
   253 #else
   271 #else
   254     _exit(42);
   272     _exit(42);
   255 #endif
   273 #endif
   256 }
   274 }
   309         }
   327         }
   310     }
   328     }
   311 
   329 
   312     /* platform-specific UI... */
   330     /* platform-specific UI... */
   313 
   331 
   314 #ifdef __WINDOWS__
   332 #ifdef __WIN32__
   315     state = SDL_PromptAssertion_windows(data);
   333     state = SDL_PromptAssertion_windows(data);
   316 
   334 
   317 #elif __MACOSX__
   335 #elif __MACOSX__
   318     /* This has to be done in an Objective-C (*.m) file, so we call out. */
   336     /* This has to be done in an Objective-C (*.m) file, so we call out. */
   319     extern SDL_assert_state SDL_PromptAssertion_cocoa(const SDL_assert_data *);
   337     extern SDL_assert_state SDL_PromptAssertion_cocoa(const SDL_assert_data *);