forked from gabrielrcouto/awesome-php-ffi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.php
64 lines (49 loc) · 1.42 KB
/
run.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
<?php
// To install SDL2 Lib on Mac, use:
// brew install sdl2
const SDL_INIT_VIDEO = 0x00000020;
const SDL_RENDERER_ACCELERATED = 0x00000002;
const SDL_RENDERER_PRESENTVSYNC = 0x00000004;
const SDL_QUIT = 0x100;
const SDL_WINDOW_SHOWN = 0x00000004;
$sdl = FFI::load('./sdl.h');
// Initializes the SDL
if ($sdl->SDL_Init(SDL_INIT_VIDEO) != 0) {
throw new Exception('SDL_Init Error: ' . $sdl->SDL_GetError(), 1);
}
$width = 640;
$height = 480;
// Create the window
$win = $sdl->SDL_CreateWindow('FFI', 0, 0, $width, $height, SDL_WINDOW_SHOWN);
if ($win == null){
throw new Exception('SDL_CreateWindow Error: ' . $sdl->SDL_GetError(), 1);
$sdl->SDL_Quit();
}
// Create the window renderer
$ren = $sdl->SDL_CreateRenderer($win, -1, 0);
// Set the background color to black
$sdl->SDL_SetRenderDrawColor($ren, 0, 0, 0, 255);
$sdl->SDL_RenderClear($ren);
$event = $sdl->new('SDL_Event', false);
$x = 0;
$y = 0;
$isRunning = true;
while ($isRunning) {
// Set the foreground color to green
$sdl->SDL_SetRenderDrawColor($ren, 0, 255, 0, 255);
// Draw a single point
$sdl->SDL_RenderDrawPoint($ren, $x++, $y);
if ($x === $width - 1) {
$x = 0;
$y++;
}
// Render
$sdl->SDL_RenderPresent($ren);
// Verify if the window was closed
if ($sdl->SDL_PollEvent(FFI::addr($event))) {
if ($event->type == SDL_QUIT) {
$isRunning = false;
}
}
}
$sdl->SDL_Quit();