Added a test program for line drawing
authorSam Lantinga <slouken@libsdl.org>
Sat, 20 Dec 2008 12:32:53 +0000
changeset 2887f8c0c5ef6d54
parent 2886 633ce79b7a5b
child 2888 32e8bbba1e94
Added a test program for line drawing
test/Makefile.in
test/testlines2.c
     1.1 --- a/test/Makefile.in	Sat Dec 20 12:32:38 2008 +0000
     1.2 +++ b/test/Makefile.in	Sat Dec 20 12:32:53 2008 +0000
     1.3 @@ -7,7 +7,7 @@
     1.4  CFLAGS  = @CFLAGS@
     1.5  LIBS	= @LIBS@
     1.6  
     1.7 -TARGETS = checkkeys$(EXE) graywin$(EXE) loopwave$(EXE) testaudioinfo$(EXE) testmultiaudio$(EXE) testalpha$(EXE) testbitmap$(EXE) testblitspeed$(EXE) testcdrom$(EXE) testcursor$(EXE) testdyngl$(EXE) testerror$(EXE) testfile$(EXE) testgamma$(EXE) testgl$(EXE) testgl2$(EXE) testhread$(EXE) testiconv$(EXE) testjoystick$(EXE) testkeys$(EXE) testlock$(EXE) testoverlay2$(EXE) testoverlay$(EXE) testpalette$(EXE) testplatform$(EXE) testsem$(EXE) testsprite$(EXE) testsprite2$(EXE) testtimer$(EXE) testver$(EXE) testvidinfo$(EXE) testwin$(EXE) testwm$(EXE) testwm2$(EXE) threadwin$(EXE) torturethread$(EXE) testloadso$(EXE) testhaptic$(EXE) testmmousetablet$(EXE) 
     1.8 +TARGETS = checkkeys$(EXE) graywin$(EXE) loopwave$(EXE) testaudioinfo$(EXE) testmultiaudio$(EXE) testalpha$(EXE) testbitmap$(EXE) testblitspeed$(EXE) testcdrom$(EXE) testcursor$(EXE) testdyngl$(EXE) testerror$(EXE) testfile$(EXE) testgamma$(EXE) testgl$(EXE) testgl2$(EXE) testhread$(EXE) testiconv$(EXE) testjoystick$(EXE) testkeys$(EXE) testlines2$(EXE) testlock$(EXE) testoverlay2$(EXE) testoverlay$(EXE) testpalette$(EXE) testplatform$(EXE) testsem$(EXE) testsprite$(EXE) testsprite2$(EXE) testtimer$(EXE) testver$(EXE) testvidinfo$(EXE) testwin$(EXE) testwm$(EXE) testwm2$(EXE) threadwin$(EXE) torturethread$(EXE) testloadso$(EXE) testhaptic$(EXE) testmmousetablet$(EXE) 
     1.9  
    1.10  all: Makefile $(TARGETS)
    1.11  
    1.12 @@ -74,6 +74,9 @@
    1.13  testkeys$(EXE): $(srcdir)/testkeys.c
    1.14  	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
    1.15  
    1.16 +testlines2$(EXE): $(srcdir)/testlines2.c $(srcdir)/common.c
    1.17 +	$(CC) -o $@ $(srcdir)/testlines2.c $(srcdir)/common.c $(CFLAGS) $(LIBS)
    1.18 +
    1.19  testlock$(EXE): $(srcdir)/testlock.c
    1.20  	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
    1.21  
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/testlines2.c	Sat Dec 20 12:32:53 2008 +0000
     2.3 @@ -0,0 +1,187 @@
     2.4 +
     2.5 +/* Simple program:  draw as many random lines on the screen as possible */
     2.6 +
     2.7 +#include <stdlib.h>
     2.8 +#include <stdio.h>
     2.9 +#include <time.h>
    2.10 +
    2.11 +#include "common.h"
    2.12 +
    2.13 +#define NUM_LINES	100
    2.14 +
    2.15 +static CommonState *state;
    2.16 +static int num_lines;
    2.17 +static SDL_bool cycle_color;
    2.18 +static SDL_bool cycle_alpha;
    2.19 +static int cycle_direction = 1;
    2.20 +static int current_alpha = 255;
    2.21 +static int current_color = 255;
    2.22 +static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
    2.23 +
    2.24 +void
    2.25 +DrawLines(SDL_WindowID window)
    2.26 +{
    2.27 +    int i, n;
    2.28 +    int x1, y1, x2, y2;
    2.29 +    int window_w, window_h;
    2.30 +
    2.31 +    SDL_SelectRenderer(window);
    2.32 +
    2.33 +    /* Query the sizes */
    2.34 +    SDL_GetWindowSize(window, &window_w, &window_h);
    2.35 +
    2.36 +    /* Move the sprite, bounce at the wall, and draw */
    2.37 +    n = 0;
    2.38 +    SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
    2.39 +    SDL_RenderFill(NULL);
    2.40 +
    2.41 +    SDL_SetRenderDrawBlendMode(blendMode);
    2.42 +    for (i = 0; i < num_lines; ++i) {
    2.43 +        /* Cycle the color and alpha, if desired */
    2.44 +        if (cycle_color) {
    2.45 +            current_color += cycle_direction;
    2.46 +            if (current_color < 0) {
    2.47 +                current_color = 0;
    2.48 +                cycle_direction = -cycle_direction;
    2.49 +            }
    2.50 +            if (current_color > 255) {
    2.51 +                current_color = 255;
    2.52 +                cycle_direction = -cycle_direction;
    2.53 +            }
    2.54 +        }
    2.55 +        if (cycle_alpha) {
    2.56 +            current_alpha += cycle_direction;
    2.57 +            if (current_alpha < 0) {
    2.58 +                current_alpha = 0;
    2.59 +                cycle_direction = -cycle_direction;
    2.60 +            }
    2.61 +            if (current_alpha > 255) {
    2.62 +                current_alpha = 255;
    2.63 +                cycle_direction = -cycle_direction;
    2.64 +            }
    2.65 +        }
    2.66 +        SDL_SetRenderDrawColor(255, (Uint8) current_color,
    2.67 +                               (Uint8) current_color, (Uint8) current_alpha);
    2.68 +
    2.69 +        x1 = rand() % window_w;
    2.70 +        x2 = rand() % window_w;
    2.71 +        y1 = rand() % window_h;
    2.72 +        y2 = rand() % window_h;
    2.73 +        SDL_RenderLine(x1, y1, x2, y2);
    2.74 +    }
    2.75 +    SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
    2.76 +
    2.77 +    /* Update the screen! */
    2.78 +    SDL_RenderPresent();
    2.79 +}
    2.80 +
    2.81 +int
    2.82 +main(int argc, char *argv[])
    2.83 +{
    2.84 +    int i, done;
    2.85 +    SDL_Event event;
    2.86 +    Uint32 then, now, frames;
    2.87 +
    2.88 +    /* Initialize parameters */
    2.89 +    num_lines = NUM_LINES;
    2.90 +
    2.91 +    /* Initialize test framework */
    2.92 +    state = CommonCreateState(argv, SDL_INIT_VIDEO);
    2.93 +    if (!state) {
    2.94 +        return 1;
    2.95 +    }
    2.96 +    for (i = 1; i < argc;) {
    2.97 +        int consumed;
    2.98 +
    2.99 +        consumed = CommonArg(state, i);
   2.100 +        if (consumed == 0) {
   2.101 +            consumed = -1;
   2.102 +            if (SDL_strcasecmp(argv[i], "--blend") == 0) {
   2.103 +                if (argv[i + 1]) {
   2.104 +                    if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
   2.105 +                        blendMode = SDL_BLENDMODE_NONE;
   2.106 +                        consumed = 2;
   2.107 +                    } else if (SDL_strcasecmp(argv[i + 1], "mask") == 0) {
   2.108 +                        blendMode = SDL_BLENDMODE_MASK;
   2.109 +                        consumed = 2;
   2.110 +                    } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
   2.111 +                        blendMode = SDL_BLENDMODE_BLEND;
   2.112 +                        consumed = 2;
   2.113 +                    } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
   2.114 +                        blendMode = SDL_BLENDMODE_ADD;
   2.115 +                        consumed = 2;
   2.116 +                    } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
   2.117 +                        blendMode = SDL_BLENDMODE_MOD;
   2.118 +                        consumed = 2;
   2.119 +                    }
   2.120 +                }
   2.121 +            } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
   2.122 +                cycle_color = SDL_TRUE;
   2.123 +                consumed = 1;
   2.124 +            } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
   2.125 +                cycle_alpha = SDL_TRUE;
   2.126 +                consumed = 1;
   2.127 +            } else if (SDL_isdigit(*argv[i])) {
   2.128 +                num_lines = SDL_atoi(argv[i]);
   2.129 +                consumed = 1;
   2.130 +            }
   2.131 +        }
   2.132 +        if (consumed < 0) {
   2.133 +            fprintf(stderr,
   2.134 +                    "Usage: %s %s [--blend none|mask|blend|add|mod] [--cyclecolor] [--cyclealpha]\n",
   2.135 +                    argv[0], CommonUsage(state));
   2.136 +            return 1;
   2.137 +        }
   2.138 +        i += consumed;
   2.139 +    }
   2.140 +    if (!CommonInit(state)) {
   2.141 +        return 2;
   2.142 +    }
   2.143 +
   2.144 +    /* Create the windows and initialize the renderers */
   2.145 +    for (i = 0; i < state->num_windows; ++i) {
   2.146 +        SDL_SelectRenderer(state->windows[i]);
   2.147 +        SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
   2.148 +        SDL_RenderFill(NULL);
   2.149 +    }
   2.150 +
   2.151 +    srand(time(NULL));
   2.152 +
   2.153 +    /* Main render loop */
   2.154 +    frames = 0;
   2.155 +    then = SDL_GetTicks();
   2.156 +    done = 0;
   2.157 +    while (!done) {
   2.158 +        /* Check for events */
   2.159 +        ++frames;
   2.160 +        while (SDL_PollEvent(&event)) {
   2.161 +            CommonEvent(state, &event, &done);
   2.162 +            switch (event.type) {
   2.163 +            case SDL_WINDOWEVENT:
   2.164 +                switch (event.window.event) {
   2.165 +                case SDL_WINDOWEVENT_EXPOSED:
   2.166 +                    SDL_SelectRenderer(event.window.windowID);
   2.167 +                    SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
   2.168 +                    SDL_RenderFill(NULL);
   2.169 +                    break;
   2.170 +                }
   2.171 +                break;
   2.172 +            default:
   2.173 +                break;
   2.174 +            }
   2.175 +        }
   2.176 +        for (i = 0; i < state->num_windows; ++i) {
   2.177 +            DrawLines(state->windows[i]);
   2.178 +        }
   2.179 +    }
   2.180 +
   2.181 +    /* Print out some timing information */
   2.182 +    now = SDL_GetTicks();
   2.183 +    if (now > then) {
   2.184 +        double fps = ((double) frames * 1000) / (now - then);
   2.185 +        printf("%2.2f frames per second\n", fps);
   2.186 +    }
   2.187 +    return 0;
   2.188 +}
   2.189 +
   2.190 +/* vi: set ts=4 sw=4 expandtab: */