Skip to content

Commit

Permalink
Added gamepad demo
Browse files Browse the repository at this point in the history
  • Loading branch information
joseph-montanez committed Mar 25, 2020
1 parent 3e96604 commit 51b780b
Show file tree
Hide file tree
Showing 13 changed files with 914 additions and 195 deletions.
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,13 @@ BSD-like license that allows static linking with closed source software. Check [

*Copyright (c) 2017 Joseph Montanez ([@joseph-montanez](https://twitter.com/shabb_jm))*

## Binary Releases

[Windows PHP Extension DLL](https://github.com/joseph-montanez/raylib-php/releases)

## How To Build PHP Extension

Windows is by far the hardest platform to support and build for. Please use the binaries for Windows instead unless you want to commit fixes to this repo.

### MacOS & Linux

Expand All @@ -74,12 +79,7 @@ BSD-like license that allows static linking with closed source software. Check [

### Windows

Windows requires compiling with PHP sources, you will still get a .dll in the end.

cmake clean
buildconf --force
configure --with-raylib
nmake
Windows requires compiling with PHP sources (Currently Visual Studio 2017 needed), you will still get a .dll in the end.

You will need to also compile RayLib as well. At the time writing I am using Visual Studio 2017 compiler, since PHP 7.4 requires this. Once compiled paste the following files:

Expand All @@ -103,6 +103,13 @@ You will need to also compile RayLib as well. At the time writing I am using Vis
C:\src\raylib-2.6.0\cmake-build-debug\src\raymath.h -> C:\php-sdk\phpmaster\vc15\x64\deps\include\raymath.h
C:\src\raylib-2.6.0\cmake-build-debug\src\rlgl.h -> C:\php-sdk\phpmaster\vc15\x64\deps\include\rlgl.h

Once you have PHP & Raylib setup you can then compile the extension

%comspec% /k "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Auxiliary\Build\vcvars64.bat"
cd C:/php-sdk
phpsdk-vs16-x64.bat
cd phpmaster\vc15\x64\php-src
cls && buildconf --force && configure --disable-all --enable-cli --with-raylib && nmake

## How To Run raylib PHP Extension

Expand Down
2 changes: 1 addition & 1 deletion config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,5 @@ if test "$PHP_RAYLIB" != "no"; then

PHP_SUBST(RAYLIB_SHARED_LIBADD)

PHP_NEW_EXTENSION(raylib, raylib.c raylib-vector4.c raylib-vector3.c raylib-vector2.c raylib-rectangle.c raylib-image.c raylib-window.c raylib-timming.c raylib-camera3d.c raylib-camera2d.c raylib-draw.c raylib-rendertexture.c raylib-text.c raylib-cursor.c raylib-texture.c raylib-utils.c raylib-font.c raylib-key.c raylib-mouse.c raylib-collision.c raylib-color.c, $ext_shared,)
PHP_NEW_EXTENSION(raylib, raylib.c raylib-vector4.c raylib-vector3.c raylib-vector2.c raylib-rectangle.c raylib-image.c raylib-window.c raylib-timming.c raylib-camera3d.c raylib-camera2d.c raylib-draw.c raylib-rendertexture.c raylib-text.c raylib-cursor.c raylib-texture.c raylib-utils.c raylib-font.c raylib-key.c raylib-mouse.c raylib-collision.c raylib-color.c raylib-gamepad.c, $ext_shared,)
fi
4 changes: 2 additions & 2 deletions config.w32
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ if (PHP_RAYLIB != "no") {
ADD_FLAG("LIBS_RAYLIB", "kernel32.lib user32.lib gdi32.lib winmm.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib")


var raylib_sources = "raylib.c raylib-camera3d.c raylib-camera2d.c raylib-cursor.c raylib-draw.c raylib-image.c raylib-key.c raylib-rendertexture.c raylib-font.c raylib-text.c raylib-texture.c raylib-timming.c raylib-utils.c raylib-window.c raylib-mouse.c raylib-collision.c raylib-color.c raylib-vector4.c raylib-vector3.c raylib-vector2.c raylib-rectangle.c";
PHP_INSTALL_HEADERS("ext/raylib", "php_raylib.h raylib-camera3d.h raylib-camera2d.h raylib-cursor.h raylib-draw.h raylib-image.h raylib-key.h raylib-rendertexture.h raylib-font.h raylib-text.h raylib-texture.h raylib-timming.h raylib-utils.h raylib-window.h raylib-mouse.h raylib-collision.h raylib-color.h raylib-vector4.h raylib-vector3.h raylib-rectangle.h");
var raylib_sources = "raylib.c raylib-camera3d.c raylib-camera2d.c raylib-cursor.c raylib-draw.c raylib-image.c raylib-key.c raylib-rendertexture.c raylib-font.c raylib-text.c raylib-texture.c raylib-timming.c raylib-utils.c raylib-window.c raylib-mouse.c raylib-collision.c raylib-color.c raylib-vector4.c raylib-vector3.c raylib-vector2.c raylib-rectangle.c raylib-gamepad.c";
PHP_INSTALL_HEADERS("ext/raylib", "php_raylib.h raylib-camera3d.h raylib-camera2d.h raylib-cursor.h raylib-draw.h raylib-image.h raylib-key.h raylib-rendertexture.h raylib-font.h raylib-text.h raylib-texture.h raylib-timming.h raylib-utils.h raylib-window.h raylib-mouse.h raylib-collision.h raylib-color.h raylib-vector4.h raylib-vector3.h raylib-rectangle.h raylib-gamepad.h");
EXTENSION("raylib", raylib_sources, true, "/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1", "php_raylib.dll");

} else {
Expand Down
231 changes: 231 additions & 0 deletions examples/core/input-gamepad-input.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
<?php

if (!extension_loaded('raylib')) {
echo 'no raylib';
exit;
}

use raylib\Color;
use raylib\Draw;
use raylib\Input\Gamepad;
use raylib\Text;
use raylib\Timming;
use raylib\Vector2;
use raylib\Window;
use raylib\Texture;

const XBOX360_NAME_ID = "Xbox 360 Controller";
const PS3_NAME_ID = "PLAYSTATION(R)3 Controller";

// Initialization
//--------------------------------------------------------------------------------------
$screenWidth = 800;
$screenHeight = 450;

raylib\SetConfigFlags(raylib\flags\MSAA_4X_HINT);

$rayWhite = new Color(245, 245, 245, 255);
$lightGray = new Color(200, 200, 200, 255);
$darkGray = new Color(80, 80, 80, 255);
$maroon = new Color(190, 33, 55, 255);
$gray = new Color(130, 130, 130, 255);
$red = new Color(230, 41, 55, 255);
$blue = new Color(0, 121, 241, 255);
$gold = new Color(255, 203, 0, 255);
$lime = new Color(0, 158, 47, 255);
$darkBlue = new Color(0, 82, 172, 255);
$black = new Color(0, 0, 0, 255);
$violet = new Color(135, 60, 190, 255);
$pink = new Color(255, 109, 194, 255);

Window::init($screenWidth, $screenHeight, "raylib [core] example - gamepad input");

$texPs3Pad = new Texture(__DIR__ . "/resources/ps3.png");
$texXboxPad = new Texture(__DIR__ . "/resources/xbox.png");

$psControllerV1 = new Vector2(436, 168);
$psControllerV2 = new Vector2(436, 185);
$psControllerV3 = new Vector2(464, 177);

Timming::setTargetFps(60); // Set target frames-per-second
//--------------------------------------------------------------------------------------

// Main game loop
while (!Window::shouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
// ...
//----------------------------------------------------------------------------------

// Draw
//----------------------------------------------------------------------------------
Draw::begin();
Draw::clearBackground($rayWhite);

if (GamePad::isAvailable(Gamepad::PLAYER1)) {
Text::draw(sprintf("GP1: %s", Gamepad::getName(Gamepad::PLAYER1)), 10, 10, 10, $black);

if (stristr(Gamepad::getName(Gamepad::PLAYER1), 'xbox')) {
$texXboxPad->draw(0, 0, $darkGray);

// Draw buttons: xbox home
if (GamePad::isButtonDown(Gamepad::PLAYER1, Gamepad::BUTTON_MIDDLE)) {
Draw::circle(394, 89, 19, $red);
}

// Draw buttons: basic
if (GamePad::isButtonDown(Gamepad::PLAYER1, Gamepad::BUTTON_MIDDLE_RIGHT)) {
Draw::circle(436, 150, 9, $red);
}
if (GamePad::isButtonDown(Gamepad::PLAYER1, Gamepad::BUTTON_MIDDLE_LEFT)) {
Draw::circle(352, 150, 9, $red);
}
if (GamePad::isButtonDown(Gamepad::PLAYER1, Gamepad::BUTTON_RIGHT_FACE_LEFT)) {
Draw::circle(501, 151, 15, $blue);
}
if (GamePad::isButtonDown(Gamepad::PLAYER1, Gamepad::BUTTON_RIGHT_FACE_DOWN)) {
Draw::circle(536, 187, 15, $lime);
}
if (GamePad::isButtonDown(Gamepad::PLAYER1, Gamepad::BUTTON_RIGHT_FACE_RIGHT)) {
Draw::circle(572, 151, 15, $maroon);
}
if (GamePad::isButtonDown(Gamepad::PLAYER1, Gamepad::BUTTON_RIGHT_FACE_UP)) {
Draw::circle(536, 115, 15, $gold);
}

// Draw buttons: d-pad
Draw::rectangle(317, 202, 19, 71, $black);
Draw::rectangle(293, 228, 69, 19, $black);
if (GamePad::isButtonDown(Gamepad::PLAYER1, Gamepad::BUTTON_LEFT_FACE_UP)) {
Draw::rectangle(317, 202, 19, 26, $red);
}
if (GamePad::isButtonDown(Gamepad::PLAYER1, Gamepad::BUTTON_LEFT_FACE_DOWN)) {
Draw::rectangle(317, 202 + 45, 19, 26, $red);
}
if (GamePad::isButtonDown(Gamepad::PLAYER1, Gamepad::BUTTON_LEFT_FACE_LEFT)) {
Draw::rectangle(292, 228, 25, 19, $red);
}
if (GamePad::isButtonDown(Gamepad::PLAYER1, Gamepad::BUTTON_LEFT_FACE_RIGHT)) {
Draw::rectangle(292 + 44, 228, 26, 19, $red);
}
// Draw buttons: left-right back
if (GamePad::isButtonDown(Gamepad::PLAYER1, Gamepad::BUTTON_LEFT_TRIGGER_1)) Draw::circle(259, 61, 20, $red);
if (GamePad::isButtonDown(Gamepad::PLAYER1, Gamepad::BUTTON_RIGHT_TRIGGER_1)) Draw::circle(536, 61, 20, $red);

// Draw axis: left joystick
Draw::circle(259, 152, 39, $black);
Draw::circle(259, 152, 34, $lightGray);
Draw::circle(259 + (Gamepad::getAxisMovement(Gamepad::PLAYER1, Gamepad::AXIS_LEFT_X) * 20),
152 - (Gamepad::getAxisMovement(Gamepad::PLAYER1, Gamepad::AXIS_LEFT_Y) * 20), 25, $black);

// Draw axis: right joystick
Draw::circle(461, 237, 38, $black);
Draw::circle(461, 237, 33, $lightGray);
Draw::circle(461 + (Gamepad::getAxisMovement(Gamepad::PLAYER1, Gamepad::AXIS_RIGHT_X) * 20),
237 - (Gamepad::getAxisMovement(Gamepad::PLAYER1, Gamepad::AXIS_RIGHT_Y) * 20), 25, $black);

// Draw axis: left-right triggers
Draw::rectangle(170, 30, 15, 70, $gray);
Draw::rectangle(604, 30, 15, 70, $gray);
Draw::rectangle(170, 30, 15, (((1.0 + Gamepad::getAxisMovement(Gamepad::PLAYER1, Gamepad::AXIS_LEFT_TRIGGER)) / 2.0) * 70), $red);
Draw::rectangle(604, 30, 15, (((1.0 + Gamepad::getAxisMovement(Gamepad::PLAYER1, Gamepad::AXIS_RIGHT_TRIGGER)) / 2.0) * 70), $red);

} elseif (stristr(Gamepad::getName(Gamepad::PLAYER1), 'playstation')) {
$texPs3Pad->draw(0, 0, $gray);

// Draw buttons: ps
if (GamePad::isButtonDown(Gamepad::PLAYER1, Gamepad::BUTTON_MIDDLE)) Draw::circle(396, 222, 13, $red);

// Draw buttons: basic
if (GamePad::isButtonDown(Gamepad::PLAYER1, Gamepad::BUTTON_MIDDLE_LEFT)) {
Draw::rectangle(328, 170, 32, 13, $red);
}
if (GamePad::isButtonDown(Gamepad::PLAYER1, Gamepad::BUTTON_MIDDLE_RIGHT)) {
Draw::triangle($psControllerV1, $psControllerV2, $psControllerV3, $red);
}
if (GamePad::isButtonDown(Gamepad::PLAYER1, Gamepad::BUTTON_RIGHT_FACE_UP)) {
Draw::circle(557, 144, 13, $lime);
}
if (GamePad::isButtonDown(Gamepad::PLAYER1, Gamepad::BUTTON_RIGHT_FACE_RIGHT)) {
Draw::circle(586, 173, 13, $red);
}
if (GamePad::isButtonDown(Gamepad::PLAYER1, Gamepad::BUTTON_RIGHT_FACE_DOWN)) {
Draw::circle(557, 203, 13, $violet);
}
if (GamePad::isButtonDown(Gamepad::PLAYER1, Gamepad::BUTTON_RIGHT_FACE_LEFT)) {
Draw::circle(527, 173, 13, $pink);
}

// Draw buttons: d-pad
Draw::rectangle(225, 132, 24, 84, $black);
Draw::rectangle(195, 161, 84, 25, $black);
if (GamePad::isButtonDown(Gamepad::PLAYER1, Gamepad::BUTTON_LEFT_FACE_UP)) {
Draw::rectangle(225, 132, 24, 29, $red);
}
if (GamePad::isButtonDown(Gamepad::PLAYER1, Gamepad::BUTTON_LEFT_FACE_DOWN)) {
Draw::rectangle(225, 132 + 54, 24, 30, $red);
}
if (GamePad::isButtonDown(Gamepad::PLAYER1, Gamepad::BUTTON_LEFT_FACE_LEFT)) {
Draw::rectangle(195, 161, 30, 25, $red);
}
if (GamePad::isButtonDown(Gamepad::PLAYER1, Gamepad::BUTTON_LEFT_FACE_RIGHT)) {
Draw::rectangle(195 + 54, 161, 30, 25, $red);
}

// Draw buttons: left-right back buttons
if (GamePad::isButtonDown(Gamepad::PLAYER1, Gamepad::BUTTON_LEFT_TRIGGER_1)) {
Draw::circle(239, 82, 20, $red);
}
if (GamePad::isButtonDown(Gamepad::PLAYER1, Gamepad::BUTTON_RIGHT_TRIGGER_1)) {
Draw::circle(557, 82, 20, $red);
}

// Draw axis: left joystick
Draw::circle(319, 255, 35, $black);
Draw::circle(319, 255, 31, $lightGray);
Draw::circle(319 + (Gamepad::getAxisMovement(Gamepad::PLAYER1, Gamepad::AXIS_LEFT_X) * 20),
255 + (Gamepad::getAxisMovement(Gamepad::PLAYER1, Gamepad::AXIS_LEFT_Y) * 20), 25, $black);

// Draw axis: right joystick
Draw::circle(475, 255, 35, $black);
Draw::circle(475, 255, 31, $lightGray);
Draw::circle(475 + (Gamepad::getAxisMovement(Gamepad::PLAYER1, Gamepad::AXIS_RIGHT_X) * 20),
255 + (Gamepad::getAxisMovement(Gamepad::PLAYER1, Gamepad::AXIS_RIGHT_Y) * 20), 25, $black);

// Draw axis: left-right triggers
Draw::rectangle(169, 48, 15, 70, $gray);
Draw::rectangle(611, 48, 15, 70, $gray);
Draw::rectangle(169, 48, 15, (((1.0 - Gamepad::getAxisMovement(Gamepad::PLAYER1, Gamepad::AXIS_LEFT_TRIGGER)) / 2.0) * 70), $red);
Draw::rectangle(611, 48, 15, (((1.0 - Gamepad::getAxisMovement(Gamepad::PLAYER1, Gamepad::AXIS_RIGHT_TRIGGER)) / 2.0) * 70), $red);
} else {
Text::draw("GP1: NOT DETECTED", 280, 180, 20, $gray);

// TODO: Draw generic gamepad
}

Text::draw(sprintf("DETECTED AXIS [%i]:", Gamepad::getAxisCount(Gamepad::PLAYER1)), 10, 50, 10, $maroon);

for ($i = 0; $i < Gamepad::getAxisCount(Gamepad::PLAYER1); $i++) {
Text::draw(sprintf("AXIS %i: %.02f", $i, Gamepad::getAxisMovement(Gamepad::PLAYER1, $i)), 20, 70 + 20 * $i, 10, $darkGray);
}

if (Gamepad::getButtonPressed() != -1) {
Text::draw(sprintf("DETECTED BUTTON: %i", Gamepad::getButtonPressed()), 10, 430, 10, $red);
} else {
Text::draw("DETECTED BUTTON: NONE", 10, 430, 10, $gray);
}
} else {
Text::draw("GP1: NOT DETECTED", 10, 10, 10, $gray);

$texXboxPad->draw(0, 0, $lightGray);
}

Draw::end();
//----------------------------------------------------------------------------------
}

// De-Initialization
//--------------------------------------------------------------------------------------
Window::close(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
52 changes: 52 additions & 0 deletions examples/core/resources/distortion100.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#version 100

precision mediump float;

// Input vertex attributes (from vertex shader)
varying vec2 fragTexCoord;
varying vec4 fragColor;

// Input uniform values
uniform sampler2D texture0;
uniform vec4 colDiffuse;

// NOTE: Add here your custom variables
uniform vec2 leftLensCenter;
uniform vec2 rightLensCenter;
uniform vec2 leftScreenCenter;
uniform vec2 rightScreenCenter;
uniform vec2 scale;
uniform vec2 scaleIn;
uniform vec4 hmdWarpParam;
uniform vec4 chromaAbParam;

void main()
{
// Compute lens distortion
vec2 lensCenter = fragTexCoord.x < 0.5? leftLensCenter : rightLensCenter;
vec2 screenCenter = fragTexCoord.x < 0.5? leftScreenCenter : rightScreenCenter;
vec2 theta = (fragTexCoord - lensCenter)*scaleIn;
float rSq = theta.x*theta.x + theta.y*theta.y;
vec2 theta1 = theta*(hmdWarpParam.x + hmdWarpParam.y*rSq + hmdWarpParam.z*rSq*rSq + hmdWarpParam.w*rSq*rSq*rSq);
vec2 thetaBlue = theta1*(chromaAbParam.z + chromaAbParam.w*rSq);
vec2 tcBlue = lensCenter + scale*thetaBlue;

if (any(bvec2(clamp(tcBlue, screenCenter - vec2(0.25, 0.5), screenCenter + vec2(0.25, 0.5)) - tcBlue)))
{
// Set black fragment for everything outside the lens border
gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
}
else
{
// Compute color chroma aberration
float blue = texture2D(texture0, tcBlue).b;
vec2 tcGreen = lensCenter + scale*theta1;
float green = texture2D(texture0, tcGreen).g;

vec2 thetaRed = theta1*(chromaAbParam.x + chromaAbParam.y*rSq);
vec2 tcRed = lensCenter + scale*thetaRed;

float red = texture2D(texture0, tcRed).r;
gl_FragColor = vec4(red, green, blue, 1.0);
}
}
53 changes: 53 additions & 0 deletions examples/core/resources/distortion330.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#version 330

// Input vertex attributes (from vertex shader)
in vec2 fragTexCoord;
in vec4 fragColor;

// Input uniform values
uniform sampler2D texture0;
uniform vec4 colDiffuse;

// Output fragment color
out vec4 finalColor;

// NOTE: Add here your custom variables
uniform vec2 leftLensCenter = vec2(0.288, 0.5);
uniform vec2 rightLensCenter = vec2(0.712, 0.5);
uniform vec2 leftScreenCenter = vec2(0.25, 0.5);
uniform vec2 rightScreenCenter = vec2(0.75, 0.5);
uniform vec2 scale = vec2(0.25, 0.45);
uniform vec2 scaleIn = vec2(4, 2.2222);
uniform vec4 hmdWarpParam = vec4(1, 0.22, 0.24, 0);
uniform vec4 chromaAbParam = vec4(0.996, -0.004, 1.014, 0.0);

void main()
{
// Compute lens distortion
vec2 lensCenter = fragTexCoord.x < 0.5? leftLensCenter : rightLensCenter;
vec2 screenCenter = fragTexCoord.x < 0.5? leftScreenCenter : rightScreenCenter;
vec2 theta = (fragTexCoord - lensCenter)*scaleIn;
float rSq = theta.x*theta.x + theta.y*theta.y;
vec2 theta1 = theta*(hmdWarpParam.x + hmdWarpParam.y*rSq + hmdWarpParam.z*rSq*rSq + hmdWarpParam.w*rSq*rSq*rSq);
vec2 thetaBlue = theta1*(chromaAbParam.z + chromaAbParam.w*rSq);
vec2 tcBlue = lensCenter + scale*thetaBlue;

if (any(bvec2(clamp(tcBlue, screenCenter - vec2(0.25, 0.5), screenCenter + vec2(0.25, 0.5)) - tcBlue)))
{
// Set black fragment for everything outside the lens border
finalColor = vec4(0.0, 0.0, 0.0, 1.0);
}
else
{
// Compute color chroma aberration
float blue = texture(texture0, tcBlue).b;
vec2 tcGreen = lensCenter + scale*theta1;
float green = texture(texture0, tcGreen).g;

vec2 thetaRed = theta1*(chromaAbParam.x + chromaAbParam.y*rSq);
vec2 tcRed = lensCenter + scale*thetaRed;

float red = texture(texture0, tcRed).r;
finalColor = vec4(red, green, blue, 1.0);
}
}
Binary file added examples/core/resources/ps3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/core/resources/xbox.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 51b780b

Please sign in to comment.