123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- /*
- * 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[])
- {
- SDL_SetAppMetadata("Example Renderer Lines", "1.0", "com.example.renderer-lines");
- if (!SDL_Init(SDL_INIT_VIDEO)) {
- SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
- return SDL_APP_FAILURE;
- }
- if (!SDL_CreateWindowAndRenderer("examples/renderer/lines", 640, 480, 0, &window, &renderer)) {
- SDL_Log("Couldn't create window/renderer: %s", SDL_GetError());
- 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)
- {
- 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, SDL_ALPHA_OPAQUE); /* 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, SDL_ALPHA_OPAQUE);
- 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, SDL_ALPHA_OPAQUE);
- 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), SDL_ALPHA_OPAQUE);
- 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_AppResult result)
- {
- /* SDL will clean up the window/renderer for us. */
- }
|