SlideShare a Scribd company logo
VIDEOGIOCHI IN PHP
👾
dal game loop alle collisioni
Manuel Baldassarri | PHPDay GrUSP edition 2022
PHP EXTENSIONS
Mysql - Postgres - Curl - GD - Json - Intl - ...
Memcache - ImageMagick - MongoDB - Redis - ...
PHP AND MULTIMEDIA
Videogiochi in PHP 👾
SDL
Simple DirectMedia Layer
cross-platform: Windows, MacOS, Linux, iOS, Android
audio, keyboard, mouse, game controller
graphics hardware via OpenGL, Direct3D and Vulkan
Videogiochi in PHP 👾
Videogiochi in PHP 👾
TODO
Install deps
Window
Renderer
Game loop
Input
Collision
INSTALL DEPS
pecl install sdl-beta
1
WINDOW
SDL_Init(SDL_INIT_VIDEO);
1
2
$window = SDL_CreateWindow(
3
	 'This is not a window',
4
	 SDL_WINDOWPOS_UNDEFINED, // SDL_WINDOWPOS_CENTERED
5
	 SDL_WINDOWPOS_UNDEFINED, // SDL_WINDOWPOS_CENTERED
6
	 800,
7
	 400,
8
	 0 // SDL_WINDOW_(FULLSCREEN|HIDDEN|RESIZABLE)
9
	 // SDL_WINDOW_(OPENGL|VULKAN|METAL)
10
);
11
12
sleep(10);
13
14
SDL_DestroyWindow($window);
15
SDL_Quit();
16
$window = SDL_CreateWindow(
	 'This is not a window',
	 SDL_WINDOWPOS_UNDEFINED, // SDL_WINDOWPOS_CENTERED
	 SDL_WINDOWPOS_UNDEFINED, // SDL_WINDOWPOS_CENTERED
	 800,
	 400,
	 0 // SDL_WINDOW_(FULLSCREEN|HIDDEN|RESIZABLE)
	 // SDL_WINDOW_(OPENGL|VULKAN|METAL)
);
SDL_Init(SDL_INIT_VIDEO);
1
2
3
4
5
6
7
8
9
10
11
12
sleep(10);
13
14
SDL_DestroyWindow($window);
15
SDL_Quit();
16
sleep(10);
SDL_DestroyWindow($window);
SDL_Quit();
SDL_Init(SDL_INIT_VIDEO);
1
2
$window = SDL_CreateWindow(
3
	 'This is not a window',
4
	 SDL_WINDOWPOS_UNDEFINED, // SDL_WINDOWPOS_CENTERED
5
	 SDL_WINDOWPOS_UNDEFINED, // SDL_WINDOWPOS_CENTERED
6
	 800,
7
	 400,
8
	 0 // SDL_WINDOW_(FULLSCREEN|HIDDEN|RESIZABLE)
9
	 // SDL_WINDOW_(OPENGL|VULKAN|METAL)
10
);
11
12
13
14
15
16
$ php 00-window.php
1
RENDERER
$renderer = SDL_CreateRenderer($window, 0, 0);
SDL_Init(SDL_INIT_VIDEO);
1
$window = SDL_CreateWindow(...);
2
3
4
$orange = [0xd7, 0x6b, 0x2b, 0];
5
SDL_SetRenderDrawColor($renderer, ...$orange);
6
SDL_RenderClear($renderer);
7
SDL_SetRenderDrawColor($renderer, 230, 230, 230, 255);
8
SDL_RenderDrawRect($renderer, new SDL_Rect(100, 100, 100, 200
9
SDL_RenderDrawRect($renderer, new SDL_Rect(110, 110, 80, 180)
10
SDL_RenderDrawRect($renderer, new SDL_Rect(120, 120, 60, 160)
11
SDL_RenderPresent($renderer);
12
13
SDL_Delay(10000);
14
15
SDL_DestroyRenderer($renderer);
16
i d $ i d
1
$orange = [0xd7, 0x6b, 0x2b, 0];
SDL_SetRenderDrawColor($renderer, ...$orange);
SDL_Init(SDL_INIT_VIDEO);
1
$window = SDL_CreateWindow(...);
2
3
$renderer = SDL_CreateRenderer($window, 0, 0);
4
5
6
SDL_RenderClear($renderer);
7
SDL_SetRenderDrawColor($renderer, 230, 230, 230, 255);
8
SDL_RenderDrawRect($renderer, new SDL_Rect(100, 100, 100, 200
9
SDL_RenderDrawRect($renderer, new SDL_Rect(110, 110, 80, 180)
10
SDL_RenderDrawRect($renderer, new SDL_Rect(120, 120, 60, 160)
11
SDL_RenderPresent($renderer);
12
13
SDL_Delay(10000);
14
15
SDL_DestroyRenderer($renderer);
16
i d $ i d
1
SDL_RenderClear($renderer);
SDL_Init(SDL_INIT_VIDEO);
1
$window = SDL_CreateWindow(...);
2
3
$renderer = SDL_CreateRenderer($window, 0, 0);
4
$orange = [0xd7, 0x6b, 0x2b, 0];
5
SDL_SetRenderDrawColor($renderer, ...$orange);
6
7
SDL_SetRenderDrawColor($renderer, 230, 230, 230, 255);
8
SDL_RenderDrawRect($renderer, new SDL_Rect(100, 100, 100, 200
9
SDL_RenderDrawRect($renderer, new SDL_Rect(110, 110, 80, 180)
10
SDL_RenderDrawRect($renderer, new SDL_Rect(120, 120, 60, 160)
11
SDL_RenderPresent($renderer);
12
13
SDL_Delay(10000);
14
15
SDL_DestroyRenderer($renderer);
16
i d $ i d
1
SDL_SetRenderDrawColor($renderer, 230, 230, 230, 255);
SDL_Init(SDL_INIT_VIDEO);
1
$window = SDL_CreateWindow(...);
2
3
$renderer = SDL_CreateRenderer($window, 0, 0);
4
$orange = [0xd7, 0x6b, 0x2b, 0];
5
SDL_SetRenderDrawColor($renderer, ...$orange);
6
SDL_RenderClear($renderer);
7
8
SDL_RenderDrawRect($renderer, new SDL_Rect(100, 100, 100, 200
9
SDL_RenderDrawRect($renderer, new SDL_Rect(110, 110, 80, 180)
10
SDL_RenderDrawRect($renderer, new SDL_Rect(120, 120, 60, 160)
11
SDL_RenderPresent($renderer);
12
13
SDL_Delay(10000);
14
15
SDL_DestroyRenderer($renderer);
16
i d $ i d
1
SDL_RenderDrawRect($renderer, new SDL_Rect(100, 100, 100, 200
SDL_RenderDrawRect($renderer, new SDL_Rect(110, 110, 80, 180)
SDL_RenderDrawRect($renderer, new SDL_Rect(120, 120, 60, 160)
SDL_Init(SDL_INIT_VIDEO);
1
$window = SDL_CreateWindow(...);
2
3
$renderer = SDL_CreateRenderer($window, 0, 0);
4
$orange = [0xd7, 0x6b, 0x2b, 0];
5
SDL_SetRenderDrawColor($renderer, ...$orange);
6
SDL_RenderClear($renderer);
7
SDL_SetRenderDrawColor($renderer, 230, 230, 230, 255);
8
9
10
11
SDL_RenderPresent($renderer);
12
13
SDL_Delay(10000);
14
15
SDL_DestroyRenderer($renderer);
16
i d $ i d
1
SDL_RenderPresent($renderer);
SDL_Init(SDL_INIT_VIDEO);
1
$window = SDL_CreateWindow(...);
2
3
$renderer = SDL_CreateRenderer($window, 0, 0);
4
$orange = [0xd7, 0x6b, 0x2b, 0];
5
SDL_SetRenderDrawColor($renderer, ...$orange);
6
SDL_RenderClear($renderer);
7
SDL_SetRenderDrawColor($renderer, 230, 230, 230, 255);
8
SDL_RenderDrawRect($renderer, new SDL_Rect(100, 100, 100, 200
9
SDL_RenderDrawRect($renderer, new SDL_Rect(110, 110, 80, 180)
10
SDL_RenderDrawRect($renderer, new SDL_Rect(120, 120, 60, 160)
11
12
13
SDL_Delay(10000);
14
15
SDL_DestroyRenderer($renderer);
16
i d $ i d
1
SDL_Delay(10000);
SDL_Init(SDL_INIT_VIDEO);
1
$window = SDL_CreateWindow(...);
2
3
$renderer = SDL_CreateRenderer($window, 0, 0);
4
$orange = [0xd7, 0x6b, 0x2b, 0];
5
SDL_SetRenderDrawColor($renderer, ...$orange);
6
SDL_RenderClear($renderer);
7
SDL_SetRenderDrawColor($renderer, 230, 230, 230, 255);
8
SDL_RenderDrawRect($renderer, new SDL_Rect(100, 100, 100, 200
9
SDL_RenderDrawRect($renderer, new SDL_Rect(110, 110, 80, 180)
10
SDL_RenderDrawRect($renderer, new SDL_Rect(120, 120, 60, 160)
11
SDL_RenderPresent($renderer);
12
13
14
15
SDL_DestroyRenderer($renderer);
16
i d $ i d
1
Videogiochi in PHP 👾
DRAW FUNCTIONS
SDL_RenderDrawPoint
SDL_RenderDrawLine
SDL_RenderDrawRect
SDL_RenderFillRect
SDL_RenderCopy
SDL_RenderCopyEx
SDL_RenderDrawPointF
SDL_RenderDrawLineF
SDL_RenderDrawRectF
SDL_RenderFillRectF
SDL_RenderCopyF
SDL_RenderCopyExF
SDL_RECT
class SDL_Rect {
	 public int $x;
	 public int $y;
	 public int $w;
	 public int $h;
}
1
2
3
4
5
6
SDL_FRECT
class SDL_FRect {
	 public float $x;
	 public float $y;
	 public float $w;
	 public float $h;
}
1
2
3
4
5
6
TEXTURE
$surface = SDL_LoadBMP(__DIR__."/spaceship.bmp");
$texture = SDL_CreateTextureFromSurface($renderer, $surface);
1
2
3
SDL_IMAGE EXTENSION
pecl install sdl_image-devel
1
$texture = IMG_LoadTexture($renderer, $imageFilename);
1
PNG, JPG, BMP, TGA, TIF, ...
SDL_RENDERCOPY
SDL_RenderCopy(
	 $renderer,
	 $texture,
	 SDL_Rect $sourceRect,
	 SDL_Rect $destinationRect
)
1
2
3
4
5
6
SPRITE
$texture = IMG_LoadTexture($renderer, __DIR__."/barrel.png");
1
2
$sourceRect = new SDL_Rect(0, 0, 40, 40);
3
$destRect = new SDL_Rect(400, 200, 40, 40);
4
5
SDL_RenderCopy($renderer, $texture, $sourceRect, $destRect);
6
$sourceRect = new SDL_Rect(0, 0, 40, 40);
$destRect = new SDL_Rect(400, 200, 40, 40);
$texture = IMG_LoadTexture($renderer, __DIR__."/barrel.png");
1
2
3
4
5
SDL_RenderCopy($renderer, $texture, $sourceRect, $destRect);
6 SDL_RenderCopy($renderer, $texture, $sourceRect, $destRect);
$texture = IMG_LoadTexture($renderer, __DIR__."/barrel.png");
1
2
$sourceRect = new SDL_Rect(0, 0, 40, 40);
3
$destRect = new SDL_Rect(400, 200, 40, 40);
4
5
6
Videogiochi in PHP 👾
SPRITE
SDL_RenderCopyEx(
	 $renderer,
	 $texture,
	 SDL_Rect $sourceRect,
	 SDL_Rect$destRect,
	 float $rotationAngle,
	 SDL_Point $rotationCenter,
	 int $flip
	 // SDL_FLIP_NONE | SDL_FLIP_HORIZONTAL | SDL_FLIP_VERTICAL
)
1
2
3
4
5
6
7
8
9
10
LEVEL
+
$level = [
	 "XXXXX XXXXXXXX XXXXX",
	 " ",
	 " ",
	 " ",
	 " ",
	 " XXXXXXX XXXXXXX ",
	 " ",
	 " ",
	 " ",
	 " XXXXXXXXXXXXXXXXXXXXXX ",
	 " ",
	 " ",
	 " ",
	 "XXXXXXXXX XXXXXXXXX",
	 " ",
" "
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Videogiochi in PHP 👾
Videogiochi in PHP 👾
GAME LOOP
/* Init window/renderer/load images */
do {
	 // a lot of stuff
} while (!$quit);
/* Clean the kitchen */
1
2
3
4
5
6
7
GAME LOOP
/* Init window/renderer/load images */
$event = new SDL_Event();
do {
	 SDL_PollEvent($event);
	 $inputStatus = checkInput();
	 $quit = updateGame($inputStatus, $event);
	 render();
} while (!$quit);
/* Clean the kitchen */
1
2
3
4
5
6
7
8
9
10
11
UPDATE
class Player {
	 private int SPEED = 1; // pixels per loop
	 private int $x;
	 public function update() {
	 	 $this->x += self::SPEED;
	 }
	 public function render($renderer) {
	 	 $renderer->drawItPlease($playerTexture, $this->x);
	 }
}
1
2
3
4
5
6
7
8
9
10
11
12
UPDATE
class Player {
	 private int SPEED = 30; // pixels per second
	 private int $x;
	 public function update(float $deltaTime) {
	 	 $this->x += self::SPEED * $deltaTime;
	 }
	 public function render($renderer) {
	 	 $renderer->drawItPlease($playerTexture, $this->x);
	 }
}
1
2
3
4
5
6
7
8
9
10
11
12
	 private int $x;
class Player {
1
	 private int SPEED = 30; // pixels per second
2
3
4
	 public function update(float $deltaTime) {
5
	 	 $this->x += self::SPEED * $deltaTime;
6
	 }
7
8
	 public function render($renderer) {
9
	 	 $renderer->drawItPlease($playerTexture, $this->x);
10
	 }
11
}
12
UPDATE
	 private float $x;
class Player {
1
	 private int SPEED = 30; // pixels per second
2
3
4
	 public function update(float $deltaTime) {
5
	 	 $this->x += self::SPEED * $deltaTime;
6
	 }
7
8
	 public function render($renderer) {
9
	 	 $renderer->drawItPlease($playerTexture, (int)$this->x);
10
	 }
11
}
12
COLLISIONS WITH AXIS-ALIGNED BOUNDING BOX
COLLISIONS WITH AABB
COLLISIONS WITH AABB
COLLISIONS WITH AABB
$rect1 = SDL_Rect($x1, $y1, $w1, $h1);
$rect2 = SDL_Rect($x2, $y2, $w2, $h3);
1
2
COLLISIONS WITH AABB
SDL_HasIntersection(SDL_Rect $rect1, SDL_Rect $rect2): bool
1
COLLISIONS WITH AABB
SDL_HasIntersection(SDL_Rect $rect1, SDL_Rect $rect2): bool
1
COLLISIONS WITH AABB
$rect1->HasIntersection($rect2);
1
COLLISIONS
COLLISIONS
5px
20px
SDL_IntersectRect(
	 SDL_Rect $rect1, SDL_Rect $rect2, SDL_Rect &$intersection
)
1
2
3
JOYSTICK
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
$joystick = SDL_JoystickOpen(0);
if ($joystick !== null) {
	 // Joystick could not be found
}
$xJoystickMotion = SDL_JoystickGetAxis($joystick, 0);
$yJoystickMotion = SDL_JoystickGetAxis($joystick, 1);
// SDL_JoystickGetAxis($j, $xOrY): int (-32768 to 32767)
1
2
3
4
5
6
7
8
9
10
11
12
KEYBOARD
$numKeys = 0;
$keys = SDL_GetKeyboardState($numKeys, false);
if (in_array(SDL_SCANCODE_LEFT, $keys)) { // left arrow presse
	 $goToLeft = true;
}
if (in_array(SDL_SCANCODE_Q, $keys)) { // q or Q pressed
	 $quit = true;
}
1
2
3
4
5
6
7
8
9
SDL_MIXER EXTENSION
~60 functions for playing music and sfx
pecl install sdl_mixer-devel
1
STATE OF THE ART (KPI*)
PHP: 20
* Kea Perception Index
Python: 500K
C/C++/C#: 100M
GRAZIE!
Manuel Baldassarri

Twitter @k3a

Github @kea









https://wireframe.raspberrypi.org/books/code-the-classics1
https://github.com/kea/code-the-classics1-php
https://www.libsdl.org/
https://github.com/easysoft/phpmicro

More Related Content

PDF
Game Development with SDL and Perl
 
PDF
De CRUD Ă  DDD pas Ă  pas
PDF
Game Development using SDL and the PDK
 
PDF
Building Glassware with the Glass Development Kit
PDF
The Ring programming language version 1.7 book - Part 116 of 196
DOCX
Database Implementation Final Document
PDF
Html5 game, websocket e arduino
PDF
Html5 game, websocket e arduino
Game Development with SDL and Perl
 
De CRUD Ă  DDD pas Ă  pas
Game Development using SDL and the PDK
 
Building Glassware with the Glass Development Kit
The Ring programming language version 1.7 book - Part 116 of 196
Database Implementation Final Document
Html5 game, websocket e arduino
Html5 game, websocket e arduino

What's hot (20)

DOCX
Nullcon HackIM 2012 Solutions
PDF
Writing Mirror API and Native Apps for Google Glass
PDF
R57shell
 
TXT
3D dualcopter
PDF
The Ring programming language version 1.10 book - Part 70 of 212
PDF
DAHO.AM 2015 - Abusing phones to make the internet of things
PDF
MANUAL DE FUNCIONAMIENTO APAFA
PDF
building_games_with_ruby_rubyconf
PPT
J2 me 07_5
KEY
Ojagnaha vol7
PDF
The Ring programming language version 1.6 book - Part 61 of 189
PDF
The Ring programming language version 1.5.4 book - Part 55 of 185
PDF
Gdg san diego android 11 meetups what's new in android - ui and dev tools
KEY
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
KEY
Keyboard Access APIs
TXT
Daily notes
PDF
Informe Final Red de Area Comunal
PDF
Node Access in Drupal 7 (and Drupal 8)
DOCX
Calculadora
PDF
The Ring programming language version 1.9 book - Part 129 of 210
Nullcon HackIM 2012 Solutions
Writing Mirror API and Native Apps for Google Glass
R57shell
 
3D dualcopter
The Ring programming language version 1.10 book - Part 70 of 212
DAHO.AM 2015 - Abusing phones to make the internet of things
MANUAL DE FUNCIONAMIENTO APAFA
building_games_with_ruby_rubyconf
J2 me 07_5
Ojagnaha vol7
The Ring programming language version 1.6 book - Part 61 of 189
The Ring programming language version 1.5.4 book - Part 55 of 185
Gdg san diego android 11 meetups what's new in android - ui and dev tools
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
Keyboard Access APIs
Daily notes
Informe Final Red de Area Comunal
Node Access in Drupal 7 (and Drupal 8)
Calculadora
The Ring programming language version 1.9 book - Part 129 of 210
Ad

Similar to Videogiochi in PHP 👾 (20)

PDF
The Ring programming language version 1.2 book - Part 63 of 84
ODP
SDL2 Game Development VT Code Camp 2013
PDF
The Ring programming language version 1.10 book - Part 131 of 212
PDF
The Ring programming language version 1.8 book - Part 122 of 202
PDF
The Ring programming language version 1.8 book - Part 53 of 202
PDF
The Ring programming language version 1.6 book - Part 50 of 189
PDF
The Ring programming language version 1.5.1 book - Part 83 of 180
PDF
The Ring programming language version 1.6 book - Part 90 of 189
PPTX
C game programming - SDL
PDF
The Ring programming language version 1.5.2 book - Part 47 of 181
PDF
The Ring programming language version 1.4.1 book - Part 14 of 31
PDF
The Ring programming language version 1.5.2 book - Part 84 of 181
PDF
The Ring programming language version 1.5.4 book - Part 87 of 185
PDF
The Ring programming language version 1.7 book - Part 117 of 196
PDF
The Ring programming language version 1.9 book - Part 130 of 210
PDF
The Ring programming language version 1.2 book - Part 64 of 84
PDF
The Ring programming language version 1.10 book - Part 132 of 212
PDF
The Ring programming language version 1.6 book - Part 91 of 189
PDF
Game Programming I - GD4N
PDF
The Ring programming language version 1.5.1 book - Part 46 of 180
The Ring programming language version 1.2 book - Part 63 of 84
SDL2 Game Development VT Code Camp 2013
The Ring programming language version 1.10 book - Part 131 of 212
The Ring programming language version 1.8 book - Part 122 of 202
The Ring programming language version 1.8 book - Part 53 of 202
The Ring programming language version 1.6 book - Part 50 of 189
The Ring programming language version 1.5.1 book - Part 83 of 180
The Ring programming language version 1.6 book - Part 90 of 189
C game programming - SDL
The Ring programming language version 1.5.2 book - Part 47 of 181
The Ring programming language version 1.4.1 book - Part 14 of 31
The Ring programming language version 1.5.2 book - Part 84 of 181
The Ring programming language version 1.5.4 book - Part 87 of 185
The Ring programming language version 1.7 book - Part 117 of 196
The Ring programming language version 1.9 book - Part 130 of 210
The Ring programming language version 1.2 book - Part 64 of 84
The Ring programming language version 1.10 book - Part 132 of 212
The Ring programming language version 1.6 book - Part 91 of 189
Game Programming I - GD4N
The Ring programming language version 1.5.1 book - Part 46 of 180
Ad

More from Manuel Baldassarri (8)

PDF
Swoole Overview
PDF
From * to Symfony2
PDF
Un CMS in 25min con Symfony CMF
PDF
Automazione quotidiana in php
KEY
Symfony2 security layer
KEY
Symfony CMF: un nuovo paradigma per la gestione dei contenuti
KEY
Ant vs Phing
ODP
Form refactoring
Swoole Overview
From * to Symfony2
Un CMS in 25min con Symfony CMF
Automazione quotidiana in php
Symfony2 security layer
Symfony CMF: un nuovo paradigma per la gestione dei contenuti
Ant vs Phing
Form refactoring

Recently uploaded (20)

PDF
Empathic Computing: Creating Shared Understanding
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Encapsulation theory and applications.pdf
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Programs and apps: productivity, graphics, security and other tools
PPT
Teaching material agriculture food technology
PDF
cuic standard and advanced reporting.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
 
PPTX
Big Data Technologies - Introduction.pptx
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Spectroscopy.pptx food analysis technology
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
Empathic Computing: Creating Shared Understanding
A comparative analysis of optical character recognition models for extracting...
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Digital-Transformation-Roadmap-for-Companies.pptx
Encapsulation_ Review paper, used for researhc scholars
Per capita expenditure prediction using model stacking based on satellite ima...
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
sap open course for s4hana steps from ECC to s4
Encapsulation theory and applications.pdf
Assigned Numbers - 2025 - Bluetooth® Document
Diabetes mellitus diagnosis method based random forest with bat algorithm
Programs and apps: productivity, graphics, security and other tools
Teaching material agriculture food technology
cuic standard and advanced reporting.pdf
The AUB Centre for AI in Media Proposal.docx
 
Big Data Technologies - Introduction.pptx
20250228 LYD VKU AI Blended-Learning.pptx
Review of recent advances in non-invasive hemoglobin estimation
Spectroscopy.pptx food analysis technology
“AI and Expert System Decision Support & Business Intelligence Systems”

Videogiochi in PHP 👾

  • 1. VIDEOGIOCHI IN PHP 👾 dal game loop alle collisioni Manuel Baldassarri | PHPDay GrUSP edition 2022
  • 2. PHP EXTENSIONS Mysql - Postgres - Curl - GD - Json - Intl - ... Memcache - ImageMagick - MongoDB - Redis - ...
  • 5. SDL Simple DirectMedia Layer cross-platform: Windows, MacOS, Linux, iOS, Android audio, keyboard, mouse, game controller graphics hardware via OpenGL, Direct3D and Vulkan
  • 10. WINDOW SDL_Init(SDL_INIT_VIDEO); 1 2 $window = SDL_CreateWindow( 3 'This is not a window', 4 SDL_WINDOWPOS_UNDEFINED, // SDL_WINDOWPOS_CENTERED 5 SDL_WINDOWPOS_UNDEFINED, // SDL_WINDOWPOS_CENTERED 6 800, 7 400, 8 0 // SDL_WINDOW_(FULLSCREEN|HIDDEN|RESIZABLE) 9 // SDL_WINDOW_(OPENGL|VULKAN|METAL) 10 ); 11 12 sleep(10); 13 14 SDL_DestroyWindow($window); 15 SDL_Quit(); 16 $window = SDL_CreateWindow( 'This is not a window', SDL_WINDOWPOS_UNDEFINED, // SDL_WINDOWPOS_CENTERED SDL_WINDOWPOS_UNDEFINED, // SDL_WINDOWPOS_CENTERED 800, 400, 0 // SDL_WINDOW_(FULLSCREEN|HIDDEN|RESIZABLE) // SDL_WINDOW_(OPENGL|VULKAN|METAL) ); SDL_Init(SDL_INIT_VIDEO); 1 2 3 4 5 6 7 8 9 10 11 12 sleep(10); 13 14 SDL_DestroyWindow($window); 15 SDL_Quit(); 16 sleep(10); SDL_DestroyWindow($window); SDL_Quit(); SDL_Init(SDL_INIT_VIDEO); 1 2 $window = SDL_CreateWindow( 3 'This is not a window', 4 SDL_WINDOWPOS_UNDEFINED, // SDL_WINDOWPOS_CENTERED 5 SDL_WINDOWPOS_UNDEFINED, // SDL_WINDOWPOS_CENTERED 6 800, 7 400, 8 0 // SDL_WINDOW_(FULLSCREEN|HIDDEN|RESIZABLE) 9 // SDL_WINDOW_(OPENGL|VULKAN|METAL) 10 ); 11 12 13 14 15 16
  • 12. RENDERER $renderer = SDL_CreateRenderer($window, 0, 0); SDL_Init(SDL_INIT_VIDEO); 1 $window = SDL_CreateWindow(...); 2 3 4 $orange = [0xd7, 0x6b, 0x2b, 0]; 5 SDL_SetRenderDrawColor($renderer, ...$orange); 6 SDL_RenderClear($renderer); 7 SDL_SetRenderDrawColor($renderer, 230, 230, 230, 255); 8 SDL_RenderDrawRect($renderer, new SDL_Rect(100, 100, 100, 200 9 SDL_RenderDrawRect($renderer, new SDL_Rect(110, 110, 80, 180) 10 SDL_RenderDrawRect($renderer, new SDL_Rect(120, 120, 60, 160) 11 SDL_RenderPresent($renderer); 12 13 SDL_Delay(10000); 14 15 SDL_DestroyRenderer($renderer); 16 i d $ i d 1 $orange = [0xd7, 0x6b, 0x2b, 0]; SDL_SetRenderDrawColor($renderer, ...$orange); SDL_Init(SDL_INIT_VIDEO); 1 $window = SDL_CreateWindow(...); 2 3 $renderer = SDL_CreateRenderer($window, 0, 0); 4 5 6 SDL_RenderClear($renderer); 7 SDL_SetRenderDrawColor($renderer, 230, 230, 230, 255); 8 SDL_RenderDrawRect($renderer, new SDL_Rect(100, 100, 100, 200 9 SDL_RenderDrawRect($renderer, new SDL_Rect(110, 110, 80, 180) 10 SDL_RenderDrawRect($renderer, new SDL_Rect(120, 120, 60, 160) 11 SDL_RenderPresent($renderer); 12 13 SDL_Delay(10000); 14 15 SDL_DestroyRenderer($renderer); 16 i d $ i d 1 SDL_RenderClear($renderer); SDL_Init(SDL_INIT_VIDEO); 1 $window = SDL_CreateWindow(...); 2 3 $renderer = SDL_CreateRenderer($window, 0, 0); 4 $orange = [0xd7, 0x6b, 0x2b, 0]; 5 SDL_SetRenderDrawColor($renderer, ...$orange); 6 7 SDL_SetRenderDrawColor($renderer, 230, 230, 230, 255); 8 SDL_RenderDrawRect($renderer, new SDL_Rect(100, 100, 100, 200 9 SDL_RenderDrawRect($renderer, new SDL_Rect(110, 110, 80, 180) 10 SDL_RenderDrawRect($renderer, new SDL_Rect(120, 120, 60, 160) 11 SDL_RenderPresent($renderer); 12 13 SDL_Delay(10000); 14 15 SDL_DestroyRenderer($renderer); 16 i d $ i d 1 SDL_SetRenderDrawColor($renderer, 230, 230, 230, 255); SDL_Init(SDL_INIT_VIDEO); 1 $window = SDL_CreateWindow(...); 2 3 $renderer = SDL_CreateRenderer($window, 0, 0); 4 $orange = [0xd7, 0x6b, 0x2b, 0]; 5 SDL_SetRenderDrawColor($renderer, ...$orange); 6 SDL_RenderClear($renderer); 7 8 SDL_RenderDrawRect($renderer, new SDL_Rect(100, 100, 100, 200 9 SDL_RenderDrawRect($renderer, new SDL_Rect(110, 110, 80, 180) 10 SDL_RenderDrawRect($renderer, new SDL_Rect(120, 120, 60, 160) 11 SDL_RenderPresent($renderer); 12 13 SDL_Delay(10000); 14 15 SDL_DestroyRenderer($renderer); 16 i d $ i d 1 SDL_RenderDrawRect($renderer, new SDL_Rect(100, 100, 100, 200 SDL_RenderDrawRect($renderer, new SDL_Rect(110, 110, 80, 180) SDL_RenderDrawRect($renderer, new SDL_Rect(120, 120, 60, 160) SDL_Init(SDL_INIT_VIDEO); 1 $window = SDL_CreateWindow(...); 2 3 $renderer = SDL_CreateRenderer($window, 0, 0); 4 $orange = [0xd7, 0x6b, 0x2b, 0]; 5 SDL_SetRenderDrawColor($renderer, ...$orange); 6 SDL_RenderClear($renderer); 7 SDL_SetRenderDrawColor($renderer, 230, 230, 230, 255); 8 9 10 11 SDL_RenderPresent($renderer); 12 13 SDL_Delay(10000); 14 15 SDL_DestroyRenderer($renderer); 16 i d $ i d 1 SDL_RenderPresent($renderer); SDL_Init(SDL_INIT_VIDEO); 1 $window = SDL_CreateWindow(...); 2 3 $renderer = SDL_CreateRenderer($window, 0, 0); 4 $orange = [0xd7, 0x6b, 0x2b, 0]; 5 SDL_SetRenderDrawColor($renderer, ...$orange); 6 SDL_RenderClear($renderer); 7 SDL_SetRenderDrawColor($renderer, 230, 230, 230, 255); 8 SDL_RenderDrawRect($renderer, new SDL_Rect(100, 100, 100, 200 9 SDL_RenderDrawRect($renderer, new SDL_Rect(110, 110, 80, 180) 10 SDL_RenderDrawRect($renderer, new SDL_Rect(120, 120, 60, 160) 11 12 13 SDL_Delay(10000); 14 15 SDL_DestroyRenderer($renderer); 16 i d $ i d 1 SDL_Delay(10000); SDL_Init(SDL_INIT_VIDEO); 1 $window = SDL_CreateWindow(...); 2 3 $renderer = SDL_CreateRenderer($window, 0, 0); 4 $orange = [0xd7, 0x6b, 0x2b, 0]; 5 SDL_SetRenderDrawColor($renderer, ...$orange); 6 SDL_RenderClear($renderer); 7 SDL_SetRenderDrawColor($renderer, 230, 230, 230, 255); 8 SDL_RenderDrawRect($renderer, new SDL_Rect(100, 100, 100, 200 9 SDL_RenderDrawRect($renderer, new SDL_Rect(110, 110, 80, 180) 10 SDL_RenderDrawRect($renderer, new SDL_Rect(120, 120, 60, 160) 11 SDL_RenderPresent($renderer); 12 13 14 15 SDL_DestroyRenderer($renderer); 16 i d $ i d 1
  • 15. SDL_RECT class SDL_Rect { public int $x; public int $y; public int $w; public int $h; } 1 2 3 4 5 6
  • 16. SDL_FRECT class SDL_FRect { public float $x; public float $y; public float $w; public float $h; } 1 2 3 4 5 6
  • 17. TEXTURE $surface = SDL_LoadBMP(__DIR__."/spaceship.bmp"); $texture = SDL_CreateTextureFromSurface($renderer, $surface); 1 2 3
  • 18. SDL_IMAGE EXTENSION pecl install sdl_image-devel 1 $texture = IMG_LoadTexture($renderer, $imageFilename); 1 PNG, JPG, BMP, TGA, TIF, ...
  • 19. SDL_RENDERCOPY SDL_RenderCopy( $renderer, $texture, SDL_Rect $sourceRect, SDL_Rect $destinationRect ) 1 2 3 4 5 6
  • 20. SPRITE $texture = IMG_LoadTexture($renderer, __DIR__."/barrel.png"); 1 2 $sourceRect = new SDL_Rect(0, 0, 40, 40); 3 $destRect = new SDL_Rect(400, 200, 40, 40); 4 5 SDL_RenderCopy($renderer, $texture, $sourceRect, $destRect); 6 $sourceRect = new SDL_Rect(0, 0, 40, 40); $destRect = new SDL_Rect(400, 200, 40, 40); $texture = IMG_LoadTexture($renderer, __DIR__."/barrel.png"); 1 2 3 4 5 SDL_RenderCopy($renderer, $texture, $sourceRect, $destRect); 6 SDL_RenderCopy($renderer, $texture, $sourceRect, $destRect); $texture = IMG_LoadTexture($renderer, __DIR__."/barrel.png"); 1 2 $sourceRect = new SDL_Rect(0, 0, 40, 40); 3 $destRect = new SDL_Rect(400, 200, 40, 40); 4 5 6
  • 22. SPRITE SDL_RenderCopyEx( $renderer, $texture, SDL_Rect $sourceRect, SDL_Rect$destRect, float $rotationAngle, SDL_Point $rotationCenter, int $flip // SDL_FLIP_NONE | SDL_FLIP_HORIZONTAL | SDL_FLIP_VERTICAL ) 1 2 3 4 5 6 7 8 9 10
  • 23. LEVEL + $level = [ "XXXXX XXXXXXXX XXXXX", " ", " ", " ", " ", " XXXXXXX XXXXXXX ", " ", " ", " ", " XXXXXXXXXXXXXXXXXXXXXX ", " ", " ", " ", "XXXXXXXXX XXXXXXXXX", " ", " " 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
  • 26. GAME LOOP /* Init window/renderer/load images */ do { // a lot of stuff } while (!$quit); /* Clean the kitchen */ 1 2 3 4 5 6 7
  • 27. GAME LOOP /* Init window/renderer/load images */ $event = new SDL_Event(); do { SDL_PollEvent($event); $inputStatus = checkInput(); $quit = updateGame($inputStatus, $event); render(); } while (!$quit); /* Clean the kitchen */ 1 2 3 4 5 6 7 8 9 10 11
  • 28. UPDATE class Player { private int SPEED = 1; // pixels per loop private int $x; public function update() { $this->x += self::SPEED; } public function render($renderer) { $renderer->drawItPlease($playerTexture, $this->x); } } 1 2 3 4 5 6 7 8 9 10 11 12
  • 29. UPDATE class Player { private int SPEED = 30; // pixels per second private int $x; public function update(float $deltaTime) { $this->x += self::SPEED * $deltaTime; } public function render($renderer) { $renderer->drawItPlease($playerTexture, $this->x); } } 1 2 3 4 5 6 7 8 9 10 11 12 private int $x; class Player { 1 private int SPEED = 30; // pixels per second 2 3 4 public function update(float $deltaTime) { 5 $this->x += self::SPEED * $deltaTime; 6 } 7 8 public function render($renderer) { 9 $renderer->drawItPlease($playerTexture, $this->x); 10 } 11 } 12
  • 30. UPDATE private float $x; class Player { 1 private int SPEED = 30; // pixels per second 2 3 4 public function update(float $deltaTime) { 5 $this->x += self::SPEED * $deltaTime; 6 } 7 8 public function render($renderer) { 9 $renderer->drawItPlease($playerTexture, (int)$this->x); 10 } 11 } 12
  • 34. COLLISIONS WITH AABB $rect1 = SDL_Rect($x1, $y1, $w1, $h1); $rect2 = SDL_Rect($x2, $y2, $w2, $h3); 1 2
  • 35. COLLISIONS WITH AABB SDL_HasIntersection(SDL_Rect $rect1, SDL_Rect $rect2): bool 1
  • 36. COLLISIONS WITH AABB SDL_HasIntersection(SDL_Rect $rect1, SDL_Rect $rect2): bool 1
  • 39. COLLISIONS 5px 20px SDL_IntersectRect( SDL_Rect $rect1, SDL_Rect $rect2, SDL_Rect &$intersection ) 1 2 3
  • 40. JOYSTICK SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK); $joystick = SDL_JoystickOpen(0); if ($joystick !== null) { // Joystick could not be found } $xJoystickMotion = SDL_JoystickGetAxis($joystick, 0); $yJoystickMotion = SDL_JoystickGetAxis($joystick, 1); // SDL_JoystickGetAxis($j, $xOrY): int (-32768 to 32767) 1 2 3 4 5 6 7 8 9 10 11 12
  • 41. KEYBOARD $numKeys = 0; $keys = SDL_GetKeyboardState($numKeys, false); if (in_array(SDL_SCANCODE_LEFT, $keys)) { // left arrow presse $goToLeft = true; } if (in_array(SDL_SCANCODE_Q, $keys)) { // q or Q pressed $quit = true; } 1 2 3 4 5 6 7 8 9
  • 42. SDL_MIXER EXTENSION ~60 functions for playing music and sfx pecl install sdl_mixer-devel 1
  • 43. STATE OF THE ART (KPI*) PHP: 20 * Kea Perception Index Python: 500K C/C++/C#: 100M
  • 44. GRAZIE! Manuel Baldassarri Twitter @k3a Github @kea https://wireframe.raspberrypi.org/books/code-the-classics1 https://github.com/kea/code-the-classics1-php https://www.libsdl.org/ https://github.com/easysoft/phpmicro