test/testdraw2.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 15 Feb 2011 13:59:59 -0800
changeset 5297 1800dc39b74c
parent 5184 d976b67150c5
child 5535 96594ac5fd1a
permissions -rw-r--r--
Changed the concept of a render clip rect to a render viewport.
The render viewport is automatically re-centered when the window changes size, so applications that don't care will not have to handle recalculating their rendering coordinates.

Fixed API for drawing and filling multiple rectangles - the parameter should be an array of rects, not an array of pointers to rects.

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