diff --git a/samples/Directory.Build.props b/samples/Directory.Build.props new file mode 100644 index 0000000..b5555ad --- /dev/null +++ b/samples/Directory.Build.props @@ -0,0 +1,12 @@ + + + + + $(DefaultNetVersion) + release + + + + + + diff --git a/samples/graphics/clear.cs b/samples/graphics/clear.cs new file mode 100644 index 0000000..e7ba506 --- /dev/null +++ b/samples/graphics/clear.cs @@ -0,0 +1,56 @@ +#!/usr/bin/env dotnet + +// Copyright (c) KappaDuck. +// Licensed under the MIT license. + +// This example code creates a 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. + +using KappaDuck.Quack.Core; +using KappaDuck.Quack.Events; +using KappaDuck.Quack.Graphics.Rendering; +using KappaDuck.Quack.Video.Pixels; +using KappaDuck.Quack.Windows; +using System.Diagnostics; + +// Initialize the engine with metadata and the Video Subsystem +QuackEngine.SetMetadata(new ApplicationMetadata("Clear example", "1.0.0", "com.example.clear")); +using EngineScope _ = QuackEngine.Init(Subsystem.Video); + +// Create a resizable window and a renderer +using Window window = new("Clear example", 640, 480) { Resizable = true }; +using Renderer renderer = new(window) +{ + Presentation = (640, 480, LogicalPresentation.Letterbox) +}; + +// Start a stopwatch for the sine wave +Stopwatch stopwatch = Stopwatch.StartNew(); + +// Run the main loop +while (window.IsOpen) +{ + // Poll the events + while (window.Poll(out Event e)) + { + // If the user requests to quit the application, it will automatically close the window and exit the loop. + if (e is QuitRequestedEvent) + return; + } + + // Clear the window with a sine wave color + renderer.Clear(SineWave(stopwatch.Elapsed.TotalSeconds)); + + // Presents all the drawn content on the window + renderer.Present(); +} + +static ColorF SineWave(double seconds) +{ + float r = (float)(0.5 + (0.5 * Math.Sin(seconds))); + float g = (float)(0.5 + (0.5 * Math.Sin(seconds + (Math.PI * 2 / 3)))); + float b = (float)(0.5 + (0.5 * Math.Sin(seconds + (Math.PI * 4 / 3)))); + + return new ColorF(r, g, b); +} diff --git a/samples/readme.md b/samples/readme.md new file mode 100644 index 0000000..22a0bf8 --- /dev/null +++ b/samples/readme.md @@ -0,0 +1,43 @@ +# How to use Quack! Engine examples 🦆 + +This document provides an overview of how to run and explore the Quack! game engine examples. +> You need to have **.NET 11.0 SDK** installed to run theses examples via [dotnet run app.cs]. + +## Prerequisites + +- [.NET 11.0 SDK](https://dotnet.microsoft.com/en-us/download/dotnet/10.0) + +> Quack! examples are single-file C# scripts, so most IDEs cannot run them directly. +> +> VS Code provides intellisense but requires running via the terminal. + +## Running Examples + +Run the example directly in your terminal: + +#### Window +```bash +dotnet ./examples/windows/window.cs +``` + +#### Linux +```bash +chmod +x ./examples/windows/window.cs # only needed once +./examples/windows/window.cs +``` + +Alternatively, you can open the example in VS Code for editing and exploring the code, but +execution must be done via the terminal as shown above. + +> For more details on single-file, see [dotnet run app.cs] + +## Examples + +Each example demonstrates a specific feature or capability of the Quack! engine. + +## Graphics + +- [Clear](./graphics/clear.cs) - Clear the screen with a sine wave pattern. + + +[dotnet run app.cs]: https://devblogs.microsoft.com/dotnet/announcing-dotnet-run-app/