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