Atlas - testrotate.c

Home / ext / SDL / test Lines: 2 | Size: 5400 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Copyright (C) 1997-2025 Sam Lantinga <[email protected]> 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#define SDL_MAIN_USE_CALLBACKS 1 14#include <SDL3/SDL_test.h> 15#include <SDL3/SDL_test_common.h> 16#include <SDL3/SDL_main.h> 17 18#define IMAGE_SIZE 256 19 20static SDLTest_CommonState *state; 21static SDL_Surface *image; 22static SDL_Texture *texture; 23static int format_index = -1; 24static SDL_PixelFormat formats[] = { 25 SDL_PIXELFORMAT_RGBA32, 26 SDL_PIXELFORMAT_ARGB32, 27 SDL_PIXELFORMAT_RGBX32, 28 SDL_PIXELFORMAT_XRGB32, 29 SDL_PIXELFORMAT_ARGB1555, 30 SDL_PIXELFORMAT_INDEX8 31}; 32static int angle; 33static int direction = 1; 34 35static bool UpdateImageFormat(void) 36{ 37 static const SDL_Color colors[] = { 38 { 255, 255, 255, SDL_ALPHA_TRANSPARENT }, /* Colorkey - white with transparent alpha */ 39 { 255, 0, 0, SDL_ALPHA_OPAQUE }, /* Red */ 40 { 255, 255, 0, SDL_ALPHA_OPAQUE }, /* Yellow */ 41 { 0, 255, 0, SDL_ALPHA_OPAQUE }, /* Green */ 42 { 0, 0, 255, SDL_ALPHA_OPAQUE }, /* Blue */ 43 }; 44 SDL_Rect rect; 45 Uint32 color; 46 47 ++format_index; 48 if (format_index == SDL_arraysize(formats)) { 49 format_index = 0; 50 } 51 52 SDL_DestroySurface(image); 53 54 image = SDL_CreateSurface(IMAGE_SIZE, IMAGE_SIZE, formats[format_index]); 55 if (!image) { 56 SDL_Log("Couldn't create surface: %s\n", SDL_GetError()); 57 return false; 58 } 59 60 if (image->format == SDL_PIXELFORMAT_INDEX8) { 61 /* Set the palette and colorkey */ 62 SDL_Palette *palette = SDL_CreateSurfacePalette(image); 63 64 SDL_SetPaletteColors(palette, colors, 0, SDL_arraysize(colors)); 65 SDL_SetSurfaceColorKey(image, true, 0); 66 } 67 68 /* Upper left */ 69 rect.x = 0; 70 rect.y = 0; 71 rect.w = IMAGE_SIZE / 2; 72 rect.h = IMAGE_SIZE / 2; 73 color = SDL_MapSurfaceRGB(image, colors[1].r, colors[1].g, colors[1].b); 74 SDL_FillSurfaceRect(image, &rect, color); 75 76 /* Upper right */ 77 rect.x += rect.w; 78 color = SDL_MapSurfaceRGB(image, colors[2].r, colors[2].g, colors[2].b); 79 SDL_FillSurfaceRect(image, &rect, color); 80 81 /* Lower left */ 82 rect.x = 0; 83 rect.y += rect.h; 84 color = SDL_MapSurfaceRGB(image, colors[3].r, colors[3].g, colors[3].b); 85 SDL_FillSurfaceRect(image, &rect, color); 86 87 /* Lower right */ 88 rect.x += rect.w; 89 color = SDL_MapSurfaceRGB(image, colors[4].r, colors[4].g, colors[4].b); 90 SDL_FillSurfaceRect(image, &rect, color); 91 92 return true; 93} 94 95static bool UpdateRotation(SDL_Renderer *renderer) 96{ 97 SDL_Surface *rotated; 98 99 angle += direction; 100 101 rotated = SDL_RotateSurface(image, (float)angle); 102 if (!rotated) { 103 SDL_Log("Couldn't rotate surface: %s", SDL_GetError()); 104 return false; 105 } 106 107 SDL_DestroyTexture(texture); 108 texture = SDL_CreateTextureFromSurface(renderer, rotated); 109 SDL_DestroySurface(rotated); 110 if (!texture) { 111 SDL_Log("Couldn't create texture: %s", SDL_GetError()); 112 return false; 113 } 114 115 return true; 116} 117 118static void Draw(SDL_Renderer *renderer) 119{ 120 int w, h; 121 SDL_FRect dst; 122 123 /* Clear the screen */ 124 SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE); 125 SDL_RenderClear(renderer); 126 127 UpdateRotation(renderer); 128 129 /* Draw the rotated image */ 130 SDL_GetCurrentRenderOutputSize(renderer, &w, &h); 131 dst.x = (w - texture->w) / 2.0f; 132 dst.y = (h - texture->h) / 2.0f; 133 dst.w = (float)texture->w; 134 dst.h = (float)texture->h; 135 SDL_RenderTexture(renderer, texture, NULL, &dst); 136 137 /* Show the current format */ 138 SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE); 139 SDL_RenderDebugTextFormat(renderer, 4.0f, 4.0f, "Format: %s, press SPACE to cycle", SDL_GetPixelFormatName(formats[format_index])); 140 141 /* All done! */ 142 SDL_RenderPresent(renderer); 143} 144 145void SDL_AppQuit(void *appstate, SDL_AppResult result) 146{ 147 SDLTest_CommonQuit(state); 148} 149 150SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) 151{ 152 /* Initialize test framework */ 153 state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO); 154 if (!state) { 155 return SDL_APP_FAILURE; 156 } 157 158 if (!SDLTest_CommonInit(state)) { 159 return SDL_APP_FAILURE; 160 } 161 162 /* Create the spinning image */ 163 UpdateImageFormat(); 164 165 return SDL_APP_CONTINUE; 166} 167 168 169SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) 170{ 171 switch (event->type) { 172 case SDL_EVENT_KEY_UP: 173 switch (event->key.key) { 174 case SDLK_SPACE: 175 UpdateImageFormat(); 176 break; 177 case SDLK_LEFT: 178 direction = -1; 179 break; 180 case SDLK_RIGHT: 181 direction = 1; 182 break; 183 default: 184 break; 185 } 186 break; 187 default: 188 break; 189 } 190 return SDLTest_CommonEventMainCallbacks(state, event); 191} 192 193SDL_AppResult SDL_AppIterate(void *appstate) 194{ 195 int i; 196 197 for (i = 0; i < state->num_windows; ++i) { 198 Draw(state->renderers[i]); 199 } 200 201 /* Wait a bit so we don't spin too quickly to see */ 202 SDL_Delay(10); 203 204 return SDL_APP_CONTINUE; 205} 206
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.