diff options
Diffstat (limited to 'contrib/SDL-3.2.8/examples/renderer/15-cliprect/cliprect.c')
| -rw-r--r-- | contrib/SDL-3.2.8/examples/renderer/15-cliprect/cliprect.c | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/examples/renderer/15-cliprect/cliprect.c b/contrib/SDL-3.2.8/examples/renderer/15-cliprect/cliprect.c new file mode 100644 index 0000000..058072c --- /dev/null +++ b/contrib/SDL-3.2.8/examples/renderer/15-cliprect/cliprect.c | |||
| @@ -0,0 +1,137 @@ | |||
| 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. */ | ||
| 18 | static SDL_Window *window = NULL; | ||
| 19 | static SDL_Renderer *renderer = NULL; | ||
| 20 | static SDL_Texture *texture = NULL; | ||
| 21 | static SDL_FPoint cliprect_position; | ||
| 22 | static SDL_FPoint cliprect_direction; | ||
| 23 | static 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. */ | ||
| 30 | SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) | ||
| 31 | { | ||
| 32 | SDL_Surface *surface = NULL; | ||
| 33 | char *bmp_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, 0, &window, &renderer)) { | ||
| 43 | SDL_Log("Couldn't create window/renderer: %s", SDL_GetError()); | ||
| 44 | return SDL_APP_FAILURE; | ||
| 45 | } | ||
| 46 | |||
| 47 | cliprect_direction.x = cliprect_direction.y = 1.0f; | ||
| 48 | |||
| 49 | last_time = SDL_GetTicks(); | ||
| 50 | |||
| 51 | /* Textures are pixel data that we upload to the video hardware for fast drawing. Lots of 2D | ||
| 52 | engines refer to these as "sprites." We'll do a static texture (upload once, draw many | ||
| 53 | times) with data from a bitmap file. */ | ||
| 54 | |||
| 55 | /* SDL_Surface is pixel data the CPU can access. SDL_Texture is pixel data the GPU can access. | ||
| 56 | Load a .bmp into a surface, move it to a texture from there. */ | ||
| 57 | SDL_asprintf(&bmp_path, "%ssample.bmp", SDL_GetBasePath()); /* allocate a string of the full file path */ | ||
| 58 | surface = SDL_LoadBMP(bmp_path); | ||
| 59 | if (!surface) { | ||
| 60 | SDL_Log("Couldn't load bitmap: %s", SDL_GetError()); | ||
| 61 | return SDL_APP_FAILURE; | ||
| 62 | } | ||
| 63 | |||
| 64 | SDL_free(bmp_path); /* done with this, the file is loaded. */ | ||
| 65 | |||
| 66 | texture = SDL_CreateTextureFromSurface(renderer, surface); | ||
| 67 | if (!texture) { | ||
| 68 | SDL_Log("Couldn't create static texture: %s", SDL_GetError()); | ||
| 69 | return SDL_APP_FAILURE; | ||
| 70 | } | ||
| 71 | |||
| 72 | SDL_DestroySurface(surface); /* done with this, the texture has a copy of the pixels now. */ | ||
| 73 | |||
| 74 | return SDL_APP_CONTINUE; /* carry on with the program! */ | ||
| 75 | } | ||
| 76 | |||
| 77 | /* This function runs when a new event (mouse input, keypresses, etc) occurs. */ | ||
| 78 | SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) | ||
| 79 | { | ||
| 80 | if (event->type == SDL_EVENT_QUIT) { | ||
| 81 | return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */ | ||
| 82 | } | ||
| 83 | return SDL_APP_CONTINUE; /* carry on with the program! */ | ||
| 84 | } | ||
| 85 | |||
| 86 | /* This function runs once per frame, and is the heart of the program. */ | ||
| 87 | SDL_AppResult SDL_AppIterate(void *appstate) | ||
| 88 | { | ||
| 89 | const SDL_Rect cliprect = { (int) SDL_roundf(cliprect_position.x), (int) SDL_roundf(cliprect_position.y), CLIPRECT_SIZE, CLIPRECT_SIZE }; | ||
| 90 | const Uint64 now = SDL_GetTicks(); | ||
| 91 | const float elapsed = ((float) (now - last_time)) / 1000.0f; /* seconds since last iteration */ | ||
| 92 | const float distance = elapsed * CLIPRECT_SPEED; | ||
| 93 | |||
| 94 | /* Set a new clipping rectangle position */ | ||
| 95 | cliprect_position.x += distance * cliprect_direction.x; | ||
| 96 | if (cliprect_position.x < 0.0f) { | ||
| 97 | cliprect_position.x = 0.0f; | ||
| 98 | cliprect_direction.x = 1.0f; | ||
| 99 | } else if (cliprect_position.x >= (WINDOW_WIDTH - CLIPRECT_SIZE)) { | ||
| 100 | cliprect_position.x = (WINDOW_WIDTH - CLIPRECT_SIZE) - 1; | ||
| 101 | cliprect_direction.x = -1.0f; | ||
| 102 | } | ||
| 103 | |||
| 104 | cliprect_position.y += distance * cliprect_direction.y; | ||
| 105 | if (cliprect_position.y < 0.0f) { | ||
| 106 | cliprect_position.y = 0.0f; | ||
| 107 | cliprect_direction.y = 1.0f; | ||
| 108 | } else if (cliprect_position.y >= (WINDOW_HEIGHT - CLIPRECT_SIZE)) { | ||
| 109 | cliprect_position.y = (WINDOW_HEIGHT - CLIPRECT_SIZE) - 1; | ||
| 110 | cliprect_direction.y = -1.0f; | ||
| 111 | } | ||
| 112 | SDL_SetRenderClipRect(renderer, &cliprect); | ||
| 113 | |||
| 114 | last_time = now; | ||
| 115 | |||
| 116 | /* okay, now draw! */ | ||
| 117 | |||
| 118 | /* Note that SDL_RenderClear is _not_ affected by the clipping rectangle! */ | ||
| 119 | SDL_SetRenderDrawColor(renderer, 33, 33, 33, SDL_ALPHA_OPAQUE); /* grey, full alpha */ | ||
| 120 | SDL_RenderClear(renderer); /* start with a blank canvas. */ | ||
| 121 | |||
| 122 | /* stretch the texture across the entire window. Only the piece in the | ||
| 123 | clipping rectangle will actually render, though! */ | ||
| 124 | SDL_RenderTexture(renderer, texture, NULL, NULL); | ||
| 125 | |||
| 126 | SDL_RenderPresent(renderer); /* put it all on the screen! */ | ||
| 127 | |||
| 128 | return SDL_APP_CONTINUE; /* carry on with the program! */ | ||
| 129 | } | ||
| 130 | |||
| 131 | /* This function runs once at shutdown. */ | ||
| 132 | void SDL_AppQuit(void *appstate, SDL_AppResult result) | ||
| 133 | { | ||
| 134 | SDL_DestroyTexture(texture); | ||
| 135 | /* SDL will clean up the window/renderer for us. */ | ||
| 136 | } | ||
| 137 | |||
