test/testlines2.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 20 Dec 2008 18:26:06 +0000
changeset 2892 e4bafaa29c65
parent 2887 f8c0c5ef6d54
child 2895 9328f53a0ca2
permissions -rw-r--r--
Add a few test pattern lines
slouken@2887
     1
slouken@2887
     2
/* Simple program:  draw as many random lines on the screen as possible */
slouken@2887
     3
slouken@2887
     4
#include <stdlib.h>
slouken@2887
     5
#include <stdio.h>
slouken@2887
     6
#include <time.h>
slouken@2887
     7
slouken@2887
     8
#include "common.h"
slouken@2887
     9
slouken@2887
    10
#define NUM_LINES	100
slouken@2887
    11
slouken@2887
    12
static CommonState *state;
slouken@2887
    13
static int num_lines;
slouken@2887
    14
static SDL_bool cycle_color;
slouken@2887
    15
static SDL_bool cycle_alpha;
slouken@2887
    16
static int cycle_direction = 1;
slouken@2887
    17
static int current_alpha = 255;
slouken@2887
    18
static int current_color = 255;
slouken@2887
    19
static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
slouken@2887
    20
slouken@2887
    21
void
slouken@2887
    22
DrawLines(SDL_WindowID window)
slouken@2887
    23
{
slouken@2887
    24
    int i, n;
slouken@2887
    25
    int x1, y1, x2, y2;
slouken@2887
    26
    int window_w, window_h;
slouken@2887
    27
slouken@2887
    28
    SDL_SelectRenderer(window);
slouken@2887
    29
slouken@2887
    30
    /* Query the sizes */
slouken@2887
    31
    SDL_GetWindowSize(window, &window_w, &window_h);
slouken@2887
    32
slouken@2887
    33
    /* Move the sprite, bounce at the wall, and draw */
slouken@2887
    34
    n = 0;
slouken@2887
    35
    SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
slouken@2887
    36
    SDL_RenderFill(NULL);
slouken@2887
    37
slouken@2887
    38
    SDL_SetRenderDrawBlendMode(blendMode);
slouken@2887
    39
    for (i = 0; i < num_lines; ++i) {
slouken@2887
    40
        /* Cycle the color and alpha, if desired */
slouken@2887
    41
        if (cycle_color) {
slouken@2887
    42
            current_color += cycle_direction;
slouken@2887
    43
            if (current_color < 0) {
slouken@2887
    44
                current_color = 0;
slouken@2887
    45
                cycle_direction = -cycle_direction;
slouken@2887
    46
            }
slouken@2887
    47
            if (current_color > 255) {
slouken@2887
    48
                current_color = 255;
slouken@2887
    49
                cycle_direction = -cycle_direction;
slouken@2887
    50
            }
slouken@2887
    51
        }
slouken@2887
    52
        if (cycle_alpha) {
slouken@2887
    53
            current_alpha += cycle_direction;
slouken@2887
    54
            if (current_alpha < 0) {
slouken@2887
    55
                current_alpha = 0;
slouken@2887
    56
                cycle_direction = -cycle_direction;
slouken@2887
    57
            }
slouken@2887
    58
            if (current_alpha > 255) {
slouken@2887
    59
                current_alpha = 255;
slouken@2887
    60
                cycle_direction = -cycle_direction;
slouken@2887
    61
            }
slouken@2887
    62
        }
slouken@2887
    63
        SDL_SetRenderDrawColor(255, (Uint8) current_color,
slouken@2887
    64
                               (Uint8) current_color, (Uint8) current_alpha);
slouken@2887
    65
slouken@2892
    66
        if (i == 0) {
slouken@2892
    67
            SDL_RenderLine(0, 0, window_w, window_h);
slouken@2892
    68
            SDL_RenderLine(0, window_h, window_w, 0);
slouken@2892
    69
            SDL_RenderLine(0, window_h/2, window_w, window_h/2);
slouken@2892
    70
            SDL_RenderLine(window_w/2, 0, window_w/2, window_h);
slouken@2892
    71
        } else {
slouken@2892
    72
            x1 = rand() % window_w;
slouken@2892
    73
            x2 = rand() % window_w;
slouken@2892
    74
            y1 = rand() % window_h;
slouken@2892
    75
            y2 = rand() % window_h;
slouken@2892
    76
            SDL_RenderLine(x1, y1, x2, y2);
slouken@2892
    77
        }
slouken@2887
    78
    }
slouken@2887
    79
    SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
slouken@2887
    80
slouken@2887
    81
    /* Update the screen! */
slouken@2887
    82
    SDL_RenderPresent();
slouken@2887
    83
}
slouken@2887
    84
slouken@2887
    85
int
slouken@2887
    86
main(int argc, char *argv[])
slouken@2887
    87
{
slouken@2887
    88
    int i, done;
slouken@2887
    89
    SDL_Event event;
slouken@2887
    90
    Uint32 then, now, frames;
slouken@2887
    91
slouken@2887
    92
    /* Initialize parameters */
slouken@2887
    93
    num_lines = NUM_LINES;
slouken@2887
    94
slouken@2887
    95
    /* Initialize test framework */
slouken@2887
    96
    state = CommonCreateState(argv, SDL_INIT_VIDEO);
slouken@2887
    97
    if (!state) {
slouken@2887
    98
        return 1;
slouken@2887
    99
    }
slouken@2887
   100
    for (i = 1; i < argc;) {
slouken@2887
   101
        int consumed;
slouken@2887
   102
slouken@2887
   103
        consumed = CommonArg(state, i);
slouken@2887
   104
        if (consumed == 0) {
slouken@2887
   105
            consumed = -1;
slouken@2887
   106
            if (SDL_strcasecmp(argv[i], "--blend") == 0) {
slouken@2887
   107
                if (argv[i + 1]) {
slouken@2887
   108
                    if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
slouken@2887
   109
                        blendMode = SDL_BLENDMODE_NONE;
slouken@2887
   110
                        consumed = 2;
slouken@2887
   111
                    } else if (SDL_strcasecmp(argv[i + 1], "mask") == 0) {
slouken@2887
   112
                        blendMode = SDL_BLENDMODE_MASK;
slouken@2887
   113
                        consumed = 2;
slouken@2887
   114
                    } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
slouken@2887
   115
                        blendMode = SDL_BLENDMODE_BLEND;
slouken@2887
   116
                        consumed = 2;
slouken@2887
   117
                    } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
slouken@2887
   118
                        blendMode = SDL_BLENDMODE_ADD;
slouken@2887
   119
                        consumed = 2;
slouken@2887
   120
                    } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
slouken@2887
   121
                        blendMode = SDL_BLENDMODE_MOD;
slouken@2887
   122
                        consumed = 2;
slouken@2887
   123
                    }
slouken@2887
   124
                }
slouken@2887
   125
            } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
slouken@2887
   126
                cycle_color = SDL_TRUE;
slouken@2887
   127
                consumed = 1;
slouken@2887
   128
            } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
slouken@2887
   129
                cycle_alpha = SDL_TRUE;
slouken@2887
   130
                consumed = 1;
slouken@2887
   131
            } else if (SDL_isdigit(*argv[i])) {
slouken@2887
   132
                num_lines = SDL_atoi(argv[i]);
slouken@2887
   133
                consumed = 1;
slouken@2887
   134
            }
slouken@2887
   135
        }
slouken@2887
   136
        if (consumed < 0) {
slouken@2887
   137
            fprintf(stderr,
slouken@2887
   138
                    "Usage: %s %s [--blend none|mask|blend|add|mod] [--cyclecolor] [--cyclealpha]\n",
slouken@2887
   139
                    argv[0], CommonUsage(state));
slouken@2887
   140
            return 1;
slouken@2887
   141
        }
slouken@2887
   142
        i += consumed;
slouken@2887
   143
    }
slouken@2887
   144
    if (!CommonInit(state)) {
slouken@2887
   145
        return 2;
slouken@2887
   146
    }
slouken@2887
   147
slouken@2887
   148
    /* Create the windows and initialize the renderers */
slouken@2887
   149
    for (i = 0; i < state->num_windows; ++i) {
slouken@2887
   150
        SDL_SelectRenderer(state->windows[i]);
slouken@2887
   151
        SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
slouken@2887
   152
        SDL_RenderFill(NULL);
slouken@2887
   153
    }
slouken@2887
   154
slouken@2887
   155
    srand(time(NULL));
slouken@2887
   156
slouken@2887
   157
    /* Main render loop */
slouken@2887
   158
    frames = 0;
slouken@2887
   159
    then = SDL_GetTicks();
slouken@2887
   160
    done = 0;
slouken@2887
   161
    while (!done) {
slouken@2887
   162
        /* Check for events */
slouken@2887
   163
        ++frames;
slouken@2887
   164
        while (SDL_PollEvent(&event)) {
slouken@2887
   165
            CommonEvent(state, &event, &done);
slouken@2887
   166
            switch (event.type) {
slouken@2887
   167
            case SDL_WINDOWEVENT:
slouken@2887
   168
                switch (event.window.event) {
slouken@2887
   169
                case SDL_WINDOWEVENT_EXPOSED:
slouken@2887
   170
                    SDL_SelectRenderer(event.window.windowID);
slouken@2887
   171
                    SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
slouken@2887
   172
                    SDL_RenderFill(NULL);
slouken@2887
   173
                    break;
slouken@2887
   174
                }
slouken@2887
   175
                break;
slouken@2887
   176
            default:
slouken@2887
   177
                break;
slouken@2887
   178
            }
slouken@2887
   179
        }
slouken@2887
   180
        for (i = 0; i < state->num_windows; ++i) {
slouken@2887
   181
            DrawLines(state->windows[i]);
slouken@2887
   182
        }
slouken@2887
   183
    }
slouken@2887
   184
slouken@2887
   185
    /* Print out some timing information */
slouken@2887
   186
    now = SDL_GetTicks();
slouken@2887
   187
    if (now > then) {
slouken@2887
   188
        double fps = ((double) frames * 1000) / (now - then);
slouken@2887
   189
        printf("%2.2f frames per second\n", fps);
slouken@2887
   190
    }
slouken@2887
   191
    return 0;
slouken@2887
   192
}
slouken@2887
   193
slouken@2887
   194
/* vi: set ts=4 sw=4 expandtab: */