SDL

Форк
0
/
teststreaming.c 
211 строк · 6.9 Кб
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
 *                                                                              *
14
 * Running moose :) Coded by Mike Gorchak.                                      *
15
 *                                                                              *
16
 ********************************************************************************/
17

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

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

27
#include <stdlib.h>
28

29
#define MOOSEPIC_W 64
30
#define MOOSEPIC_H 88
31

32
#define MOOSEFRAME_SIZE   (MOOSEPIC_W * MOOSEPIC_H)
33
#define MOOSEFRAMES_COUNT 10
34

35
/* *INDENT-OFF* */ /* clang-format off */
36
static SDL_Color MooseColors[84] = {
37
    {49, 49, 49, 255}, {66, 24, 0, 255}, {66, 33, 0, 255}, {66, 66, 66, 255},
38
    {66, 115, 49, 255}, {74, 33, 0, 255}, {74, 41, 16, 255}, {82, 33, 8, 255},
39
    {82, 41, 8, 255}, {82, 49, 16, 255}, {82, 82, 82, 255}, {90, 41, 8, 255},
40
    {90, 41, 16, 255}, {90, 57, 24, 255}, {99, 49, 16, 255}, {99, 66, 24, 255},
41
    {99, 66, 33, 255}, {99, 74, 33, 255}, {107, 57, 24, 255}, {107, 82, 41, 255},
42
    {115, 57, 33, 255}, {115, 66, 33, 255}, {115, 66, 41, 255}, {115, 74, 0, 255},
43
    {115, 90, 49, 255}, {115, 115, 115, 255}, {123, 82, 0, 255}, {123, 99, 57, 255},
44
    {132, 66, 41, 255}, {132, 74, 41, 255}, {132, 90, 8, 255}, {132, 99, 33, 255},
45
    {132, 99, 66, 255}, {132, 107, 66, 255}, {140, 74, 49, 255}, {140, 99, 16, 255},
46
    {140, 107, 74, 255}, {140, 115, 74, 255}, {148, 107, 24, 255}, {148, 115, 82, 255},
47
    {148, 123, 74, 255}, {148, 123, 90, 255}, {156, 115, 33, 255}, {156, 115, 90, 255},
48
    {156, 123, 82, 255}, {156, 132, 82, 255}, {156, 132, 99, 255}, {156, 156, 156, 255},
49
    {165, 123, 49, 255}, {165, 123, 90, 255}, {165, 132, 82, 255}, {165, 132, 90, 255},
50
    {165, 132, 99, 255}, {165, 140, 90, 255}, {173, 132, 57, 255}, {173, 132, 99, 255},
51
    {173, 140, 107, 255}, {173, 140, 115, 255}, {173, 148, 99, 255}, {173, 173, 173, 255},
52
    {181, 140, 74, 255}, {181, 148, 115, 255}, {181, 148, 123, 255}, {181, 156, 107, 255},
53
    {189, 148, 123, 255}, {189, 156, 82, 255}, {189, 156, 123, 255}, {189, 156, 132, 255},
54
    {189, 189, 189, 255}, {198, 156, 123, 255}, {198, 165, 132, 255}, {206, 165, 99, 255},
55
    {206, 165, 132, 255}, {206, 173, 140, 255}, {206, 206, 206, 255}, {214, 173, 115, 255},
56
    {214, 173, 140, 255}, {222, 181, 148, 255}, {222, 189, 132, 255}, {222, 189, 156, 255},
57
    {222, 222, 222, 255}, {231, 198, 165, 255}, {231, 231, 231, 255}, {239, 206, 173, 255}
58
};
59
/* *INDENT-ON* */ /* clang-format on */
60

61
static Uint8 MooseFrames[MOOSEFRAMES_COUNT][MOOSEFRAME_SIZE];
62

63
static SDL_Renderer *renderer;
64
static int frame;
65
static SDL_Texture *MooseTexture;
66
static SDL_bool done = SDL_FALSE;
67
static SDLTest_CommonState *state;
68

69
static void quit(int rc)
70
{
71
    SDL_Quit();
72
    SDLTest_CommonDestroyState(state);
73
    /* Let 'main()' return normally */
74
    if (rc != 0) {
75
        exit(rc);
76
    }
77
}
78

79
static void UpdateTexture(SDL_Texture *texture)
80
{
81
    SDL_Color *color;
82
    Uint8 *src;
83
    Uint32 *dst;
84
    int row, col;
85
    void *pixels;
86
    int pitch;
87

88
    if (SDL_LockTexture(texture, NULL, &pixels, &pitch) < 0) {
89
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't lock texture: %s\n", SDL_GetError());
90
        quit(5);
91
    }
92
    src = MooseFrames[frame];
93
    for (row = 0; row < MOOSEPIC_H; ++row) {
94
        dst = (Uint32 *)((Uint8 *)pixels + row * pitch);
95
        for (col = 0; col < MOOSEPIC_W; ++col) {
96
            color = &MooseColors[*src++];
97
            *dst++ = (0xFF000000 | (color->r << 16) | (color->g << 8) | color->b);
98
        }
99
    }
100
    SDL_UnlockTexture(texture);
101
}
102

103
static void loop(void)
104
{
105
    SDL_Event event;
106

107
    while (SDL_PollEvent(&event)) {
108
        switch (event.type) {
109
        case SDL_EVENT_KEY_DOWN:
110
            if (event.key.key == SDLK_ESCAPE) {
111
                done = SDL_TRUE;
112
            }
113
            break;
114
        case SDL_EVENT_QUIT:
115
            done = SDL_TRUE;
116
            break;
117
        default:
118
            break;
119
        }
120
    }
121

122
    frame = (frame + 1) % MOOSEFRAMES_COUNT;
123
    UpdateTexture(MooseTexture);
124

125
    SDL_RenderClear(renderer);
126
    SDL_RenderTexture(renderer, MooseTexture, NULL, NULL);
127
    SDL_RenderPresent(renderer);
128

129
#ifdef SDL_PLATFORM_EMSCRIPTEN
130
    if (done) {
131
        emscripten_cancel_main_loop();
132
    }
133
#endif
134
}
135

136
int main(int argc, char **argv)
137
{
138
    SDL_Window *window;
139
    SDL_IOStream *handle;
140
    char *filename = NULL;
141

142
    /* Initialize test framework */
143
    state = SDLTest_CommonCreateState(argv, 0);
144
    if (!state) {
145
        return 1;
146
    }
147

148
    /* Enable standard application logging */
149
    SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
150

151
    if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
152
        SDL_Quit();
153
        SDLTest_CommonDestroyState(state);
154
        return 1;
155
    }
156

157
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
158
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
159
        return 1;
160
    }
161

162
    /* load the moose images */
163
    filename = GetResourceFilename(NULL, "moose.dat");
164
    if (!filename) {
165
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory\n");
166
        return -1;
167
    }
168
    handle = SDL_IOFromFile(filename, "rb");
169
    SDL_free(filename);
170
    if (!handle) {
171
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Can't find the file moose.dat !\n");
172
        quit(2);
173
    }
174
    SDL_ReadIO(handle, MooseFrames, MOOSEFRAME_SIZE * MOOSEFRAMES_COUNT);
175
    SDL_CloseIO(handle);
176

177
    /* Create the window and renderer */
178
    window = SDL_CreateWindow("Happy Moose", MOOSEPIC_W * 4, MOOSEPIC_H * 4, SDL_WINDOW_RESIZABLE);
179
    if (!window) {
180
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create window: %s\n", SDL_GetError());
181
        quit(3);
182
    }
183

184
    renderer = SDL_CreateRenderer(window, NULL);
185
    if (!renderer) {
186
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create renderer: %s\n", SDL_GetError());
187
        quit(4);
188
    }
189

190
    MooseTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, MOOSEPIC_W, MOOSEPIC_H);
191
    if (!MooseTexture) {
192
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create texture: %s\n", SDL_GetError());
193
        quit(5);
194
    }
195

196
    /* Loop, waiting for QUIT or the escape key */
197
    frame = 0;
198

199
#ifdef SDL_PLATFORM_EMSCRIPTEN
200
    emscripten_set_main_loop(loop, 0, 1);
201
#else
202
    while (!done) {
203
        loop();
204
    }
205
#endif
206

207
    SDL_DestroyRenderer(renderer);
208

209
    quit(0);
210
    return 0;
211
}
212

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

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

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

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