SDL

Форк
0
/
testnative.c 
241 строка · 7.2 Кб
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
/* Simple program:  Create a native window and attach an SDL renderer */
13

14
#include "testnative.h"
15

16
#include <SDL3/SDL.h>
17
#include <SDL3/SDL_main.h>
18
#include <SDL3/SDL_test.h>
19

20
#include "testutils.h"
21

22
#include <stdlib.h>
23

24
#define WINDOW_W    640
25
#define WINDOW_H    480
26
#define NUM_SPRITES 100
27
#define MAX_SPEED   1
28

29
static NativeWindowFactory *factories[] = {
30
#ifdef TEST_NATIVE_WINDOWS
31
    &WindowsWindowFactory,
32
#endif
33
#ifdef TEST_NATIVE_WAYLAND
34
    &WaylandWindowFactory,
35
#endif
36
#ifdef TEST_NATIVE_X11
37
    &X11WindowFactory,
38
#endif
39
#ifdef TEST_NATIVE_COCOA
40
    &CocoaWindowFactory,
41
#endif
42
    NULL
43
};
44
static NativeWindowFactory *factory = NULL;
45
static void *native_window;
46
static SDL_FRect *positions, *velocities;
47
static SDLTest_CommonState *state;
48

49
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
50
static void
51
quit(int rc)
52
{
53
    if (native_window && factory) {
54
        factory->DestroyNativeWindow(native_window);
55
    }
56
    SDL_Quit();
57
    SDLTest_CommonDestroyState(state);
58
    /* Let 'main()' return normally */
59
    if (rc != 0) {
60
        exit(rc);
61
    }
62
}
63

64
static void MoveSprites(SDL_Renderer *renderer, SDL_Texture *sprite)
65
{
66
    float sprite_w, sprite_h;
67
    int i;
68
    SDL_Rect viewport;
69
    SDL_FRect *position, *velocity;
70

71
    /* Query the sizes */
72
    SDL_GetRenderViewport(renderer, &viewport);
73
    SDL_GetTextureSize(sprite, &sprite_w, &sprite_h);
74

75
    /* Draw a gray background */
76
    SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
77
    SDL_RenderClear(renderer);
78

79
    /* Move the sprite, bounce at the wall, and draw */
80
    for (i = 0; i < NUM_SPRITES; ++i) {
81
        position = &positions[i];
82
        velocity = &velocities[i];
83
        position->x += velocity->x;
84
        if ((position->x < 0) || (position->x >= (viewport.w - sprite_w))) {
85
            velocity->x = -velocity->x;
86
            position->x += velocity->x;
87
        }
88
        position->y += velocity->y;
89
        if ((position->y < 0) || (position->y >= (viewport.h - sprite_h))) {
90
            velocity->y = -velocity->y;
91
            position->y += velocity->y;
92
        }
93

94
        /* Blit the sprite onto the screen */
95
        SDL_RenderTexture(renderer, sprite, NULL, position);
96
    }
97

98
    /* Update the screen! */
99
    SDL_RenderPresent(renderer);
100
}
101

102
int main(int argc, char *argv[])
103
{
104
    int i, done;
105
    const char *driver;
106
    SDL_PropertiesID props;
107
    SDL_Window *window;
108
    SDL_Renderer *renderer;
109
    SDL_Texture *sprite;
110
    int window_w, window_h;
111
    float sprite_w, sprite_h;
112
    SDL_Event event;
113

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

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

123
    /* Parse commandline */
124
    if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
125
        return 1;
126
    }
127

128
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
129
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL video: %s\n",
130
                     SDL_GetError());
131
        exit(1);
132
    }
133
    driver = SDL_GetCurrentVideoDriver();
134

135
    /* Find a native window driver and create a native window */
136
    for (i = 0; factories[i]; ++i) {
137
        if (SDL_strcmp(driver, factories[i]->tag) == 0) {
138
            factory = factories[i];
139
            break;
140
        }
141
    }
142
    if (!factory) {
143
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't find native window code for %s driver\n",
144
                     driver);
145
        quit(2);
146
    }
147
    SDL_Log("Creating native window for %s driver\n", driver);
148
    native_window = factory->CreateNativeWindow(WINDOW_W, WINDOW_H);
149
    if (!native_window) {
150
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create native window\n");
151
        quit(3);
152
    }
153
    props = SDL_CreateProperties();
154
    SDL_SetPointerProperty(props, "sdl2-compat.external_window", native_window);
155
    SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_OPENGL_BOOLEAN, SDL_TRUE);
156
    SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, WINDOW_W);
157
    SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, WINDOW_H);
158
    window = SDL_CreateWindowWithProperties(props);
159
    SDL_DestroyProperties(props);
160
    if (!window) {
161
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create SDL window: %s\n", SDL_GetError());
162
        quit(4);
163
    }
164
    SDL_SetWindowTitle(window, "SDL Native Window Test");
165

166
    /* Create the renderer */
167
    renderer = SDL_CreateRenderer(window, NULL);
168
    if (!renderer) {
169
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
170
        quit(5);
171
    }
172

173
    /* Clear the window, load the sprite and go! */
174
    SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
175
    SDL_RenderClear(renderer);
176

177
    sprite = LoadTexture(renderer, "icon.bmp", SDL_TRUE, NULL, NULL);
178
    if (!sprite) {
179
        quit(6);
180
    }
181

182
    /* Allocate memory for the sprite info */
183
    SDL_GetWindowSize(window, &window_w, &window_h);
184
    SDL_GetTextureSize(sprite, &sprite_w, &sprite_h);
185
    positions = (SDL_FRect *)SDL_malloc(NUM_SPRITES * sizeof(*positions));
186
    velocities = (SDL_FRect *)SDL_malloc(NUM_SPRITES * sizeof(*velocities));
187
    if (!positions || !velocities) {
188
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!\n");
189
        quit(2);
190
    }
191
    for (i = 0; i < NUM_SPRITES; ++i) {
192
        positions[i].x = (float)(SDL_rand(window_w - (int)sprite_w));
193
        positions[i].y = (float)(SDL_rand(window_h - (int)sprite_h));
194
        positions[i].w = sprite_w;
195
        positions[i].h = sprite_h;
196
        velocities[i].x = 0.0f;
197
        velocities[i].y = 0.0f;
198
        while (velocities[i].x == 0.f && velocities[i].y == 0.f) {
199
            velocities[i].x = (float)(SDL_rand(MAX_SPEED * 2 + 1) - MAX_SPEED);
200
            velocities[i].y = (float)(SDL_rand(MAX_SPEED * 2 + 1) - MAX_SPEED);
201
        }
202
    }
203

204
    /* Main render loop */
205
    done = 0;
206
    while (!done) {
207
        /* Check for events */
208
        while (SDL_PollEvent(&event)) {
209
            if (state->verbose & VERBOSE_EVENT) {
210
                if (((event.type != SDL_EVENT_MOUSE_MOTION) &&
211
                    (event.type != SDL_EVENT_FINGER_MOTION)) ||
212
                    (state->verbose & VERBOSE_MOTION)) {
213
                    SDLTest_PrintEvent(&event);
214
                }
215
            }
216

217
            switch (event.type) {
218
            case SDL_EVENT_WINDOW_EXPOSED:
219
                SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
220
                SDL_RenderClear(renderer);
221
                break;
222
            case SDL_EVENT_QUIT:
223
                done = 1;
224
                break;
225
            default:
226
                break;
227
            }
228
        }
229
        MoveSprites(renderer, sprite);
230
    }
231

232
    SDL_DestroyTexture(sprite);
233
    SDL_DestroyRenderer(renderer);
234
    SDL_DestroyWindow(window);
235
    SDL_free(positions);
236
    SDL_free(velocities);
237

238
    quit(0);
239

240
    return 0; /* to prevent compiler warning */
241
}
242

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

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

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

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