blend modes
There is no browser build of this example yet.
Is it the same as C?
- Desktop:verified The Zig version renders a final frame that is byte-identical to the C example.
- Browser:not checked yet The browser comparison does not cover this example yet.
//! raylib [textures] example - blend modes
//!
//! NOTE: Images are loaded in CPU memory (RAM); textures are loaded in GPU memory (VRAM)
//!
//! Zig-API translation of examples/textures/textures_blend_modes.c (raylib 6.2).
//! Example contributed by Karlo Licudine (@accidentalrebel) and reviewed by Ramon Santamaria (@raysan5)
//! Example licensed under an unmodified zlib/libpng license.
//! Copyright (c) 2020-2025 Karlo Licudine (@accidentalrebel)
const rl = @import("raylib");
pub fn main() !void {
// Initialization
const screen_width = 800;
const screen_height = 450;
rl.initWindow(screen_width, screen_height, "raylib [textures] example - blend modes");
defer rl.closeWindow(); // Close window and OpenGL context
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
const bg_image = try rl.loadImage("resources/cyberpunk_street_background.png"); // Loaded in CPU memory (RAM)
const bg_texture = try rl.loadTextureFromImage(bg_image); // Image converted to texture, GPU memory (VRAM)
defer rl.unloadTexture(bg_texture);
const fg_image = try rl.loadImage("resources/cyberpunk_street_foreground.png"); // Loaded in CPU memory (RAM)
const fg_texture = try rl.loadTextureFromImage(fg_image); // Image converted to texture, GPU memory (VRAM)
defer rl.unloadTexture(fg_texture);
// Once image has been converted to texture and uploaded to VRAM, it can be unloaded from RAM
rl.unloadImage(bg_image);
rl.unloadImage(fg_image);
// The blend modes cycled through with SPACE
const blend_modes = [_]rl.BlendMode{ .alpha, .additive, .multiplied, .add_colors };
var blend_index: usize = 0;
rl.setTargetFPS(60); // Set our game to run at 60 frames-per-second
// Main game loop
while (!rl.windowShouldClose()) { // Detect window close button or ESC key
// Update
if (rl.isKeyPressed(.space)) blend_index = (blend_index + 1) % blend_modes.len;
const blend_mode = blend_modes[blend_index];
// Draw
rl.beginDrawing();
defer rl.endDrawing();
rl.clearBackground(.ray_white);
rl.drawTexture(bg_texture, @divTrunc(screen_width, 2) - @divTrunc(bg_texture.width, 2), @divTrunc(screen_height, 2) - @divTrunc(bg_texture.height, 2), .white);
// Apply the blend mode and then draw the foreground texture
rl.beginBlendMode(blend_mode);
rl.drawTexture(fg_texture, @divTrunc(screen_width, 2) - @divTrunc(fg_texture.width, 2), @divTrunc(screen_height, 2) - @divTrunc(fg_texture.height, 2), .white);
rl.endBlendMode();
// Draw the texts
rl.drawText("Press SPACE to change blend modes.", 310, 350, 10, .gray);
const label: [:0]const u8 = switch (blend_mode) {
.alpha => "Current: BLEND_ALPHA",
.additive => "Current: BLEND_ADDITIVE",
.multiplied => "Current: BLEND_MULTIPLIED",
.add_colors => "Current: BLEND_ADD_COLORS",
else => unreachable,
};
rl.drawText(label, (screen_width / 2) - 60, 370, 10, .gray);
rl.drawText("(c) Cyberpunk Street Environment by Luis Zuno (@ansimuz)", screen_width - 330, screen_height - 20, 10, .gray);
}
}/*******************************************************************************************
*
* 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
*
* Example contributed by Karlo Licudine (@accidentalrebel) 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) 2020-2025 Karlo Licudine (@accidentalrebel)
*
********************************************************************************************/
#include "raylib.h"
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// 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"); // Loaded in CPU memory (RAM)
Texture2D bgTexture = LoadTextureFromImage(bgImage); // Image converted to texture, GPU memory (VRAM)
Image fgImage = LoadImage("resources/cyberpunk_street_foreground.png"); // Loaded in CPU memory (RAM)
Texture2D fgTexture = LoadTextureFromImage(fgImage); // Image converted to texture, GPU memory (VRAM)
// 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;
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
//----------------------------------------------------------------------------------
if (IsKeyPressed(KEY_SPACE))
{
if (blendMode >= (blendCountMax - 1)) blendMode = 0;
else blendMode++;
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
DrawTexture(bgTexture, screenWidth/2 - bgTexture.width/2, screenHeight/2 - bgTexture.height/2, WHITE);
// Apply the blend mode and then draw the foreground texture
BeginBlendMode(blendMode);
DrawTexture(fgTexture, screenWidth/2 - fgTexture.width/2, screenHeight/2 - fgTexture.height/2, WHITE);
EndBlendMode();
// Draw the texts
DrawText("Press SPACE to change blend modes.", 310, 350, 10, GRAY);
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;
default: break;
}
DrawText("(c) Cyberpunk Street Environment by Luis Zuno (@ansimuz)", screenWidth - 330, screenHeight - 20, 10, GRAY);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(fgTexture); // Unload foreground texture
UnloadTexture(bgTexture); // Unload background texture
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}From raylib 6.2. The original on GitHub.