chore: clean recommit
This commit is contained in:
parent
8024c6ac40
commit
60ad2e7fb1
122 changed files with 23950 additions and 323 deletions
248
Examples/Textures/CellularAutomata.cs
Normal file
248
Examples/Textures/CellularAutomata.cs
Normal file
|
|
@ -0,0 +1,248 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [textures] example - cellular automata
|
||||
*
|
||||
* Example complexity rating: [★★☆☆] 2/4
|
||||
*
|
||||
* Example originally created with raylib 5.6, last time updated with raylib 5.6
|
||||
*
|
||||
* Example contributed by Jordi Santonja (@JordSant) and reviewed by Ramon Santamaria (@raysan5)
|
||||
*
|
||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||
* BSD-like license that allows static linking with closed source software
|
||||
*
|
||||
* Copyright (c) 2025 Jordi Santonja (@JordSant)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
using static Raylib_cs.Raylib;
|
||||
|
||||
namespace Examples.Textures;
|
||||
|
||||
public partial class CellularAutomata : IExample
|
||||
{
|
||||
// Initialization constants
|
||||
//--------------------------------------------------------------------------------------
|
||||
private const int screenWidth = 800;
|
||||
private const int screenHeight = 450;
|
||||
private const int imageWidth = 800;
|
||||
private const int imageHeight = 800 / 2;
|
||||
|
||||
// Rule button sizes and positions
|
||||
private const int drawRuleStartX = 585;
|
||||
private const int drawRuleStartY = 10;
|
||||
private const int drawRuleSpacing = 15;
|
||||
private const int drawRuleGroupSpacing = 50;
|
||||
private const int drawRuleSize = 14;
|
||||
private const int drawRuleInnerSize = 10;
|
||||
|
||||
// Preset button sizes
|
||||
private const int presetsSizeX = 42;
|
||||
private const int presetsSizeY = 22;
|
||||
|
||||
private const int linesUpdatedPerFrame = 4;
|
||||
|
||||
public string Name => "Textures / Cellular Automata";
|
||||
|
||||
public string Title => "raylib [textures] example - cellular automata";
|
||||
|
||||
// Some interesting rules
|
||||
private static readonly int[] presetValues = { 18, 30, 60, 86, 102, 124, 126, 150, 182, 225 };
|
||||
private const int presetsCount = 10;
|
||||
|
||||
private Image image;
|
||||
private Texture2D texture;
|
||||
private int rule;
|
||||
private int line;
|
||||
|
||||
private static void ComputeLine(ref Image image, int line, int rule)
|
||||
{
|
||||
// Compute next line pixels. Boundaries are not computed, always 0
|
||||
for (var i = 1; i < imageWidth - 1; i++)
|
||||
{
|
||||
// Get, from the previous line, the 3 pixels states as a binary value
|
||||
var prevValue = ((GetImageColor(image, i - 1, line - 1).R < 5) ? 4 : 0) + // Left pixel
|
||||
((GetImageColor(image, i, line - 1).R < 5) ? 2 : 0) + // Center pixel
|
||||
((GetImageColor(image, i + 1, line - 1).R < 5) ? 1 : 0); // Right pixel
|
||||
// Get next value from rule bitmask
|
||||
var currValue = (rule & (1 << prevValue)) != 0;
|
||||
// Update pixel color
|
||||
ImageDrawPixel(ref image, i, line, currValue ? Color.Black : Color.RayWhite);
|
||||
}
|
||||
}
|
||||
|
||||
public void Init()
|
||||
{
|
||||
// Image that contains the cellular automaton
|
||||
image = GenImageColor(imageWidth, imageHeight, Color.RayWhite);
|
||||
// The top central pixel set as black
|
||||
ImageDrawPixel(ref image, imageWidth / 2, 0, Color.Black);
|
||||
|
||||
texture = LoadTextureFromImage(image);
|
||||
|
||||
// Variables
|
||||
rule = 30; // Starting rule
|
||||
line = 1; // Line to compute, starting from line 1. One point in line 0 is already set
|
||||
}
|
||||
|
||||
public unsafe void Update()
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
// Handle mouse
|
||||
var mouse = GetMousePosition();
|
||||
var mouseInCell = -1; // -1: outside any button; 0-7: rule cells; 8+: preset cells
|
||||
|
||||
// Check mouse on rule cells
|
||||
for (var i = 0; i < 8; i++)
|
||||
{
|
||||
var cellX = drawRuleStartX - drawRuleGroupSpacing * i + drawRuleSpacing;
|
||||
var cellY = drawRuleStartY + drawRuleSpacing;
|
||||
if ((mouse.X >= cellX) && (mouse.X <= cellX + drawRuleSize) &&
|
||||
(mouse.Y >= cellY) && (mouse.Y <= cellY + drawRuleSize))
|
||||
{
|
||||
mouseInCell = i; // 0-7: rule cells
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Check mouse on preset cells
|
||||
if (mouseInCell < 0)
|
||||
{
|
||||
for (var i = 0; i < presetsCount; i++)
|
||||
{
|
||||
var cellX = 4 + (presetsSizeX + 2) * (i / 2);
|
||||
var cellY = 2 + (presetsSizeY + 2) * (i % 2);
|
||||
if ((mouse.X >= cellX) && (mouse.X <= cellX + presetsSizeX) &&
|
||||
(mouse.Y >= cellY) && (mouse.Y <= cellY + presetsSizeY))
|
||||
{
|
||||
mouseInCell = i + 8; // 8+: preset cells
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (IsMouseButtonPressed(MouseButton.Left) && (mouseInCell >= 0))
|
||||
{
|
||||
// Rule changed both by selecting a preset or toggling a bit
|
||||
if (mouseInCell < 8)
|
||||
{
|
||||
rule ^= (1 << mouseInCell);
|
||||
}
|
||||
else
|
||||
{
|
||||
rule = presetValues[mouseInCell - 8];
|
||||
}
|
||||
|
||||
// Reset image
|
||||
ImageClearBackground(ref image, Color.RayWhite);
|
||||
ImageDrawPixel(ref image, imageWidth / 2, 0, Color.Black);
|
||||
line = 1;
|
||||
}
|
||||
|
||||
// Compute next lines
|
||||
//----------------------------------------------------------------------------------
|
||||
if (line < imageHeight)
|
||||
{
|
||||
for (var i = 0; (i < linesUpdatedPerFrame) && (line + i < imageHeight); i++)
|
||||
{
|
||||
ComputeLine(ref image, line + i, rule);
|
||||
}
|
||||
line += linesUpdatedPerFrame;
|
||||
|
||||
UpdateTexture(texture, image.Data);
|
||||
}
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
ClearBackground(Color.RayWhite);
|
||||
|
||||
// Draw cellular automaton texture
|
||||
DrawTexture(texture, 0, screenHeight - imageHeight, Color.White);
|
||||
|
||||
// Draw preset values
|
||||
for (var i = 0; i < presetsCount; i++)
|
||||
{
|
||||
DrawText($"{presetValues[i]}", 8 + (presetsSizeX + 2) * (i / 2), 4 + (presetsSizeY + 2) * (i % 2), 20, Color.Gray);
|
||||
DrawRectangleLines(4 + (presetsSizeX + 2) * (i / 2), 2 + (presetsSizeY + 2) * (i % 2), presetsSizeX, presetsSizeY, Color.Blue);
|
||||
|
||||
// If the mouse is on this preset, highlight it
|
||||
if (mouseInCell == i + 8)
|
||||
{
|
||||
DrawRectangleLinesEx(new Rectangle(2 + (presetsSizeX + 2.0f) * (i / 2),
|
||||
(presetsSizeY + 2.0f) * (i % 2),
|
||||
presetsSizeX + 4.0f, presetsSizeY + 4.0f), 3, Color.Red);
|
||||
}
|
||||
}
|
||||
|
||||
// Draw rule bits
|
||||
for (var i = 0; i < 8; i++)
|
||||
{
|
||||
// The three input bits
|
||||
for (var j = 0; j < 3; j++)
|
||||
{
|
||||
DrawRectangleLines(drawRuleStartX - drawRuleGroupSpacing * i + drawRuleSpacing * j, drawRuleStartY, drawRuleSize, drawRuleSize, Color.Gray);
|
||||
if ((i & (4 >> j)) != 0)
|
||||
{
|
||||
DrawRectangle(drawRuleStartX + 2 - drawRuleGroupSpacing * i + drawRuleSpacing * j, drawRuleStartY + 2, drawRuleInnerSize, drawRuleInnerSize, Color.Black);
|
||||
}
|
||||
}
|
||||
|
||||
// The output bit
|
||||
DrawRectangleLines(drawRuleStartX - drawRuleGroupSpacing * i + drawRuleSpacing, drawRuleStartY + drawRuleSpacing, drawRuleSize, drawRuleSize, Color.Blue);
|
||||
if ((rule & (1 << i)) != 0)
|
||||
{
|
||||
DrawRectangle(drawRuleStartX + 2 - drawRuleGroupSpacing * i + drawRuleSpacing, drawRuleStartY + 2 + drawRuleSpacing, drawRuleInnerSize, drawRuleInnerSize, Color.Black);
|
||||
}
|
||||
|
||||
// If the mouse is on this rule bit, highlight it
|
||||
if (mouseInCell == i)
|
||||
{
|
||||
DrawRectangleLinesEx(new Rectangle(drawRuleStartX - drawRuleGroupSpacing * i + drawRuleSpacing - 2.0f,
|
||||
drawRuleStartY + drawRuleSpacing - 2.0f,
|
||||
drawRuleSize + 4.0f, drawRuleSize + 4.0f), 3, Color.Red);
|
||||
}
|
||||
}
|
||||
|
||||
DrawText($"RULE: {rule}", drawRuleStartX + drawRuleSpacing * 4, drawRuleStartY + 1, 30, Color.Gray);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
public void Unload()
|
||||
{
|
||||
UnloadImage(image);
|
||||
UnloadTexture(texture);
|
||||
}
|
||||
|
||||
public static int Main()
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
InitWindow(screenWidth, screenHeight, "raylib [textures] example - cellular automata");
|
||||
|
||||
SetTargetFPS(60);
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
var game = new CellularAutomata();
|
||||
game.Init();
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
game.Update();
|
||||
}
|
||||
|
||||
game.Unload();
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
144
Examples/Textures/ClipboardImage.cs
Normal file
144
Examples/Textures/ClipboardImage.cs
Normal file
|
|
@ -0,0 +1,144 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [textures] example - clipboard image
|
||||
*
|
||||
* Example complexity rating: [★☆☆☆] 1/4
|
||||
*
|
||||
* Example originally created with raylib 6.0, last time updated with raylib 6.0
|
||||
*
|
||||
* Example contributed by Maicon Santana (@maiconpintoabreu) and reviewed by Ramon Santamaria (@raysan5)
|
||||
*
|
||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||
* BSD-like license that allows static linking with closed source software
|
||||
*
|
||||
* Copyright (c) 2026 Maicon Santana (@maiconpintoabreu)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
using System.Numerics;
|
||||
using static Raylib_cs.Raylib;
|
||||
|
||||
namespace Examples.Textures;
|
||||
|
||||
[ExcludeFromBrowser("GetClipboardImage() is a desktop-only OS clipboard feature")]
|
||||
public partial class ClipboardImage : IExample
|
||||
{
|
||||
private const int screenWidth = 800;
|
||||
private const int screenHeight = 450;
|
||||
|
||||
private const int MaxTextureCollection = 20;
|
||||
|
||||
public string Name => "Textures / Clipboard Image";
|
||||
|
||||
public string Title => "raylib [textures] example - clipboard image";
|
||||
|
||||
private struct TextureCollection
|
||||
{
|
||||
public Texture2D Texture;
|
||||
public Vector2 Position;
|
||||
}
|
||||
|
||||
private TextureCollection[] collection;
|
||||
private int currentCollectionIndex;
|
||||
|
||||
public void Init()
|
||||
{
|
||||
collection = new TextureCollection[MaxTextureCollection];
|
||||
currentCollectionIndex = 0;
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
if (IsKeyPressed(KeyboardKey.R)) // Reset image collection
|
||||
{
|
||||
// Unload textures to avoid memory leaks
|
||||
for (var i = 0; i < MaxTextureCollection; i++)
|
||||
{
|
||||
UnloadTexture(collection[i].Texture);
|
||||
}
|
||||
|
||||
currentCollectionIndex = 0;
|
||||
}
|
||||
|
||||
if (IsKeyDown(KeyboardKey.LeftControl) && IsKeyPressed(KeyboardKey.V) &&
|
||||
(currentCollectionIndex < MaxTextureCollection))
|
||||
{
|
||||
var image = GetClipboardImage();
|
||||
|
||||
if (IsImageValid(image))
|
||||
{
|
||||
collection[currentCollectionIndex].Texture = LoadTextureFromImage(image);
|
||||
collection[currentCollectionIndex].Position = GetMousePosition();
|
||||
currentCollectionIndex++;
|
||||
UnloadImage(image);
|
||||
}
|
||||
else
|
||||
{
|
||||
TraceLog(TraceLogLevel.Info, "IMAGE: Could not retrieve image from clipboard");
|
||||
}
|
||||
}
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(Color.RayWhite);
|
||||
|
||||
for (var i = 0; i < currentCollectionIndex; i++)
|
||||
{
|
||||
if (IsTextureValid(collection[i].Texture))
|
||||
{
|
||||
DrawTexturePro(collection[i].Texture,
|
||||
new Rectangle(0, 0, collection[i].Texture.Width, collection[i].Texture.Height),
|
||||
new Rectangle(collection[i].Position.X, collection[i].Position.Y, collection[i].Texture.Width, collection[i].Texture.Height),
|
||||
new Vector2(collection[i].Texture.Width * 0.5f, collection[i].Texture.Height * 0.5f),
|
||||
0.0f, Color.White);
|
||||
}
|
||||
}
|
||||
|
||||
DrawRectangle(0, 0, screenWidth, 40, Color.Black);
|
||||
DrawText("Clipboard Image - Ctrl+V to Paste and R to Reset ", 120, 10, 20, Color.LightGray);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
public void Unload()
|
||||
{
|
||||
for (var i = 0; i < MaxTextureCollection; i++)
|
||||
{
|
||||
UnloadTexture(collection[i].Texture);
|
||||
}
|
||||
}
|
||||
|
||||
public static int Main()
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
InitWindow(screenWidth, screenHeight, "raylib [textures] example - clipboard image");
|
||||
|
||||
SetTargetFPS(60);
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
var game = new ClipboardImage();
|
||||
game.Init();
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
game.Update();
|
||||
}
|
||||
|
||||
game.Unload();
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
233
Examples/Textures/FogOfWar.cs
Normal file
233
Examples/Textures/FogOfWar.cs
Normal file
|
|
@ -0,0 +1,233 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [textures] example - fog of war
|
||||
*
|
||||
* Example complexity rating: [★★★☆] 3/4
|
||||
*
|
||||
* Example originally created with raylib 4.2, last time updated with raylib 4.2
|
||||
*
|
||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||
* BSD-like license that allows static linking with closed source software
|
||||
*
|
||||
* Copyright (c) 2018-2025 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
using System.Numerics;
|
||||
using static Raylib_cs.Raylib;
|
||||
|
||||
namespace Examples.Textures;
|
||||
|
||||
public partial class FogOfWar : IExample
|
||||
{
|
||||
private const int screenWidth = 800;
|
||||
private const int screenHeight = 450;
|
||||
|
||||
private const int MapTileSize = 32; // Tiles size 32x32 pixels
|
||||
private const int PlayerSize = 16; // Player size
|
||||
private const int PlayerTileVisibility = 2; // Player can see 2 tiles around its position
|
||||
|
||||
public string Name => "Textures / Fog of War";
|
||||
|
||||
public string Title => "raylib [textures] example - fog of war";
|
||||
|
||||
// Map data type
|
||||
private struct Map
|
||||
{
|
||||
public uint TilesX; // Number of tiles in X axis
|
||||
public uint TilesY; // Number of tiles in Y axis
|
||||
public byte[] TileIds; // Tile ids (tilesX*tilesY), defines type of tile to draw
|
||||
public byte[] TileFog; // Tile fog state (tilesX*tilesY), defines if a tile has fog or half-fog
|
||||
}
|
||||
|
||||
private Map map;
|
||||
private Vector2 playerPosition;
|
||||
private int playerTileX;
|
||||
private int playerTileY;
|
||||
private RenderTexture2D fogOfWar;
|
||||
|
||||
public void Init()
|
||||
{
|
||||
map = new Map();
|
||||
map.TilesX = 25;
|
||||
map.TilesY = 15;
|
||||
|
||||
// NOTE: We can have up to 256 values for tile ids and for tile fog state,
|
||||
// probably we don't need that many values for fog state, it can be optimized
|
||||
// to use only 2 bits per fog state (reducing size by 4) but logic will be a bit more complex
|
||||
map.TileIds = new byte[map.TilesX * map.TilesY];
|
||||
map.TileFog = new byte[map.TilesX * map.TilesY];
|
||||
|
||||
// Load map tiles (generating 2 random tile ids for testing)
|
||||
// NOTE: Map tile ids should be probably loaded from an external map file
|
||||
for (uint i = 0; i < map.TilesY * map.TilesX; i++)
|
||||
{
|
||||
map.TileIds[i] = (byte)GetRandomValue(0, 1);
|
||||
}
|
||||
|
||||
// Player position on the screen (pixel coordinates, not tile coordinates)
|
||||
playerPosition = new Vector2(180, 130);
|
||||
playerTileX = 0;
|
||||
playerTileY = 0;
|
||||
|
||||
// Render texture to render fog of war
|
||||
// NOTE: To get an automatic smooth-fog effect we use a render texture to render fog
|
||||
// at a smaller size (one pixel per tile) and scale it on drawing with bilinear filtering
|
||||
fogOfWar = LoadRenderTexture((int)map.TilesX, (int)map.TilesY);
|
||||
SetTextureFilter(fogOfWar.Texture, TextureFilter.Bilinear);
|
||||
SetTextureWrap(fogOfWar.Texture, TextureWrap.Clamp);
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
// Move player around
|
||||
if (IsKeyDown(KeyboardKey.Right))
|
||||
{
|
||||
playerPosition.X += 5;
|
||||
}
|
||||
if (IsKeyDown(KeyboardKey.Left))
|
||||
{
|
||||
playerPosition.X -= 5;
|
||||
}
|
||||
if (IsKeyDown(KeyboardKey.Down))
|
||||
{
|
||||
playerPosition.Y += 5;
|
||||
}
|
||||
if (IsKeyDown(KeyboardKey.Up))
|
||||
{
|
||||
playerPosition.Y -= 5;
|
||||
}
|
||||
|
||||
// Check player position to avoid moving outside tilemap limits
|
||||
if (playerPosition.X < 0)
|
||||
{
|
||||
playerPosition.X = 0;
|
||||
}
|
||||
else if ((playerPosition.X + PlayerSize) > (map.TilesX * MapTileSize))
|
||||
{
|
||||
playerPosition.X = (float)map.TilesX * MapTileSize - PlayerSize;
|
||||
}
|
||||
if (playerPosition.Y < 0)
|
||||
{
|
||||
playerPosition.Y = 0;
|
||||
}
|
||||
else if ((playerPosition.Y + PlayerSize) > (map.TilesY * MapTileSize))
|
||||
{
|
||||
playerPosition.Y = (float)map.TilesY * MapTileSize - PlayerSize;
|
||||
}
|
||||
|
||||
// Previous visited tiles are set to partial fog
|
||||
for (uint i = 0; i < map.TilesX * map.TilesY; i++)
|
||||
{
|
||||
if (map.TileFog[i] == 1)
|
||||
{
|
||||
map.TileFog[i] = 2;
|
||||
}
|
||||
}
|
||||
|
||||
// Get current tile position from player pixel position
|
||||
playerTileX = (int)((playerPosition.X + (float)MapTileSize / 2) / MapTileSize);
|
||||
playerTileY = (int)((playerPosition.Y + (float)MapTileSize / 2) / MapTileSize);
|
||||
|
||||
// Check visibility and update fog
|
||||
// NOTE: We check tilemap limits to avoid processing tiles out-of-array-bounds (it could crash program)
|
||||
for (var y = (playerTileY - PlayerTileVisibility); y < (playerTileY + PlayerTileVisibility); y++)
|
||||
{
|
||||
for (var x = (playerTileX - PlayerTileVisibility); x < (playerTileX + PlayerTileVisibility); x++)
|
||||
{
|
||||
if ((x >= 0) && (x < (int)map.TilesX) && (y >= 0) && (y < (int)map.TilesY))
|
||||
{
|
||||
map.TileFog[y * map.TilesX + x] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
// Draw fog of war to a small render texture for automatic smoothing on scaling
|
||||
BeginTextureMode(fogOfWar);
|
||||
ClearBackground(Color.Blank);
|
||||
for (uint y = 0; y < map.TilesY; y++)
|
||||
{
|
||||
for (uint x = 0; x < map.TilesX; x++)
|
||||
{
|
||||
if (map.TileFog[y * map.TilesX + x] == 0)
|
||||
{
|
||||
DrawRectangle((int)x, (int)y, 1, 1, Color.Black);
|
||||
}
|
||||
else if (map.TileFog[y * map.TilesX + x] == 2)
|
||||
{
|
||||
DrawRectangle((int)x, (int)y, 1, 1, Fade(Color.Black, 0.8f));
|
||||
}
|
||||
}
|
||||
}
|
||||
EndTextureMode();
|
||||
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(Color.RayWhite);
|
||||
|
||||
for (uint y = 0; y < map.TilesY; y++)
|
||||
{
|
||||
for (uint x = 0; x < map.TilesX; x++)
|
||||
{
|
||||
// Draw tiles from id (and tile borders)
|
||||
DrawRectangle((int)x * MapTileSize, (int)y * MapTileSize, MapTileSize, MapTileSize,
|
||||
(map.TileIds[y * map.TilesX + x] == 0) ? Color.Blue : Fade(Color.Blue, 0.9f));
|
||||
DrawRectangleLines((int)x * MapTileSize, (int)y * MapTileSize, MapTileSize, MapTileSize, Fade(Color.DarkBlue, 0.5f));
|
||||
}
|
||||
}
|
||||
|
||||
// Draw player
|
||||
DrawRectangleV(playerPosition, new Vector2(PlayerSize, PlayerSize), Color.Red);
|
||||
|
||||
// Draw fog of war (scaled to full map, bilinear filtering)
|
||||
DrawTexturePro(fogOfWar.Texture,
|
||||
new Rectangle(0, 0, (float)fogOfWar.Texture.Width, (float)-fogOfWar.Texture.Height),
|
||||
new Rectangle(0, 0, (float)map.TilesX * MapTileSize, (float)map.TilesY * MapTileSize),
|
||||
new Vector2(0, 0), 0.0f, Color.White);
|
||||
|
||||
// Draw player current tile
|
||||
DrawText($"Current tile: [{playerTileX},{playerTileY}]", 10, 10, 20, Color.RayWhite);
|
||||
DrawText("ARROW KEYS to move", 10, screenHeight - 25, 20, Color.RayWhite);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
public void Unload()
|
||||
{
|
||||
UnloadRenderTexture(fogOfWar); // Unload render texture
|
||||
}
|
||||
|
||||
public static int Main()
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
InitWindow(screenWidth, screenHeight, "raylib [textures] example - fog of war");
|
||||
|
||||
SetTargetFPS(60);
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
var game = new FogOfWar();
|
||||
game.Init();
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
game.Update();
|
||||
}
|
||||
|
||||
game.Unload();
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
248
Examples/Textures/FramebufferRendering.cs
Normal file
248
Examples/Textures/FramebufferRendering.cs
Normal file
|
|
@ -0,0 +1,248 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [textures] example - framebuffer rendering
|
||||
*
|
||||
* Example complexity rating: [★★☆☆] 2/4
|
||||
*
|
||||
* Example originally created with raylib 5.6, last time updated with raylib 5.6
|
||||
*
|
||||
* Example contributed by Jack Boakes (@jackboakes) and reviewed by Ramon Santamaria (@raysan5)
|
||||
*
|
||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||
* BSD-like license that allows static linking with closed source software
|
||||
*
|
||||
* Copyright (c) 2026 Jack Boakes (@jackboakes)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
using System.Numerics;
|
||||
using static Raylib_cs.Raylib;
|
||||
using static Raylib_cs.Raymath;
|
||||
|
||||
namespace Examples.Textures;
|
||||
|
||||
public partial class FramebufferRendering : IExample
|
||||
{
|
||||
private const int screenWidth = 800;
|
||||
private const int screenHeight = 450;
|
||||
private const int splitWidth = screenWidth / 2;
|
||||
|
||||
public string Name => "Textures / Framebuffer Rendering";
|
||||
|
||||
public string Title => "raylib [textures] example - framebuffer rendering";
|
||||
|
||||
public bool CursorDisabled => true;
|
||||
|
||||
private Camera3D subjectCamera;
|
||||
private Camera3D observerCamera;
|
||||
|
||||
private RenderTexture2D observerTarget;
|
||||
private Rectangle observerSource;
|
||||
private Rectangle observerDest;
|
||||
|
||||
private RenderTexture2D subjectTarget;
|
||||
private Rectangle subjectSource;
|
||||
private Rectangle subjectDest;
|
||||
private float textureAspectRatio;
|
||||
|
||||
private const float captureSize = 128.0f;
|
||||
private Rectangle cropSource;
|
||||
private Rectangle cropDest;
|
||||
|
||||
public void Init()
|
||||
{
|
||||
// Camera to look at the 3D world
|
||||
subjectCamera = new Camera3D();
|
||||
subjectCamera.Position = new Vector3(5.0f, 5.0f, 5.0f);
|
||||
subjectCamera.Target = new Vector3(0.0f, 0.0f, 0.0f);
|
||||
subjectCamera.Up = new Vector3(0.0f, 1.0f, 0.0f);
|
||||
subjectCamera.FovY = 45.0f;
|
||||
subjectCamera.Projection = CameraProjection.Perspective;
|
||||
|
||||
// Camera to observe the subject camera and 3D world
|
||||
observerCamera = new Camera3D();
|
||||
observerCamera.Position = new Vector3(10.0f, 10.0f, 10.0f);
|
||||
observerCamera.Target = new Vector3(0.0f, 0.0f, 0.0f);
|
||||
observerCamera.Up = new Vector3(0.0f, 1.0f, 0.0f);
|
||||
observerCamera.FovY = 45.0f;
|
||||
observerCamera.Projection = CameraProjection.Perspective;
|
||||
|
||||
// Set up render textures
|
||||
observerTarget = LoadRenderTexture(splitWidth, screenHeight);
|
||||
observerSource = new Rectangle(0.0f, 0.0f, observerTarget.Texture.Width, -observerTarget.Texture.Height);
|
||||
observerDest = new Rectangle(0.0f, 0.0f, splitWidth, screenHeight);
|
||||
|
||||
subjectTarget = LoadRenderTexture(splitWidth, screenHeight);
|
||||
subjectSource = new Rectangle(0.0f, 0.0f, subjectTarget.Texture.Width, -subjectTarget.Texture.Height);
|
||||
subjectDest = new Rectangle(splitWidth, 0.0f, splitWidth, screenHeight);
|
||||
textureAspectRatio = (float)subjectTarget.Texture.Width / subjectTarget.Texture.Height;
|
||||
|
||||
// Rectangles for cropping render texture
|
||||
cropSource = new Rectangle((subjectTarget.Texture.Width - captureSize) / 2.0f, (subjectTarget.Texture.Height - captureSize) / 2.0f, captureSize, -captureSize);
|
||||
cropDest = new Rectangle(splitWidth + 20.0f, 20.0f, captureSize, captureSize);
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
UpdateCamera(ref observerCamera, CameraMode.Free);
|
||||
UpdateCamera(ref subjectCamera, CameraMode.Orbital);
|
||||
|
||||
if (IsKeyPressed(KeyboardKey.R))
|
||||
{
|
||||
observerCamera.Target = new Vector3(0.0f, 0.0f, 0.0f);
|
||||
}
|
||||
|
||||
// Build LHS observer view texture
|
||||
BeginTextureMode(observerTarget);
|
||||
|
||||
ClearBackground(Color.RayWhite);
|
||||
|
||||
BeginMode3D(observerCamera);
|
||||
|
||||
DrawGrid(10, 1.0f);
|
||||
DrawCube(new Vector3(0.0f, 0.0f, 0.0f), 2.0f, 2.0f, 2.0f, Color.Gold);
|
||||
DrawCubeWires(new Vector3(0.0f, 0.0f, 0.0f), 2.0f, 2.0f, 2.0f, Color.Pink);
|
||||
DrawCameraPrism(subjectCamera, textureAspectRatio, Color.Green);
|
||||
|
||||
EndMode3D();
|
||||
|
||||
DrawText("Observer View", 10, observerTarget.Texture.Height - 30, 20, Color.Black);
|
||||
DrawText("WASD + Mouse to Move", 10, 10, 20, Color.DarkGray);
|
||||
DrawText("Scroll to Zoom", 10, 30, 20, Color.DarkGray);
|
||||
DrawText("R to Reset Observer Target", 10, 50, 20, Color.DarkGray);
|
||||
|
||||
EndTextureMode();
|
||||
|
||||
// Build RHS subject view texture
|
||||
BeginTextureMode(subjectTarget);
|
||||
|
||||
ClearBackground(Color.RayWhite);
|
||||
|
||||
BeginMode3D(subjectCamera);
|
||||
|
||||
DrawCube(new Vector3(0.0f, 0.0f, 0.0f), 2.0f, 2.0f, 2.0f, Color.Gold);
|
||||
DrawCubeWires(new Vector3(0.0f, 0.0f, 0.0f), 2.0f, 2.0f, 2.0f, Color.Pink);
|
||||
DrawGrid(10, 1.0f);
|
||||
|
||||
EndMode3D();
|
||||
|
||||
DrawRectangleLines((int)((subjectTarget.Texture.Width - captureSize) / 2.0f), (int)((subjectTarget.Texture.Height - captureSize) / 2.0f), (int)captureSize, (int)captureSize, Color.Green);
|
||||
DrawText("Subject View", 10, subjectTarget.Texture.Height - 30, 20, Color.Black);
|
||||
|
||||
EndTextureMode();
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(Color.Black);
|
||||
|
||||
// Draw observer texture LHS
|
||||
DrawTexturePro(observerTarget.Texture, observerSource, observerDest, new Vector2(0.0f, 0.0f), 0.0f, Color.White);
|
||||
|
||||
// Draw subject texture RHS
|
||||
DrawTexturePro(subjectTarget.Texture, subjectSource, subjectDest, new Vector2(0.0f, 0.0f), 0.0f, Color.White);
|
||||
|
||||
// Draw the small crop overlay on top
|
||||
DrawTexturePro(subjectTarget.Texture, cropSource, cropDest, new Vector2(0.0f, 0.0f), 0.0f, Color.White);
|
||||
DrawRectangleLinesEx(cropDest, 2, Color.Black);
|
||||
|
||||
// Draw split screen divider line
|
||||
DrawLine(splitWidth, 0, splitWidth, screenHeight, Color.Black);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
public void Unload()
|
||||
{
|
||||
UnloadRenderTexture(observerTarget);
|
||||
UnloadRenderTexture(subjectTarget);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Module Functions Definition
|
||||
//----------------------------------------------------------------------------------
|
||||
private static void DrawCameraPrism(Camera3D camera, float aspect, Color color)
|
||||
{
|
||||
float length = Vector3Distance(camera.Position, camera.Target);
|
||||
// Define the 4 corners of the camera's prism plane sliced at the target in Normalized Device Coordinates
|
||||
Vector3[] planeNDC =
|
||||
{
|
||||
new(-1.0f, -1.0f, 1.0f), // Bottom Left
|
||||
new( 1.0f, -1.0f, 1.0f), // Bottom Right
|
||||
new( 1.0f, 1.0f, 1.0f), // Top Right
|
||||
new(-1.0f, 1.0f, 1.0f) // Top Left
|
||||
};
|
||||
|
||||
// Build the matrices
|
||||
Matrix4x4 view = GetCameraMatrix(camera);
|
||||
Matrix4x4 proj = MatrixPerspective(camera.FovY * DEG2RAD, aspect, 0.05f, length);
|
||||
// Combine view and projection so we can reverse the full camera transform
|
||||
Matrix4x4 viewProj = MatrixMultiply(view, proj);
|
||||
// Invert the view-projection matrix to unproject points from NDC space back into world space
|
||||
Matrix4x4 inverseViewProj = MatrixInvert(viewProj);
|
||||
|
||||
// Transform the 4 plane corners from NDC into world space
|
||||
Vector3[] corners = new Vector3[4];
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
float x = planeNDC[i].X;
|
||||
float y = planeNDC[i].Y;
|
||||
float z = planeNDC[i].Z;
|
||||
|
||||
// Multiply NDC position by the inverse view-projection matrix
|
||||
// This produces a homogeneous (x, y, z, w) position in world space
|
||||
float vx = inverseViewProj.M11 * x + inverseViewProj.M12 * y + inverseViewProj.M13 * z + inverseViewProj.M14;
|
||||
float vy = inverseViewProj.M21 * x + inverseViewProj.M22 * y + inverseViewProj.M23 * z + inverseViewProj.M24;
|
||||
float vz = inverseViewProj.M31 * x + inverseViewProj.M32 * y + inverseViewProj.M33 * z + inverseViewProj.M34;
|
||||
float vw = inverseViewProj.M41 * x + inverseViewProj.M42 * y + inverseViewProj.M43 * z + inverseViewProj.M44;
|
||||
|
||||
corners[i] = new Vector3(vx / vw, vy / vw, vz / vw);
|
||||
}
|
||||
|
||||
// Draw the far plane sliced at the target
|
||||
DrawLine3D(corners[0], corners[1], color);
|
||||
DrawLine3D(corners[1], corners[2], color);
|
||||
DrawLine3D(corners[2], corners[3], color);
|
||||
DrawLine3D(corners[3], corners[0], color);
|
||||
|
||||
// Draw the prism lines from the far plane to the camera position
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
DrawLine3D(camera.Position, corners[i], color);
|
||||
}
|
||||
}
|
||||
|
||||
public static int Main()
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
InitWindow(screenWidth, screenHeight, "raylib [textures] example - framebuffer rendering");
|
||||
|
||||
SetTargetFPS(60);
|
||||
DisableCursor();
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
var game = new FramebufferRendering();
|
||||
game.Init();
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
game.Update();
|
||||
}
|
||||
|
||||
game.Unload();
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
171
Examples/Textures/GifPlayer.cs
Normal file
171
Examples/Textures/GifPlayer.cs
Normal file
|
|
@ -0,0 +1,171 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [textures] example - gif player
|
||||
*
|
||||
* Example complexity rating: [★★★☆] 3/4
|
||||
*
|
||||
* Example originally created with raylib 4.2, last time updated with raylib 4.2
|
||||
*
|
||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||
* BSD-like license that allows static linking with closed source software
|
||||
*
|
||||
* Copyright (c) 2021-2025 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
using static Raylib_cs.Raylib;
|
||||
|
||||
namespace Examples.Textures;
|
||||
|
||||
public partial class GifPlayer : IExample
|
||||
{
|
||||
private const int screenWidth = 800;
|
||||
private const int screenHeight = 450;
|
||||
|
||||
private const int MaxFrameDelay = 20;
|
||||
private const int MinFrameDelay = 1;
|
||||
|
||||
public string Name => "Textures / Gif Player";
|
||||
|
||||
public string Title => "raylib [textures] example - gif player";
|
||||
|
||||
private int animFrames;
|
||||
private Image imScarfyAnim;
|
||||
private Texture2D texScarfyAnim;
|
||||
private uint nextFrameDataOffset; // Current byte offset to next frame in image.data
|
||||
private int currentAnimFrame; // Current animation frame to load and draw
|
||||
private int frameDelay; // Frame delay to switch between animation frames
|
||||
private int frameCounter; // General frames counter
|
||||
|
||||
public void Init()
|
||||
{
|
||||
animFrames = 0;
|
||||
|
||||
// Load all GIF animation frames into a single Image
|
||||
// NOTE: GIF data is always loaded as RGBA (32bit) by default
|
||||
// NOTE: Frames are just appended one after another in image.data memory
|
||||
imScarfyAnim = LoadImageAnim("resources/scarfy_run.gif", out animFrames);
|
||||
|
||||
// Load texture from image
|
||||
// NOTE: We will update this texture when required with next frame data
|
||||
// WARNING: It's not recommended to use this technique for sprites animation,
|
||||
// use spritesheets instead, like illustrated in textures_sprite_anim example
|
||||
texScarfyAnim = LoadTextureFromImage(imScarfyAnim);
|
||||
|
||||
nextFrameDataOffset = 0;
|
||||
|
||||
currentAnimFrame = 0;
|
||||
frameDelay = 8;
|
||||
frameCounter = 0;
|
||||
}
|
||||
|
||||
public unsafe void Update()
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
frameCounter++;
|
||||
if (frameCounter >= frameDelay)
|
||||
{
|
||||
// Move to next frame
|
||||
// NOTE: If final frame is reached we return to first frame
|
||||
currentAnimFrame++;
|
||||
if (currentAnimFrame >= animFrames)
|
||||
{
|
||||
currentAnimFrame = 0;
|
||||
}
|
||||
|
||||
// Get memory offset position for next frame data in image.data
|
||||
nextFrameDataOffset = (uint)(imScarfyAnim.Width * imScarfyAnim.Height * 4 * currentAnimFrame);
|
||||
|
||||
// Update GPU texture data with next frame image data
|
||||
// WARNING: Data size (frame size) and pixel format must match already created texture
|
||||
UpdateTexture(texScarfyAnim, (byte*)imScarfyAnim.Data + nextFrameDataOffset);
|
||||
|
||||
frameCounter = 0;
|
||||
}
|
||||
|
||||
// Control frames delay
|
||||
if (IsKeyPressed(KeyboardKey.Right))
|
||||
{
|
||||
frameDelay++;
|
||||
}
|
||||
else if (IsKeyPressed(KeyboardKey.Left))
|
||||
{
|
||||
frameDelay--;
|
||||
}
|
||||
|
||||
if (frameDelay > MaxFrameDelay)
|
||||
{
|
||||
frameDelay = MaxFrameDelay;
|
||||
}
|
||||
else if (frameDelay < MinFrameDelay)
|
||||
{
|
||||
frameDelay = MinFrameDelay;
|
||||
}
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(Color.RayWhite);
|
||||
|
||||
DrawText($"TOTAL GIF FRAMES: {animFrames:D2}", 50, 30, 20, Color.LightGray);
|
||||
DrawText($"CURRENT FRAME: {currentAnimFrame:D2}", 50, 60, 20, Color.Gray);
|
||||
DrawText($"CURRENT FRAME IMAGE.DATA OFFSET: {nextFrameDataOffset:D2}", 50, 90, 20, Color.Gray);
|
||||
|
||||
DrawText("FRAMES DELAY: ", 100, 305, 10, Color.DarkGray);
|
||||
DrawText($"{frameDelay:D2} frames", 620, 305, 10, Color.DarkGray);
|
||||
DrawText("PRESS RIGHT/LEFT KEYS to CHANGE SPEED!", 290, 350, 10, Color.DarkGray);
|
||||
|
||||
for (var i = 0; i < MaxFrameDelay; i++)
|
||||
{
|
||||
if (i < frameDelay)
|
||||
{
|
||||
DrawRectangle(190 + 21 * i, 300, 20, 20, Color.Red);
|
||||
}
|
||||
DrawRectangleLines(190 + 21 * i, 300, 20, 20, Color.Maroon);
|
||||
}
|
||||
|
||||
DrawTexture(texScarfyAnim, GetScreenWidth() / 2 - texScarfyAnim.Width / 2, 140, Color.White);
|
||||
|
||||
DrawText("(c) Scarfy sprite by Eiden Marsal", screenWidth - 200, screenHeight - 20, 10, Color.Gray);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
public void Unload()
|
||||
{
|
||||
UnloadTexture(texScarfyAnim); // Unload texture
|
||||
UnloadImage(imScarfyAnim); // Unload image (contains all frames)
|
||||
}
|
||||
|
||||
public static int Main()
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
InitWindow(screenWidth, screenHeight, "raylib [textures] example - gif player");
|
||||
|
||||
SetTargetFPS(60);
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
var game = new GifPlayer();
|
||||
game.Init();
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
game.Update();
|
||||
}
|
||||
|
||||
game.Unload();
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
149
Examples/Textures/ImageChannel.cs
Normal file
149
Examples/Textures/ImageChannel.cs
Normal file
|
|
@ -0,0 +1,149 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [textures] example - image channel
|
||||
*
|
||||
* NOTE: Images are loaded in CPU memory (RAM); textures are loaded in GPU memory (VRAM)
|
||||
*
|
||||
* Example complexity rating: [★★☆☆] 2/4
|
||||
*
|
||||
* Example originally created with raylib 5.5, last time updated with raylib 5.5
|
||||
*
|
||||
* Example contributed by Bruno Cabral (@brccabral) and reviewed by Ramon Santamaria (@raysan5)
|
||||
*
|
||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||
* BSD-like license that allows static linking with closed source software
|
||||
*
|
||||
* Copyright (c) 2024-2025 Bruno Cabral (@brccabral) and Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
using System.Numerics;
|
||||
using static Raylib_cs.Raylib;
|
||||
|
||||
namespace Examples.Textures;
|
||||
|
||||
public partial class ImageChannel : IExample
|
||||
{
|
||||
private const int screenWidth = 800;
|
||||
private const int screenHeight = 450;
|
||||
|
||||
public string Name => "Textures / Image Channel";
|
||||
|
||||
public string Title => "raylib [textures] example - image channel";
|
||||
|
||||
private Texture2D fudesumiTexture;
|
||||
private Texture2D textureAlpha;
|
||||
private Texture2D textureRed;
|
||||
private Texture2D textureGreen;
|
||||
private Texture2D textureBlue;
|
||||
private Texture2D backgroundTexture;
|
||||
|
||||
private Rectangle fudesumiRec;
|
||||
private Rectangle fudesumiPos;
|
||||
private Rectangle redPos;
|
||||
private Rectangle greenPos;
|
||||
private Rectangle bluePos;
|
||||
private Rectangle alphaPos;
|
||||
|
||||
public void Init()
|
||||
{
|
||||
var fudesumiImage = LoadImage("resources/fudesumi.png");
|
||||
|
||||
var imageAlpha = ImageFromChannel(fudesumiImage, 3);
|
||||
ImageAlphaMask(ref imageAlpha, imageAlpha);
|
||||
|
||||
var imageRed = ImageFromChannel(fudesumiImage, 0);
|
||||
ImageAlphaMask(ref imageRed, imageAlpha);
|
||||
|
||||
var imageGreen = ImageFromChannel(fudesumiImage, 1);
|
||||
ImageAlphaMask(ref imageGreen, imageAlpha);
|
||||
|
||||
var imageBlue = ImageFromChannel(fudesumiImage, 2);
|
||||
ImageAlphaMask(ref imageBlue, imageAlpha);
|
||||
|
||||
var backgroundImage = GenImageChecked(screenWidth, screenHeight, screenWidth / 20, screenHeight / 20, Color.Orange, Color.Yellow);
|
||||
|
||||
fudesumiTexture = LoadTextureFromImage(fudesumiImage);
|
||||
textureAlpha = LoadTextureFromImage(imageAlpha);
|
||||
textureRed = LoadTextureFromImage(imageRed);
|
||||
textureGreen = LoadTextureFromImage(imageGreen);
|
||||
textureBlue = LoadTextureFromImage(imageBlue);
|
||||
backgroundTexture = LoadTextureFromImage(backgroundImage);
|
||||
|
||||
fudesumiRec = new Rectangle(0, 0, fudesumiImage.Width, fudesumiImage.Height);
|
||||
|
||||
fudesumiPos = new Rectangle(50, 10, fudesumiImage.Width * 0.8f, fudesumiImage.Height * 0.8f);
|
||||
redPos = new Rectangle(410, 10, fudesumiPos.Width / 2.0f, fudesumiPos.Height / 2.0f);
|
||||
greenPos = new Rectangle(600, 10, fudesumiPos.Width / 2.0f, fudesumiPos.Height / 2.0f);
|
||||
bluePos = new Rectangle(410, 230, fudesumiPos.Width / 2.0f, fudesumiPos.Height / 2.0f);
|
||||
alphaPos = new Rectangle(600, 230, fudesumiPos.Width / 2.0f, fudesumiPos.Height / 2.0f);
|
||||
|
||||
UnloadImage(fudesumiImage);
|
||||
UnloadImage(imageAlpha);
|
||||
UnloadImage(imageRed);
|
||||
UnloadImage(imageGreen);
|
||||
UnloadImage(imageBlue);
|
||||
UnloadImage(backgroundImage);
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
// Nothing to update...
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
DrawTexture(backgroundTexture, 0, 0, Color.White);
|
||||
DrawTexturePro(fudesumiTexture, fudesumiRec, fudesumiPos, new Vector2(0, 0), 0, Color.White);
|
||||
|
||||
DrawTexturePro(textureRed, fudesumiRec, redPos, new Vector2(0, 0), 0, Color.Red);
|
||||
DrawTexturePro(textureGreen, fudesumiRec, greenPos, new Vector2(0, 0), 0, Color.Green);
|
||||
DrawTexturePro(textureBlue, fudesumiRec, bluePos, new Vector2(0, 0), 0, Color.Blue);
|
||||
DrawTexturePro(textureAlpha, fudesumiRec, alphaPos, new Vector2(0, 0), 0, Color.White);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
public void Unload()
|
||||
{
|
||||
UnloadTexture(backgroundTexture);
|
||||
UnloadTexture(fudesumiTexture);
|
||||
UnloadTexture(textureRed);
|
||||
UnloadTexture(textureGreen);
|
||||
UnloadTexture(textureBlue);
|
||||
UnloadTexture(textureAlpha);
|
||||
}
|
||||
|
||||
public static int Main()
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
InitWindow(screenWidth, screenHeight, "raylib [textures] example - image channel");
|
||||
|
||||
SetTargetFPS(60);
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
var game = new ImageChannel();
|
||||
game.Init();
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
game.Update();
|
||||
}
|
||||
|
||||
game.Unload();
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
165
Examples/Textures/ImageKernel.cs
Normal file
165
Examples/Textures/ImageKernel.cs
Normal file
|
|
@ -0,0 +1,165 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [textures] example - image kernel
|
||||
*
|
||||
* Example complexity rating: [★★★★] 4/4
|
||||
*
|
||||
* NOTE: Images are loaded in CPU memory (RAM); textures are loaded in GPU memory (VRAM)
|
||||
*
|
||||
* Example contributed by Karim Salem (@kimo-s) and reviewed by Ramon Santamaria (@raysan5)
|
||||
*
|
||||
* Example originally created with raylib 1.3, last time updated with raylib 1.3
|
||||
*
|
||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||
* BSD-like license that allows static linking with closed source software
|
||||
*
|
||||
* Copyright (c) 2015-2025 Karim Salem (@kimo-s)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
using static Raylib_cs.Raylib;
|
||||
|
||||
namespace Examples.Textures;
|
||||
|
||||
public partial class ImageKernel : IExample
|
||||
{
|
||||
private const int screenWidth = 800;
|
||||
private const int screenHeight = 450;
|
||||
|
||||
public string Name => "Textures / Image Kernel";
|
||||
|
||||
public string Title => "raylib [textures] example - image kernel";
|
||||
|
||||
private Texture2D texture;
|
||||
private Texture2D catSharpendTexture;
|
||||
private Texture2D catSobelTexture;
|
||||
private Texture2D catGaussianTexture;
|
||||
|
||||
private static void NormalizeKernel(float[] kernel, int size)
|
||||
{
|
||||
var sum = 0.0f;
|
||||
for (var i = 0; i < size; i++)
|
||||
{
|
||||
sum += kernel[i];
|
||||
}
|
||||
|
||||
if (sum != 0.0f)
|
||||
{
|
||||
for (var i = 0; i < size; i++)
|
||||
{
|
||||
kernel[i] /= sum;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Init()
|
||||
{
|
||||
var image = LoadImage("resources/cat.png"); // Loaded in CPU memory (RAM)
|
||||
|
||||
float[] gaussiankernel = {
|
||||
1.0f, 2.0f, 1.0f,
|
||||
2.0f, 4.0f, 2.0f,
|
||||
1.0f, 2.0f, 1.0f
|
||||
};
|
||||
|
||||
float[] sobelkernel = {
|
||||
1.0f, 0.0f, -1.0f,
|
||||
2.0f, 0.0f, -2.0f,
|
||||
1.0f, 0.0f, -1.0f
|
||||
};
|
||||
|
||||
float[] sharpenkernel = {
|
||||
0.0f, -1.0f, 0.0f,
|
||||
-1.0f, 5.0f, -1.0f,
|
||||
0.0f, -1.0f, 0.0f
|
||||
};
|
||||
|
||||
NormalizeKernel(gaussiankernel, 9);
|
||||
NormalizeKernel(sharpenkernel, 9);
|
||||
NormalizeKernel(sobelkernel, 9);
|
||||
|
||||
var catSharpend = ImageCopy(image);
|
||||
ImageKernelConvolution(ref catSharpend, sharpenkernel);
|
||||
|
||||
var catSobel = ImageCopy(image);
|
||||
ImageKernelConvolution(ref catSobel, sobelkernel);
|
||||
|
||||
var catGaussian = ImageCopy(image);
|
||||
|
||||
for (var i = 0; i < 6; i++)
|
||||
{
|
||||
ImageKernelConvolution(ref catGaussian, gaussiankernel);
|
||||
}
|
||||
|
||||
ImageCrop(ref image, new Rectangle(0, 0, 200, 450));
|
||||
ImageCrop(ref catGaussian, new Rectangle(0, 0, 200, 450));
|
||||
ImageCrop(ref catSobel, new Rectangle(0, 0, 200, 450));
|
||||
ImageCrop(ref catSharpend, new Rectangle(0, 0, 200, 450));
|
||||
|
||||
// Images converted to texture, GPU memory (VRAM)
|
||||
texture = LoadTextureFromImage(image);
|
||||
catSharpendTexture = LoadTextureFromImage(catSharpend);
|
||||
catSobelTexture = LoadTextureFromImage(catSobel);
|
||||
catGaussianTexture = LoadTextureFromImage(catGaussian);
|
||||
|
||||
// Once images have been converted to texture and uploaded to VRAM,
|
||||
// they can be unloaded from RAM
|
||||
UnloadImage(image);
|
||||
UnloadImage(catGaussian);
|
||||
UnloadImage(catSobel);
|
||||
UnloadImage(catSharpend);
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(Color.RayWhite);
|
||||
|
||||
DrawTexture(catSharpendTexture, 0, 0, Color.White);
|
||||
DrawTexture(catSobelTexture, 200, 0, Color.White);
|
||||
DrawTexture(catGaussianTexture, 400, 0, Color.White);
|
||||
DrawTexture(texture, 600, 0, Color.White);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
public void Unload()
|
||||
{
|
||||
UnloadTexture(texture);
|
||||
UnloadTexture(catGaussianTexture);
|
||||
UnloadTexture(catSobelTexture);
|
||||
UnloadTexture(catSharpendTexture);
|
||||
}
|
||||
|
||||
public static int Main()
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
InitWindow(screenWidth, screenHeight, "raylib [textures] example - image kernel");
|
||||
|
||||
SetTargetFPS(60);
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
var game = new ImageKernel();
|
||||
game.Init();
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
game.Update();
|
||||
}
|
||||
|
||||
game.Unload();
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
121
Examples/Textures/ImageRotate.cs
Normal file
121
Examples/Textures/ImageRotate.cs
Normal file
|
|
@ -0,0 +1,121 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [textures] example - image rotate
|
||||
*
|
||||
* Example complexity rating: [★★☆☆] 2/4
|
||||
*
|
||||
* Example originally created with raylib 1.0, last time updated with raylib 1.0
|
||||
*
|
||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||
* BSD-like license that allows static linking with closed source software
|
||||
*
|
||||
* Copyright (c) 2014-2025 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
using static Raylib_cs.Raylib;
|
||||
|
||||
namespace Examples.Textures;
|
||||
|
||||
public partial class ImageRotate : IExample
|
||||
{
|
||||
private const int screenWidth = 800;
|
||||
private const int screenHeight = 450;
|
||||
|
||||
private const int NumTextures = 3;
|
||||
|
||||
public string Name => "Textures / Image Rotate";
|
||||
|
||||
public string Title => "raylib [textures] example - image rotate";
|
||||
|
||||
private Texture2D[] textures;
|
||||
private int currentTexture;
|
||||
|
||||
public void Init()
|
||||
{
|
||||
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
|
||||
var image45 = LoadImage("resources/raylib_logo.png");
|
||||
var image90 = LoadImage("resources/raylib_logo.png");
|
||||
var imageNeg90 = LoadImage("resources/raylib_logo.png");
|
||||
|
||||
ImageRotate(ref image45, 45);
|
||||
ImageRotate(ref image90, 90);
|
||||
ImageRotate(ref imageNeg90, -90);
|
||||
|
||||
textures = new Texture2D[NumTextures];
|
||||
|
||||
textures[0] = LoadTextureFromImage(image45);
|
||||
textures[1] = LoadTextureFromImage(image90);
|
||||
textures[2] = LoadTextureFromImage(imageNeg90);
|
||||
|
||||
UnloadImage(image45);
|
||||
UnloadImage(image90);
|
||||
UnloadImage(imageNeg90);
|
||||
|
||||
currentTexture = 0;
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
if (IsMouseButtonPressed(MouseButton.Left) || IsKeyPressed(KeyboardKey.Right))
|
||||
{
|
||||
currentTexture = (currentTexture + 1) % NumTextures; // Cycle between the textures
|
||||
}
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(Color.RayWhite);
|
||||
|
||||
DrawTexture(
|
||||
textures[currentTexture],
|
||||
screenWidth / 2 - textures[currentTexture].Width / 2,
|
||||
screenHeight / 2 - textures[currentTexture].Height / 2,
|
||||
Color.White);
|
||||
|
||||
DrawText("Press LEFT MOUSE BUTTON to rotate the image clockwise", 250, 420, 10, Color.DarkGray);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
public void Unload()
|
||||
{
|
||||
for (var i = 0; i < NumTextures; i++)
|
||||
{
|
||||
UnloadTexture(textures[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public static int Main()
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
InitWindow(screenWidth, screenHeight, "raylib [textures] example - image rotate");
|
||||
|
||||
SetTargetFPS(60);
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
var game = new ImageRotate();
|
||||
game.Init();
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
game.Update();
|
||||
}
|
||||
|
||||
game.Unload();
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
160
Examples/Textures/MagnifyingGlass.cs
Normal file
160
Examples/Textures/MagnifyingGlass.cs
Normal file
|
|
@ -0,0 +1,160 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib textures example - magnifying glass
|
||||
*
|
||||
* Example complexity rating: [★★★☆] 3/4
|
||||
*
|
||||
* Example originally created with raylib 5.6, last time updated with raylib 5.6
|
||||
*
|
||||
* Example contributed by Luke Vaughan (@badram) and reviewed by Ramon Santamaria (@raysan5)
|
||||
*
|
||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||
* BSD-like license that allows static linking with closed source software
|
||||
*
|
||||
* Copyright (c) 2026 Luke Vaughan (@badram)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
using System.Numerics;
|
||||
using static Raylib_cs.Raylib;
|
||||
|
||||
namespace Examples.Textures;
|
||||
|
||||
public partial class MagnifyingGlass : IExample
|
||||
{
|
||||
private const int screenWidth = 800;
|
||||
private const int screenHeight = 450;
|
||||
|
||||
public string Name => "Textures / Magnifying Glass";
|
||||
|
||||
public string Title => "raylib [textures] example - magnifying glass";
|
||||
|
||||
private Texture2D bunny;
|
||||
private Texture2D parrots;
|
||||
private Texture2D mask;
|
||||
private RenderTexture2D magnifiedWorld;
|
||||
private Camera2D camera;
|
||||
|
||||
public void Init()
|
||||
{
|
||||
bunny = LoadTexture("resources/raybunny.png");
|
||||
parrots = LoadTexture("resources/parrots.png");
|
||||
|
||||
// Use image draw to generate a mask texture instead of loading it from a file.
|
||||
var circle = GenImageColor(256, 256, Color.Blank);
|
||||
ImageDrawCircle(ref circle, 128, 128, 128, Color.White);
|
||||
mask = LoadTextureFromImage(circle); // Copy the mask image from RAM to VRAM
|
||||
UnloadImage(circle); // Unload the image from RAM
|
||||
|
||||
magnifiedWorld = LoadRenderTexture(256, 256);
|
||||
|
||||
camera = new Camera2D();
|
||||
// Set magnifying glass zoom
|
||||
camera.Zoom = 2;
|
||||
// Offset by half the size of the magnifying glass to counteract drawing the texture centered on the mouse position
|
||||
camera.Offset = new Vector2(128, 128);
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
var mPos = GetMousePosition();
|
||||
camera.Target = mPos;
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(Color.RayWhite);
|
||||
|
||||
// Draw the normal version of the world
|
||||
DrawTexture(parrots, 144, 33, Color.White);
|
||||
DrawText("Use the magnifying glass to find hidden bunnies!", 154, 6, 20, Color.Black);
|
||||
|
||||
// Render to a the magnifying glass
|
||||
BeginTextureMode(magnifiedWorld);
|
||||
ClearBackground(Color.RayWhite);
|
||||
|
||||
BeginMode2D(camera);
|
||||
// Draw the same things in the magnified world as were in the normal version
|
||||
DrawTexture(parrots, 144, 33, Color.White);
|
||||
DrawText("Use the magnifying glass to find hidden bunnies!", 154, 6, 20, Color.Black);
|
||||
|
||||
// Draw bunnies only in the magnified world.
|
||||
// BLEND_MULTIPLIED lets them take on the color of the image below them.
|
||||
BeginBlendMode(BlendMode.Multiplied);
|
||||
DrawTexture(bunny, 250, 350, Color.White);
|
||||
DrawTexture(bunny, 500, 100, Color.White);
|
||||
DrawTexture(bunny, 420, 300, Color.White);
|
||||
DrawTexture(bunny, 650, 10, Color.White);
|
||||
EndBlendMode();
|
||||
EndMode2D();
|
||||
|
||||
// Mask the magnifying glass view texture to a circle
|
||||
// To make the mask affect only alpha, a CUSTOM blend mode is used with SEPARATE color/alpha functions
|
||||
BeginBlendMode(BlendMode.CustomSeparate);
|
||||
// C: Color, A: Alpha, s: source (texture to draw), d: destination (texture drawn to)
|
||||
// glSrcRGB: RL_ZERO - Cs * 0 = 0 - discard source rgb because we don't want to draw our texture's colors at all
|
||||
// glDstRGB: RL_ONE - Cd * 1 = Cd - use destination colors unmodified
|
||||
// glSrcAlpha: RL_ONE - As * 1 = As - use source alpha unmodified
|
||||
// glDstAlpha: RL_ZERO - Ad * 0 = 0 - discard destination alpha
|
||||
// glEqRGB: RL_FUNC_ADD - Cs(0) + Cd = Cd - destination color is unmodified
|
||||
// glEqAlpha: RL_FUNC_ADD - As + Ad(0) = As - destination alpha is set to source alpha
|
||||
Rlgl.SetBlendFactorsSeparate(Rlgl.ZERO, Rlgl.ONE, Rlgl.ONE, Rlgl.ZERO, Rlgl.FUNC_ADD, Rlgl.FUNC_ADD);
|
||||
DrawTexture(mask, 0, 0, Color.White);
|
||||
EndBlendMode();
|
||||
EndTextureMode();
|
||||
|
||||
// Draw magnifiedWorld to screen, centered on cursor
|
||||
DrawTextureRec(magnifiedWorld.Texture, new Rectangle(0, 0, 256, -256), new Vector2(mPos.X - 128, mPos.Y - 128), Color.White);
|
||||
|
||||
// Draw the outer ring of the magnifying glass
|
||||
DrawRing(mPos, 126, 130, 0, 360, 64, Color.Black);
|
||||
|
||||
// Draw floating specular highlight on the glass
|
||||
var rx = mPos.X / 800;
|
||||
var ry = mPos.Y / 800;
|
||||
DrawCircle((int)(mPos.X - 64 * rx) - 32, (int)(mPos.Y - 64 * ry) - 32, 4, ColorAlpha(Color.White, 0.5f));
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
public void Unload()
|
||||
{
|
||||
UnloadTexture(parrots);
|
||||
UnloadTexture(bunny);
|
||||
UnloadTexture(mask);
|
||||
UnloadRenderTexture(magnifiedWorld);
|
||||
}
|
||||
|
||||
public static int Main()
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
InitWindow(screenWidth, screenHeight, "raylib [textures] example - magnifying glass");
|
||||
|
||||
SetTargetFPS(60);
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
var game = new MagnifyingGlass();
|
||||
game.Init();
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
game.Update();
|
||||
}
|
||||
|
||||
game.Unload();
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
181
Examples/Textures/ScreenBuffer.cs
Normal file
181
Examples/Textures/ScreenBuffer.cs
Normal file
|
|
@ -0,0 +1,181 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [textures] example - screen buffer
|
||||
*
|
||||
* Example complexity rating: [★★☆☆] 2/4
|
||||
*
|
||||
* Example originally created with raylib 5.5, last time updated with raylib 5.5
|
||||
*
|
||||
* Example contributed by Agnis Aldiņš (@nezvers) and reviewed by Ramon Santamaria (@raysan5)
|
||||
*
|
||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||
* BSD-like license that allows static linking with closed source software
|
||||
*
|
||||
* Copyright (c) 2025 Agnis Aldiņš (@nezvers)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
using System.Numerics;
|
||||
using static Raylib_cs.Raylib;
|
||||
|
||||
namespace Examples.Textures;
|
||||
|
||||
public partial class ScreenBuffer : IExample
|
||||
{
|
||||
private const int screenWidth = 800;
|
||||
private const int screenHeight = 450;
|
||||
|
||||
private const int MaxColors = 256;
|
||||
private const int ScaleFactor = 2;
|
||||
|
||||
private const int imageWidth = screenWidth / ScaleFactor;
|
||||
private const int imageHeight = screenHeight / ScaleFactor;
|
||||
private const int flameWidth = screenWidth / ScaleFactor;
|
||||
|
||||
public string Name => "Textures / Screen Buffer";
|
||||
|
||||
public string Title => "raylib [textures] example - screen buffer";
|
||||
|
||||
private Color[] palette;
|
||||
private byte[] indexBuffer;
|
||||
private byte[] flameRootBuffer;
|
||||
private Image screenImage;
|
||||
private Texture2D screenTexture;
|
||||
|
||||
public void Init()
|
||||
{
|
||||
palette = new Color[MaxColors];
|
||||
indexBuffer = new byte[imageWidth * imageWidth];
|
||||
flameRootBuffer = new byte[flameWidth];
|
||||
|
||||
screenImage = GenImageColor(imageWidth, imageHeight, Color.Black);
|
||||
screenTexture = LoadTextureFromImage(screenImage);
|
||||
|
||||
// Generate flame color palette
|
||||
for (var i = 0; i < MaxColors; i++)
|
||||
{
|
||||
var t = (float)i / (float)(MaxColors - 1);
|
||||
var hue = t * t;
|
||||
var saturation = t;
|
||||
var value = t;
|
||||
|
||||
palette[i] = ColorFromHSV(250.0f + 150.0f * hue, saturation, value);
|
||||
}
|
||||
}
|
||||
|
||||
public unsafe void Update()
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
// Grow flameRoot
|
||||
for (var x = 2; x < flameWidth; x++)
|
||||
{
|
||||
var flame = (int)flameRootBuffer[x];
|
||||
flame += GetRandomValue(0, 2);
|
||||
flameRootBuffer[x] = (flame > 255) ? (byte)255 : (byte)flame;
|
||||
}
|
||||
|
||||
// Transfer flameRoot to indexBuffer
|
||||
for (var x = 0; x < flameWidth; x++)
|
||||
{
|
||||
var i = x + (imageHeight - 1) * imageWidth;
|
||||
indexBuffer[i] = flameRootBuffer[x];
|
||||
}
|
||||
|
||||
// Clear top row, because it can't move any higher
|
||||
for (var x = 0; x < imageWidth; x++)
|
||||
{
|
||||
if (indexBuffer[x] != 0)
|
||||
{
|
||||
indexBuffer[x] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Skip top row, it is already cleared
|
||||
for (var y = 1; y < imageHeight; y++)
|
||||
{
|
||||
for (var x = 0; x < imageWidth; x++)
|
||||
{
|
||||
var i = x + y * imageWidth;
|
||||
int colorIndex = indexBuffer[i];
|
||||
|
||||
if (colorIndex != 0)
|
||||
{
|
||||
// Move pixel a row above
|
||||
indexBuffer[i] = 0;
|
||||
var moveX = GetRandomValue(0, 2) - 1;
|
||||
var newX = x + moveX;
|
||||
|
||||
if ((newX > 0) && (newX < imageWidth))
|
||||
{
|
||||
var iabove = i - imageWidth + moveX;
|
||||
var decay = GetRandomValue(0, 3);
|
||||
colorIndex -= (decay < colorIndex) ? decay : colorIndex;
|
||||
indexBuffer[iabove] = (byte)colorIndex;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Update screenImage with palette colors
|
||||
for (var y = 1; y < imageHeight; y++)
|
||||
{
|
||||
for (var x = 0; x < imageWidth; x++)
|
||||
{
|
||||
var i = x + y * imageWidth;
|
||||
int colorIndex = indexBuffer[i];
|
||||
var col = palette[colorIndex];
|
||||
|
||||
ImageDrawPixel(ref screenImage, x, y, col);
|
||||
}
|
||||
}
|
||||
|
||||
UpdateTexture(screenTexture, screenImage.Data);
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(Color.RayWhite);
|
||||
|
||||
DrawTextureEx(screenTexture, new Vector2(0, 0), 0.0f, 2.0f, Color.White);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
public void Unload()
|
||||
{
|
||||
UnloadTexture(screenTexture);
|
||||
UnloadImage(screenImage);
|
||||
}
|
||||
|
||||
public static int Main()
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
InitWindow(screenWidth, screenHeight, "raylib [textures] example - screen buffer");
|
||||
|
||||
SetTargetFPS(60);
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
var game = new ScreenBuffer();
|
||||
game.Init();
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
game.Update();
|
||||
}
|
||||
|
||||
game.Unload();
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
140
Examples/Textures/SpriteStacking.cs
Normal file
140
Examples/Textures/SpriteStacking.cs
Normal file
|
|
@ -0,0 +1,140 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [textures] example - sprite stacking
|
||||
*
|
||||
* Example complexity rating: [★★☆☆] 2/4
|
||||
*
|
||||
* Example originally created with raylib 6.0, last time updated with raylib 6.0
|
||||
*
|
||||
* Example contributed by Robin (@RobinsAviary) and reviewed by Ramon Santamaria (@raysan5)
|
||||
*
|
||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||
* BSD-like license that allows static linking with closed source software
|
||||
*
|
||||
* Redbooth model (c) 2017-2025 @kluchek under https://creativecommons.org/licenses/by/4.0/ https://github.com/kluchek/vox-models/
|
||||
* Copyright (c) 2025 Robin (@RobinsAviary)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
using System.Numerics;
|
||||
using static Raylib_cs.Raylib;
|
||||
|
||||
namespace Examples.Textures;
|
||||
|
||||
public partial class SpriteStacking : IExample
|
||||
{
|
||||
private const int screenWidth = 800;
|
||||
private const int screenHeight = 450;
|
||||
|
||||
private const float speedChange = 0.25f; // Amount speed will change by when the user presses A/D
|
||||
|
||||
public string Name => "Textures / Sprite Stacking";
|
||||
|
||||
public string Title => "raylib [textures] example - sprite stacking";
|
||||
|
||||
private Texture2D booth;
|
||||
private float stackScale; // Overall scale of the stacked sprite
|
||||
private float stackSpacing; // Vertical spacing between each layer
|
||||
private uint stackCount; // Number of layers, used for calculating the size of a single slice
|
||||
private float rotationSpeed; // Stacked sprites rotation speed
|
||||
private float rotation; // Current rotation of the stacked sprite
|
||||
|
||||
public void Init()
|
||||
{
|
||||
booth = LoadTexture("resources/booth.png");
|
||||
|
||||
stackScale = 3.0f;
|
||||
stackSpacing = 2.0f;
|
||||
stackCount = 122;
|
||||
rotationSpeed = 30.0f;
|
||||
rotation = 0.0f;
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
// Use mouse wheel to affect stack separation
|
||||
stackSpacing += GetMouseWheelMove() * 0.1f;
|
||||
stackSpacing = Math.Clamp(stackSpacing, 0.0f, 5.0f);
|
||||
|
||||
// Add a positive/negative offset to spin right/left at different speeds
|
||||
if (IsKeyDown(KeyboardKey.Left) || IsKeyDown(KeyboardKey.A))
|
||||
{
|
||||
rotationSpeed -= speedChange;
|
||||
}
|
||||
if (IsKeyDown(KeyboardKey.Right) || IsKeyDown(KeyboardKey.D))
|
||||
{
|
||||
rotationSpeed += speedChange;
|
||||
}
|
||||
|
||||
rotation += rotationSpeed * GetFrameTime();
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(Color.RayWhite);
|
||||
|
||||
// Get the size of a single slice
|
||||
var frameWidth = (float)booth.Width;
|
||||
var frameHeight = (float)booth.Height / (float)stackCount;
|
||||
|
||||
// Get the scaled resolution to draw at
|
||||
var scaledWidth = frameWidth * stackScale;
|
||||
var scaledHeight = frameHeight * stackScale;
|
||||
|
||||
// Draw the stacked sprite, rotated to the correct angle, with an vertical offset applied based on its y location
|
||||
for (var i = (int)stackCount - 1; i >= 0; i--)
|
||||
{
|
||||
// Center vertically
|
||||
Rectangle source = new(0.0f, (float)i * frameHeight, frameWidth, frameHeight);
|
||||
Rectangle dest = new(screenWidth / 2.0f, (screenHeight / 2.0f) + (i * stackSpacing) - (stackSpacing * stackCount / 2.0f), scaledWidth, scaledHeight);
|
||||
Vector2 origin = new(scaledWidth / 2.0f, scaledHeight / 2.0f);
|
||||
|
||||
DrawTexturePro(booth, source, dest, origin, rotation, Color.White);
|
||||
}
|
||||
|
||||
DrawText("A/D to spin\nmouse wheel to change separation (aka 'angle')", 10, 10, 20, Color.DarkGray);
|
||||
DrawText($"current spacing: {stackSpacing:F1}", 10, 50, 20, Color.DarkGray);
|
||||
DrawText($"current speed: {rotationSpeed:F2}", 10, 70, 20, Color.DarkGray);
|
||||
DrawText("redbooth model (c) kluchek under cc 4.0", 10, 420, 20, Color.DarkGray);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
public void Unload()
|
||||
{
|
||||
UnloadTexture(booth);
|
||||
}
|
||||
|
||||
public static int Main()
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
InitWindow(screenWidth, screenHeight, "raylib [textures] example - sprite stacking");
|
||||
|
||||
SetTargetFPS(60);
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
var game = new SpriteStacking();
|
||||
game.Init();
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
game.Update();
|
||||
}
|
||||
|
||||
game.Unload();
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue