test/testdraw2.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Wed, 11 Mar 2015 21:14:21 +0100
changeset 9380 07b7c1005a23
parent 9278 8900afb78a19
child 9607 7746ab058d12
permissions -rw-r--r--
Updated internal documentation comments.
     1 /*
     2   Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
     3 
     4   This software is provided 'as-is', without any express or implied
     5   warranty.  In no event will the authors be held liable for any damages
     6   arising from the use of this software.
     7 
     8   Permission is granted to anyone to use this software for any purpose,
     9   including commercial applications, and to alter it and redistribute it
    10   freely.
    11 */
    12 
    13 /* Simple program:  draw as many random objects on the screen as possible */
    14 
    15 #include <stdlib.h>
    16 #include <stdio.h>
    17 #include <time.h>
    18 
    19 #ifdef __EMSCRIPTEN__
    20 #include <emscripten/emscripten.h>
    21 #endif
    22 
    23 #include "SDL_test_common.h"
    24 
    25 #define NUM_OBJECTS 100
    26 
    27 static SDLTest_CommonState *state;
    28 static int num_objects;
    29 static SDL_bool cycle_color;
    30 static SDL_bool cycle_alpha;
    31 static int cycle_direction = 1;
    32 static int current_alpha = 255;
    33 static int current_color = 255;
    34 static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
    35 
    36 int done;
    37 
    38 void
    39 DrawPoints(SDL_Renderer * renderer)
    40 {
    41     int i;
    42     int x, y;
    43     SDL_Rect viewport;
    44 
    45     /* Query the sizes */
    46     SDL_RenderGetViewport(renderer, &viewport);
    47 
    48     for (i = 0; i < num_objects * 4; ++i) {
    49         /* Cycle the color and alpha, if desired */
    50         if (cycle_color) {
    51             current_color += cycle_direction;
    52             if (current_color < 0) {
    53                 current_color = 0;
    54                 cycle_direction = -cycle_direction;
    55             }
    56             if (current_color > 255) {
    57                 current_color = 255;
    58                 cycle_direction = -cycle_direction;
    59             }
    60         }
    61         if (cycle_alpha) {
    62             current_alpha += cycle_direction;
    63             if (current_alpha < 0) {
    64                 current_alpha = 0;
    65                 cycle_direction = -cycle_direction;
    66             }
    67             if (current_alpha > 255) {
    68                 current_alpha = 255;
    69                 cycle_direction = -cycle_direction;
    70             }
    71         }
    72         SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color,
    73                                (Uint8) current_color, (Uint8) current_alpha);
    74 
    75         x = rand() % viewport.w;
    76         y = rand() % viewport.h;
    77         SDL_RenderDrawPoint(renderer, x, y);
    78     }
    79 }
    80 
    81 void
    82 DrawLines(SDL_Renderer * renderer)
    83 {
    84     int i;
    85     int x1, y1, x2, y2;
    86     SDL_Rect viewport;
    87 
    88     /* Query the sizes */
    89     SDL_RenderGetViewport(renderer, &viewport);
    90 
    91     for (i = 0; i < num_objects; ++i) {
    92         /* Cycle the color and alpha, if desired */
    93         if (cycle_color) {
    94             current_color += cycle_direction;
    95             if (current_color < 0) {
    96                 current_color = 0;
    97                 cycle_direction = -cycle_direction;
    98             }
    99             if (current_color > 255) {
   100                 current_color = 255;
   101                 cycle_direction = -cycle_direction;
   102             }
   103         }
   104         if (cycle_alpha) {
   105             current_alpha += cycle_direction;
   106             if (current_alpha < 0) {
   107                 current_alpha = 0;
   108                 cycle_direction = -cycle_direction;
   109             }
   110             if (current_alpha > 255) {
   111                 current_alpha = 255;
   112                 cycle_direction = -cycle_direction;
   113             }
   114         }
   115         SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color,
   116                                (Uint8) current_color, (Uint8) current_alpha);
   117 
   118         if (i == 0) {
   119             SDL_RenderDrawLine(renderer, 0, 0, viewport.w - 1, viewport.h - 1);
   120             SDL_RenderDrawLine(renderer, 0, viewport.h - 1, viewport.w - 1, 0);
   121             SDL_RenderDrawLine(renderer, 0, viewport.h / 2, viewport.w - 1, viewport.h / 2);
   122             SDL_RenderDrawLine(renderer, viewport.w / 2, 0, viewport.w / 2, viewport.h - 1);
   123         } else {
   124             x1 = (rand() % (viewport.w*2)) - viewport.w;
   125             x2 = (rand() % (viewport.w*2)) - viewport.w;
   126             y1 = (rand() % (viewport.h*2)) - viewport.h;
   127             y2 = (rand() % (viewport.h*2)) - viewport.h;
   128             SDL_RenderDrawLine(renderer, x1, y1, x2, y2);
   129         }
   130     }
   131 }
   132 
   133 void
   134 DrawRects(SDL_Renderer * renderer)
   135 {
   136     int i;
   137     SDL_Rect rect;
   138     SDL_Rect viewport;
   139 
   140     /* Query the sizes */
   141     SDL_RenderGetViewport(renderer, &viewport);
   142 
   143     for (i = 0; i < num_objects / 4; ++i) {
   144         /* Cycle the color and alpha, if desired */
   145         if (cycle_color) {
   146             current_color += cycle_direction;
   147             if (current_color < 0) {
   148                 current_color = 0;
   149                 cycle_direction = -cycle_direction;
   150             }
   151             if (current_color > 255) {
   152                 current_color = 255;
   153                 cycle_direction = -cycle_direction;
   154             }
   155         }
   156         if (cycle_alpha) {
   157             current_alpha += cycle_direction;
   158             if (current_alpha < 0) {
   159                 current_alpha = 0;
   160                 cycle_direction = -cycle_direction;
   161             }
   162             if (current_alpha > 255) {
   163                 current_alpha = 255;
   164                 cycle_direction = -cycle_direction;
   165             }
   166         }
   167         SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color,
   168                                (Uint8) current_color, (Uint8) current_alpha);
   169 
   170         rect.w = rand() % (viewport.h / 2);
   171         rect.h = rand() % (viewport.h / 2);
   172         rect.x = (rand() % (viewport.w*2) - viewport.w) - (rect.w / 2);
   173         rect.y = (rand() % (viewport.h*2) - viewport.h) - (rect.h / 2);
   174         SDL_RenderFillRect(renderer, &rect);
   175     }
   176 }
   177 
   178 void
   179 loop()
   180 {
   181     int i;
   182     SDL_Event event;
   183 
   184     /* Check for events */
   185     while (SDL_PollEvent(&event)) {
   186         SDLTest_CommonEvent(state, &event, &done);
   187     }
   188     for (i = 0; i < state->num_windows; ++i) {
   189         SDL_Renderer *renderer = state->renderers[i];
   190         if (state->windows[i] == NULL)
   191             continue;
   192         SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
   193         SDL_RenderClear(renderer);
   194 
   195         DrawRects(renderer);
   196         DrawLines(renderer);
   197         DrawPoints(renderer);
   198 
   199         SDL_RenderPresent(renderer);
   200     }
   201 }
   202 
   203 int
   204 main(int argc, char *argv[])
   205 {
   206     int i;
   207     Uint32 then, now, frames;
   208 
   209 	/* Enable standard application logging */
   210 	SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
   211 
   212     /* Initialize parameters */
   213     num_objects = NUM_OBJECTS;
   214 
   215     /* Initialize test framework */
   216     state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
   217     if (!state) {
   218         return 1;
   219     }
   220     for (i = 1; i < argc;) {
   221         int consumed;
   222 
   223         consumed = SDLTest_CommonArg(state, i);
   224         if (consumed == 0) {
   225             consumed = -1;
   226             if (SDL_strcasecmp(argv[i], "--blend") == 0) {
   227                 if (argv[i + 1]) {
   228                     if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
   229                         blendMode = SDL_BLENDMODE_NONE;
   230                         consumed = 2;
   231                     } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
   232                         blendMode = SDL_BLENDMODE_BLEND;
   233                         consumed = 2;
   234                     } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
   235                         blendMode = SDL_BLENDMODE_ADD;
   236                         consumed = 2;
   237                     } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
   238                         blendMode = SDL_BLENDMODE_MOD;
   239                         consumed = 2;
   240                     }
   241                 }
   242             } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
   243                 cycle_color = SDL_TRUE;
   244                 consumed = 1;
   245             } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
   246                 cycle_alpha = SDL_TRUE;
   247                 consumed = 1;
   248             } else if (SDL_isdigit(*argv[i])) {
   249                 num_objects = SDL_atoi(argv[i]);
   250                 consumed = 1;
   251             }
   252         }
   253         if (consumed < 0) {
   254             SDL_Log("Usage: %s %s [--blend none|blend|add|mod] [--cyclecolor] [--cyclealpha]\n",
   255                     argv[0], SDLTest_CommonUsage(state));
   256             return 1;
   257         }
   258         i += consumed;
   259     }
   260     if (!SDLTest_CommonInit(state)) {
   261         return 2;
   262     }
   263 
   264     /* Create the windows and initialize the renderers */
   265     for (i = 0; i < state->num_windows; ++i) {
   266         SDL_Renderer *renderer = state->renderers[i];
   267         SDL_SetRenderDrawBlendMode(renderer, blendMode);
   268         SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
   269         SDL_RenderClear(renderer);
   270     }
   271 
   272     srand((unsigned int)time(NULL));
   273 
   274     /* Main render loop */
   275     frames = 0;
   276     then = SDL_GetTicks();
   277     done = 0;
   278 
   279 #ifdef __EMSCRIPTEN__
   280     emscripten_set_main_loop(loop, 0, 1);
   281 #else
   282     while (!done) {
   283         ++frames;
   284         loop();
   285         }
   286 #endif
   287 
   288 
   289     SDLTest_CommonQuit(state);
   290 
   291     /* Print out some timing information */
   292     now = SDL_GetTicks();
   293     if (now > then) {
   294         double fps = ((double) frames * 1000) / (now - then);
   295         SDL_Log("%2.2f frames per second\n", fps);
   296     }
   297     return 0;
   298 }
   299 
   300 /* vi: set ts=4 sw=4 expandtab: */