2
0
mirror of https://github.com/raylib-cs/raylib-cs synced 2025-06-30 19:03:42 -04:00

Update/merge examples back into main repo (#192)

This commit is contained in:
2023-08-25 08:08:32 +01:00
committed by GitHub
parent fa682b00ac
commit bcdf8cec5b
320 changed files with 57965 additions and 1 deletions

View File

@ -0,0 +1,116 @@
/*******************************************************************************************
*
* raylib [textures] example - Background scrolling
*
* This example has been created using raylib 2.0 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2019 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
using System.Numerics;
using static Raylib_cs.Raylib;
namespace Examples.Textures;
public class BackgroundScrolling
{
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [textures] example - background scrolling");
// NOTE: Be careful, background width must be equal or bigger than screen width
// if not, texture should be draw more than two times for scrolling effect
Texture2D background = LoadTexture("resources/cyberpunk_street_background.png");
Texture2D midground = LoadTexture("resources/cyberpunk_street_midground.png");
Texture2D foreground = LoadTexture("resources/cyberpunk_street_foreground.png");
float scrollingBack = 0.0f;
float scrollingMid = 0.0f;
float scrollingFore = 0.0f;
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose())
{
// Update
//----------------------------------------------------------------------------------
scrollingBack -= 0.1f;
scrollingMid -= 0.5f;
scrollingFore -= 1.0f;
// NOTE: Texture is scaled twice its size, so it sould be considered on scrolling
if (scrollingBack <= -background.Width * 2)
{
scrollingBack = 0;
}
if (scrollingMid <= -midground.Width * 2)
{
scrollingMid = 0;
}
if (scrollingFore <= -foreground.Width * 2)
{
scrollingFore = 0;
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(GetColor(0x052c46ff));
// Draw background image twice
// NOTE: Texture is scaled twice its size
DrawTextureEx(background, new Vector2(scrollingBack, 20), 0.0f, 2.0f, Color.WHITE);
DrawTextureEx(
background,
new Vector2(background.Width * 2 + scrollingBack, 20),
0.0f,
2.0f,
Color.WHITE
);
// Draw midground image twice
DrawTextureEx(midground, new Vector2(scrollingMid, 20), 0.0f, 2.0f, Color.WHITE);
DrawTextureEx(midground, new Vector2(midground.Width * 2 + scrollingMid, 20), 0.0f, 2.0f, Color.WHITE);
// Draw foreground image twice
DrawTextureEx(foreground, new Vector2(scrollingFore, 70), 0.0f, 2.0f, Color.WHITE);
DrawTextureEx(
foreground,
new Vector2(foreground.Width * 2 + scrollingFore, 70),
0.0f,
2.0f,
Color.WHITE
);
DrawText("BACKGROUND SCROLLING & PARALLAX", 10, 10, 20, Color.RED);
int x = screenWidth - 330;
int y = screenHeight - 20;
DrawText("(c) Cyberpunk Street Environment by Luis Zuno (@ansimuz)", x, y, 10, Color.RAYWHITE);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(background);
UnloadTexture(midground);
UnloadTexture(foreground);
CloseWindow();
//--------------------------------------------------------------------------------------
return 0;
}
}

View File

@ -0,0 +1,117 @@
/*******************************************************************************************
*
* raylib [textures] example - blend modes
*
* NOTE: Images are loaded in CPU memory (RAM); textures are loaded in GPU memory (VRAM)
*
* This example has been created using raylib 3.5 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Example contributed by Karlo Licudine (@accidentalrebel) and reviewed by Ramon Santamaria (@raysan5)
*
* Copyright (c) 2020 Karlo Licudine (@accidentalrebel)
*
********************************************************************************************/
using static Raylib_cs.Raylib;
namespace Examples.Textures;
public class BlendModes
{
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [textures] example - blend modes");
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
Image bgImage = LoadImage("resources/cyberpunk_street_background.png");
Texture2D bgTexture = LoadTextureFromImage(bgImage);
Image fgImage = LoadImage("resources/cyberpunk_street_foreground.png");
Texture2D fgTexture = LoadTextureFromImage(fgImage);
// Once image has been converted to texture and uploaded to VRAM, it can be unloaded from RAM
UnloadImage(bgImage);
UnloadImage(fgImage);
const int blendCountMax = 4;
BlendMode blendMode = 0;
// Main game loop
while (!WindowShouldClose())
{
// Update
//----------------------------------------------------------------------------------
if (IsKeyPressed(KeyboardKey.KEY_SPACE))
{
if ((int)blendMode >= (blendCountMax - 1))
{
blendMode = 0;
}
else
{
blendMode++;
}
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RAYWHITE);
int bgX = screenWidth / 2 - bgTexture.Width / 2;
int bgY = screenHeight / 2 - bgTexture.Height / 2;
DrawTexture(bgTexture, bgX, bgY, Color.WHITE);
// Apply the blend mode and then draw the foreground texture
BeginBlendMode(blendMode);
int fgX = screenWidth / 2 - fgTexture.Width / 2;
int fgY = screenHeight / 2 - fgTexture.Height / 2;
DrawTexture(fgTexture, fgX, fgY, Color.WHITE);
EndBlendMode();
// Draw the texts
DrawText("Press SPACE to change blend modes.", 310, 350, 10, Color.GRAY);
switch (blendMode)
{
case BlendMode.BLEND_ALPHA:
DrawText("Current: BLEND_ALPHA", (screenWidth / 2) - 60, 370, 10, Color.GRAY);
break;
case BlendMode.BLEND_ADDITIVE:
DrawText("Current: BLEND_ADDITIVE", (screenWidth / 2) - 60, 370, 10, Color.GRAY);
break;
case BlendMode.BLEND_MULTIPLIED:
DrawText("Current: BLEND_MULTIPLIED", (screenWidth / 2) - 60, 370, 10, Color.GRAY);
break;
case BlendMode.BLEND_ADD_COLORS:
DrawText("Current: BLEND_ADD_COLORS", (screenWidth / 2) - 60, 370, 10, Color.GRAY);
break;
default:
break;
}
string text = "(c) Cyberpunk Street Environment by Luis Zuno (@ansimuz)";
DrawText(text, screenWidth - 330, screenHeight - 20, 10, Color.GRAY);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(fgTexture);
UnloadTexture(bgTexture);
CloseWindow();
//--------------------------------------------------------------------------------------
return 0;
}
}

View File

@ -0,0 +1,133 @@
/*******************************************************************************************
*
* raylib [textures] example - Bunnymark
*
* This example has been created using raylib 1.6 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2014-2019 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
using System.Numerics;
using static Raylib_cs.Raylib;
namespace Examples.Textures;
public class Bunnymark
{
public const int MaxBunnies = 150000;
// This is the maximum amount of elements (quads) per batch
// NOTE: This value is defined in [rlgl] module and can be changed there
public const int MAX_BATCH_ELEMENTS = 8192;
struct Bunny
{
public Vector2 Position;
public Vector2 Speed;
public Color Color;
}
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [textures] example - bunnymark");
// Load bunny texture
Texture2D texBunny = LoadTexture("resources/wabbit_alpha.png");
// 50K bunnies limit
Bunny[] bunnies = new Bunny[MaxBunnies];
int bunniesCount = 0;
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose())
{
// Update
//----------------------------------------------------------------------------------
if (IsMouseButtonDown(MouseButton.MOUSE_LEFT_BUTTON))
{
// Create more bunnies
for (int i = 0; i < 100; i++)
{
if (bunniesCount < MaxBunnies)
{
bunnies[bunniesCount].Position = GetMousePosition();
bunnies[bunniesCount].Speed.X = (float)GetRandomValue(-250, 250) / 60.0f;
bunnies[bunniesCount].Speed.Y = (float)GetRandomValue(-250, 250) / 60.0f;
bunnies[bunniesCount].Color = new Color(
GetRandomValue(50, 240),
GetRandomValue(80, 240),
GetRandomValue(100, 240), 255
);
bunniesCount++;
}
}
}
// Update bunnies
Vector2 screen = new(GetScreenWidth(), GetScreenHeight());
Vector2 halfSize = new Vector2(texBunny.Width, texBunny.Height) / 2;
for (int i = 0; i < bunniesCount; i++)
{
bunnies[i].Position += bunnies[i].Speed;
if (((bunnies[i].Position.X + halfSize.X) > screen.X) ||
((bunnies[i].Position.X + halfSize.X) < 0))
{
bunnies[i].Speed.X *= -1;
}
if (((bunnies[i].Position.Y + halfSize.Y) > screen.Y) ||
((bunnies[i].Position.Y + halfSize.Y - 40) < 0))
{
bunnies[i].Speed.Y *= -1;
}
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RAYWHITE);
for (int i = 0; i < bunniesCount; i++)
{
// NOTE: When internal batch buffer limit is reached (MAX_BATCH_ELEMENTS),
// a draw call is launched and buffer starts being filled again;
// before issuing a draw call, updated vertex data from internal CPU buffer is send to GPU...
// Process of sending data is costly and it could happen that GPU data has not been completely
// processed for drawing while new data is tried to be sent (updating current in-use buffers)
// it could generates a stall and consequently a frame drop, limiting the number of drawn bunnies
DrawTexture(texBunny, (int)bunnies[i].Position.X, (int)bunnies[i].Position.Y, bunnies[i].Color);
}
DrawRectangle(0, 0, screenWidth, 40, Color.BLACK);
DrawText($"bunnies: {bunniesCount}", 120, 10, 20, Color.GREEN);
DrawText($"batched draw calls: {1 + bunniesCount / MAX_BATCH_ELEMENTS}", 320, 10, 20, Color.MAROON);
DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(texBunny);
CloseWindow();
//--------------------------------------------------------------------------------------
return 0;
}
}

View File

@ -0,0 +1,449 @@
/*******************************************************************************************
*
* raylib [textures] example - Draw part of the texture tiled
*
* This example has been created using raylib 3.0 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2020 Vlad Adrian (@demizdor) and Ramon Santamaria (@raysan5)
*
********************************************************************************************/
using System.Numerics;
using static Raylib_cs.Raylib;
namespace Examples.Textures;
public class DrawTiled
{
const int OptWidth = 220;
const int MarginSize = 8;
const int ColorSize = 16;
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
int screenWidth = 800;
int screenHeight = 450;
SetConfigFlags(ConfigFlags.FLAG_WINDOW_RESIZABLE);
InitWindow(screenWidth, screenHeight, "raylib [textures] example - Draw part of a texture tiled");
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
Texture2D texPattern = LoadTexture("resources/patterns.png");
// Makes the texture smoother when upscaled
SetTextureFilter(texPattern, TextureFilter.TEXTURE_FILTER_TRILINEAR);
// Coordinates for all patterns inside the texture
Rectangle[] recPattern = new[] {
new Rectangle(3, 3, 66, 66),
new Rectangle(75, 3, 100, 100),
new Rectangle(3, 75, 66, 66),
new Rectangle(7, 156, 50, 50),
new Rectangle(85, 106, 90, 45),
new Rectangle(75, 154, 100, 60)
};
// Setup colors
Color[] colors = new[]
{
Color.BLACK,
Color.MAROON,
Color.ORANGE,
Color.BLUE,
Color.PURPLE,
Color.BEIGE,
Color.LIME,
Color.RED,
Color.DARKGRAY,
Color.SKYBLUE
};
Rectangle[] colorRec = new Rectangle[colors.Length];
// Calculate rectangle for each color
for (int i = 0, x = 0, y = 0; i < colors.Length; i++)
{
colorRec[i].X = 2 + MarginSize + x;
colorRec[i].Y = 22 + 256 + MarginSize + y;
colorRec[i].Width = ColorSize * 2;
colorRec[i].Height = ColorSize;
if (i == (colors.Length / 2 - 1))
{
x = 0;
y += ColorSize + MarginSize;
}
else
{
x += (ColorSize * 2 + MarginSize);
}
}
int activePattern = 0, activeCol = 0;
float scale = 1.0f, rotation = 0.0f;
SetTargetFPS(60);
//---------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose())
{
// Update
//----------------------------------------------------------------------------------
screenWidth = GetScreenWidth();
screenHeight = GetScreenHeight();
// Handle mouse
if (IsMouseButtonPressed(MouseButton.MOUSE_LEFT_BUTTON))
{
Vector2 mouse = GetMousePosition();
// Check which pattern was clicked and set it as the active pattern
for (int i = 0; i < recPattern.Length; i++)
{
Rectangle rec = new(
2 + MarginSize + recPattern[i].X,
40 + MarginSize + recPattern[i].Y,
recPattern[i].Width,
recPattern[i].Height
);
if (CheckCollisionPointRec(mouse, rec))
{
activePattern = i;
break;
}
}
// Check to see which color was clicked and set it as the active color
for (int i = 0; i < colors.Length; ++i)
{
if (CheckCollisionPointRec(mouse, colorRec[i]))
{
activeCol = i;
break;
}
}
}
// Handle keys
// Change scale
if (IsKeyPressed(KeyboardKey.KEY_UP))
{
scale += 0.25f;
}
if (IsKeyPressed(KeyboardKey.KEY_DOWN))
{
scale -= 0.25f;
}
if (scale > 10.0f)
{
scale = 10.0f;
}
else if (scale <= 0.0f)
{
scale = 0.25f;
}
// Change rotation
if (IsKeyPressed(KeyboardKey.KEY_LEFT))
{
rotation -= 25.0f;
}
if (IsKeyPressed(KeyboardKey.KEY_RIGHT))
{
rotation += 25.0f;
}
// Reset
if (IsKeyPressed(KeyboardKey.KEY_SPACE))
{
rotation = 0.0f;
scale = 1.0f;
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RAYWHITE);
// Draw the tiled area
Rectangle source = recPattern[activePattern];
Rectangle dest = new(
OptWidth + MarginSize,
MarginSize,
screenWidth - OptWidth - 2 * MarginSize,
screenHeight - 2 * MarginSize
);
DrawTextureTiled(texPattern, source, dest, Vector2.Zero, rotation, scale, colors[activeCol]);
// Draw options
Color color = ColorAlpha(Color.LIGHTGRAY, 0.5f);
DrawRectangle(MarginSize, MarginSize, OptWidth - MarginSize, screenHeight - 2 * MarginSize, color);
DrawText("Select Pattern", 2 + MarginSize, 30 + MarginSize, 10, Color.BLACK);
DrawTexture(texPattern, 2 + MarginSize, 40 + MarginSize, Color.BLACK);
DrawRectangle(
2 + MarginSize + (int)recPattern[activePattern].X,
40 + MarginSize + (int)recPattern[activePattern].Y,
(int)recPattern[activePattern].Width,
(int)recPattern[activePattern].Height,
ColorAlpha(Color.DARKBLUE, 0.3f)
);
DrawText("Select Color", 2 + MarginSize, 10 + 256 + MarginSize, 10, Color.BLACK);
for (int i = 0; i < colors.Length; i++)
{
DrawRectangleRec(colorRec[i], colors[i]);
if (activeCol == i)
{
DrawRectangleLinesEx(colorRec[i], 3, ColorAlpha(Color.WHITE, 0.5f));
}
}
DrawText("Scale (UP/DOWN to change)", 2 + MarginSize, 80 + 256 + MarginSize, 10, Color.BLACK);
DrawText($"{scale}x", 2 + MarginSize, 92 + 256 + MarginSize, 20, Color.BLACK);
DrawText("Rotation (LEFT/RIGHT to change)", 2 + MarginSize, 122 + 256 + MarginSize, 10, Color.BLACK);
DrawText($"{rotation} degrees", 2 + MarginSize, 134 + 256 + MarginSize, 20, Color.BLACK);
DrawText("Press [SPACE] to reset", 2 + MarginSize, 164 + 256 + MarginSize, 10, Color.DARKBLUE);
// Draw FPS
DrawText($"{GetFPS()}", 2 + MarginSize, 2 + MarginSize, 20, Color.BLACK);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(texPattern);
CloseWindow();
//--------------------------------------------------------------------------------------
return 0;
}
// Draw part of a texture (defined by a rectangle) with rotation and scale tiled into dest.
static void DrawTextureTiled(
Texture2D texture,
Rectangle source,
Rectangle dest,
Vector2 origin,
float rotation,
float scale,
Color tint
)
{
if ((texture.Id <= 0) || (scale <= 0.0f))
{
// Wanna see a infinite loop?!...just delete this line!
return;
}
if ((source.Width == 0) || (source.Height == 0))
{
return;
}
int tileWidth = (int)(source.Width * scale), tileHeight = (int)(source.Height * scale);
if ((dest.Width < tileWidth) && (dest.Height < tileHeight))
{
// Can fit only one tile
DrawTexturePro(
texture,
new Rectangle(
source.X,
source.Y,
((float)dest.Width / tileWidth) * source.Width,
((float)dest.Height / tileHeight) * source.Height
),
new Rectangle(dest.X, dest.Y, dest.Width, dest.Height), origin, rotation, tint
);
}
else if (dest.Width <= tileWidth)
{
// Tiled vertically (one column)
int dy = 0;
for (; dy + tileHeight < dest.Height; dy += tileHeight)
{
DrawTexturePro(
texture,
new Rectangle(
source.X,
source.Y,
((float)dest.Width / tileWidth) * source.Width,
source.Height
),
new Rectangle(dest.X, dest.Y + dy, dest.Width, (float)tileHeight),
origin,
rotation,
tint
);
}
// Fit last tile
if (dy < dest.Height)
{
DrawTexturePro(
texture,
new Rectangle(
source.X,
source.Y,
((float)dest.Width / tileWidth) * source.Width,
((float)(dest.Height - dy) / tileHeight) * source.Height
),
new Rectangle(dest.X, dest.Y + dy, dest.Width, dest.Height - dy),
origin,
rotation,
tint
);
}
}
else if (dest.Height <= tileHeight)
{
// Tiled horizontally (one row)
int dx = 0;
for (; dx + tileWidth < dest.Width; dx += tileWidth)
{
DrawTexturePro(
texture,
new Rectangle(
source.X,
source.Y,
source.Width,
((float)dest.Height / tileHeight) * source.Height
),
new Rectangle(dest.X + dx, dest.Y, (float)tileWidth, dest.Height),
origin,
rotation,
tint
);
}
// Fit last tile
if (dx < dest.Width)
{
DrawTexturePro(
texture,
new Rectangle(
source.X,
source.Y,
((float)(dest.Width - dx) / tileWidth) * source.Width,
((float)dest.Height / tileHeight) * source.Height
),
new Rectangle(
dest.X + dx,
dest.Y,
dest.Width - dx,
dest.Height
),
origin,
rotation,
tint
);
}
}
else
{
// Tiled both horizontally and vertically (rows and columns)
int dx = 0;
for (; dx + tileWidth < dest.Width; dx += tileWidth)
{
int dy = 0;
for (; dy + tileHeight < dest.Height; dy += tileHeight)
{
DrawTexturePro(
texture,
source,
new Rectangle(
dest.X + dx,
dest.Y + dy,
(float)tileWidth,
(float)tileHeight
),
origin,
rotation,
tint
);
}
if (dy < dest.Height)
{
DrawTexturePro(
texture,
new Rectangle(
source.X,
source.Y,
source.Width,
((float)(dest.Height - dy) / tileHeight) * source.Height
),
new Rectangle(
dest.X + dx,
dest.Y + dy,
(float)tileWidth, dest.Height - dy
),
origin,
rotation,
tint
);
}
}
// Fit last column of tiles
if (dx < dest.Width)
{
int dy = 0;
for (; dy + tileHeight < dest.Height; dy += tileHeight)
{
DrawTexturePro(
texture,
new Rectangle(
source.X,
source.Y,
((float)(dest.Width - dx) / tileWidth) * source.Width,
source.Height
),
new Rectangle(
dest.X + dx,
dest.Y + dy,
dest.Width - dx,
(float)tileHeight
),
origin,
rotation,
tint
);
}
// Draw final tile in the bottom right corner
if (dy < dest.Height)
{
DrawTexturePro(
texture,
new Rectangle(
source.X,
source.Y,
((float)(dest.Width - dx) / tileWidth) * source.Width,
((float)(dest.Height - dy) / tileHeight) * source.Height
),
new Rectangle(
dest.X + dx,
dest.Y + dy,
dest.Width - dx,
dest.Height - dy
),
origin,
rotation,
tint
);
}
}
}
}
}

View File

@ -0,0 +1,102 @@
/*******************************************************************************************
*
* raylib [textures] example - Image loading and drawing on it
*
* NOTE: Images are loaded in CPU memory (RAM); textures are loaded in GPU memory (VRAM)
*
* This example has been created using raylib 1.4 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2016 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
using System.Numerics;
using static Raylib_cs.Raylib;
namespace Examples.Textures;
public class ImageDrawing
{
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [textures] example - image drawing");
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
Image cat = LoadImage("resources/cat.png");
ImageCrop(ref cat, new Rectangle(100, 10, 280, 380));
ImageFlipHorizontal(ref cat);
ImageResize(ref cat, 150, 200);
Image parrots = LoadImage("resources/parrots.png");
// Draw one image over the other with a scaling of 1.5f
Rectangle src = new(0, 0, cat.Width, cat.Height);
ImageDraw(ref parrots, cat, src, new Rectangle(30, 40, cat.Width * 1.5f, cat.Height * 1.5f), Color.WHITE);
ImageCrop(ref parrots, new Rectangle(0, 50, parrots.Width, parrots.Height - 100));
// Draw on the image with a few image draw methods
ImageDrawPixel(ref parrots, 10, 10, Color.RAYWHITE);
ImageDrawCircle(ref parrots, 10, 10, 5, Color.RAYWHITE);
ImageDrawRectangle(ref parrots, 5, 20, 10, 10, Color.RAYWHITE);
UnloadImage(cat);
// Load custom font for frawing on image
Font font = LoadFont("resources/fonts/custom_jupiter_crash.png");
// Draw over image using custom font
ImageDrawTextEx(ref parrots, font, "PARROTS & CAT", new Vector2(300, 230), font.BaseSize, -2, Color.WHITE);
// Unload custom spritefont (already drawn used on image)
UnloadFont(font);
Texture2D texture = LoadTextureFromImage(parrots);
UnloadImage(parrots);
SetTargetFPS(60);
//---------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose())
{
// Update
//----------------------------------------------------------------------------------
// TODO: Update your variables here
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RAYWHITE);
int x = screenWidth / 2 - texture.Width / 2;
int y = screenHeight / 2 - texture.Height / 2;
DrawTexture(texture, x, y - 40, Color.WHITE);
DrawRectangleLines(x, y - 40, texture.Width, texture.Height, Color.DARKGRAY);
DrawText("We are drawing only one texture from various images composed!", 240, 350, 10, Color.DARKGRAY);
string text = "Source images have been cropped, scaled, flipped and copied one over the other.";
DrawText(text, 90, 370, 10, Color.DARKGRAY);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(texture);
CloseWindow();
//--------------------------------------------------------------------------------------
return 0;
}
}

View File

@ -0,0 +1,119 @@
/*******************************************************************************************
*
* raylib [textures] example - Procedural images generation
*
* This example has been created using raylib 1.8 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2O17 Wilhem Barbier (@nounoursheureux)
*
********************************************************************************************/
using static Raylib_cs.Raylib;
namespace Examples.Textures;
public class ImageGeneration
{
public const int NumTextures = 6;
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [textures] example - procedural images generation");
Image verticalGradient = GenImageGradientV(screenWidth, screenHeight, Color.RED, Color.BLUE);
Image horizontalGradient = GenImageGradientH(screenWidth, screenHeight, Color.RED, Color.BLUE);
Image radialGradient = GenImageGradientRadial(screenWidth, screenHeight, 0.0f, Color.WHITE, Color.BLACK);
Image isChecked = GenImageChecked(screenWidth, screenHeight, 32, 32, Color.RED, Color.BLUE);
Image whiteNoise = GenImageWhiteNoise(screenWidth, screenHeight, 0.5f);
Image cellular = GenImageCellular(screenWidth, screenHeight, 32);
Texture2D[] textures = new Texture2D[NumTextures];
textures[0] = LoadTextureFromImage(verticalGradient);
textures[1] = LoadTextureFromImage(horizontalGradient);
textures[2] = LoadTextureFromImage(radialGradient);
textures[3] = LoadTextureFromImage(isChecked);
textures[4] = LoadTextureFromImage(whiteNoise);
textures[5] = LoadTextureFromImage(cellular);
UnloadImage(verticalGradient);
UnloadImage(horizontalGradient);
UnloadImage(radialGradient);
UnloadImage(isChecked);
UnloadImage(whiteNoise);
UnloadImage(cellular);
int currentTexture = 0;
SetTargetFPS(60);
//---------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose())
{
// Update
//----------------------------------------------------------------------------------
if (IsMouseButtonPressed(MouseButton.MOUSE_LEFT_BUTTON) || IsKeyPressed(KeyboardKey.KEY_RIGHT))
{
// Cycle between the textures
currentTexture = (currentTexture + 1) % NumTextures;
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RAYWHITE);
DrawTexture(textures[currentTexture], 0, 0, Color.WHITE);
DrawRectangle(30, 400, 325, 30, ColorAlpha(Color.SKYBLUE, 0.5f));
DrawRectangleLines(30, 400, 325, 30, ColorAlpha(Color.WHITE, 0.5f));
DrawText("MOUSE LEFT BUTTON to CYCLE PROCEDURAL TEXTURES", 40, 410, 10, Color.WHITE);
switch (currentTexture)
{
case 0:
DrawText("VERTICAL GRADIENT", 560, 10, 20, Color.RAYWHITE);
break;
case 1:
DrawText("HORIZONTAL GRADIENT", 540, 10, 20, Color.RAYWHITE);
break;
case 2:
DrawText("RADIAL GRADIENT", 580, 10, 20, Color.LIGHTGRAY);
break;
case 3:
DrawText("CHECKED", 680, 10, 20, Color.RAYWHITE);
break;
case 4:
DrawText("Color.WHITE NOISE", 640, 10, 20, Color.RED);
break;
case 5:
DrawText("CELLULAR", 670, 10, 20, Color.RAYWHITE);
break;
default:
break;
}
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
for (int i = 0; i < textures.Length; i++)
{
UnloadTexture(textures[i]);
}
CloseWindow();
//--------------------------------------------------------------------------------------
return 0;
}
}

View File

@ -0,0 +1,72 @@
/*******************************************************************************************
*
* raylib [textures] example - Image loading and texture creation
*
* NOTE: Images are loaded in CPU memory (RAM); textures are loaded in GPU memory (VRAM)
*
* This example has been created using raylib 1.3 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
using static Raylib_cs.Raylib;
namespace Examples.Textures;
public class ImageLoading
{
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [textures] example - image loading");
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
Image image = LoadImage("resources/raylib-cs_logo.png");
Texture2D texture = LoadTextureFromImage(image);
UnloadImage(image);
//---------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose())
{
// Update
//----------------------------------------------------------------------------------
// TODO: Update your variables here
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RAYWHITE);
DrawTexture(
texture,
screenWidth / 2 - texture.Width / 2,
screenHeight / 2 - texture.Height / 2,
Color.WHITE
);
DrawText("this IS a texture loaded from an image!", 300, 370, 10, Color.GRAY);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(texture);
CloseWindow();
//--------------------------------------------------------------------------------------
return 0;
}
}

View File

@ -0,0 +1,221 @@
/*******************************************************************************************
*
* raylib [textures] example - Image processing
*
* NOTE: Images are loaded in CPU memory (RAM); textures are loaded in GPU memory (VRAM)
*
* This example has been created using raylib 1.4 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2016 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
using static Raylib_cs.Raylib;
namespace Examples.Textures;
public class ImageProcessing
{
public const int NumProcesses = 9;
enum ImageProcess
{
None = 0,
ColorGrayScale,
ColorTint,
ColorInvert,
ColorContrast,
ColorBrightness,
GaussianBlur,
FlipVertical,
FlipHorizontal
}
static string[] processText = {
"NO PROCESSING",
"COLOR GRAYSCALE",
"COLOR TINT",
"COLOR INVERT",
"COLOR CONTRAST",
"COLOR BRIGHTNESS",
"GAUSSIAN BLUR",
"FLIP VERTICAL",
"FLIP HORIZONTAL"
};
public unsafe static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [textures] example - image processing");
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
Image imageOrigin = LoadImage("resources/parrots.png");
ImageFormat(ref imageOrigin, PixelFormat.PIXELFORMAT_UNCOMPRESSED_R8G8B8A8);
Texture2D texture = LoadTextureFromImage(imageOrigin);
Image imageCopy = ImageCopy(imageOrigin);
ImageProcess currentProcess = ImageProcess.None;
bool textureReload = false;
Rectangle[] toggleRecs = new Rectangle[NumProcesses];
int mouseHoverRec = -1;
for (int i = 0; i < NumProcesses; i++)
{
toggleRecs[i] = new Rectangle(40, 50 + 32 * i, 150, 30);
}
SetTargetFPS(60);
//---------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose())
{
// Update
//----------------------------------------------------------------------------------
// Mouse toggle group logic
for (int i = 0; i < NumProcesses; i++)
{
if (CheckCollisionPointRec(GetMousePosition(), toggleRecs[i]))
{
mouseHoverRec = i;
if (IsMouseButtonReleased(MouseButton.MOUSE_LEFT_BUTTON))
{
currentProcess = (ImageProcess)i;
textureReload = true;
}
break;
}
else
{
mouseHoverRec = -1;
}
}
// Keyboard toggle group logic
if (IsKeyPressed(KeyboardKey.KEY_DOWN))
{
currentProcess++;
if ((int)currentProcess > (NumProcesses - 1))
{
currentProcess = 0;
}
textureReload = true;
}
else if (IsKeyPressed(KeyboardKey.KEY_UP))
{
currentProcess--;
if (currentProcess < 0)
{
currentProcess = ImageProcess.FlipHorizontal;
}
textureReload = true;
}
if (textureReload)
{
UnloadImage(imageCopy);
imageCopy = ImageCopy(imageOrigin);
// NOTE: Image processing is a costly CPU process to be done every frame,
// If image processing is required in a frame-basis, it should be done
// with a texture and by shaders
switch (currentProcess)
{
case ImageProcess.ColorGrayScale:
ImageColorGrayscale(ref imageCopy);
break;
case ImageProcess.ColorTint:
ImageColorTint(ref imageCopy, Color.GREEN);
break;
case ImageProcess.ColorInvert:
ImageColorInvert(ref imageCopy);
break;
case ImageProcess.ColorContrast:
ImageColorContrast(ref imageCopy, -40);
break;
case ImageProcess.ColorBrightness:
ImageColorBrightness(ref imageCopy, -80);
break;
case ImageProcess.GaussianBlur:
ImageBlurGaussian(ref imageCopy, 10);
break;
case ImageProcess.FlipVertical:
ImageFlipVertical(ref imageCopy);
break;
case ImageProcess.FlipHorizontal:
ImageFlipHorizontal(ref imageCopy);
break;
default:
break;
}
// Get pixel data from image (RGBA 32bit)
Color* pixels = LoadImageColors(imageCopy);
UpdateTexture(texture, pixels);
UnloadImageColors(pixels);
textureReload = false;
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RAYWHITE);
DrawText("IMAGE PROCESSING:", 40, 30, 10, Color.DARKGRAY);
// Draw rectangles
for (int i = 0; i < NumProcesses; i++)
{
DrawRectangleRec(toggleRecs[i], (i == (int)currentProcess) ? Color.SKYBLUE : Color.LIGHTGRAY);
DrawRectangleLines(
(int)toggleRecs[i].X,
(int)toggleRecs[i].Y,
(int)toggleRecs[i].Width,
(int)toggleRecs[i].Height,
(i == (int)currentProcess) ? Color.BLUE : Color.GRAY
);
int labelX = (int)(toggleRecs[i].X + toggleRecs[i].Width / 2);
DrawText(
processText[i],
(int)(labelX - MeasureText(processText[i], 10) / 2),
(int)toggleRecs[i].Y + 11,
10,
(i == (int)currentProcess) ? Color.DARKBLUE : Color.DARKGRAY
);
}
int x = screenWidth - texture.Width - 60;
int y = screenHeight / 2 - texture.Height / 2;
DrawTexture(texture, x, y, Color.WHITE);
DrawRectangleLines(x, y, texture.Width, texture.Height, Color.BLACK);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(texture);
UnloadImage(imageOrigin);
UnloadImage(imageCopy);
CloseWindow();
//--------------------------------------------------------------------------------------
return 0;
}
}

View File

@ -0,0 +1,108 @@
/*******************************************************************************************
*
* raylib [texture] example - Image text drawing using TTF generated spritefont
*
* This example has been created using raylib 1.8 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2017 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
using System.Numerics;
using static Raylib_cs.Raylib;
namespace Examples.Textures;
public class ImageText
{
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [texture] example - image text drawing");
// TTF Font loading with custom generation parameters
Font font = LoadFontEx("resources/fonts/KAISG.ttf", 64, null, 95);
Image parrots = LoadImage("resources/parrots.png");
// Draw over image using custom font
ImageDrawTextEx(
ref parrots,
font,
"[Parrots font drawing]",
new Vector2(20, 20),
font.BaseSize,
0,
Color.WHITE
);
// Image converted to texture, uploaded to GPU memory (VRAM)
Texture2D texture = LoadTextureFromImage(parrots);
UnloadImage(parrots);
Vector2 position = new(
screenWidth / 2 - texture.Width / 2,
screenHeight / 2 - texture.Height / 2 - 20
);
bool showFont = false;
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose())
{
// Update
//----------------------------------------------------------------------------------
if (IsKeyDown(KeyboardKey.KEY_SPACE))
{
showFont = true;
}
else
{
showFont = false;
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RAYWHITE);
if (!showFont)
{
// Draw texture with text already drawn inside
DrawTextureV(texture, position, Color.WHITE);
// Draw text directly using sprite font
Vector2 textPosition = new(position.X + 20, position.Y + 20 + 280);
DrawTextEx(font, "[Parrots font drawing]", textPosition, font.BaseSize, 0, Color.WHITE);
}
else
{
DrawTexture(font.Texture, screenWidth / 2 - font.Texture.Width / 2, 50, Color.BLACK);
}
DrawText("PRESS SPACE to SEE USED SPRITEFONT ", 290, 420, 10, Color.DARKGRAY);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(texture);
UnloadFont(font);
CloseWindow();
//--------------------------------------------------------------------------------------
return 0;
}
}

View File

@ -0,0 +1,66 @@
/*******************************************************************************************
*
* raylib [textures] example - Texture loading and drawing
*
* This example has been created using raylib 1.0 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2014 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
using static Raylib_cs.Raylib;
namespace Examples.Textures;
public class LogoRaylibTexture
{
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [textures] example - texture loading and drawing");
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
Texture2D texture = LoadTexture("resources/raylib-cs_logo.png");
//---------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose())
{
// Update
//----------------------------------------------------------------------------------
// TODO: Update your variables here
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RAYWHITE);
DrawTexture(
texture,
screenWidth / 2 - texture.Width / 2,
screenHeight / 2 - texture.Height / 2,
Color.WHITE
);
DrawText("this IS a texture!", 360, 370, 10, Color.GRAY);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(texture);
CloseWindow();
//--------------------------------------------------------------------------------------
return 0;
}
}

View File

@ -0,0 +1,292 @@
/*******************************************************************************************
*
* raylib [textures] example - Mouse painting
*
* This example has been created using raylib 2.5 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Example contributed by Chris Dill (@MysteriousSpace) and reviewed by Ramon Santamaria (@raysan5)
*
* Copyright (c) 2019 Chris Dill (@MysteriousSpace) and Ramon Santamaria (@raysan5)
*
********************************************************************************************/
using System.Numerics;
using static Raylib_cs.Raylib;
namespace Examples.Textures;
public class MousePainting
{
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [textures] example - mouse painting");
// Colours to choose from
Color[] colors = new Color[] {
Color.RAYWHITE,
Color.YELLOW,
Color.GOLD,
Color.ORANGE,
Color.PINK,
Color.RED,
Color.MAROON,
Color.GREEN,
Color.LIME,
Color.DARKGREEN,
Color.SKYBLUE,
Color.BLUE,
Color.DARKBLUE,
Color.PURPLE,
Color.VIOLET,
Color.DARKPURPLE,
Color.BEIGE,
Color.BROWN,
Color.DARKBROWN,
Color.LIGHTGRAY,
Color.GRAY,
Color.DARKGRAY,
Color.BLACK
};
// Define colorsRecs data (for every rectangle)
Rectangle[] colorsRecs = new Rectangle[colors.Length];
for (int i = 0; i < colorsRecs.Length; i++)
{
colorsRecs[i].X = 10 + 30 * i + 2 * i;
colorsRecs[i].Y = 10;
colorsRecs[i].Width = 30;
colorsRecs[i].Height = 30;
}
int colorSelected = 0;
int colorSelectedPrev = colorSelected;
int colorMouseHover = 0;
int brushSize = 20;
Rectangle btnSaveRec = new(750, 10, 40, 30);
bool btnSaveMouseHover = false;
bool showSaveMessage = false;
int saveMessageCounter = 0;
// Create a RenderTexture2D to use as a canvas
RenderTexture2D target = LoadRenderTexture(screenWidth, screenHeight);
// Clear render texture before entering the game loop
BeginTextureMode(target);
ClearBackground(colors[0]);
EndTextureMode();
SetTargetFPS(120); // Set our game to run at 120 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose())
{
// Update
//----------------------------------------------------------------------------------
Vector2 mousePos = GetMousePosition();
// Move between colors with keys
if (IsKeyPressed(KeyboardKey.KEY_RIGHT))
{
colorSelected++;
}
else if (IsKeyPressed(KeyboardKey.KEY_LEFT))
{
colorSelected--;
}
if (colorSelected >= colors.Length)
{
colorSelected = colors.Length - 1;
}
else if (colorSelected < 0)
{
colorSelected = 0;
}
// Choose color with mouse
for (int i = 0; i < colors.Length; i++)
{
if (CheckCollisionPointRec(mousePos, colorsRecs[i]))
{
colorMouseHover = i;
break;
}
else
{
colorMouseHover = -1;
}
}
if ((colorMouseHover >= 0) && IsMouseButtonPressed(MouseButton.MOUSE_LEFT_BUTTON))
{
colorSelected = colorMouseHover;
colorSelectedPrev = colorSelected;
}
// Change brush size
brushSize += (int)(GetMouseWheelMove() * 5);
if (brushSize < 2)
{
brushSize = 2;
}
if (brushSize > 50)
{
brushSize = 50;
}
if (IsKeyPressed(KeyboardKey.KEY_C))
{
// Clear render texture to clear color
BeginTextureMode(target);
ClearBackground(colors[0]);
EndTextureMode();
}
if (IsMouseButtonDown(MouseButton.MOUSE_LEFT_BUTTON))
{
// Paint circle into render texture
// NOTE: To avoid discontinuous circles, we could store
// previous-next mouse points and just draw a line using brush size
BeginTextureMode(target);
if (mousePos.Y > 50)
{
DrawCircle((int)mousePos.X, (int)mousePos.Y, brushSize, colors[colorSelected]);
}
EndTextureMode();
}
else if (IsMouseButtonDown(MouseButton.MOUSE_RIGHT_BUTTON))
{
colorSelected = 0;
// Erase circle from render texture
BeginTextureMode(target);
if (mousePos.Y > 50)
{
DrawCircle((int)mousePos.X, (int)mousePos.Y, brushSize, colors[0]);
}
EndTextureMode();
}
else
{
colorSelected = colorSelectedPrev;
}
// Check mouse hover save button
if (CheckCollisionPointRec(mousePos, btnSaveRec))
{
btnSaveMouseHover = true;
}
else
{
btnSaveMouseHover = false;
}
// Image saving logic
// NOTE: Saving painted texture to a default named image
if ((btnSaveMouseHover && IsMouseButtonReleased(MouseButton.MOUSE_LEFT_BUTTON)) ||
IsKeyPressed(KeyboardKey.KEY_S))
{
Image image = LoadImageFromTexture(target.Texture);
ImageFlipVertical(ref image);
ExportImage(image, "my_amazing_texture_painting.png");
UnloadImage(image);
showSaveMessage = true;
}
if (showSaveMessage)
{
// On saving, show a full screen message for 2 seconds
saveMessageCounter++;
if (saveMessageCounter > 240)
{
showSaveMessage = false;
saveMessageCounter = 0;
}
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RAYWHITE);
// NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom)
Rectangle source = new(0, 0, target.Texture.Width, -target.Texture.Height);
DrawTextureRec(target.Texture, source, new Vector2(0, 0), Color.WHITE);
// Draw drawing circle for reference
if (mousePos.Y > 50)
{
if (IsMouseButtonDown(MouseButton.MOUSE_RIGHT_BUTTON))
{
DrawCircleLines((int)mousePos.X, (int)mousePos.Y, brushSize, colors[colorSelected]);
}
else
{
DrawCircle(GetMouseX(), GetMouseY(), brushSize, colors[colorSelected]);
}
}
// Draw top panel
DrawRectangle(0, 0, GetScreenWidth(), 50, Color.RAYWHITE);
DrawLine(0, 50, GetScreenWidth(), 50, Color.LIGHTGRAY);
// Draw color selection rectangles
for (int i = 0; i < colors.Length; i++)
{
DrawRectangleRec(colorsRecs[i], colors[i]);
}
DrawRectangleLines(10, 10, 30, 30, Color.LIGHTGRAY);
if (colorMouseHover >= 0)
{
DrawRectangleRec(colorsRecs[colorMouseHover], ColorAlpha(Color.WHITE, 0.6f));
}
Rectangle rec = new(
colorsRecs[colorSelected].X - 2,
colorsRecs[colorSelected].Y - 2,
colorsRecs[colorSelected].Width + 4,
colorsRecs[colorSelected].Height + 4
);
DrawRectangleLinesEx(rec, 2, Color.BLACK);
// Draw save image button
DrawRectangleLinesEx(btnSaveRec, 2, btnSaveMouseHover ? Color.RED : Color.BLACK);
DrawText("SAVE!", 755, 20, 10, btnSaveMouseHover ? Color.RED : Color.BLACK);
// Draw save image message
if (showSaveMessage)
{
DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), ColorAlpha(Color.RAYWHITE, 0.8f));
DrawRectangle(0, 150, GetScreenWidth(), 80, Color.BLACK);
DrawText("IMAGE SAVED: my_amazing_texture_painting.png", 150, 180, 20, Color.RAYWHITE);
}
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadRenderTexture(target);
CloseWindow();
//--------------------------------------------------------------------------------------
return 0;
}
}

View File

@ -0,0 +1,145 @@
/*******************************************************************************************
*
* raylib [textures] example - N-patch drawing
*
* NOTE: Images are loaded in CPU memory (RAM); textures are loaded in GPU memory (VRAM)
*
* This example has been created using raylib 2.0 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Example contributed by Jorge A. Gomes (@overdev) and reviewed by Ramon Santamaria (@raysan5)
*
* Copyright (c) 2018 Jorge A. Gomes (@overdev) and Ramon Santamaria (@raysan5)
*
********************************************************************************************/
using System;
using System.Numerics;
using static Raylib_cs.Raylib;
namespace Examples.Textures;
public class NpatchDrawing
{
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [textures] example - N-patch drawing");
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
Texture2D nPatchTexture = LoadTexture("resources/ninepatch_button.png");
Vector2 mousePosition = new(0.0f, 0.0f);
Vector2 origin = new(0.0f, 0.0f);
// Position and size of the n-patches
Rectangle dstRec1 = new(480.0f, 160.0f, 32.0f, 32.0f);
Rectangle dstRec2 = new(160.0f, 160.0f, 32.0f, 32.0f);
Rectangle dstRecH = new(160.0f, 93.0f, 32.0f, 32.0f);
Rectangle dstRecV = new(92.0f, 160.0f, 32.0f, 32.0f);
// A 9-patch (NPT_9PATCH) changes its sizes in both axis
NPatchInfo ninePatchInfo1 = new NPatchInfo
{
Source = new Rectangle(0.0f, 0.0f, 64.0f, 64.0f),
Left = 12,
Top = 40,
Right = 12,
Bottom = 12,
Layout = NPatchLayout.NPATCH_NINE_PATCH
};
NPatchInfo ninePatchInfo2 = new NPatchInfo
{
Source = new Rectangle(0.0f, 128.0f, 64.0f, 64.0f),
Left = 16,
Top = 16,
Right = 16,
Bottom = 16,
Layout = NPatchLayout.NPATCH_NINE_PATCH
};
// A horizontal 3-patch (NPT_3PATCH_HORIZONTAL) changes its sizes along the x axis only
NPatchInfo h3PatchInfo = new NPatchInfo
{
Source = new Rectangle(0.0f, 64.0f, 64.0f, 64.0f),
Left = 8,
Top = 8,
Right = 8,
Bottom = 8,
Layout = NPatchLayout.NPATCH_THREE_PATCH_HORIZONTAL
};
// A vertical 3-patch (NPT_3PATCH_VERTICAL) changes its sizes along the y axis only
NPatchInfo v3PatchInfo = new NPatchInfo
{
Source = new Rectangle(0.0f, 192.0f, 64.0f, 64.0f),
Left = 6,
Top = 6,
Right = 6,
Bottom = 6,
Layout = NPatchLayout.NPATCH_THREE_PATCH_VERTICAL
};
SetTargetFPS(60);
//---------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose())
{
// Update
//----------------------------------------------------------------------------------
mousePosition = GetMousePosition();
// Resize the n-patches based on mouse position
dstRec1.Width = mousePosition.X - dstRec1.X;
dstRec1.Height = mousePosition.Y - dstRec1.Y;
dstRec2.Width = mousePosition.X - dstRec2.X;
dstRec2.Height = mousePosition.Y - dstRec2.Y;
dstRecH.Width = mousePosition.X - dstRecH.X;
dstRecV.Height = mousePosition.Y - dstRecV.Y;
// Set a minimum width and/or height
dstRec1.Width = Math.Clamp(dstRec1.Width, 1.0f, 300.0f);
dstRec1.Height = MathF.Max(dstRec1.Height, 1.0f);
dstRec2.Width = Math.Clamp(dstRec2.Width, 1.0f, 300.0f);
dstRec2.Height = MathF.Max(dstRec2.Height, 1.0f);
dstRecH.Width = MathF.Max(dstRecH.Width, 1.0f);
dstRecV.Height = MathF.Max(dstRecV.Height, 1.0f);
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RAYWHITE);
// Draw the n-patches
DrawTextureNPatch(nPatchTexture, ninePatchInfo2, dstRec2, origin, 0.0f, Color.WHITE);
DrawTextureNPatch(nPatchTexture, ninePatchInfo1, dstRec1, origin, 0.0f, Color.WHITE);
DrawTextureNPatch(nPatchTexture, h3PatchInfo, dstRecH, origin, 0.0f, Color.WHITE);
DrawTextureNPatch(nPatchTexture, v3PatchInfo, dstRecV, origin, 0.0f, Color.WHITE);
// Draw the source texture
DrawRectangleLines(5, 88, 74, 266, Color.BLUE);
DrawTexture(nPatchTexture, 10, 93, Color.WHITE);
DrawText("TEXTURE", 15, 360, 10, Color.DARKGRAY);
DrawText("Move the mouse to stretch or shrink the n-patches", 10, 20, 20, Color.DARKGRAY);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(nPatchTexture);
CloseWindow();
//--------------------------------------------------------------------------------------
return 0;
}
}

View File

@ -0,0 +1,172 @@
/*******************************************************************************************
*
* raylib example - particles blending
*
* This example has been created using raylib 1.7 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2017 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
using System.Numerics;
using static Raylib_cs.Raylib;
namespace Examples.Textures;
public class ParticlesBlending
{
public const int MaxParticles = 200;
// Particle structure with basic data
struct Particle
{
public Vector2 Position;
public Color Color;
public float Alpha;
public float Size;
public float Rotation;
// NOTE: Use it to activate/deactive particle
public bool Active;
}
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [textures] example - particles blending");
// Particles pool, reuse them!
Particle[] mouseTail = new Particle[MaxParticles];
// Initialize particles
for (int i = 0; i < mouseTail.Length; i++)
{
mouseTail[i].Position = new Vector2(0, 0);
mouseTail[i].Color = new Color(
GetRandomValue(0, 255),
GetRandomValue(0, 255),
GetRandomValue(0, 255),
255
);
mouseTail[i].Alpha = 1.0f;
mouseTail[i].Size = (float)GetRandomValue(1, 30) / 20.0f;
mouseTail[i].Rotation = GetRandomValue(0, 360);
mouseTail[i].Active = false;
}
float gravity = 3.0f;
Texture2D smoke = LoadTexture("resources/spark_flame.png");
BlendMode blending = BlendMode.BLEND_ALPHA;
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose())
{
// Update
//----------------------------------------------------------------------------------
// Activate one particle every frame and Update active particles
// NOTE: Particles initial position should be mouse position when activated
// NOTE: Particles fall down with gravity and rotation... and disappear after 2 seconds (alpha = 0)
// NOTE: When a particle disappears, active = false and it can be reused.
for (int i = 0; i < mouseTail.Length; i++)
{
if (!mouseTail[i].Active)
{
mouseTail[i].Active = true;
mouseTail[i].Alpha = 1.0f;
mouseTail[i].Position = GetMousePosition();
i = mouseTail.Length;
}
}
for (int i = 0; i < mouseTail.Length; i++)
{
if (mouseTail[i].Active)
{
mouseTail[i].Position.Y += gravity / 2;
mouseTail[i].Alpha -= 0.005f;
if (mouseTail[i].Alpha <= 0.0f)
{
mouseTail[i].Active = false;
}
mouseTail[i].Rotation += 2.0f;
}
}
if (IsKeyPressed(KeyboardKey.KEY_SPACE))
{
if (blending == BlendMode.BLEND_ALPHA)
{
blending = BlendMode.BLEND_ADDITIVE;
}
else
{
blending = BlendMode.BLEND_ALPHA;
}
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.DARKGRAY);
BeginBlendMode(blending);
// Draw active particles
for (int i = 0; i < mouseTail.Length; i++)
{
if (mouseTail[i].Active)
{
Rectangle source = new(0, 0, smoke.Width, smoke.Height);
Rectangle dest = new(
mouseTail[i].Position.X,
mouseTail[i].Position.Y,
smoke.Width * mouseTail[i].Size,
smoke.Height * mouseTail[i].Size
);
Vector2 position = new(
smoke.Width * mouseTail[i].Size / 2,
smoke.Height * mouseTail[i].Size / 2
);
Color color = ColorAlpha(mouseTail[i].Color, mouseTail[i].Alpha);
DrawTexturePro(smoke, source, dest, position, mouseTail[i].Rotation, color);
}
}
EndBlendMode();
DrawText("PRESS SPACE to CHANGE BLENDING MODE", 180, 20, 20, Color.BLACK);
if (blending == BlendMode.BLEND_ALPHA)
{
DrawText("ALPHA BLENDING", 290, screenHeight - 40, 20, Color.BLACK);
}
else
{
DrawText("ADDITIVE BLENDING", 280, screenHeight - 40, 20, Color.RAYWHITE);
}
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(smoke);
CloseWindow();
//--------------------------------------------------------------------------------------
return 0;
}
}

View File

@ -0,0 +1,141 @@
/*******************************************************************************************
*
* raylib [shapes] example - Draw Textured Polygon
*
* This example has been created using raylib 99.98 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2014 Ramon Santamaria (@raysan5)
* Copyright (c) 2021 Chris Camacho (codifies - bedroomcoders.co.uk)
*
********************************************************************************************/
using System.Numerics;
using static Raylib_cs.Raylib;
namespace Examples.Textures;
public class Polygon
{
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [textures] example - Textured Polygon");
Vector2[] texcoords = new[] {
new Vector2(0.75f, 0),
new Vector2(0.25f, 0),
new Vector2(0, 0.5f),
new Vector2(0, 0.75f),
new Vector2(0.25f, 1),
new Vector2(0.375f, 0.875f),
new Vector2(0.625f, 0.875f),
new Vector2(0.75f, 1),
new Vector2(1, 0.75f),
new Vector2(1, 0.5f),
// Close the poly
new Vector2(0.75f, 0)
};
Vector2[] points = new Vector2[11];
// Define the base poly vertices from the UV's
// NOTE: They can be specified in any other way
for (int i = 0; i < points.Length; i++)
{
points[i].X = (texcoords[i].X - 0.5f) * 256.0f;
points[i].Y = (texcoords[i].Y - 0.5f) * 256.0f;
}
// Define the vertices drawing position
// NOTE: Initially same as points but updated every frame
Vector2[] positions = new Vector2[points.Length];
for (int i = 0; i < positions.Length; i++)
{
positions[i] = points[i];
}
Texture2D texture = LoadTexture("resources/cat.png");
float angle = 0;
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose())
{
// Update
//----------------------------------------------------------------------------------
// Update your variables here
//----------------------------------------------------------------------------------
angle += 1;
for (int i = 0; i < positions.Length; i++)
{
positions[i] = Raymath.Vector2Rotate(points[i], angle * Raylib.DEG2RAD);
}
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RAYWHITE);
DrawText("Textured Polygon", 20, 20, 20, Color.DARKGRAY);
Vector2 center = new(screenWidth / 2, screenHeight / 2);
DrawTexturePoly(texture, center, positions, texcoords, positions.Length, Color.WHITE);
EndDrawing();
//----------------------------------------------------------------------------------
}
UnloadTexture(texture);
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow();
//--------------------------------------------------------------------------------------
return 0;
}
// Draw textured polygon, defined by vertex and texture coordinates
// NOTE: Polygon center must have straight line path to all points
// without crossing perimeter, points must be in anticlockwise order
static void DrawTexturePoly(
Texture2D texture,
Vector2 center,
Vector2[] points,
Vector2[] texcoords,
int pointCount,
Color tint
)
{
Rlgl.SetTexture(texture.Id);
// Texturing is only supported on RL_QUADS
Rlgl.Begin(DrawMode.QUADS);
Rlgl.Color4ub(tint.R, tint.G, tint.B, tint.A);
for (int i = 0; i < pointCount - 1; i++)
{
Rlgl.TexCoord2f(0.5f, 0.5f);
Rlgl.Vertex2f(center.X, center.Y);
Rlgl.TexCoord2f(texcoords[i].X, texcoords[i].Y);
Rlgl.Vertex2f(points[i].X + center.X, points[i].Y + center.Y);
Rlgl.TexCoord2f(texcoords[i + 1].X, texcoords[i + 1].Y);
Rlgl.Vertex2f(points[i + 1].X + center.X, points[i + 1].Y + center.Y);
Rlgl.TexCoord2f(texcoords[i + 1].X, texcoords[i + 1].Y);
Rlgl.Vertex2f(points[i + 1].X + center.X, points[i + 1].Y + center.Y);
}
Rlgl.End();
Rlgl.SetTexture(0);
}
}

View File

@ -0,0 +1,114 @@
/*******************************************************************************************
*
* raylib [textures] example - Load textures from raw data
*
* NOTE: Images are loaded in CPU memory (RAM); textures are loaded in GPU memory (VRAM)
*
* This example has been created using raylib 1.3 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
using static Raylib_cs.Raylib;
namespace Examples.Textures;
public class RawData
{
public unsafe static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [textures] example - texture from raw data");
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
// Load RAW image data (512x512, 32bit RGBA, no file header)
Image fudesumiRaw = LoadImageRaw(
"resources/fudesumi.raw",
384,
512,
PixelFormat.PIXELFORMAT_UNCOMPRESSED_R8G8B8A8,
0
);
Texture2D fudesumi = LoadTextureFromImage(fudesumiRaw);
UnloadImage(fudesumiRaw);
// Generate a checked texture by code
int width = 960;
int height = 480;
// Store pixel data
Color* pixels = (Color*)Raylib.MemAlloc(width * height * sizeof(Color));
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
if (((x / 32 + y / 32) / 1) % 2 == 0)
{
pixels[y * width + x] = Color.ORANGE;
}
else
{
pixels[y * width + x] = Color.GOLD;
}
}
}
// Load pixels data into an image structure and create texture
Image checkedIm = new Image
{
Data = pixels,
Width = width,
Height = height,
Format = PixelFormat.PIXELFORMAT_UNCOMPRESSED_R8G8B8A8,
Mipmaps = 1,
};
Texture2D checkedTex = LoadTextureFromImage(checkedIm);
Raylib.MemFree(pixels);
//---------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose())
{
// Update
//----------------------------------------------------------------------------------
// TODO: Update your variables here
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RAYWHITE);
int x = screenWidth / 2 - checkedTex.Width / 2;
int y = screenHeight / 2 - checkedTex.Height / 2;
DrawTexture(checkedTex, x, y, ColorAlpha(Color.WHITE, 0.5f));
DrawTexture(fudesumi, 430, -30, Color.WHITE);
DrawText("CHECKED TEXTURE ", 84, 85, 30, Color.BROWN);
DrawText("GENERATED by CODE", 72, 148, 30, Color.BROWN);
DrawText("and RAW IMAGE LOADING", 46, 210, 30, Color.BROWN);
DrawText("(c) Fudesumi sprite by Eiden Marsal", 310, screenHeight - 20, 10, Color.BROWN);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(fudesumi);
UnloadTexture(checkedTex);
CloseWindow();
//--------------------------------------------------------------------------------------
return 0;
}
}

View File

@ -0,0 +1,124 @@
/*******************************************************************************************
*
* raylib [textures] example - Texture loading and drawing a part defined by a rectangle
*
* This example has been created using raylib 1.3 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2014 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
using System;
using System.Numerics;
using static Raylib_cs.Raylib;
namespace Examples.Textures;
public class SpriteAnim
{
public const int MaxFrameSpeed = 15;
public const int MinFrameSpeed = 1;
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [texture] example - texture rectangle");
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
Texture2D scarfy = LoadTexture("resources/scarfy.png");
Vector2 position = new(350.0f, 280.0f);
Rectangle frameRec = new(0.0f, 0.0f, (float)scarfy.Width / 6, (float)scarfy.Height);
int currentFrame = 0;
int framesCounter = 0;
// Number of spritesheet frames shown by second
int framesSpeed = 8;
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose())
{
// Update
//----------------------------------------------------------------------------------
framesCounter++;
if (framesCounter >= (60 / framesSpeed))
{
framesCounter = 0;
currentFrame++;
if (currentFrame > 5)
{
currentFrame = 0;
}
frameRec.X = (float)currentFrame * (float)scarfy.Width / 6;
}
if (IsKeyPressed(KeyboardKey.KEY_RIGHT))
{
framesSpeed++;
}
else if (IsKeyPressed(KeyboardKey.KEY_LEFT))
{
framesSpeed--;
}
framesSpeed = Math.Clamp(framesSpeed, MinFrameSpeed, MaxFrameSpeed);
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RAYWHITE);
DrawTexture(scarfy, 15, 40, Color.WHITE);
DrawRectangleLines(15, 40, scarfy.Width, scarfy.Height, Color.LIME);
DrawRectangleLines(
15 + (int)frameRec.X,
40 + (int)frameRec.Y,
(int)frameRec.Width,
(int)frameRec.Height,
Color.RED
);
DrawText("FRAME SPEED: ", 165, 210, 10, Color.DARKGRAY);
DrawText($"{framesSpeed:2F} FPS", 575, 210, 10, Color.DARKGRAY);
DrawText("PRESS RIGHT/LEFT KEYS to CHANGE SPEED!", 290, 240, 10, Color.DARKGRAY);
for (int i = 0; i < MaxFrameSpeed; i++)
{
if (i < framesSpeed)
{
DrawRectangle(250 + 21 * i, 205, 20, 20, Color.RED);
}
DrawRectangleLines(250 + 21 * i, 205, 20, 20, Color.MAROON);
}
// Draw part of the texture
DrawTextureRec(scarfy, frameRec, position, Color.WHITE);
DrawText("(c) Scarfy sprite by Eiden Marsal", screenWidth - 200, screenHeight - 20, 10, Color.GRAY);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(scarfy);
CloseWindow();
//--------------------------------------------------------------------------------------
return 0;
}
}

View File

@ -0,0 +1,122 @@
/*******************************************************************************************
*
* raylib [textures] example - sprite button
*
* This example has been created using raylib 2.5 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2019 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
using System.Numerics;
using static Raylib_cs.Raylib;
namespace Examples.Textures;
public class SpriteButton
{
// Number of frames (rectangles) for the button sprite texture
public const int NumFrames = 3;
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [textures] example - sprite button");
InitAudioDevice();
Sound fxButton = LoadSound("resources/audio/buttonfx.wav");
Texture2D button = LoadTexture("resources/button.png");
// Define frame rectangle for drawing
int frameHeight = button.Height / NumFrames;
Rectangle sourceRec = new(0, 0, button.Width, frameHeight);
// Define button bounds on screen
Rectangle btnBounds = new(
screenWidth / 2 - button.Width / 2,
screenHeight / 2 - button.Height / NumFrames / 2,
button.Width,
frameHeight
);
// Button state: 0-NORMAL, 1-MOUSE_HOVER, 2-PRESSED
int btnState = 0;
// Button action should be activated
bool btnAction = false;
Vector2 mousePoint = new(0.0f, 0.0f);
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose())
{
// Update
//----------------------------------------------------------------------------------
mousePoint = GetMousePosition();
btnAction = false;
// Check button state
if (CheckCollisionPointRec(mousePoint, btnBounds))
{
if (IsMouseButtonDown(MouseButton.MOUSE_LEFT_BUTTON))
{
btnState = 2;
}
else
{
btnState = 1;
}
if (IsMouseButtonReleased(MouseButton.MOUSE_LEFT_BUTTON))
{
btnAction = true;
}
}
else
{
btnState = 0;
}
if (btnAction)
{
PlaySound(fxButton);
// TODO: Any desired action
}
// Calculate button frame rectangle to draw depending on button state
sourceRec.Y = btnState * frameHeight;
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RAYWHITE);
// Draw button frame
DrawTextureRec(button, sourceRec, new Vector2(btnBounds.X, btnBounds.Y), Color.WHITE);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(button);
UnloadSound(fxButton);
CloseAudioDevice();
CloseWindow();
//--------------------------------------------------------------------------------------
return 0;
}
}

View File

@ -0,0 +1,132 @@
/*******************************************************************************************
*
* raylib [textures] example - sprite explosion
*
* This example has been created using raylib 2.5 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2019 Anata and Ramon Santamaria (@raysan5)
*
********************************************************************************************/
using System.Numerics;
using static Raylib_cs.Raylib;
namespace Examples.Textures;
public class SpriteExplosion
{
const int NumFramesPerLine = 5;
const int NumLines = 5;
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [textures] example - sprite explosion");
InitAudioDevice();
// Load explosion sound
Sound fxBoom = LoadSound("resources/audio/boom.wav");
// Load explosion texture
Texture2D explosion = LoadTexture("resources/explosion.png");
// Init variables for animation
// Sprite one frame rectangle width
int frameWidth = explosion.Width / NumFramesPerLine;
// Sprite one frame rectangle height
int frameHeight = explosion.Height / NumLines;
int currentFrame = 0;
int currentLine = 0;
Rectangle frameRec = new(0, 0, frameWidth, frameHeight);
Vector2 position = new(0.0f, 0.0f);
bool active = false;
int framesCounter = 0;
SetTargetFPS(120);
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose())
{
// Update
//----------------------------------------------------------------------------------
// Check for mouse button pressed and activate explosion (if not active)
if (IsMouseButtonPressed(MouseButton.MOUSE_LEFT_BUTTON) && !active)
{
position = GetMousePosition();
active = true;
position.X -= frameWidth / 2;
position.Y -= frameHeight / 2;
PlaySound(fxBoom);
}
// Compute explosion animation frames
if (active)
{
framesCounter++;
if (framesCounter > 2)
{
currentFrame++;
if (currentFrame >= NumFramesPerLine)
{
currentFrame = 0;
currentLine++;
if (currentLine >= NumLines)
{
currentLine = 0;
active = false;
}
}
framesCounter = 0;
}
}
frameRec.X = frameWidth * currentFrame;
frameRec.Y = frameHeight * currentLine;
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RAYWHITE);
// Draw explosion required frame rectangle
if (active)
{
DrawTextureRec(explosion, frameRec, position, Color.WHITE);
}
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(explosion);
UnloadSound(fxBoom);
CloseAudioDevice();
CloseWindow();
//--------------------------------------------------------------------------------------
return 0;
}
}

View File

@ -0,0 +1,87 @@
/*******************************************************************************************
*
* raylib [textures] example - Texture source and destination rectangles
*
* This example has been created using raylib 1.3 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
using System.Numerics;
using static Raylib_cs.Raylib;
namespace Examples.Textures;
public class SrcRecDstRec
{
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
string title = "raylib [textures] examples - texture source and destination rectangles";
InitWindow(screenWidth, screenHeight, title);
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
Texture2D scarfy = LoadTexture("resources/scarfy.png");
int frameWidth = scarfy.Width / 6;
int frameHeight = scarfy.Height;
// NOTE: Source rectangle (part of the texture to use for drawing)
Rectangle sourceRec = new(0, 0, frameWidth, frameHeight);
// NOTE: Destination rectangle (screen rectangle where drawing part of texture)
Rectangle destRec = new(screenWidth / 2, screenHeight / 2, frameWidth * 2, frameHeight * 2);
// NOTE: Origin of the texture (rotation/scale point), it's relative to destination rectangle size
Vector2 origin = new(frameWidth, frameHeight);
int rotation = 0;
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose())
{
// Update
//----------------------------------------------------------------------------------
rotation++;
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RAYWHITE);
// NOTE: Using DrawTexturePro() we can easily rotate and scale the part of the texture we draw
// sourceRec defines the part of the texture we use for drawing
// destRec defines the rectangle where our texture part will fit (scaling it to fit)
// origin defines the point of the texture used as reference for rotation and scaling
// rotation defines the texture rotation (using origin as rotation point)
DrawTexturePro(scarfy, sourceRec, destRec, origin, rotation, Color.WHITE);
DrawLine((int)destRec.X, 0, (int)destRec.X, screenHeight, Color.GRAY);
DrawLine(0, (int)destRec.Y, screenWidth, (int)destRec.Y, Color.GRAY);
DrawText("(c) Scarfy sprite by Eiden Marsal", screenWidth - 200, screenHeight - 20, 10, Color.GRAY);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(scarfy);
CloseWindow();
//--------------------------------------------------------------------------------------
return 0;
}
}

View File

@ -0,0 +1,284 @@
using System;
using System.Numerics;
using static Raylib_cs.Raylib;
namespace Examples.Textures;
public unsafe class TexturedCurve
{
public class CurvePoint
{
public Vector2 value;
public float X => value.X;
public float Y => value.Y;
public static implicit operator CurvePoint(Vector2 v) => new CurvePoint { value = v };
public static implicit operator Vector2(CurvePoint v) => v.value;
}
static Texture2D texRoad;
static bool showCurve = false;
static float curveWidth = 50;
static int curveSegments = 24;
static CurvePoint curveStartPosition;
static CurvePoint curveStartPositionTangent;
static CurvePoint curveEndPosition;
static CurvePoint curveEndPositionTangent;
static CurvePoint curveSelectedPoint;
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
SetConfigFlags(ConfigFlags.FLAG_VSYNC_HINT | ConfigFlags.FLAG_MSAA_4X_HINT);
InitWindow(screenWidth, screenHeight, "raylib [textures] examples - textured curve");
// Load the road texture
texRoad = LoadTexture("resources/road.png");
SetTextureFilter(texRoad, TextureFilter.TEXTURE_FILTER_BILINEAR);
// Setup the curve
curveStartPosition = new Vector2(80, 100);
curveStartPositionTangent = new Vector2(100, 300);
curveEndPosition = new Vector2(700, 350);
curveEndPositionTangent = new Vector2(600, 100);
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose())
{
// Update
//----------------------------------------------------------------------------------
UpdateCurve();
UpdateOptions();
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RAYWHITE);
DrawTexturedCurve();
DrawCurve();
DrawText("Drag points to move curve, press SPACE to show/hide base curve", 10, 10, 10, Color.DARKGRAY);
DrawText($"Curve width: {curveWidth} (Use + and - to adjust)", 10, 30, 10, Color.DARKGRAY);
DrawText($"Curve segments: {curveSegments} (Use LEFT and RIGHT to adjust)", 10, 50, 10, Color.DARKGRAY);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(texRoad);
CloseWindow();
//--------------------------------------------------------------------------------------
return 0;
}
static void DrawCurve()
{
if (showCurve)
{
DrawLineBezierCubic(
curveStartPosition,
curveEndPosition,
curveStartPositionTangent,
curveEndPositionTangent,
2,
Color.BLUE
);
}
// Draw the various control points and highlight where the mouse is
DrawLineV(curveStartPosition, curveStartPositionTangent, Color.SKYBLUE);
DrawLineV(curveEndPosition, curveEndPositionTangent, Color.PURPLE);
Vector2 mouse = GetMousePosition();
if (CheckCollisionPointCircle(mouse, curveStartPosition, 6))
{
DrawCircleV(curveStartPosition, 7, Color.YELLOW);
}
DrawCircleV(curveStartPosition, 5, Color.RED);
if (CheckCollisionPointCircle(mouse, curveStartPositionTangent, 6))
{
DrawCircleV(curveStartPositionTangent, 7, Color.YELLOW);
}
DrawCircleV(curveStartPositionTangent, 5, Color.MAROON);
if (CheckCollisionPointCircle(mouse, curveEndPosition, 6))
{
DrawCircleV(curveEndPosition, 7, Color.YELLOW);
}
DrawCircleV(curveEndPosition, 5, Color.GREEN);
if (CheckCollisionPointCircle(mouse, curveEndPositionTangent, 6))
{
DrawCircleV(curveEndPositionTangent, 7, Color.YELLOW);
}
DrawCircleV(curveEndPositionTangent, 5, Color.DARKGREEN);
}
static void UpdateCurve()
{
// If the mouse is not down, we are not editing the curve so clear the selection
if (!IsMouseButtonDown(MouseButton.MOUSE_LEFT_BUTTON))
{
return;
}
// If a point was selected, move it
if (curveSelectedPoint != null)
{
curveSelectedPoint.value += GetMouseDelta();
}
// The mouse is down, and nothing was selected, so see if anything was picked
Vector2 mouse = GetMousePosition();
if (CheckCollisionPointCircle(mouse, curveStartPosition, 6))
{
curveSelectedPoint = curveStartPosition;
}
else if (CheckCollisionPointCircle(mouse, curveStartPositionTangent, 6))
{
curveSelectedPoint = curveStartPositionTangent;
}
else if (CheckCollisionPointCircle(mouse, curveEndPosition, 6))
{
curveSelectedPoint = curveEndPosition;
}
else if (CheckCollisionPointCircle(mouse, curveEndPositionTangent, 6))
{
curveSelectedPoint = curveEndPositionTangent;
}
}
static void DrawTexturedCurve()
{
float step = 1.0f / curveSegments;
Vector2 previous = curveStartPosition;
Vector2 previousTangent = Vector2.Zero;
float previousV = 0;
// We can't compute a tangent for the first point, so we need to reuse the tangent from the first segment
bool tangentSet = false;
Vector2 current = Vector2.Zero;
float t = 0.0f;
for (int i = 1; i <= curveSegments; i++)
{
// Segment the curve
t = step * i;
float a = MathF.Pow(1 - t, 3);
float b = 3 * MathF.Pow(1 - t, 2) * t;
float c = 3 * (1 - t) * MathF.Pow(t, 2);
float d = MathF.Pow(t, 3);
// Compute the endpoint for this segment
current.Y = a * curveStartPosition.Y + b * curveStartPositionTangent.Y;
current.Y += c * curveEndPositionTangent.Y + d * curveEndPosition.Y;
current.X = a * curveStartPosition.X + b * curveStartPositionTangent.X;
current.X += c * curveEndPositionTangent.X + d * curveEndPosition.X;
// Vector from previous to current
Vector2 delta = new(current.X - previous.X, current.Y - previous.Y);
// The right hand normal to the delta vector
Vector2 normal = Vector2.Normalize(new Vector2(-delta.Y, delta.X));
// The v teXture coordinate of the segment (add up the length of all the segments so far)
float v = previousV + delta.Length();
// Make sure the start point has a normal
if (!tangentSet)
{
previousTangent = normal;
tangentSet = true;
}
// EXtend out the normals from the previous and current points to get the quad for this segment
Vector2 prevPosNormal = previous + (previousTangent * curveWidth);
Vector2 prevNegNormal = previous + (previousTangent * -curveWidth);
Vector2 currentPosNormal = current + (normal * curveWidth);
Vector2 currentNegNormal = current + (normal * -curveWidth);
// Draw the segment as a quad
Rlgl.SetTexture(texRoad.Id);
Rlgl.Begin(DrawMode.QUADS);
Rlgl.Color4ub(255, 255, 255, 255);
Rlgl.Normal3f(0.0f, 0.0f, 1.0f);
Rlgl.TexCoord2f(0, previousV);
Rlgl.Vertex2f(prevNegNormal.X, prevNegNormal.Y);
Rlgl.TexCoord2f(1, previousV);
Rlgl.Vertex2f(prevPosNormal.X, prevPosNormal.Y);
Rlgl.TexCoord2f(1, v);
Rlgl.Vertex2f(currentPosNormal.X, currentPosNormal.Y);
Rlgl.TexCoord2f(0, v);
Rlgl.Vertex2f(currentNegNormal.X, currentNegNormal.Y);
Rlgl.End();
// The current step is the start of the neXt step
previous = current;
previousTangent = normal;
previousV = v;
}
}
static void UpdateOptions()
{
if (IsKeyPressed(KeyboardKey.KEY_SPACE))
{
showCurve = !showCurve;
}
// Update with
if (IsKeyPressed(KeyboardKey.KEY_EQUAL))
{
curveWidth += 2;
}
if (IsKeyPressed(KeyboardKey.KEY_MINUS))
{
curveWidth -= 2;
}
if (curveWidth < 2)
{
curveWidth = 2;
}
// Update segments
if (IsKeyPressed(KeyboardKey.KEY_LEFT))
{
curveSegments -= 2;
}
if (IsKeyPressed(KeyboardKey.KEY_RIGHT))
{
curveSegments += 2;
}
if (curveSegments < 2)
{
curveSegments = 2;
}
}
}

View File

@ -0,0 +1,74 @@
/*******************************************************************************************
*
* raylib [textures] example - Retrieve image data from texture: GetTextureData()
*
* NOTE: Images are loaded in CPU memory (RAM); textures are loaded in GPU memory (VRAM)
*
* This example has been created using raylib 1.3 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
using static Raylib_cs.Raylib;
namespace Examples.Textures;
public class ToImage
{
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [textures] example - texture to image");
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
Image image = LoadImage("resources/raylib-cs_logo.png");
Texture2D texture = LoadTextureFromImage(image);
UnloadImage(image);
image = LoadImageFromTexture(texture);
UnloadTexture(texture);
texture = LoadTextureFromImage(image);
UnloadImage(image);
//---------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose())
{
// Update
//----------------------------------------------------------------------------------
// TODO: Update your variables here
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RAYWHITE);
int x = screenWidth / 2 - texture.Width / 2;
int y = screenHeight / 2 - texture.Height / 2;
DrawTexture(texture, x, y, Color.WHITE);
DrawText("this IS a texture loaded from an image!", 300, 370, 10, Color.GRAY);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(texture);
CloseWindow();
//--------------------------------------------------------------------------------------
return 0;
}
}