test/testlines2.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 20 Dec 2008 12:32:53 +0000
changeset 2887 f8c0c5ef6d54
child 2892 e4bafaa29c65
permissions -rw-r--r--
Added a test program for line drawing
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@2887
    66
        x1 = rand() % window_w;
slouken@2887
    67
        x2 = rand() % window_w;
slouken@2887
    68
        y1 = rand() % window_h;
slouken@2887
    69
        y2 = rand() % window_h;
slouken@2887
    70
        SDL_RenderLine(x1, y1, x2, y2);
slouken@2887
    71
    }
slouken@2887
    72
    SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
slouken@2887
    73
slouken@2887
    74
    /* Update the screen! */
slouken@2887
    75
    SDL_RenderPresent();
slouken@2887
    76
}
slouken@2887
    77
slouken@2887
    78
int
slouken@2887
    79
main(int argc, char *argv[])
slouken@2887
    80
{
slouken@2887
    81
    int i, done;
slouken@2887
    82
    SDL_Event event;
slouken@2887
    83
    Uint32 then, now, frames;
slouken@2887
    84
slouken@2887
    85
    /* Initialize parameters */
slouken@2887
    86
    num_lines = NUM_LINES;
slouken@2887
    87
slouken@2887
    88
    /* Initialize test framework */
slouken@2887
    89
    state = CommonCreateState(argv, SDL_INIT_VIDEO);
slouken@2887
    90
    if (!state) {
slouken@2887
    91
        return 1;
slouken@2887
    92
    }
slouken@2887
    93
    for (i = 1; i < argc;) {
slouken@2887
    94
        int consumed;
slouken@2887
    95
slouken@2887
    96
        consumed = CommonArg(state, i);
slouken@2887
    97
        if (consumed == 0) {
slouken@2887
    98
            consumed = -1;
slouken@2887
    99
            if (SDL_strcasecmp(argv[i], "--blend") == 0) {
slouken@2887
   100
                if (argv[i + 1]) {
slouken@2887
   101
                    if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
slouken@2887
   102
                        blendMode = SDL_BLENDMODE_NONE;
slouken@2887
   103
                        consumed = 2;
slouken@2887
   104
                    } else if (SDL_strcasecmp(argv[i + 1], "mask") == 0) {
slouken@2887
   105
                        blendMode = SDL_BLENDMODE_MASK;
slouken@2887
   106
                        consumed = 2;
slouken@2887
   107
                    } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
slouken@2887
   108
                        blendMode = SDL_BLENDMODE_BLEND;
slouken@2887
   109
                        consumed = 2;
slouken@2887
   110
                    } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
slouken@2887
   111
                        blendMode = SDL_BLENDMODE_ADD;
slouken@2887
   112
                        consumed = 2;
slouken@2887
   113
                    } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
slouken@2887
   114
                        blendMode = SDL_BLENDMODE_MOD;
slouken@2887
   115
                        consumed = 2;
slouken@2887
   116
                    }
slouken@2887
   117
                }
slouken@2887
   118
            } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
slouken@2887
   119
                cycle_color = SDL_TRUE;
slouken@2887
   120
                consumed = 1;
slouken@2887
   121
            } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
slouken@2887
   122
                cycle_alpha = SDL_TRUE;
slouken@2887
   123
                consumed = 1;
slouken@2887
   124
            } else if (SDL_isdigit(*argv[i])) {
slouken@2887
   125
                num_lines = SDL_atoi(argv[i]);
slouken@2887
   126
                consumed = 1;
slouken@2887
   127
            }
slouken@2887
   128
        }
slouken@2887
   129
        if (consumed < 0) {
slouken@2887
   130
            fprintf(stderr,
slouken@2887
   131
                    "Usage: %s %s [--blend none|mask|blend|add|mod] [--cyclecolor] [--cyclealpha]\n",
slouken@2887
   132
                    argv[0], CommonUsage(state));
slouken@2887
   133
            return 1;
slouken@2887
   134
        }
slouken@2887
   135
        i += consumed;
slouken@2887
   136
    }
slouken@2887
   137
    if (!CommonInit(state)) {
slouken@2887
   138
        return 2;
slouken@2887
   139
    }
slouken@2887
   140
slouken@2887
   141
    /* Create the windows and initialize the renderers */
slouken@2887
   142
    for (i = 0; i < state->num_windows; ++i) {
slouken@2887
   143
        SDL_SelectRenderer(state->windows[i]);
slouken@2887
   144
        SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
slouken@2887
   145
        SDL_RenderFill(NULL);
slouken@2887
   146
    }
slouken@2887
   147
slouken@2887
   148
    srand(time(NULL));
slouken@2887
   149
slouken@2887
   150
    /* Main render loop */
slouken@2887
   151
    frames = 0;
slouken@2887
   152
    then = SDL_GetTicks();
slouken@2887
   153
    done = 0;
slouken@2887
   154
    while (!done) {
slouken@2887
   155
        /* Check for events */
slouken@2887
   156
        ++frames;
slouken@2887
   157
        while (SDL_PollEvent(&event)) {
slouken@2887
   158
            CommonEvent(state, &event, &done);
slouken@2887
   159
            switch (event.type) {
slouken@2887
   160
            case SDL_WINDOWEVENT:
slouken@2887
   161
                switch (event.window.event) {
slouken@2887
   162
                case SDL_WINDOWEVENT_EXPOSED:
slouken@2887
   163
                    SDL_SelectRenderer(event.window.windowID);
slouken@2887
   164
                    SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
slouken@2887
   165
                    SDL_RenderFill(NULL);
slouken@2887
   166
                    break;
slouken@2887
   167
                }
slouken@2887
   168
                break;
slouken@2887
   169
            default:
slouken@2887
   170
                break;
slouken@2887
   171
            }
slouken@2887
   172
        }
slouken@2887
   173
        for (i = 0; i < state->num_windows; ++i) {
slouken@2887
   174
            DrawLines(state->windows[i]);
slouken@2887
   175
        }
slouken@2887
   176
    }
slouken@2887
   177
slouken@2887
   178
    /* Print out some timing information */
slouken@2887
   179
    now = SDL_GetTicks();
slouken@2887
   180
    if (now > then) {
slouken@2887
   181
        double fps = ((double) frames * 1000) / (now - then);
slouken@2887
   182
        printf("%2.2f frames per second\n", fps);
slouken@2887
   183
    }
slouken@2887
   184
    return 0;
slouken@2887
   185
}
slouken@2887
   186
slouken@2887
   187
/* vi: set ts=4 sw=4 expandtab: */