-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathmain.php
More file actions
82 lines (70 loc) · 2.54 KB
/
Copy pathmain.php
File metadata and controls
82 lines (70 loc) · 2.54 KB
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
<?php
// Software framebuffer demo using off-heap memory plus SDL2 rendering.
// Run with:
// elephc -l SDL2 -L /opt/homebrew/lib examples/sdl_framebuffer/main.php
extern "SDL2" {
function SDL_Init(int $flags): int;
function SDL_Quit(): void;
function SDL_CreateWindow(string $title, int $x, int $y, int $w, int $h, int $flags): ptr;
function SDL_DestroyWindow(ptr $window): void;
function SDL_CreateRenderer(ptr $window, int $index, int $flags): ptr;
function SDL_DestroyRenderer(ptr $renderer): void;
function SDL_SetRenderDrawColor(ptr $renderer, int $r, int $g, int $b, int $a): int;
function SDL_RenderClear(ptr $renderer): int;
function SDL_RenderDrawPoint(ptr $renderer, int $x, int $y): int;
function SDL_RenderPresent(ptr $renderer): void;
function SDL_Delay(int $ms): void;
function SDL_GetError(): string;
}
extern "System" {
function malloc(int $size): ptr;
function free(ptr $p): void;
function memset(ptr $dest, int $byte, int $count): ptr;
}
$SDL_INIT_VIDEO = 32;
$SDL_RENDERER_ACCELERATED = 2;
$w = 64;
$h = 64;
$scale = 6;
if (SDL_Init($SDL_INIT_VIDEO) != 0) {
echo "SDL_Init failed: " . SDL_GetError() . "\n";
exit(1);
}
$window = SDL_CreateWindow("framebuffer demo", 100, 100, $w * $scale, $h * $scale, 0);
$renderer = SDL_CreateRenderer($window, -1, $SDL_RENDERER_ACCELERATED);
$buffer = malloc($w * $h);
if (ptr_is_null($window) || ptr_is_null($renderer) || ptr_is_null($buffer)) {
echo "setup failed\n";
if (!ptr_is_null($buffer)) { free($buffer); }
if (!ptr_is_null($renderer)) { SDL_DestroyRenderer($renderer); }
if (!ptr_is_null($window)) { SDL_DestroyWindow($window); }
SDL_Quit();
exit(1);
}
memset($buffer, 0, $w * $h);
for ($y = 0; $y < $h; $y++) {
for ($x = 0; $x < $w; $x++) {
$on = (($x / 8) % 2) === (($y / 8) % 2);
ptr_write8(ptr_offset($buffer, $y * $w + $x), $on ? 255 : 40);
}
}
SDL_SetRenderDrawColor($renderer, 10, 10, 16, 255);
SDL_RenderClear($renderer);
for ($y = 0; $y < $h; $y++) {
for ($x = 0; $x < $w; $x++) {
$shade = ptr_read8(ptr_offset($buffer, $y * $w + $x));
SDL_SetRenderDrawColor($renderer, $shade, 160, 255 - $shade, 255);
for ($sy = 0; $sy < $scale; $sy++) {
for ($sx = 0; $sx < $scale; $sx++) {
SDL_RenderDrawPoint($renderer, $x * $scale + $sx, $y * $scale + $sy);
}
}
}
}
SDL_RenderPresent($renderer);
echo "framebuffer ok\n";
SDL_Delay(1000);
free($buffer);
SDL_DestroyRenderer($renderer);
SDL_DestroyWindow($window);
SDL_Quit();