-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtextures_image_text.php
114 lines (93 loc) · 3.43 KB
/
textures_image_text.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
<?php
declare(strict_types=1);
use Nawarian\Raylib\Raylib;
use Nawarian\Raylib\Types\{Color, Vector2};
use function Nawarian\Raylib\{
BeginDrawing,
ClearBackground,
CloseWindow,
DrawText,
DrawTextEx,
DrawTexture,
DrawTextureV,
EndDrawing,
ImageDrawTextEx,
InitWindow,
IsKeyDown,
LoadFontEx,
LoadImage,
LoadTextureFromImage,
SetTargetFPS,
UnloadFont,
UnloadImage,
UnloadTexture,
WindowShouldClose
};
require_once __DIR__ . '/../../vendor/autoload.php';
// Initialization
//--------------------------------------------------------------------------------------
$screenWidth = 800;
$screenHeight = 450;
InitWindow($screenWidth, $screenHeight, 'raylib [texture] example - image text drawing');
$parrots = LoadImage(__DIR__ . '/resources/parrots.png'); // Load image in CPU memory (RAM)
// TTF Font loading with custom generation parameters
$font = LoadFontEx(__DIR__ . '/resources/KAISG.ttf', 64, 0, 0);
// Draw over image using custom font
ImageDrawTextEx(
$parrots,
$font,
'[Parrots font drawing]',
new Vector2(20.0, 20.0),
(float) $font->baseSize,
0.0,
Color::red()
);
$texture = LoadTextureFromImage($parrots); // Image converted to texture, uploaded to GPU memory (VRAM)
//phpcs:ignore Generic.Files.LineLength.TooLong
UnloadImage($parrots); // Once image has been converted to texture and uploaded to VRAM, it can be unloaded from RAM
$position = new Vector2(
(float) ($screenWidth / 2 - $texture->width / 2),
(float) ($screenHeight / 2 - $texture->height / 2 - 20),
);
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) { // Detect window close button or ESC key
// Update
//----------------------------------------------------------------------------------
if (IsKeyDown(Raylib::KEY_SPACE)) {
$showFont = true;
} else {
$showFont = false;
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
//phpcs:disable Generic.WhiteSpace.ScopeIndent.IncorrectExact
BeginDrawing();
ClearBackground(Color::rayWhite());
if (!$showFont) {
// Draw texture with text already drawn inside
DrawTextureV($texture, $position, Color::white());
// Draw text directly using sprite font
DrawTextEx(
$font,
'[Parrots font drawing]',
new Vector2($position->x + 20, $position->y + 20 + 280),
(float) $font->baseSize,
0.0,
Color::white()
);
} else {
DrawTexture($font->texture, (int) ($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); // Texture unloading
UnloadFont($font); // Unload custom spritefont
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------