src/SDL_assert.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 08 Apr 2011 13:03:26 -0700
changeset 5535 96594ac5fd1a
parent 5262 b530ef003506
child 5541 b63f1383f8c9
permissions -rw-r--r--
SDL 1.3 is now under the zlib license.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     1
/*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     2
  Simple DirectMedia Layer
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     3
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     5
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     7
  arising from the use of this software.
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    11
  freely, subject to the following restrictions:
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    14
     claim that you wrote the original software. If you use this software
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    18
     misrepresented as being the original software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    20
*/
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    21
3651
cb5b1aedb5a7 Fixed include ordering
Sam Lantinga <slouken@libsdl.org>
parents: 3648
diff changeset
    22
#include "SDL.h"
5006
8e8876e4aec6 Include windows.h in SDL_atomic.h by default, but don't include the atomic API in SDL.h
Sam Lantinga <slouken@libsdl.org>
parents: 4472
diff changeset
    23
#include "SDL_atomic.h"
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    24
#include "SDL_assert.h"
4472
791b3256fb22 Mostly cleaned up warnings with -Wmissing-prototypes
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
    25
#include "SDL_assert_c.h"
3671
0d6f520c0eb9 Fixed building under Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 3670
diff changeset
    26
#include "video/SDL_sysvideo.h"
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    27
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
    28
#ifdef __WIN32__
5090
327f181542f1 Include windows.h in a single point in the source, so we can be consistent about the definition of UNICODE and have core utility functions for Windows that all modules can share.
Sam Lantinga <slouken@libsdl.org>
parents: 5086
diff changeset
    29
#include "core/windows/SDL_windows.h"
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
    30
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
    31
#ifndef WS_OVERLAPPEDWINDOW
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
    32
#define WS_OVERLAPPEDWINDOW 0
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
    33
#endif
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    34
#else  /* fprintf, _exit(), etc. */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    35
#include <stdio.h>
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    36
#include <stdlib.h>
3648
a9d830c05998 Fixed build problems with gcc __attribute__.
Ryan C. Gordon <icculus@icculus.org>
parents: 3647
diff changeset
    37
#include <unistd.h>
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    38
#endif
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    39
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
    40
static SDL_assert_state
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
    41
SDL_PromptAssertion(const SDL_assert_data *data, void *userdata);
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
    42
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
    43
/*
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
    44
 * We keep all triggered assertions in a singly-linked list so we can
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    45
 *  generate a report later.
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    46
 */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    47
static SDL_assert_data assertion_list_terminator = { 0, 0, 0, 0, 0, 0, 0 };
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    48
static SDL_assert_data *triggered_assertions = &assertion_list_terminator;
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
    49
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
    50
static SDL_mutex *assertion_mutex = NULL;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
    51
static SDL_AssertionHandler assertion_handler = SDL_PromptAssertion;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
    52
static void *assertion_userdata = NULL;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    53
3648
a9d830c05998 Fixed build problems with gcc __attribute__.
Ryan C. Gordon <icculus@icculus.org>
parents: 3647
diff changeset
    54
#ifdef __GNUC__
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
    55
static void
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
    56
debug_print(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
3648
a9d830c05998 Fixed build problems with gcc __attribute__.
Ryan C. Gordon <icculus@icculus.org>
parents: 3647
diff changeset
    57
#endif
a9d830c05998 Fixed build problems with gcc __attribute__.
Ryan C. Gordon <icculus@icculus.org>
parents: 3647
diff changeset
    58
a9d830c05998 Fixed build problems with gcc __attribute__.
Ryan C. Gordon <icculus@icculus.org>
parents: 3647
diff changeset
    59
static void
a9d830c05998 Fixed build problems with gcc __attribute__.
Ryan C. Gordon <icculus@icculus.org>
parents: 3647
diff changeset
    60
debug_print(const char *fmt, ...)
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    61
{
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
    62
#ifdef __WIN32__
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    63
    /* Format into a buffer for OutputDebugStringA(). */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    64
    char buf[1024];
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    65
    char *startptr;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    66
    char *ptr;
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
    67
    LPTSTR tstr;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    68
    int len;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    69
    va_list ap;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    70
    va_start(ap, fmt);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    71
    len = (int) SDL_vsnprintf(buf, sizeof (buf), fmt, ap);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    72
    va_end(ap);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    73
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    74
    /* Visual C's vsnprintf() may not null-terminate the buffer. */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    75
    if ((len >= sizeof (buf)) || (len < 0)) {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    76
        buf[sizeof (buf) - 1] = '\0';
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    77
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    78
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    79
    /* Write it, sorting out the Unix newlines... */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    80
    startptr = buf;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    81
    for (ptr = startptr; *ptr; ptr++) {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    82
        if (*ptr == '\n') {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    83
            *ptr = '\0';
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
    84
            tstr = WIN_UTF8ToString(startptr);
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
    85
            OutputDebugString(tstr);
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
    86
            SDL_free(tstr);
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
    87
            OutputDebugString(TEXT("\r\n"));
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    88
            startptr = ptr+1;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    89
        }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    90
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    91
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    92
    /* catch that last piece if it didn't have a newline... */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    93
    if (startptr != ptr) {
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
    94
        tstr = WIN_UTF8ToString(startptr);
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
    95
        OutputDebugString(tstr);
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
    96
        SDL_free(tstr);
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    97
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    98
#else
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    99
    /* Unix has it easy. Just dump it to stderr. */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   100
    va_list ap;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   101
    va_start(ap, fmt);
3669
46d27a9571fa Whoops, that should be vfprintf(), not fprintf(). :)
Ryan C. Gordon <icculus@icculus.org>
parents: 3664
diff changeset
   102
    vfprintf(stderr, fmt, ap);
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   103
    va_end(ap);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   104
    fflush(stderr);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   105
#endif
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   106
}
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   107
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   108
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
   109
#ifdef __WIN32__
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   110
static SDL_assert_state SDL_Windows_AssertChoice = SDL_ASSERTION_ABORT;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   111
static const SDL_assert_data *SDL_Windows_AssertData = NULL;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   112
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   113
static LRESULT CALLBACK
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   114
SDL_Assertion_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   115
{
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   116
    switch (msg)
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   117
    {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   118
        case WM_CREATE:
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   119
        {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   120
            /* !!! FIXME: all this code stinks. */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   121
            const SDL_assert_data *data = SDL_Windows_AssertData;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   122
            char buf[1024];
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
   123
            LPTSTR tstr;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   124
            const int w = 100;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   125
            const int h = 25;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   126
            const int gap = 10;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   127
            int x = gap;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   128
            int y = 50;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   129
            int len;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   130
            int i;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   131
            static const struct { 
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
   132
                LPCTSTR name;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   133
                SDL_assert_state state;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   134
            } buttons[] = {
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
   135
                {TEXT("Abort"), SDL_ASSERTION_ABORT },
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
   136
                {TEXT("Break"), SDL_ASSERTION_BREAK },
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
   137
                {TEXT("Retry"), SDL_ASSERTION_RETRY },
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
   138
                {TEXT("Ignore"), SDL_ASSERTION_IGNORE },
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
   139
                {TEXT("Always Ignore"), SDL_ASSERTION_ALWAYS_IGNORE },
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   140
            };
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   141
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   142
            len = (int) SDL_snprintf(buf, sizeof (buf), 
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   143
                         "Assertion failure at %s (%s:%d), triggered %u time%s:\r\n  '%s'",
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   144
                         data->function, data->filename, data->linenum,
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   145
                         data->trigger_count, (data->trigger_count == 1) ? "" : "s",
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   146
                         data->condition);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   147
            if ((len < 0) || (len >= sizeof (buf))) {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   148
                buf[sizeof (buf) - 1] = '\0';
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   149
            }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   150
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
   151
            tstr = WIN_UTF8ToString(buf);
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
   152
            CreateWindow(TEXT("STATIC"), tstr,
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   153
                         WS_VISIBLE | WS_CHILD | SS_LEFT,
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   154
                         x, y, 550, 100,
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   155
                         hwnd, (HMENU) 1, NULL, NULL);
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
   156
            SDL_free(tstr);
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   157
            y += 110;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   158
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   159
            for (i = 0; i < (sizeof (buttons) / sizeof (buttons[0])); i++) {
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
   160
                CreateWindow(TEXT("BUTTON"), buttons[i].name,
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   161
                         WS_VISIBLE | WS_CHILD,
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   162
                         x, y, w, h,
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   163
                         hwnd, (HMENU) buttons[i].state, NULL, NULL);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   164
                x += w + gap;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   165
            }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   166
            break;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   167
        }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   168
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   169
        case WM_COMMAND:
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   170
            SDL_Windows_AssertChoice = ((SDL_assert_state) (LOWORD(wParam)));
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   171
            SDL_Windows_AssertData = NULL;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   172
            break;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   173
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   174
        case WM_DESTROY:
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   175
            SDL_Windows_AssertData = NULL;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   176
            break;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   177
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   178
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   179
    return DefWindowProc(hwnd, msg, wParam, lParam);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   180
}
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   181
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   182
static SDL_assert_state
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   183
SDL_PromptAssertion_windows(const SDL_assert_data *data)
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   184
{
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   185
    HINSTANCE hInstance = 0;  /* !!! FIXME? */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   186
    HWND hwnd;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   187
    MSG msg;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   188
    WNDCLASS wc = {0};
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   189
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   190
    SDL_Windows_AssertChoice = SDL_ASSERTION_ABORT;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   191
    SDL_Windows_AssertData = data;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   192
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   193
    wc.lpszClassName = TEXT("SDL_assert");
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   194
    wc.hInstance = hInstance ;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   195
    wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   196
    wc.lpfnWndProc = SDL_Assertion_WndProc;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   197
    wc.hCursor = LoadCursor(0, IDC_ARROW);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   198
  
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   199
    RegisterClass(&wc);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   200
    hwnd = CreateWindow(wc.lpszClassName, TEXT("SDL assertion failure"),
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   201
                 WS_OVERLAPPEDWINDOW | WS_VISIBLE,
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   202
                 150, 150, 570, 260, 0, 0, hInstance, 0);  
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   203
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   204
    while (GetMessage(&msg, NULL, 0, 0) && (SDL_Windows_AssertData != NULL)) {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   205
        TranslateMessage(&msg);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   206
        DispatchMessage(&msg);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   207
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   208
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   209
    DestroyWindow(hwnd);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   210
    UnregisterClass(wc.lpszClassName, hInstance);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   211
    return SDL_Windows_AssertChoice;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   212
}
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   213
#endif
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   214
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   215
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   216
static void SDL_AddAssertionToReport(SDL_assert_data *data)
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   217
{
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   218
    /* (data) is always a static struct defined with the assert macros, so
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   219
       we don't have to worry about copying or allocating them. */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   220
    if (data->next == NULL) {  /* not yet added? */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   221
        data->next = triggered_assertions;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   222
        triggered_assertions = data;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   223
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   224
}
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   225
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   226
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   227
static void SDL_GenerateAssertionReport(void)
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   228
{
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   229
    const SDL_assert_data *item;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   230
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   231
    /* only do this if the app hasn't assigned an assertion handler. */
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   232
    if (assertion_handler != SDL_PromptAssertion)
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   233
        return;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   234
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   235
    item = SDL_GetAssertionReport();
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   236
    if (item->condition)
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   237
    {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   238
        debug_print("\n\nSDL assertion report.\n");
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   239
        debug_print("All SDL assertions between last init/quit:\n\n");
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   240
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   241
        while (item->condition) {
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   242
            debug_print(
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   243
                "'%s'\n"
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   244
                "    * %s (%s:%d)\n"
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   245
                "    * triggered %u time%s.\n"
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   246
                "    * always ignore: %s.\n",
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   247
                item->condition, item->function, item->filename,
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   248
                item->linenum, item->trigger_count,
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   249
                (item->trigger_count == 1) ? "" : "s",
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   250
                item->always_ignore ? "yes" : "no");
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   251
            item = item->next;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   252
        }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   253
        debug_print("\n");
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   254
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   255
        SDL_ResetAssertionReport();
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   256
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   257
}
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   258
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   259
static void SDL_ExitProcess(int exitcode)
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   260
{
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
   261
#ifdef __WIN32__
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   262
    ExitProcess(42);
3656
f17ea6f49745 Assume _exit() is available. It may be, even if unix isn't defined.
Sam Lantinga <slouken@libsdl.org>
parents: 3655
diff changeset
   263
#else
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   264
    _exit(42);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   265
#endif
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   266
}
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   267
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   268
static void SDL_AbortAssertion(void)
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   269
{
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   270
    SDL_Quit();
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   271
    SDL_ExitProcess(42);
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   272
}
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   273
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   274
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   275
static SDL_assert_state
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   276
SDL_PromptAssertion(const SDL_assert_data *data, void *userdata)
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   277
{
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   278
    const char *envr;
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   279
    SDL_assert_state state = SDL_ASSERTION_ABORT;
3685
64ce267332c6 Switched from SDL_WindowID and SDL_TextureID to SDL_Window* and SDL_Texture* for code simplicity and improved performance.
Sam Lantinga <slouken@libsdl.org>
parents: 3671
diff changeset
   280
    SDL_Window *window;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   281
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   282
    (void) userdata;  /* unused in default handler. */
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   283
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   284
    debug_print("\n\n"
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   285
                "Assertion failure at %s (%s:%d), triggered %u time%s:\n"
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   286
                "  '%s'\n"
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   287
                "\n",
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   288
                data->function, data->filename, data->linenum,
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   289
                data->trigger_count, (data->trigger_count == 1) ? "" : "s",
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   290
                data->condition);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   291
3655
1cc7f0143c12 Moved SDL_FUNCTION out so it's always available, and added SDL_FILE and SDL_LINE
Sam Lantinga <slouken@libsdl.org>
parents: 3651
diff changeset
   292
    /* let env. variable override, so unit tests won't block in a GUI. */
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   293
    envr = SDL_getenv("SDL_ASSERT");
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   294
    if (envr != NULL) {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   295
        if (SDL_strcmp(envr, "abort") == 0) {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   296
            return SDL_ASSERTION_ABORT;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   297
        } else if (SDL_strcmp(envr, "break") == 0) {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   298
            return SDL_ASSERTION_BREAK;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   299
        } else if (SDL_strcmp(envr, "retry") == 0) {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   300
            return SDL_ASSERTION_RETRY;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   301
        } else if (SDL_strcmp(envr, "ignore") == 0) {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   302
            return SDL_ASSERTION_IGNORE;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   303
        } else if (SDL_strcmp(envr, "always_ignore") == 0) {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   304
            return SDL_ASSERTION_ALWAYS_IGNORE;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   305
        } else {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   306
            return SDL_ASSERTION_ABORT;  /* oh well. */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   307
        }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   308
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   309
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   310
    /* Leave fullscreen mode, if possible (scary!) */
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   311
    window = SDL_GetFocusWindow();
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   312
    if (window) {
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   313
        if (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN) {
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   314
            SDL_MinimizeWindow(window);
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   315
        } else {
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   316
            /* !!! FIXME: ungrab the input if we're not fullscreen? */
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   317
            /* No need to mess with the window */
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   318
            window = 0;
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   319
        }
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   320
    }
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   321
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   322
    /* platform-specific UI... */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   323
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
   324
#ifdef __WIN32__
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   325
    state = SDL_PromptAssertion_windows(data);
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   326
3690
e431b888ac6c Fixed compilation on iPhone
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
   327
#elif __MACOSX__
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   328
    /* This has to be done in an Objective-C (*.m) file, so we call out. */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   329
    extern SDL_assert_state SDL_PromptAssertion_cocoa(const SDL_assert_data *);
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   330
    state = SDL_PromptAssertion_cocoa(data);
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   331
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   332
#else
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   333
    /* this is a little hacky. */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   334
    for ( ; ; ) {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   335
        char buf[32];
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   336
        fprintf(stderr, "Abort/Break/Retry/Ignore/AlwaysIgnore? [abriA] : ");
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   337
        fflush(stderr);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   338
        if (fgets(buf, sizeof (buf), stdin) == NULL) {
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   339
            break;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   340
        }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   341
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   342
        if (SDL_strcmp(buf, "a") == 0) {
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   343
            state = SDL_ASSERTION_ABORT;
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   344
            break;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   345
        } else if (SDL_strcmp(envr, "b") == 0) {
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   346
            state = SDL_ASSERTION_BREAK;
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   347
            break;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   348
        } else if (SDL_strcmp(envr, "r") == 0) {
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   349
            state = SDL_ASSERTION_RETRY;
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   350
            break;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   351
        } else if (SDL_strcmp(envr, "i") == 0) {
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   352
            state = SDL_ASSERTION_IGNORE;
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   353
            break;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   354
        } else if (SDL_strcmp(envr, "A") == 0) {
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   355
            state = SDL_ASSERTION_ALWAYS_IGNORE;
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   356
            break;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   357
        }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   358
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   359
#endif
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   360
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   361
    /* Re-enter fullscreen mode */
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   362
    if (window) {
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   363
        SDL_RestoreWindow(window);
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   364
    }
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   365
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   366
    return state;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   367
}
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   368
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   369
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   370
SDL_assert_state
3655
1cc7f0143c12 Moved SDL_FUNCTION out so it's always available, and added SDL_FILE and SDL_LINE
Sam Lantinga <slouken@libsdl.org>
parents: 3651
diff changeset
   371
SDL_ReportAssertion(SDL_assert_data *data, const char *func, const char *file,
1cc7f0143c12 Moved SDL_FUNCTION out so it's always available, and added SDL_FILE and SDL_LINE
Sam Lantinga <slouken@libsdl.org>
parents: 3651
diff changeset
   372
                    int line)
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   373
{
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   374
    static int assertion_running = 0;
3662
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   375
    static SDL_SpinLock spinlock = 0;
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   376
    SDL_assert_state state = SDL_ASSERTION_IGNORE;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   377
3662
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   378
    SDL_AtomicLock(&spinlock);
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   379
    if (assertion_mutex == NULL) { /* never called SDL_Init()? */
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   380
        assertion_mutex = SDL_CreateMutex();
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   381
        if (assertion_mutex == NULL) {
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   382
            SDL_AtomicUnlock(&spinlock);
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   383
            return SDL_ASSERTION_IGNORE;   /* oh well, I guess. */
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   384
        }
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   385
    }
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   386
    SDL_AtomicUnlock(&spinlock);
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   387
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   388
    if (SDL_LockMutex(assertion_mutex) < 0) {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   389
        return SDL_ASSERTION_IGNORE;   /* oh well, I guess. */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   390
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   391
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   392
    /* doing this because Visual C is upset over assigning in the macro. */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   393
    if (data->trigger_count == 0) {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   394
        data->function = func;
3655
1cc7f0143c12 Moved SDL_FUNCTION out so it's always available, and added SDL_FILE and SDL_LINE
Sam Lantinga <slouken@libsdl.org>
parents: 3651
diff changeset
   395
        data->filename = file;
1cc7f0143c12 Moved SDL_FUNCTION out so it's always available, and added SDL_FILE and SDL_LINE
Sam Lantinga <slouken@libsdl.org>
parents: 3651
diff changeset
   396
        data->linenum = line;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   397
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   398
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   399
    SDL_AddAssertionToReport(data);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   400
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   401
    data->trigger_count++;
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   402
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   403
    assertion_running++;
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   404
    if (assertion_running > 1) {   /* assert during assert! Abort. */
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   405
        if (assertion_running == 2) {
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   406
            SDL_AbortAssertion();
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   407
        } else if (assertion_running == 3) {  /* Abort asserted! */
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   408
            SDL_ExitProcess(42);
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   409
        } else {
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   410
            while (1) { /* do nothing but spin; what else can you do?! */ }
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   411
        }
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   412
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   413
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   414
    if (!data->always_ignore) {
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   415
        state = assertion_handler(data, assertion_userdata);
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   416
    }
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   417
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   418
    switch (state)
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   419
    {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   420
        case SDL_ASSERTION_ABORT:
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   421
            SDL_AbortAssertion();
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   422
            return SDL_ASSERTION_IGNORE;  /* shouldn't return, but oh well. */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   423
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   424
        case SDL_ASSERTION_ALWAYS_IGNORE:
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   425
            state = SDL_ASSERTION_IGNORE;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   426
            data->always_ignore = 1;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   427
            break;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   428
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   429
        case SDL_ASSERTION_IGNORE:
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   430
        case SDL_ASSERTION_RETRY:
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   431
        case SDL_ASSERTION_BREAK:
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   432
            break;  /* macro handles these. */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   433
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   434
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   435
    assertion_running--;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   436
    SDL_UnlockMutex(assertion_mutex);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   437
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   438
    return state;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   439
}
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   440
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   441
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   442
int SDL_AssertionsInit(void)
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   443
{
3664
8bdc37b1a52a Handle assert init/quit better.
Ryan C. Gordon <icculus@icculus.org>
parents: 3662
diff changeset
   444
    /* this is a no-op at the moment. */
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   445
    return 0;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   446
}
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   447
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   448
void SDL_AssertionsQuit(void)
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   449
{
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   450
    SDL_GenerateAssertionReport();
3664
8bdc37b1a52a Handle assert init/quit better.
Ryan C. Gordon <icculus@icculus.org>
parents: 3662
diff changeset
   451
    if (assertion_mutex != NULL) {
8bdc37b1a52a Handle assert init/quit better.
Ryan C. Gordon <icculus@icculus.org>
parents: 3662
diff changeset
   452
        SDL_DestroyMutex(assertion_mutex);
8bdc37b1a52a Handle assert init/quit better.
Ryan C. Gordon <icculus@icculus.org>
parents: 3662
diff changeset
   453
        assertion_mutex = NULL;
8bdc37b1a52a Handle assert init/quit better.
Ryan C. Gordon <icculus@icculus.org>
parents: 3662
diff changeset
   454
    }
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   455
}
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   456
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   457
void SDL_SetAssertionHandler(SDL_AssertionHandler handler, void *userdata)
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   458
{
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   459
    if (handler != NULL) {
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   460
        assertion_handler = handler;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   461
        assertion_userdata = userdata;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   462
    } else {
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   463
        assertion_handler = SDL_PromptAssertion;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   464
        assertion_userdata = NULL;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   465
    }
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   466
}
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   467
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   468
const SDL_assert_data *SDL_GetAssertionReport(void)
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   469
{
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   470
    return triggered_assertions;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   471
}
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   472
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   473
void SDL_ResetAssertionReport(void)
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   474
{
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   475
    SDL_assert_data *item = triggered_assertions;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   476
    SDL_assert_data *next = NULL;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   477
    for (item = triggered_assertions; item->condition; item = next) {
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   478
        next = (SDL_assert_data *) item->next;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   479
        item->always_ignore = SDL_FALSE;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   480
        item->trigger_count = 0;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   481
        item->next = NULL;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   482
    }
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   483
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   484
    triggered_assertions = &assertion_list_terminator;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   485
}
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   486
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   487
/* vi: set ts=4 sw=4 expandtab: */