SDL

Форк
0
/
testdraw.c 
316 строк · 10.0 Кб
1
/*
2
  Copyright (C) 1997-2024 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 <SDL3/SDL.h>
16
#include <SDL3/SDL_main.h>
17
#include <SDL3/SDL_test.h>
18

19
#ifdef SDL_PLATFORM_EMSCRIPTEN
20
#include <emscripten/emscripten.h>
21
#endif
22

23
#define NUM_OBJECTS 100
24

25
static SDLTest_CommonState *state;
26
static int num_objects;
27
static SDL_bool cycle_color;
28
static SDL_bool cycle_alpha;
29
static int cycle_direction = 1;
30
static int current_alpha = 255;
31
static int current_color = 255;
32
static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
33
static Uint64 next_fps_check;
34
static Uint32 frames;
35
static const int fps_check_delay = 5000;
36

37
static int done;
38

39
static void DrawPoints(SDL_Renderer *renderer)
40
{
41
    int i;
42
    float x, y;
43
    SDL_Rect viewport;
44

45
    /* Query the sizes */
46
    SDL_GetRenderViewport(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 = (float)SDL_rand(viewport.w);
76
        y = (float)SDL_rand(viewport.h);
77
        SDL_RenderPoint(renderer, x, y);
78
    }
79
}
80

81
static void DrawLines(SDL_Renderer *renderer)
82
{
83
    int i;
84
    float x1, y1, x2, y2;
85
    SDL_Rect viewport;
86

87
    /* Query the sizes */
88
    SDL_GetRenderViewport(renderer, &viewport);
89

90
    for (i = 0; i < num_objects; ++i) {
91
        /* Cycle the color and alpha, if desired */
92
        if (cycle_color) {
93
            current_color += cycle_direction;
94
            if (current_color < 0) {
95
                current_color = 0;
96
                cycle_direction = -cycle_direction;
97
            }
98
            if (current_color > 255) {
99
                current_color = 255;
100
                cycle_direction = -cycle_direction;
101
            }
102
        }
103
        if (cycle_alpha) {
104
            current_alpha += cycle_direction;
105
            if (current_alpha < 0) {
106
                current_alpha = 0;
107
                cycle_direction = -cycle_direction;
108
            }
109
            if (current_alpha > 255) {
110
                current_alpha = 255;
111
                cycle_direction = -cycle_direction;
112
            }
113
        }
114
        SDL_SetRenderDrawColor(renderer, 255, (Uint8)current_color,
115
                               (Uint8)current_color, (Uint8)current_alpha);
116

117
        if (i == 0) {
118
            SDL_RenderLine(renderer, 0.0f, 0.0f, (float)(viewport.w - 1), (float)(viewport.h - 1));
119
            SDL_RenderLine(renderer, 0.0f, (float)(viewport.h - 1), (float)(viewport.w - 1), 0.0f);
120
            SDL_RenderLine(renderer, 0.0f, (float)(viewport.h / 2), (float)(viewport.w - 1), (float)(viewport.h / 2));
121
            SDL_RenderLine(renderer, (float)(viewport.w / 2), 0.0f, (float)(viewport.w / 2), (float)(viewport.h - 1));
122
        } else {
123
            x1 = (float)(SDL_rand(viewport.w * 2) - viewport.w);
124
            x2 = (float)(SDL_rand(viewport.w * 2) - viewport.w);
125
            y1 = (float)(SDL_rand(viewport.h * 2) - viewport.h);
126
            y2 = (float)(SDL_rand(viewport.h * 2) - viewport.h);
127
            SDL_RenderLine(renderer, x1, y1, x2, y2);
128
        }
129
    }
130
}
131

132
static void DrawRects(SDL_Renderer *renderer)
133
{
134
    int i;
135
    SDL_FRect rect;
136
    SDL_Rect viewport;
137

138
    /* Query the sizes */
139
    SDL_GetRenderViewport(renderer, &viewport);
140

141
    for (i = 0; i < num_objects / 4; ++i) {
142
        /* Cycle the color and alpha, if desired */
143
        if (cycle_color) {
144
            current_color += cycle_direction;
145
            if (current_color < 0) {
146
                current_color = 0;
147
                cycle_direction = -cycle_direction;
148
            }
149
            if (current_color > 255) {
150
                current_color = 255;
151
                cycle_direction = -cycle_direction;
152
            }
153
        }
154
        if (cycle_alpha) {
155
            current_alpha += cycle_direction;
156
            if (current_alpha < 0) {
157
                current_alpha = 0;
158
                cycle_direction = -cycle_direction;
159
            }
160
            if (current_alpha > 255) {
161
                current_alpha = 255;
162
                cycle_direction = -cycle_direction;
163
            }
164
        }
165
        SDL_SetRenderDrawColor(renderer, 255, (Uint8)current_color,
166
                               (Uint8)current_color, (Uint8)current_alpha);
167

168
        rect.w = (float)SDL_rand(viewport.h / 2);
169
        rect.h = (float)SDL_rand(viewport.h / 2);
170
        rect.x = (float)((SDL_rand(viewport.w * 2) - viewport.w) - (rect.w / 2));
171
        rect.y = (float)((SDL_rand(viewport.h * 2) - viewport.h) - (rect.h / 2));
172
        SDL_RenderFillRect(renderer, &rect);
173
    }
174
}
175

176
static void loop(void)
177
{
178
    Uint64 now;
179
    int i;
180
    SDL_Event event;
181

182
    /* Check for events */
183
    while (SDL_PollEvent(&event)) {
184
        SDLTest_CommonEvent(state, &event, &done);
185
    }
186
    for (i = 0; i < state->num_windows; ++i) {
187
        SDL_Renderer *renderer = state->renderers[i];
188
        if (state->windows[i] == NULL) {
189
            continue;
190
        }
191
        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
192
        SDL_RenderClear(renderer);
193

194
        DrawRects(renderer);
195
        DrawLines(renderer);
196
        DrawPoints(renderer);
197

198
        SDL_RenderPresent(renderer);
199
    }
200
#ifdef SDL_PLATFORM_EMSCRIPTEN
201
    if (done) {
202
        emscripten_cancel_main_loop();
203
    }
204
#endif
205
    frames++;
206
    now = SDL_GetTicks();
207
    if (now >= next_fps_check) {
208
        /* Print out some timing information */
209
        const Uint64 then = next_fps_check - fps_check_delay;
210
        const double fps = ((double)frames * 1000) / (now - then);
211
        SDL_Log("%2.2f frames per second\n", fps);
212
        next_fps_check = now + fps_check_delay;
213
        frames = 0;
214
    }
215
}
216

217
int main(int argc, char *argv[])
218
{
219
    int i;
220
    /* Initialize parameters */
221
    num_objects = NUM_OBJECTS;
222

223
    /* Initialize test framework */
224
    state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
225
    if (!state) {
226
        return 1;
227
    }
228

229
    /* Enable standard application logging */
230
    SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
231

232
    for (i = 1; i < argc;) {
233
        int consumed;
234

235
        consumed = SDLTest_CommonArg(state, i);
236
        if (consumed == 0) {
237
            consumed = -1;
238
            if (SDL_strcasecmp(argv[i], "--blend") == 0) {
239
                if (argv[i + 1]) {
240
                    if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
241
                        blendMode = SDL_BLENDMODE_NONE;
242
                        consumed = 2;
243
                    } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
244
                        blendMode = SDL_BLENDMODE_BLEND;
245
                        consumed = 2;
246
                    } else if (SDL_strcasecmp(argv[i + 1], "blend_premultiplied") == 0) {
247
                        blendMode = SDL_BLENDMODE_BLEND_PREMULTIPLIED;
248
                        consumed = 2;
249
                    } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
250
                        blendMode = SDL_BLENDMODE_ADD;
251
                        consumed = 2;
252
                    } else if (SDL_strcasecmp(argv[i + 1], "add_premultiplied") == 0) {
253
                        blendMode = SDL_BLENDMODE_ADD_PREMULTIPLIED;
254
                        consumed = 2;
255
                    } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
256
                        blendMode = SDL_BLENDMODE_MOD;
257
                        consumed = 2;
258
                    } else if (SDL_strcasecmp(argv[i + 1], "mul") == 0) {
259
                        blendMode = SDL_BLENDMODE_MUL;
260
                        consumed = 2;
261
                    }
262
                }
263
            } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
264
                cycle_color = SDL_TRUE;
265
                consumed = 1;
266
            } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
267
                cycle_alpha = SDL_TRUE;
268
                consumed = 1;
269
            } else if (SDL_isdigit(*argv[i])) {
270
                num_objects = SDL_atoi(argv[i]);
271
                consumed = 1;
272
            }
273
        }
274
        if (consumed < 0) {
275
            static const char *options[] = {
276
                "[--blend none|blend|blend_premultiplied|add|add_premultiplied|mod|mul]",
277
                "[--cyclecolor]",
278
                "[--cyclealpha]",
279
                "[num_objects]",
280
                NULL
281
            };
282
            SDLTest_CommonLogUsage(state, argv[0], options);
283
            return 1;
284
        }
285
        i += consumed;
286
    }
287
    if (!SDLTest_CommonInit(state)) {
288
        return 2;
289
    }
290

291
    /* Create the windows and initialize the renderers */
292
    for (i = 0; i < state->num_windows; ++i) {
293
        SDL_Renderer *renderer = state->renderers[i];
294
        SDL_SetRenderDrawBlendMode(renderer, blendMode);
295
        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
296
        SDL_RenderClear(renderer);
297
    }
298

299
    /* Main render loop */
300
    frames = 0;
301
    next_fps_check = SDL_GetTicks() + fps_check_delay;
302
    done = 0;
303

304
#ifdef SDL_PLATFORM_EMSCRIPTEN
305
    emscripten_set_main_loop(loop, 0, 1);
306
#else
307
    while (!done) {
308
        loop();
309
    }
310
#endif
311

312
    SDLTest_CleanupTextDrawing();
313
    SDLTest_CommonQuit(state);
314

315
    return 0;
316
}
317

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.