Merge commit 'a22ac9e1c8e7b1cd78aa197bf1727a9f97f74b5e' into dev
This commit is contained in:
12
external/SDL/examples/CMakeLists.txt
vendored
12
external/SDL/examples/CMakeLists.txt
vendored
@@ -120,8 +120,16 @@ macro(add_sdl_example_executable TARGET)
|
||||
target_include_directories(${TARGET} PRIVATE "$<TARGET_PROPERTY:SDL3::${sdl_name_component},INCLUDE_DIRECTORIES>")
|
||||
endmacro()
|
||||
|
||||
add_sdl_example_executable(renderer-clear SOURCES renderer/01-clear/renderer-clear.c)
|
||||
add_sdl_example_executable(renderer-primitives SOURCES renderer/02-primitives/renderer-primitives.c)
|
||||
add_sdl_example_executable(renderer-clear SOURCES renderer/01-clear/clear.c)
|
||||
add_sdl_example_executable(renderer-primitives SOURCES renderer/02-primitives/primitives.c)
|
||||
add_sdl_example_executable(renderer-lines SOURCES renderer/03-lines/lines.c)
|
||||
add_sdl_example_executable(renderer-points SOURCES renderer/04-points/points.c)
|
||||
add_sdl_example_executable(renderer-rectangles SOURCES renderer/05-rectangles/rectangles.c)
|
||||
add_sdl_example_executable(renderer-textures SOURCES renderer/06-textures/textures.c DATAFILES ${CMAKE_CURRENT_SOURCE_DIR}/../test/sample.bmp)
|
||||
add_sdl_example_executable(renderer-streaming-textures SOURCES renderer/07-streaming-textures/streaming-textures.c)
|
||||
add_sdl_example_executable(renderer-rotating-textures SOURCES renderer/08-rotating-textures/rotating-textures.c DATAFILES ${CMAKE_CURRENT_SOURCE_DIR}/../test/sample.bmp)
|
||||
add_sdl_example_executable(renderer-scaling-textures SOURCES renderer/09-scaling-textures/scaling-textures.c DATAFILES ${CMAKE_CURRENT_SOURCE_DIR}/../test/sample.bmp)
|
||||
add_sdl_example_executable(renderer-geometry SOURCES renderer/10-geometry/geometry.c DATAFILES ${CMAKE_CURRENT_SOURCE_DIR}/../test/sample.bmp)
|
||||
add_sdl_example_executable(audio-simple-playback SOURCES audio/01-simple-playback/simple-playback.c)
|
||||
add_sdl_example_executable(audio-simple-playback-callback SOURCES audio/02-simple-playback-callback/simple-playback-callback.c)
|
||||
add_sdl_example_executable(audio-load-wav SOURCES audio/03-load-wav/load-wav.c DATAFILES ${CMAKE_CURRENT_SOURCE_DIR}/../test/sample.wav)
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
This example creates an SDL window and renderer, and then draws some lines,
|
||||
rectangles and points to it every frame.
|
||||
|
||||
This is just a quick overview of simple drawing primitives; futher examples
|
||||
will explore them in more detail.
|
||||
|
||||
|
||||
|
||||
3
external/SDL/examples/renderer/03-lines/README.txt
vendored
Normal file
3
external/SDL/examples/renderer/03-lines/README.txt
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
This example creates an SDL window and renderer, and then draws a something
|
||||
roughly like a Christmas tree with nothing but lines, every frame.
|
||||
|
||||
93
external/SDL/examples/renderer/03-lines/lines.c
vendored
Normal file
93
external/SDL/examples/renderer/03-lines/lines.c
vendored
Normal file
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* This example creates an SDL window and renderer, and then draws some lines
|
||||
* to it every frame.
|
||||
*
|
||||
* This code is public domain. Feel free to use it for any purpose!
|
||||
*/
|
||||
|
||||
#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3/SDL_main.h>
|
||||
|
||||
/* We will use this renderer to draw into this window every frame. */
|
||||
static SDL_Window *window = NULL;
|
||||
static SDL_Renderer *renderer = NULL;
|
||||
|
||||
/* This function runs once at startup. */
|
||||
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
|
||||
{
|
||||
if (!SDL_Init(SDL_INIT_VIDEO)) {
|
||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't initialize SDL!", SDL_GetError(), NULL);
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
if (!SDL_CreateWindowAndRenderer("examples/renderer/lines", 640, 480, 0, &window, &renderer)) {
|
||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't create window/renderer!", SDL_GetError(), NULL);
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
SDL_srand(0); /* seed the random number generator */
|
||||
|
||||
return SDL_APP_CONTINUE; /* carry on with the program! */
|
||||
}
|
||||
|
||||
/* This function runs when a new event (mouse input, keypresses, etc) occurs. */
|
||||
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
|
||||
{
|
||||
if (event->type == SDL_EVENT_QUIT) {
|
||||
return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
|
||||
}
|
||||
return SDL_APP_CONTINUE; /* carry on with the program! */
|
||||
}
|
||||
|
||||
/* This function runs once per frame, and is the heart of the program. */
|
||||
SDL_AppResult SDL_AppIterate(void *appstate)
|
||||
{
|
||||
int i;
|
||||
|
||||
/* Lines (line segments, really) are drawn in terms of points: a set of
|
||||
X and Y coordinates, one set for each end of the line.
|
||||
(0, 0) is the top left of the window, and larger numbers go down
|
||||
and to the right. This isn't how geometry works, but this is pretty
|
||||
standard in 2D graphics. */
|
||||
static const SDL_FPoint line_points[] = {
|
||||
{ 100, 354 }, { 220, 230 }, { 140, 230 }, { 320, 100 }, { 500, 230 },
|
||||
{ 420, 230 }, { 540, 354 }, { 400, 354 }, { 100, 354 }
|
||||
};
|
||||
|
||||
/* as you can see from this, rendering draws over whatever was drawn before it. */
|
||||
SDL_SetRenderDrawColor(renderer, 100, 100, 100, 255); /* grey, full alpha */
|
||||
SDL_RenderClear(renderer); /* start with a blank canvas. */
|
||||
|
||||
/* You can draw lines, one at a time, like these brown ones... */
|
||||
SDL_SetRenderDrawColor(renderer, 127, 49, 32, 255);
|
||||
SDL_RenderLine(renderer, 240, 450, 400, 450);
|
||||
SDL_RenderLine(renderer, 240, 356, 400, 356);
|
||||
SDL_RenderLine(renderer, 240, 356, 240, 450);
|
||||
SDL_RenderLine(renderer, 400, 356, 400, 450);
|
||||
|
||||
/* You can also draw a series of connected lines in a single batch... */
|
||||
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
|
||||
SDL_RenderLines(renderer, line_points, SDL_arraysize(line_points));
|
||||
|
||||
/* here's a bunch of lines drawn out from a center point in a circle. */
|
||||
/* we randomize the color of each line, so it functions as animation. */
|
||||
for (i = 0; i < 360; i++) {
|
||||
const float size = 30.0f;
|
||||
const float x = 320.0f;
|
||||
const float y = 95.0f - (size / 2.0f);
|
||||
SDL_SetRenderDrawColor(renderer, SDL_rand(256), SDL_rand(256), SDL_rand(256), 255);
|
||||
SDL_RenderLine(renderer, x, y, x + SDL_sinf((float) i) * size, y + SDL_cosf((float) i) * size);
|
||||
}
|
||||
|
||||
SDL_RenderPresent(renderer); /* put it all on the screen! */
|
||||
|
||||
return SDL_APP_CONTINUE; /* carry on with the program! */
|
||||
}
|
||||
|
||||
/* This function runs once at shutdown. */
|
||||
void SDL_AppQuit(void *appstate)
|
||||
{
|
||||
/* SDL will clean up the window/renderer for us. */
|
||||
}
|
||||
|
||||
3
external/SDL/examples/renderer/04-points/README.txt
vendored
Normal file
3
external/SDL/examples/renderer/04-points/README.txt
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
This example creates an SDL window and renderer, and then draws a bunch of
|
||||
single points, moving across the screen.
|
||||
|
||||
118
external/SDL/examples/renderer/04-points/points.c
vendored
Normal file
118
external/SDL/examples/renderer/04-points/points.c
vendored
Normal file
@@ -0,0 +1,118 @@
|
||||
/*
|
||||
* This example creates an SDL window and renderer, and then draws some points
|
||||
* to it every frame.
|
||||
*
|
||||
* This code is public domain. Feel free to use it for any purpose!
|
||||
*/
|
||||
|
||||
#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3/SDL_main.h>
|
||||
|
||||
/* We will use this renderer to draw into this window every frame. */
|
||||
static SDL_Window *window = NULL;
|
||||
static SDL_Renderer *renderer = NULL;
|
||||
static Uint64 last_time = 0;
|
||||
|
||||
#define WINDOW_WIDTH 640
|
||||
#define WINDOW_HEIGHT 480
|
||||
|
||||
#define NUM_POINTS 500
|
||||
#define MIN_PIXELS_PER_SECOND 30 /* move at least this many pixels per second. */
|
||||
#define MAX_PIXELS_PER_SECOND 60 /* move this many pixels per second at most. */
|
||||
|
||||
/* (track everything as parallel arrays instead of a array of structs,
|
||||
so we can pass the coordinates to the renderer in a single function call.) */
|
||||
|
||||
/* Points are plotted as a set of X and Y coordinates.
|
||||
(0, 0) is the top left of the window, and larger numbers go down
|
||||
and to the right. This isn't how geometry works, but this is pretty
|
||||
standard in 2D graphics. */
|
||||
static SDL_FPoint points[NUM_POINTS];
|
||||
static float point_speeds[NUM_POINTS];
|
||||
|
||||
/* This function runs once at startup. */
|
||||
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
|
||||
{
|
||||
int i;
|
||||
|
||||
if (!SDL_Init(SDL_INIT_VIDEO)) {
|
||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't initialize SDL!", SDL_GetError(), NULL);
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
if (!SDL_CreateWindowAndRenderer("examples/renderer/points", WINDOW_WIDTH, WINDOW_HEIGHT, 0, &window, &renderer)) {
|
||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't create window/renderer!", SDL_GetError(), NULL);
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
SDL_srand(0); /* seed the random number generator */
|
||||
|
||||
/* set up the data for a bunch of points. */
|
||||
for (i = 0; i < SDL_arraysize(points); i++) {
|
||||
points[i].x = SDL_randf() * ((float) WINDOW_WIDTH);
|
||||
points[i].y = SDL_randf() * ((float) WINDOW_HEIGHT);
|
||||
point_speeds[i] = MIN_PIXELS_PER_SECOND + (SDL_randf() * (MAX_PIXELS_PER_SECOND - MIN_PIXELS_PER_SECOND));
|
||||
}
|
||||
|
||||
last_time = SDL_GetTicks();
|
||||
|
||||
return SDL_APP_CONTINUE; /* carry on with the program! */
|
||||
}
|
||||
|
||||
/* This function runs when a new event (mouse input, keypresses, etc) occurs. */
|
||||
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
|
||||
{
|
||||
if (event->type == SDL_EVENT_QUIT) {
|
||||
return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
|
||||
}
|
||||
return SDL_APP_CONTINUE; /* carry on with the program! */
|
||||
}
|
||||
|
||||
/* This function runs once per frame, and is the heart of the program. */
|
||||
SDL_AppResult SDL_AppIterate(void *appstate)
|
||||
{
|
||||
const Uint64 now = SDL_GetTicks();
|
||||
const float elapsed = ((float) (now - last_time)) / 1000.0f; /* seconds since last iteration */
|
||||
int i;
|
||||
|
||||
/* let's move all our points a little for a new frame. */
|
||||
for (i = 0; i < SDL_arraysize(points); i++) {
|
||||
const float distance = elapsed * point_speeds[i];
|
||||
points[i].x += distance;
|
||||
points[i].y += distance;
|
||||
if ((points[i].x >= WINDOW_WIDTH) || (points[i].y >= WINDOW_HEIGHT)) {
|
||||
/* off the screen; restart it elsewhere! */
|
||||
if (SDL_rand(2)) {
|
||||
points[i].x = SDL_randf() * ((float) WINDOW_WIDTH);
|
||||
points[i].y = 0.0f;
|
||||
} else {
|
||||
points[i].x = 0.0f;
|
||||
points[i].y = SDL_randf() * ((float) WINDOW_HEIGHT);
|
||||
}
|
||||
point_speeds[i] = MIN_PIXELS_PER_SECOND + (SDL_randf() * (MAX_PIXELS_PER_SECOND - MIN_PIXELS_PER_SECOND));
|
||||
}
|
||||
}
|
||||
|
||||
last_time = now;
|
||||
|
||||
/* as you can see from this, rendering draws over whatever was drawn before it. */
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); /* black, full alpha */
|
||||
SDL_RenderClear(renderer); /* start with a blank canvas. */
|
||||
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); /* white, full alpha */
|
||||
SDL_RenderPoints(renderer, points, SDL_arraysize(points)); /* draw all the points! */
|
||||
|
||||
/* You can also draw single points with SDL_RenderPoint(), but it's
|
||||
cheaper (sometimes significantly so) to do them all at once. */
|
||||
|
||||
SDL_RenderPresent(renderer); /* put it all on the screen! */
|
||||
|
||||
return SDL_APP_CONTINUE; /* carry on with the program! */
|
||||
}
|
||||
|
||||
/* This function runs once at shutdown. */
|
||||
void SDL_AppQuit(void *appstate)
|
||||
{
|
||||
/* SDL will clean up the window/renderer for us. */
|
||||
}
|
||||
|
||||
3
external/SDL/examples/renderer/05-rectangles/README.txt
vendored
Normal file
3
external/SDL/examples/renderer/05-rectangles/README.txt
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
This example creates an SDL window and renderer, and then draws a few
|
||||
rectangles that change size each frame.
|
||||
|
||||
110
external/SDL/examples/renderer/05-rectangles/rectangles.c
vendored
Normal file
110
external/SDL/examples/renderer/05-rectangles/rectangles.c
vendored
Normal file
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* This example creates an SDL window and renderer, and then draws some
|
||||
* rectangles to it every frame.
|
||||
*
|
||||
* This code is public domain. Feel free to use it for any purpose!
|
||||
*/
|
||||
|
||||
#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3/SDL_main.h>
|
||||
|
||||
/* We will use this renderer to draw into this window every frame. */
|
||||
static SDL_Window *window = NULL;
|
||||
static SDL_Renderer *renderer = NULL;
|
||||
|
||||
#define WINDOW_WIDTH 640
|
||||
#define WINDOW_HEIGHT 480
|
||||
|
||||
/* This function runs once at startup. */
|
||||
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
|
||||
{
|
||||
if (!SDL_Init(SDL_INIT_VIDEO)) {
|
||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't initialize SDL!", SDL_GetError(), NULL);
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
if (!SDL_CreateWindowAndRenderer("examples/renderer/rectangles", WINDOW_WIDTH, WINDOW_HEIGHT, 0, &window, &renderer)) {
|
||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't create window/renderer!", SDL_GetError(), NULL);
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
return SDL_APP_CONTINUE; /* carry on with the program! */
|
||||
}
|
||||
|
||||
/* This function runs when a new event (mouse input, keypresses, etc) occurs. */
|
||||
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
|
||||
{
|
||||
if (event->type == SDL_EVENT_QUIT) {
|
||||
return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
|
||||
}
|
||||
return SDL_APP_CONTINUE; /* carry on with the program! */
|
||||
}
|
||||
|
||||
/* This function runs once per frame, and is the heart of the program. */
|
||||
SDL_AppResult SDL_AppIterate(void *appstate)
|
||||
{
|
||||
SDL_FRect rects[16];
|
||||
const Uint64 now = SDL_GetTicks();
|
||||
int i;
|
||||
|
||||
/* we'll have the rectangles grow and shrink over a few seconds. */
|
||||
const float direction = ((now % 2000) >= 1000) ? 1.0f : -1.0f;
|
||||
const float scale = ((float) (((int) (now % 1000)) - 500) / 500.0f) * direction;
|
||||
|
||||
/* as you can see from this, rendering draws over whatever was drawn before it. */
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); /* black, full alpha */
|
||||
SDL_RenderClear(renderer); /* start with a blank canvas. */
|
||||
|
||||
/* Rectangles are comprised of set of X and Y coordinates, plus width and
|
||||
height. (0, 0) is the top left of the window, and larger numbers go
|
||||
down and to the right. This isn't how geometry works, but this is
|
||||
pretty standard in 2D graphics. */
|
||||
|
||||
/* Let's draw a single rectangle (square, really). */
|
||||
rects[0].x = rects[0].y = 100;
|
||||
rects[0].w = rects[0].h = 100 + (100 * scale);
|
||||
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); /* red, full alpha */
|
||||
SDL_RenderRect(renderer, &rects[0]);
|
||||
|
||||
/* Now let's draw several rectangles with one function call. */
|
||||
for (i = 0; i < 3; i++) {
|
||||
const float size = (i+1) * 50.0f;
|
||||
rects[i].w = rects[i].h = size + (size * scale);
|
||||
rects[i].x = (WINDOW_WIDTH - rects[i].w) / 2; /* center it. */
|
||||
rects[i].y = (WINDOW_HEIGHT - rects[i].h) / 2; /* center it. */
|
||||
}
|
||||
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255); /* green, full alpha */
|
||||
SDL_RenderRects(renderer, rects, 3); /* draw three rectangles at once */
|
||||
|
||||
/* those were rectangle _outlines_, really. You can also draw _filled_ rectangles! */
|
||||
rects[0].x = 400;
|
||||
rects[0].y = 50;
|
||||
rects[0].w = 100 + (100 * scale);
|
||||
rects[0].h = 50 + (50 * scale);
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255); /* blue, full alpha */
|
||||
SDL_RenderFillRect(renderer, &rects[0]);
|
||||
|
||||
/* ...and also fill a bunch of rectangles at once... */
|
||||
for (i = 0; i < SDL_arraysize(rects); i++) {
|
||||
const float w = (float) (WINDOW_WIDTH / SDL_arraysize(rects));
|
||||
const float h = i * 8.0f;
|
||||
rects[i].x = i * w;
|
||||
rects[i].y = WINDOW_HEIGHT - h;
|
||||
rects[i].w = w;
|
||||
rects[i].h = h;
|
||||
}
|
||||
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); /* white, full alpha */
|
||||
SDL_RenderFillRects(renderer, rects, SDL_arraysize(rects));
|
||||
|
||||
SDL_RenderPresent(renderer); /* put it all on the screen! */
|
||||
|
||||
return SDL_APP_CONTINUE; /* carry on with the program! */
|
||||
}
|
||||
|
||||
/* This function runs once at shutdown. */
|
||||
void SDL_AppQuit(void *appstate)
|
||||
{
|
||||
/* SDL will clean up the window/renderer for us. */
|
||||
}
|
||||
|
||||
3
external/SDL/examples/renderer/06-textures/README.txt
vendored
Normal file
3
external/SDL/examples/renderer/06-textures/README.txt
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
This example creates an SDL window and renderer, loads a texture from a
|
||||
.bmp file, and then draws it a few times each frame.
|
||||
|
||||
125
external/SDL/examples/renderer/06-textures/textures.c
vendored
Normal file
125
external/SDL/examples/renderer/06-textures/textures.c
vendored
Normal file
@@ -0,0 +1,125 @@
|
||||
/*
|
||||
* This example creates an SDL window and renderer, and then draws some
|
||||
* textures to it every frame.
|
||||
*
|
||||
* This code is public domain. Feel free to use it for any purpose!
|
||||
*/
|
||||
|
||||
#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3/SDL_main.h>
|
||||
|
||||
/* We will use this renderer to draw into this window every frame. */
|
||||
static SDL_Window *window = NULL;
|
||||
static SDL_Renderer *renderer = NULL;
|
||||
static SDL_Texture *texture = NULL;
|
||||
static int texture_width = 0;
|
||||
static int texture_height = 0;
|
||||
|
||||
#define WINDOW_WIDTH 640
|
||||
#define WINDOW_HEIGHT 480
|
||||
|
||||
/* This function runs once at startup. */
|
||||
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
|
||||
{
|
||||
SDL_Surface *surface = NULL;
|
||||
char *bmp_path = NULL;
|
||||
|
||||
if (!SDL_Init(SDL_INIT_VIDEO)) {
|
||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't initialize SDL!", SDL_GetError(), NULL);
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
if (!SDL_CreateWindowAndRenderer("examples/renderer/textures", WINDOW_WIDTH, WINDOW_HEIGHT, 0, &window, &renderer)) {
|
||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't create window/renderer!", SDL_GetError(), NULL);
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
/* Textures are pixel data that we upload to the video hardware for fast drawing. Lots of 2D
|
||||
engines refer to these as "sprites." We'll do a static texture (upload once, draw many
|
||||
times) with data from a bitmap file. */
|
||||
|
||||
/* SDL_Surface is pixel data the CPU can access. SDL_Texture is pixel data the GPU can access.
|
||||
Load a .bmp into a surface, move it to a texture from there. */
|
||||
SDL_asprintf(&bmp_path, "%ssample.bmp", SDL_GetBasePath()); /* allocate a string of the full file path */
|
||||
surface = SDL_LoadBMP(bmp_path);
|
||||
if (!surface) {
|
||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't load bitmap!", SDL_GetError(), NULL);
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
SDL_free(bmp_path); /* done with this, the file is loaded. */
|
||||
|
||||
texture_width = surface->w;
|
||||
texture_height = surface->h;
|
||||
|
||||
texture = SDL_CreateTextureFromSurface(renderer, surface);
|
||||
if (!texture) {
|
||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't create static texture!", SDL_GetError(), NULL);
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
SDL_DestroySurface(surface); /* done with this, the texture has a copy of the pixels now. */
|
||||
|
||||
return SDL_APP_CONTINUE; /* carry on with the program! */
|
||||
}
|
||||
|
||||
/* This function runs when a new event (mouse input, keypresses, etc) occurs. */
|
||||
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
|
||||
{
|
||||
if (event->type == SDL_EVENT_QUIT) {
|
||||
return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
|
||||
}
|
||||
return SDL_APP_CONTINUE; /* carry on with the program! */
|
||||
}
|
||||
|
||||
/* This function runs once per frame, and is the heart of the program. */
|
||||
SDL_AppResult SDL_AppIterate(void *appstate)
|
||||
{
|
||||
SDL_FRect dst_rect;
|
||||
const Uint64 now = SDL_GetTicks();
|
||||
|
||||
/* we'll have some textures move around over a few seconds. */
|
||||
const float direction = ((now % 2000) >= 1000) ? 1.0f : -1.0f;
|
||||
const float scale = ((float) (((int) (now % 1000)) - 500) / 500.0f) * direction;
|
||||
|
||||
/* as you can see from this, rendering draws over whatever was drawn before it. */
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); /* black, full alpha */
|
||||
SDL_RenderClear(renderer); /* start with a blank canvas. */
|
||||
|
||||
/* Just draw the static texture a few times. You can think of it like a
|
||||
stamp, there isn't a limit to the number of times you can draw with it. */
|
||||
|
||||
/* top left */
|
||||
dst_rect.x = (100.0f * scale);
|
||||
dst_rect.y = 0.0f;
|
||||
dst_rect.w = (float) texture_width;
|
||||
dst_rect.h = (float) texture_height;
|
||||
SDL_RenderTexture(renderer, texture, NULL, &dst_rect);
|
||||
|
||||
/* center this one. */
|
||||
dst_rect.x = ((float) (WINDOW_WIDTH - texture_width)) / 2.0f;
|
||||
dst_rect.y = ((float) (WINDOW_HEIGHT - texture_height)) / 2.0f;
|
||||
dst_rect.w = (float) texture_width;
|
||||
dst_rect.h = (float) texture_height;
|
||||
SDL_RenderTexture(renderer, texture, NULL, &dst_rect);
|
||||
|
||||
/* bottom right. */
|
||||
dst_rect.x = ((float) (WINDOW_WIDTH - texture_width)) - (100.0f * scale);
|
||||
dst_rect.y = (float) (WINDOW_HEIGHT - texture_height);
|
||||
dst_rect.w = (float) texture_width;
|
||||
dst_rect.h = (float) texture_height;
|
||||
SDL_RenderTexture(renderer, texture, NULL, &dst_rect);
|
||||
|
||||
SDL_RenderPresent(renderer); /* put it all on the screen! */
|
||||
|
||||
return SDL_APP_CONTINUE; /* carry on with the program! */
|
||||
}
|
||||
|
||||
/* This function runs once at shutdown. */
|
||||
void SDL_AppQuit(void *appstate)
|
||||
{
|
||||
SDL_DestroyTexture(texture);
|
||||
/* SDL will clean up the window/renderer for us. */
|
||||
}
|
||||
|
||||
3
external/SDL/examples/renderer/07-streaming-textures/README.txt
vendored
Normal file
3
external/SDL/examples/renderer/07-streaming-textures/README.txt
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
This example creates an SDL window and renderer, then a streaming texture that
|
||||
it will update every frame before drawing it to the screen.
|
||||
|
||||
107
external/SDL/examples/renderer/07-streaming-textures/streaming-textures.c
vendored
Normal file
107
external/SDL/examples/renderer/07-streaming-textures/streaming-textures.c
vendored
Normal file
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* This example creates an SDL window and renderer, and then draws a streaming
|
||||
* texture to it every frame.
|
||||
*
|
||||
* This code is public domain. Feel free to use it for any purpose!
|
||||
*/
|
||||
|
||||
#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3/SDL_main.h>
|
||||
|
||||
/* We will use this renderer to draw into this window every frame. */
|
||||
static SDL_Window *window = NULL;
|
||||
static SDL_Renderer *renderer = NULL;
|
||||
static SDL_Texture *texture = NULL;
|
||||
|
||||
#define TEXTURE_SIZE 150
|
||||
|
||||
#define WINDOW_WIDTH 640
|
||||
#define WINDOW_HEIGHT 480
|
||||
|
||||
/* This function runs once at startup. */
|
||||
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
|
||||
{
|
||||
if (!SDL_Init(SDL_INIT_VIDEO)) {
|
||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't initialize SDL!", SDL_GetError(), NULL);
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
if (!SDL_CreateWindowAndRenderer("examples/renderer/streaming-textures", WINDOW_WIDTH, WINDOW_HEIGHT, 0, &window, &renderer)) {
|
||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't create window/renderer!", SDL_GetError(), NULL);
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, TEXTURE_SIZE, TEXTURE_SIZE);
|
||||
if (!texture) {
|
||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't create streaming texture!", SDL_GetError(), NULL);
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
return SDL_APP_CONTINUE; /* carry on with the program! */
|
||||
}
|
||||
|
||||
/* This function runs when a new event (mouse input, keypresses, etc) occurs. */
|
||||
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
|
||||
{
|
||||
if (event->type == SDL_EVENT_QUIT) {
|
||||
return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
|
||||
}
|
||||
return SDL_APP_CONTINUE; /* carry on with the program! */
|
||||
}
|
||||
|
||||
/* This function runs once per frame, and is the heart of the program. */
|
||||
SDL_AppResult SDL_AppIterate(void *appstate)
|
||||
{
|
||||
SDL_FRect dst_rect;
|
||||
const Uint64 now = SDL_GetTicks();
|
||||
SDL_Surface *surface = NULL;
|
||||
|
||||
/* we'll have some color move around over a few seconds. */
|
||||
const float direction = ((now % 2000) >= 1000) ? 1.0f : -1.0f;
|
||||
const float scale = ((float) (((int) (now % 1000)) - 500) / 500.0f) * direction;
|
||||
|
||||
/* To update a streaming texture, you need to lock it first. This gets you access to the pixels.
|
||||
Note that this is considered a _write-only_ operation: the buffer you get from locking
|
||||
might not acutally have the existing contents of the texture, and you have to write to every
|
||||
locked pixel! */
|
||||
|
||||
/* You can use SDL_LockTexture() to get an array of raw pixels, but we're going to use
|
||||
SDL_LockTextureToSurface() here, because it wraps that array in a temporary SDL_Surface,
|
||||
letting us use the surface drawing functions instead of lighting up individual pixels. */
|
||||
if (SDL_LockTextureToSurface(texture, NULL, &surface)) {
|
||||
SDL_Rect r;
|
||||
SDL_FillSurfaceRect(surface, NULL, SDL_MapRGB(SDL_GetPixelFormatDetails(surface->format), NULL, 0, 0, 0)); /* make the whole surface black */
|
||||
r.w = TEXTURE_SIZE;
|
||||
r.h = TEXTURE_SIZE / 10;
|
||||
r.x = 0;
|
||||
r.y = (int) (((float) (TEXTURE_SIZE - r.h)) * ((scale + 1.0f) / 2.0f));
|
||||
SDL_FillSurfaceRect(surface, &r, SDL_MapRGB(SDL_GetPixelFormatDetails(surface->format), NULL, 0, 255, 0)); /* make a strip of the surface green */
|
||||
SDL_UnlockTexture(texture); /* upload the changes (and frees the temporary surface)! */
|
||||
}
|
||||
|
||||
/* as you can see from this, rendering draws over whatever was drawn before it. */
|
||||
SDL_SetRenderDrawColor(renderer, 66, 66, 66, 255); /* grey, full alpha */
|
||||
SDL_RenderClear(renderer); /* start with a blank canvas. */
|
||||
|
||||
/* Just draw the static texture a few times. You can think of it like a
|
||||
stamp, there isn't a limit to the number of times you can draw with it. */
|
||||
|
||||
/* Center this one. It'll draw the latest version of the texture we drew while it was locked. */
|
||||
dst_rect.x = ((float) (WINDOW_WIDTH - TEXTURE_SIZE)) / 2.0f;
|
||||
dst_rect.y = ((float) (WINDOW_HEIGHT - TEXTURE_SIZE)) / 2.0f;
|
||||
dst_rect.w = dst_rect.h = (float) TEXTURE_SIZE;
|
||||
SDL_RenderTexture(renderer, texture, NULL, &dst_rect);
|
||||
|
||||
SDL_RenderPresent(renderer); /* put it all on the screen! */
|
||||
|
||||
return SDL_APP_CONTINUE; /* carry on with the program! */
|
||||
}
|
||||
|
||||
/* This function runs once at shutdown. */
|
||||
void SDL_AppQuit(void *appstate)
|
||||
{
|
||||
SDL_DestroyTexture(texture);
|
||||
/* SDL will clean up the window/renderer for us. */
|
||||
}
|
||||
|
||||
3
external/SDL/examples/renderer/08-rotating-textures/README.txt
vendored
Normal file
3
external/SDL/examples/renderer/08-rotating-textures/README.txt
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
This example creates an SDL window and renderer, loads a texture from a .bmp
|
||||
file, and then draws it, rotating around the center of the screen.
|
||||
|
||||
111
external/SDL/examples/renderer/08-rotating-textures/rotating-textures.c
vendored
Normal file
111
external/SDL/examples/renderer/08-rotating-textures/rotating-textures.c
vendored
Normal file
@@ -0,0 +1,111 @@
|
||||
/*
|
||||
* This example creates an SDL window and renderer, and then draws some
|
||||
* rotated textures to it every frame.
|
||||
*
|
||||
* This code is public domain. Feel free to use it for any purpose!
|
||||
*/
|
||||
|
||||
#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3/SDL_main.h>
|
||||
|
||||
/* We will use this renderer to draw into this window every frame. */
|
||||
static SDL_Window *window = NULL;
|
||||
static SDL_Renderer *renderer = NULL;
|
||||
static SDL_Texture *texture = NULL;
|
||||
static int texture_width = 0;
|
||||
static int texture_height = 0;
|
||||
|
||||
#define WINDOW_WIDTH 640
|
||||
#define WINDOW_HEIGHT 480
|
||||
|
||||
/* This function runs once at startup. */
|
||||
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
|
||||
{
|
||||
SDL_Surface *surface = NULL;
|
||||
char *bmp_path = NULL;
|
||||
|
||||
if (!SDL_Init(SDL_INIT_VIDEO)) {
|
||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't initialize SDL!", SDL_GetError(), NULL);
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
if (!SDL_CreateWindowAndRenderer("examples/renderer/rotating-textures", WINDOW_WIDTH, WINDOW_HEIGHT, 0, &window, &renderer)) {
|
||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't create window/renderer!", SDL_GetError(), NULL);
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
/* Textures are pixel data that we upload to the video hardware for fast drawing. Lots of 2D
|
||||
engines refer to these as "sprites." We'll do a static texture (upload once, draw many
|
||||
times) with data from a bitmap file. */
|
||||
|
||||
/* SDL_Surface is pixel data the CPU can access. SDL_Texture is pixel data the GPU can access.
|
||||
Load a .bmp into a surface, move it to a texture from there. */
|
||||
SDL_asprintf(&bmp_path, "%ssample.bmp", SDL_GetBasePath()); /* allocate a string of the full file path */
|
||||
surface = SDL_LoadBMP(bmp_path);
|
||||
if (!surface) {
|
||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't load bitmap!", SDL_GetError(), NULL);
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
SDL_free(bmp_path); /* done with this, the file is loaded. */
|
||||
|
||||
texture_width = surface->w;
|
||||
texture_height = surface->h;
|
||||
|
||||
texture = SDL_CreateTextureFromSurface(renderer, surface);
|
||||
if (!texture) {
|
||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't create static texture!", SDL_GetError(), NULL);
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
SDL_DestroySurface(surface); /* done with this, the texture has a copy of the pixels now. */
|
||||
|
||||
return SDL_APP_CONTINUE; /* carry on with the program! */
|
||||
}
|
||||
|
||||
/* This function runs when a new event (mouse input, keypresses, etc) occurs. */
|
||||
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
|
||||
{
|
||||
if (event->type == SDL_EVENT_QUIT) {
|
||||
return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
|
||||
}
|
||||
return SDL_APP_CONTINUE; /* carry on with the program! */
|
||||
}
|
||||
|
||||
/* This function runs once per frame, and is the heart of the program. */
|
||||
SDL_AppResult SDL_AppIterate(void *appstate)
|
||||
{
|
||||
SDL_FPoint center;
|
||||
SDL_FRect dst_rect;
|
||||
const Uint64 now = SDL_GetTicks();
|
||||
|
||||
/* we'll have a texture rotate around over 2 seconds (2000 milliseconds). 360 degrees in a circle! */
|
||||
const float rotation = (((float) ((int) (now % 2000))) / 2000.0f) * 360.0f;
|
||||
|
||||
/* as you can see from this, rendering draws over whatever was drawn before it. */
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); /* black, full alpha */
|
||||
SDL_RenderClear(renderer); /* start with a blank canvas. */
|
||||
|
||||
/* Center this one, and draw it with some rotation so it spins! */
|
||||
dst_rect.x = ((float) (WINDOW_WIDTH - texture_width)) / 2.0f;
|
||||
dst_rect.y = ((float) (WINDOW_HEIGHT - texture_height)) / 2.0f;
|
||||
dst_rect.w = (float) texture_width;
|
||||
dst_rect.h = (float) texture_height;
|
||||
/* rotate it around the center of the texture; you can rotate it from a different point, too! */
|
||||
center.x = texture_width / 2.0f;
|
||||
center.y = texture_height / 2.0f;
|
||||
SDL_RenderTextureRotated(renderer, texture, NULL, &dst_rect, rotation, ¢er, SDL_FLIP_NONE);
|
||||
|
||||
SDL_RenderPresent(renderer); /* put it all on the screen! */
|
||||
|
||||
return SDL_APP_CONTINUE; /* carry on with the program! */
|
||||
}
|
||||
|
||||
/* This function runs once at shutdown. */
|
||||
void SDL_AppQuit(void *appstate)
|
||||
{
|
||||
SDL_DestroyTexture(texture);
|
||||
/* SDL will clean up the window/renderer for us. */
|
||||
}
|
||||
|
||||
3
external/SDL/examples/renderer/09-scaling-textures/README.txt
vendored
Normal file
3
external/SDL/examples/renderer/09-scaling-textures/README.txt
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
This example creates an SDL window and renderer, loads a texture from a .bmp
|
||||
file, and then draws it, scaling it up and down.
|
||||
|
||||
108
external/SDL/examples/renderer/09-scaling-textures/scaling-textures.c
vendored
Normal file
108
external/SDL/examples/renderer/09-scaling-textures/scaling-textures.c
vendored
Normal file
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* This example creates an SDL window and renderer, and then draws some
|
||||
* textures to it every frame.
|
||||
*
|
||||
* This code is public domain. Feel free to use it for any purpose!
|
||||
*/
|
||||
|
||||
#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3/SDL_main.h>
|
||||
|
||||
/* We will use this renderer to draw into this window every frame. */
|
||||
static SDL_Window *window = NULL;
|
||||
static SDL_Renderer *renderer = NULL;
|
||||
static SDL_Texture *texture = NULL;
|
||||
static int texture_width = 0;
|
||||
static int texture_height = 0;
|
||||
|
||||
#define WINDOW_WIDTH 640
|
||||
#define WINDOW_HEIGHT 480
|
||||
|
||||
/* This function runs once at startup. */
|
||||
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
|
||||
{
|
||||
SDL_Surface *surface = NULL;
|
||||
char *bmp_path = NULL;
|
||||
|
||||
if (!SDL_Init(SDL_INIT_VIDEO)) {
|
||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't initialize SDL!", SDL_GetError(), NULL);
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
if (!SDL_CreateWindowAndRenderer("examples/renderer/scaling-textures", WINDOW_WIDTH, WINDOW_HEIGHT, 0, &window, &renderer)) {
|
||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't create window/renderer!", SDL_GetError(), NULL);
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
/* Textures are pixel data that we upload to the video hardware for fast drawing. Lots of 2D
|
||||
engines refer to these as "sprites." We'll do a static texture (upload once, draw many
|
||||
times) with data from a bitmap file. */
|
||||
|
||||
/* SDL_Surface is pixel data the CPU can access. SDL_Texture is pixel data the GPU can access.
|
||||
Load a .bmp into a surface, move it to a texture from there. */
|
||||
SDL_asprintf(&bmp_path, "%ssample.bmp", SDL_GetBasePath()); /* allocate a string of the full file path */
|
||||
surface = SDL_LoadBMP(bmp_path);
|
||||
if (!surface) {
|
||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't load bitmap!", SDL_GetError(), NULL);
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
SDL_free(bmp_path); /* done with this, the file is loaded. */
|
||||
|
||||
texture_width = surface->w;
|
||||
texture_height = surface->h;
|
||||
|
||||
texture = SDL_CreateTextureFromSurface(renderer, surface);
|
||||
if (!texture) {
|
||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't create static texture!", SDL_GetError(), NULL);
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
SDL_DestroySurface(surface); /* done with this, the texture has a copy of the pixels now. */
|
||||
|
||||
return SDL_APP_CONTINUE; /* carry on with the program! */
|
||||
}
|
||||
|
||||
/* This function runs when a new event (mouse input, keypresses, etc) occurs. */
|
||||
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
|
||||
{
|
||||
if (event->type == SDL_EVENT_QUIT) {
|
||||
return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
|
||||
}
|
||||
return SDL_APP_CONTINUE; /* carry on with the program! */
|
||||
}
|
||||
|
||||
/* This function runs once per frame, and is the heart of the program. */
|
||||
SDL_AppResult SDL_AppIterate(void *appstate)
|
||||
{
|
||||
SDL_FRect dst_rect;
|
||||
const Uint64 now = SDL_GetTicks();
|
||||
|
||||
/* we'll have the texture grow and shrink over a few seconds. */
|
||||
const float direction = ((now % 2000) >= 1000) ? 1.0f : -1.0f;
|
||||
const float scale = ((float) (((int) (now % 1000)) - 500) / 500.0f) * direction;
|
||||
|
||||
/* as you can see from this, rendering draws over whatever was drawn before it. */
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); /* black, full alpha */
|
||||
SDL_RenderClear(renderer); /* start with a blank canvas. */
|
||||
|
||||
/* center this one and make it grow and shrink. */
|
||||
dst_rect.w = (float) texture_width + (texture_width * scale);
|
||||
dst_rect.h = (float) texture_height + (texture_height * scale);
|
||||
dst_rect.x = ((float) (WINDOW_WIDTH - dst_rect.w)) / 2.0f;
|
||||
dst_rect.y = ((float) (WINDOW_HEIGHT - dst_rect.h)) / 2.0f;
|
||||
SDL_RenderTexture(renderer, texture, NULL, &dst_rect);
|
||||
|
||||
SDL_RenderPresent(renderer); /* put it all on the screen! */
|
||||
|
||||
return SDL_APP_CONTINUE; /* carry on with the program! */
|
||||
}
|
||||
|
||||
/* This function runs once at shutdown. */
|
||||
void SDL_AppQuit(void *appstate)
|
||||
{
|
||||
SDL_DestroyTexture(texture);
|
||||
/* SDL will clean up the window/renderer for us. */
|
||||
}
|
||||
|
||||
3
external/SDL/examples/renderer/10-geometry/README.txt
vendored
Normal file
3
external/SDL/examples/renderer/10-geometry/README.txt
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
This example creates an SDL window and renderer, loads a texture from a .bmp
|
||||
file, and then draws geometry (arbitrary polygons) using it.
|
||||
|
||||
164
external/SDL/examples/renderer/10-geometry/geometry.c
vendored
Normal file
164
external/SDL/examples/renderer/10-geometry/geometry.c
vendored
Normal file
@@ -0,0 +1,164 @@
|
||||
/*
|
||||
* This example creates an SDL window and renderer, and then draws some
|
||||
* geometry (arbitrary polygons) to it every frame.
|
||||
*
|
||||
* This code is public domain. Feel free to use it for any purpose!
|
||||
*/
|
||||
|
||||
#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3/SDL_main.h>
|
||||
|
||||
/* We will use this renderer to draw into this window every frame. */
|
||||
static SDL_Window *window = NULL;
|
||||
static SDL_Renderer *renderer = NULL;
|
||||
static SDL_Texture *texture = NULL;
|
||||
static int texture_width = 0;
|
||||
static int texture_height = 0;
|
||||
|
||||
#define WINDOW_WIDTH 640
|
||||
#define WINDOW_HEIGHT 480
|
||||
|
||||
/* This function runs once at startup. */
|
||||
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
|
||||
{
|
||||
SDL_Surface *surface = NULL;
|
||||
char *bmp_path = NULL;
|
||||
|
||||
if (!SDL_Init(SDL_INIT_VIDEO)) {
|
||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't initialize SDL!", SDL_GetError(), NULL);
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
if (!SDL_CreateWindowAndRenderer("examples/renderer/geometry", WINDOW_WIDTH, WINDOW_HEIGHT, 0, &window, &renderer)) {
|
||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't create window/renderer!", SDL_GetError(), NULL);
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
/* Textures are pixel data that we upload to the video hardware for fast drawing. Lots of 2D
|
||||
engines refer to these as "sprites." We'll do a static texture (upload once, draw many
|
||||
times) with data from a bitmap file. */
|
||||
|
||||
/* SDL_Surface is pixel data the CPU can access. SDL_Texture is pixel data the GPU can access.
|
||||
Load a .bmp into a surface, move it to a texture from there. */
|
||||
SDL_asprintf(&bmp_path, "%ssample.bmp", SDL_GetBasePath()); /* allocate a string of the full file path */
|
||||
surface = SDL_LoadBMP(bmp_path);
|
||||
if (!surface) {
|
||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't load bitmap!", SDL_GetError(), NULL);
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
SDL_free(bmp_path); /* done with this, the file is loaded. */
|
||||
|
||||
texture_width = surface->w;
|
||||
texture_height = surface->h;
|
||||
|
||||
texture = SDL_CreateTextureFromSurface(renderer, surface);
|
||||
if (!texture) {
|
||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't create static texture!", SDL_GetError(), NULL);
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
SDL_DestroySurface(surface); /* done with this, the texture has a copy of the pixels now. */
|
||||
|
||||
return SDL_APP_CONTINUE; /* carry on with the program! */
|
||||
}
|
||||
|
||||
/* This function runs when a new event (mouse input, keypresses, etc) occurs. */
|
||||
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
|
||||
{
|
||||
if (event->type == SDL_EVENT_QUIT) {
|
||||
return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
|
||||
}
|
||||
return SDL_APP_CONTINUE; /* carry on with the program! */
|
||||
}
|
||||
|
||||
/* This function runs once per frame, and is the heart of the program. */
|
||||
SDL_AppResult SDL_AppIterate(void *appstate)
|
||||
{
|
||||
const Uint64 now = SDL_GetTicks();
|
||||
|
||||
/* we'll have the triangle grow and shrink over a few seconds. */
|
||||
const float direction = ((now % 2000) >= 1000) ? 1.0f : -1.0f;
|
||||
const float scale = ((float) (((int) (now % 1000)) - 500) / 500.0f) * direction;
|
||||
const float size = 200.0f + (200.0f * scale);
|
||||
|
||||
SDL_Vertex vertices[4];
|
||||
int i;
|
||||
|
||||
/* as you can see from this, rendering draws over whatever was drawn before it. */
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); /* black, full alpha */
|
||||
SDL_RenderClear(renderer); /* start with a blank canvas. */
|
||||
|
||||
/* Draw a single triangle with a different color at each vertex. Center this one and make it grow and shrink. */
|
||||
/* You always draw triangles with this, but you can string triangles together to form polygons. */
|
||||
SDL_zeroa(vertices);
|
||||
vertices[0].position.x = ((float) WINDOW_WIDTH) / 2.0f;
|
||||
vertices[0].position.y = (((float) WINDOW_HEIGHT) - size) / 2.0f;
|
||||
vertices[0].color.r = 1.0f;
|
||||
vertices[0].color.a = 1.0f;
|
||||
vertices[1].position.x = (((float) WINDOW_WIDTH) + size) / 2.0f;
|
||||
vertices[1].position.y = (((float) WINDOW_HEIGHT) + size) / 2.0f;
|
||||
vertices[1].color.g = 1.0f;
|
||||
vertices[1].color.a = 1.0f;
|
||||
vertices[2].position.x = (((float) WINDOW_WIDTH) - size) / 2.0f;
|
||||
vertices[2].position.y = (((float) WINDOW_HEIGHT) + size) / 2.0f;
|
||||
vertices[2].color.b = 1.0f;
|
||||
vertices[2].color.a = 1.0f;
|
||||
|
||||
SDL_RenderGeometry(renderer, NULL, vertices, 3, NULL, 0);
|
||||
|
||||
/* you can also map a texture to the geometry! Texture coordinates go from 0.0f to 1.0f. That will be the location
|
||||
in the texture bound to this vertex. */
|
||||
SDL_zeroa(vertices);
|
||||
vertices[0].position.x = 10.0f;
|
||||
vertices[0].position.y = 10.0f;
|
||||
vertices[0].color.r = vertices[0].color.g = vertices[0].color.b = vertices[0].color.a = 1.0f;
|
||||
vertices[0].tex_coord.x = 0.0f;
|
||||
vertices[0].tex_coord.y = 0.0f;
|
||||
vertices[1].position.x = 150.0f;
|
||||
vertices[1].position.y = 10.0f;
|
||||
vertices[1].color.r = vertices[1].color.g = vertices[1].color.b = vertices[1].color.a = 1.0f;
|
||||
vertices[1].tex_coord.x = 1.0f;
|
||||
vertices[1].tex_coord.y = 0.0f;
|
||||
vertices[2].position.x = 10.0f;
|
||||
vertices[2].position.y = 150.0f;
|
||||
vertices[2].color.r = vertices[2].color.g = vertices[2].color.b = vertices[2].color.a = 1.0f;
|
||||
vertices[2].tex_coord.x = 0.0f;
|
||||
vertices[2].tex_coord.y = 1.0f;
|
||||
SDL_RenderGeometry(renderer, texture, vertices, 3, NULL, 0);
|
||||
|
||||
/* Did that only draw half of the texture? You can do multiple triangles sharing some vertices,
|
||||
using indices, to get the whole thing on the screen: */
|
||||
|
||||
/* Let's just move this over so it doesn't overlap... */
|
||||
for (i = 0; i < 3; i++) {
|
||||
vertices[i].position.x += 450;
|
||||
}
|
||||
|
||||
/* we need one more vertex, since the two triangles can share two of them. */
|
||||
vertices[3].position.x = 600.0f;
|
||||
vertices[3].position.y = 150.0f;
|
||||
vertices[3].color.r = vertices[0].color.g = vertices[0].color.b = vertices[0].color.a = 1.0f;
|
||||
vertices[3].tex_coord.x = 1.0f;
|
||||
vertices[3].tex_coord.y = 1.0f;
|
||||
|
||||
/* And an index to tell it to reuse some of the vertices between triangles... */
|
||||
{
|
||||
/* 4 vertices, but 6 actual places they used. Indices need less bandwidth to transfer and can reorder vertices easily! */
|
||||
const int indices[] = { 0, 1, 2, 1, 2, 3 };
|
||||
SDL_RenderGeometry(renderer, texture, vertices, 4, indices, SDL_arraysize(indices));
|
||||
}
|
||||
|
||||
SDL_RenderPresent(renderer); /* put it all on the screen! */
|
||||
|
||||
return SDL_APP_CONTINUE; /* carry on with the program! */
|
||||
}
|
||||
|
||||
/* This function runs once at shutdown. */
|
||||
void SDL_AppQuit(void *appstate)
|
||||
{
|
||||
SDL_DestroyTexture(texture);
|
||||
/* SDL will clean up the window/renderer for us. */
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user