raylib/ports/textures/textures_blend_modes

Licence This example has its own licence, rather than Odin's: raylib/ports/LICENSE
These are ported Raylib examples, and as such, they are subject to Raylib's license. Copyright (c) 2013-2024 Ramon Santamaria (@raysan5)
The rest of the licence
This software is provided "as-is", without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution.
A screenshot of textures_blend_modes

Code

textures_blend_modes.odin ¶
105 linesSource

1/*******************************************************************************************
2*
3*   raylib [textures] example - blend modes
4*
5*   Example complexity rating: [★☆☆☆] 1/4
6*
7*   NOTE: Images are loaded in CPU memory (RAM); textures are loaded in GPU memory (VRAM)
8*
9*   Example originally created with raylib 3.5, last time updated with raylib 3.5
10*
11*   Example contributed by Karlo Licudine (@accidentalrebel) and reviewed by Ramon Santamaria (@raysan5)
12*
13*   Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
14*   BSD-like license that allows static linking with closed source software
15*
16*   Copyright (c) 2020-2025 Karlo Licudine (@accidentalrebel)
17*
18********************************************************************************************/
19
20package raylib_examples
21
22import rl "vendor:raylib"
23
24//------------------------------------------------------------------------------------
25// Program main entry point
26//------------------------------------------------------------------------------------
27main :: proc() {
28	// Initialization
29	//--------------------------------------------------------------------------------------
30	SCREEN_WIDTH :: 800
31	SCREEN_HEIGHT :: 450
32
33	rl.InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "raylib [textures] example - blend modes")
34
35	// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
36	bg_image := rl.LoadImage("resources/cyberpunk_street_background.png")     // Loaded in CPU memory (RAM)
37	bg_texture := rl.LoadTextureFromImage(bg_image)          // Image converted to texture, GPU memory (VRAM)
38
39	fg_image := rl.LoadImage("resources/cyberpunk_street_foreground.png")     // Loaded in CPU memory (RAM)
40	fg_texture := rl.LoadTextureFromImage(fg_image)          // Image converted to texture, GPU memory (VRAM)
41
42	// Once image has been converted to texture and uploaded to VRAM, it can be unloaded from RAM
43	rl.UnloadImage(bg_image)
44	rl.UnloadImage(fg_image)
45
46	BLEND_COUNT_MAX :: 4
47	blend_mode := rl.BlendMode(0)
48
49	rl.SetTargetFPS(60)               // Set our game to run at 60 frames-per-second
50	//---------------------------------------------------------------------------------------
51
52	// Main game loop
53	for !rl.WindowShouldClose() {    // Detect window close button or ESC key
54		// Update
55		//----------------------------------------------------------------------------------
56		if rl.IsKeyPressed(.SPACE) {
57			if int(blend_mode) >= BLEND_COUNT_MAX - 1 {
58				blend_mode = rl.BlendMode(0)
59			} else {
60				blend_mode = rl.BlendMode(int(blend_mode) + 1)
61			}
62		}
63		//----------------------------------------------------------------------------------
64
65		// Draw
66		//----------------------------------------------------------------------------------
67		rl.BeginDrawing()
68
69			rl.ClearBackground(rl.RAYWHITE)
70
71			rl.DrawTexture(bg_texture, SCREEN_WIDTH/2 - bg_texture.width/2, SCREEN_HEIGHT/2 - bg_texture.height/2, rl.WHITE)
72
73			// Apply the blend mode and then draw the foreground texture
74			rl.BeginBlendMode(blend_mode)
75				rl.DrawTexture(fg_texture, SCREEN_WIDTH/2 - fg_texture.width/2, SCREEN_HEIGHT/2 - fg_texture.height/2, rl.WHITE)
76			rl.EndBlendMode()
77
78			// Draw the texts
79			rl.DrawText("Press SPACE to change blend modes.", 310, 350, 10, rl.GRAY)
80			
81			#partial switch blend_mode {
82				case .ALPHA:
83					rl.DrawText("Current: BLEND_ALPHA", (SCREEN_WIDTH/2) - 60, 370, 10, rl.GRAY)
84				case .ADDITIVE:
85					rl.DrawText("Current: BLEND_ADDITIVE", (SCREEN_WIDTH/2) - 60, 370, 10, rl.GRAY)
86				case .MULTIPLIED:
87					rl.DrawText("Current: BLEND_MULTIPLIED", (SCREEN_WIDTH/2) - 60, 370, 10, rl.GRAY)
88				case .ADD_COLORS:
89					rl.DrawText("Current: BLEND_ADD_COLORS", (SCREEN_WIDTH/2) - 60, 370, 10, rl.GRAY)
90			}
91
92			rl.DrawText("(c) Cyberpunk Street Environment by Luis Zuno (@ansimuz)", SCREEN_WIDTH - 330, SCREEN_HEIGHT - 20, 10, rl.GRAY)
93
94		rl.EndDrawing()
95		//----------------------------------------------------------------------------------
96	}
97
98	// De-Initialization
99	//--------------------------------------------------------------------------------------
100	rl.UnloadTexture(fg_texture) // Unload foreground texture
101	rl.UnloadTexture(bg_texture) // Unload background texture
102
103	rl.CloseWindow()            // Close window and OpenGL context
104	//--------------------------------------------------------------------------------------
105}

Assets 2

Declarations Used 20