|
|
|
@@ -0,0 +1,81 @@
|
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
using SDL3;
|
|
|
|
|
|
|
|
|
|
namespace SDL3.Example;
|
|
|
|
|
|
|
|
|
|
class Program
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* This example code creates an SDL window and renderer, and then clears the
|
|
|
|
|
* window to a different color every frame, so you'll effectively get a window
|
|
|
|
|
* that's smoothly fading between colors.
|
|
|
|
|
*
|
|
|
|
|
* This code is public domain. Feel free to use it for any purpose!
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* 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. */
|
|
|
|
|
static SDL.AppResult AppInit(void **appstate, int32 argc, char8 **argv)
|
|
|
|
|
{
|
|
|
|
|
SDL.SetAppMetadata("Example Renderer Clear", "1.0", "com.example.renderer-clear");
|
|
|
|
|
|
|
|
|
|
if (!SDL.Init(.Video)) {
|
|
|
|
|
SDL.Log("Couldn't initialize SDL: %s", SDL.GetError());
|
|
|
|
|
return .Failure;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!SDL.CreateWindowAndRenderer("examples/renderer/clear", 640, 480, .Resizable, &window, &renderer)) {
|
|
|
|
|
SDL.Log("Couldn't create window/renderer: %s", SDL.GetError());
|
|
|
|
|
return .Failure;
|
|
|
|
|
}
|
|
|
|
|
SDL.SetRenderLogicalPresentation(renderer, 640, 480, .Letterbox);
|
|
|
|
|
|
|
|
|
|
return .Continue; /* carry on with the program! */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* This function runs when a new event (mouse input, keypresses, etc) occurs. */
|
|
|
|
|
static SDL.AppResult AppEvent(void *appstate, SDL.Event *event)
|
|
|
|
|
{
|
|
|
|
|
if (event.type == (.)SDL.EventType.Quit) {
|
|
|
|
|
return .Success; /* end the program, reporting success to the OS. */
|
|
|
|
|
}
|
|
|
|
|
return .Continue; /* carry on with the program! */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* This function runs once per frame, and is the heart of the program. */
|
|
|
|
|
static SDL.AppResult AppIterate(void *appstate)
|
|
|
|
|
{
|
|
|
|
|
double now = ((double)SDL.GetTicks()) / 1000.0; /* convert from milliseconds to seconds. */
|
|
|
|
|
/* choose the color for the frame we will draw. The sine wave trick makes it fade between colors smoothly. */
|
|
|
|
|
float red = (float) (0.5 + 0.5 * SDL.sin(now));
|
|
|
|
|
float green = (float) (0.5 + 0.5 * SDL.sin(now + SDL.PI_D * 2 / 3));
|
|
|
|
|
float blue = (float) (0.5 + 0.5 * SDL.sin(now + SDL.PI_D * 4 / 3));
|
|
|
|
|
SDL.SetRenderDrawColorFloat(renderer, red, green, blue, SDL.ALPHA_OPAQUE_FLOAT); /* new color, full alpha. */
|
|
|
|
|
|
|
|
|
|
/* clear the window to the draw color. */
|
|
|
|
|
SDL.RenderClear(renderer);
|
|
|
|
|
|
|
|
|
|
/* put the newly-cleared rendering on the screen. */
|
|
|
|
|
SDL.RenderPresent(renderer);
|
|
|
|
|
|
|
|
|
|
return .Continue; /* carry on with the program! */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* This function runs once at shutdown. */
|
|
|
|
|
static void AppQuit(void *appstate, SDL.AppResult result)
|
|
|
|
|
{
|
|
|
|
|
/* SDL will clean up the window/renderer for us. */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int Main(String[] args)
|
|
|
|
|
{
|
|
|
|
|
char8*[] argv = scope .[args.Count];
|
|
|
|
|
for (var arg in ref argv)
|
|
|
|
|
arg = args[@arg].Ptr;
|
|
|
|
|
return SDL.EnterAppMainCallbacks((.)argv.Count, argv.Ptr, => AppInit, => AppIterate, => AppEvent, => AppQuit);
|
|
|
|
|
}
|
|
|
|
|
}
|