Create a texture for a rendering context.
Defined in <SDL3/SDL_render.h>
int w, int h); SDL_Texture * SDL_CreateTexture(SDL_Renderer *renderer, SDL_PixelFormat format, SDL_TextureAccess access,
SDL_Renderer * | renderer | the rendering context. |
SDL_PixelFormat | format | one of the enumerated values in SDL_PixelFormat. |
SDL_TextureAccess | access | one of the enumerated values in SDL_TextureAccess. |
int | w | the width of the texture in pixels. |
int | h | the height of the texture in pixels. |
(SDL_Texture *) Returns a pointer to the created texture or NULL if no rendering context was active, the format was unsupported, or the width or height were out of range; call SDL_GetError() for more information.
The contents of a texture when first created are not defined.
This function should only be called on the main thread.
This function is available since SDL 3.1.3.
#include <SDL3/SDL.h>
#include <stdlib.h>
/* Moving Rectangle */
int main(int argc, char *argv[])
{
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Texture *texture;
SDL_Event event;
SDL_FRect r;
if (!SDL_Init(SDL_INIT_VIDEO)) {
"Couldn't initialize SDL: %s", SDL_GetError());
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, return 3;
}
"SDL_CreateTexture",
window = SDL_CreateWindow(1024, 768,
SDL_WINDOW_RESIZABLE);
100;
r.w = 50;
r.h =
renderer = SDL_CreateRenderer(window, NULL);
1024, 768);
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET,
while (1) {
SDL_PollEvent(&event);if(event.type == SDL_EVENT_QUIT)
break;
500;
r.x=rand()%500;
r.y=rand()%
SDL_SetRenderTarget(renderer, texture);0x00, 0x00, 0x00, 0x00);
SDL_SetRenderDrawColor(renderer,
SDL_RenderClear(renderer);
SDL_RenderRect(renderer,&r);0xFF, 0x00, 0x00, 0x00);
SDL_SetRenderDrawColor(renderer,
SDL_RenderFillRect(renderer, &r);
SDL_SetRenderTarget(renderer, NULL);
SDL_RenderTexture(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
}
SDL_DestroyRenderer(renderer);
SDL_Quit();return 0;
}