-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtextures_blend_modes.php
123 lines (102 loc) · 4.18 KB
/
textures_blend_modes.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
declare(strict_types=1);
use Nawarian\Raylib\Raylib;
use Nawarian\Raylib\Types\Color;
use function Nawarian\Raylib\{
BeginBlendMode,
BeginDrawing,
ClearBackground,
CloseWindow,
DrawText,
DrawTexture,
EndBlendMode,
EndDrawing,
InitWindow,
IsKeyPressed,
LoadImage,
LoadTextureFromImage,
UnloadImage,
UnloadTexture,
WindowShouldClose
};
require_once __DIR__ . '/../../vendor/autoload.php';
// Initialization
//--------------------------------------------------------------------------------------
$screenWidth = 800;
$screenHeight = 450;
InitWindow($screenWidth, $screenHeight, 'raylib [textures] example - blend modes');
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
$bgImage = LoadImage(__DIR__ . '/resources/cyberpunk_street_background.png'); // Loaded in CPU memory (RAM)
$bgTexture = LoadTextureFromImage($bgImage); // Image converted to texture, GPU memory (VRAM)
$fgImage = LoadImage(__DIR__ . '/resources/cyberpunk_street_foreground.png'); // Loaded in CPU memory (RAM)
$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);
$blendCountMax = 4;
$blendMode = 0;
// Main game loop
while (!WindowShouldClose()) { // Detect window close button or ESC key
// Update
//----------------------------------------------------------------------------------
if (IsKeyPressed(Raylib::KEY_SPACE)) {
if ($blendMode >= ($blendCountMax - 1)) {
$blendMode = 0;
} else {
$blendMode++;
};
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color::rayWhite());
// phpcs:disable Generic.WhiteSpace.ScopeIndent.IncorrectExact
DrawTexture(
$bgTexture,
(int) ($screenWidth / 2 - $bgTexture->width / 2),
(int) ($screenHeight / 2 - $bgTexture->height / 2),
Color::white()
);
// Apply the blend mode and then draw the foreground texture
BeginBlendMode($blendMode);
DrawTexture(
$fgTexture,
(int) ($screenWidth / 2 - $fgTexture->width / 2),
(int) ($screenHeight / 2 - $fgTexture->height / 2),
Color::white()
);
EndBlendMode();
// Draw the texts
DrawText('Press SPACE to change blend modes.', 310, 350, 10, Color::gray());
switch ($blendMode) {
case Raylib::BLEND_ALPHA:
DrawText('Current: BLEND_ALPHA', (int) (($screenWidth / 2) - 60), 370, 10, Color::gray());
break;
case Raylib::BLEND_ADDITIVE:
DrawText('Current: BLEND_ADDITIVE', (int) (($screenWidth / 2) - 60), 370, 10, Color::gray());
break;
case Raylib::BLEND_MULTIPLIED:
DrawText('Current: BLEND_MULTIPLIED', (int) (($screenWidth / 2) - 60), 370, 10, Color::gray());
break;
case Raylib::BLEND_ADD_COLORS:
DrawText('Current: BLEND_ADD_COLORS', (int) (($screenWidth / 2) - 60), 370, 10, Color::gray());
break;
}
DrawText(
'(c) Cyberpunk Street Environment by Luis Zuno (@ansimuz)',
$screenWidth - 330,
$screenHeight - 20,
10,
Color::gray()
);
// phpcs:enable Generic.WhiteSpace.ScopeIndent.IncorrectExact
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture($fgTexture); // Unload foreground texture
UnloadTexture($bgTexture); // Unload background texture
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------