Atlas - cliprect.c

Home / ext / SDL / examples / renderer / 15-cliprect Lines: 1 | Size: 5312 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 * This example creates an SDL window and renderer, and then draws a scene 3 * to it every frame, while sliding around a clipping rectangle. 4 * 5 * This code is public domain. Feel free to use it for any purpose! 6 */ 7 8#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */ 9#include <SDL3/SDL.h> 10#include <SDL3/SDL_main.h> 11 12#define WINDOW_WIDTH 640 13#define WINDOW_HEIGHT 480 14#define CLIPRECT_SIZE 250 15#define CLIPRECT_SPEED 200 /* pixels per second */ 16 17/* We will use this renderer to draw into this window every frame. */ 18static SDL_Window *window = NULL; 19static SDL_Renderer *renderer = NULL; 20static SDL_Texture *texture = NULL; 21static SDL_FPoint cliprect_position; 22static SDL_FPoint cliprect_direction; 23static Uint64 last_time = 0; 24 25/* A lot of this program is examples/renderer/02-primitives, so we have a good 26 visual that we can slide a clip rect around. The actual new magic in here 27 is the SDL_SetRenderClipRect() function. */ 28 29/* This function runs once at startup. */ 30SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) 31{ 32 SDL_Surface *surface = NULL; 33 char *png_path = NULL; 34 35 SDL_SetAppMetadata("Example Renderer Clipping Rectangle", "1.0", "com.example.renderer-cliprect"); 36 37 if (!SDL_Init(SDL_INIT_VIDEO)) { 38 SDL_Log("Couldn't initialize SDL: %s", SDL_GetError()); 39 return SDL_APP_FAILURE; 40 } 41 42 if (!SDL_CreateWindowAndRenderer("examples/renderer/cliprect", WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_RESIZABLE, &window, &renderer)) { 43 SDL_Log("Couldn't create window/renderer: %s", SDL_GetError()); 44 return SDL_APP_FAILURE; 45 } 46 SDL_SetRenderLogicalPresentation(renderer, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_LOGICAL_PRESENTATION_LETTERBOX); 47 48 cliprect_direction.x = cliprect_direction.y = 1.0f; 49 50 last_time = SDL_GetTicks(); 51 52 /* Textures are pixel data that we upload to the video hardware for fast drawing. Lots of 2D 53 engines refer to these as "sprites." We'll do a static texture (upload once, draw many 54 times) with data from a bitmap file. */ 55 56 /* SDL_Surface is pixel data the CPU can access. SDL_Texture is pixel data the GPU can access. 57 Load a .png into a surface, move it to a texture from there. */ 58 SDL_asprintf(&png_path, "%ssample.png", SDL_GetBasePath()); /* allocate a string of the full file path */ 59 surface = SDL_LoadPNG(png_path); 60 if (!surface) { 61 SDL_Log("Couldn't load bitmap: %s", SDL_GetError()); 62 return SDL_APP_FAILURE; 63 } 64 65 SDL_free(png_path); /* done with this, the file is loaded. */ 66 67 texture = SDL_CreateTextureFromSurface(renderer, surface); 68 if (!texture) { 69 SDL_Log("Couldn't create static texture: %s", SDL_GetError()); 70 return SDL_APP_FAILURE; 71 } 72 73 SDL_DestroySurface(surface); /* done with this, the texture has a copy of the pixels now. */ 74 75 return SDL_APP_CONTINUE; /* carry on with the program! */ 76} 77 78/* This function runs when a new event (mouse input, keypresses, etc) occurs. */ 79SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) 80{ 81 if (event->type == SDL_EVENT_QUIT) { 82 return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */ 83 } 84 return SDL_APP_CONTINUE; /* carry on with the program! */ 85} 86 87/* This function runs once per frame, and is the heart of the program. */ 88SDL_AppResult SDL_AppIterate(void *appstate) 89{ 90 const SDL_Rect cliprect = { (int) SDL_roundf(cliprect_position.x), (int) SDL_roundf(cliprect_position.y), CLIPRECT_SIZE, CLIPRECT_SIZE }; 91 const Uint64 now = SDL_GetTicks(); 92 const float elapsed = ((float) (now - last_time)) / 1000.0f; /* seconds since last iteration */ 93 const float distance = elapsed * CLIPRECT_SPEED; 94 95 /* Set a new clipping rectangle position */ 96 cliprect_position.x += distance * cliprect_direction.x; 97 if (cliprect_position.x < -CLIPRECT_SIZE) { 98 cliprect_position.x = -CLIPRECT_SIZE; 99 cliprect_direction.x = 1.0f; 100 } else if (cliprect_position.x >= WINDOW_WIDTH) { 101 cliprect_position.x = WINDOW_WIDTH - 1; 102 cliprect_direction.x = -1.0f; 103 } 104 105 cliprect_position.y += distance * cliprect_direction.y; 106 if (cliprect_position.y < -CLIPRECT_SIZE) { 107 cliprect_position.y = -CLIPRECT_SIZE; 108 cliprect_direction.y = 1.0f; 109 } else if (cliprect_position.y >= WINDOW_HEIGHT) { 110 cliprect_position.y = WINDOW_HEIGHT - 1; 111 cliprect_direction.y = -1.0f; 112 } 113 SDL_SetRenderClipRect(renderer, &cliprect); 114 115 last_time = now; 116 117 /* okay, now draw! */ 118 119 /* Note that SDL_RenderClear is _not_ affected by the clipping rectangle! */ 120 SDL_SetRenderDrawColor(renderer, 33, 33, 33, SDL_ALPHA_OPAQUE); /* grey, full alpha */ 121 SDL_RenderClear(renderer); /* start with a blank canvas. */ 122 123 /* stretch the texture across the entire window. Only the piece in the 124 clipping rectangle will actually render, though! */ 125 SDL_RenderTexture(renderer, texture, NULL, NULL); 126 127 SDL_RenderPresent(renderer); /* put it all on the screen! */ 128 129 return SDL_APP_CONTINUE; /* carry on with the program! */ 130} 131 132/* This function runs once at shutdown. */ 133void SDL_AppQuit(void *appstate, SDL_AppResult result) 134{ 135 SDL_DestroyTexture(texture); 136 /* SDL will clean up the window/renderer for us. */ 137} 138 139
[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.