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