Merge branch 'master' into rhythia
|
|
@ -8,7 +8,7 @@
|
|||
| explosion.png | [Unity Labs Paris](https://blogs.unity3d.com/2016/11/28/free-vfx-image-sequences-flipbooks/) | [CC0](https://creativecommons.org/publicdomain/zero/1.0/) |
|
||||
| parrots.png | [Kodak set](http://r0k.us/graphics/kodak/) | ❔ | Original name: `kodim23.png`
|
||||
| cat.png | ❔ | ❔ | - |
|
||||
| wabbit_alpha.png | ❔ | ❔ | - |
|
||||
| raybunny.png | [VoidSrc*](https://x.com/voidsrc) | [CC0](https://creativecommons.org/publicdomain/zero/1.0/) | - |
|
||||
| custom_jupiter_crash.png | [Brian Kent (AEnigma)](https://www.dafont.com/es/aenigma.d188) | [Freeware](https://www.dafont.com/es/jupiter-crash.font) | Atlas created by [@raysan5](https://github.com/raysan5) |
|
||||
| KAISG.ttf | [Dieter Steffmann](http://www.steffmann.de/wordpress/) | [Freeware](https://www.1001fonts.com/users/steffmann/) | [Kaiserzeit Gotisch](https://www.dafont.com/es/kaiserzeit-gotisch.font) font |
|
||||
| fudesumi.png | [Eiden Marsal](https://www.artstation.com/marshall_z) | [CC-BY-NC](https://creativecommons.org/licenses/by-nc/4.0/) | - |
|
||||
|
|
|
|||
BIN
examples/textures/resources/booth.png
Normal file
|
After Width: | Height: | Size: 62 KiB |
BIN
examples/textures/resources/raybunny.png
Normal file
|
After Width: | Height: | Size: 466 B |
|
Before Width: | Height: | Size: 496 B |
|
|
@ -1,13 +1,15 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [textures] example - Background scrolling
|
||||
* raylib [textures] example - background scrolling
|
||||
*
|
||||
* Example complexity rating: [★☆☆☆] 1/4
|
||||
*
|
||||
* Example originally created with raylib 2.0, last time updated with raylib 2.5
|
||||
*
|
||||
* 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) 2019-2024 Ramon Santamaria (@raysan5)
|
||||
* Copyright (c) 2019-2025 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@
|
|||
*
|
||||
* raylib [textures] example - blend modes
|
||||
*
|
||||
* Example complexity rating: [★☆☆☆] 1/4
|
||||
*
|
||||
* NOTE: Images are loaded in CPU memory (RAM); textures are loaded in GPU memory (VRAM)
|
||||
*
|
||||
* Example originally created with raylib 3.5, last time updated with raylib 3.5
|
||||
|
|
@ -11,7 +13,7 @@
|
|||
* 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) 2020-2024 Karlo Licudine (@accidentalrebel)
|
||||
* Copyright (c) 2020-2025 Karlo Licudine (@accidentalrebel)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
|
|
@ -76,10 +78,10 @@ int main(void)
|
|||
|
||||
switch (blendMode)
|
||||
{
|
||||
case BLEND_ALPHA: DrawText("Current: BLEND_ALPHA", (screenWidth / 2) - 60, 370, 10, GRAY); break;
|
||||
case BLEND_ADDITIVE: DrawText("Current: BLEND_ADDITIVE", (screenWidth / 2) - 60, 370, 10, GRAY); break;
|
||||
case BLEND_MULTIPLIED: DrawText("Current: BLEND_MULTIPLIED", (screenWidth / 2) - 60, 370, 10, GRAY); break;
|
||||
case BLEND_ADD_COLORS: DrawText("Current: BLEND_ADD_COLORS", (screenWidth / 2) - 60, 370, 10, GRAY); break;
|
||||
case BLEND_ALPHA: DrawText("Current: BLEND_ALPHA", (screenWidth/2) - 60, 370, 10, GRAY); break;
|
||||
case BLEND_ADDITIVE: DrawText("Current: BLEND_ADDITIVE", (screenWidth/2) - 60, 370, 10, GRAY); break;
|
||||
case BLEND_MULTIPLIED: DrawText("Current: BLEND_MULTIPLIED", (screenWidth/2) - 60, 370, 10, GRAY); break;
|
||||
case BLEND_ADD_COLORS: DrawText("Current: BLEND_ADD_COLORS", (screenWidth/2) - 60, 370, 10, GRAY); break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,13 +1,15 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [textures] example - Bunnymark
|
||||
* raylib [textures] example - bunnymark
|
||||
*
|
||||
* Example complexity rating: [★★★☆] 3/4
|
||||
*
|
||||
* Example originally created with raylib 1.6, last time updated with raylib 2.5
|
||||
*
|
||||
* 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-2024 Ramon Santamaria (@raysan5)
|
||||
* Copyright (c) 2014-2025 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
|
|
@ -15,12 +17,15 @@
|
|||
|
||||
#include <stdlib.h> // Required for: malloc(), free()
|
||||
|
||||
#define MAX_BUNNIES 50000 // 50K bunnies limit
|
||||
#define MAX_BUNNIES 80000 // 80K bunnies limit
|
||||
|
||||
// This is the maximum amount of elements (quads) per batch
|
||||
// NOTE: This value is defined in [rlgl] module and can be changed there
|
||||
#define MAX_BATCH_ELEMENTS 8192
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Types and Structures Definition
|
||||
//----------------------------------------------------------------------------------
|
||||
typedef struct Bunny {
|
||||
Vector2 position;
|
||||
Vector2 speed;
|
||||
|
|
@ -40,13 +45,15 @@ int main(void)
|
|||
InitWindow(screenWidth, screenHeight, "raylib [textures] example - bunnymark");
|
||||
|
||||
// Load bunny texture
|
||||
Texture2D texBunny = LoadTexture("resources/wabbit_alpha.png");
|
||||
Texture2D texBunny = LoadTexture("resources/raybunny.png");
|
||||
|
||||
Bunny *bunnies = (Bunny *)malloc(MAX_BUNNIES*sizeof(Bunny)); // Bunnies array
|
||||
Bunny *bunnies = (Bunny *)RL_MALLOC(MAX_BUNNIES*sizeof(Bunny)); // Bunnies array
|
||||
|
||||
int bunniesCount = 0; // Bunnies counter
|
||||
|
||||
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||
bool paused = false;
|
||||
|
||||
//SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
|
|
@ -62,8 +69,8 @@ int main(void)
|
|||
if (bunniesCount < MAX_BUNNIES)
|
||||
{
|
||||
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].speed.x = (float)GetRandomValue(-250, 250);
|
||||
bunnies[bunniesCount].speed.y = (float)GetRandomValue(-250, 250);
|
||||
bunnies[bunniesCount].color = (Color){ GetRandomValue(50, 240),
|
||||
GetRandomValue(80, 240),
|
||||
GetRandomValue(100, 240), 255 };
|
||||
|
|
@ -72,16 +79,21 @@ int main(void)
|
|||
}
|
||||
}
|
||||
|
||||
// Update bunnies
|
||||
for (int i = 0; i < bunniesCount; i++)
|
||||
{
|
||||
bunnies[i].position.x += bunnies[i].speed.x;
|
||||
bunnies[i].position.y += bunnies[i].speed.y;
|
||||
if (IsKeyPressed(KEY_P)) paused = !paused;
|
||||
|
||||
if (((bunnies[i].position.x + texBunny.width/2) > GetScreenWidth()) ||
|
||||
((bunnies[i].position.x + texBunny.width/2) < 0)) bunnies[i].speed.x *= -1;
|
||||
if (((bunnies[i].position.y + texBunny.height/2) > GetScreenHeight()) ||
|
||||
((bunnies[i].position.y + texBunny.height/2 - 40) < 0)) bunnies[i].speed.y *= -1;
|
||||
if (!paused)
|
||||
{
|
||||
// Update bunnies
|
||||
for (int i = 0; i < bunniesCount; i++)
|
||||
{
|
||||
bunnies[i].position.x += bunnies[i].speed.x*GetFrameTime();
|
||||
bunnies[i].position.y += bunnies[i].speed.y*GetFrameTime();
|
||||
|
||||
if (((bunnies[i].position.x + (float)texBunny.width/2) > GetScreenWidth()) ||
|
||||
((bunnies[i].position.x + (float)texBunny.width/2) < 0)) bunnies[i].speed.x *= -1;
|
||||
if (((bunnies[i].position.y + (float)texBunny.height/2) > GetScreenHeight()) ||
|
||||
((bunnies[i].position.y + (float)texBunny.height/2 - 40) < 0)) bunnies[i].speed.y *= -1;
|
||||
}
|
||||
}
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
|
|
@ -114,7 +126,7 @@ int main(void)
|
|||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
free(bunnies); // Unload bunnies data array
|
||||
RL_FREE(bunnies); // Unload bunnies data array
|
||||
|
||||
UnloadTexture(texBunny); // Unload bunny texture
|
||||
|
||||
|
|
|
|||
|
Before Width: | Height: | Size: 427 KiB After Width: | Height: | Size: 423 KiB |
212
examples/textures/textures_cellular_automata.c
Normal file
|
|
@ -0,0 +1,212 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* 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)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
// Initialization constants
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
const int imageWidth = 800;
|
||||
const int imageHeight = 800/2;
|
||||
|
||||
// Rule button sizes and positions
|
||||
const int drawRuleStartX = 585;
|
||||
const int drawRuleStartY = 10;
|
||||
const int drawRuleSpacing = 15;
|
||||
const int drawRuleGroupSpacing = 50;
|
||||
const int drawRuleSize = 14;
|
||||
const int drawRuleInnerSize = 10;
|
||||
|
||||
// Preset button sizes
|
||||
const int presetsSizeX = 42;
|
||||
const int presetsSizeY = 22;
|
||||
|
||||
const int linesUpdatedPerFrame = 4;
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Functions
|
||||
//----------------------------------------------------------------------------------
|
||||
void ComputeLine(Image *image, int line, int rule)
|
||||
{
|
||||
// Compute next line pixels. Boundaries are not computed, always 0
|
||||
for (int i = 1; i < imageWidth - 1; i++)
|
||||
{
|
||||
// Get, from the previous line, the 3 pixels states as a binary value
|
||||
const int 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
|
||||
const bool currValue = (rule & (1 << prevValue));
|
||||
// Update pixel color
|
||||
ImageDrawPixel(image, i, line, (currValue)? BLACK : RAYWHITE);
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Program main entry point
|
||||
//------------------------------------------------------------------------------------
|
||||
int main(void)
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
InitWindow(screenWidth, screenHeight, "raylib [textures] example - cellular automata");
|
||||
|
||||
// Image that contains the cellular automaton
|
||||
Image image = GenImageColor(imageWidth, imageHeight, RAYWHITE);
|
||||
// The top central pixel set as black
|
||||
ImageDrawPixel(&image, imageWidth/2, 0, BLACK);
|
||||
|
||||
Texture2D texture = LoadTextureFromImage(image);
|
||||
|
||||
// Some interesting rules
|
||||
const int presetValues[] = { 18, 30, 60, 86, 102, 124, 126, 150, 182, 225 };
|
||||
const int presetsCount = sizeof(presetValues)/sizeof(presetValues[0]);
|
||||
|
||||
// Variables
|
||||
int rule = 30; // Starting rule
|
||||
int line = 1; // Line to compute, starting from line 1. One point in line 0 is already set
|
||||
|
||||
SetTargetFPS(60);
|
||||
//---------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
// Handle mouse
|
||||
const Vector2 mouse = GetMousePosition();
|
||||
int mouseInCell = -1; // -1: outside any button; 0-7: rule cells; 8+: preset cells
|
||||
|
||||
// Check mouse on rule cells
|
||||
for (int i = 0; i < 8; i++)
|
||||
{
|
||||
const int cellX = drawRuleStartX - drawRuleGroupSpacing*i + drawRuleSpacing;
|
||||
const int 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 (int i = 0; i < presetsCount; i++)
|
||||
{
|
||||
const int cellX = 4 + (presetsSizeX + 2)*(i/2);
|
||||
const int 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(MOUSE_BUTTON_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(&image, RAYWHITE);
|
||||
ImageDrawPixel(&image, imageWidth/2, 0, BLACK);
|
||||
line = 1;
|
||||
}
|
||||
|
||||
// Compute next lines
|
||||
//----------------------------------------------------------------------------------
|
||||
if (line < imageHeight)
|
||||
{
|
||||
for (int i = 0; (i < linesUpdatedPerFrame) && (line + i < imageHeight); i++)
|
||||
ComputeLine(&image, line + i, rule);
|
||||
line += linesUpdatedPerFrame;
|
||||
|
||||
UpdateTexture(texture, image.data);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
// Draw cellular automaton texture
|
||||
DrawTexture(texture, 0, screenHeight - imageHeight, WHITE);
|
||||
|
||||
// Draw preset values
|
||||
for (int i = 0; i < presetsCount; i++)
|
||||
{
|
||||
DrawText(TextFormat("%i", presetValues[i]), 8 + (presetsSizeX + 2)*(i/2), 4 + (presetsSizeY + 2)*(i%2), 20, GRAY);
|
||||
DrawRectangleLines(4 + (presetsSizeX + 2)*(i/2), 2 + (presetsSizeY + 2)*(i%2), presetsSizeX, presetsSizeY, BLUE);
|
||||
|
||||
// If the mouse is on this preset, highlight it
|
||||
if (mouseInCell == i + 8)
|
||||
DrawRectangleLinesEx((Rectangle) { 2 + (presetsSizeX + 2.0f)*(i/2),
|
||||
(presetsSizeY + 2.0f)*(i%2),
|
||||
presetsSizeX + 4.0f, presetsSizeY + 4.0f }, 3, RED);
|
||||
}
|
||||
|
||||
// Draw rule bits
|
||||
for (int i = 0; i < 8; i++)
|
||||
{
|
||||
// The three input bits
|
||||
for (int j = 0; j < 3; j++)
|
||||
{
|
||||
DrawRectangleLines(drawRuleStartX - drawRuleGroupSpacing*i + drawRuleSpacing*j, drawRuleStartY, drawRuleSize, drawRuleSize, GRAY);
|
||||
if (i & (4 >> j))
|
||||
DrawRectangle(drawRuleStartX + 2 - drawRuleGroupSpacing*i + drawRuleSpacing*j, drawRuleStartY + 2, drawRuleInnerSize, drawRuleInnerSize, BLACK);
|
||||
}
|
||||
|
||||
// The output bit
|
||||
DrawRectangleLines(drawRuleStartX - drawRuleGroupSpacing*i + drawRuleSpacing, drawRuleStartY + drawRuleSpacing, drawRuleSize, drawRuleSize, BLUE);
|
||||
if (rule & (1 << i))
|
||||
DrawRectangle(drawRuleStartX + 2 - drawRuleGroupSpacing*i + drawRuleSpacing, drawRuleStartY + 2 + drawRuleSpacing, drawRuleInnerSize, drawRuleInnerSize, BLACK);
|
||||
|
||||
// If the mouse is on this rule bit, highlight it
|
||||
if (mouseInCell == i)
|
||||
DrawRectangleLinesEx((Rectangle){ drawRuleStartX - drawRuleGroupSpacing*i + drawRuleSpacing - 2.0f,
|
||||
drawRuleStartY + drawRuleSpacing - 2.0f,
|
||||
drawRuleSize + 4.0f, drawRuleSize + 4.0f }, 3, RED);
|
||||
}
|
||||
|
||||
DrawText(TextFormat("RULE: %i", rule), drawRuleStartX + drawRuleSpacing*4, drawRuleStartY + 1, 30, GRAY);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
UnloadImage(image);
|
||||
UnloadTexture(texture);
|
||||
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
BIN
examples/textures/textures_cellular_automata.png
Normal file
|
After Width: | Height: | Size: 14 KiB |
110
examples/textures/textures_clipboard_image.c
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* 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)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
#define MAX_TEXTURE_COLLECTION 20
|
||||
|
||||
typedef struct TextureCollection {
|
||||
Texture2D texture;
|
||||
Vector2 position;
|
||||
} TextureCollection;
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Program main entry point
|
||||
//------------------------------------------------------------------------------------
|
||||
int main(void)
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [textures] example - clipboard image");
|
||||
|
||||
TextureCollection collection[MAX_TEXTURE_COLLECTION] = { 0 };
|
||||
int currentCollectionIndex = 0;
|
||||
|
||||
SetTargetFPS(60);
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
if (IsKeyPressed(KEY_R)) // Reset image collection
|
||||
{
|
||||
// Unload textures to avoid memory leaks
|
||||
for (int i = 0; i < MAX_TEXTURE_COLLECTION; i++) UnloadTexture(collection[i].texture);
|
||||
|
||||
currentCollectionIndex = 0;
|
||||
}
|
||||
|
||||
if (IsKeyDown(KEY_LEFT_CONTROL) && IsKeyPressed(KEY_V) &&
|
||||
(currentCollectionIndex < MAX_TEXTURE_COLLECTION))
|
||||
{
|
||||
Image image = GetClipboardImage();
|
||||
|
||||
if (IsImageValid(image))
|
||||
{
|
||||
collection[currentCollectionIndex].texture = LoadTextureFromImage(image);
|
||||
collection[currentCollectionIndex].position = GetMousePosition();
|
||||
currentCollectionIndex++;
|
||||
UnloadImage(image);
|
||||
}
|
||||
else TraceLog(LOG_INFO, "IMAGE: Could not retrieve image from clipboard");
|
||||
}
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
for (int i = 0; i < currentCollectionIndex; i++)
|
||||
{
|
||||
if (IsTextureValid(collection[i].texture))
|
||||
{
|
||||
DrawTexturePro(collection[i].texture,
|
||||
(Rectangle){0,0,collection[i].texture.width, collection[i].texture.height},
|
||||
(Rectangle){collection[i].position.x,collection[i].position.y,collection[i].texture.width, collection[i].texture.height},
|
||||
(Vector2){collection[i].texture.width*0.5f, collection[i].texture.height*0.5f},
|
||||
0.0f, WHITE);
|
||||
}
|
||||
}
|
||||
|
||||
DrawRectangle(0, 0, screenWidth, 40, BLACK);
|
||||
DrawText("Clipboard Image - Ctrl+V to Paste and R to Reset ", 120, 10, 20, LIGHTGRAY);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
for (int i = 0; i < MAX_TEXTURE_COLLECTION;i ++)
|
||||
{
|
||||
UnloadTexture(collection[i].texture);
|
||||
}
|
||||
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
BIN
examples/textures/textures_clipboard_image.png
Normal file
|
After Width: | Height: | Size: 197 KiB |
|
|
@ -1,13 +1,15 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [textures] example - Fog of war
|
||||
* 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-2024 Ramon Santamaria (@raysan5)
|
||||
* Copyright (c) 2018-2025 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
|
|
@ -19,6 +21,9 @@
|
|||
#define PLAYER_SIZE 16 // Player size
|
||||
#define PLAYER_TILE_VISIBILITY 2 // Player can see 2 tiles around its position
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Types and Structures Definition
|
||||
//----------------------------------------------------------------------------------
|
||||
// Map data type
|
||||
typedef struct Map {
|
||||
unsigned int tilesX; // Number of tiles in X axis
|
||||
|
|
@ -46,8 +51,8 @@ int main(void)
|
|||
// 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 = (unsigned char *)calloc(map.tilesX*map.tilesY, sizeof(unsigned char));
|
||||
map.tileFog = (unsigned char *)calloc(map.tilesX*map.tilesY, sizeof(unsigned char));
|
||||
map.tileIds = (unsigned char *)RL_CALLOC(map.tilesX*map.tilesY, sizeof(unsigned char));
|
||||
map.tileFog = (unsigned char *)RL_CALLOC(map.tilesX*map.tilesY, sizeof(unsigned char));
|
||||
|
||||
// Load map tiles (generating 2 random tile ids for testing)
|
||||
// NOTE: Map tile ids should be probably loaded from an external map file
|
||||
|
|
@ -63,6 +68,7 @@ int main(void)
|
|||
// at a smaller size (one pixel per tile) and scale it on drawing with bilinear filtering
|
||||
RenderTexture2D fogOfWar = LoadRenderTexture(map.tilesX, map.tilesY);
|
||||
SetTextureFilter(fogOfWar.texture, TEXTURE_FILTER_BILINEAR);
|
||||
SetTextureWrap(fogOfWar.texture, TEXTURE_WRAP_CLAMP);
|
||||
|
||||
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
|
@ -88,8 +94,8 @@ int main(void)
|
|||
for (unsigned int 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 + MAP_TILE_SIZE/2)/MAP_TILE_SIZE);
|
||||
playerTileY = (int)((playerPosition.y + MAP_TILE_SIZE/2)/MAP_TILE_SIZE);
|
||||
playerTileX = (int)((playerPosition.x + (float)MAP_TILE_SIZE/2)/MAP_TILE_SIZE);
|
||||
playerTileY = (int)((playerPosition.y + (float)MAP_TILE_SIZE/2)/MAP_TILE_SIZE);
|
||||
|
||||
// Check visibility and update fog
|
||||
// NOTE: We check tilemap limits to avoid processing tiles out-of-array-bounds (it could crash program)
|
||||
|
|
@ -143,8 +149,8 @@ int main(void)
|
|||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
free(map.tileIds); // Free allocated map tile ids
|
||||
free(map.tileFog); // Free allocated map tile fog state
|
||||
RL_FREE(map.tileIds); // Free allocated map tile ids
|
||||
RL_FREE(map.tileFog); // Free allocated map tile fog state
|
||||
|
||||
UnloadRenderTexture(fogOfWar); // Unload render texture
|
||||
|
||||
|
|
|
|||
209
examples/textures/textures_framebuffer_rendering.c
Normal file
|
|
@ -0,0 +1,209 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* 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)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#include "raylib.h"
|
||||
#include "raymath.h"
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Module Functions Declaration
|
||||
//------------------------------------------------------------------------------------
|
||||
static void DrawCameraPrism(Camera3D camera, float aspect, Color color);
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Program main entry point
|
||||
//------------------------------------------------------------------------------------
|
||||
int main(void)
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
const int splitWidth = screenWidth/2;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [textures] example - framebuffer rendering");
|
||||
|
||||
// Camera to look at the 3D world
|
||||
Camera3D subjectCamera = { 0 };
|
||||
subjectCamera.position = (Vector3){ 5.0f, 5.0f, 5.0f };
|
||||
subjectCamera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
|
||||
subjectCamera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
|
||||
subjectCamera.fovy = 45.0f;
|
||||
subjectCamera.projection = CAMERA_PERSPECTIVE;
|
||||
|
||||
// Camera to observe the subject camera and 3D world
|
||||
Camera3D observerCamera = { 0 };
|
||||
observerCamera.position = (Vector3){ 10.0f, 10.0f, 10.0f };
|
||||
observerCamera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
|
||||
observerCamera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
|
||||
observerCamera.fovy = 45.0f;
|
||||
observerCamera.projection = CAMERA_PERSPECTIVE;
|
||||
|
||||
// Set up render textures
|
||||
RenderTexture2D observerTarget = LoadRenderTexture(splitWidth, screenHeight);
|
||||
Rectangle observerSource = { 0.0f, 0.0f, (float)observerTarget.texture.width, -(float)observerTarget.texture.height };
|
||||
Rectangle observerDest = { 0.0f, 0.0f, (float)splitWidth, (float)screenHeight };
|
||||
|
||||
RenderTexture2D subjectTarget = LoadRenderTexture(splitWidth, screenHeight);
|
||||
Rectangle subjectSource = { 0.0f, 0.0f, (float)subjectTarget.texture.width, -(float)subjectTarget.texture.height };
|
||||
Rectangle subjectDest = { (float)splitWidth, 0.0f, (float)splitWidth, (float)screenHeight };
|
||||
const float textureAspectRatio = (float)subjectTarget.texture.width/(float)subjectTarget.texture.height;
|
||||
|
||||
// Rectangles for cropping render texture
|
||||
const float captureSize = 128.0f;
|
||||
Rectangle cropSource = { (subjectTarget.texture.width - captureSize)/2.0f, (subjectTarget.texture.height - captureSize)/2.0f, captureSize, -captureSize };
|
||||
Rectangle cropDest = { splitWidth + 20.0f, 20.0f, captureSize, captureSize};
|
||||
|
||||
SetTargetFPS(60);
|
||||
DisableCursor();
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
UpdateCamera(&observerCamera, CAMERA_FREE);
|
||||
UpdateCamera(&subjectCamera, CAMERA_ORBITAL);
|
||||
|
||||
if (IsKeyPressed(KEY_R)) observerCamera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
|
||||
|
||||
// Build LHS observer view texture
|
||||
BeginTextureMode(observerTarget);
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
BeginMode3D(observerCamera);
|
||||
|
||||
DrawGrid(10, 1.0f);
|
||||
DrawCube((Vector3){ 0.0f, 0.0f, 0.0f }, 2.0f, 2.0f, 2.0f, GOLD);
|
||||
DrawCubeWires((Vector3){ 0.0f, 0.0f, 0.0f }, 2.0f, 2.0f, 2.0f, PINK);
|
||||
DrawCameraPrism(subjectCamera, textureAspectRatio, GREEN);
|
||||
|
||||
EndMode3D();
|
||||
|
||||
DrawText("Observer View", 10, observerTarget.texture.height - 30, 20, BLACK);
|
||||
DrawText("WASD + Mouse to Move", 10, 10, 20, DARKGRAY);
|
||||
DrawText("Scroll to Zoom", 10, 30, 20, DARKGRAY);
|
||||
DrawText("R to Reset Observer Target", 10, 50, 20, DARKGRAY);
|
||||
|
||||
EndTextureMode();
|
||||
|
||||
// Build RHS subject view texture
|
||||
BeginTextureMode(subjectTarget);
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
BeginMode3D(subjectCamera);
|
||||
|
||||
DrawCube((Vector3){ 0.0f, 0.0f, 0.0f }, 2.0f, 2.0f, 2.0f, GOLD);
|
||||
DrawCubeWires((Vector3){ 0.0f, 0.0f, 0.0f }, 2.0f, 2.0f, 2.0f, PINK);
|
||||
DrawGrid(10, 1.0f);
|
||||
|
||||
EndMode3D();
|
||||
|
||||
DrawRectangleLines((int)((subjectTarget.texture.width - captureSize)/2.0f), (int)((subjectTarget.texture.height - captureSize)/2.0f), captureSize, captureSize, GREEN);
|
||||
DrawText("Subject View", 10, subjectTarget.texture.height - 30, 20, BLACK);
|
||||
|
||||
EndTextureMode();
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(BLACK);
|
||||
|
||||
// Draw observer texture LHS
|
||||
DrawTexturePro(observerTarget.texture, observerSource, observerDest, (Vector2){0.0f, 0.0f }, 0.0f, WHITE);
|
||||
|
||||
// Draw subject texture RHS
|
||||
DrawTexturePro(subjectTarget.texture, subjectSource, subjectDest, (Vector2){ 0.0f, 0.0f }, 0.0f, WHITE);
|
||||
|
||||
// Draw the small crop overlay on top
|
||||
DrawTexturePro(subjectTarget.texture, cropSource, cropDest, (Vector2){ 0.0f, 0.0f }, 0.0f, WHITE);
|
||||
DrawRectangleLinesEx(cropDest, 2, BLACK);
|
||||
|
||||
// Draw split screen divider line
|
||||
DrawLine(splitWidth, 0, splitWidth, screenHeight, BLACK);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
UnloadRenderTexture(observerTarget);
|
||||
UnloadRenderTexture(subjectTarget);
|
||||
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Module Functions Definition
|
||||
//----------------------------------------------------------------------------------
|
||||
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[4] = {
|
||||
{ -1.0f, -1.0f, 1.0f }, // Bottom Left
|
||||
{ 1.0f, -1.0f, 1.0f }, // Bottom Right
|
||||
{ 1.0f, 1.0f, 1.0f }, // Top Right
|
||||
{ -1.0f, 1.0f, 1.0f } // Top Left
|
||||
};
|
||||
|
||||
// Build the matrices
|
||||
Matrix view = GetCameraMatrix(camera);
|
||||
Matrix proj = MatrixPerspective(camera.fovy * DEG2RAD, aspect, 0.05f, length);
|
||||
// Combine view and projection so we can reverse the full camera transform
|
||||
Matrix viewProj = MatrixMultiply(view, proj);
|
||||
// Invert the view-projection matrix to unproject points from NDC space back into world space
|
||||
Matrix inverseViewProj = MatrixInvert(viewProj);
|
||||
|
||||
// Transform the 4 plane corners from NDC into world space
|
||||
Vector3 corners[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.m0*x + inverseViewProj.m4*y + inverseViewProj.m8*z + inverseViewProj.m12;
|
||||
float vy = inverseViewProj.m1*x + inverseViewProj.m5*y + inverseViewProj.m9*z + inverseViewProj.m13;
|
||||
float vz = inverseViewProj.m2*x + inverseViewProj.m6*y + inverseViewProj.m10*z + inverseViewProj.m14;
|
||||
float vw = inverseViewProj.m3*x + inverseViewProj.m7*y + inverseViewProj.m11*z + inverseViewProj.m15;
|
||||
|
||||
corners[i] = (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);
|
||||
}
|
||||
}
|
||||
BIN
examples/textures/textures_framebuffer_rendering.png
Normal file
|
After Width: | Height: | Size: 31 KiB |
|
|
@ -1,13 +1,15 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [textures] example - gif playing
|
||||
* 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-2024 Ramon Santamaria (@raysan5)
|
||||
* Copyright (c) 2021-2025 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
|
|
@ -26,7 +28,7 @@ int main(void)
|
|||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [textures] example - gif playing");
|
||||
InitWindow(screenWidth, screenHeight, "raylib [textures] example - gif player");
|
||||
|
||||
int animFrames = 0;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,21 +1,23 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [textures] example - Retrive image channel (mask)
|
||||
* raylib [textures] example - image channel
|
||||
*
|
||||
* NOTE: Images are loaded in CPU memory (RAM); textures are loaded in GPU memory (VRAM)
|
||||
*
|
||||
* Example originally created with raylib 5.1-dev, last time updated with raylib 5.1-dev
|
||||
* Example complexity rating: [★★☆☆] 2/4
|
||||
*
|
||||
* Example contributed by Bruno Cabral (github.com/brccabral) and reviewed by Ramon Santamaria (@raysan5)
|
||||
* 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-2024 Bruno Cabral (github.com/brccabral) and Ramon Santamaria (@raysan5)
|
||||
* Copyright (c) 2024-2025 Bruno Cabral (@brccabral) and Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#include <raylib.h>
|
||||
#include "raylib.h"
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Program main entry point
|
||||
|
|
@ -24,11 +26,10 @@ int main(void)
|
|||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [textures] example - extract channel from image");
|
||||
InitWindow(screenWidth, screenHeight, "raylib [textures] example - image channel");
|
||||
|
||||
Image fudesumiImage = LoadImage("resources/fudesumi.png");
|
||||
|
||||
|
|
@ -60,21 +61,25 @@ int main(void)
|
|||
UnloadImage(imageBlue);
|
||||
UnloadImage(backgroundImage);
|
||||
|
||||
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||
|
||||
Rectangle fudesumiRec = {0, 0, fudesumiImage.width, fudesumiImage.height};
|
||||
Rectangle fudesumiRec = {0, 0, (float)fudesumiImage.width, (float)fudesumiImage.height};
|
||||
|
||||
Rectangle fudesumiPos = {50, 10, fudesumiImage.width*0.8f, fudesumiImage.height*0.8f};
|
||||
Rectangle redPos = { 410, 10, fudesumiPos.width / 2, fudesumiPos.height / 2 };
|
||||
Rectangle greenPos = { 600, 10, fudesumiPos.width / 2, fudesumiPos.height / 2 };
|
||||
Rectangle bluePos = { 410, 230, fudesumiPos.width / 2, fudesumiPos.height / 2 };
|
||||
Rectangle alphaPos = { 600, 230, fudesumiPos.width / 2, fudesumiPos.height / 2 };
|
||||
Rectangle redPos = { 410, 10, fudesumiPos.width/2.0f, fudesumiPos.height/2.0f };
|
||||
Rectangle greenPos = { 600, 10, fudesumiPos.width/2.0f, fudesumiPos.height/2.0f };
|
||||
Rectangle bluePos = { 410, 230, fudesumiPos.width/2.0f, fudesumiPos.height/2.0f };
|
||||
Rectangle alphaPos = { 600, 230, fudesumiPos.width/2.0f, fudesumiPos.height/2.0f };
|
||||
|
||||
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
// Nothing to update...
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
|
@ -99,6 +104,7 @@ int main(void)
|
|||
UnloadTexture(textureGreen);
|
||||
UnloadTexture(textureBlue);
|
||||
UnloadTexture(textureAlpha);
|
||||
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [textures] example - Image loading and drawing on it
|
||||
* raylib [textures] example - image drawing
|
||||
*
|
||||
* Example complexity rating: [★★☆☆] 2/4
|
||||
*
|
||||
* NOTE: Images are loaded in CPU memory (RAM); textures are loaded in GPU memory (VRAM)
|
||||
*
|
||||
|
|
@ -9,7 +11,7 @@
|
|||
* 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) 2016-2024 Ramon Santamaria (@raysan5)
|
||||
* Copyright (c) 2016-2025 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
|
|
|
|||
|
|
@ -1,13 +1,17 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [textures] example - Procedural images generation
|
||||
* raylib [textures] example - image generation
|
||||
*
|
||||
* Example complexity rating: [★★☆☆] 2/4
|
||||
*
|
||||
* Example originally created with raylib 1.8, last time updated with raylib 1.8
|
||||
*
|
||||
* Example contributed by Wilhem Barbier (@nounoursheureux) 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) 2O17-2024 Wilhem Barbier (@nounoursheureux) and Ramon Santamaria (@raysan5)
|
||||
* Copyright (c) 2017-2025 Wilhem Barbier (@nounoursheureux) and Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
|
|
@ -25,7 +29,7 @@ int main(void)
|
|||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [textures] example - procedural images generation");
|
||||
InitWindow(screenWidth, screenHeight, "raylib [textures] example - image generation");
|
||||
|
||||
Image verticalGradient = GenImageGradientLinear(screenWidth, screenHeight, 0, RED, BLUE);
|
||||
Image horizontalGradient = GenImageGradientLinear(screenWidth, screenHeight, 90, RED, BLUE);
|
||||
|
|
@ -88,7 +92,7 @@ int main(void)
|
|||
DrawRectangleLines(30, 400, 325, 30, Fade(WHITE, 0.5f));
|
||||
DrawText("MOUSE LEFT BUTTON to CYCLE PROCEDURAL TEXTURES", 40, 410, 10, WHITE);
|
||||
|
||||
switch(currentTexture)
|
||||
switch (currentTexture)
|
||||
{
|
||||
case 0: DrawText("VERTICAL GRADIENT", 560, 10, 20, RAYWHITE); break;
|
||||
case 1: DrawText("HORIZONTAL GRADIENT", 540, 10, 20, RAYWHITE); break;
|
||||
|
|
|
|||
|
|
@ -1,34 +1,32 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [textures] example - Image loading and texture creation
|
||||
* 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-2024 Karim Salem (@kimo-s)
|
||||
* Copyright (c) 2015-2025 Karim Salem (@kimo-s)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Module Functions Declaration
|
||||
//------------------------------------------------------------------------------------
|
||||
static void NormalizeKernel(float *kernel, int size);
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Program main entry point
|
||||
//------------------------------------------------------------------------------------
|
||||
void NormalizeKernel(float *kernel, int size)
|
||||
{
|
||||
float sum = 0.0f;
|
||||
for (int i = 0; i < size; i++) sum += kernel[i];
|
||||
|
||||
if (sum != 0.0f)
|
||||
{
|
||||
for (int i = 0; i < size; i++) kernel[i] /= sum;
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Initialization
|
||||
|
|
@ -36,24 +34,27 @@ int main(void)
|
|||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [textures] example - image convolution");
|
||||
|
||||
Image image = LoadImage("resources/cat.png"); // Loaded in CPU memory (RAM)
|
||||
InitWindow(screenWidth, screenHeight, "raylib [textures] example - image kernel");
|
||||
|
||||
float gaussiankernel[] = {
|
||||
Image 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 };
|
||||
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 };
|
||||
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 };
|
||||
0.0f, -1.0f, 0.0f
|
||||
};
|
||||
|
||||
NormalizeKernel(gaussiankernel, 9);
|
||||
NormalizeKernel(sharpenkernel, 9);
|
||||
|
|
@ -61,12 +62,12 @@ int main(void)
|
|||
|
||||
Image catSharpend = ImageCopy(image);
|
||||
ImageKernelConvolution(&catSharpend, sharpenkernel, 9);
|
||||
|
||||
|
||||
Image catSobel = ImageCopy(image);
|
||||
ImageKernelConvolution(&catSobel, sobelkernel, 9);
|
||||
|
||||
Image catGaussian = ImageCopy(image);
|
||||
|
||||
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
ImageKernelConvolution(&catGaussian, gaussiankernel, 9);
|
||||
|
|
@ -76,14 +77,14 @@ int main(void)
|
|||
ImageCrop(&catGaussian, (Rectangle){ 0, 0, (float)200, (float)450 });
|
||||
ImageCrop(&catSobel, (Rectangle){ 0, 0, (float)200, (float)450 });
|
||||
ImageCrop(&catSharpend, (Rectangle){ 0, 0, (float)200, (float)450 });
|
||||
|
||||
|
||||
// Images converted to texture, GPU memory (VRAM)
|
||||
Texture2D texture = LoadTextureFromImage(image);
|
||||
Texture2D catSharpendTexture = LoadTextureFromImage(catSharpend);
|
||||
Texture2D catSobelTexture = LoadTextureFromImage(catSobel);
|
||||
Texture2D catGaussianTexture = LoadTextureFromImage(catGaussian);
|
||||
|
||||
// Once images have been converted to texture and uploaded to VRAM,
|
||||
|
||||
// Once images have been converted to texture and uploaded to VRAM,
|
||||
// they can be unloaded from RAM
|
||||
UnloadImage(image);
|
||||
UnloadImage(catGaussian);
|
||||
|
|
@ -128,3 +129,17 @@ int main(void)
|
|||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Module Functions Definition
|
||||
//------------------------------------------------------------------------------------
|
||||
static void NormalizeKernel(float *kernel, int size)
|
||||
{
|
||||
float sum = 0.0f;
|
||||
for (int i = 0; i < size; i++) sum += kernel[i];
|
||||
|
||||
if (sum != 0.0f)
|
||||
{
|
||||
for (int i = 0; i < size; i++) kernel[i] /= sum;
|
||||
}
|
||||
}
|
||||
|
|
|
|||