test/testdraw2.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 31 Jan 2011 23:23:57 -0800
changeset 5140 e743b9c3f6d6
parent 4884 27ab20a36eba
child 5147 ad50b3db78bd
permissions -rw-r--r--
Making the API simpler, the blend modes are "none, blend, add" and are supported by all renderers.
     1 
     2 /* Simple program:  draw as many random objects 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_OBJECTS	100
    11 
    12 static CommonState *state;
    13 static int num_objects;
    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 DrawPoints(SDL_Window * window)
    23 {
    24     int i;
    25     int x, y;
    26     int window_w, window_h;
    27 
    28     /* Query the sizes */
    29     SDL_GetWindowSize(window, &window_w, &window_h);
    30 
    31     SDL_SetRenderDrawBlendMode(blendMode);
    32     for (i = 0; i < num_objects * 4; ++i) {
    33         /* Cycle the color and alpha, if desired */
    34         if (cycle_color) {
    35             current_color += cycle_direction;
    36             if (current_color < 0) {
    37                 current_color = 0;
    38                 cycle_direction = -cycle_direction;
    39             }
    40             if (current_color > 255) {
    41                 current_color = 255;
    42                 cycle_direction = -cycle_direction;
    43             }
    44         }
    45         if (cycle_alpha) {
    46             current_alpha += cycle_direction;
    47             if (current_alpha < 0) {
    48                 current_alpha = 0;
    49                 cycle_direction = -cycle_direction;
    50             }
    51             if (current_alpha > 255) {
    52                 current_alpha = 255;
    53                 cycle_direction = -cycle_direction;
    54             }
    55         }
    56         SDL_SetRenderDrawColor(255, (Uint8) current_color,
    57                                (Uint8) current_color, (Uint8) current_alpha);
    58 
    59         x = rand() % window_w;
    60         y = rand() % window_h;
    61         SDL_RenderDrawPoint(x, y);
    62     }
    63     SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
    64 }
    65 
    66 void
    67 DrawLines(SDL_Window * window)
    68 {
    69     int i;
    70     int x1, y1, x2, y2;
    71     int window_w, window_h;
    72 
    73     /* Query the sizes */
    74     SDL_GetWindowSize(window, &window_w, &window_h);
    75 
    76     SDL_SetRenderDrawBlendMode(blendMode);
    77     for (i = 0; i < num_objects; ++i) {
    78         /* Cycle the color and alpha, if desired */
    79         if (cycle_color) {
    80             current_color += cycle_direction;
    81             if (current_color < 0) {
    82                 current_color = 0;
    83                 cycle_direction = -cycle_direction;
    84             }
    85             if (current_color > 255) {
    86                 current_color = 255;
    87                 cycle_direction = -cycle_direction;
    88             }
    89         }
    90         if (cycle_alpha) {
    91             current_alpha += cycle_direction;
    92             if (current_alpha < 0) {
    93                 current_alpha = 0;
    94                 cycle_direction = -cycle_direction;
    95             }
    96             if (current_alpha > 255) {
    97                 current_alpha = 255;
    98                 cycle_direction = -cycle_direction;
    99             }
   100         }
   101         SDL_SetRenderDrawColor(255, (Uint8) current_color,
   102                                (Uint8) current_color, (Uint8) current_alpha);
   103 
   104         if (i == 0) {
   105             SDL_RenderDrawLine(0, 0, window_w - 1, window_h - 1);
   106             SDL_RenderDrawLine(0, window_h - 1, window_w - 1, 0);
   107             SDL_RenderDrawLine(0, window_h / 2, window_w - 1, window_h / 2);
   108             SDL_RenderDrawLine(window_w / 2, 0, window_w / 2, window_h - 1);
   109         } else {
   110             x1 = (rand() % (window_w*2)) - window_w;
   111             x2 = (rand() % (window_w*2)) - window_w;
   112             y1 = (rand() % (window_h*2)) - window_h;
   113             y2 = (rand() % (window_h*2)) - window_h;
   114             SDL_RenderDrawLine(x1, y1, x2, y2);
   115         }
   116     }
   117     SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
   118 }
   119 
   120 void
   121 DrawRects(SDL_Window * window)
   122 {
   123     int i;
   124     SDL_Rect rect;
   125     int window_w, window_h;
   126 
   127     /* Query the sizes */
   128     SDL_GetWindowSize(window, &window_w, &window_h);
   129 
   130     SDL_SetRenderDrawBlendMode(blendMode);
   131     for (i = 0; i < num_objects / 4; ++i) {
   132         /* Cycle the color and alpha, if desired */
   133         if (cycle_color) {
   134             current_color += cycle_direction;
   135             if (current_color < 0) {
   136                 current_color = 0;
   137                 cycle_direction = -cycle_direction;
   138             }
   139             if (current_color > 255) {
   140                 current_color = 255;
   141                 cycle_direction = -cycle_direction;
   142             }
   143         }
   144         if (cycle_alpha) {
   145             current_alpha += cycle_direction;
   146             if (current_alpha < 0) {
   147                 current_alpha = 0;
   148                 cycle_direction = -cycle_direction;
   149             }
   150             if (current_alpha > 255) {
   151                 current_alpha = 255;
   152                 cycle_direction = -cycle_direction;
   153             }
   154         }
   155         SDL_SetRenderDrawColor(255, (Uint8) current_color,
   156                                (Uint8) current_color, (Uint8) current_alpha);
   157 
   158         rect.w = rand() % (window_h / 2);
   159         rect.h = rand() % (window_h / 2);
   160         rect.x = (rand() % (window_w*2) - window_w) - (rect.w / 2);
   161         rect.y = (rand() % (window_h*2) - window_h) - (rect.h / 2);
   162         SDL_RenderFillRect(&rect);
   163     }
   164     SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
   165 }
   166 
   167 int
   168 main(int argc, char *argv[])
   169 {
   170     int i, done;
   171     SDL_Event event;
   172     Uint32 then, now, frames;
   173 
   174     /* Initialize parameters */
   175     num_objects = NUM_OBJECTS;
   176 
   177     /* Initialize test framework */
   178     state = CommonCreateState(argv, SDL_INIT_VIDEO);
   179     if (!state) {
   180         return 1;
   181     }
   182     for (i = 1; i < argc;) {
   183         int consumed;
   184 
   185         consumed = CommonArg(state, i);
   186         if (consumed == 0) {
   187             consumed = -1;
   188             if (SDL_strcasecmp(argv[i], "--blend") == 0) {
   189                 if (argv[i + 1]) {
   190                     if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
   191                         blendMode = SDL_BLENDMODE_NONE;
   192                         consumed = 2;
   193                     } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
   194                         blendMode = SDL_BLENDMODE_BLEND;
   195                         consumed = 2;
   196                     } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
   197                         blendMode = SDL_BLENDMODE_ADD;
   198                         consumed = 2;
   199                     }
   200                 }
   201             } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
   202                 cycle_color = SDL_TRUE;
   203                 consumed = 1;
   204             } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
   205                 cycle_alpha = SDL_TRUE;
   206                 consumed = 1;
   207             } else if (SDL_isdigit(*argv[i])) {
   208                 num_objects = SDL_atoi(argv[i]);
   209                 consumed = 1;
   210             }
   211         }
   212         if (consumed < 0) {
   213             fprintf(stderr,
   214                     "Usage: %s %s [--blend none|blend|add] [--cyclecolor] [--cyclealpha]\n",
   215                     argv[0], CommonUsage(state));
   216             return 1;
   217         }
   218         i += consumed;
   219     }
   220     if (!CommonInit(state)) {
   221         return 2;
   222     }
   223 
   224     /* Create the windows and initialize the renderers */
   225     for (i = 0; i < state->num_windows; ++i) {
   226         SDL_SelectRenderer(state->windows[i]);
   227         SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
   228         SDL_RenderClear();
   229     }
   230 
   231     srand((unsigned int)time(NULL));
   232 
   233     /* Main render loop */
   234     frames = 0;
   235     then = SDL_GetTicks();
   236     done = 0;
   237     while (!done) {
   238         /* Check for events */
   239         ++frames;
   240         while (SDL_PollEvent(&event)) {
   241             CommonEvent(state, &event, &done);
   242             switch (event.type) {
   243             case SDL_WINDOWEVENT:
   244                 switch (event.window.event) {
   245                 case SDL_WINDOWEVENT_EXPOSED:
   246                     SDL_SelectRenderer(SDL_GetWindowFromID(event.window.windowID));
   247                     SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
   248                     SDL_RenderClear();
   249                     break;
   250                 }
   251                 break;
   252             default:
   253                 break;
   254             }
   255         }
   256         for (i = 0; i < state->num_windows; ++i) {
   257             SDL_SelectRenderer(state->windows[i]);
   258             SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
   259             SDL_RenderClear();
   260 
   261             DrawRects(state->windows[i]);
   262             DrawLines(state->windows[i]);
   263             DrawPoints(state->windows[i]);
   264 
   265             SDL_RenderPresent();
   266         }
   267     }
   268 
   269     CommonQuit(state);
   270 
   271     /* Print out some timing information */
   272     now = SDL_GetTicks();
   273     if (now > then) {
   274         double fps = ((double) frames * 1000) / (now - then);
   275         printf("%2.2f frames per second\n", fps);
   276     }
   277     return 0;
   278 }
   279 
   280 /* vi: set ts=4 sw=4 expandtab: */