test/testintersections.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Wed, 17 Aug 2016 21:04:50 +0200
changeset 10275 ba7dc3ebb81e
parent 9998 f67cf37e9cd4
permissions -rw-r--r--
Emscripten: Fixed opening previously closed joystick.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5297
diff changeset
     1
/*
9998
f67cf37e9cd4 Updated copyright to 2016
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
     2
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5297
diff changeset
     3
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5297
diff changeset
     4
  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: 5297
diff changeset
     5
  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: 5297
diff changeset
     6
  arising from the use of this software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5297
diff changeset
     7
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5297
diff changeset
     8
  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: 5297
diff changeset
     9
  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: 5297
diff changeset
    10
  freely.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5297
diff changeset
    11
*/
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    13
/* Simple program:  draw as many random objects on the screen as possible */
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    14
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    15
#include <stdlib.h>
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    16
#include <stdio.h>
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    17
#include <time.h>
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    18
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    19
#ifdef __EMSCRIPTEN__
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    20
#include <emscripten/emscripten.h>
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    21
#endif
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    22
6785
c094c438c0a6 Switched the test code over to use the common functions in the test library.
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
    23
#include "SDL_test_common.h"
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    24
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    25
#define SWAP(typ,a,b) do{typ t=a;a=b;b=t;}while(0)
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6785
diff changeset
    26
#define NUM_OBJECTS 100
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    27
6785
c094c438c0a6 Switched the test code over to use the common functions in the test library.
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
    28
static SDLTest_CommonState *state;
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    29
static int num_objects;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    30
static SDL_bool cycle_color;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    31
static SDL_bool cycle_alpha;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    32
static int cycle_direction = 1;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    33
static int current_alpha = 255;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    34
static int current_color = 255;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    35
static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    36
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    37
int mouse_begin_x = -1, mouse_begin_y = -1;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    38
int done;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    39
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    40
void
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5184
diff changeset
    41
DrawPoints(SDL_Renderer * renderer)
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    42
{
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    43
    int i;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    44
    int x, y;
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5184
diff changeset
    45
    SDL_Rect viewport;
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    46
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    47
    /* Query the sizes */
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5184
diff changeset
    48
    SDL_RenderGetViewport(renderer, &viewport);
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    49
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    50
    for (i = 0; i < num_objects * 4; ++i) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    51
        /* Cycle the color and alpha, if desired */
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    52
        if (cycle_color) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    53
            current_color += cycle_direction;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    54
            if (current_color < 0) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    55
                current_color = 0;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    56
                cycle_direction = -cycle_direction;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    57
            }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    58
            if (current_color > 255) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    59
                current_color = 255;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    60
                cycle_direction = -cycle_direction;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    61
            }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    62
        }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    63
        if (cycle_alpha) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    64
            current_alpha += cycle_direction;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    65
            if (current_alpha < 0) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    66
                current_alpha = 0;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    67
                cycle_direction = -cycle_direction;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    68
            }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    69
            if (current_alpha > 255) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    70
                current_alpha = 255;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    71
                cycle_direction = -cycle_direction;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    72
            }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    73
        }
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5140
diff changeset
    74
        SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color,
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    75
                               (Uint8) current_color, (Uint8) current_alpha);
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    76
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5184
diff changeset
    77
        x = rand() % viewport.w;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5184
diff changeset
    78
        y = rand() % viewport.h;
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5140
diff changeset
    79
        SDL_RenderDrawPoint(renderer, x, y);
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    80
    }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    81
}
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    82
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    83
#define MAX_LINES 16
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    84
int num_lines = 0;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    85
SDL_Rect lines[MAX_LINES];
2997
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
    86
static int
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
    87
add_line(int x1, int y1, int x2, int y2)
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
    88
{
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
    89
    if (num_lines >= MAX_LINES)
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
    90
        return 0;
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
    91
    if ((x1 == x2) && (y1 == y2))
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
    92
        return 0;
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    93
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
    94
    SDL_Log("adding line (%d, %d), (%d, %d)\n", x1, y1, x2, y2);
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    95
    lines[num_lines].x = x1;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    96
    lines[num_lines].y = y1;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    97
    lines[num_lines].w = x2;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    98
    lines[num_lines].h = y2;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    99
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   100
    return ++num_lines;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   101
}
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   102
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   103
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   104
void
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5184
diff changeset
   105
DrawLines(SDL_Renderer * renderer)
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   106
{
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   107
    int i;
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5184
diff changeset
   108
    SDL_Rect viewport;
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   109
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   110
    /* Query the sizes */
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5184
diff changeset
   111
    SDL_RenderGetViewport(renderer, &viewport);
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5184
diff changeset
   112
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5184
diff changeset
   113
    SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   114
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   115
    for (i = 0; i < num_lines; ++i) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   116
        if (i == -1) {
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5184
diff changeset
   117
            SDL_RenderDrawLine(renderer, 0, 0, viewport.w - 1, viewport.h - 1);
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5184
diff changeset
   118
            SDL_RenderDrawLine(renderer, 0, viewport.h - 1, viewport.w - 1, 0);
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5184
diff changeset
   119
            SDL_RenderDrawLine(renderer, 0, viewport.h / 2, viewport.w - 1, viewport.h / 2);
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5184
diff changeset
   120
            SDL_RenderDrawLine(renderer, viewport.w / 2, 0, viewport.w / 2, viewport.h - 1);
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   121
        } else {
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5140
diff changeset
   122
            SDL_RenderDrawLine(renderer, lines[i].x, lines[i].y, lines[i].w, lines[i].h);
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   123
        }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   124
    }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   125
}
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   126
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   127
#define MAX_RECTS 16
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   128
int num_rects = 0;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   129
SDL_Rect rects[MAX_RECTS];
2997
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
   130
static int
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
   131
add_rect(int x1, int y1, int x2, int y2)
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
   132
{
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
   133
    if (num_rects >= MAX_RECTS)
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
   134
        return 0;
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
   135
    if ((x1 == x2) || (y1 == y2))
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
   136
        return 0;
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   137
2997
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
   138
    if (x1 > x2)
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
   139
        SWAP(int, x1, x2);
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
   140
    if (y1 > y2)
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
   141
        SWAP(int, y1, y2);
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   142
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
   143
    SDL_Log("adding rect (%d, %d), (%d, %d) [%dx%d]\n", x1, y1, x2, y2,
2997
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
   144
           x2 - x1, y2 - y1);
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   145
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   146
    rects[num_rects].x = x1;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   147
    rects[num_rects].y = y1;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   148
    rects[num_rects].w = x2 - x1;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   149
    rects[num_rects].h = y2 - y1;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   150
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   151
    return ++num_rects;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   152
}
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   153
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   154
static void
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5184
diff changeset
   155
DrawRects(SDL_Renderer * renderer)
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   156
{
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5184
diff changeset
   157
    SDL_SetRenderDrawColor(renderer, 255, 127, 0, 255);
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5184
diff changeset
   158
    SDL_RenderFillRects(renderer, rects, num_rects);
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   159
}
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   160
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   161
static void
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5184
diff changeset
   162
DrawRectLineIntersections(SDL_Renderer * renderer)
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   163
{
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5184
diff changeset
   164
    int i, j;
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   165
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5184
diff changeset
   166
    SDL_SetRenderDrawColor(renderer, 0, 255, 55, 255);
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   167
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   168
    for (i = 0; i < num_rects; i++)
2997
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
   169
        for (j = 0; j < num_lines; j++) {
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
   170
            int x1, y1, x2, y2;
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
   171
            SDL_Rect r;
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   172
2997
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
   173
            r = rects[i];
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
   174
            x1 = lines[j].x;
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
   175
            y1 = lines[j].y;
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
   176
            x2 = lines[j].w;
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
   177
            y2 = lines[j].h;
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
   178
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
   179
            if (SDL_IntersectRectAndLine(&r, &x1, &y1, &x2, &y2)) {
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5140
diff changeset
   180
                SDL_RenderDrawLine(renderer, x1, y1, x2, y2);
2997
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
   181
            }
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   182
        }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   183
}
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   184
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   185
static void
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5184
diff changeset
   186
DrawRectRectIntersections(SDL_Renderer * renderer)
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   187
{
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   188
    int i, j;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   189
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5184
diff changeset
   190
    SDL_SetRenderDrawColor(renderer, 255, 200, 0, 255);
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5184
diff changeset
   191
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   192
    for (i = 0; i < num_rects; i++)
2997
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
   193
        for (j = i + 1; j < num_rects; j++) {
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
   194
            SDL_Rect r;
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
   195
            if (SDL_IntersectRect(&rects[i], &rects[j], &r)) {
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5140
diff changeset
   196
                SDL_RenderFillRect(renderer, &r);
2997
Sam Lantinga <slouken@libsdl.org>
parents: 2994
diff changeset
   197
            }
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   198
        }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   199
}
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   200
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   201
void
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   202
loop()
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   203
{
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   204
    int i;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   205
    SDL_Event event;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   206
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   207
    /* Check for events */
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   208
    while (SDL_PollEvent(&event)) {
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   209
        SDLTest_CommonEvent(state, &event, &done);
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   210
        switch (event.type) {
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   211
        case SDL_MOUSEBUTTONDOWN:
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   212
            mouse_begin_x = event.button.x;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   213
            mouse_begin_y = event.button.y;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   214
            break;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   215
        case SDL_MOUSEBUTTONUP:
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   216
            if (event.button.button == 3)
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   217
                add_line(mouse_begin_x, mouse_begin_y, event.button.x,
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   218
                         event.button.y);
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   219
            if (event.button.button == 1)
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   220
                add_rect(mouse_begin_x, mouse_begin_y, event.button.x,
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   221
                         event.button.y);
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   222
            break;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   223
        case SDL_KEYDOWN:
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   224
            switch (event.key.keysym.sym) {
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   225
            case 'l':
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   226
                if (event.key.keysym.mod & KMOD_SHIFT)
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   227
                    num_lines = 0;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   228
                else
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   229
                    add_line(rand() % 640, rand() % 480, rand() % 640,
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   230
                             rand() % 480);
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   231
                break;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   232
            case 'r':
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   233
                if (event.key.keysym.mod & KMOD_SHIFT)
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   234
                    num_rects = 0;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   235
                else
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   236
                    add_rect(rand() % 640, rand() % 480, rand() % 640,
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   237
                             rand() % 480);
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   238
                break;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   239
            }
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   240
            break;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   241
        default:
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   242
            break;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   243
        }
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   244
    }
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   245
    for (i = 0; i < state->num_windows; ++i) {
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   246
        SDL_Renderer *renderer = state->renderers[i];
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   247
        if (state->windows[i] == NULL)
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   248
            continue;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   249
        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   250
        SDL_RenderClear(renderer);
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   251
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   252
        DrawRects(renderer);
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   253
        DrawPoints(renderer);
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   254
        DrawRectRectIntersections(renderer);
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   255
        DrawLines(renderer);
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   256
        DrawRectLineIntersections(renderer);
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   257
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   258
        SDL_RenderPresent(renderer);
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   259
    }
9607
7746ab058d12 Added missing loop cancel for Emscripten in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9278
diff changeset
   260
#ifdef __EMSCRIPTEN__
7746ab058d12 Added missing loop cancel for Emscripten in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9278
diff changeset
   261
    if (done) {
7746ab058d12 Added missing loop cancel for Emscripten in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9278
diff changeset
   262
        emscripten_cancel_main_loop();
7746ab058d12 Added missing loop cancel for Emscripten in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9278
diff changeset
   263
    }
7746ab058d12 Added missing loop cancel for Emscripten in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9278
diff changeset
   264
#endif
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   265
}
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   266
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   267
int
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   268
main(int argc, char *argv[])
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   269
{
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   270
    int i;
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   271
    Uint32 then, now, frames;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   272
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
   273
    /* Enable standard application logging */
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
   274
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
   275
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   276
    /* Initialize parameters */
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   277
    num_objects = NUM_OBJECTS;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   278
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   279
    /* Initialize test framework */
6785
c094c438c0a6 Switched the test code over to use the common functions in the test library.
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
   280
    state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   281
    if (!state) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   282
        return 1;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   283
    }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   284
    for (i = 1; i < argc;) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   285
        int consumed;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   286
6785
c094c438c0a6 Switched the test code over to use the common functions in the test library.
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
   287
        consumed = SDLTest_CommonArg(state, i);
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   288
        if (consumed == 0) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   289
            consumed = -1;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   290
            if (SDL_strcasecmp(argv[i], "--blend") == 0) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   291
                if (argv[i + 1]) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   292
                    if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   293
                        blendMode = SDL_BLENDMODE_NONE;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   294
                        consumed = 2;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   295
                    } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   296
                        blendMode = SDL_BLENDMODE_BLEND;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   297
                        consumed = 2;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   298
                    } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   299
                        blendMode = SDL_BLENDMODE_ADD;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   300
                        consumed = 2;
5184
d976b67150c5 Restored SDL_BLENDMODE_MOD for MAME
Sam Lantinga <slouken@libsdl.org>
parents: 5147
diff changeset
   301
                    } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
d976b67150c5 Restored SDL_BLENDMODE_MOD for MAME
Sam Lantinga <slouken@libsdl.org>
parents: 5147
diff changeset
   302
                        blendMode = SDL_BLENDMODE_MOD;
d976b67150c5 Restored SDL_BLENDMODE_MOD for MAME
Sam Lantinga <slouken@libsdl.org>
parents: 5147
diff changeset
   303
                        consumed = 2;
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   304
                    }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   305
                }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   306
            } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   307
                cycle_color = SDL_TRUE;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   308
                consumed = 1;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   309
            } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   310
                cycle_alpha = SDL_TRUE;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   311
                consumed = 1;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   312
            } else if (SDL_isdigit(*argv[i])) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   313
                num_objects = SDL_atoi(argv[i]);
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   314
                consumed = 1;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   315
            }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   316
        }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   317
        if (consumed < 0) {
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
   318
            SDL_Log("Usage: %s %s [--blend none|blend|add|mod] [--cyclecolor] [--cyclealpha]\n",
6785
c094c438c0a6 Switched the test code over to use the common functions in the test library.
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
   319
                    argv[0], SDLTest_CommonUsage(state));
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   320
            return 1;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   321
        }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   322
        i += consumed;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   323
    }
6785
c094c438c0a6 Switched the test code over to use the common functions in the test library.
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
   324
    if (!SDLTest_CommonInit(state)) {
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   325
        return 2;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   326
    }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   327
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   328
    /* Create the windows and initialize the renderers */
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   329
    for (i = 0; i < state->num_windows; ++i) {
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5140
diff changeset
   330
        SDL_Renderer *renderer = state->renderers[i];
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5140
diff changeset
   331
        SDL_SetRenderDrawBlendMode(renderer, blendMode);
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5140
diff changeset
   332
        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 5140
diff changeset
   333
        SDL_RenderClear(renderer);
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   334
    }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   335
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   336
    srand(time(NULL));
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   337
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   338
    /* Main render loop */
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   339
    frames = 0;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   340
    then = SDL_GetTicks();
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   341
    done = 0;
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   342
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   343
#ifdef __EMSCRIPTEN__
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   344
    emscripten_set_main_loop(loop, 0, 1);
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   345
#else
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   346
    while (!done) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   347
        ++frames;
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   348
        loop();
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   349
    }
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   350
#endif
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   351
6785
c094c438c0a6 Switched the test code over to use the common functions in the test library.
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
   352
    SDLTest_CommonQuit(state);
3371
438ba87e9578 Call CommonQuit() at exit has been added.
Mike Gorchak <lestat@i.com.ua>
parents: 2997
diff changeset
   353
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   354
    /* Print out some timing information */
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   355
    now = SDL_GetTicks();
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   356
    if (now > then) {
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   357
        double fps = ((double) frames * 1000) / (now - then);
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
   358
        SDL_Log("%2.2f frames per second\n", fps);
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   359
    }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   360
    return 0;
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   361
}
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   362
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   363
/* vi: set ts=4 sw=4 expandtab: */