Skip to content

Commit

Permalink
PPPPPPPPPPPPPPPPPPPPPP
Browse files Browse the repository at this point in the history
  • Loading branch information
isage committed Jun 20, 2022
0 parents commit a6236ec
Show file tree
Hide file tree
Showing 13 changed files with 303 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build

58 changes: 58 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
cmake_minimum_required(VERSION 3.0)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")

if(NOT DEFINED CMAKE_TOOLCHAIN_FILE)
if(DEFINED ENV{VITASDK})
set(CMAKE_TOOLCHAIN_FILE "$ENV{VITASDK}/share/vita.toolchain.cmake" CACHE PATH "toolchain file")
else()
message(FATAL_ERROR "Please define VITASDK to point to your SDK path!")
endif()
endif()

include("$ENV{VITASDK}/share/vita.cmake" REQUIRED)

set(VITA_MKSFOEX_FLAGS "${VITA_MKSFOEX_FLAGS} -d PARENTAL_LEVEL=1")
set(VITA_MKSFOEX_FLAGS "${VITA_MKSFOEX_FLAGS} -d ATTRIBUTE2=12")

project(pppp-vita)

find_package(PHP)
find_package(SDL2)

include_directories(
"src"
${PHP_INCLUDE_DIRS}
)

# CHANGE THOSE
set(ELFNAME "pppp")
set(TITLE_NAME "pppp-demo")
set(TITLE_ID "PPPP00817")

file(GLOB SOURCES src/*.cpp)

set(ELF "${ELFNAME}.elf")
set(SELF "${ELFNAME}.self")

add_executable("${ELF}"
${SOURCES}
)

target_link_libraries("${ELF}" php SDL2_mixer SDL2_image SDL2::SDL2 FLAC mpg123 opusfile opus modplug mikmod vorbisfile vorbis ogg png jpeg webp pthread z c m)

VITA_create_self("${SELF}"
"${ELF}"
UNSAFE
)

# Build VPK

set(VPK "${ELFNAME}.vpk")
set(TITLE_VER "01.00")

VITA_create_vpk("${VPK}" "${TITLE_ID}" "${SELF}"
NAME "${TITLE_NAME}"
VERSION "${TITLE_VER}"
FILE sce_sys sce_sys
FILE data data
)
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# PHP-Player-plus-plus (pp++ for short)

Embedded php interpreter with `SDL2/SDL2_mixer/SDL2_image` support for PSVita.
For when you don't know better language.

## Building

### Requirements:

* Latest [VitaSDK](https://vitasdk.org/)
* [Vita PHP8 port](https://github.com/isage/vita-packages-extra)

### Compiling

```
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
make
```

### Customizing

Entry script is located in `data/index.php`.
Livearea assets in sce_sys.
App name and id in CMakeLists.txt
70 changes: 70 additions & 0 deletions cmake/FindPHP.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#
# Copyright (C) 2017 Adam Saponara <as@php.net>
# Copyright (C) 2017-2022 Sébastien Helleu <flashcode@flashtux.org>
#
# This file is part of WeeChat, the extensible chat client.
#
# WeeChat is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# WeeChat is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with WeeChat. If not, see <https://www.gnu.org/licenses/>.
#

if(PHP_FOUND)
set(PHP_FIND_QUIETLY TRUE)
endif()

find_package(PkgConfig)
if(PKG_CONFIG_FOUND)
pkg_search_module(PHP php8 php7)
endif()

if(NOT PHP_FOUND)
find_program(PHP_CONFIG_EXECUTABLE NAMES
php-config8.1 php-config81
php-config8.0 php-config80
php-config8
php-config7.4 php-config74
php-config7.3 php-config73
php-config7.2 php-config72
php-config7.1 php-config71
php-config7.0 php-config70
php-config7
php-config
)
if (PHP_CONFIG_EXECUTABLE)
execute_process(COMMAND ${PHP_CONFIG_EXECUTABLE} --prefix OUTPUT_VARIABLE PHP_LIB_PREFIX OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(COMMAND ${PHP_CONFIG_EXECUTABLE} --includes OUTPUT_VARIABLE PHP_INCLUDE_DIRS OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(COMMAND ${PHP_CONFIG_EXECUTABLE} --libs OUTPUT_VARIABLE PHP_LIBS OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(COMMAND ${PHP_CONFIG_EXECUTABLE} --version OUTPUT_VARIABLE PHP_VERSION OUTPUT_STRIP_TRAILING_WHITESPACE)
if(${PHP_VERSION} MATCHES "^[78]")
find_library(PHP_LIB
NAMES php8.1 php8.0 php8 php7.4 php7.3 php7.2 php7.1 php7.0 php7 php
HINTS ${PHP_LIB_PREFIX} ${PHP_LIB_PREFIX}/lib ${PHP_LIB_PREFIX}/lib64
)
if(PHP_LIB)
get_filename_component(PHP_LIB_DIR ${PHP_LIB} DIRECTORY)
string(REPLACE "-I" "" PHP_INCLUDE_DIRS ${PHP_INCLUDE_DIRS})
SEPARATE_ARGUMENTS(PHP_INCLUDE_DIRS)
set(PHP_LDFLAGS "-L${PHP_LIB_DIR} ${PHP_LIBS}")
set(PHP_FOUND 1)
endif()
endif()
endif()
endif()

if(NOT PHP_FOUND)
message(WARNING "Could not find libphp. "
"Ensure PHP >=7.0.0 development libraries are installed and compiled with `--enable-embed`. "
"Ensure `php-config` is in `PATH`. "
"You may set `-DCMAKE_LIBRARY_PATH=...` to the directory containing libphp."
)
endif()
20 changes: 20 additions & 0 deletions data/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php declare(strict_types=1);

error_reporting(E_ALL);

if (!extension_loaded('sdl')) {
fprintf(STDERR, "The sdl extension is not loaded. Make sure it is in the system and there is a line for it on the php.ini file (eg \"extension=sdl.so\")");
exit(1);
}

function initSDLOrExit() {
if(SDL_Init(SDL_INIT_EVERYTHING) !== 0) {
printSdlErrorAndExit();
}
}

function printSdlErrorAndExit() {
fprintf(STDERR, "ERROR: %s\n", SDL_GetError());
exit(1);
}

Binary file added data/explode.wav
Binary file not shown.
102 changes: 102 additions & 0 deletions data/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

declare(strict_types=1);

require 'data/bootstrap.php';

if (SDL_GetVersion($version)) {
printf('Powered by PHP %s, SDL extension %s and SDL2 library %s.' . PHP_EOL, phpversion(), phpversion('sdl'), implode('.', $version));
} else {
trigger_error('SDL version could not be retrieved', E_USER_NOTICE);
}

$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("example", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 960, 544, SDL_WINDOW_SHOWN);
$renderer = SDL_CreateRenderer($window, -1, 0);

$texture = IMG_LoadTexture($renderer, "data/spaceship.png");
if ($texture === null) {
exit('Unable to load 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(0,0,64,64);
$destRect->x = $x = 100;
$destRect->y = $y = 100;

if (\Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0) {
throw new RuntimeException("Cannot open audio device");
}

$wav = Mix_LoadWAV('data/explode.wav');

$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;
case SDL_JOYBUTTONDOWN:
if ($event->jbutton->button == 2)
Mix_PlayChannel(-1, $wav, 0);
break;
}
}

if ($update) {
SDL_RenderClear($renderer);
$destRect->x = intval($x);
$destRect->y = intval($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();
Binary file added data/spaceship.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 sce_sys/icon0.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 sce_sys/livearea/contents/bg0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions sce_sys/livearea/contents/template.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<livearea style="a1" format-ver="01.00">
<livearea-background>
<image>bg0.png</image>
</livearea-background>
<gate>
</gate>
</livearea>
Binary file added sce_sys/pic0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <sapi/embed/php_embed.h>

int _newlib_heap_size_user = 250 * 1024 * 1024;

int main(int argc, char **argv)
{
PHP_EMBED_START_BLOCK(argc, argv)

zend_file_handle file_handle;
zend_stream_init_filename(&file_handle, "data/index.php");

if (php_execute_script(&file_handle) == FAILURE) {
php_printf("Failed to execute PHP script.\n");
}

PHP_EMBED_END_BLOCK()
}

0 comments on commit a6236ec

Please sign in to comment.