forked from Ponup/php-sdl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path007-input-handling.php
94 lines (79 loc) · 2.16 KB
/
007-input-handling.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
<?php
declare(strict_types=1);
require 'bootstrap.php';
$quit = false;
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
$joystick = SDL_JoystickOpen(0);
$joystickFound = !is_null($joystick);
if (!$joystickFound) {
trigger_error('A joystick could not be found.', E_USER_NOTICE);
}
$window = SDL_CreateWindow("Input handling example", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
$renderer = SDL_CreateRenderer($window, -1, 0);
$image = SDL_LoadBMP("spaceship.bmp");
if ($image === null) {
exit('Unable to load image');
}
$windowSurface = SDL_GetWindowSurface($window);
$color = SDL_MapRGB($windowSurface->format, 0xff, 0xff, 0xff);
SDL_SetColorKey($image, true, $color);
$texture = SDL_CreateTextureFromSurface($renderer, $image);
$drect = $image->clip_rect;
SDL_FreeSurface($image);
SDL_SetRenderDrawColor($renderer, 0xbb, 0xcc, 0xdd, 0xff);
SDL_RenderClear($renderer);
SDL_RenderPresent($renderer);
$rotCenter = new SDL_Point(10, 10);
$event = new SDL_Event;
$destRect = new SDL_Rect;
$destRect->x = $x = 100;
$destRect->y = $y = 100;
$destRect->w = 64;
$destRect->h = 64;
$update = true;
while (!$quit) {
if ($joystickFound) {
$xJoystickMotion = SDL_JoystickGetAxis($joystick, 0);
if ($xJoystickMotion !== 0) {
$x += ceil($xJoystickMotion / 32767) * 5;
$update = true;
}
$yJoystickMotion = SDL_JoystickGetAxis($joystick, 1);
if ($yJoystickMotion !== 0) {
$y += ceil($yJoystickMotion / 32767) * 5;
$update = true;
}
}
while (SDL_PollEvent($event)) {
switch ($event->type) {
case SDL_QUIT:
$quit = true;
break;
case SDL_MOUSEMOTION:
$x = $event->motion->x;
$y = $event->motion->y;
$update = true;
break;
case SDL_JOYAXISMOTION:
break;
}
}
if ($update) {
SDL_RenderClear($renderer);
$destRect->x = $x;
$destRect->y = $y;
if (SDL_RenderCopyEx($renderer, $texture, NULL, $destRect, 90, $rotCenter, SDL_FLIP_NONE) != 0) {
echo SDL_GetError(), PHP_EOL;
}
SDL_RenderPresent($renderer);
$update = false;
}
SDL_Delay(5);
}
if ($joystickFound) {
SDL_JoystickClose($joystick);
}
SDL_DestroyTexture($texture);
SDL_DestroyRenderer($renderer);
SDL_DestroyWindow($window);
SDL_Quit();