SDL

Форк
0
/
testgeometry.c 
286 строк · 8.5 Кб
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 a RGB triangle, with texture  */
14

15
#include <stdlib.h>
16

17
#include "testutils.h"
18
#include <SDL3/SDL.h>
19
#include <SDL3/SDL_main.h>
20
#include <SDL3/SDL_test_common.h>
21

22
#ifdef SDL_PLATFORM_EMSCRIPTEN
23
#include <emscripten/emscripten.h>
24
#endif
25

26
static SDLTest_CommonState *state;
27
static SDL_bool use_texture = SDL_FALSE;
28
static SDL_Texture **sprites;
29
static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
30
static float angle = 0.0f;
31
static int sprite_w, sprite_h;
32
static int translate_cx = 0;
33
static int translate_cy = 0;
34

35
static int done;
36

37
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
38
static void
39
quit(int rc)
40
{
41
    SDL_free(sprites);
42
    SDLTest_CommonQuit(state);
43
    /* Let 'main()' return normally */
44
    if (rc != 0) {
45
        exit(rc);
46
    }
47
}
48

49
static int LoadSprite(const char *file)
50
{
51
    int i;
52

53
    for (i = 0; i < state->num_windows; ++i) {
54
        /* This does the SDL_LoadBMP step repeatedly, but that's OK for test code. */
55
        sprites[i] = LoadTexture(state->renderers[i], file, SDL_TRUE, &sprite_w, &sprite_h);
56
        if (!sprites[i]) {
57
            return -1;
58
        }
59
        if (SDL_SetTextureBlendMode(sprites[i], blendMode) < 0) {
60
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set blend mode: %s\n", SDL_GetError());
61
            SDL_DestroyTexture(sprites[i]);
62
            return -1;
63
        }
64
    }
65

66
    /* We're ready to roll. :) */
67
    return 0;
68
}
69

70
static void loop(void)
71
{
72
    int i;
73
    SDL_Event event;
74

75
    /* Check for events */
76
    while (SDL_PollEvent(&event)) {
77

78
        if (event.type == SDL_EVENT_MOUSE_MOTION) {
79
            if (event.motion.state) {
80
                float xrel, yrel;
81
                int window_w, window_h;
82
                SDL_Window *window = SDL_GetWindowFromID(event.motion.windowID);
83
                SDL_GetWindowSize(window, &window_w, &window_h);
84
                xrel = event.motion.xrel;
85
                yrel = event.motion.yrel;
86
                if (event.motion.y < (float)window_h / 2.0f) {
87
                    angle += xrel;
88
                } else {
89
                    angle -= xrel;
90
                }
91
                if (event.motion.x < (float)window_w / 2.0f) {
92
                    angle -= yrel;
93
                } else {
94
                    angle += yrel;
95
                }
96
            }
97
        } else if (event.type == SDL_EVENT_KEY_DOWN) {
98
            if (event.key.key == SDLK_LEFT) {
99
                translate_cx -= 1;
100
            } else if (event.key.key == SDLK_RIGHT) {
101
                translate_cx += 1;
102
            } else if (event.key.key == SDLK_UP) {
103
                translate_cy -= 1;
104
            } else if (event.key.key == SDLK_DOWN) {
105
                translate_cy += 1;
106
            } else {
107
                SDLTest_CommonEvent(state, &event, &done);
108
            }
109
        } else {
110
            SDLTest_CommonEvent(state, &event, &done);
111
        }
112
    }
113

114
    for (i = 0; i < state->num_windows; ++i) {
115
        SDL_Renderer *renderer = state->renderers[i];
116
        if (state->windows[i] == NULL) {
117
            continue;
118
        }
119
        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
120
        SDL_RenderClear(renderer);
121

122
        {
123
            SDL_Rect viewport;
124
            SDL_Vertex verts[3];
125
            float a;
126
            float d;
127
            int cx, cy;
128

129
            /* Query the sizes */
130
            SDL_GetRenderViewport(renderer, &viewport);
131
            SDL_zeroa(verts);
132
            cx = viewport.x + viewport.w / 2;
133
            cy = viewport.y + viewport.h / 2;
134
            d = (viewport.w + viewport.h) / 5.f;
135

136
            cx += translate_cx;
137
            cy += translate_cy;
138

139
            a = (angle * SDL_PI_F) / 180.0f;
140
            verts[0].position.x = cx + d * SDL_cosf(a);
141
            verts[0].position.y = cy + d * SDL_sinf(a);
142
            verts[0].color.r = 1.0f;
143
            verts[0].color.g = 0;
144
            verts[0].color.b = 0;
145
            verts[0].color.a = 1.0f;
146

147
            a = ((angle + 120) * SDL_PI_F) / 180.0f;
148
            verts[1].position.x = cx + d * SDL_cosf(a);
149
            verts[1].position.y = cy + d * SDL_sinf(a);
150
            verts[1].color.r = 0;
151
            verts[1].color.g = 1.0f;
152
            verts[1].color.b = 0;
153
            verts[1].color.a = 1.0f;
154

155
            a = ((angle + 240) * SDL_PI_F) / 180.0f;
156
            verts[2].position.x = cx + d * SDL_cosf(a);
157
            verts[2].position.y = cy + d * SDL_sinf(a);
158
            verts[2].color.r = 0;
159
            verts[2].color.g = 0;
160
            verts[2].color.b = 1.0f;
161
            verts[2].color.a = 1.0f;
162

163
            if (use_texture) {
164
                verts[0].tex_coord.x = 0.5f;
165
                verts[0].tex_coord.y = 0.0f;
166
                verts[1].tex_coord.x = 1.0f;
167
                verts[1].tex_coord.y = 1.0f;
168
                verts[2].tex_coord.x = 0.0f;
169
                verts[2].tex_coord.y = 1.0f;
170
            }
171

172
            SDL_RenderGeometry(renderer, sprites[i], verts, 3, NULL, 0);
173
        }
174

175
        SDL_RenderPresent(renderer);
176
    }
177
#ifdef SDL_PLATFORM_EMSCRIPTEN
178
    if (done) {
179
        emscripten_cancel_main_loop();
180
    }
181
#endif
182
}
183

184
int main(int argc, char *argv[])
185
{
186
    int i;
187
    const char *icon = "icon.bmp";
188
    Uint64 then, now;
189
    Uint32 frames;
190

191
    /* Initialize test framework */
192
    state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
193
    if (!state) {
194
        return 1;
195
    }
196

197
    /* Enable standard application logging */
198
    SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
199

200
    for (i = 1; i < argc;) {
201
        int consumed;
202

203
        consumed = SDLTest_CommonArg(state, i);
204
        if (consumed == 0) {
205
            consumed = -1;
206
            if (SDL_strcasecmp(argv[i], "--blend") == 0) {
207
                if (argv[i + 1]) {
208
                    if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
209
                        blendMode = SDL_BLENDMODE_NONE;
210
                        consumed = 2;
211
                    } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
212
                        blendMode = SDL_BLENDMODE_BLEND;
213
                        consumed = 2;
214
                    } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
215
                        blendMode = SDL_BLENDMODE_ADD;
216
                        consumed = 2;
217
                    } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
218
                        blendMode = SDL_BLENDMODE_MOD;
219
                        consumed = 2;
220
                    } else if (SDL_strcasecmp(argv[i + 1], "mul") == 0) {
221
                        blendMode = SDL_BLENDMODE_MUL;
222
                        consumed = 2;
223
                    }
224
                }
225
            } else if (SDL_strcasecmp(argv[i], "--use-texture") == 0) {
226
                use_texture = SDL_TRUE;
227
                consumed = 1;
228
            }
229
        }
230
        if (consumed < 0) {
231
            static const char *options[] = { "[--blend none|blend|add|mod|mul]", "[--use-texture]", NULL };
232
            SDLTest_CommonLogUsage(state, argv[0], options);
233
            return 1;
234
        }
235
        i += consumed;
236
    }
237
    if (!SDLTest_CommonInit(state)) {
238
        return 2;
239
    }
240

241
    /* Create the windows, initialize the renderers, and load the textures */
242
    sprites =
243
        (SDL_Texture **)SDL_malloc(state->num_windows * sizeof(*sprites));
244
    if (!sprites) {
245
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!\n");
246
        quit(2);
247
    }
248
    /* Create the windows and initialize the renderers */
249
    for (i = 0; i < state->num_windows; ++i) {
250
        SDL_Renderer *renderer = state->renderers[i];
251
        SDL_SetRenderDrawBlendMode(renderer, blendMode);
252
        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
253
        SDL_RenderClear(renderer);
254
        sprites[i] = NULL;
255
    }
256
    if (use_texture) {
257
        if (LoadSprite(icon) < 0) {
258
            quit(2);
259
        }
260
    }
261

262
    /* Main render loop */
263
    frames = 0;
264
    then = SDL_GetTicks();
265
    done = 0;
266

267
#ifdef SDL_PLATFORM_EMSCRIPTEN
268
    emscripten_set_main_loop(loop, 0, 1);
269
#else
270
    while (!done) {
271
        ++frames;
272
        loop();
273
    }
274
#endif
275

276
    /* Print out some timing information */
277
    now = SDL_GetTicks();
278
    if (now > then) {
279
        double fps = ((double)frames * 1000) / (now - then);
280
        SDL_Log("%2.2f frames per second\n", fps);
281
    }
282

283
    quit(0);
284

285
    return 0;
286
}
287

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

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

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

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