test/testlines2.c
changeset 2887 f8c0c5ef6d54
child 2892 e4bafaa29c65
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testlines2.c	Sat Dec 20 12:32:53 2008 +0000
     1.3 @@ -0,0 +1,187 @@
     1.4 +
     1.5 +/* Simple program:  draw as many random lines on the screen as possible */
     1.6 +
     1.7 +#include <stdlib.h>
     1.8 +#include <stdio.h>
     1.9 +#include <time.h>
    1.10 +
    1.11 +#include "common.h"
    1.12 +
    1.13 +#define NUM_LINES	100
    1.14 +
    1.15 +static CommonState *state;
    1.16 +static int num_lines;
    1.17 +static SDL_bool cycle_color;
    1.18 +static SDL_bool cycle_alpha;
    1.19 +static int cycle_direction = 1;
    1.20 +static int current_alpha = 255;
    1.21 +static int current_color = 255;
    1.22 +static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
    1.23 +
    1.24 +void
    1.25 +DrawLines(SDL_WindowID window)
    1.26 +{
    1.27 +    int i, n;
    1.28 +    int x1, y1, x2, y2;
    1.29 +    int window_w, window_h;
    1.30 +
    1.31 +    SDL_SelectRenderer(window);
    1.32 +
    1.33 +    /* Query the sizes */
    1.34 +    SDL_GetWindowSize(window, &window_w, &window_h);
    1.35 +
    1.36 +    /* Move the sprite, bounce at the wall, and draw */
    1.37 +    n = 0;
    1.38 +    SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
    1.39 +    SDL_RenderFill(NULL);
    1.40 +
    1.41 +    SDL_SetRenderDrawBlendMode(blendMode);
    1.42 +    for (i = 0; i < num_lines; ++i) {
    1.43 +        /* Cycle the color and alpha, if desired */
    1.44 +        if (cycle_color) {
    1.45 +            current_color += cycle_direction;
    1.46 +            if (current_color < 0) {
    1.47 +                current_color = 0;
    1.48 +                cycle_direction = -cycle_direction;
    1.49 +            }
    1.50 +            if (current_color > 255) {
    1.51 +                current_color = 255;
    1.52 +                cycle_direction = -cycle_direction;
    1.53 +            }
    1.54 +        }
    1.55 +        if (cycle_alpha) {
    1.56 +            current_alpha += cycle_direction;
    1.57 +            if (current_alpha < 0) {
    1.58 +                current_alpha = 0;
    1.59 +                cycle_direction = -cycle_direction;
    1.60 +            }
    1.61 +            if (current_alpha > 255) {
    1.62 +                current_alpha = 255;
    1.63 +                cycle_direction = -cycle_direction;
    1.64 +            }
    1.65 +        }
    1.66 +        SDL_SetRenderDrawColor(255, (Uint8) current_color,
    1.67 +                               (Uint8) current_color, (Uint8) current_alpha);
    1.68 +
    1.69 +        x1 = rand() % window_w;
    1.70 +        x2 = rand() % window_w;
    1.71 +        y1 = rand() % window_h;
    1.72 +        y2 = rand() % window_h;
    1.73 +        SDL_RenderLine(x1, y1, x2, y2);
    1.74 +    }
    1.75 +    SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
    1.76 +
    1.77 +    /* Update the screen! */
    1.78 +    SDL_RenderPresent();
    1.79 +}
    1.80 +
    1.81 +int
    1.82 +main(int argc, char *argv[])
    1.83 +{
    1.84 +    int i, done;
    1.85 +    SDL_Event event;
    1.86 +    Uint32 then, now, frames;
    1.87 +
    1.88 +    /* Initialize parameters */
    1.89 +    num_lines = NUM_LINES;
    1.90 +
    1.91 +    /* Initialize test framework */
    1.92 +    state = CommonCreateState(argv, SDL_INIT_VIDEO);
    1.93 +    if (!state) {
    1.94 +        return 1;
    1.95 +    }
    1.96 +    for (i = 1; i < argc;) {
    1.97 +        int consumed;
    1.98 +
    1.99 +        consumed = CommonArg(state, i);
   1.100 +        if (consumed == 0) {
   1.101 +            consumed = -1;
   1.102 +            if (SDL_strcasecmp(argv[i], "--blend") == 0) {
   1.103 +                if (argv[i + 1]) {
   1.104 +                    if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
   1.105 +                        blendMode = SDL_BLENDMODE_NONE;
   1.106 +                        consumed = 2;
   1.107 +                    } else if (SDL_strcasecmp(argv[i + 1], "mask") == 0) {
   1.108 +                        blendMode = SDL_BLENDMODE_MASK;
   1.109 +                        consumed = 2;
   1.110 +                    } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
   1.111 +                        blendMode = SDL_BLENDMODE_BLEND;
   1.112 +                        consumed = 2;
   1.113 +                    } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
   1.114 +                        blendMode = SDL_BLENDMODE_ADD;
   1.115 +                        consumed = 2;
   1.116 +                    } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
   1.117 +                        blendMode = SDL_BLENDMODE_MOD;
   1.118 +                        consumed = 2;
   1.119 +                    }
   1.120 +                }
   1.121 +            } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
   1.122 +                cycle_color = SDL_TRUE;
   1.123 +                consumed = 1;
   1.124 +            } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
   1.125 +                cycle_alpha = SDL_TRUE;
   1.126 +                consumed = 1;
   1.127 +            } else if (SDL_isdigit(*argv[i])) {
   1.128 +                num_lines = SDL_atoi(argv[i]);
   1.129 +                consumed = 1;
   1.130 +            }
   1.131 +        }
   1.132 +        if (consumed < 0) {
   1.133 +            fprintf(stderr,
   1.134 +                    "Usage: %s %s [--blend none|mask|blend|add|mod] [--cyclecolor] [--cyclealpha]\n",
   1.135 +                    argv[0], CommonUsage(state));
   1.136 +            return 1;
   1.137 +        }
   1.138 +        i += consumed;
   1.139 +    }
   1.140 +    if (!CommonInit(state)) {
   1.141 +        return 2;
   1.142 +    }
   1.143 +
   1.144 +    /* Create the windows and initialize the renderers */
   1.145 +    for (i = 0; i < state->num_windows; ++i) {
   1.146 +        SDL_SelectRenderer(state->windows[i]);
   1.147 +        SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
   1.148 +        SDL_RenderFill(NULL);
   1.149 +    }
   1.150 +
   1.151 +    srand(time(NULL));
   1.152 +
   1.153 +    /* Main render loop */
   1.154 +    frames = 0;
   1.155 +    then = SDL_GetTicks();
   1.156 +    done = 0;
   1.157 +    while (!done) {
   1.158 +        /* Check for events */
   1.159 +        ++frames;
   1.160 +        while (SDL_PollEvent(&event)) {
   1.161 +            CommonEvent(state, &event, &done);
   1.162 +            switch (event.type) {
   1.163 +            case SDL_WINDOWEVENT:
   1.164 +                switch (event.window.event) {
   1.165 +                case SDL_WINDOWEVENT_EXPOSED:
   1.166 +                    SDL_SelectRenderer(event.window.windowID);
   1.167 +                    SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
   1.168 +                    SDL_RenderFill(NULL);
   1.169 +                    break;
   1.170 +                }
   1.171 +                break;
   1.172 +            default:
   1.173 +                break;
   1.174 +            }
   1.175 +        }
   1.176 +        for (i = 0; i < state->num_windows; ++i) {
   1.177 +            DrawLines(state->windows[i]);
   1.178 +        }
   1.179 +    }
   1.180 +
   1.181 +    /* Print out some timing information */
   1.182 +    now = SDL_GetTicks();
   1.183 +    if (now > then) {
   1.184 +        double fps = ((double) frames * 1000) / (now - then);
   1.185 +        printf("%2.2f frames per second\n", fps);
   1.186 +    }
   1.187 +    return 0;
   1.188 +}
   1.189 +
   1.190 +/* vi: set ts=4 sw=4 expandtab: */