SDL

Форк
0
/
testgles.c 
348 строк · 11.3 Кб
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
#include <stdlib.h>
13

14
#include <SDL3/SDL_test_common.h>
15
#include <SDL3/SDL_main.h>
16

17
#if defined(SDL_PLATFORM_IOS) || defined(SDL_PLATFORM_ANDROID)
18
#define HAVE_OPENGLES
19
#endif
20

21
#ifdef HAVE_OPENGLES
22

23
#include <SDL3/SDL_opengles.h>
24

25
static SDLTest_CommonState *state;
26
static SDL_GLContext *context = NULL;
27
static int depth = 16;
28

29
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
30
static void
31
quit(int rc)
32
{
33
    int i;
34

35
    if (context) {
36
        for (i = 0; i < state->num_windows; i++) {
37
            if (context[i]) {
38
                SDL_GL_DestroyContext(context[i]);
39
            }
40
        }
41

42
        SDL_free(context);
43
    }
44

45
    SDLTest_CommonQuit(state);
46
    /* Let 'main()' return normally */
47
    if (rc != 0) {
48
        exit(rc);
49
    }
50
}
51

52
static void
53
Render(void)
54
{
55
    static GLubyte color[8][4] = { { 255, 0, 0, 0 },
56
                                   { 255, 0, 0, 255 },
57
                                   { 0, 255, 0, 255 },
58
                                   { 0, 255, 0, 255 },
59
                                   { 0, 255, 0, 255 },
60
                                   { 255, 255, 255, 255 },
61
                                   { 255, 0, 255, 255 },
62
                                   { 0, 0, 255, 255 } };
63
    static GLfloat cube[8][3] = { { 0.5, 0.5, -0.5 },
64
                                  { 0.5f, -0.5f, -0.5f },
65
                                  { -0.5f, -0.5f, -0.5f },
66
                                  { -0.5f, 0.5f, -0.5f },
67
                                  { -0.5f, 0.5f, 0.5f },
68
                                  { 0.5f, 0.5f, 0.5f },
69
                                  { 0.5f, -0.5f, 0.5f },
70
                                  { -0.5f, -0.5f, 0.5f } };
71
    static GLubyte indices[36] = { 0, 3, 4,
72
                                   4, 5, 0,
73
                                   0, 5, 6,
74
                                   6, 1, 0,
75
                                   6, 7, 2,
76
                                   2, 1, 6,
77
                                   7, 4, 3,
78
                                   3, 2, 7,
79
                                   5, 4, 7,
80
                                   7, 6, 5,
81
                                   2, 3, 1,
82
                                   3, 0, 1 };
83

84
    /* Do our drawing, too. */
85
    glClearColor(0.0, 0.0, 0.0, 1.0);
86
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
87

88
    /* Draw the cube */
89
    glColorPointer(4, GL_UNSIGNED_BYTE, 0, color);
90
    glEnableClientState(GL_COLOR_ARRAY);
91
    glVertexPointer(3, GL_FLOAT, 0, cube);
92
    glEnableClientState(GL_VERTEX_ARRAY);
93
    glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, indices);
94

95
    glMatrixMode(GL_MODELVIEW);
96
    glRotatef(5.0, 1.0, 1.0, 1.0);
97
}
98

99
int main(int argc, char *argv[])
100
{
101
    int fsaa, accel;
102
    int value;
103
    int i, done;
104
    const SDL_DisplayMode *mode;
105
    SDL_Event event;
106
    Uint32 then, now, frames;
107
    int status;
108

109
    /* Initialize parameters */
110
    fsaa = 0;
111
    accel = 0;
112

113
    /* Initialize test framework */
114
    state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
115
    if (!state) {
116
        return 1;
117
    }
118

119
    /* Enable standard application logging */
120
    SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
121

122
    for (i = 1; i < argc;) {
123
        int consumed;
124

125
        consumed = SDLTest_CommonArg(state, i);
126
        if (consumed == 0) {
127
            if (SDL_strcasecmp(argv[i], "--fsaa") == 0) {
128
                ++fsaa;
129
                consumed = 1;
130
            } else if (SDL_strcasecmp(argv[i], "--accel") == 0) {
131
                ++accel;
132
                consumed = 1;
133
            } else if (SDL_strcasecmp(argv[i], "--zdepth") == 0) {
134
                i++;
135
                if (!argv[i]) {
136
                    consumed = -1;
137
                } else {
138
                    char *endptr = NULL;
139
                    depth = (int)SDL_strtol(argv[i], &endptr, 0);
140
                    if (endptr != argv[i] && *endptr == '\0') {
141
                        consumed = 1;
142
                    } else {
143
                        consumed = -1;
144
                    }
145
                }
146
            } else {
147
                consumed = -1;
148
            }
149
        }
150
        if (consumed < 0) {
151
            static const char *options[] = { "[--fsaa]", "[--accel]", "[--zdepth %d]", NULL };
152
            SDLTest_CommonLogUsage(state, argv[0], options);
153
            quit(1);
154
        }
155
        i += consumed;
156
    }
157

158
    /* Set OpenGL parameters */
159
    state->window_flags |= SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_BORDERLESS;
160
    state->gl_red_size = 5;
161
    state->gl_green_size = 5;
162
    state->gl_blue_size = 5;
163
    state->gl_depth_size = depth;
164
    state->gl_major_version = 1;
165
    state->gl_minor_version = 1;
166
    state->gl_profile_mask = SDL_GL_CONTEXT_PROFILE_ES;
167
    if (fsaa) {
168
        state->gl_multisamplebuffers = 1;
169
        state->gl_multisamplesamples = fsaa;
170
    }
171
    if (accel) {
172
        state->gl_accelerated = 1;
173
    }
174
    if (!SDLTest_CommonInit(state)) {
175
        quit(2);
176
    }
177

178
    context = (SDL_GLContext *)SDL_calloc(state->num_windows, sizeof(*context));
179
    if (!context) {
180
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!\n");
181
        quit(2);
182
    }
183

184
    /* Create OpenGL ES contexts */
185
    for (i = 0; i < state->num_windows; i++) {
186
        context[i] = SDL_GL_CreateContext(state->windows[i]);
187
        if (!context[i]) {
188
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_GL_CreateContext(): %s\n", SDL_GetError());
189
            quit(2);
190
        }
191
    }
192

193
    SDL_GL_SetSwapInterval(state->render_vsync);
194

195
    mode = SDL_GetCurrentDisplayMode(SDL_GetPrimaryDisplay());
196
    if (mode) {
197
        SDL_Log("Screen bpp: %d\n", SDL_BITSPERPIXEL(mode->format));
198
        SDL_Log("\n");
199
    }
200
    SDL_Log("Vendor     : %s\n", glGetString(GL_VENDOR));
201
    SDL_Log("Renderer   : %s\n", glGetString(GL_RENDERER));
202
    SDL_Log("Version    : %s\n", glGetString(GL_VERSION));
203
    SDL_Log("Extensions : %s\n", glGetString(GL_EXTENSIONS));
204
    SDL_Log("\n");
205

206
    status = SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value);
207
    if (!status) {
208
        SDL_Log("SDL_GL_RED_SIZE: requested %d, got %d\n", 5, value);
209
    } else {
210
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_RED_SIZE: %s\n",
211
                     SDL_GetError());
212
    }
213
    status = SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value);
214
    if (!status) {
215
        SDL_Log("SDL_GL_GREEN_SIZE: requested %d, got %d\n", 5, value);
216
    } else {
217
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_GREEN_SIZE: %s\n",
218
                     SDL_GetError());
219
    }
220
    status = SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value);
221
    if (!status) {
222
        SDL_Log("SDL_GL_BLUE_SIZE: requested %d, got %d\n", 5, value);
223
    } else {
224
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_BLUE_SIZE: %s\n",
225
                     SDL_GetError());
226
    }
227
    status = SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value);
228
    if (!status) {
229
        SDL_Log("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", depth, value);
230
    } else {
231
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_DEPTH_SIZE: %s\n",
232
                     SDL_GetError());
233
    }
234
    if (fsaa) {
235
        status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &value);
236
        if (!status) {
237
            SDL_Log("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
238
        } else {
239
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_MULTISAMPLEBUFFERS: %s\n",
240
                         SDL_GetError());
241
        }
242
        status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value);
243
        if (!status) {
244
            SDL_Log("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
245
                    value);
246
        } else {
247
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_MULTISAMPLESAMPLES: %s\n",
248
                         SDL_GetError());
249
        }
250
    }
251
    if (accel) {
252
        status = SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value);
253
        if (!status) {
254
            SDL_Log("SDL_GL_ACCELERATED_VISUAL: requested 1, got %d\n", value);
255
        } else {
256
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_ACCELERATED_VISUAL: %s\n",
257
                         SDL_GetError());
258
        }
259
    }
260

261
    /* Set rendering settings for each context */
262
    for (i = 0; i < state->num_windows; ++i) {
263
        float aspectAdjust;
264

265
        status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
266
        if (status) {
267
            SDL_Log("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
268

269
            /* Continue for next window */
270
            continue;
271
        }
272

273
        aspectAdjust = (4.0f / 3.0f) / ((float)state->window_w / state->window_h);
274
        glViewport(0, 0, state->window_w, state->window_h);
275
        glMatrixMode(GL_PROJECTION);
276
        glLoadIdentity();
277
        glOrthof(-2.0, 2.0, -2.0 * aspectAdjust, 2.0 * aspectAdjust, -20.0, 20.0);
278
        glMatrixMode(GL_MODELVIEW);
279
        glLoadIdentity();
280
        glEnable(GL_DEPTH_TEST);
281
        glDepthFunc(GL_LESS);
282
        glShadeModel(GL_SMOOTH);
283
    }
284

285
    /* Main render loop */
286
    frames = 0;
287
    then = SDL_GetTicks();
288
    done = 0;
289
    while (!done) {
290
        /* Check for events */
291
        ++frames;
292
        while (SDL_PollEvent(&event)) {
293
            if (event.type == SDL_EVENT_WINDOW_RESIZED) {
294
                for (i = 0; i < state->num_windows; ++i) {
295
                    if (event.window.windowID == SDL_GetWindowID(state->windows[i])) {
296
                        status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
297
                        if (status) {
298
                            SDL_Log("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
299
                            break;
300
                        }
301
                        /* Change view port to the new window dimensions */
302
                        glViewport(0, 0, event.window.data1, event.window.data2);
303
                        /* Update window content */
304
                        Render();
305
                        SDL_GL_SwapWindow(state->windows[i]);
306
                        break;
307
                    }
308
                }
309
            }
310
            SDLTest_CommonEvent(state, &event, &done);
311
        }
312
        for (i = 0; i < state->num_windows; ++i) {
313
            if (state->windows[i] == NULL) {
314
                continue;
315
            }
316
            status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
317
            if (status) {
318
                SDL_Log("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
319

320
                /* Continue for next window */
321
                continue;
322
            }
323
            Render();
324
            SDL_GL_SwapWindow(state->windows[i]);
325
        }
326
    }
327

328
    /* Print out some timing information */
329
    now = SDL_GetTicks();
330
    if (now > then) {
331
        SDL_Log("%2.2f frames per second\n",
332
                ((double)frames * 1000) / (now - then));
333
    }
334
#ifndef SDL_PLATFORM_ANDROID
335
    quit(0);
336
#endif
337
    return 0;
338
}
339

340
#else /* HAVE_OPENGLES */
341

342
int main(int argc, char *argv[])
343
{
344
    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "No OpenGL ES support on this system\n");
345
    return 1;
346
}
347

348
#endif /* HAVE_OPENGLES */
349

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

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

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

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