src/SDL_assert.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 03 Jun 2011 16:03:10 -0400
changeset 5547 4ccecd0901e2
parent 5541 b63f1383f8c9
child 6051 cb1f941ce38e
permissions -rw-r--r--
Assert code's stdio interface was reading from the wrong variable. Thanks to Frank Zago for the catch.
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
 */
5541
b63f1383f8c9 Removed assertion list terminator (just do it like a normal linked list).
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    47
static SDL_assert_data *triggered_assertions = NULL;
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
    48
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
    49
static SDL_mutex *assertion_mutex = NULL;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
    50
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
    51
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
    52
3648
a9d830c05998 Fixed build problems with gcc __attribute__.
Ryan C. Gordon <icculus@icculus.org>
parents: 3647
diff changeset
    53
#ifdef __GNUC__
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
    54
static void
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
    55
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
    56
#endif
a9d830c05998 Fixed build problems with gcc __attribute__.
Ryan C. Gordon <icculus@icculus.org>
parents: 3647
diff changeset
    57
a9d830c05998 Fixed build problems with gcc __attribute__.
Ryan C. Gordon <icculus@icculus.org>
parents: 3647
diff changeset
    58
static void
a9d830c05998 Fixed build problems with gcc __attribute__.
Ryan C. Gordon <icculus@icculus.org>
parents: 3647
diff changeset
    59
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
    60
{
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
    61
#ifdef __WIN32__
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    62
    /* 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
    63
    char buf[1024];
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    64
    char *startptr;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    65
    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
    66
    LPTSTR tstr;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    67
    int len;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    68
    va_list ap;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    69
    va_start(ap, fmt);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    70
    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
    71
    va_end(ap);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    72
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    73
    /* 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
    74
    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
    75
        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
    76
    }
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
    /* 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
    79
    startptr = buf;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    80
    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
    81
        if (*ptr == '\n') {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    82
            *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
    83
            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
    84
            OutputDebugString(tstr);
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
    85
            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
    86
            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
    87
            startptr = ptr+1;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    88
        }
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
    /* 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
    92
    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
    93
        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
    94
        OutputDebugString(tstr);
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
    95
        SDL_free(tstr);
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    96
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    97
#else
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    98
    /* 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
    99
    va_list ap;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   100
    va_start(ap, fmt);
3669
46d27a9571fa Whoops, that should be vfprintf(), not fprintf(). :)
Ryan C. Gordon <icculus@icculus.org>
parents: 3664
diff changeset
   101
    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
   102
    va_end(ap);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   103
    fflush(stderr);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   104
#endif
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   105
}
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
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
   108
#ifdef __WIN32__
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   109
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
   110
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
   111
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   112
static LRESULT CALLBACK
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   113
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
   114
{
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   115
    switch (msg)
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   116
    {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   117
        case WM_CREATE:
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   118
        {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   119
            /* !!! 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
   120
            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
   121
            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
   122
            LPTSTR tstr;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   123
            const int w = 100;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   124
            const int h = 25;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   125
            const int gap = 10;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   126
            int x = gap;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   127
            int y = 50;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   128
            int len;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   129
            int i;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   130
            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
   131
                LPCTSTR name;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   132
                SDL_assert_state state;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   133
            } buttons[] = {
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
   134
                {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
   135
                {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
   136
                {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
   137
                {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
   138
                {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
   139
            };
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
            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
   142
                         "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
   143
                         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
   144
                         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
   145
                         data->condition);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   146
            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
   147
                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
   148
            }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   149
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
   150
            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
   151
            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
   152
                         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
   153
                         x, y, 550, 100,
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   154
                         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
   155
            SDL_free(tstr);
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   156
            y += 110;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   157
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   158
            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
   159
                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
   160
                         WS_VISIBLE | WS_CHILD,
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   161
                         x, y, w, h,
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   162
                         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
   163
                x += w + gap;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   164
            }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   165
            break;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   166
        }
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
        case WM_COMMAND:
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   169
            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
   170
            SDL_Windows_AssertData = NULL;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   171
            break;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   172
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   173
        case WM_DESTROY:
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   174
            SDL_Windows_AssertData = NULL;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   175
            break;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   176
    }
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
    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
   179
}
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
static SDL_assert_state
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   182
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
   183
{
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   184
    HINSTANCE hInstance = 0;  /* !!! FIXME? */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   185
    HWND hwnd;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   186
    MSG msg;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   187
    WNDCLASS wc = {0};
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   188
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   189
    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
   190
    SDL_Windows_AssertData = data;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   191
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   192
    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
   193
    wc.hInstance = hInstance ;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   194
    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
   195
    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
   196
    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
   197
  
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   198
    RegisterClass(&wc);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   199
    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
   200
                 WS_OVERLAPPEDWINDOW | WS_VISIBLE,
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   201
                 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
   202
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   203
    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
   204
        TranslateMessage(&msg);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   205
        DispatchMessage(&msg);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   206
    }
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
    DestroyWindow(hwnd);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   209
    UnregisterClass(wc.lpszClassName, hInstance);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   210
    return SDL_Windows_AssertChoice;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   211
}
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   212
#endif
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   213
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
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
   216
{
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   217
    /* (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
   218
       we don't have to worry about copying or allocating them. */
5541
b63f1383f8c9 Removed assertion list terminator (just do it like a normal linked list).
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   219
    data->trigger_count++;
b63f1383f8c9 Removed assertion list terminator (just do it like a normal linked list).
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   220
    if (data->trigger_count == 1) {  /* not yet added? */
3647
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
{
5541
b63f1383f8c9 Removed assertion list terminator (just do it like a normal linked list).
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   229
    const SDL_assert_data *item = triggered_assertions;
3670
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. */
5541
b63f1383f8c9 Removed assertion list terminator (just do it like a normal linked list).
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   232
    if ((item != NULL) && (assertion_handler != SDL_PromptAssertion)) {
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   233
        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
   234
        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
   235
5541
b63f1383f8c9 Removed assertion list terminator (just do it like a normal linked list).
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   236
        while (item != NULL) {
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   237
            debug_print(
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   238
                "'%s'\n"
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   239
                "    * %s (%s:%d)\n"
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   240
                "    * 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
   241
                "    * always ignore: %s.\n",
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   242
                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
   243
                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
   244
                (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
   245
                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
   246
            item = item->next;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   247
        }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   248
        debug_print("\n");
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   249
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   250
        SDL_ResetAssertionReport();
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   251
    }
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
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   254
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
   255
{
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
   256
#ifdef __WIN32__
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   257
    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
   258
#else
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   259
    _exit(42);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   260
#endif
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   261
}
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   262
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   263
static void SDL_AbortAssertion(void)
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   264
{
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   265
    SDL_Quit();
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   266
    SDL_ExitProcess(42);
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
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   269
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   270
static SDL_assert_state
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   271
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
   272
{
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   273
    const char *envr;
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   274
    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
   275
    SDL_Window *window;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   276
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   277
    (void) userdata;  /* unused in default handler. */
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   278
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   279
    debug_print("\n\n"
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   280
                "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
   281
                "  '%s'\n"
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   282
                "\n",
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   283
                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
   284
                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
   285
                data->condition);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   286
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
   287
    /* 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
   288
    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
   289
    if (envr != NULL) {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   290
        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
   291
            return SDL_ASSERTION_ABORT;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   292
        } 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
   293
            return SDL_ASSERTION_BREAK;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   294
        } 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
   295
            return SDL_ASSERTION_RETRY;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   296
        } 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
   297
            return SDL_ASSERTION_IGNORE;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   298
        } 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
   299
            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
   300
        } else {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   301
            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
   302
        }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   303
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   304
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   305
    /* Leave fullscreen mode, if possible (scary!) */
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   306
    window = SDL_GetFocusWindow();
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   307
    if (window) {
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   308
        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
   309
            SDL_MinimizeWindow(window);
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   310
        } else {
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   311
            /* !!! 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
   312
            /* 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
   313
            window = 0;
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   314
        }
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   315
    }
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   316
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   317
    /* platform-specific UI... */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   318
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
   319
#ifdef __WIN32__
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   320
    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
   321
3690
e431b888ac6c Fixed compilation on iPhone
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
   322
#elif __MACOSX__
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   323
    /* 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
   324
    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
   325
    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
   326
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   327
#else
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   328
    /* 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
   329
    for ( ; ; ) {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   330
        char buf[32];
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   331
        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
   332
        fflush(stderr);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   333
        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
   334
            break;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   335
        }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   336
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   337
        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
   338
            state = SDL_ASSERTION_ABORT;
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   339
            break;
5547
4ccecd0901e2 Assert code's stdio interface was reading from the wrong variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 5541
diff changeset
   340
        } else if (SDL_strcmp(buf, "b") == 0) {
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   341
            state = SDL_ASSERTION_BREAK;
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   342
            break;
5547
4ccecd0901e2 Assert code's stdio interface was reading from the wrong variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 5541
diff changeset
   343
        } else if (SDL_strcmp(buf, "r") == 0) {
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   344
            state = SDL_ASSERTION_RETRY;
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   345
            break;
5547
4ccecd0901e2 Assert code's stdio interface was reading from the wrong variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 5541
diff changeset
   346
        } else if (SDL_strcmp(buf, "i") == 0) {
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   347
            state = SDL_ASSERTION_IGNORE;
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   348
            break;
5547
4ccecd0901e2 Assert code's stdio interface was reading from the wrong variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 5541
diff changeset
   349
        } else 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
   350
            state = SDL_ASSERTION_ALWAYS_IGNORE;
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   351
            break;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   352
        }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   353
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   354
#endif
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   355
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   356
    /* Re-enter fullscreen mode */
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   357
    if (window) {
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   358
        SDL_RestoreWindow(window);
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   359
    }
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   360
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   361
    return state;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   362
}
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   363
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   364
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   365
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
   366
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
   367
                    int line)
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   368
{
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   369
    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
   370
    static SDL_SpinLock spinlock = 0;
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   371
    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
   372
3662
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   373
    SDL_AtomicLock(&spinlock);
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   374
    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
   375
        assertion_mutex = SDL_CreateMutex();
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   376
        if (assertion_mutex == NULL) {
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   377
            SDL_AtomicUnlock(&spinlock);
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   378
            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
   379
        }
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   380
    }
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   381
    SDL_AtomicUnlock(&spinlock);
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   382
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   383
    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
   384
        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
   385
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   386
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   387
    /* 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
   388
    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
   389
        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
   390
        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
   391
        data->linenum = line;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   392
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   393
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   394
    SDL_AddAssertionToReport(data);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   395
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   396
    assertion_running++;
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   397
    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
   398
        if (assertion_running == 2) {
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   399
            SDL_AbortAssertion();
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   400
        } else if (assertion_running == 3) {  /* Abort asserted! */
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   401
            SDL_ExitProcess(42);
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   402
        } else {
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   403
            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
   404
        }
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   405
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   406
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   407
    if (!data->always_ignore) {
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   408
        state = assertion_handler(data, assertion_userdata);
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   409
    }
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   410
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   411
    switch (state)
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
        case SDL_ASSERTION_ABORT:
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   414
            SDL_AbortAssertion();
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   415
            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
   416
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   417
        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
   418
            state = SDL_ASSERTION_IGNORE;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   419
            data->always_ignore = 1;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   420
            break;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   421
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   422
        case SDL_ASSERTION_IGNORE:
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   423
        case SDL_ASSERTION_RETRY:
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   424
        case SDL_ASSERTION_BREAK:
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   425
            break;  /* macro handles these. */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   426
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   427
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   428
    assertion_running--;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   429
    SDL_UnlockMutex(assertion_mutex);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   430
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   431
    return state;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   432
}
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
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   435
int SDL_AssertionsInit(void)
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   436
{
3664
8bdc37b1a52a Handle assert init/quit better.
Ryan C. Gordon <icculus@icculus.org>
parents: 3662
diff changeset
   437
    /* 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
   438
    return 0;
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
void SDL_AssertionsQuit(void)
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   442
{
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   443
    SDL_GenerateAssertionReport();
3664
8bdc37b1a52a Handle assert init/quit better.
Ryan C. Gordon <icculus@icculus.org>
parents: 3662
diff changeset
   444
    if (assertion_mutex != NULL) {
8bdc37b1a52a Handle assert init/quit better.
Ryan C. Gordon <icculus@icculus.org>
parents: 3662
diff changeset
   445
        SDL_DestroyMutex(assertion_mutex);
8bdc37b1a52a Handle assert init/quit better.
Ryan C. Gordon <icculus@icculus.org>
parents: 3662
diff changeset
   446
        assertion_mutex = NULL;
8bdc37b1a52a Handle assert init/quit better.
Ryan C. Gordon <icculus@icculus.org>
parents: 3662
diff changeset
   447
    }
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   448
}
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   449
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   450
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
   451
{
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   452
    if (handler != NULL) {
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   453
        assertion_handler = handler;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   454
        assertion_userdata = userdata;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   455
    } else {
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   456
        assertion_handler = SDL_PromptAssertion;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   457
        assertion_userdata = NULL;
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
}
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   460
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   461
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
   462
{
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   463
    return triggered_assertions;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   464
}
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
void SDL_ResetAssertionReport(void)
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
    SDL_assert_data *next = NULL;
5541
b63f1383f8c9 Removed assertion list terminator (just do it like a normal linked list).
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   469
    SDL_assert_data *item;
b63f1383f8c9 Removed assertion list terminator (just do it like a normal linked list).
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   470
    for (item = triggered_assertions; item != NULL; item = next) {
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   471
        next = (SDL_assert_data *) item->next;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   472
        item->always_ignore = SDL_FALSE;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   473
        item->trigger_count = 0;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   474
        item->next = NULL;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   475
    }
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   476
5541
b63f1383f8c9 Removed assertion list terminator (just do it like a normal linked list).
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   477
    triggered_assertions = NULL;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   478
}
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   479
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   480
/* vi: set ts=4 sw=4 expandtab: */