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