Initial dump
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
.luarepl-history
|
18
README.md
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
# Ray-Casting Learning
|
||||||
|
|
||||||
|
This repository holds my learning of implementing ray-casting in Love2D using
|
||||||
|
Fennel. I hope also to implement a similar technique in the TIC-80, also using
|
||||||
|
Fennel; I suspect much of the code will be replicated.
|
||||||
|
|
||||||
|
This learning depends primarily on the work of Lode ([Raycasting
|
||||||
|
Tutorial](https://lodev.org/cgtutor/raycasting.html)), and supplemented by
|
||||||
|
wojciech-graj and their [Wolf-80](https://github.com/wojciech-graj/Wolf-80)
|
||||||
|
project.
|
||||||
|
|
||||||
|
This project was started with the
|
||||||
|
[min-love2d-fennel](https://gitlab.com/alexjgriffith/min-love2d-fennel) project
|
||||||
|
authored by Alex Griffith.
|
||||||
|
|
||||||
|
A huge thank you to all aforementioned. You have made my learning possible, and
|
||||||
|
my exploration into game-dev and Fennel/Love2D much more accessible and
|
||||||
|
enjoyable!
|
0
assets/.keep
Normal file
166
buildtools/appimage/build.sh
Executable file
|
@ -0,0 +1,166 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
set -eo >/dev/null
|
||||||
|
|
||||||
|
CURRENT_APPIMAGEKIT_RELEASE=9
|
||||||
|
ARCH="$(uname -m)"
|
||||||
|
|
||||||
|
if [[ $# -lt 1 ]]; then
|
||||||
|
echo "Usage: $0 <version>"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
VERSION="$1"
|
||||||
|
LOVEFILE="$2"
|
||||||
|
USERNAME=$3
|
||||||
|
TOKEN=$4
|
||||||
|
|
||||||
|
# https://github.com/love2d/love/releases/download/11.5/love-11.5-x86_64.AppImage
|
||||||
|
# https://github.com/love2d/love/releases/download/11.4/love-11.4-x86_64.AppImage
|
||||||
|
# https://github.com/love2d/love/releases/download/11.3/love-11.3-x86_64.AppImage
|
||||||
|
# https://github.com/love2d/love/releases/download/11.2/love-11.2-x86_64.AppImage
|
||||||
|
# https://github.com/love2d/love/releases/download/11.1/love-11.1-linux-x86_64.AppImage
|
||||||
|
LOVE_AppImage_URL=https://github.com/love2d/love/releases/download/${VERSION}/love-${VERSION}-${ARCH}.AppImage
|
||||||
|
if [ $VERSION == "11.1" ]
|
||||||
|
then
|
||||||
|
LOVE_AppImage_URL=https://github.com/love2d/love/releases/download/${VERSION}/love-${VERSION}-linux-${ARCH}.AppImage
|
||||||
|
fi
|
||||||
|
CACHE_DIR=${HOME}/.cache/love-release/love
|
||||||
|
LOVE_AppImage=$CACHE_DIR/love-${VERSION}-${ARCH}.AppImage
|
||||||
|
|
||||||
|
if ! test -d ${CACHE_DIR}; then
|
||||||
|
mkdir -p ${CACHE_DIR}
|
||||||
|
fi
|
||||||
|
|
||||||
|
get-development-artifact(){
|
||||||
|
owner=love2d
|
||||||
|
repo=love
|
||||||
|
branch="12.0-development"
|
||||||
|
artifact="love-x86_64.AppImage" # love-windows-x64 love-macos
|
||||||
|
token=$2
|
||||||
|
username=$1
|
||||||
|
list_run_url="https://api.github.com/repos/${owner}/${repo}/actions/runs"
|
||||||
|
echo "find latest run of branch 12.0"
|
||||||
|
latest_run=$(curl -v --silent \
|
||||||
|
-H "Accept: application/vnd.github.v3+json" \
|
||||||
|
-H "Authorization token ${token}" "${list_run_url}" \
|
||||||
|
--stderr - |\
|
||||||
|
grep -B 4 "\"head_branch\": \"12.0-development\"" |\
|
||||||
|
grep "id" |\
|
||||||
|
head -1 |\
|
||||||
|
sed 's/^.* \(.*\),$/\1/g')
|
||||||
|
list_artifacts_url="https://api.github.com/repos/${owner}/${repo}/actions/runs/${latest_run}/artifacts"
|
||||||
|
echo "get artifact_url"
|
||||||
|
artifact_url=$(curl --silent \
|
||||||
|
-H "Accept: application/vnd.github.v3+json" \
|
||||||
|
-H "Authorization token ${token}" \
|
||||||
|
"${list_artifacts_url}" \
|
||||||
|
--stderr - |\
|
||||||
|
grep -A 5 "\"name\": \"${artifact}\"," |\
|
||||||
|
grep "archive_download_url" |\
|
||||||
|
head -1 |\
|
||||||
|
sed 's/^.* \"\(.*\)\",$/\1/g')
|
||||||
|
echo "download target"
|
||||||
|
echo curl -L -u ${username}:${token} ${artifact_url} -o love.zip
|
||||||
|
echo $(pwd)
|
||||||
|
rm -f love.zip love-*.AppImage && \
|
||||||
|
curl -L -u ${username}:${token} ${artifact_url} -o love.zip \
|
||||||
|
&& unzip love.zip && \
|
||||||
|
rm -f love.zip ${artifact} && \
|
||||||
|
mv love-*.AppImage ${artifact}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ! test -f ${LOVE_AppImage}; then
|
||||||
|
if [ $VERSION != "12.0" ]
|
||||||
|
then
|
||||||
|
curl -L -C - -o ${LOVE_AppImage} ${LOVE_AppImage_URL}
|
||||||
|
else
|
||||||
|
get-development-artifact $USERNAME $TOKEN
|
||||||
|
mv love-x86_64.AppImage ${LOVE_AppImage}
|
||||||
|
fi
|
||||||
|
chmod +x ${LOVE_AppImage}
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo $CACHE_DIR
|
||||||
|
|
||||||
|
if ! test -f ${LOVE_AppImage}; then
|
||||||
|
echo "No tarball found for $VERSION in $LOVE_AppImage"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
download_if_needed() {
|
||||||
|
if ! test -f "$1"; then
|
||||||
|
if ! curl -L -o "$1" "https://github.com/AppImage/AppImageKit/releases/download/${CURRENT_APPIMAGEKIT_RELEASE}/$1"; then
|
||||||
|
echo "Failed to download appimagetool"
|
||||||
|
echo "Please supply it manually"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
chmod +x "$1"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
main() {
|
||||||
|
download_if_needed appimagetool-${ARCH}.AppImage
|
||||||
|
# Extract the tarball build into a folder
|
||||||
|
rm -rf love-prepared
|
||||||
|
mkdir -p love-prepared
|
||||||
|
cd love-prepared
|
||||||
|
$LOVE_AppImage --appimage-extract 1> /dev/null 2> /dev/null
|
||||||
|
mv squashfs-root/* .
|
||||||
|
rm squashfs-root/.DirIcon
|
||||||
|
rmdir squashfs-root
|
||||||
|
# Add our small wrapper script (yay, more wrappers), and AppRun
|
||||||
|
local desktopfile="love.desktop"
|
||||||
|
local icon="love"
|
||||||
|
local target="love-${VERSION}"
|
||||||
|
|
||||||
|
if test -f ../../game.desktop.in; then
|
||||||
|
desktopfile="game.desktop"
|
||||||
|
cp ../../game.desktop.in .
|
||||||
|
fi
|
||||||
|
if test -f ../../game.svg; then
|
||||||
|
icon="game"
|
||||||
|
cp ../../game.svg .
|
||||||
|
fi
|
||||||
|
if test -f ${LOVEFILE}; then
|
||||||
|
if [[ $VERSION == "11.4" || $VERSION == "11.5" || $VERSION == "12.0" ]]
|
||||||
|
then
|
||||||
|
dir="bin"
|
||||||
|
else
|
||||||
|
dir="usr/bin"
|
||||||
|
fi
|
||||||
|
target="game"
|
||||||
|
cat $dir/love ${LOVEFILE} > $dir/love-fused
|
||||||
|
mv $dir/love-fused $dir/love
|
||||||
|
chmod +x $dir/love
|
||||||
|
else
|
||||||
|
echo "Love file ${LOVEFILE} not found"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Add our desktop file
|
||||||
|
mv ${desktopfile} ${desktopfile}.in
|
||||||
|
sed -e "s/%ICON%/${icon}/" "${desktopfile}.in" > "$desktopfile"
|
||||||
|
rm "${desktopfile}.in"
|
||||||
|
|
||||||
|
# Add a DirIcon
|
||||||
|
cp "${icon}.svg" .DirIcon
|
||||||
|
|
||||||
|
# Clean up
|
||||||
|
if test -f ../../game.desktop.in; then
|
||||||
|
rm love.desktop.in
|
||||||
|
fi
|
||||||
|
if test -f ../../game.svg; then
|
||||||
|
rm love.svg
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Now build the final AppImage
|
||||||
|
cd ..
|
||||||
|
|
||||||
|
# ./love-prepared/AppRun -n love-prepared "${target}-${ARCH}.AppImage"
|
||||||
|
# Work around missing FUSE/docker
|
||||||
|
./appimagetool-${ARCH}.AppImage --appimage-extract 1> /dev/null 2> /dev/null
|
||||||
|
./squashfs-root/AppRun -n love-prepared "${target}-${ARCH}.AppImage" 1> /dev/null 2> /dev/null
|
||||||
|
## rm -rf love-prepared/
|
||||||
|
}
|
||||||
|
|
||||||
|
main "$@"
|
216
buildtools/love-js/love-js.sh
Executable file
|
@ -0,0 +1,216 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
help="love-js.sh: \n
|
||||||
|
A tool for assembling love.js projects without relying on npm / node. \n\
|
||||||
|
For the full project please see https://github.com/Davidobot/love.js \n\
|
||||||
|
\n\
|
||||||
|
usage: \n\
|
||||||
|
./love-js.sh [love-file] [project-name] [opts]\n\
|
||||||
|
\n
|
||||||
|
opts:\n
|
||||||
|
-v= (--version=) Version of the Game. Will be included in file name.\n
|
||||||
|
-o= (--output-directory=) Target directory. Defaults to PWD\n
|
||||||
|
-t= (--text-colour=) Text Colour. Defaults to \"240,234,214\"\n
|
||||||
|
-c= (--canvas-colour=) Canvas Colour. Defaults to \"54,69,79\"\n
|
||||||
|
-a= (--author=) Author (Shows up on loading page).\n
|
||||||
|
-w= (--width=) Canvas Width (game width in conf). Defaults to 800\n
|
||||||
|
-h= (--height=) Canvas Height (game height in conf). Defaults to 600\n
|
||||||
|
-r (--run) Set flag if you want to run a local copy on port 8000\n
|
||||||
|
-d (--debug) Set flag for debug info\n
|
||||||
|
-h (--help) Display this text\n
|
||||||
|
\n
|
||||||
|
eg: \n\
|
||||||
|
|
||||||
|
Pack up sample.love\n
|
||||||
|
\t./love-js.sh sample.love sample\n
|
||||||
|
\t> sample-web.zip\n
|
||||||
|
\n
|
||||||
|
Pack up sample.love version 0.1.0\n
|
||||||
|
\t./love-js.sh sample-0.1.0.love sample -v=0.1.0\n
|
||||||
|
\t> sample-0.1.0-web.zip\n
|
||||||
|
\n
|
||||||
|
Pack up sample.love and set the background colour to black\n
|
||||||
|
\t./love-js.sh sample-0.1.0.love sample -v=0.1.0 -c=\"0,0,0\"\n
|
||||||
|
\t> sample-0.1.0-web.zip\n
|
||||||
|
\n
|
||||||
|
Pack up sample.love and set the author to \"Sample Name\"\n
|
||||||
|
\t./love-js.sh sample-0.1.0.love sample -a=\"Sample Name\"\n
|
||||||
|
\t> sample-web.zip"
|
||||||
|
|
||||||
|
if [ "$#" -lt "2" ]
|
||||||
|
then
|
||||||
|
echo "ERROR! love-js.sh expects at least two arguments."
|
||||||
|
echo -e $help
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# use gdu on macOS, fixes 'invalid option -b' error
|
||||||
|
if [ "$(uname -s)" = "Darwin" ]; then
|
||||||
|
DU_CMD=gdu
|
||||||
|
else
|
||||||
|
DU_CMD=du
|
||||||
|
fi
|
||||||
|
|
||||||
|
love_file=$1
|
||||||
|
name=$2
|
||||||
|
## confirm that $release_dir/$name-$version.love exists
|
||||||
|
if [ ! -f $love_file ]; then
|
||||||
|
echo "love file not found!"
|
||||||
|
echo $love_file
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -f /proc/sys/kernel/random/uuid ]; then
|
||||||
|
uuid=$(cat /proc/sys/kernel/random/uuid)
|
||||||
|
else
|
||||||
|
uuid="2fd99e56-5455-45dd-86dd-7af724874d65"
|
||||||
|
fi
|
||||||
|
|
||||||
|
author="";
|
||||||
|
run=false;
|
||||||
|
debug=false;
|
||||||
|
gethelp=false;
|
||||||
|
width=800
|
||||||
|
height=600
|
||||||
|
release="compat"
|
||||||
|
love_version="11.5"
|
||||||
|
canvas_colour="54,69,79"
|
||||||
|
text_colour="240,234,214"
|
||||||
|
initial_memory=0
|
||||||
|
module_size=$($DU_CMD -b $love_file | awk '{print $1}')
|
||||||
|
title=$(echo $name | sed -r 's/\<./\U&/g' | sed -r 's/-/\ /g')
|
||||||
|
version=""
|
||||||
|
dash_version=""
|
||||||
|
output_dir=$(pwd)
|
||||||
|
cachemodule="true"
|
||||||
|
|
||||||
|
for i in "$@"
|
||||||
|
do
|
||||||
|
case $i in
|
||||||
|
-v=*|--version=*)
|
||||||
|
version="${i#*=}"
|
||||||
|
dash_version="-${i#*=}"
|
||||||
|
;;
|
||||||
|
-o=*|--output-directory=*)
|
||||||
|
output_dir="${i#*=}"
|
||||||
|
;;
|
||||||
|
-w=*|--width=*)
|
||||||
|
width="${i#*=}"
|
||||||
|
;;
|
||||||
|
-h=*|--height=*)
|
||||||
|
height="${i#*=}"
|
||||||
|
;;
|
||||||
|
-t=*|--text-colour=*)
|
||||||
|
text_colour="${i#*=}"
|
||||||
|
;;
|
||||||
|
-c=*|--canvas-colour=*)
|
||||||
|
canvas_colour="${i#*=}"
|
||||||
|
;;
|
||||||
|
-a=*|--author=*)
|
||||||
|
author="${i#*=}"
|
||||||
|
;;
|
||||||
|
-r|--run)
|
||||||
|
run=true
|
||||||
|
;;
|
||||||
|
-d|--debug)
|
||||||
|
debug=true
|
||||||
|
;;
|
||||||
|
-h|--help)
|
||||||
|
gethelp=true
|
||||||
|
;;
|
||||||
|
-n|--no-cache)
|
||||||
|
cachemodule="false"
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
# unknown option
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
page_colour=$canvas_colour
|
||||||
|
|
||||||
|
file_name=$output_dir/$name$dash_version-web
|
||||||
|
|
||||||
|
debug (){
|
||||||
|
if [ $debug = true ]
|
||||||
|
then
|
||||||
|
echo ""
|
||||||
|
echo "Debug: love-js.sh"
|
||||||
|
echo "love file: ${love_file}"
|
||||||
|
echo "output file: $file_name"
|
||||||
|
echo "author: $author"
|
||||||
|
echo "version: $version"
|
||||||
|
echo "text colour: $text_colour"
|
||||||
|
echo "canvas colour: $canvas_colour"
|
||||||
|
echo "canvas size: ${height}, ${width}"
|
||||||
|
echo "run: ${run}"
|
||||||
|
echo "use cache: $cachemodule"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
call_dir=$(pwd)
|
||||||
|
root="$(dirname "$0")"
|
||||||
|
|
||||||
|
build(){
|
||||||
|
rm -fr $file_name
|
||||||
|
mkdir -p $file_name && mkdir -p $file_name/theme
|
||||||
|
|
||||||
|
cat $root/src/index.html | \
|
||||||
|
sed "s/{{title}}/${title}/g" | \
|
||||||
|
sed "s/{{version}}/${version}/g" | \
|
||||||
|
sed "s/{{author}}/${author}/g" | \
|
||||||
|
sed "s/{{width}}/${width}/g" | \
|
||||||
|
sed "s/{{height}}/${height}/g" | \
|
||||||
|
sed "s/{{initial-memory}}/${initial_memory}/g" | \
|
||||||
|
sed "s/{{canvas-colour}}/${canvas_colour}/g" | \
|
||||||
|
sed "s/{{text-colour}}/${text_colour}/g" > \
|
||||||
|
$file_name/index.html
|
||||||
|
|
||||||
|
cat $root/src/love.css | \
|
||||||
|
sed "s/{{page-colour}}/${page_colour}/g" > \
|
||||||
|
$file_name/theme/love.css
|
||||||
|
|
||||||
|
cat $root/src/game.js | \
|
||||||
|
sed "s/{{{cachemodule}}}/${cachemodule}/g" | \
|
||||||
|
sed "s/{{{metadata}}}/{\"package_uuid\":\"${uuid}\",\"remote_package_size\":$module_size,\"files\":[{\"filename\":\"\/game.love\",\"crunched\":0,\"start\":0,\"end\":$module_size,\"audio\":false}]}/" > \
|
||||||
|
$file_name/game.js
|
||||||
|
cp $root/src/serve.py $file_name
|
||||||
|
cp $root/src/consolewrapper.js $file_name
|
||||||
|
cp $love_file $file_name/game.love
|
||||||
|
cp $root/src/love-$love_version/$release/love.js $file_name
|
||||||
|
cp $root/src/love-$love_version/$release/love.wasm $file_name
|
||||||
|
if [ $release == "release" ]; then
|
||||||
|
cp $root/src/release/love.worker.js $file_name
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
clean(){
|
||||||
|
rm -rf $file_name
|
||||||
|
}
|
||||||
|
|
||||||
|
release (){
|
||||||
|
rm -fr $file_name
|
||||||
|
build
|
||||||
|
debug
|
||||||
|
zip -r -q tmp $file_name
|
||||||
|
rm -f $file_name.zip
|
||||||
|
mv tmp.zip $file_name.zip
|
||||||
|
clean
|
||||||
|
}
|
||||||
|
|
||||||
|
run (){
|
||||||
|
debug
|
||||||
|
build
|
||||||
|
cd $file_name
|
||||||
|
python3 serve.py
|
||||||
|
}
|
||||||
|
|
||||||
|
if [ $gethelp = true ]
|
||||||
|
then
|
||||||
|
echo -e $help
|
||||||
|
elif [ $run = false ]
|
||||||
|
then
|
||||||
|
release
|
||||||
|
else
|
||||||
|
run
|
||||||
|
fi
|
99
buildtools/love-js/src/consolewrapper.js
Normal file
|
@ -0,0 +1,99 @@
|
||||||
|
var newConsole = (function(oldConsole)
|
||||||
|
{
|
||||||
|
return {
|
||||||
|
log : function()
|
||||||
|
{
|
||||||
|
var data = [];
|
||||||
|
for (var _i = 0; _i < arguments.length; _i++) {
|
||||||
|
data[_i] = arguments[_i];
|
||||||
|
}
|
||||||
|
if(data.length == 1) //Start looking for api's (And dont show anything)
|
||||||
|
{
|
||||||
|
if(typeof(data[0]) == "string" && data[0].indexOf("callJavascriptFunction") != -1) //Contains function
|
||||||
|
{
|
||||||
|
// oldConsole.log(data[0]);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return eval(data[0].split("callJavascriptFunction ")[1]);
|
||||||
|
}
|
||||||
|
catch(e)
|
||||||
|
{
|
||||||
|
oldConsole.error("Something went wrong with your callJS: \nCode: " + data[0].split("callJavascriptFunction ")[1] + "\nError: '" + e.message + "'");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
oldConsole.log(data[0]);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
oldConsole.log(data[0], data.splice(1));
|
||||||
|
return null;
|
||||||
|
},
|
||||||
|
warn : function()
|
||||||
|
{
|
||||||
|
var data = [];
|
||||||
|
for (var _i = 0; _i < arguments.length; _i++) {
|
||||||
|
data[_i] = arguments[_i];
|
||||||
|
}
|
||||||
|
if(data.length == 1)
|
||||||
|
oldConsole.warn(data[0]);
|
||||||
|
else
|
||||||
|
oldConsole.warn(data[0], data.splice(1));
|
||||||
|
},
|
||||||
|
error : function()
|
||||||
|
{
|
||||||
|
var data = [];
|
||||||
|
for (var _i = 0; _i < arguments.length; _i++) {
|
||||||
|
data[_i] = arguments[_i];
|
||||||
|
}
|
||||||
|
if(data.length == 1)
|
||||||
|
oldConsole.error(data[0]);
|
||||||
|
else
|
||||||
|
oldConsole.error(data[0], data.splice(1));
|
||||||
|
},
|
||||||
|
info : function()
|
||||||
|
{
|
||||||
|
var data = [];
|
||||||
|
for (var _i = 0; _i < arguments.length; _i++) {
|
||||||
|
data[_i] = arguments[_i];
|
||||||
|
}
|
||||||
|
if(data.length == 1)
|
||||||
|
oldConsole.info(data[0]);
|
||||||
|
else
|
||||||
|
oldConsole.info(data[0], data.splice(1));
|
||||||
|
},
|
||||||
|
clear : function()
|
||||||
|
{
|
||||||
|
oldConsole.clear()
|
||||||
|
},
|
||||||
|
assert : function()
|
||||||
|
{
|
||||||
|
for (var _i = 0; _i < arguments.length; _i++) {
|
||||||
|
data[_i] = arguments[_i];
|
||||||
|
}
|
||||||
|
oldConsole.assert(data[0], data[1], data.splice(2));
|
||||||
|
},
|
||||||
|
group : function()
|
||||||
|
{
|
||||||
|
for (var _i = 0; _i < arguments.length; _i++) {
|
||||||
|
data[_i] = arguments[_i];
|
||||||
|
}
|
||||||
|
oldConsole.group(data[0], data.splice(1));
|
||||||
|
},
|
||||||
|
groupCollapsed : function()
|
||||||
|
{
|
||||||
|
for (var _i = 0; _i < arguments.length; _i++) {
|
||||||
|
data[_i] = arguments[_i];
|
||||||
|
}
|
||||||
|
oldConsole.groupCollapsed(data[0], data.splice(1));
|
||||||
|
},
|
||||||
|
groupEnd : function()
|
||||||
|
{
|
||||||
|
oldConsole.groupEnd()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}(window.console));
|
||||||
|
window.console = newConsole;
|
295
buildtools/love-js/src/game.js
Normal file
|
@ -0,0 +1,295 @@
|
||||||
|
var Module;
|
||||||
|
|
||||||
|
var CACHEMODULE = {{{cachemodule}}};
|
||||||
|
|
||||||
|
if (typeof Module === 'undefined') Module = eval('(function() { try { return Module || {} } catch(e) { return {} } })()');
|
||||||
|
|
||||||
|
if (!Module.expectedDataFileDownloads) {
|
||||||
|
Module.expectedDataFileDownloads = 0;
|
||||||
|
Module.finishedDataFileDownloads = 0;
|
||||||
|
}
|
||||||
|
Module.expectedDataFileDownloads++;
|
||||||
|
(function() {
|
||||||
|
var loadPackage = function(metadata) {
|
||||||
|
|
||||||
|
var PACKAGE_PATH;
|
||||||
|
if (typeof window === 'object') {
|
||||||
|
PACKAGE_PATH = window['encodeURIComponent'](window.location.pathname.toString().substring(0, window.location.pathname.toString().lastIndexOf('/')) + '/');
|
||||||
|
} else if (typeof location !== 'undefined') {
|
||||||
|
// worker
|
||||||
|
PACKAGE_PATH = encodeURIComponent(location.pathname.toString().substring(0, location.pathname.toString().lastIndexOf('/')) + '/');
|
||||||
|
} else {
|
||||||
|
throw 'using preloaded data can only be done on a web page or in a web worker';
|
||||||
|
}
|
||||||
|
var PACKAGE_NAME = 'game.love';
|
||||||
|
var REMOTE_PACKAGE_BASE = 'game.love';
|
||||||
|
if (typeof Module['locateFilePackage'] === 'function' && !Module['locateFile']) {
|
||||||
|
Module['locateFile'] = Module['locateFilePackage'];
|
||||||
|
Module.printErr('warning: you defined Module.locateFilePackage, that has been renamed to Module.locateFile (using your locateFilePackage for now)');
|
||||||
|
}
|
||||||
|
var REMOTE_PACKAGE_NAME = typeof Module['locateFile'] === 'function' ?
|
||||||
|
Module['locateFile'](REMOTE_PACKAGE_BASE) :
|
||||||
|
((Module['filePackagePrefixURL'] || '') + REMOTE_PACKAGE_BASE);
|
||||||
|
|
||||||
|
var REMOTE_PACKAGE_SIZE = metadata.remote_package_size;
|
||||||
|
var PACKAGE_UUID = metadata.package_uuid;
|
||||||
|
|
||||||
|
function fetchRemotePackage(packageName, packageSize, callback, errback) {
|
||||||
|
var xhr = new XMLHttpRequest();
|
||||||
|
xhr.open('GET', packageName, true);
|
||||||
|
xhr.responseType = 'arraybuffer';
|
||||||
|
xhr.onprogress = function(event) {
|
||||||
|
var url = packageName;
|
||||||
|
var size = packageSize;
|
||||||
|
if (event.total) size = event.total;
|
||||||
|
if (event.loaded) {
|
||||||
|
if (!xhr.addedTotal) {
|
||||||
|
xhr.addedTotal = true;
|
||||||
|
if (!Module.dataFileDownloads) Module.dataFileDownloads = {};
|
||||||
|
Module.dataFileDownloads[url] = {
|
||||||
|
loaded: event.loaded,
|
||||||
|
total: size
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
Module.dataFileDownloads[url].loaded = event.loaded;
|
||||||
|
}
|
||||||
|
var total = 0;
|
||||||
|
var loaded = 0;
|
||||||
|
var num = 0;
|
||||||
|
for (var download in Module.dataFileDownloads) {
|
||||||
|
var data = Module.dataFileDownloads[download];
|
||||||
|
total += data.total;
|
||||||
|
loaded += data.loaded;
|
||||||
|
num++;
|
||||||
|
}
|
||||||
|
total = Math.ceil(total * Module.expectedDataFileDownloads/num);
|
||||||
|
if (Module['setStatus']) Module['setStatus']('Downloading data... (' + loaded + '/' + total + ')');
|
||||||
|
} else if (!Module.dataFileDownloads) {
|
||||||
|
if (Module['setStatus']) Module['setStatus']('Downloading data...');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
xhr.onerror = function(event) {
|
||||||
|
throw new Error("NetworkError for: " + packageName);
|
||||||
|
}
|
||||||
|
xhr.onload = function(event) {
|
||||||
|
if (xhr.status == 200 || xhr.status == 304 || xhr.status == 206 || (xhr.status == 0 && xhr.response)) { // file URLs can return 0
|
||||||
|
var packageData = xhr.response;
|
||||||
|
callback(packageData);
|
||||||
|
} else {
|
||||||
|
throw new Error(xhr.statusText + " : " + xhr.responseURL);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
xhr.send(null);
|
||||||
|
};
|
||||||
|
|
||||||
|
function handleError(error) {
|
||||||
|
console.error('package error:', error);
|
||||||
|
};
|
||||||
|
|
||||||
|
function runWithFS() {
|
||||||
|
|
||||||
|
function assert(check, msg) {
|
||||||
|
if (!check) throw msg + new Error().stack;
|
||||||
|
}
|
||||||
|
// {{{create_file_paths}}}
|
||||||
|
|
||||||
|
function DataRequest(start, end, crunched, audio) {
|
||||||
|
this.start = start;
|
||||||
|
this.end = end;
|
||||||
|
this.crunched = crunched;
|
||||||
|
this.audio = audio;
|
||||||
|
}
|
||||||
|
DataRequest.prototype = {
|
||||||
|
requests: {},
|
||||||
|
open: function(mode, name) {
|
||||||
|
this.name = name;
|
||||||
|
this.requests[name] = this;
|
||||||
|
Module['addRunDependency']('fp ' + this.name);
|
||||||
|
},
|
||||||
|
send: function() {},
|
||||||
|
onload: function() {
|
||||||
|
var byteArray = this.byteArray.subarray(this.start, this.end);
|
||||||
|
|
||||||
|
this.finish(byteArray);
|
||||||
|
|
||||||
|
},
|
||||||
|
finish: function(byteArray) {
|
||||||
|
var that = this;
|
||||||
|
|
||||||
|
Module['FS_createDataFile'](this.name, null, byteArray, true, true, true); // canOwn this data in the filesystem, it is a slide into the heap that will never change
|
||||||
|
Module['removeRunDependency']('fp ' + that.name);
|
||||||
|
|
||||||
|
this.requests[this.name] = null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var files = metadata.files;
|
||||||
|
for (i = 0; i < files.length; ++i) {
|
||||||
|
new DataRequest(files[i].start, files[i].end, files[i].crunched, files[i].audio).open('GET', files[i].filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
var indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
|
||||||
|
var IDB_RO = "readonly";
|
||||||
|
var IDB_RW = "readwrite";
|
||||||
|
var DB_NAME = "EM_PRELOAD_CACHE";
|
||||||
|
var DB_VERSION = 1;
|
||||||
|
var METADATA_STORE_NAME = 'METADATA';
|
||||||
|
var PACKAGE_STORE_NAME = 'PACKAGES';
|
||||||
|
function openDatabase(callback, errback) {
|
||||||
|
try {
|
||||||
|
var openRequest = indexedDB.open(DB_NAME, DB_VERSION);
|
||||||
|
} catch (e) {
|
||||||
|
return errback(e);
|
||||||
|
}
|
||||||
|
openRequest.onupgradeneeded = function(event) {
|
||||||
|
var db = event.target.result;
|
||||||
|
|
||||||
|
if(db.objectStoreNames.contains(PACKAGE_STORE_NAME)) {
|
||||||
|
db.deleteObjectStore(PACKAGE_STORE_NAME);
|
||||||
|
}
|
||||||
|
var packages = db.createObjectStore(PACKAGE_STORE_NAME);
|
||||||
|
|
||||||
|
if(db.objectStoreNames.contains(METADATA_STORE_NAME)) {
|
||||||
|
db.deleteObjectStore(METADATA_STORE_NAME);
|
||||||
|
}
|
||||||
|
var metadata = db.createObjectStore(METADATA_STORE_NAME);
|
||||||
|
};
|
||||||
|
openRequest.onsuccess = function(event) {
|
||||||
|
var db = event.target.result;
|
||||||
|
callback(db);
|
||||||
|
};
|
||||||
|
openRequest.onerror = function(error) {
|
||||||
|
errback(error);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
/* Check if there's a cached package, and if so whether it's the latest available */
|
||||||
|
function checkCachedPackage(db, packageName, callback, errback) {
|
||||||
|
var transaction = db.transaction([METADATA_STORE_NAME], IDB_RO);
|
||||||
|
var metadata = transaction.objectStore(METADATA_STORE_NAME);
|
||||||
|
|
||||||
|
var getRequest = metadata.get("metadata/" + packageName);
|
||||||
|
getRequest.onsuccess = function(event) {
|
||||||
|
var result = event.target.result;
|
||||||
|
if (!result) {
|
||||||
|
return callback(false);
|
||||||
|
} else {
|
||||||
|
return callback(PACKAGE_UUID === result.uuid);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
getRequest.onerror = function(error) {
|
||||||
|
errback(error);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
function fetchCachedPackage(db, packageName, callback, errback) {
|
||||||
|
var transaction = db.transaction([PACKAGE_STORE_NAME], IDB_RO);
|
||||||
|
var packages = transaction.objectStore(PACKAGE_STORE_NAME);
|
||||||
|
|
||||||
|
var getRequest = packages.get("package/" + packageName);
|
||||||
|
getRequest.onsuccess = function(event) {
|
||||||
|
var result = event.target.result;
|
||||||
|
callback(result);
|
||||||
|
};
|
||||||
|
getRequest.onerror = function(error) {
|
||||||
|
errback(error);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
function cacheRemotePackage(db, packageName, packageData, packageMeta, callback, errback) {
|
||||||
|
var transaction_packages = db.transaction([PACKAGE_STORE_NAME], IDB_RW);
|
||||||
|
var packages = transaction_packages.objectStore(PACKAGE_STORE_NAME);
|
||||||
|
|
||||||
|
var putPackageRequest = packages.put(packageData, "package/" + packageName);
|
||||||
|
putPackageRequest.onsuccess = function(event) {
|
||||||
|
var transaction_metadata = db.transaction([METADATA_STORE_NAME], IDB_RW);
|
||||||
|
var metadata = transaction_metadata.objectStore(METADATA_STORE_NAME);
|
||||||
|
var putMetadataRequest = metadata.put(packageMeta, "metadata/" + packageName);
|
||||||
|
putMetadataRequest.onsuccess = function(event) {
|
||||||
|
callback(packageData);
|
||||||
|
};
|
||||||
|
putMetadataRequest.onerror = function(error) {
|
||||||
|
errback(error);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
putPackageRequest.onerror = function(error) {
|
||||||
|
errback(error);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
function processPackageData(arrayBuffer) {
|
||||||
|
Module.finishedDataFileDownloads++;
|
||||||
|
assert(arrayBuffer, 'Loading data file failed.');
|
||||||
|
assert(arrayBuffer instanceof ArrayBuffer, 'bad input to processPackageData');
|
||||||
|
var byteArray = new Uint8Array(arrayBuffer);
|
||||||
|
var curr;
|
||||||
|
|
||||||
|
// copy the entire loaded file into a spot in the heap. Files will refer to slices in that. They cannot be freed though
|
||||||
|
// (we may be allocating before malloc is ready, during startup).
|
||||||
|
if (Module['SPLIT_MEMORY']) Module.printErr('warning: you should run the file packager with --no-heap-copy when SPLIT_MEMORY is used, otherwise copying into the heap may fail due to the splitting');
|
||||||
|
var ptr = Module['getMemory'](byteArray.length);
|
||||||
|
Module['HEAPU8'].set(byteArray, ptr);
|
||||||
|
DataRequest.prototype.byteArray = Module['HEAPU8'].subarray(ptr, ptr+byteArray.length);
|
||||||
|
|
||||||
|
var files = metadata.files;
|
||||||
|
for (i = 0; i < files.length; ++i) {
|
||||||
|
DataRequest.prototype.requests[files[i].filename].onload();
|
||||||
|
}
|
||||||
|
Module['removeRunDependency']('datafile_game.data');
|
||||||
|
|
||||||
|
};
|
||||||
|
Module['addRunDependency']('datafile_game.data');
|
||||||
|
|
||||||
|
if (!Module.preloadResults) Module.preloadResults = {};
|
||||||
|
|
||||||
|
function preloadFallback(error) {
|
||||||
|
console.error(error);
|
||||||
|
console.error('falling back to default preload behavior');
|
||||||
|
fetchRemotePackage(REMOTE_PACKAGE_NAME, REMOTE_PACKAGE_SIZE, processPackageData, handleError);
|
||||||
|
};
|
||||||
|
|
||||||
|
openDatabase(
|
||||||
|
function(db) {
|
||||||
|
checkCachedPackage(db, PACKAGE_PATH + PACKAGE_NAME,
|
||||||
|
function(useCached) {
|
||||||
|
Module.preloadResults[PACKAGE_NAME] = {fromCache: useCached};
|
||||||
|
if (useCached && CACHEMODULE) {
|
||||||
|
console.info('loading ' + PACKAGE_NAME + ' from cache');
|
||||||
|
fetchCachedPackage(db, PACKAGE_PATH + PACKAGE_NAME, processPackageData, preloadFallback);
|
||||||
|
} else {
|
||||||
|
console.info('loading ' + PACKAGE_NAME + ' from remote');
|
||||||
|
fetchRemotePackage(REMOTE_PACKAGE_NAME, REMOTE_PACKAGE_SIZE,
|
||||||
|
function(packageData) {
|
||||||
|
if(CACHEMODULE){
|
||||||
|
cacheRemotePackage(db, PACKAGE_PATH + PACKAGE_NAME, packageData, {uuid:PACKAGE_UUID}, processPackageData,
|
||||||
|
function(error) {
|
||||||
|
console.error(error);
|
||||||
|
processPackageData(packageData);
|
||||||
|
});}
|
||||||
|
else {
|
||||||
|
processPackageData(packageData);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
, preloadFallback);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
, preloadFallback);
|
||||||
|
}
|
||||||
|
, preloadFallback);
|
||||||
|
|
||||||
|
if (Module['setStatus']) Module['setStatus']('Downloading...');
|
||||||
|
|
||||||
|
}
|
||||||
|
if (Module['calledRun']) {
|
||||||
|
runWithFS();
|
||||||
|
} else {
|
||||||
|
if (!Module['preRun']) Module['preRun'] = [];
|
||||||
|
Module["preRun"].push(runWithFS); // FS is not initialized yet, wait for it
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
loadPackage({{{metadata}}});
|
||||||
|
|
||||||
|
})();
|
162
buildtools/love-js/src/index.html
Normal file
|
@ -0,0 +1,162 @@
|
||||||
|
<!doctype html>
|
||||||
|
<html lang="en-us">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
||||||
|
<title>{{title}}</title>
|
||||||
|
<script src = "consolewrapper.js"></script>
|
||||||
|
<!-- Load custom style sheet -->
|
||||||
|
<link rel="stylesheet" type="text/css" href="theme/love.css">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<center>
|
||||||
|
<div>
|
||||||
|
<canvas id="canvas" oncontextmenu="event.preventDefault()"></canvas>
|
||||||
|
<canvas id="loadingCanvas" oncontextmenu="event.preventDefault()"
|
||||||
|
width="{{width}}" height="{{height}}"></canvas>
|
||||||
|
</div>
|
||||||
|
</center>
|
||||||
|
|
||||||
|
<script type='text/javascript'>
|
||||||
|
function goFullScreen(){
|
||||||
|
var canvas = document.getElementById("canvas");
|
||||||
|
if(canvas.requestFullScreen)
|
||||||
|
canvas.requestFullScreen();
|
||||||
|
else if(canvas.webkitRequestFullScreen)
|
||||||
|
canvas.webkitRequestFullScreen();
|
||||||
|
else if(canvas.mozRequestFullScreen)
|
||||||
|
canvas.mozRequestFullScreen();
|
||||||
|
}
|
||||||
|
|
||||||
|
function closeFullScreen() {
|
||||||
|
if (document.exitFullscreen) {
|
||||||
|
document.exitFullscreen();
|
||||||
|
} else if (document.webkitExitFullscreen) { /* Safari */
|
||||||
|
document.webkitExitFullscreen();
|
||||||
|
} else if (document.msExitFullscreen) { /* IE11 */
|
||||||
|
document.msExitFullscreen();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function toggleFullScreen(){
|
||||||
|
if((window.fullScreen) || /* firefox */
|
||||||
|
(window.innerWidth == screen.width && /* everything else */
|
||||||
|
window.innerHeight == screen.height)) {
|
||||||
|
closeFullScreen();
|
||||||
|
} else {
|
||||||
|
goFullScreen();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var loadingContext = document.getElementById('loadingCanvas').getContext('2d');
|
||||||
|
var complete = false;
|
||||||
|
function drawLoadingText(text, soFar, total) {
|
||||||
|
var canvas = loadingContext.canvas;
|
||||||
|
var ratio = complete ? 1 : 0;
|
||||||
|
if (soFar && total){
|
||||||
|
ratio = soFar / total
|
||||||
|
}
|
||||||
|
if (ratio == 1){
|
||||||
|
complete = true
|
||||||
|
}
|
||||||
|
//
|
||||||
|
loadingContext.fillStyle = "rgb({{canvas-colour}})";
|
||||||
|
loadingContext.fillRect(0, 0, canvas.scrollWidth, canvas.scrollHeight);
|
||||||
|
|
||||||
|
loadingContext.font = '2em arial';
|
||||||
|
loadingContext.textAlign = 'center'
|
||||||
|
//
|
||||||
|
loadingContext.fillStyle = "rgb({{text-colour}})";
|
||||||
|
loadingContext.fillText(text, canvas.scrollWidth / 2, (canvas.scrollHeight / 2) - 40);
|
||||||
|
//
|
||||||
|
loadingContext.beginPath();
|
||||||
|
loadingContext.strokeStyle = "rgb({{text-colour}})";
|
||||||
|
loadingContext.rect((canvas.scrollWidth / 2) - 200,
|
||||||
|
( canvas.scrollHeight / 2) - 20,
|
||||||
|
400,
|
||||||
|
40
|
||||||
|
|
||||||
|
|
||||||
|
);
|
||||||
|
loadingContext.stroke();
|
||||||
|
loadingContext.beginPath();
|
||||||
|
loadingContext.rect((canvas.scrollWidth / 2) - 200,
|
||||||
|
( canvas.scrollHeight / 2) - 20,
|
||||||
|
400 * ratio,
|
||||||
|
40
|
||||||
|
|
||||||
|
|
||||||
|
);
|
||||||
|
loadingContext.fill();
|
||||||
|
loadingContext.font = '4em arial';
|
||||||
|
loadingContext.fillText("{{title}}", canvas.scrollWidth / 2, canvas.scrollHeight / 4);
|
||||||
|
loadingContext.font = '2em arial';
|
||||||
|
loadingContext.fillText("{{version}}", canvas.scrollWidth / 2, (canvas.scrollHeight / 4 + 45));
|
||||||
|
loadingContext.font = '1em arial';
|
||||||
|
loadingContext.textAlign = 'left'
|
||||||
|
loadingContext.fillText("Powered By LÖVE", canvas.scrollWidth / 2 - 300, canvas.scrollHeight / 4 * 3);
|
||||||
|
loadingContext.textAlign = 'right'
|
||||||
|
loadingContext.fillText("Game By: {{author}}", canvas.scrollWidth / 2 + 300, canvas.scrollHeight / 4 * 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
window.onload = function () { window.focus(); };
|
||||||
|
window.onclick = function () { window.focus(); };
|
||||||
|
|
||||||
|
window.addEventListener("keydown", function(e) {
|
||||||
|
// space and arrow keys
|
||||||
|
if([32, 37, 38, 39, 40].indexOf(e.keyCode) > -1) {
|
||||||
|
e.preventDefault();
|
||||||
|
}
|
||||||
|
}, false);
|
||||||
|
|
||||||
|
var Module = {
|
||||||
|
arguments: ["game.love","web"],
|
||||||
|
INITIAL_MEMORY: {{initial-memory}},
|
||||||
|
printErr: console.error.bind(console),
|
||||||
|
canvas: (function() {
|
||||||
|
var canvas = document.getElementById('canvas');
|
||||||
|
|
||||||
|
// As a default initial behavior, pop up an alert when webgl context is lost. To make your
|
||||||
|
// application robust, you may want to override this behavior before shipping!
|
||||||
|
// See http://www.khronos.org/registry/webgl/specs/latest/1.0/#5.15.2
|
||||||
|
canvas.addEventListener("webglcontextlost", function(e) { alert('WebGL context lost. You will need to reload the page.'); e.preventDefault(); }, false);
|
||||||
|
|
||||||
|
return canvas;
|
||||||
|
})(),
|
||||||
|
setStatus: function(text, soFar, total) {
|
||||||
|
if (text) {
|
||||||
|
drawLoadingText(text, soFar, total);
|
||||||
|
} else if (Module.remainingDependencies === 0) {
|
||||||
|
document.getElementById('loadingCanvas').style.display = 'none';
|
||||||
|
document.getElementById('canvas').style.display = 'block';
|
||||||
|
}
|
||||||
|
},
|
||||||
|
totalDependencies: 0,
|
||||||
|
remainingDependencies: 0,
|
||||||
|
monitorRunDependencies: function(left) {
|
||||||
|
this.remainingDependencies = left;
|
||||||
|
this.totalDependencies = Math.max(this.totalDependencies, left);
|
||||||
|
Module.setStatus(left ? 'Preparing... (' + (this.totalDependencies-left) + '/' + this.totalDependencies + ')' : 'All downloads complete.',
|
||||||
|
this.totalDependencies-left,
|
||||||
|
this.totalDependencies);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Module.setStatus('Downloading...');
|
||||||
|
window.onerror = function(event) {
|
||||||
|
// TODO: do not warn on ok events like simulating an infinite loop or exitStatus
|
||||||
|
Module.setStatus('Exception thrown, see JavaScript console');
|
||||||
|
Module.setStatus = function(text) {
|
||||||
|
if (text) Module.printErr('[post-exception status] ' + text);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
var applicationLoad = function(e) {
|
||||||
|
Love(Module);
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
<script type="text/javascript" src="game.js"></script>
|
||||||
|
<script async type="text/javascript" src="love.js" onload="applicationLoad(this)"></script>
|
||||||
|
<!-- <footer> -->
|
||||||
|
<!-- <button onclick="goFullScreen();">Go Fullscreen</button> -->
|
||||||
|
<!-- </footer> -->
|
||||||
|
</body>
|
||||||
|
</html>
|
22
buildtools/love-js/src/love-11.3/compat/love.js
Normal file
BIN
buildtools/love-js/src/love-11.3/compat/love.wasm
Normal file
22
buildtools/love-js/src/love-11.3/release/love.js
Normal file
BIN
buildtools/love-js/src/love-11.3/release/love.wasm
Normal file
1
buildtools/love-js/src/love-11.3/release/love.worker.js
Normal file
|
@ -0,0 +1 @@
|
||||||
|
var threadInfoStruct=0;var selfThreadId=0;var parentThreadId=0;var Module={};function threadPrintErr(){var text=Array.prototype.slice.call(arguments).join(" ");console.error(text)}function threadAlert(){var text=Array.prototype.slice.call(arguments).join(" ");postMessage({cmd:"alert",text:text,threadId:selfThreadId})}var err=threadPrintErr;this.alert=threadAlert;Module["instantiateWasm"]=function(info,receiveInstance){var instance=new WebAssembly.Instance(Module["wasmModule"],info);Module["wasmModule"]=null;receiveInstance(instance);return instance.exports};this.onmessage=function(e){try{if(e.data.cmd==="load"){Module["DYNAMIC_BASE"]=e.data.DYNAMIC_BASE;Module["DYNAMICTOP_PTR"]=e.data.DYNAMICTOP_PTR;Module["wasmModule"]=e.data.wasmModule;Module["wasmMemory"]=e.data.wasmMemory;Module["buffer"]=Module["wasmMemory"].buffer;Module["ENVIRONMENT_IS_PTHREAD"]=true;if(typeof e.data.urlOrBlob==="string"){importScripts(e.data.urlOrBlob)}else{var objectUrl=URL.createObjectURL(e.data.urlOrBlob);importScripts(objectUrl);URL.revokeObjectURL(objectUrl)}Love(Module).then(function(instance){Module=instance;postMessage({"cmd":"loaded"})})}else if(e.data.cmd==="objectTransfer"){Module["PThread"].receiveObjectTransfer(e.data)}else if(e.data.cmd==="run"){Module["__performance_now_clock_drift"]=performance.now()-e.data.time;threadInfoStruct=e.data.threadInfoStruct;Module["registerPthreadPtr"](threadInfoStruct,0,0);selfThreadId=e.data.selfThreadId;parentThreadId=e.data.parentThreadId;var max=e.data.stackBase;var top=e.data.stackBase+e.data.stackSize;Module["establishStackSpace"](top,max);Module["_emscripten_tls_init"]();Module["PThread"].receiveObjectTransfer(e.data);Module["PThread"].setThreadStatus(Module["_pthread_self"](),1);try{var result=Module["dynCall"]("ii",e.data.start_routine,[e.data.arg]);if(!Module["getNoExitRuntime"]())Module["PThread"].threadExit(result)}catch(ex){if(ex==="Canceled!"){Module["PThread"].threadCancel()}else if(ex!="unwind"){Atomics.store(Module["HEAPU32"],threadInfoStruct+4>>2,ex instanceof Module["ExitStatus"]?ex.status:-2);Atomics.store(Module["HEAPU32"],threadInfoStruct+0>>2,1);Module["_emscripten_futex_wake"](threadInfoStruct+0,2147483647);if(!(ex instanceof Module["ExitStatus"]))throw ex}}}else if(e.data.cmd==="cancel"){if(threadInfoStruct){Module["PThread"].threadCancel()}}else if(e.data.target==="setimmediate"){}else if(e.data.cmd==="processThreadQueue"){if(threadInfoStruct){Module["_emscripten_current_thread_process_queued_calls"]()}}else{err("worker.js received unknown command "+e.data.cmd);err(e.data)}}catch(ex){err("worker.js onmessage() captured an uncaught exception: "+ex);if(ex.stack)err(ex.stack);throw ex}};if(typeof process==="object"&&typeof process.versions==="object"&&typeof process.versions.node==="string"){self={location:{href:__filename}};var onmessage=this.onmessage;var nodeWorkerThreads=require("worker_threads");global.Worker=nodeWorkerThreads.Worker;var parentPort=nodeWorkerThreads.parentPort;parentPort.on("message",function(data){onmessage({data:data})});var nodeFS=require("fs");var nodeRead=function(filename){return nodeFS.readFileSync(filename,"utf8")};function globalEval(x){global.require=require;global.Module=Module;eval.call(null,x)}importScripts=function(f){globalEval(nodeRead(f))};postMessage=function(msg){parentPort.postMessage(msg)};if(typeof performance==="undefined"){performance={now:function(){return Date.now()}}}}
|
22
buildtools/love-js/src/love-11.4/compat/love.js
Normal file
BIN
buildtools/love-js/src/love-11.4/compat/love.wasm
Normal file
22
buildtools/love-js/src/love-11.4/release/love.js
Normal file
BIN
buildtools/love-js/src/love-11.4/release/love.wasm
Normal file
1
buildtools/love-js/src/love-11.4/release/love.worker.js
Normal file
|
@ -0,0 +1 @@
|
||||||
|
var threadInfoStruct=0;var selfThreadId=0;var parentThreadId=0;var Module={};function threadPrintErr(){var text=Array.prototype.slice.call(arguments).join(" ");console.error(text)}function threadAlert(){var text=Array.prototype.slice.call(arguments).join(" ");postMessage({cmd:"alert",text:text,threadId:selfThreadId})}var err=threadPrintErr;this.alert=threadAlert;Module["instantiateWasm"]=function(info,receiveInstance){var instance=new WebAssembly.Instance(Module["wasmModule"],info);Module["wasmModule"]=null;receiveInstance(instance);return instance.exports};this.onmessage=function(e){try{if(e.data.cmd==="load"){Module["DYNAMIC_BASE"]=e.data.DYNAMIC_BASE;Module["DYNAMICTOP_PTR"]=e.data.DYNAMICTOP_PTR;Module["wasmModule"]=e.data.wasmModule;Module["wasmMemory"]=e.data.wasmMemory;Module["buffer"]=Module["wasmMemory"].buffer;Module["ENVIRONMENT_IS_PTHREAD"]=true;if(typeof e.data.urlOrBlob==="string"){importScripts(e.data.urlOrBlob)}else{var objectUrl=URL.createObjectURL(e.data.urlOrBlob);importScripts(objectUrl);URL.revokeObjectURL(objectUrl)}Love(Module).then(function(instance){Module=instance;postMessage({"cmd":"loaded"})})}else if(e.data.cmd==="objectTransfer"){Module["PThread"].receiveObjectTransfer(e.data)}else if(e.data.cmd==="run"){Module["__performance_now_clock_drift"]=performance.now()-e.data.time;threadInfoStruct=e.data.threadInfoStruct;Module["registerPthreadPtr"](threadInfoStruct,0,0);selfThreadId=e.data.selfThreadId;parentThreadId=e.data.parentThreadId;var max=e.data.stackBase;var top=e.data.stackBase+e.data.stackSize;Module["establishStackSpace"](top,max);Module["_emscripten_tls_init"]();Module["PThread"].receiveObjectTransfer(e.data);Module["PThread"].setThreadStatus(Module["_pthread_self"](),1);try{var result=Module["dynCall_ii"](e.data.start_routine,e.data.arg);if(!Module["getNoExitRuntime"]())Module["PThread"].threadExit(result)}catch(ex){if(ex==="Canceled!"){Module["PThread"].threadCancel()}else if(ex!="unwind"){Atomics.store(Module["HEAPU32"],threadInfoStruct+4>>2,ex instanceof Module["ExitStatus"]?ex.status:-2);Atomics.store(Module["HEAPU32"],threadInfoStruct+0>>2,1);Module["_emscripten_futex_wake"](threadInfoStruct+0,2147483647);if(!(ex instanceof Module["ExitStatus"]))throw ex}}}else if(e.data.cmd==="cancel"){if(threadInfoStruct){Module["PThread"].threadCancel()}}else if(e.data.target==="setimmediate"){}else if(e.data.cmd==="processThreadQueue"){if(threadInfoStruct){Module["_emscripten_current_thread_process_queued_calls"]()}}else{err("worker.js received unknown command "+e.data.cmd);err(e.data)}}catch(ex){err("worker.js onmessage() captured an uncaught exception: "+ex);if(ex.stack)err(ex.stack);throw ex}};if(typeof process==="object"&&typeof process.versions==="object"&&typeof process.versions.node==="string"){self={location:{href:__filename}};var onmessage=this.onmessage;var nodeWorkerThreads=require("worker_threads");global.Worker=nodeWorkerThreads.Worker;var parentPort=nodeWorkerThreads.parentPort;parentPort.on("message",function(data){onmessage({data:data})});var nodeFS=require("fs");var nodeRead=function(filename){return nodeFS.readFileSync(filename,"utf8")};function globalEval(x){global.require=require;global.Module=Module;eval.call(null,x)}importScripts=function(f){globalEval(nodeRead(f))};postMessage=function(msg){parentPort.postMessage(msg)};if(typeof performance==="undefined"){performance={now:function(){return Date.now()}}}}
|
18
buildtools/love-js/src/love-11.5/compat/love.js
Normal file
BIN
buildtools/love-js/src/love-11.5/compat/love.wasm
Normal file
18
buildtools/love-js/src/love-11.5/release/love.js
Normal file
BIN
buildtools/love-js/src/love-11.5/release/love.wasm
Normal file
1
buildtools/love-js/src/love-11.5/release/love.worker.js
Normal file
|
@ -0,0 +1 @@
|
||||||
|
"use strict";var Module={};var ENVIRONMENT_IS_NODE=typeof process=="object"&&typeof process.versions=="object"&&typeof process.versions.node=="string";if(ENVIRONMENT_IS_NODE){var nodeWorkerThreads=require("worker_threads");var parentPort=nodeWorkerThreads.parentPort;parentPort.on("message",data=>onmessage({data:data}));var fs=require("fs");var vm=require("vm");Object.assign(global,{self:global,require:require,Module:Module,location:{href:__filename},Worker:nodeWorkerThreads.Worker,importScripts:f=>vm.runInThisContext(fs.readFileSync(f,"utf8"),{filename:f}),postMessage:msg=>parentPort.postMessage(msg),performance:global.performance||{now:Date.now}})}var initializedJS=false;function threadPrintErr(...args){var text=args.join(" ");if(ENVIRONMENT_IS_NODE){fs.writeSync(2,text+"\n");return}console.error(text)}function threadAlert(...args){var text=args.join(" ");postMessage({cmd:"alert",text:text,threadId:Module["_pthread_self"]()})}var err=threadPrintErr;self.alert=threadAlert;Module["instantiateWasm"]=(info,receiveInstance)=>{var module=Module["wasmModule"];Module["wasmModule"]=null;var instance=new WebAssembly.Instance(module,info);return receiveInstance(instance,module)};self.onunhandledrejection=e=>{throw e.reason||e};function handleMessage(e){try{if(e.data.cmd==="load"){let messageQueue=[];self.onmessage=e=>messageQueue.push(e);self.startWorker=instance=>{Module=instance;postMessage({"cmd":"loaded"});for(let msg of messageQueue){handleMessage(msg)}self.onmessage=handleMessage};Module["wasmModule"]=e.data.wasmModule;for(const handler of e.data.handlers){Module[handler]=(...args)=>{postMessage({cmd:"callHandler",handler:handler,args:args})}}Module["wasmMemory"]=e.data.wasmMemory;Module["buffer"]=Module["wasmMemory"].buffer;Module["ENVIRONMENT_IS_PTHREAD"]=true;if(typeof e.data.urlOrBlob=="string"){importScripts(e.data.urlOrBlob)}else{var objectUrl=URL.createObjectURL(e.data.urlOrBlob);importScripts(objectUrl);URL.revokeObjectURL(objectUrl)}Love(Module)}else if(e.data.cmd==="run"){Module["__emscripten_thread_init"](e.data.pthread_ptr,0,0,1);Module["__emscripten_thread_mailbox_await"](e.data.pthread_ptr);Module["establishStackSpace"]();Module["PThread"].receiveObjectTransfer(e.data);Module["PThread"].threadInitTLS();if(!initializedJS){initializedJS=true}try{Module["invokeEntryPoint"](e.data.start_routine,e.data.arg)}catch(ex){if(ex!="unwind"){throw ex}}}else if(e.data.cmd==="cancel"){if(Module["_pthread_self"]()){Module["__emscripten_thread_exit"](-1)}}else if(e.data.target==="setimmediate"){}else if(e.data.cmd==="checkMailbox"){if(initializedJS){Module["checkMailbox"]()}}else if(e.data.cmd){err(`worker.js received unknown command ${e.data.cmd}`);err(e.data)}}catch(ex){Module["__emscripten_thread_crashed"]?.();throw ex}}self.onmessage=handleMessage;
|
48
buildtools/love-js/src/love.css
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
* {
|
||||||
|
box-sizing: border-box;
|
||||||
|
}
|
||||||
|
|
||||||
|
h1 {
|
||||||
|
font-family: arial;
|
||||||
|
color: rgb( 11, 86, 117 );
|
||||||
|
}
|
||||||
|
|
||||||
|
body {
|
||||||
|
background-repeat: no-repeat;
|
||||||
|
font-family: arial;
|
||||||
|
margin: 0;
|
||||||
|
padding: none;
|
||||||
|
background-color: rgb({{page-colour}});
|
||||||
|
color: rgb( 28, 78, 104 );
|
||||||
|
}
|
||||||
|
|
||||||
|
footer {
|
||||||
|
font-family: arial;
|
||||||
|
font-size: 12px;
|
||||||
|
padding-left: 10px;
|
||||||
|
padding-bottom: 10px;
|
||||||
|
position:absolute;
|
||||||
|
bottom: 0;
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Links */
|
||||||
|
a {
|
||||||
|
text-decoration: none;
|
||||||
|
}
|
||||||
|
a:link {
|
||||||
|
color: rgb( 233, 73, 154 );
|
||||||
|
}
|
||||||
|
a:visited {
|
||||||
|
color: rgb( 110, 30, 71 );
|
||||||
|
}
|
||||||
|
a:hover {
|
||||||
|
color: rgb( 252, 207, 230 );
|
||||||
|
}
|
||||||
|
|
||||||
|
/* the canvas *must not* have any border or padding, or mouse coords will be wrong */
|
||||||
|
#canvas {
|
||||||
|
padding-right: 0;
|
||||||
|
display: none;
|
||||||
|
border: 0px none;
|
||||||
|
}
|
22
buildtools/love-js/src/serve.py
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
# Attribution: https://stackoverflow.com/questions/21956683/enable-access-control-on-simple-http-server
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Python 3
|
||||||
|
from http.server import HTTPServer, SimpleHTTPRequestHandler, test as test_orig
|
||||||
|
import sys
|
||||||
|
def test (*args):
|
||||||
|
test_orig(*args, port=int(sys.argv[1]) if len(sys.argv) > 1 else 8000)
|
||||||
|
except ImportError: # Python 2
|
||||||
|
from BaseHTTPServer import HTTPServer, test
|
||||||
|
from SimpleHTTPServer import SimpleHTTPRequestHandler
|
||||||
|
|
||||||
|
class CORSRequestHandler (SimpleHTTPRequestHandler):
|
||||||
|
def end_headers (self):
|
||||||
|
self.send_header('Cross-Origin-Opener-Policy', 'same-origin')
|
||||||
|
self.send_header('Cross-Origin-Embedder-Policy', 'require-corp')
|
||||||
|
SimpleHTTPRequestHandler.end_headers(self)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
test(CORSRequestHandler, HTTPServer)
|
1053
buildtools/love-release.sh
Executable file
10
conf.lua
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
love.conf = function(t)
|
||||||
|
t.gammacorrect = true
|
||||||
|
t.title, t.identity = "Space Crawler", "Bill Niblock"
|
||||||
|
t.modules.joystick = false
|
||||||
|
t.modules.physics = false
|
||||||
|
t.window.width = 1280
|
||||||
|
t.window.height = 720
|
||||||
|
t.window.vsync = false
|
||||||
|
t.version = "11.5"
|
||||||
|
end
|
41
error-mode.fnl
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
;; This mode has two purposes:
|
||||||
|
;; * display the stack trace that caused the error
|
||||||
|
;; * allow the user to decide whether to retry after reloading or quit
|
||||||
|
|
||||||
|
;; Since we can't know which module needs to be reloaded, we rely on the user
|
||||||
|
;; doing a ,reload foo in the repl.
|
||||||
|
|
||||||
|
(local state {:msg "" :traceback "" :old-mode :intro})
|
||||||
|
|
||||||
|
(local explanation "Press escape to quit.
|
||||||
|
Press space to return to the previous mode after reloading in the repl.")
|
||||||
|
|
||||||
|
(fn draw []
|
||||||
|
(love.graphics.clear 0.34 0.61 0.86)
|
||||||
|
(love.graphics.setColor 0.9 0.9 0.9)
|
||||||
|
(love.graphics.print explanation 15 10)
|
||||||
|
(love.graphics.print state.msg 10 60)
|
||||||
|
(love.graphics.print state.traceback 15 125))
|
||||||
|
|
||||||
|
(fn keypressed [key set-mode]
|
||||||
|
(match key
|
||||||
|
:escape (love.event.quit)
|
||||||
|
:space (set-mode state.old-mode)))
|
||||||
|
|
||||||
|
(fn color-msg [msg]
|
||||||
|
;; convert compiler's ansi escape codes to love2d-friendly codes
|
||||||
|
(case (msg:match "(.*)\027%[7m(.*)\027%[0m(.*)")
|
||||||
|
(pre selected post) [[1 1 1] pre
|
||||||
|
[1 0.2 0.2] selected
|
||||||
|
[1 1 1] post]
|
||||||
|
_ msg))
|
||||||
|
|
||||||
|
(fn activate [old-mode msg traceback]
|
||||||
|
(love.graphics.setNewFont 16) ; use a monospace font here if you have one
|
||||||
|
(print msg)
|
||||||
|
(print traceback)
|
||||||
|
(set state.old-mode old-mode)
|
||||||
|
(set state.msg (color-msg msg))
|
||||||
|
(set state.traceback traceback))
|
||||||
|
|
||||||
|
{: draw : keypressed : activate}
|
6991
lib/fennel
Executable file
6468
lib/fennel.lua
Normal file
780
lib/lume.lua
Normal file
|
@ -0,0 +1,780 @@
|
||||||
|
--
|
||||||
|
-- lume
|
||||||
|
--
|
||||||
|
-- Copyright (c) 2018 rxi
|
||||||
|
--
|
||||||
|
-- Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||||
|
-- this software and associated documentation files (the "Software"), to deal in
|
||||||
|
-- the Software without restriction, including without limitation the rights to
|
||||||
|
-- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||||
|
-- of the Software, and to permit persons to whom the Software is furnished to do
|
||||||
|
-- so, subject to the following conditions:
|
||||||
|
--
|
||||||
|
-- The above copyright notice and this permission notice shall be included in all
|
||||||
|
-- copies or substantial portions of the Software.
|
||||||
|
--
|
||||||
|
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
-- SOFTWARE.
|
||||||
|
--
|
||||||
|
|
||||||
|
local lume = { _version = "2.3.0" }
|
||||||
|
|
||||||
|
local pairs, ipairs = pairs, ipairs
|
||||||
|
local type, assert, unpack = type, assert, unpack or table.unpack
|
||||||
|
local tostring, tonumber = tostring, tonumber
|
||||||
|
local math_floor = math.floor
|
||||||
|
local math_ceil = math.ceil
|
||||||
|
local math_atan2 = math.atan2 or math.atan
|
||||||
|
local math_sqrt = math.sqrt
|
||||||
|
local math_abs = math.abs
|
||||||
|
|
||||||
|
local noop = function()
|
||||||
|
end
|
||||||
|
|
||||||
|
local identity = function(x)
|
||||||
|
return x
|
||||||
|
end
|
||||||
|
|
||||||
|
local patternescape = function(str)
|
||||||
|
return str:gsub("[%(%)%.%%%+%-%*%?%[%]%^%$]", "%%%1")
|
||||||
|
end
|
||||||
|
|
||||||
|
local absindex = function(len, i)
|
||||||
|
return i < 0 and (len + i + 1) or i
|
||||||
|
end
|
||||||
|
|
||||||
|
local iscallable = function(x)
|
||||||
|
if type(x) == "function" then return true end
|
||||||
|
local mt = getmetatable(x)
|
||||||
|
return mt and mt.__call ~= nil
|
||||||
|
end
|
||||||
|
|
||||||
|
local getiter = function(x)
|
||||||
|
if lume.isarray(x) then
|
||||||
|
return ipairs
|
||||||
|
elseif type(x) == "table" then
|
||||||
|
return pairs
|
||||||
|
end
|
||||||
|
error("expected table", 3)
|
||||||
|
end
|
||||||
|
|
||||||
|
local iteratee = function(x)
|
||||||
|
if x == nil then return identity end
|
||||||
|
if iscallable(x) then return x end
|
||||||
|
if type(x) == "table" then
|
||||||
|
return function(z)
|
||||||
|
for k, v in pairs(x) do
|
||||||
|
if z[k] ~= v then return false end
|
||||||
|
end
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return function(z) return z[x] end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
function lume.clamp(x, min, max)
|
||||||
|
return x < min and min or (x > max and max or x)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.round(x, increment)
|
||||||
|
if increment then return lume.round(x / increment) * increment end
|
||||||
|
return x >= 0 and math_floor(x + .5) or math_ceil(x - .5)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.sign(x)
|
||||||
|
return x < 0 and -1 or 1
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.lerp(a, b, amount)
|
||||||
|
return a + (b - a) * lume.clamp(amount, 0, 1)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.smooth(a, b, amount)
|
||||||
|
local t = lume.clamp(amount, 0, 1)
|
||||||
|
local m = t * t * (3 - 2 * t)
|
||||||
|
return a + (b - a) * m
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.pingpong(x)
|
||||||
|
return 1 - math_abs(1 - x % 2)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.distance(x1, y1, x2, y2, squared)
|
||||||
|
local dx = x1 - x2
|
||||||
|
local dy = y1 - y2
|
||||||
|
local s = dx * dx + dy * dy
|
||||||
|
return squared and s or math_sqrt(s)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.angle(x1, y1, x2, y2)
|
||||||
|
return math_atan2(y2 - y1, x2 - x1)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.vector(angle, magnitude)
|
||||||
|
return math.cos(angle) * magnitude, math.sin(angle) * magnitude
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.random(a, b)
|
||||||
|
if not a then a, b = 0, 1 end
|
||||||
|
if not b then b = 0 end
|
||||||
|
return a + math.random() * (b - a)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.randomchoice(t)
|
||||||
|
return t[math.random(#t)]
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.weightedchoice(t)
|
||||||
|
local sum = 0
|
||||||
|
for _, v in pairs(t) do
|
||||||
|
assert(v >= 0, "weight value less than zero")
|
||||||
|
sum = sum + v
|
||||||
|
end
|
||||||
|
assert(sum ~= 0, "all weights are zero")
|
||||||
|
local rnd = lume.random(sum)
|
||||||
|
for k, v in pairs(t) do
|
||||||
|
if rnd < v then return k end
|
||||||
|
rnd = rnd - v
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.isarray(x)
|
||||||
|
return type(x) == "table" and x[1] ~= nil
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.push(t, ...)
|
||||||
|
local n = select("#", ...)
|
||||||
|
for i = 1, n do
|
||||||
|
t[#t + 1] = select(i, ...)
|
||||||
|
end
|
||||||
|
return ...
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.remove(t, x)
|
||||||
|
local iter = getiter(t)
|
||||||
|
for i, v in iter(t) do
|
||||||
|
if v == x then
|
||||||
|
if lume.isarray(t) then
|
||||||
|
table.remove(t, i)
|
||||||
|
break
|
||||||
|
else
|
||||||
|
t[i] = nil
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return x
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.clear(t)
|
||||||
|
local iter = getiter(t)
|
||||||
|
for k in iter(t) do
|
||||||
|
t[k] = nil
|
||||||
|
end
|
||||||
|
return t
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.extend(t, ...)
|
||||||
|
for i = 1, select("#", ...) do
|
||||||
|
local x = select(i, ...)
|
||||||
|
if x then
|
||||||
|
for k, v in pairs(x) do
|
||||||
|
t[k] = v
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return t
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.shuffle(t)
|
||||||
|
local rtn = {}
|
||||||
|
for i = 1, #t do
|
||||||
|
local r = math.random(i)
|
||||||
|
if r ~= i then
|
||||||
|
rtn[i] = rtn[r]
|
||||||
|
end
|
||||||
|
rtn[r] = t[i]
|
||||||
|
end
|
||||||
|
return rtn
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.sort(t, comp)
|
||||||
|
local rtn = lume.clone(t)
|
||||||
|
if comp then
|
||||||
|
if type(comp) == "string" then
|
||||||
|
table.sort(rtn, function(a, b) return a[comp] < b[comp] end)
|
||||||
|
else
|
||||||
|
table.sort(rtn, comp)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
table.sort(rtn)
|
||||||
|
end
|
||||||
|
return rtn
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.array(...)
|
||||||
|
local t = {}
|
||||||
|
for x in ... do t[#t + 1] = x end
|
||||||
|
return t
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.each(t, fn, ...)
|
||||||
|
local iter = getiter(t)
|
||||||
|
if type(fn) == "string" then
|
||||||
|
for _, v in iter(t) do v[fn](v, ...) end
|
||||||
|
else
|
||||||
|
for _, v in iter(t) do fn(v, ...) end
|
||||||
|
end
|
||||||
|
return t
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.map(t, fn)
|
||||||
|
fn = iteratee(fn)
|
||||||
|
local iter = getiter(t)
|
||||||
|
local rtn = {}
|
||||||
|
for k, v in iter(t) do rtn[k] = fn(v) end
|
||||||
|
return rtn
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.all(t, fn)
|
||||||
|
fn = iteratee(fn)
|
||||||
|
local iter = getiter(t)
|
||||||
|
for _, v in iter(t) do
|
||||||
|
if not fn(v) then return false end
|
||||||
|
end
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.any(t, fn)
|
||||||
|
fn = iteratee(fn)
|
||||||
|
local iter = getiter(t)
|
||||||
|
for _, v in iter(t) do
|
||||||
|
if fn(v) then return true end
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.reduce(t, fn, first)
|
||||||
|
local acc = first
|
||||||
|
local started = first and true or false
|
||||||
|
local iter = getiter(t)
|
||||||
|
for _, v in iter(t) do
|
||||||
|
if started then
|
||||||
|
acc = fn(acc, v)
|
||||||
|
else
|
||||||
|
acc = v
|
||||||
|
started = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
assert(started, "reduce of an empty table with no first value")
|
||||||
|
return acc
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.unique(t)
|
||||||
|
local rtn = {}
|
||||||
|
for k in pairs(lume.invert(t)) do
|
||||||
|
rtn[#rtn + 1] = k
|
||||||
|
end
|
||||||
|
return rtn
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.filter(t, fn, retainkeys)
|
||||||
|
fn = iteratee(fn)
|
||||||
|
local iter = getiter(t)
|
||||||
|
local rtn = {}
|
||||||
|
if retainkeys then
|
||||||
|
for k, v in iter(t) do
|
||||||
|
if fn(v) then rtn[k] = v end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
for _, v in iter(t) do
|
||||||
|
if fn(v) then rtn[#rtn + 1] = v end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return rtn
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.reject(t, fn, retainkeys)
|
||||||
|
fn = iteratee(fn)
|
||||||
|
local iter = getiter(t)
|
||||||
|
local rtn = {}
|
||||||
|
if retainkeys then
|
||||||
|
for k, v in iter(t) do
|
||||||
|
if not fn(v) then rtn[k] = v end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
for _, v in iter(t) do
|
||||||
|
if not fn(v) then rtn[#rtn + 1] = v end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return rtn
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.merge(...)
|
||||||
|
local rtn = {}
|
||||||
|
for i = 1, select("#", ...) do
|
||||||
|
local t = select(i, ...)
|
||||||
|
local iter = getiter(t)
|
||||||
|
for k, v in iter(t) do
|
||||||
|
rtn[k] = v
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return rtn
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.concat(...)
|
||||||
|
local rtn = {}
|
||||||
|
for i = 1, select("#", ...) do
|
||||||
|
local t = select(i, ...)
|
||||||
|
if t ~= nil then
|
||||||
|
local iter = getiter(t)
|
||||||
|
for _, v in iter(t) do
|
||||||
|
rtn[#rtn + 1] = v
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return rtn
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.find(t, value)
|
||||||
|
local iter = getiter(t)
|
||||||
|
for k, v in iter(t) do
|
||||||
|
if v == value then return k end
|
||||||
|
end
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.match(t, fn)
|
||||||
|
fn = iteratee(fn)
|
||||||
|
local iter = getiter(t)
|
||||||
|
for k, v in iter(t) do
|
||||||
|
if fn(v) then return v, k end
|
||||||
|
end
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.count(t, fn)
|
||||||
|
local count = 0
|
||||||
|
local iter = getiter(t)
|
||||||
|
if fn then
|
||||||
|
fn = iteratee(fn)
|
||||||
|
for _, v in iter(t) do
|
||||||
|
if fn(v) then count = count + 1 end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
if lume.isarray(t) then
|
||||||
|
return #t
|
||||||
|
end
|
||||||
|
for _ in iter(t) do count = count + 1 end
|
||||||
|
end
|
||||||
|
return count
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.slice(t, i, j)
|
||||||
|
i = i and absindex(#t, i) or 1
|
||||||
|
j = j and absindex(#t, j) or #t
|
||||||
|
local rtn = {}
|
||||||
|
for x = i < 1 and 1 or i, j > #t and #t or j do
|
||||||
|
rtn[#rtn + 1] = t[x]
|
||||||
|
end
|
||||||
|
return rtn
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.first(t, n)
|
||||||
|
if not n then return t[1] end
|
||||||
|
return lume.slice(t, 1, n)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.last(t, n)
|
||||||
|
if not n then return t[#t] end
|
||||||
|
return lume.slice(t, -n, -1)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.invert(t)
|
||||||
|
local rtn = {}
|
||||||
|
for k, v in pairs(t) do rtn[v] = k end
|
||||||
|
return rtn
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.pick(t, ...)
|
||||||
|
local rtn = {}
|
||||||
|
for i = 1, select("#", ...) do
|
||||||
|
local k = select(i, ...)
|
||||||
|
rtn[k] = t[k]
|
||||||
|
end
|
||||||
|
return rtn
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.keys(t)
|
||||||
|
local rtn = {}
|
||||||
|
local iter = getiter(t)
|
||||||
|
for k in iter(t) do rtn[#rtn + 1] = k end
|
||||||
|
return rtn
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.clone(t)
|
||||||
|
local rtn = {}
|
||||||
|
for k, v in pairs(t) do rtn[k] = v end
|
||||||
|
return rtn
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.fn(fn, ...)
|
||||||
|
assert(iscallable(fn), "expected a function as the first argument")
|
||||||
|
local args = { ... }
|
||||||
|
return function(...)
|
||||||
|
local a = lume.concat(args, { ... })
|
||||||
|
return fn(unpack(a))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.once(fn, ...)
|
||||||
|
local f = lume.fn(fn, ...)
|
||||||
|
local done = false
|
||||||
|
return function(...)
|
||||||
|
if done then return end
|
||||||
|
done = true
|
||||||
|
return f(...)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
local memoize_fnkey = {}
|
||||||
|
local memoize_nil = {}
|
||||||
|
|
||||||
|
function lume.memoize(fn)
|
||||||
|
local cache = {}
|
||||||
|
return function(...)
|
||||||
|
local c = cache
|
||||||
|
for i = 1, select("#", ...) do
|
||||||
|
local a = select(i, ...) or memoize_nil
|
||||||
|
c[a] = c[a] or {}
|
||||||
|
c = c[a]
|
||||||
|
end
|
||||||
|
c[memoize_fnkey] = c[memoize_fnkey] or {fn(...)}
|
||||||
|
return unpack(c[memoize_fnkey])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.combine(...)
|
||||||
|
local n = select('#', ...)
|
||||||
|
if n == 0 then return noop end
|
||||||
|
if n == 1 then
|
||||||
|
local fn = select(1, ...)
|
||||||
|
if not fn then return noop end
|
||||||
|
assert(iscallable(fn), "expected a function or nil")
|
||||||
|
return fn
|
||||||
|
end
|
||||||
|
local funcs = {}
|
||||||
|
for i = 1, n do
|
||||||
|
local fn = select(i, ...)
|
||||||
|
if fn ~= nil then
|
||||||
|
assert(iscallable(fn), "expected a function or nil")
|
||||||
|
funcs[#funcs + 1] = fn
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return function(...)
|
||||||
|
for _, f in ipairs(funcs) do f(...) end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.call(fn, ...)
|
||||||
|
if fn then
|
||||||
|
return fn(...)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.time(fn, ...)
|
||||||
|
local start = os.clock()
|
||||||
|
local rtn = {fn(...)}
|
||||||
|
return (os.clock() - start), unpack(rtn)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
local lambda_cache = {}
|
||||||
|
|
||||||
|
function lume.lambda(str)
|
||||||
|
if not lambda_cache[str] then
|
||||||
|
local args, body = str:match([[^([%w,_ ]-)%->(.-)$]])
|
||||||
|
assert(args and body, "bad string lambda")
|
||||||
|
local s = "return function(" .. args .. ")\nreturn " .. body .. "\nend"
|
||||||
|
lambda_cache[str] = lume.dostring(s)
|
||||||
|
end
|
||||||
|
return lambda_cache[str]
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
local serialize
|
||||||
|
|
||||||
|
local serialize_map = {
|
||||||
|
[ "boolean" ] = tostring,
|
||||||
|
[ "nil" ] = tostring,
|
||||||
|
[ "string" ] = function(v) return string.format("%q", v) end,
|
||||||
|
[ "number" ] = function(v)
|
||||||
|
if v ~= v then return "0/0" -- nan
|
||||||
|
elseif v == 1 / 0 then return "1/0" -- inf
|
||||||
|
elseif v == -1 / 0 then return "-1/0" end -- -inf
|
||||||
|
return tostring(v)
|
||||||
|
end,
|
||||||
|
[ "table" ] = function(t, stk)
|
||||||
|
stk = stk or {}
|
||||||
|
if stk[t] then error("circular reference") end
|
||||||
|
local rtn = {}
|
||||||
|
stk[t] = true
|
||||||
|
for k, v in pairs(t) do
|
||||||
|
rtn[#rtn + 1] = "[" .. serialize(k, stk) .. "]=" .. serialize(v, stk)
|
||||||
|
end
|
||||||
|
stk[t] = nil
|
||||||
|
return "{" .. table.concat(rtn, ",") .. "}"
|
||||||
|
end
|
||||||
|
}
|
||||||
|
|
||||||
|
setmetatable(serialize_map, {
|
||||||
|
__index = function(_, k) error("unsupported serialize type: " .. k) end
|
||||||
|
})
|
||||||
|
|
||||||
|
serialize = function(x, stk)
|
||||||
|
return serialize_map[type(x)](x, stk)
|
||||||
|
end
|
||||||
|
|
||||||
|
function lume.serialize(x)
|
||||||
|
return serialize(x)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.deserialize(str)
|
||||||
|
return lume.dostring("return " .. str)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.split(str, sep)
|
||||||
|
if not sep then
|
||||||
|
return lume.array(str:gmatch("([%S]+)"))
|
||||||
|
else
|
||||||
|
assert(sep ~= "", "empty separator")
|
||||||
|
local psep = patternescape(sep)
|
||||||
|
return lume.array((str..sep):gmatch("(.-)("..psep..")"))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.trim(str, chars)
|
||||||
|
if not chars then return str:match("^[%s]*(.-)[%s]*$") end
|
||||||
|
chars = patternescape(chars)
|
||||||
|
return str:match("^[" .. chars .. "]*(.-)[" .. chars .. "]*$")
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.wordwrap(str, limit)
|
||||||
|
limit = limit or 72
|
||||||
|
local check
|
||||||
|
if type(limit) == "number" then
|
||||||
|
check = function(s) return #s >= limit end
|
||||||
|
else
|
||||||
|
check = limit
|
||||||
|
end
|
||||||
|
local rtn = {}
|
||||||
|
local line = ""
|
||||||
|
for word, spaces in str:gmatch("(%S+)(%s*)") do
|
||||||
|
local s = line .. word
|
||||||
|
if check(s) then
|
||||||
|
table.insert(rtn, line .. "\n")
|
||||||
|
line = word
|
||||||
|
else
|
||||||
|
line = s
|
||||||
|
end
|
||||||
|
for c in spaces:gmatch(".") do
|
||||||
|
if c == "\n" then
|
||||||
|
table.insert(rtn, line .. "\n")
|
||||||
|
line = ""
|
||||||
|
else
|
||||||
|
line = line .. c
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
table.insert(rtn, line)
|
||||||
|
return table.concat(rtn)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.format(str, vars)
|
||||||
|
if not vars then return str end
|
||||||
|
local f = function(x)
|
||||||
|
return tostring(vars[x] or vars[tonumber(x)] or "{" .. x .. "}")
|
||||||
|
end
|
||||||
|
return (str:gsub("{(.-)}", f))
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.trace(...)
|
||||||
|
local info = debug.getinfo(2, "Sl")
|
||||||
|
local t = { info.short_src .. ":" .. info.currentline .. ":" }
|
||||||
|
for i = 1, select("#", ...) do
|
||||||
|
local x = select(i, ...)
|
||||||
|
if type(x) == "number" then
|
||||||
|
x = string.format("%g", lume.round(x, .01))
|
||||||
|
end
|
||||||
|
t[#t + 1] = tostring(x)
|
||||||
|
end
|
||||||
|
print(table.concat(t, " "))
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.dostring(str)
|
||||||
|
return assert((loadstring or load)(str))()
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.uuid()
|
||||||
|
local fn = function(x)
|
||||||
|
local r = math.random(16) - 1
|
||||||
|
r = (x == "x") and (r + 1) or (r % 4) + 9
|
||||||
|
return ("0123456789abcdef"):sub(r, r)
|
||||||
|
end
|
||||||
|
return (("xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"):gsub("[xy]", fn))
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.hotswap(modname)
|
||||||
|
local oldglobal = lume.clone(_G)
|
||||||
|
local updated = {}
|
||||||
|
local function update(old, new)
|
||||||
|
if updated[old] then return end
|
||||||
|
updated[old] = true
|
||||||
|
local oldmt, newmt = getmetatable(old), getmetatable(new)
|
||||||
|
if oldmt and newmt then update(oldmt, newmt) end
|
||||||
|
for k, v in pairs(new) do
|
||||||
|
if type(v) == "table" then update(old[k], v) else old[k] = v end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local err = nil
|
||||||
|
local function onerror(e)
|
||||||
|
for k in pairs(_G) do _G[k] = oldglobal[k] end
|
||||||
|
err = lume.trim(e)
|
||||||
|
end
|
||||||
|
local ok, oldmod = pcall(require, modname)
|
||||||
|
oldmod = ok and oldmod or nil
|
||||||
|
xpcall(function()
|
||||||
|
package.loaded[modname] = nil
|
||||||
|
local newmod = require(modname)
|
||||||
|
if type(oldmod) == "table" then update(oldmod, newmod) end
|
||||||
|
for k, v in pairs(oldglobal) do
|
||||||
|
if v ~= _G[k] and type(v) == "table" then
|
||||||
|
update(v, _G[k])
|
||||||
|
_G[k] = v
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end, onerror)
|
||||||
|
package.loaded[modname] = oldmod
|
||||||
|
if err then return nil, err end
|
||||||
|
return oldmod
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
local ripairs_iter = function(t, i)
|
||||||
|
i = i - 1
|
||||||
|
local v = t[i]
|
||||||
|
if v ~= nil then
|
||||||
|
return i, v
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function lume.ripairs(t)
|
||||||
|
return ripairs_iter, t, (#t + 1)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.color(str, mul)
|
||||||
|
mul = mul or 1
|
||||||
|
local r, g, b, a
|
||||||
|
r, g, b = str:match("#(%x%x)(%x%x)(%x%x)")
|
||||||
|
if r then
|
||||||
|
r = tonumber(r, 16) / 0xff
|
||||||
|
g = tonumber(g, 16) / 0xff
|
||||||
|
b = tonumber(b, 16) / 0xff
|
||||||
|
a = 1
|
||||||
|
elseif str:match("rgba?%s*%([%d%s%.,]+%)") then
|
||||||
|
local f = str:gmatch("[%d.]+")
|
||||||
|
r = (f() or 0) / 0xff
|
||||||
|
g = (f() or 0) / 0xff
|
||||||
|
b = (f() or 0) / 0xff
|
||||||
|
a = f() or 1
|
||||||
|
else
|
||||||
|
error(("bad color string '%s'"):format(str))
|
||||||
|
end
|
||||||
|
return r * mul, g * mul, b * mul, a * mul
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
local chain_mt = {}
|
||||||
|
chain_mt.__index = lume.map(lume.filter(lume, iscallable, true),
|
||||||
|
function(fn)
|
||||||
|
return function(self, ...)
|
||||||
|
self._value = fn(self._value, ...)
|
||||||
|
return self
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
chain_mt.__index.result = function(x) return x._value end
|
||||||
|
|
||||||
|
function lume.chain(value)
|
||||||
|
return setmetatable({ _value = value }, chain_mt)
|
||||||
|
end
|
||||||
|
|
||||||
|
setmetatable(lume, {
|
||||||
|
__call = function(_, ...)
|
||||||
|
return lume.chain(...)
|
||||||
|
end
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
return lume
|
51
lib/stdio.fnl
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
(local fennel (require :lib.fennel))
|
||||||
|
(require :love.event)
|
||||||
|
|
||||||
|
;; This module exists in order to expose stdio over a channel so that it
|
||||||
|
;; can be used in a non-blocking way from another thread.
|
||||||
|
|
||||||
|
(fn prompt [cont?]
|
||||||
|
(io.write (if cont? ".." ">> ")) (io.flush) (.. (tostring (io.read)) "\n"))
|
||||||
|
|
||||||
|
;; This module is loaded twice: initially in the main thread where ... is nil,
|
||||||
|
;; and then again in a separate thread where ... contains the channel used to
|
||||||
|
;; communicate with the main thread.
|
||||||
|
|
||||||
|
(fn looper [event channel]
|
||||||
|
(match (channel:demand)
|
||||||
|
[:write vals] (do (io.write (table.concat vals "\t"))
|
||||||
|
(io.write "\n"))
|
||||||
|
[:read cont?] (love.event.push event (prompt cont?)))
|
||||||
|
(looper event channel))
|
||||||
|
|
||||||
|
(match ...
|
||||||
|
(event channel) (looper event channel))
|
||||||
|
|
||||||
|
{:start (fn start-repl []
|
||||||
|
(let [code (love.filesystem.read "lib/stdio.fnl")
|
||||||
|
luac (if code
|
||||||
|
(love.filesystem.newFileData
|
||||||
|
(fennel.compileString code) "io")
|
||||||
|
(love.filesystem.read "lib/stdio.lua"))
|
||||||
|
thread (love.thread.newThread luac)
|
||||||
|
io-channel (love.thread.newChannel)
|
||||||
|
;; fennel 1.5 has updated repl to be a table
|
||||||
|
;; with a metatable __call value. coroutine.create
|
||||||
|
;; does not reference the metatable __call value
|
||||||
|
;; by applying partial we fix this issue in a
|
||||||
|
;; backwards compatible way.
|
||||||
|
coro (coroutine.create (partial fennel.repl))
|
||||||
|
options {:readChunk (fn [{: stack-size}]
|
||||||
|
(io-channel:push [:read (< 0 stack-size)])
|
||||||
|
(coroutine.yield))
|
||||||
|
:onValues (fn [vals]
|
||||||
|
(io-channel:push [:write vals]))
|
||||||
|
:onError (fn [errtype err]
|
||||||
|
(io-channel:push [:write [err]]))
|
||||||
|
:moduleName "lib.fennel"}]
|
||||||
|
;; this thread will send "eval" events for us to consume:
|
||||||
|
(coroutine.resume coro options)
|
||||||
|
(thread:start "eval" io-channel)
|
||||||
|
(set love.handlers.eval
|
||||||
|
(fn [input]
|
||||||
|
(coroutine.resume coro input)))))}
|
76
lib/stdio.lua
Normal file
|
@ -0,0 +1,76 @@
|
||||||
|
local fennel = require("lib.fennel")
|
||||||
|
require("love.event")
|
||||||
|
local function prompt(cont_3f)
|
||||||
|
local function _1_()
|
||||||
|
if cont_3f then
|
||||||
|
return ".."
|
||||||
|
else
|
||||||
|
return ">> "
|
||||||
|
end
|
||||||
|
end
|
||||||
|
io.write(_1_())
|
||||||
|
io.flush()
|
||||||
|
return (tostring(io.read()) .. "\n")
|
||||||
|
end
|
||||||
|
local function looper(event, channel)
|
||||||
|
do
|
||||||
|
local _2_ = channel:demand()
|
||||||
|
if ((_G.type(_2_) == "table") and (_2_[1] == "write") and (nil ~= _2_[2])) then
|
||||||
|
local vals = _2_[2]
|
||||||
|
io.write(table.concat(vals, "\9"))
|
||||||
|
io.write("\n")
|
||||||
|
elseif ((_G.type(_2_) == "table") and (_2_[1] == "read") and (nil ~= _2_[2])) then
|
||||||
|
local cont_3f = _2_[2]
|
||||||
|
love.event.push(event, prompt(cont_3f))
|
||||||
|
else
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return looper(event, channel)
|
||||||
|
end
|
||||||
|
do
|
||||||
|
local _4_, _5_ = ...
|
||||||
|
if ((nil ~= _4_) and (nil ~= _5_)) then
|
||||||
|
local event = _4_
|
||||||
|
local channel = _5_
|
||||||
|
looper(event, channel)
|
||||||
|
else
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local function start_repl()
|
||||||
|
local code = love.filesystem.read("lib/stdio.fnl")
|
||||||
|
local luac
|
||||||
|
if code then
|
||||||
|
luac = love.filesystem.newFileData(fennel.compileString(code), "io")
|
||||||
|
else
|
||||||
|
luac = love.filesystem.read("lib/stdio.lua")
|
||||||
|
end
|
||||||
|
local thread = love.thread.newThread(luac)
|
||||||
|
local io_channel = love.thread.newChannel()
|
||||||
|
local coro
|
||||||
|
local function _8_(...)
|
||||||
|
return fennel.repl(...)
|
||||||
|
end
|
||||||
|
coro = coroutine.create(_8_)
|
||||||
|
local options
|
||||||
|
local function _11_(_9_)
|
||||||
|
local _arg_10_ = _9_
|
||||||
|
local stack_size = _arg_10_["stack-size"]
|
||||||
|
io_channel:push({"read", (0 < stack_size)})
|
||||||
|
return coroutine.yield()
|
||||||
|
end
|
||||||
|
local function _12_(vals)
|
||||||
|
return io_channel:push({"write", vals})
|
||||||
|
end
|
||||||
|
local function _13_(errtype, err)
|
||||||
|
return io_channel:push({"write", {err}})
|
||||||
|
end
|
||||||
|
options = {readChunk = _11_, onValues = _12_, onError = _13_, moduleName = "lib.fennel"}
|
||||||
|
coroutine.resume(coro, options)
|
||||||
|
thread:start("eval", io_channel)
|
||||||
|
local function _14_(input)
|
||||||
|
return coroutine.resume(coro, input)
|
||||||
|
end
|
||||||
|
love.handlers.eval = _14_
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
return {start = start_repl}
|
674
license.txt
Normal file
|
@ -0,0 +1,674 @@
|
||||||
|
GNU GENERAL PUBLIC LICENSE
|
||||||
|
Version 3, 29 June 2007
|
||||||
|
|
||||||
|
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||||
|
Everyone is permitted to copy and distribute verbatim copies
|
||||||
|
of this license document, but changing it is not allowed.
|
||||||
|
|
||||||
|
Preamble
|
||||||
|
|
||||||
|
The GNU General Public License is a free, copyleft license for
|
||||||
|
software and other kinds of works.
|
||||||
|
|
||||||
|
The licenses for most software and other practical works are designed
|
||||||
|
to take away your freedom to share and change the works. By contrast,
|
||||||
|
the GNU General Public License is intended to guarantee your freedom to
|
||||||
|
share and change all versions of a program--to make sure it remains free
|
||||||
|
software for all its users. We, the Free Software Foundation, use the
|
||||||
|
GNU General Public License for most of our software; it applies also to
|
||||||
|
any other work released this way by its authors. You can apply it to
|
||||||
|
your programs, too.
|
||||||
|
|
||||||
|
When we speak of free software, we are referring to freedom, not
|
||||||
|
price. Our General Public Licenses are designed to make sure that you
|
||||||
|
have the freedom to distribute copies of free software (and charge for
|
||||||
|
them if you wish), that you receive source code or can get it if you
|
||||||
|
want it, that you can change the software or use pieces of it in new
|
||||||
|
free programs, and that you know you can do these things.
|
||||||
|
|
||||||
|
To protect your rights, we need to prevent others from denying you
|
||||||
|
these rights or asking you to surrender the rights. Therefore, you have
|
||||||
|
certain responsibilities if you distribute copies of the software, or if
|
||||||
|
you modify it: responsibilities to respect the freedom of others.
|
||||||
|
|
||||||
|
For example, if you distribute copies of such a program, whether
|
||||||
|
gratis or for a fee, you must pass on to the recipients the same
|
||||||
|
freedoms that you received. You must make sure that they, too, receive
|
||||||
|
or can get the source code. And you must show them these terms so they
|
||||||
|
know their rights.
|
||||||
|
|
||||||
|
Developers that use the GNU GPL protect your rights with two steps:
|
||||||
|
(1) assert copyright on the software, and (2) offer you this License
|
||||||
|
giving you legal permission to copy, distribute and/or modify it.
|
||||||
|
|
||||||
|
For the developers' and authors' protection, the GPL clearly explains
|
||||||
|
that there is no warranty for this free software. For both users' and
|
||||||
|
authors' sake, the GPL requires that modified versions be marked as
|
||||||
|
changed, so that their problems will not be attributed erroneously to
|
||||||
|
authors of previous versions.
|
||||||
|
|
||||||
|
Some devices are designed to deny users access to install or run
|
||||||
|
modified versions of the software inside them, although the manufacturer
|
||||||
|
can do so. This is fundamentally incompatible with the aim of
|
||||||
|
protecting users' freedom to change the software. The systematic
|
||||||
|
pattern of such abuse occurs in the area of products for individuals to
|
||||||
|
use, which is precisely where it is most unacceptable. Therefore, we
|
||||||
|
have designed this version of the GPL to prohibit the practice for those
|
||||||
|
products. If such problems arise substantially in other domains, we
|
||||||
|
stand ready to extend this provision to those domains in future versions
|
||||||
|
of the GPL, as needed to protect the freedom of users.
|
||||||
|
|
||||||
|
Finally, every program is threatened constantly by software patents.
|
||||||
|
States should not allow patents to restrict development and use of
|
||||||
|
software on general-purpose computers, but in those that do, we wish to
|
||||||
|
avoid the special danger that patents applied to a free program could
|
||||||
|
make it effectively proprietary. To prevent this, the GPL assures that
|
||||||
|
patents cannot be used to render the program non-free.
|
||||||
|
|
||||||
|
The precise terms and conditions for copying, distribution and
|
||||||
|
modification follow.
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
0. Definitions.
|
||||||
|
|
||||||
|
"This License" refers to version 3 of the GNU General Public License.
|
||||||
|
|
||||||
|
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||||
|
works, such as semiconductor masks.
|
||||||
|
|
||||||
|
"The Program" refers to any copyrightable work licensed under this
|
||||||
|
License. Each licensee is addressed as "you". "Licensees" and
|
||||||
|
"recipients" may be individuals or organizations.
|
||||||
|
|
||||||
|
To "modify" a work means to copy from or adapt all or part of the work
|
||||||
|
in a fashion requiring copyright permission, other than the making of an
|
||||||
|
exact copy. The resulting work is called a "modified version" of the
|
||||||
|
earlier work or a work "based on" the earlier work.
|
||||||
|
|
||||||
|
A "covered work" means either the unmodified Program or a work based
|
||||||
|
on the Program.
|
||||||
|
|
||||||
|
To "propagate" a work means to do anything with it that, without
|
||||||
|
permission, would make you directly or secondarily liable for
|
||||||
|
infringement under applicable copyright law, except executing it on a
|
||||||
|
computer or modifying a private copy. Propagation includes copying,
|
||||||
|
distribution (with or without modification), making available to the
|
||||||
|
public, and in some countries other activities as well.
|
||||||
|
|
||||||
|
To "convey" a work means any kind of propagation that enables other
|
||||||
|
parties to make or receive copies. Mere interaction with a user through
|
||||||
|
a computer network, with no transfer of a copy, is not conveying.
|
||||||
|
|
||||||
|
An interactive user interface displays "Appropriate Legal Notices"
|
||||||
|
to the extent that it includes a convenient and prominently visible
|
||||||
|
feature that (1) displays an appropriate copyright notice, and (2)
|
||||||
|
tells the user that there is no warranty for the work (except to the
|
||||||
|
extent that warranties are provided), that licensees may convey the
|
||||||
|
work under this License, and how to view a copy of this License. If
|
||||||
|
the interface presents a list of user commands or options, such as a
|
||||||
|
menu, a prominent item in the list meets this criterion.
|
||||||
|
|
||||||
|
1. Source Code.
|
||||||
|
|
||||||
|
The "source code" for a work means the preferred form of the work
|
||||||
|
for making modifications to it. "Object code" means any non-source
|
||||||
|
form of a work.
|
||||||
|
|
||||||
|
A "Standard Interface" means an interface that either is an official
|
||||||
|
standard defined by a recognized standards body, or, in the case of
|
||||||
|
interfaces specified for a particular programming language, one that
|
||||||
|
is widely used among developers working in that language.
|
||||||
|
|
||||||
|
The "System Libraries" of an executable work include anything, other
|
||||||
|
than the work as a whole, that (a) is included in the normal form of
|
||||||
|
packaging a Major Component, but which is not part of that Major
|
||||||
|
Component, and (b) serves only to enable use of the work with that
|
||||||
|
Major Component, or to implement a Standard Interface for which an
|
||||||
|
implementation is available to the public in source code form. A
|
||||||
|
"Major Component", in this context, means a major essential component
|
||||||
|
(kernel, window system, and so on) of the specific operating system
|
||||||
|
(if any) on which the executable work runs, or a compiler used to
|
||||||
|
produce the work, or an object code interpreter used to run it.
|
||||||
|
|
||||||
|
The "Corresponding Source" for a work in object code form means all
|
||||||
|
the source code needed to generate, install, and (for an executable
|
||||||
|
work) run the object code and to modify the work, including scripts to
|
||||||
|
control those activities. However, it does not include the work's
|
||||||
|
System Libraries, or general-purpose tools or generally available free
|
||||||
|
programs which are used unmodified in performing those activities but
|
||||||
|
which are not part of the work. For example, Corresponding Source
|
||||||
|
includes interface definition files associated with source files for
|
||||||
|
the work, and the source code for shared libraries and dynamically
|
||||||
|
linked subprograms that the work is specifically designed to require,
|
||||||
|
such as by intimate data communication or control flow between those
|
||||||
|
subprograms and other parts of the work.
|
||||||
|
|
||||||
|
The Corresponding Source need not include anything that users
|
||||||
|
can regenerate automatically from other parts of the Corresponding
|
||||||
|
Source.
|
||||||
|
|
||||||
|
The Corresponding Source for a work in source code form is that
|
||||||
|
same work.
|
||||||
|
|
||||||
|
2. Basic Permissions.
|
||||||
|
|
||||||
|
All rights granted under this License are granted for the term of
|
||||||
|
copyright on the Program, and are irrevocable provided the stated
|
||||||
|
conditions are met. This License explicitly affirms your unlimited
|
||||||
|
permission to run the unmodified Program. The output from running a
|
||||||
|
covered work is covered by this License only if the output, given its
|
||||||
|
content, constitutes a covered work. This License acknowledges your
|
||||||
|
rights of fair use or other equivalent, as provided by copyright law.
|
||||||
|
|
||||||
|
You may make, run and propagate covered works that you do not
|
||||||
|
convey, without conditions so long as your license otherwise remains
|
||||||
|
in force. You may convey covered works to others for the sole purpose
|
||||||
|
of having them make modifications exclusively for you, or provide you
|
||||||
|
with facilities for running those works, provided that you comply with
|
||||||
|
the terms of this License in conveying all material for which you do
|
||||||
|
not control copyright. Those thus making or running the covered works
|
||||||
|
for you must do so exclusively on your behalf, under your direction
|
||||||
|
and control, on terms that prohibit them from making any copies of
|
||||||
|
your copyrighted material outside their relationship with you.
|
||||||
|
|
||||||
|
Conveying under any other circumstances is permitted solely under
|
||||||
|
the conditions stated below. Sublicensing is not allowed; section 10
|
||||||
|
makes it unnecessary.
|
||||||
|
|
||||||
|
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||||
|
|
||||||
|
No covered work shall be deemed part of an effective technological
|
||||||
|
measure under any applicable law fulfilling obligations under article
|
||||||
|
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||||
|
similar laws prohibiting or restricting circumvention of such
|
||||||
|
measures.
|
||||||
|
|
||||||
|
When you convey a covered work, you waive any legal power to forbid
|
||||||
|
circumvention of technological measures to the extent such circumvention
|
||||||
|
is effected by exercising rights under this License with respect to
|
||||||
|
the covered work, and you disclaim any intention to limit operation or
|
||||||
|
modification of the work as a means of enforcing, against the work's
|
||||||
|
users, your or third parties' legal rights to forbid circumvention of
|
||||||
|
technological measures.
|
||||||
|
|
||||||
|
4. Conveying Verbatim Copies.
|
||||||
|
|
||||||
|
You may convey verbatim copies of the Program's source code as you
|
||||||
|
receive it, in any medium, provided that you conspicuously and
|
||||||
|
appropriately publish on each copy an appropriate copyright notice;
|
||||||
|
keep intact all notices stating that this License and any
|
||||||
|
non-permissive terms added in accord with section 7 apply to the code;
|
||||||
|
keep intact all notices of the absence of any warranty; and give all
|
||||||
|
recipients a copy of this License along with the Program.
|
||||||
|
|
||||||
|
You may charge any price or no price for each copy that you convey,
|
||||||
|
and you may offer support or warranty protection for a fee.
|
||||||
|
|
||||||
|
5. Conveying Modified Source Versions.
|
||||||
|
|
||||||
|
You may convey a work based on the Program, or the modifications to
|
||||||
|
produce it from the Program, in the form of source code under the
|
||||||
|
terms of section 4, provided that you also meet all of these conditions:
|
||||||
|
|
||||||
|
a) The work must carry prominent notices stating that you modified
|
||||||
|
it, and giving a relevant date.
|
||||||
|
|
||||||
|
b) The work must carry prominent notices stating that it is
|
||||||
|
released under this License and any conditions added under section
|
||||||
|
7. This requirement modifies the requirement in section 4 to
|
||||||
|
"keep intact all notices".
|
||||||
|
|
||||||
|
c) You must license the entire work, as a whole, under this
|
||||||
|
License to anyone who comes into possession of a copy. This
|
||||||
|
License will therefore apply, along with any applicable section 7
|
||||||
|
additional terms, to the whole of the work, and all its parts,
|
||||||
|
regardless of how they are packaged. This License gives no
|
||||||
|
permission to license the work in any other way, but it does not
|
||||||
|
invalidate such permission if you have separately received it.
|
||||||
|
|
||||||
|
d) If the work has interactive user interfaces, each must display
|
||||||
|
Appropriate Legal Notices; however, if the Program has interactive
|
||||||
|
interfaces that do not display Appropriate Legal Notices, your
|
||||||
|
work need not make them do so.
|
||||||
|
|
||||||
|
A compilation of a covered work with other separate and independent
|
||||||
|
works, which are not by their nature extensions of the covered work,
|
||||||
|
and which are not combined with it such as to form a larger program,
|
||||||
|
in or on a volume of a storage or distribution medium, is called an
|
||||||
|
"aggregate" if the compilation and its resulting copyright are not
|
||||||
|
used to limit the access or legal rights of the compilation's users
|
||||||
|
beyond what the individual works permit. Inclusion of a covered work
|
||||||
|
in an aggregate does not cause this License to apply to the other
|
||||||
|
parts of the aggregate.
|
||||||
|
|
||||||
|
6. Conveying Non-Source Forms.
|
||||||
|
|
||||||
|
You may convey a covered work in object code form under the terms
|
||||||
|
of sections 4 and 5, provided that you also convey the
|
||||||
|
machine-readable Corresponding Source under the terms of this License,
|
||||||
|
in one of these ways:
|
||||||
|
|
||||||
|
a) Convey the object code in, or embodied in, a physical product
|
||||||
|
(including a physical distribution medium), accompanied by the
|
||||||
|
Corresponding Source fixed on a durable physical medium
|
||||||
|
customarily used for software interchange.
|
||||||
|
|
||||||
|
b) Convey the object code in, or embodied in, a physical product
|
||||||
|
(including a physical distribution medium), accompanied by a
|
||||||
|
written offer, valid for at least three years and valid for as
|
||||||
|
long as you offer spare parts or customer support for that product
|
||||||
|
model, to give anyone who possesses the object code either (1) a
|
||||||
|
copy of the Corresponding Source for all the software in the
|
||||||
|
product that is covered by this License, on a durable physical
|
||||||
|
medium customarily used for software interchange, for a price no
|
||||||
|
more than your reasonable cost of physically performing this
|
||||||
|
conveying of source, or (2) access to copy the
|
||||||
|
Corresponding Source from a network server at no charge.
|
||||||
|
|
||||||
|
c) Convey individual copies of the object code with a copy of the
|
||||||
|
written offer to provide the Corresponding Source. This
|
||||||
|
alternative is allowed only occasionally and noncommercially, and
|
||||||
|
only if you received the object code with such an offer, in accord
|
||||||
|
with subsection 6b.
|
||||||
|
|
||||||
|
d) Convey the object code by offering access from a designated
|
||||||
|
place (gratis or for a charge), and offer equivalent access to the
|
||||||
|
Corresponding Source in the same way through the same place at no
|
||||||
|
further charge. You need not require recipients to copy the
|
||||||
|
Corresponding Source along with the object code. If the place to
|
||||||
|
copy the object code is a network server, the Corresponding Source
|
||||||
|
may be on a different server (operated by you or a third party)
|
||||||
|
that supports equivalent copying facilities, provided you maintain
|
||||||
|
clear directions next to the object code saying where to find the
|
||||||
|
Corresponding Source. Regardless of what server hosts the
|
||||||
|
Corresponding Source, you remain obligated to ensure that it is
|
||||||
|
available for as long as needed to satisfy these requirements.
|
||||||
|
|
||||||
|
e) Convey the object code using peer-to-peer transmission, provided
|
||||||
|
you inform other peers where the object code and Corresponding
|
||||||
|
Source of the work are being offered to the general public at no
|
||||||
|
charge under subsection 6d.
|
||||||
|
|
||||||
|
A separable portion of the object code, whose source code is excluded
|
||||||
|
from the Corresponding Source as a System Library, need not be
|
||||||
|
included in conveying the object code work.
|
||||||
|
|
||||||
|
A "User Product" is either (1) a "consumer product", which means any
|
||||||
|
tangible personal property which is normally used for personal, family,
|
||||||
|
or household purposes, or (2) anything designed or sold for incorporation
|
||||||
|
into a dwelling. In determining whether a product is a consumer product,
|
||||||
|
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||||
|
product received by a particular user, "normally used" refers to a
|
||||||
|
typical or common use of that class of product, regardless of the status
|
||||||
|
of the particular user or of the way in which the particular user
|
||||||
|
actually uses, or expects or is expected to use, the product. A product
|
||||||
|
is a consumer product regardless of whether the product has substantial
|
||||||
|
commercial, industrial or non-consumer uses, unless such uses represent
|
||||||
|
the only significant mode of use of the product.
|
||||||
|
|
||||||
|
"Installation Information" for a User Product means any methods,
|
||||||
|
procedures, authorization keys, or other information required to install
|
||||||
|
and execute modified versions of a covered work in that User Product from
|
||||||
|
a modified version of its Corresponding Source. The information must
|
||||||
|
suffice to ensure that the continued functioning of the modified object
|
||||||
|
code is in no case prevented or interfered with solely because
|
||||||
|
modification has been made.
|
||||||
|
|
||||||
|
If you convey an object code work under this section in, or with, or
|
||||||
|
specifically for use in, a User Product, and the conveying occurs as
|
||||||
|
part of a transaction in which the right of possession and use of the
|
||||||
|
User Product is transferred to the recipient in perpetuity or for a
|
||||||
|
fixed term (regardless of how the transaction is characterized), the
|
||||||
|
Corresponding Source conveyed under this section must be accompanied
|
||||||
|
by the Installation Information. But this requirement does not apply
|
||||||
|
if neither you nor any third party retains the ability to install
|
||||||
|
modified object code on the User Product (for example, the work has
|
||||||
|
been installed in ROM).
|
||||||
|
|
||||||
|
The requirement to provide Installation Information does not include a
|
||||||
|
requirement to continue to provide support service, warranty, or updates
|
||||||
|
for a work that has been modified or installed by the recipient, or for
|
||||||
|
the User Product in which it has been modified or installed. Access to a
|
||||||
|
network may be denied when the modification itself materially and
|
||||||
|
adversely affects the operation of the network or violates the rules and
|
||||||
|
protocols for communication across the network.
|
||||||
|
|
||||||
|
Corresponding Source conveyed, and Installation Information provided,
|
||||||
|
in accord with this section must be in a format that is publicly
|
||||||
|
documented (and with an implementation available to the public in
|
||||||
|
source code form), and must require no special password or key for
|
||||||
|
unpacking, reading or copying.
|
||||||
|
|
||||||
|
7. Additional Terms.
|
||||||
|
|
||||||
|
"Additional permissions" are terms that supplement the terms of this
|
||||||
|
License by making exceptions from one or more of its conditions.
|
||||||
|
Additional permissions that are applicable to the entire Program shall
|
||||||
|
be treated as though they were included in this License, to the extent
|
||||||
|
that they are valid under applicable law. If additional permissions
|
||||||
|
apply only to part of the Program, that part may be used separately
|
||||||
|
under those permissions, but the entire Program remains governed by
|
||||||
|
this License without regard to the additional permissions.
|
||||||
|
|
||||||
|
When you convey a copy of a covered work, you may at your option
|
||||||
|
remove any additional permissions from that copy, or from any part of
|
||||||
|
it. (Additional permissions may be written to require their own
|
||||||
|
removal in certain cases when you modify the work.) You may place
|
||||||
|
additional permissions on material, added by you to a covered work,
|
||||||
|
for which you have or can give appropriate copyright permission.
|
||||||
|
|
||||||
|
Notwithstanding any other provision of this License, for material you
|
||||||
|
add to a covered work, you may (if authorized by the copyright holders of
|
||||||
|
that material) supplement the terms of this License with terms:
|
||||||
|
|
||||||
|
a) Disclaiming warranty or limiting liability differently from the
|
||||||
|
terms of sections 15 and 16 of this License; or
|
||||||
|
|
||||||
|
b) Requiring preservation of specified reasonable legal notices or
|
||||||
|
author attributions in that material or in the Appropriate Legal
|
||||||
|
Notices displayed by works containing it; or
|
||||||
|
|
||||||
|
c) Prohibiting misrepresentation of the origin of that material, or
|
||||||
|
requiring that modified versions of such material be marked in
|
||||||
|
reasonable ways as different from the original version; or
|
||||||
|
|
||||||
|
d) Limiting the use for publicity purposes of names of licensors or
|
||||||
|
authors of the material; or
|
||||||
|
|
||||||
|
e) Declining to grant rights under trademark law for use of some
|
||||||
|
trade names, trademarks, or service marks; or
|
||||||
|
|
||||||
|
f) Requiring indemnification of licensors and authors of that
|
||||||
|
material by anyone who conveys the material (or modified versions of
|
||||||
|
it) with contractual assumptions of liability to the recipient, for
|
||||||
|
any liability that these contractual assumptions directly impose on
|
||||||
|
those licensors and authors.
|
||||||
|
|
||||||
|
All other non-permissive additional terms are considered "further
|
||||||
|
restrictions" within the meaning of section 10. If the Program as you
|
||||||
|
received it, or any part of it, contains a notice stating that it is
|
||||||
|
governed by this License along with a term that is a further
|
||||||
|
restriction, you may remove that term. If a license document contains
|
||||||
|
a further restriction but permits relicensing or conveying under this
|
||||||
|
License, you may add to a covered work material governed by the terms
|
||||||
|
of that license document, provided that the further restriction does
|
||||||
|
not survive such relicensing or conveying.
|
||||||
|
|
||||||
|
If you add terms to a covered work in accord with this section, you
|
||||||
|
must place, in the relevant source files, a statement of the
|
||||||
|
additional terms that apply to those files, or a notice indicating
|
||||||
|
where to find the applicable terms.
|
||||||
|
|
||||||
|
Additional terms, permissive or non-permissive, may be stated in the
|
||||||
|
form of a separately written license, or stated as exceptions;
|
||||||
|
the above requirements apply either way.
|
||||||
|
|
||||||
|
8. Termination.
|
||||||
|
|
||||||
|
You may not propagate or modify a covered work except as expressly
|
||||||
|
provided under this License. Any attempt otherwise to propagate or
|
||||||
|
modify it is void, and will automatically terminate your rights under
|
||||||
|
this License (including any patent licenses granted under the third
|
||||||
|
paragraph of section 11).
|
||||||
|
|
||||||
|
However, if you cease all violation of this License, then your
|
||||||
|
license from a particular copyright holder is reinstated (a)
|
||||||
|
provisionally, unless and until the copyright holder explicitly and
|
||||||
|
finally terminates your license, and (b) permanently, if the copyright
|
||||||
|
holder fails to notify you of the violation by some reasonable means
|
||||||
|
prior to 60 days after the cessation.
|
||||||
|
|
||||||
|
Moreover, your license from a particular copyright holder is
|
||||||
|
reinstated permanently if the copyright holder notifies you of the
|
||||||
|
violation by some reasonable means, this is the first time you have
|
||||||
|
received notice of violation of this License (for any work) from that
|
||||||
|
copyright holder, and you cure the violation prior to 30 days after
|
||||||
|
your receipt of the notice.
|
||||||
|
|
||||||
|
Termination of your rights under this section does not terminate the
|
||||||
|
licenses of parties who have received copies or rights from you under
|
||||||
|
this License. If your rights have been terminated and not permanently
|
||||||
|
reinstated, you do not qualify to receive new licenses for the same
|
||||||
|
material under section 10.
|
||||||
|
|
||||||
|
9. Acceptance Not Required for Having Copies.
|
||||||
|
|
||||||
|
You are not required to accept this License in order to receive or
|
||||||
|
run a copy of the Program. Ancillary propagation of a covered work
|
||||||
|
occurring solely as a consequence of using peer-to-peer transmission
|
||||||
|
to receive a copy likewise does not require acceptance. However,
|
||||||
|
nothing other than this License grants you permission to propagate or
|
||||||
|
modify any covered work. These actions infringe copyright if you do
|
||||||
|
not accept this License. Therefore, by modifying or propagating a
|
||||||
|
covered work, you indicate your acceptance of this License to do so.
|
||||||
|
|
||||||
|
10. Automatic Licensing of Downstream Recipients.
|
||||||
|
|
||||||
|
Each time you convey a covered work, the recipient automatically
|
||||||
|
receives a license from the original licensors, to run, modify and
|
||||||
|
propagate that work, subject to this License. You are not responsible
|
||||||
|
for enforcing compliance by third parties with this License.
|
||||||
|
|
||||||
|
An "entity transaction" is a transaction transferring control of an
|
||||||
|
organization, or substantially all assets of one, or subdividing an
|
||||||
|
organization, or merging organizations. If propagation of a covered
|
||||||
|
work results from an entity transaction, each party to that
|
||||||
|
transaction who receives a copy of the work also receives whatever
|
||||||
|
licenses to the work the party's predecessor in interest had or could
|
||||||
|
give under the previous paragraph, plus a right to possession of the
|
||||||
|
Corresponding Source of the work from the predecessor in interest, if
|
||||||
|
the predecessor has it or can get it with reasonable efforts.
|
||||||
|
|
||||||
|
You may not impose any further restrictions on the exercise of the
|
||||||
|
rights granted or affirmed under this License. For example, you may
|
||||||
|
not impose a license fee, royalty, or other charge for exercise of
|
||||||
|
rights granted under this License, and you may not initiate litigation
|
||||||
|
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||||
|
any patent claim is infringed by making, using, selling, offering for
|
||||||
|
sale, or importing the Program or any portion of it.
|
||||||
|
|
||||||
|
11. Patents.
|
||||||
|
|
||||||
|
A "contributor" is a copyright holder who authorizes use under this
|
||||||
|
License of the Program or a work on which the Program is based. The
|
||||||
|
work thus licensed is called the contributor's "contributor version".
|
||||||
|
|
||||||
|
A contributor's "essential patent claims" are all patent claims
|
||||||
|
owned or controlled by the contributor, whether already acquired or
|
||||||
|
hereafter acquired, that would be infringed by some manner, permitted
|
||||||
|
by this License, of making, using, or selling its contributor version,
|
||||||
|
but do not include claims that would be infringed only as a
|
||||||
|
consequence of further modification of the contributor version. For
|
||||||
|
purposes of this definition, "control" includes the right to grant
|
||||||
|
patent sublicenses in a manner consistent with the requirements of
|
||||||
|
this License.
|
||||||
|
|
||||||
|
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||||
|
patent license under the contributor's essential patent claims, to
|
||||||
|
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||||
|
propagate the contents of its contributor version.
|
||||||
|
|
||||||
|
In the following three paragraphs, a "patent license" is any express
|
||||||
|
agreement or commitment, however denominated, not to enforce a patent
|
||||||
|
(such as an express permission to practice a patent or covenant not to
|
||||||
|
sue for patent infringement). To "grant" such a patent license to a
|
||||||
|
party means to make such an agreement or commitment not to enforce a
|
||||||
|
patent against the party.
|
||||||
|
|
||||||
|
If you convey a covered work, knowingly relying on a patent license,
|
||||||
|
and the Corresponding Source of the work is not available for anyone
|
||||||
|
to copy, free of charge and under the terms of this License, through a
|
||||||
|
publicly available network server or other readily accessible means,
|
||||||
|
then you must either (1) cause the Corresponding Source to be so
|
||||||
|
available, or (2) arrange to deprive yourself of the benefit of the
|
||||||
|
patent license for this particular work, or (3) arrange, in a manner
|
||||||
|
consistent with the requirements of this License, to extend the patent
|
||||||
|
license to downstream recipients. "Knowingly relying" means you have
|
||||||
|
actual knowledge that, but for the patent license, your conveying the
|
||||||
|
covered work in a country, or your recipient's use of the covered work
|
||||||
|
in a country, would infringe one or more identifiable patents in that
|
||||||
|
country that you have reason to believe are valid.
|
||||||
|
|
||||||
|
If, pursuant to or in connection with a single transaction or
|
||||||
|
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||||
|
covered work, and grant a patent license to some of the parties
|
||||||
|
receiving the covered work authorizing them to use, propagate, modify
|
||||||
|
or convey a specific copy of the covered work, then the patent license
|
||||||
|
you grant is automatically extended to all recipients of the covered
|
||||||
|
work and works based on it.
|
||||||
|
|
||||||
|
A patent license is "discriminatory" if it does not include within
|
||||||
|
the scope of its coverage, prohibits the exercise of, or is
|
||||||
|
conditioned on the non-exercise of one or more of the rights that are
|
||||||
|
specifically granted under this License. You may not convey a covered
|
||||||
|
work if you are a party to an arrangement with a third party that is
|
||||||
|
in the business of distributing software, under which you make payment
|
||||||
|
to the third party based on the extent of your activity of conveying
|
||||||
|
the work, and under which the third party grants, to any of the
|
||||||
|
parties who would receive the covered work from you, a discriminatory
|
||||||
|
patent license (a) in connection with copies of the covered work
|
||||||
|
conveyed by you (or copies made from those copies), or (b) primarily
|
||||||
|
for and in connection with specific products or compilations that
|
||||||
|
contain the covered work, unless you entered into that arrangement,
|
||||||
|
or that patent license was granted, prior to 28 March 2007.
|
||||||
|
|
||||||
|
Nothing in this License shall be construed as excluding or limiting
|
||||||
|
any implied license or other defenses to infringement that may
|
||||||
|
otherwise be available to you under applicable patent law.
|
||||||
|
|
||||||
|
12. No Surrender of Others' Freedom.
|
||||||
|
|
||||||
|
If conditions are imposed on you (whether by court order, agreement or
|
||||||
|
otherwise) that contradict the conditions of this License, they do not
|
||||||
|
excuse you from the conditions of this License. If you cannot convey a
|
||||||
|
covered work so as to satisfy simultaneously your obligations under this
|
||||||
|
License and any other pertinent obligations, then as a consequence you may
|
||||||
|
not convey it at all. For example, if you agree to terms that obligate you
|
||||||
|
to collect a royalty for further conveying from those to whom you convey
|
||||||
|
the Program, the only way you could satisfy both those terms and this
|
||||||
|
License would be to refrain entirely from conveying the Program.
|
||||||
|
|
||||||
|
13. Use with the GNU Affero General Public License.
|
||||||
|
|
||||||
|
Notwithstanding any other provision of this License, you have
|
||||||
|
permission to link or combine any covered work with a work licensed
|
||||||
|
under version 3 of the GNU Affero General Public License into a single
|
||||||
|
combined work, and to convey the resulting work. The terms of this
|
||||||
|
License will continue to apply to the part which is the covered work,
|
||||||
|
but the special requirements of the GNU Affero General Public License,
|
||||||
|
section 13, concerning interaction through a network will apply to the
|
||||||
|
combination as such.
|
||||||
|
|
||||||
|
14. Revised Versions of this License.
|
||||||
|
|
||||||
|
The Free Software Foundation may publish revised and/or new versions of
|
||||||
|
the GNU General Public License from time to time. Such new versions will
|
||||||
|
be similar in spirit to the present version, but may differ in detail to
|
||||||
|
address new problems or concerns.
|
||||||
|
|
||||||
|
Each version is given a distinguishing version number. If the
|
||||||
|
Program specifies that a certain numbered version of the GNU General
|
||||||
|
Public License "or any later version" applies to it, you have the
|
||||||
|
option of following the terms and conditions either of that numbered
|
||||||
|
version or of any later version published by the Free Software
|
||||||
|
Foundation. If the Program does not specify a version number of the
|
||||||
|
GNU General Public License, you may choose any version ever published
|
||||||
|
by the Free Software Foundation.
|
||||||
|
|
||||||
|
If the Program specifies that a proxy can decide which future
|
||||||
|
versions of the GNU General Public License can be used, that proxy's
|
||||||
|
public statement of acceptance of a version permanently authorizes you
|
||||||
|
to choose that version for the Program.
|
||||||
|
|
||||||
|
Later license versions may give you additional or different
|
||||||
|
permissions. However, no additional obligations are imposed on any
|
||||||
|
author or copyright holder as a result of your choosing to follow a
|
||||||
|
later version.
|
||||||
|
|
||||||
|
15. Disclaimer of Warranty.
|
||||||
|
|
||||||
|
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||||
|
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||||
|
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||||
|
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||||
|
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||||
|
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||||
|
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||||
|
|
||||||
|
16. Limitation of Liability.
|
||||||
|
|
||||||
|
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||||
|
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||||
|
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
||||||
|
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
||||||
|
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
||||||
|
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
||||||
|
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
||||||
|
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
||||||
|
SUCH DAMAGES.
|
||||||
|
|
||||||
|
17. Interpretation of Sections 15 and 16.
|
||||||
|
|
||||||
|
If the disclaimer of warranty and limitation of liability provided
|
||||||
|
above cannot be given local legal effect according to their terms,
|
||||||
|
reviewing courts shall apply local law that most closely approximates
|
||||||
|
an absolute waiver of all civil liability in connection with the
|
||||||
|
Program, unless a warranty or assumption of liability accompanies a
|
||||||
|
copy of the Program in return for a fee.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
How to Apply These Terms to Your New Programs
|
||||||
|
|
||||||
|
If you develop a new program, and you want it to be of the greatest
|
||||||
|
possible use to the public, the best way to achieve this is to make it
|
||||||
|
free software which everyone can redistribute and change under these terms.
|
||||||
|
|
||||||
|
To do so, attach the following notices to the program. It is safest
|
||||||
|
to attach them to the start of each source file to most effectively
|
||||||
|
state the exclusion of warranty; and each file should have at least
|
||||||
|
the "copyright" line and a pointer to where the full notice is found.
|
||||||
|
|
||||||
|
<one line to give the program's name and a brief idea of what it does.>
|
||||||
|
Copyright (C) <year> <name of author>
|
||||||
|
|
||||||
|
This program 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.
|
||||||
|
|
||||||
|
This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
Also add information on how to contact you by electronic and paper mail.
|
||||||
|
|
||||||
|
If the program does terminal interaction, make it output a short
|
||||||
|
notice like this when it starts in an interactive mode:
|
||||||
|
|
||||||
|
<program> Copyright (C) <year> <name of author>
|
||||||
|
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||||
|
This is free software, and you are welcome to redistribute it
|
||||||
|
under certain conditions; type `show c' for details.
|
||||||
|
|
||||||
|
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||||
|
parts of the General Public License. Of course, your program's commands
|
||||||
|
might be different; for a GUI interface, you would use an "about box".
|
||||||
|
|
||||||
|
You should also get your employer (if you work as a programmer) or school,
|
||||||
|
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
||||||
|
For more information on this, and how to apply and follow the GNU GPL, see
|
||||||
|
<https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
The GNU General Public License does not permit incorporating your program
|
||||||
|
into proprietary programs. If your program is a subroutine library, you
|
||||||
|
may consider it more useful to permit linking proprietary applications with
|
||||||
|
the library. If this is what you want to do, use the GNU Lesser General
|
||||||
|
Public License instead of this License. But first, please read
|
||||||
|
<https://www.gnu.org/philosophy/why-not-lgpl.html>.
|
21
main.lua
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
-- bootstrap the compiler
|
||||||
|
|
||||||
|
local fennel = require("lib.fennel").install({correlate=true,
|
||||||
|
moduleName="lib.fennel"})
|
||||||
|
|
||||||
|
local make_love_searcher = function(env)
|
||||||
|
return function(module_name)
|
||||||
|
local path = module_name:gsub("%.", "/") .. ".fnl"
|
||||||
|
if love.filesystem.getInfo(path) then
|
||||||
|
return function(...)
|
||||||
|
local code = love.filesystem.read(path)
|
||||||
|
return fennel.eval(code, {env=env}, ...)
|
||||||
|
end, path
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
table.insert(package.loaders, make_love_searcher(_G))
|
||||||
|
table.insert(fennel["macro-searchers"], make_love_searcher("_COMPILER"))
|
||||||
|
|
||||||
|
require("wrap")
|
72
makefile
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
VERSION=0.1.0
|
||||||
|
LOVE_VERSION=11.5
|
||||||
|
NAME=change-me
|
||||||
|
ITCH_ACCOUNT=change-me-too
|
||||||
|
URL=https://gitlab.com/alexjgriffith/min-love2d-fennel
|
||||||
|
AUTHOR="Your Name"
|
||||||
|
DESCRIPTION="Minimal setup for trying out Phil Hagelberg's fennel/love game design process."
|
||||||
|
GITHUB_USERNAME := $(shell grep GITHUB_USERNAME credentials.private | cut -d= -f2)
|
||||||
|
GITHUB_PAT := $(shell grep GITHUB_PAT credentials.private | cut -d= -f2)
|
||||||
|
LIBS := $(wildcard lib/*)
|
||||||
|
LUA := $(wildcard *.lua)
|
||||||
|
SRC := $(wildcard *.fnl)
|
||||||
|
|
||||||
|
run: ; love .
|
||||||
|
|
||||||
|
count: ; cloc *.fnl
|
||||||
|
|
||||||
|
clean: ; rm -rf releases/*
|
||||||
|
|
||||||
|
LOVEFILE=releases/$(NAME)-$(VERSION).love
|
||||||
|
|
||||||
|
$(LOVEFILE): $(LUA) $(SRC) $(LIBS) assets
|
||||||
|
mkdir -p releases/
|
||||||
|
find $^ -type f | LC_ALL=C sort | env TZ=UTC zip -r -q -9 -X $@ -@
|
||||||
|
|
||||||
|
love: $(LOVEFILE)
|
||||||
|
|
||||||
|
# platform-specific distributables
|
||||||
|
|
||||||
|
REL=$(PWD)/buildtools/love-release.sh # https://p.hagelb.org/love-release.sh
|
||||||
|
FLAGS=-a "$(AUTHOR)" --description $(DESCRIPTION) \
|
||||||
|
--love $(LOVE_VERSION) --url $(URL) --version $(VERSION) --lovefile $(LOVEFILE)
|
||||||
|
|
||||||
|
releases/$(NAME)-$(VERSION)-x86_64.AppImage: $(LOVEFILE)
|
||||||
|
cd buildtools/appimage && \
|
||||||
|
./build.sh $(LOVE_VERSION) $(PWD)/$(LOVEFILE) $(GITHUB_USERNAME) $(GITHUB_PAT)
|
||||||
|
mv buildtools/appimage/game-x86_64.AppImage $@
|
||||||
|
|
||||||
|
releases/$(NAME)-$(VERSION)-macos.zip: $(LOVEFILE)
|
||||||
|
$(REL) $(FLAGS) -M
|
||||||
|
mv releases/$(NAME)-macos.zip $@
|
||||||
|
|
||||||
|
releases/$(NAME)-$(VERSION)-win.zip: $(LOVEFILE)
|
||||||
|
$(REL) $(FLAGS) -W32
|
||||||
|
mv releases/$(NAME)-win32.zip $@
|
||||||
|
|
||||||
|
releases/$(NAME)-$(VERSION)-web.zip: $(LOVEFILE)
|
||||||
|
buildtools/love-js/love-js.sh releases/$(NAME)-$(VERSION).love $(NAME) -v=$(VERSION) -a=$(AUTHOR) -o=releases
|
||||||
|
|
||||||
|
linux: releases/$(NAME)-$(VERSION)-x86_64.AppImage
|
||||||
|
mac: releases/$(NAME)-$(VERSION)-macos.zip
|
||||||
|
windows: releases/$(NAME)-$(VERSION)-win.zip
|
||||||
|
web: releases/$(NAME)-$(VERSION)-web.zip
|
||||||
|
|
||||||
|
|
||||||
|
runweb: $(LOVEFILE)
|
||||||
|
buildtools/love-js/love-js.sh $(LOVEFILE) $(NAME) -v=$(VERSION) -a=$(AUTHOR) -o=releases -r -n
|
||||||
|
# If you release on itch.io, you should install butler:
|
||||||
|
# https://itch.io/docs/butler/installing.html
|
||||||
|
|
||||||
|
uploadlinux: releases/$(NAME)-$(VERSION)-x86_64.AppImage
|
||||||
|
butler push $^ $(ITCH_ACCOUNT)/$(NAME):linux --userversion $(VERSION)
|
||||||
|
uploadmac: releases/$(NAME)-$(VERSION)-macos.zip
|
||||||
|
butler push $^ $(ITCH_ACCOUNT)/$(NAME):mac --userversion $(VERSION)
|
||||||
|
uploadwindows: releases/$(NAME)-$(VERSION)-win.zip
|
||||||
|
butler push $^ $(ITCH_ACCOUNT)/$(NAME):windows --userversion $(VERSION)
|
||||||
|
uploadweb: releases/$(NAME)-$(VERSION)-web.zip
|
||||||
|
butler push $^ $(ITCH_ACCOUNT)/$(NAME):web --userversion $(VERSION)
|
||||||
|
|
||||||
|
upload: uploadlinux uploadmac uploadwindows
|
||||||
|
|
||||||
|
release: linux mac windows upload cleansrc
|
27
mapper.fnl
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
;Generate a map of width by height:
|
||||||
|
(fn print_map [map]
|
||||||
|
(for [i 1 (length map)]
|
||||||
|
(var r "")
|
||||||
|
(for [j 1 (length (. map i))]
|
||||||
|
(set r (.. r " " (. map i j))))
|
||||||
|
(print r)))
|
||||||
|
|
||||||
|
; Function to generate the map. From here, all additional functions are used.
|
||||||
|
(fn generate [height width]
|
||||||
|
(var map [])
|
||||||
|
; Generate the outside shell. This is actually a double-lined outer layer. The
|
||||||
|
; outer-most layer allows for a single-square transitional tile, used as the
|
||||||
|
; entry/exit spot to the map.
|
||||||
|
(for [i 1 (+ height 2)]
|
||||||
|
(tset map i [])
|
||||||
|
(for [j 1 (+ width 2)]
|
||||||
|
(var tile 0) ; By default, draw an empty space
|
||||||
|
(when (or (= i 1) (= i 2) (= i (+ height 1)) (= i (+ height 2))) (set tile 1)) ; The first and last rows
|
||||||
|
(when (or (= j 1) (= j 2) (= j (+ width 1)) (= j (+ width 2))) (set tile 1)) ; The first and last rows
|
||||||
|
(tset map i j tile)))
|
||||||
|
; Add in "set pieces": structures or
|
||||||
|
map)
|
||||||
|
|
||||||
|
(print_map (generate 20 20))
|
||||||
|
|
||||||
|
{: generate}
|
140
min-love2d-fennel-readme.org
Normal file
|
@ -0,0 +1,140 @@
|
||||||
|
* Minimal Fennel Love2D Setup
|
||||||
|
|
||||||
|
In the lead up to the semi-annual [[https://itch.io/jam/autumn-lisp-game-jam-2018][Autumn Lisp Game Jam]] I thought I'd look into Phil Hegelberg's approach to last Aprils Jam, using [[https://love2d.org/][love2d]] in concert with [[https://fennel-lang.org/][fennel]]. Phil outlines his approach on his [[https://technomancy.us/187][blog]].
|
||||||
|
|
||||||
|
This repo contains the minimal viable setup to get started with Phil Hegelberg's game design process, plus some additional libraries.
|
||||||
|
|
||||||
|
* Alternatives
|
||||||
|
This repo is slowly expanding from a truly minimal setup to one that comes with a few batteries included. If you want a barebones setup to get you started check out:
|
||||||
|
[[https://sr.ht/~benthor/absolutely-minimal-love2d-fennel/][absolutely-minimal-love2d-fennel]] by @benthor.
|
||||||
|
|
||||||
|
If you want to just start coding up some fennel and love with no makefile or manual installation on linux check out [[https://gitlab.com/alexjgriffith/love-fennel][love-fennel]]
|
||||||
|
|
||||||
|
* Getting Started
|
||||||
|
The following commands will clone this project and duplicate its structure into a new folder =$PROJECT_NAME=
|
||||||
|
|
||||||
|
#+BEGIN_SRC bash
|
||||||
|
git clone https://gitlab.com/alexjgriffith/min-love2d-fennel.git
|
||||||
|
./min-love2d-fennel/.duplicate/new-game.sh $PROJECT_NAME
|
||||||
|
#+END_SRC
|
||||||
|
|
||||||
|
Check out the makefile and conf.lua files in =$PROJECT_NAME=, updating them with information relevant to your game.
|
||||||
|
|
||||||
|
You can enter =love .= in the =$PROJECT_NAME= directory to run your game, or =make run=. If you are on Windows, using =lovew .= will allow you to use the REPL.
|
||||||
|
|
||||||
|
The following lines with =Update= should be changed in the =makefile= and =love.conf= to reflect your game.
|
||||||
|
|
||||||
|
#+BEGIN_SRC makefile
|
||||||
|
VERSION=0.1.0
|
||||||
|
LOVE_VERSION=11.4
|
||||||
|
NAME=change-me # Update
|
||||||
|
ITCH_ACCOUNT=change-me-too # Update
|
||||||
|
URL=https://gitlab.com/alexjgriffith/min-love2d-fennel # Update
|
||||||
|
AUTHOR="Your Name" # Update
|
||||||
|
DESCRIPTION="Minimal setup for trying out Phil Hagelberg's fennel/love game design process." # Update
|
||||||
|
GITHUB_USERNAME := $(shell grep GITHUB_USERNAME credentials.private | cut -d= -f2) # Optional (needed for Love V 12.0)
|
||||||
|
GITHUB_PAT := $(shell grep GITHUB_PAT credentials.private | cut -d= -f2) # Optional (needed for Love V 12.0)
|
||||||
|
|
||||||
|
#+END_SRC
|
||||||
|
|
||||||
|
#+BEGIN_SRC lua
|
||||||
|
love.conf = function(t)
|
||||||
|
t.gammacorrect = true
|
||||||
|
t.title, t.identity = "change-me", "Minimal" -- Update
|
||||||
|
t.modules.joystick = false
|
||||||
|
t.modules.physics = false
|
||||||
|
t.window.width = 720
|
||||||
|
t.window.height = 450
|
||||||
|
t.window.vsync = false
|
||||||
|
t.version = "11.4"
|
||||||
|
end
|
||||||
|
#+END_SRC
|
||||||
|
|
||||||
|
* Emacs Setup
|
||||||
|
|
||||||
|
Once you install the latest version of [[https://gitlab.com/technomancy/fennel-mode][fennel-mode]], you can run
|
||||||
|
=C-u M-x fennel-repl= followed by =love .= to launch a repl.
|
||||||
|
|
||||||
|
* Default Project Structure
|
||||||
|
|
||||||
|
The =make= process as-is will only compile the contents of the root folder and the =lib/= folder+subfolders, so make sure to put your game files in either of those.
|
||||||
|
|
||||||
|
Specifically, every =.fnl= file needed at runtime needs to be situated in the root folder, and every file which is not a =.lua= or =.fnl= file needs to be put inside =lib/=.
|
||||||
|
|
||||||
|
In order to use macros, they have to be put in =.fnl= files inside =lib/=.
|
||||||
|
|
||||||
|
* Separate your Code into a /src directory
|
||||||
|
|
||||||
|
If you want a more opinionated layout, you can use pass in a =--layout= parameter when creating your project.
|
||||||
|
|
||||||
|
#+BEGIN_SRC bash
|
||||||
|
./min-love2d-fennel/.duplicate/new-game.sh $PROJECT_NAME --layout=seperate-source
|
||||||
|
#+END_SRC
|
||||||
|
|
||||||
|
This build uses =gamestate= rather than Phil's approach to scene separation and puts all your =.fnl= files into a =/src= directory. It also provides a separate makefile that handles this layout.
|
||||||
|
|
||||||
|
Note, any macros will have to be placed in the root of the project or in the =lib= folder (this can be modified in =main.lua=)
|
||||||
|
|
||||||
|
Presently the only layouts are =clone= and =seperate-source=. If you want to make your own check out the =.duplicate= directory to see how they work.
|
||||||
|
|
||||||
|
* Release Process
|
||||||
|
|
||||||
|
Use =make linux=, =make windows=, =make mac=, or =make web= to create targets for each platform, or =make release= to make all four. Check out the makefile for more commands, and remember to edit your game data in it!
|
||||||
|
|
||||||
|
* Adjusting the screen size
|
||||||
|
For those of us working with window managers it would be nice if our games behaved while we are developing. Below is code adapted from Phil's 2022 lisp game jam entry [[https://codeberg.org/technomancy/lisp-jam-2022/src/branch/main/wrap.fnl][https://codeberg.org/technomancy/lisp-jam-2022/src/branch/main/wrap.fnl]] . Adapt it to modify your =wrap.fnl= to handle window resizing automatically and adjust your mouse position.
|
||||||
|
|
||||||
|
*Note* this is _not a complete solution_. You still need to handle the translation of =love.mouse.getPos= and =love.graphics.inverseTransform=. But, if your game dosn't use those, the snippet below should work out of the box!
|
||||||
|
|
||||||
|
#+BEGIN_SRC fennel
|
||||||
|
;; define the size of your window. From your program's perspective
|
||||||
|
;; your window will always be this size regardless of size
|
||||||
|
(local window-w 1280)
|
||||||
|
(local window-h 720)
|
||||||
|
(var scale 1)
|
||||||
|
|
||||||
|
;; Love provides a handy resize callback. Hook into it to adjust the display size
|
||||||
|
;; of your window.
|
||||||
|
(fn love.resize [w h]
|
||||||
|
(set scale (math.floor (math.max 1 (math.min (/ w window-w)
|
||||||
|
(/ h window-h))))))
|
||||||
|
|
||||||
|
;; Changing the display size means that you need to translate from the "display size"
|
||||||
|
;; to the size your game thinks the window is.
|
||||||
|
(fn love.mousepressed [x y b]
|
||||||
|
(when mode.mousepressed
|
||||||
|
(safely #(mode.mousepressed (/ x scale) (/ y scale) b set-mode))))
|
||||||
|
|
||||||
|
(fn love.mousemoved [x y dx dy istouch]
|
||||||
|
(when mode.mousemoved
|
||||||
|
(safely #(mode.mousemoved (/ x scale) (/ y scale) (/ dx scale) (/ dy scale)
|
||||||
|
istouch))))
|
||||||
|
|
||||||
|
(fn love.mousereleased [x y b]
|
||||||
|
(when mode.mousereleased
|
||||||
|
(safely #(mode.mousereleased (/ x scale) (/ y scale) b set-mode))))
|
||||||
|
|
||||||
|
#+END_SRC
|
||||||
|
|
||||||
|
* Targeting the development branch of love (12.0) - LINUX ONLY
|
||||||
|
You can target the development branch of love (version 12.0) by setting the `LOVE_VERSION` parameter in the makefile to 12.0. Note that because we are working from a github artifact, rather than a release, you will also have to pass in your github username and a github PAT.
|
||||||
|
|
||||||
|
** Getting a PAT
|
||||||
|
To download artifacts created by the Github actions CI you will need to get an access token from "settings -> developer settings -> personal access tokens". The token needs `workflow` and `actions:read` permissions.
|
||||||
|
|
||||||
|
** Creating a credentials.private file
|
||||||
|
By default the makefile looks for `credentials.private` in the root directory of the project. `*.private` is part of `.gitignore` so personal information stored here will not be part of the git history or get pushed to a remote server.
|
||||||
|
|
||||||
|
The contents should look something like this:
|
||||||
|
#+BEGIN_SRC bash
|
||||||
|
GITHUB_USERNAME=username
|
||||||
|
GITHUB_PAT=PAT
|
||||||
|
#+END_SRC
|
||||||
|
|
||||||
|
Note: this is presently linux only, however it may be expanded in the future to cover macos and windows.
|
||||||
|
|
||||||
|
* Phil's Modal Callbacks (PMC)
|
||||||
|
|
||||||
|
Phil Hegelberg's [[https://gitlab.com/technomancy/exo-encounter-667/][exo-encounter-667]] is structured using a modal callback system. Each game state has a mode and each mode has a series of specific callbacks.
|
||||||
|
|
||||||
|
If you design your game as a series of states in a very simple state machine, for example *start-screen*, *play* and *end*, with unidirectional progression, you can easily separate the logic for each state into state/mode specific callbacks. As an example, in order to have state dependant rendering that differs between start-screen,play and end you could provide a *draw* callback for each of those states. Similarly if we need state dependent logic and keyboard input we could provide *update* and *keyboard* callbacks. As you iterate you can add and remove callbacks and states/modes as needed with very little friction.
|
114
mode-intro.fnl
Normal file
|
@ -0,0 +1,114 @@
|
||||||
|
(local overlay (require :overlay))
|
||||||
|
(local state (require :state))
|
||||||
|
(local mapper (require :mapper))
|
||||||
|
|
||||||
|
(love.graphics.setNewFont 10)
|
||||||
|
(local pi math.pi)
|
||||||
|
|
||||||
|
(var map (mapper.generate 10 10))
|
||||||
|
|
||||||
|
; This sets the wall texture data
|
||||||
|
(var walls [])
|
||||||
|
(var wall-textures (love.filesystem.getDirectoryItems "textures/walls"))
|
||||||
|
|
||||||
|
(each [_ v (ipairs wall-textures)]
|
||||||
|
(local wall {})
|
||||||
|
(tset wall :t (love.graphics.newImage (.. "textures/walls/" v)))
|
||||||
|
(tset wall :w (love.graphics.getWidth (. wall :t)))
|
||||||
|
(tset wall :h (love.graphics.getHeight (. wall :t)))
|
||||||
|
(table.insert walls wall))
|
||||||
|
|
||||||
|
; Map size
|
||||||
|
(var map-height (length map))
|
||||||
|
(var map-width (length (. map 1)))
|
||||||
|
|
||||||
|
; Player position and direction
|
||||||
|
(var player (state.getPlayer))
|
||||||
|
|
||||||
|
; Screen size
|
||||||
|
(var (screen-width screen-height) (love.window.getMode))
|
||||||
|
; (var screen-width 1280)
|
||||||
|
; (var screen-height 720)
|
||||||
|
(var texel-width 64)
|
||||||
|
(var texel-height 64)
|
||||||
|
|
||||||
|
; Ray-casting function
|
||||||
|
(fn cast-ray [ray-angle]
|
||||||
|
(local dx (math.cos ray-angle))
|
||||||
|
(local dy (math.sin ray-angle))
|
||||||
|
(var rays [])
|
||||||
|
|
||||||
|
(var (distance tex done hit-pos) (values 0 1 false {}))
|
||||||
|
(while (not done)
|
||||||
|
(var ray-x (+ player.x (* dx distance)))
|
||||||
|
(var ray-y (+ player.y (* dy distance)))
|
||||||
|
(set rays [ray-x ray-y])
|
||||||
|
|
||||||
|
; Check if ray hits a wall (>= 1) on the map
|
||||||
|
(if (or (>= (math.floor ray-x) map-width)
|
||||||
|
(>= (math.floor ray-y) map-height)
|
||||||
|
(<= ray-x 0)
|
||||||
|
(<= ray-y 0)
|
||||||
|
(>= (. map (math.floor ray-y) (math.floor ray-x)) 1))
|
||||||
|
(do
|
||||||
|
(set done true)
|
||||||
|
(set tex (. map (math.floor ray-y) (math.floor ray-x)))
|
||||||
|
(set hit-pos {:x (- ray-x (math.floor ray-x)) :y (- ray-y (math.floor ray-y))}))
|
||||||
|
; Increment distance
|
||||||
|
(set distance (+ distance 0.01))))
|
||||||
|
|
||||||
|
(values distance hit-pos tex rays))
|
||||||
|
|
||||||
|
; Function to handle player movement
|
||||||
|
(fn move-player [move-speed]
|
||||||
|
(let [new-x (+ player.x (* (math.cos player.d) move-speed))
|
||||||
|
new-y (+ player.y (* (math.sin player.d) move-speed))]
|
||||||
|
; Check for collisions with walls
|
||||||
|
(when (= (. map (math.floor new-y) (math.floor new-x)) 0)
|
||||||
|
(state.modO -1) (state.setX new-x) (state.setY new-y))))
|
||||||
|
|
||||||
|
; Draw function for rendering
|
||||||
|
{:draw (fn love.draw []
|
||||||
|
(love.graphics.clear)
|
||||||
|
|
||||||
|
; For each vertical slice of the screen
|
||||||
|
(var (last-x last-y) (values 0 0))
|
||||||
|
(for [i 0 (- screen-width 1)]
|
||||||
|
; Calculate angle of ray relative to player direction
|
||||||
|
(local ray-angle (+ player.d (- (* (/ i screen-width) player.f) (/ player.f 2))))
|
||||||
|
|
||||||
|
; Cast the ray to find distance to the nearest wall
|
||||||
|
(local (distance hit-pos tex rays) (values (cast-ray ray-angle)))
|
||||||
|
|
||||||
|
; Calculate height of the wall slice
|
||||||
|
(local wall-height (math.floor (/ screen-height distance)))
|
||||||
|
(local start-y (/ (- screen-height wall-height) 2))
|
||||||
|
(local end-y (/ (+ screen-height wall-height) 2))
|
||||||
|
|
||||||
|
; Draw a textured wall
|
||||||
|
(local wall-texture (. walls tex))
|
||||||
|
(var texture-x 0)
|
||||||
|
(if (> (/ (- (. rays 1) last-x) (- (. rays 2) last-y)) (/ (- (. rays 2) last-y) (- (. rays 1) last-x)))
|
||||||
|
(set texture-x (math.floor (* (. hit-pos :y) (. wall-texture :w))))
|
||||||
|
(set texture-x (math.floor (* (. hit-pos :x) (. wall-texture :w)))))
|
||||||
|
(love.graphics.draw (. wall-texture :t)
|
||||||
|
(love.graphics.newQuad texture-x 0 1 (. wall-texture :h) (. wall-texture :w) (. wall-texture :h))
|
||||||
|
i start-y 0 1 (/ wall-height (. wall-texture :h)))
|
||||||
|
(when (= 0 (% i 200))
|
||||||
|
(love.graphics.print (.. "r: " ray-angle) i 10)
|
||||||
|
(love.graphics.print (.. "h-x: " (. rays 1)) i 20)
|
||||||
|
(love.graphics.print (.. "h-y: " (. rays 2)) i 30)
|
||||||
|
)
|
||||||
|
(set (last-x last-y) (values (. rays 1) (. rays 2)))
|
||||||
|
)
|
||||||
|
|
||||||
|
(overlay.overlay)
|
||||||
|
(set player (state.getPlayer))
|
||||||
|
)
|
||||||
|
|
||||||
|
:keypressed (fn keypressed [key set-mode]
|
||||||
|
(when (= key "j") (state.setD (- player.d (/ pi 2))))
|
||||||
|
(when (= key "l") (state.setD (+ player.d (/ pi 2))))
|
||||||
|
(when (= key "i") (move-player 1))
|
||||||
|
(when (= key "k") (move-player -1))
|
||||||
|
(when (= key "x") (love.event.quit)))}
|
18
notes.md
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
# Notes
|
||||||
|
|
||||||
|
## Reminders
|
||||||
|
- `wrap.fnl` is the "entrypoint" for the project.
|
||||||
|
- Run the project via `make` or `love` (ex: `love .` from base directory)
|
||||||
|
|
||||||
|
## Ray-Casting Basics
|
||||||
|
Every frame, from left to right, a vertical slice is drawn. The slice can be a
|
||||||
|
part of a texture, or a color. Generally, the slice is drawn the full height of
|
||||||
|
the wall; half-walls are possible, and presumably so are windows. The slice to
|
||||||
|
be drawn is determined by sending a ray from the camera (or camera plane) in the
|
||||||
|
facing direction, until the ray intercepts with a wall. The walls, as well as
|
||||||
|
most other level details, are provided in a 2-d array, generally of numbers. In
|
||||||
|
this example, the number 0 represents open space, and every number above 0
|
||||||
|
represents a wall with a specific texture. The initial way of determining the
|
||||||
|
ray is using Euclidean geometry, but this can result in a fish-eye lens
|
||||||
|
appearance. Lode's tutorial uses vectors to establish a camera plane from which
|
||||||
|
all calculations are performed. This helps reduce the fish-eye appearance.
|
114
old-raycaster.fnl
Normal file
|
@ -0,0 +1,114 @@
|
||||||
|
(local overlay (require :overlay))
|
||||||
|
(local state (require :state))
|
||||||
|
(local mapper (require :mapper))
|
||||||
|
|
||||||
|
(love.graphics.setNewFont 10)
|
||||||
|
(local pi math.pi)
|
||||||
|
|
||||||
|
(var map (mapper.generate 10 10))
|
||||||
|
|
||||||
|
; This sets the wall texture data
|
||||||
|
(var walls [])
|
||||||
|
(var wall-textures (love.filesystem.getDirectoryItems "textures/walls"))
|
||||||
|
|
||||||
|
(each [_ v (ipairs wall-textures)]
|
||||||
|
(local wall {})
|
||||||
|
(tset wall :t (love.graphics.newImage (.. "textures/walls/" v)))
|
||||||
|
(tset wall :w (love.graphics.getWidth (. wall :t)))
|
||||||
|
(tset wall :h (love.graphics.getHeight (. wall :t)))
|
||||||
|
(table.insert walls wall))
|
||||||
|
|
||||||
|
; Map size
|
||||||
|
(var map-height (length map))
|
||||||
|
(var map-width (length (. map 1)))
|
||||||
|
|
||||||
|
; Player position and direction
|
||||||
|
(var player (state.getPlayer))
|
||||||
|
|
||||||
|
; Screen size
|
||||||
|
(var (screen-width screen-height) (love.window.getMode))
|
||||||
|
; (var screen-width 1280)
|
||||||
|
; (var screen-height 720)
|
||||||
|
(var texel-width 64)
|
||||||
|
(var texel-height 64)
|
||||||
|
|
||||||
|
; Ray-casting function
|
||||||
|
(fn cast-ray [ray-angle]
|
||||||
|
(local dx (math.cos ray-angle))
|
||||||
|
(local dy (math.sin ray-angle))
|
||||||
|
(var rays [])
|
||||||
|
|
||||||
|
(var (distance tex done hit-pos) (values 0 1 false {}))
|
||||||
|
(while (not done)
|
||||||
|
(var ray-x (+ player.x (* dx distance)))
|
||||||
|
(var ray-y (+ player.y (* dy distance)))
|
||||||
|
(set rays [ray-x ray-y])
|
||||||
|
|
||||||
|
; Check if ray hits a wall (>= 1) on the map
|
||||||
|
(if (or (>= (math.floor ray-x) map-width)
|
||||||
|
(>= (math.floor ray-y) map-height)
|
||||||
|
(<= ray-x 0)
|
||||||
|
(<= ray-y 0)
|
||||||
|
(>= (. map (math.floor ray-y) (math.floor ray-x)) 1))
|
||||||
|
(do
|
||||||
|
(set done true)
|
||||||
|
(set tex (. map (math.floor ray-y) (math.floor ray-x)))
|
||||||
|
(set hit-pos {:x (- ray-x (math.floor ray-x)) :y (- ray-y (math.floor ray-y))}))
|
||||||
|
; Increment distance
|
||||||
|
(set distance (+ distance 0.01))))
|
||||||
|
|
||||||
|
(values distance hit-pos tex rays))
|
||||||
|
|
||||||
|
; Function to handle player movement
|
||||||
|
(fn move-player [move-speed]
|
||||||
|
(let [new-x (+ player.x (* (math.cos player.d) move-speed))
|
||||||
|
new-y (+ player.y (* (math.sin player.d) move-speed))]
|
||||||
|
; Check for collisions with walls
|
||||||
|
(when (= (. map (math.floor new-y) (math.floor new-x)) 0)
|
||||||
|
(state.modO -1) (state.setX new-x) (state.setY new-y))))
|
||||||
|
|
||||||
|
; Draw function for rendering
|
||||||
|
{:draw (fn love.draw []
|
||||||
|
(love.graphics.clear)
|
||||||
|
|
||||||
|
; For each vertical slice of the screen
|
||||||
|
(var (last-x last-y) (values 0 0))
|
||||||
|
(for [i 0 (- screen-width 1)]
|
||||||
|
; Calculate angle of ray relative to player direction
|
||||||
|
(local ray-angle (+ player.d (- (* (/ i screen-width) player.f) (/ player.f 2))))
|
||||||
|
|
||||||
|
; Cast the ray to find distance to the nearest wall
|
||||||
|
(local (distance hit-pos tex rays) (values (cast-ray ray-angle)))
|
||||||
|
|
||||||
|
; Calculate height of the wall slice
|
||||||
|
(local wall-height (math.floor (/ screen-height distance)))
|
||||||
|
(local start-y (/ (- screen-height wall-height) 2))
|
||||||
|
(local end-y (/ (+ screen-height wall-height) 2))
|
||||||
|
|
||||||
|
; Draw a textured wall
|
||||||
|
(local wall-texture (. walls tex))
|
||||||
|
(var texture-x 0)
|
||||||
|
(if (> (/ (- (. rays 1) last-x) (- (. rays 2) last-y)) (/ (- (. rays 2) last-y) (- (. rays 1) last-x)))
|
||||||
|
(set texture-x (math.floor (* (. hit-pos :y) (. wall-texture :w))))
|
||||||
|
(set texture-x (math.floor (* (. hit-pos :x) (. wall-texture :w)))))
|
||||||
|
(love.graphics.draw (. wall-texture :t)
|
||||||
|
(love.graphics.newQuad texture-x 0 1 (. wall-texture :h) (. wall-texture :w) (. wall-texture :h))
|
||||||
|
i start-y 0 1 (/ wall-height (. wall-texture :h)))
|
||||||
|
(when (= 0 (% i 200))
|
||||||
|
(love.graphics.print (.. "r: " ray-angle) i 10)
|
||||||
|
(love.graphics.print (.. "h-x: " (. rays 1)) i 20)
|
||||||
|
(love.graphics.print (.. "h-y: " (. rays 2)) i 30)
|
||||||
|
)
|
||||||
|
(set (last-x last-y) (values (. rays 1) (. rays 2)))
|
||||||
|
)
|
||||||
|
|
||||||
|
(overlay.overlay)
|
||||||
|
(set player (state.getPlayer))
|
||||||
|
)
|
||||||
|
|
||||||
|
:keypressed (fn keypressed [key set-mode]
|
||||||
|
(when (= key "j") (state.setD (- player.d (/ pi 2))))
|
||||||
|
(when (= key "l") (state.setD (+ player.d (/ pi 2))))
|
||||||
|
(when (= key "i") (move-player 1))
|
||||||
|
(when (= key "k") (move-player -1))
|
||||||
|
(when (= key "x") (love.event.quit)))}
|
63
overlay.fnl
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
; Draw the overlay
|
||||||
|
; Relies on information about the player and the display
|
||||||
|
(local player (require :state))
|
||||||
|
(var screen-width 1280)
|
||||||
|
(var screen-height 720)
|
||||||
|
|
||||||
|
; This draws the oxygen ui
|
||||||
|
; Sequential blocks, up to 10; each block 10%
|
||||||
|
(fn oxygen-ui [x y]
|
||||||
|
(local (bx by bb o) (values 10 10 15 (player.getO)))
|
||||||
|
(local bn (math.floor (/ o 10)))
|
||||||
|
(var (dx dy) (values 0 50))
|
||||||
|
(local bp (* 5 (% o 10)))
|
||||||
|
(love.graphics.setColor 0 1 0 0.25)
|
||||||
|
(for [i 1 10]
|
||||||
|
(love.graphics.polygon "line" (+ x dx 1) (+ y by 1)
|
||||||
|
(+ x dx bx 1) (+ y by 1)
|
||||||
|
(+ x dx bx 1) (+ y by dy 1)
|
||||||
|
(+ x dx 1) (+ y by dy 1))
|
||||||
|
(if (<= i bn)
|
||||||
|
(love.graphics.polygon "fill" (+ x dx) (+ y by)
|
||||||
|
(+ x dx bx) (+ y by)
|
||||||
|
(+ x dx bx) (+ y by dy)
|
||||||
|
(+ x dx) (+ y by dy)))
|
||||||
|
(if (= i (+ bn 1))
|
||||||
|
(love.graphics.polygon "fill" (+ x dx) (+ y (- (+ dy by) bp))
|
||||||
|
(+ x dx bx) (+ y (- (+ dy by) bp))
|
||||||
|
(+ x dx bx) (+ y by dy)
|
||||||
|
(+ x dx) (+ y by dy)))
|
||||||
|
(set dx (+ dx bb))))
|
||||||
|
|
||||||
|
; This draws the power ui
|
||||||
|
; Inverse-sequential blocks, up to 10; each block 10%
|
||||||
|
(fn power-ui [x y]
|
||||||
|
(local (bx by bb p) (values 10 10 15 (player.getP)))
|
||||||
|
(local bn (math.floor (/ p 10)))
|
||||||
|
(var (dx dy) (values 0 50))
|
||||||
|
(local bp (* 5 (% p 10)))
|
||||||
|
(love.graphics.setColor 1 1 0 0.25)
|
||||||
|
(for [i 1 10]
|
||||||
|
(love.graphics.polygon "line" (- x dx 1) (+ y by 1)
|
||||||
|
(- x dx bx 1) (+ y by 1)
|
||||||
|
(- x dx bx 1) (+ y by dy 1)
|
||||||
|
(- x dx 1) (+ y by dy 1))
|
||||||
|
(if (<= i bn)
|
||||||
|
(love.graphics.polygon "fill" (- x dx) (+ y by)
|
||||||
|
(- x dx bx) (+ y by)
|
||||||
|
(- x dx bx) (+ y by dy)
|
||||||
|
(- x dx) (+ y by dy)))
|
||||||
|
(if (= i (+ bn 1))
|
||||||
|
(love.graphics.polygon "fill" (- x dx) (+ y (- (+ dy by) bp))
|
||||||
|
(- x dx bx) (+ y (- (+ dy by) bp))
|
||||||
|
(- x dx bx) (+ y by dy)
|
||||||
|
(- x dx) (+ y by dy)))
|
||||||
|
(set dx (+ dx bb))))
|
||||||
|
|
||||||
|
(fn overlay []
|
||||||
|
(oxygen-ui (+ (/ screen-width 2) 100) (- screen-height 100))
|
||||||
|
(power-ui (- (/ screen-width 2) 100) (- screen-height 100))
|
||||||
|
(love.graphics.setColor 1 1 1)
|
||||||
|
)
|
||||||
|
|
||||||
|
{: overlay}
|
BIN
pics/barrel.png
Executable file
After Width: | Height: | Size: 1.1 KiB |
BIN
pics/bluestone.png
Executable file
After Width: | Height: | Size: 2.5 KiB |
BIN
pics/colorstone.png
Executable file
After Width: | Height: | Size: 3.3 KiB |
BIN
pics/eagle.png
Executable file
After Width: | Height: | Size: 3.4 KiB |
BIN
pics/greenlight.png
Executable file
After Width: | Height: | Size: 393 B |
BIN
pics/greystone.png
Executable file
After Width: | Height: | Size: 3.6 KiB |
BIN
pics/mossy.png
Executable file
After Width: | Height: | Size: 4.2 KiB |
BIN
pics/pillar.png
Executable file
After Width: | Height: | Size: 1.5 KiB |
BIN
pics/purplestone.png
Executable file
After Width: | Height: | Size: 4.3 KiB |
BIN
pics/redbrick.png
Executable file
After Width: | Height: | Size: 3 KiB |
BIN
pics/wood.png
Executable file
After Width: | Height: | Size: 1.4 KiB |
132
ray-cast-vectors.fnl
Normal file
|
@ -0,0 +1,132 @@
|
||||||
|
; Much of this is derived from https://lodev.org/cgtutor/raycasting.html
|
||||||
|
(local pi math.pi)
|
||||||
|
(love.graphics.setColor 1 1 1)
|
||||||
|
(love.graphics.setNewFont 30)
|
||||||
|
|
||||||
|
; Define map (0 is empty space, 1 is wall)
|
||||||
|
(var map [[1 1 1 1 1 ]
|
||||||
|
[2 0 0 0 4 ]
|
||||||
|
[2 0 0 0 4 ]
|
||||||
|
[2 0 0 0 4 ]
|
||||||
|
[1 3 3 3 1 ]])
|
||||||
|
|
||||||
|
; This sets the wall texture data
|
||||||
|
(var walls [])
|
||||||
|
(var wall-textures (love.filesystem.getDirectoryItems "textures/walls"))
|
||||||
|
|
||||||
|
(each [_ v (ipairs wall-textures)]
|
||||||
|
(local wall {})
|
||||||
|
(tset wall :t (love.graphics.newImage (.. "textures/walls/" v)))
|
||||||
|
(tset wall :w (love.graphics.getWidth (. wall :t)))
|
||||||
|
(tset wall :h (love.graphics.getHeight (. wall :t)))
|
||||||
|
(table.insert walls wall))
|
||||||
|
|
||||||
|
; Map size
|
||||||
|
(var map-height (length map))
|
||||||
|
(var map-width (length (. map 1)))
|
||||||
|
|
||||||
|
; Player position and direction
|
||||||
|
; pos{x,y}: position vector of the player
|
||||||
|
; dir{x,y}: direction vector of the player
|
||||||
|
; pln{x,y}: camera plane of the player
|
||||||
|
(var player {:posx 3 :posy 3
|
||||||
|
:dirx -1 :diry 0
|
||||||
|
:plnx 0 :plny 0.66})
|
||||||
|
|
||||||
|
; Screen size
|
||||||
|
(var screen-width 1920)
|
||||||
|
(var screen-height 1080)
|
||||||
|
(var texel-width 64)
|
||||||
|
(var texel-height 64)
|
||||||
|
|
||||||
|
; Function to handle player movement
|
||||||
|
(fn move-player [move-speed]
|
||||||
|
(let [new-x (+ player.posx (* (math.cos player.dir) move-speed))
|
||||||
|
new-y (+ player.y (* (math.sin player.dir) move-speed))]
|
||||||
|
; Check for collisions with walls
|
||||||
|
(when (= (. map (math.floor new-y) (math.floor new-x)) 0)
|
||||||
|
(set player.x new-x)
|
||||||
|
(set player.y new-y))))
|
||||||
|
|
||||||
|
; Function to handle player rotation
|
||||||
|
(fn rotate-player [rot]
|
||||||
|
(local (o-dx o-px) (values player.dirx player.plnx))
|
||||||
|
(set player.dirx (- (* player.dirx (math.cos rot)) (* player.diry (math.sin rot))))
|
||||||
|
(set player.plnx (- (* player.plnx (math.cos rot)) (* player.plny (math.sin rot))))
|
||||||
|
(set player.diry (+ (* o-dx (math.sin rot)) (* player.diry (math.cos rot))))
|
||||||
|
(set player.plny (+ (* o-px (math.sin rot)) (* player.plny (math.cos rot))))
|
||||||
|
)
|
||||||
|
|
||||||
|
; Draw function for rendering
|
||||||
|
{:draw (fn love.draw []
|
||||||
|
(love.graphics.clear)
|
||||||
|
|
||||||
|
; For each vertical slice of the screen
|
||||||
|
(for [i 0 (- screen-width 1)]
|
||||||
|
; Setup a bunch of variables
|
||||||
|
; Camera Space x-coordinate, kind of the ray position
|
||||||
|
(local cam-x (- (/ (* 2 i) screen-width) 1))
|
||||||
|
; Ray direction vector values
|
||||||
|
(local (ray-dir-x ray-dir-y) (values (+ player.dirx (* player.plnx cam-x)) (+ player.diry (* player.plny cam-x))))
|
||||||
|
; Current player position on the map grid
|
||||||
|
(var (map-x map-y) (values (math.floor player.posx) (math.floor player.posy)))
|
||||||
|
; Length of ray from first x/y to the next x/y
|
||||||
|
(local (delta-dist-x delta-dist-y) (values
|
||||||
|
(math.sqrt (+ 1 (/ (* ray-dir-y ray-dir-y) (* ray-dir-x ray-dir-x))))
|
||||||
|
(math.sqrt (+ 1 (/ (* ray-dir-x ray-dir-x) (* ray-dir-y ray-dir-y))))))
|
||||||
|
; Side hit (n/s or e/w)
|
||||||
|
(var side nil)
|
||||||
|
; Calculate step and distance from player to first x/y-side (as per Love graphics grid)
|
||||||
|
(var (step-x side-dist-x) (if (< ray-dir-x 0)
|
||||||
|
(values -1 (* (- player.posx map-x) delta-dist-x))
|
||||||
|
(values 1 (* (- player.posx (+ map-x 1)) delta-dist-x))))
|
||||||
|
(var (step-y side-dist-y) (if (< ray-dir-y 0)
|
||||||
|
(values -1 (* (- player.posy map-y) delta-dist-y))
|
||||||
|
(values 1 (* (- player.posy (+ map-y 1)) delta-dist-y))))
|
||||||
|
; Shoot ze ray until it hits something
|
||||||
|
(var wall-hit false)
|
||||||
|
(while (not wall-hit)
|
||||||
|
(if (< side-dist-x side-dist-y)
|
||||||
|
(set (side-dist-x map-x side) (values (+ side-dist-x delta-dist-x) (+ map-x step-x) 0))
|
||||||
|
(set (side-dist-y map-y side) (values (+ side-dist-y delta-dist-y) (+ map-y step-y) 1)))
|
||||||
|
(set wall-hit (if (> (. map map-x map-y) 0) true false)))
|
||||||
|
; Set the perpindicular length from the camera plane to the wall
|
||||||
|
(var ray-length (if (= side 0) (- side-dist-x delta-dist-x) (- side-dist-y delta-dist-y)))
|
||||||
|
; Determine pixel-column height
|
||||||
|
(var line-height (math.floor (/ screen-height ray-length)))
|
||||||
|
(var (draw-start draw-end) (values (+ (/ (* -1 line-height) 2) (/ screen-height 2)) (+ (/ line-height 2) (/ screen-height 2))))
|
||||||
|
(if (< draw-start 0) (set draw-start 0))
|
||||||
|
(if (>= draw-end screen-height) (set draw-end (- screen-height 1)))
|
||||||
|
; Determine exactly where along the wall the ray hits
|
||||||
|
(var wall-col (if (= side 0) (+ player.posy (* ray-length ray-dir-y)) (+ player.posx (* ray-length ray-dir-x))))
|
||||||
|
(set wall-col (- wall-col (math.floor wall-col)))
|
||||||
|
; Select the texture data based on the grid number
|
||||||
|
(local wall-texture (. walls (. map map-x map-y)))
|
||||||
|
; Calculate the part of the texture to paint, and then do so
|
||||||
|
(var texture-x (math.floor (* wall-col (. wall-texture :w))))
|
||||||
|
(if (and (= side 0) (> ray-dir-x 0)) (set texture-x (- (- (. wall-texture :w) texture-x) 1)))
|
||||||
|
(if (and (= side 1) (< ray-dir-y 0)) (set texture-x (- (- (. wall-texture :w) texture-x) 1)))
|
||||||
|
(love.graphics.setColor 1 1 1)
|
||||||
|
(love.graphics.draw (. wall-texture :t)
|
||||||
|
(love.graphics.newQuad texture-x 0 1 (. wall-texture :h) (. wall-texture :w) (. wall-texture :h))
|
||||||
|
i draw-start 0 1 (/ line-height (. wall-texture :h)))
|
||||||
|
; Draw simple lines
|
||||||
|
; (love.graphics.line i draw-start i draw-end)
|
||||||
|
(love.graphics.setColor 1 0 0)
|
||||||
|
(love.graphics.print (.. "player-x: " player.posx ", player-y:" player.posy) 50 300)
|
||||||
|
(love.graphics.print (.. "map-x: " map-x ", map-y:" map-y ", val:" (. map map-x map-y)) 50 330)
|
||||||
|
(love.graphics.print (.. "side: " side ", ray-length:" ray-length) 50 370)
|
||||||
|
(love.graphics.print (.. "step-x: " step-x ", step-y:" step-y) 50 400)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
:update (fn update [dt]
|
||||||
|
(when (love.keyboard.isDown "j") (rotate-player 0.1))
|
||||||
|
(when (love.keyboard.isDown "l") (rotate-player -0.1)))
|
||||||
|
|
||||||
|
:keypressed (fn keypressed [key set-mode]
|
||||||
|
(when (= key "j") (rotate-player 0.05))
|
||||||
|
(when (= key "l") (rotate-player -0.05))
|
||||||
|
(when (= key "i") (move-player 1))
|
||||||
|
(when (= key "k") (move-player -1))
|
||||||
|
(when (= key "x") (love.event.quit)))}
|
61
ray-cast.fnl
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
(local pi (math.pi))
|
||||||
|
|
||||||
|
; Define map (0 is empty space, 1 is wall)
|
||||||
|
(var map [[1 1 1 1 1]
|
||||||
|
[1 0 0 0 1]
|
||||||
|
[1 0 1 0 1]
|
||||||
|
[1 0 0 0 1]
|
||||||
|
[1 1 1 1 1]])
|
||||||
|
|
||||||
|
; Map size
|
||||||
|
(var map-width (length map))
|
||||||
|
(var map-height (length (first map)))
|
||||||
|
|
||||||
|
; Player position and direction
|
||||||
|
(var player {:x 2.5 :y 2.5 :dir 0 :fov (/ pi 3)})
|
||||||
|
|
||||||
|
; Screen size
|
||||||
|
(var screen-width 640)
|
||||||
|
(var screen-height 480)
|
||||||
|
|
||||||
|
; Ray-casting function
|
||||||
|
(fn cast-ray [ray-angle]
|
||||||
|
(local dx (math.cos ray-angle))
|
||||||
|
(local dy (math.sin ray-angle))
|
||||||
|
|
||||||
|
(var distance 0)
|
||||||
|
(while true
|
||||||
|
(var ray-x (+ player.x (* dx distance)))
|
||||||
|
(var ray-y (+ player.y (* dy distance)))
|
||||||
|
|
||||||
|
; Check if ray hits a wall (1) on the map
|
||||||
|
(when (or (>= (math.floor ray-x) map-width)
|
||||||
|
(>= (math.floor ray-y) map-height)
|
||||||
|
(<= ray-x 0)
|
||||||
|
(<= ray-y 0)
|
||||||
|
(= (nth (nth map (math.floor ray-y)) (math.floor ray-x)) 1))
|
||||||
|
(return distance))
|
||||||
|
|
||||||
|
; Increment distance
|
||||||
|
(set distance (+ distance 0.01))))
|
||||||
|
|
||||||
|
; Draw function for rendering
|
||||||
|
(fn love.draw []
|
||||||
|
(love.graphics.clear)
|
||||||
|
|
||||||
|
; For each vertical slice of the screen
|
||||||
|
(for [i 0 (- screen-width 1)]
|
||||||
|
; Calculate angle of ray relative to player direction
|
||||||
|
(local ray-angle (+ player.dir
|
||||||
|
(- (* (/ i screen-width) player.fov) (/ player.fov 2))))
|
||||||
|
|
||||||
|
; Cast the ray to find distance to the nearest wall
|
||||||
|
(local distance (cast-ray ray-angle))
|
||||||
|
|
||||||
|
; Calculate height of the wall slice
|
||||||
|
(local wall-height (math.floor (/ screen-height distance)))
|
||||||
|
|
||||||
|
; Draw the wall slice (centered vertically)
|
||||||
|
(love.graphics.line i (/ (- screen-height wall-height) 2)
|
||||||
|
i (/ (+ screen-height wall-height) 2))))
|
||||||
|
|
206
raycaster.fnl
Normal file
|
@ -0,0 +1,206 @@
|
||||||
|
(local state (require :state))
|
||||||
|
(local mapper (require :mapper))
|
||||||
|
(local overlay (require :overlay))
|
||||||
|
(local pi math.pi)
|
||||||
|
; ### Screen Size ###
|
||||||
|
(var (screen-width screen-height) (love.window.getMode))
|
||||||
|
|
||||||
|
; ### Map Information ###
|
||||||
|
(var map (mapper.generate 15 15))
|
||||||
|
; (var map [[1 1 1 1 1 1 1 1 1 1 1 ]
|
||||||
|
; [2 0 0 0 0 0 0 0 0 0 4 ]
|
||||||
|
; [2 0 0 0 0 0 0 0 0 0 4 ]
|
||||||
|
; [2 0 0 0 0 0 0 0 0 0 4 ]
|
||||||
|
; [2 0 0 0 0 0 0 0 0 0 4 ]
|
||||||
|
; [2 0 0 0 0 0 0 0 0 0 4 ]
|
||||||
|
; [2 0 0 0 0 0 0 0 3 0 4 ]
|
||||||
|
; [2 0 0 0 0 0 0 3 3 0 4 ]
|
||||||
|
; [2 0 0 0 0 0 0 0 0 0 4 ]
|
||||||
|
; [1 1 1 1 1 1 1 1 1 1 1 ]])
|
||||||
|
|
||||||
|
; ### Texture Information ###
|
||||||
|
(var walls [])
|
||||||
|
(var wall-textures (love.filesystem.getDirectoryItems "textures/walls"))
|
||||||
|
(var (tex-height tex-width) (values 64 64))
|
||||||
|
|
||||||
|
(each [_ v (ipairs wall-textures)]
|
||||||
|
(local wall {})
|
||||||
|
(tset wall :t (love.graphics.newImage (.. "textures/walls/" v)))
|
||||||
|
(tset wall :w (love.graphics.getWidth (. wall :t)))
|
||||||
|
(tset wall :h (love.graphics.getHeight (. wall :t)))
|
||||||
|
(table.insert walls wall))
|
||||||
|
|
||||||
|
; ### "Player" variables ###
|
||||||
|
(var (posx posy) (values 4.0 4.0)) ; Initial map position
|
||||||
|
(var (dirx diry) (values -1.0 0.0)) ; Initial direction vector
|
||||||
|
(var (planex planey) (values 0 0.80)) ; Camera plane
|
||||||
|
|
||||||
|
{
|
||||||
|
:draw (fn love.draw []
|
||||||
|
(for [i 0 screen-width]
|
||||||
|
; Calculate ray position and direction
|
||||||
|
; Originals, giving a fish-eye lens effect:
|
||||||
|
; (var camerax (/ (* 2 i) (- screen-width 1)))
|
||||||
|
; (var (ray-dir-x ray-dir-y) (values (+ dirx (* planex camerax)) (+ diry (* planey camerax))))
|
||||||
|
(var camerax (/ (* 2 i) (- screen-width 1)))
|
||||||
|
(var (ray-dir-x ray-dir-y) (values (+ dirx (* planex camerax)) (+ diry (* planey camerax))))
|
||||||
|
|
||||||
|
; Which map square we're in
|
||||||
|
(var (mapx mapy) (values (math.floor posx) (math.floor posy)))
|
||||||
|
|
||||||
|
; Length of ray from current position to next x or y side
|
||||||
|
(var (side-dist-x side-dist-y) (values 0 0))
|
||||||
|
|
||||||
|
; Length of ray from one x or y side to the next x or y side
|
||||||
|
; (var (delta-dist-x delta-dist-y) (values
|
||||||
|
; (math.sqrt (+ 1 (/ (* ray-dir-y ray-dir-y) (* ray-dir-x ray-dir-x))))
|
||||||
|
; (math.sqrt (+ 1 (/ (* ray-dir-x ray-dir-x) (* ray-dir-y ray-dir-y))))))
|
||||||
|
(var (delta-dist-x delta-dist-y) (values (math.abs (/ 1 ray-dir-x)) (math.abs (/ 1 ray-dir-y))))
|
||||||
|
|
||||||
|
(var perp-wall-dist 0)
|
||||||
|
|
||||||
|
; Direction to step in, x or y
|
||||||
|
(var (stepx stepy) (values 0 0))
|
||||||
|
|
||||||
|
; Side the ray hits, x (0) or y (1)
|
||||||
|
(var side 0)
|
||||||
|
|
||||||
|
; Calculate step and initial side-dist-*
|
||||||
|
(if (< ray-dir-x 0)
|
||||||
|
(do
|
||||||
|
(set stepx -1)
|
||||||
|
(set side-dist-x (* (- posx mapx) delta-dist-x)))
|
||||||
|
(do
|
||||||
|
(set stepx 1)
|
||||||
|
(set side-dist-x (* (- (+ mapx 1.0) posx) delta-dist-x))))
|
||||||
|
(if (< ray-dir-y 0)
|
||||||
|
(do
|
||||||
|
(set stepy -1)
|
||||||
|
(set side-dist-y (* (- posy mapy) delta-dist-y)))
|
||||||
|
(do
|
||||||
|
(set stepy 1)
|
||||||
|
(set side-dist-y (* (- (+ mapy 1.0) posy) delta-dist-y))))
|
||||||
|
|
||||||
|
; Perform DDA (Digital Differential Analysis)
|
||||||
|
(var hit false)
|
||||||
|
(while (not hit)
|
||||||
|
(if (< side-dist-x side-dist-y)
|
||||||
|
(do
|
||||||
|
(set side-dist-x (+ side-dist-x delta-dist-x))
|
||||||
|
(set mapx (+ mapx stepx))
|
||||||
|
(set side 0))
|
||||||
|
(do
|
||||||
|
(set side-dist-y (+ side-dist-y delta-dist-y))
|
||||||
|
(set mapy (+ mapy stepy))
|
||||||
|
(set side 1)))
|
||||||
|
(if (> (. map mapx mapy) 0) (set hit true)))
|
||||||
|
|
||||||
|
; Calculate distance of perpendicular ray, to avoid fisheye effect
|
||||||
|
(if (= side 0)
|
||||||
|
(set perp-wall-dist (- side-dist-x delta-dist-x))
|
||||||
|
(set perp-wall-dist (- side-dist-y delta-dist-y)))
|
||||||
|
|
||||||
|
; Calculate height of line to draw on screen
|
||||||
|
(var line-height (/ screen-height perp-wall-dist))
|
||||||
|
|
||||||
|
; Calculate lowest and highest pixel to fill in current stripe
|
||||||
|
(var draw-start (+ (/ (- line-height) 2) (/ screen-height 2)))
|
||||||
|
(if (< draw-start 0) (set draw-start 0))
|
||||||
|
(var draw-end (+ (/ line-height 2) (/ screen-height 2)))
|
||||||
|
(if (>= draw-end screen-height) (set draw-end (- screen-height 1)))
|
||||||
|
|
||||||
|
; Draw wall slice
|
||||||
|
; (var color [1 1 1 1])
|
||||||
|
; (case (. map mapx mapy)
|
||||||
|
; 1 (set color [1 1 1 1])
|
||||||
|
; 2 (set color [1 0 0 1])
|
||||||
|
; 3 (set color [0 1 0 1])
|
||||||
|
; 4 (set color [0 0 1 1])
|
||||||
|
; )
|
||||||
|
|
||||||
|
; (when (= side 1)
|
||||||
|
; (tset color 1 (/ (. color 1) 2))
|
||||||
|
; (tset color 2 (/ (. color 2) 2))
|
||||||
|
; (tset color 3 (/ (. color 3) 2)))
|
||||||
|
|
||||||
|
; (love.graphics.setColor color)
|
||||||
|
; (love.graphics.line i draw-start i draw-end)
|
||||||
|
; (love.graphics.setColor 1 1 1)
|
||||||
|
|
||||||
|
; Draw textured wall
|
||||||
|
;; Choose texture
|
||||||
|
(var tex-num (. walls (. map mapx mapy)))
|
||||||
|
;; Calculate exactly where the wall was hit
|
||||||
|
(var wallx 0)
|
||||||
|
(if (= side 0)
|
||||||
|
(set wallx (+ posy (* perp-wall-dist ray-dir-y)))
|
||||||
|
(set wallx (+ posx (* perp-wall-dist ray-dir-x))))
|
||||||
|
(set wallx (- wallx (math.floor wallx)))
|
||||||
|
;; Find the x-coordinate on the texture
|
||||||
|
(var tex-x (math.floor (* wallx (. tex-num :w))))
|
||||||
|
(if (and (= side 0) (> ray-dir-x 0)) (set tex-x (- (. tex-num :w) tex-x 1)))
|
||||||
|
(if (and (= side 1) (< ray-dir-y 0)) (set tex-x (- (. tex-num :w) tex-x 1)))
|
||||||
|
;; Draw the texture
|
||||||
|
(love.graphics.draw (. tex-num :t)
|
||||||
|
(love.graphics.newQuad tex-x 0 1 (. tex-num :h) (. tex-num :w) (. tex-num :h))
|
||||||
|
i draw-start 0 1 (/ line-height (. tex-num :h)))
|
||||||
|
)
|
||||||
|
|
||||||
|
(overlay.overlay)
|
||||||
|
)
|
||||||
|
|
||||||
|
:update (fn update [dt]
|
||||||
|
(var mvspeed (* dt 3.0))
|
||||||
|
(var rtspeed (* dt 1.0))
|
||||||
|
|
||||||
|
(when (love.keyboard.isDown "up")
|
||||||
|
(when (= 0 (. map (math.floor (+ (* mvspeed dirx) posx)) (math.floor posy)))
|
||||||
|
(set posx (+ (* mvspeed dirx) posx)))
|
||||||
|
(when (= 0 (. map (math.floor posx) (math.floor (+ (* mvspeed diry) posy))))
|
||||||
|
(set posy (+ (* mvspeed diry) posy)))
|
||||||
|
)
|
||||||
|
|
||||||
|
(when (love.keyboard.isDown "down")
|
||||||
|
(when (= 0 (. map (math.floor (+ (* mvspeed dirx) posx)) (math.floor posy)))
|
||||||
|
(set posx (- posx (* mvspeed dirx))))
|
||||||
|
(when (= 0 (. map (math.floor posx) (math.floor (+ (* mvspeed diry) posy))))
|
||||||
|
(set posy (- posy (* mvspeed diry))))
|
||||||
|
)
|
||||||
|
|
||||||
|
(when (love.keyboard.isDown "right")
|
||||||
|
(var old-dirx dirx)
|
||||||
|
(set dirx (- (* dirx (math.cos (- rtspeed))) (* diry (math.sin (- rtspeed)))))
|
||||||
|
(set diry (+ (* old-dirx (math.sin (- rtspeed))) (* diry (math.cos (- rtspeed)))))
|
||||||
|
(var old-planex planex)
|
||||||
|
(set planex (- (* planex (math.cos (- rtspeed))) (* planey (math.sin (- rtspeed)))))
|
||||||
|
(set planey (+ (* old-planex (math.sin (- rtspeed))) (* planey (math.cos (- rtspeed)))))
|
||||||
|
)
|
||||||
|
|
||||||
|
(when (love.keyboard.isDown "left")
|
||||||
|
(var old-dirx dirx)
|
||||||
|
(set dirx (- (* dirx (math.cos rtspeed)) (* diry (math.sin rtspeed))))
|
||||||
|
(set diry (+ (* old-dirx (math.sin rtspeed)) (* diry (math.cos rtspeed))))
|
||||||
|
(var old-planex planex)
|
||||||
|
(set planex (- (* planex (math.cos rtspeed)) (* planey (math.sin rtspeed))))
|
||||||
|
(set planey (+ (* old-planex (math.sin rtspeed)) (* planey (math.cos rtspeed))))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
:keypressed (fn keypressed [k]
|
||||||
|
(when (= k "x") (love.event.quit))
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
; (fn move-player [x]
|
||||||
|
; (let [new-x (+ posx (* (math.cos dirx)) x) new-y (+ posy (* (math.sin diry)) x)]
|
||||||
|
; (when (= (. map (math.floor new-y) (math.floor new-x)) 0)
|
||||||
|
; (set posx new-x) (set posy new-y))))
|
||||||
|
|
||||||
|
; (fn rotate-player [d]
|
||||||
|
; (local rotation-speed 1)
|
||||||
|
; (var old-dirx dirx)
|
||||||
|
; (set dirx (- (* dirx (math.cos (* d rotation-speed))) (* diry (math.sin (* d rotation-speed)))))
|
||||||
|
; (set diry (+ (* old-dirx (math.sin (* d rotation-speed))) (* diry (math.cos (* d rotation-speed)))))
|
||||||
|
; (var old-planex planex)
|
||||||
|
; (set planex (- (* planex (math.cos (* d rotation-speed))) (* planey (math.sin (* d rotation-speed)))))
|
||||||
|
; (set planey (+ (* old-planex (math.sin (* d rotation-speed))) (* planey (math.cos (* d rotation-speed))))))
|
242
raycaster_flat.cpp
Normal file
|
@ -0,0 +1,242 @@
|
||||||
|
/*
|
||||||
|
Copyright (c) 2004-2021, Lode Vandevenne
|
||||||
|
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
|
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||||
|
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||||
|
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
|
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||||
|
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||||
|
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||||
|
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <cmath>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "quickcg.h"
|
||||||
|
using namespace QuickCG;
|
||||||
|
|
||||||
|
/*
|
||||||
|
g++ *.cpp -lSDL -O3 -W -Wall -ansi -pedantic
|
||||||
|
g++ *.cpp -lSDL
|
||||||
|
*/
|
||||||
|
|
||||||
|
//place the example code below here:
|
||||||
|
|
||||||
|
#define screenWidth 640
|
||||||
|
#define screenHeight 480
|
||||||
|
#define mapWidth 24
|
||||||
|
#define mapHeight 24
|
||||||
|
|
||||||
|
int worldMap[mapWidth][mapHeight]=
|
||||||
|
{
|
||||||
|
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
|
||||||
|
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
|
||||||
|
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
|
||||||
|
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
|
||||||
|
{1,0,0,0,0,0,2,2,2,2,2,0,0,0,0,3,0,3,0,3,0,0,0,1},
|
||||||
|
{1,0,0,0,0,0,2,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,1},
|
||||||
|
{1,0,0,0,0,0,2,0,0,0,2,0,0,0,0,3,0,0,0,3,0,0,0,1},
|
||||||
|
{1,0,0,0,0,0,2,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,1},
|
||||||
|
{1,0,0,0,0,0,2,2,0,2,2,0,0,0,0,3,0,3,0,3,0,0,0,1},
|
||||||
|
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
|
||||||
|
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
|
||||||
|
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
|
||||||
|
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
|
||||||
|
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
|
||||||
|
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
|
||||||
|
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
|
||||||
|
{1,4,4,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
|
||||||
|
{1,4,0,4,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
|
||||||
|
{1,4,0,0,0,0,5,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
|
||||||
|
{1,4,0,4,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
|
||||||
|
{1,4,0,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
|
||||||
|
{1,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
|
||||||
|
{1,4,4,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
|
||||||
|
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
|
||||||
|
};
|
||||||
|
|
||||||
|
int main(int /*argc*/, char */*argv*/[])
|
||||||
|
{
|
||||||
|
double posX = 22, posY = 12; //x and y start position
|
||||||
|
double dirX = -1, dirY = 0; //initial direction vector
|
||||||
|
double planeX = 0, planeY = 0.66; //the 2d raycaster version of camera plane
|
||||||
|
|
||||||
|
double time = 0; //time of current frame
|
||||||
|
double oldTime = 0; //time of previous frame
|
||||||
|
|
||||||
|
screen(screenWidth, screenHeight, 0, "Raycaster");
|
||||||
|
while(!done())
|
||||||
|
{
|
||||||
|
for(int x = 0; x < w; x++)
|
||||||
|
{
|
||||||
|
//calculate ray position and direction
|
||||||
|
double cameraX = 2 * x / (double)w - 1; //x-coordinate in camera space
|
||||||
|
double rayDirX = dirX + planeX * cameraX;
|
||||||
|
double rayDirY = dirY + planeY * cameraX;
|
||||||
|
//which box of the map we're in
|
||||||
|
int mapX = int(posX);
|
||||||
|
int mapY = int(posY);
|
||||||
|
|
||||||
|
//length of ray from current position to next x or y-side
|
||||||
|
double sideDistX;
|
||||||
|
double sideDistY;
|
||||||
|
|
||||||
|
//length of ray from one x or y-side to next x or y-side
|
||||||
|
//these are derived as:
|
||||||
|
//deltaDistX = sqrt(1 + (rayDirY * rayDirY) / (rayDirX * rayDirX))
|
||||||
|
//deltaDistY = sqrt(1 + (rayDirX * rayDirX) / (rayDirY * rayDirY))
|
||||||
|
//which can be simplified to abs(|rayDir| / rayDirX) and abs(|rayDir| / rayDirY)
|
||||||
|
//where |rayDir| is the length of the vector (rayDirX, rayDirY). Its length,
|
||||||
|
//unlike (dirX, dirY) is not 1, however this does not matter, only the
|
||||||
|
//ratio between deltaDistX and deltaDistY matters, due to the way the DDA
|
||||||
|
//stepping further below works. So the values can be computed as below.
|
||||||
|
// Division through zero is prevented, even though technically that's not
|
||||||
|
// needed in C++ with IEEE 754 floating point values.
|
||||||
|
double deltaDistX = (rayDirX == 0) ? 1e30 : std::abs(1 / rayDirX);
|
||||||
|
double deltaDistY = (rayDirY == 0) ? 1e30 : std::abs(1 / rayDirY);
|
||||||
|
|
||||||
|
double perpWallDist;
|
||||||
|
|
||||||
|
//what direction to step in x or y-direction (either +1 or -1)
|
||||||
|
int stepX;
|
||||||
|
int stepY;
|
||||||
|
|
||||||
|
int hit = 0; //was there a wall hit?
|
||||||
|
int side; //was a NS or a EW wall hit?
|
||||||
|
//calculate step and initial sideDist
|
||||||
|
if(rayDirX < 0)
|
||||||
|
{
|
||||||
|
stepX = -1;
|
||||||
|
sideDistX = (posX - mapX) * deltaDistX;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
stepX = 1;
|
||||||
|
sideDistX = (mapX + 1.0 - posX) * deltaDistX;
|
||||||
|
}
|
||||||
|
if(rayDirY < 0)
|
||||||
|
{
|
||||||
|
stepY = -1;
|
||||||
|
sideDistY = (posY - mapY) * deltaDistY;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
stepY = 1;
|
||||||
|
sideDistY = (mapY + 1.0 - posY) * deltaDistY;
|
||||||
|
}
|
||||||
|
//perform DDA
|
||||||
|
while(hit == 0)
|
||||||
|
{
|
||||||
|
//jump to next map square, either in x-direction, or in y-direction
|
||||||
|
if(sideDistX < sideDistY)
|
||||||
|
{
|
||||||
|
sideDistX += deltaDistX;
|
||||||
|
mapX += stepX;
|
||||||
|
side = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
sideDistY += deltaDistY;
|
||||||
|
mapY += stepY;
|
||||||
|
side = 1;
|
||||||
|
}
|
||||||
|
//Check if ray has hit a wall
|
||||||
|
if(worldMap[mapX][mapY] > 0) hit = 1;
|
||||||
|
}
|
||||||
|
//Calculate distance projected on camera direction. This is the shortest distance from the point where the wall is
|
||||||
|
//hit to the camera plane. Euclidean to center camera point would give fisheye effect!
|
||||||
|
//This can be computed as (mapX - posX + (1 - stepX) / 2) / rayDirX for side == 0, or same formula with Y
|
||||||
|
//for size == 1, but can be simplified to the code below thanks to how sideDist and deltaDist are computed:
|
||||||
|
//because they were left scaled to |rayDir|. sideDist is the entire length of the ray above after the multiple
|
||||||
|
//steps, but we subtract deltaDist once because one step more into the wall was taken above.
|
||||||
|
if(side == 0) perpWallDist = (sideDistX - deltaDistX);
|
||||||
|
else perpWallDist = (sideDistY - deltaDistY);
|
||||||
|
|
||||||
|
//Calculate height of line to draw on screen
|
||||||
|
int lineHeight = (int)(h / perpWallDist);
|
||||||
|
|
||||||
|
//calculate lowest and highest pixel to fill in current stripe
|
||||||
|
int drawStart = -lineHeight / 2 + h / 2;
|
||||||
|
if(drawStart < 0) drawStart = 0;
|
||||||
|
int drawEnd = lineHeight / 2 + h / 2;
|
||||||
|
if(drawEnd >= h) drawEnd = h - 1;
|
||||||
|
|
||||||
|
//choose wall color
|
||||||
|
ColorRGB color;
|
||||||
|
switch(worldMap[mapX][mapY])
|
||||||
|
{
|
||||||
|
case 1: color = RGB_Red; break; //red
|
||||||
|
case 2: color = RGB_Green; break; //green
|
||||||
|
case 3: color = RGB_Blue; break; //blue
|
||||||
|
case 4: color = RGB_White; break; //white
|
||||||
|
default: color = RGB_Yellow; break; //yellow
|
||||||
|
}
|
||||||
|
|
||||||
|
//give x and y sides different brightness
|
||||||
|
if(side == 1) {color = color / 2;}
|
||||||
|
|
||||||
|
//draw the pixels of the stripe as a vertical line
|
||||||
|
verLine(x, drawStart, drawEnd, color);
|
||||||
|
}
|
||||||
|
//timing for input and FPS counter
|
||||||
|
oldTime = time;
|
||||||
|
time = getTicks();
|
||||||
|
double frameTime = (time - oldTime) / 1000.0; //frameTime is the time this frame has taken, in seconds
|
||||||
|
print(1.0 / frameTime); //FPS counter
|
||||||
|
redraw();
|
||||||
|
cls();
|
||||||
|
|
||||||
|
//speed modifiers
|
||||||
|
double moveSpeed = frameTime * 5.0; //the constant value is in squares/second
|
||||||
|
double rotSpeed = frameTime * 3.0; //the constant value is in radians/second
|
||||||
|
readKeys();
|
||||||
|
//move forward if no wall in front of you
|
||||||
|
if(keyDown(SDLK_UP))
|
||||||
|
{
|
||||||
|
if(worldMap[int(posX + dirX * moveSpeed)][int(posY)] == false) posX += dirX * moveSpeed;
|
||||||
|
if(worldMap[int(posX)][int(posY + dirY * moveSpeed)] == false) posY += dirY * moveSpeed;
|
||||||
|
}
|
||||||
|
//move backwards if no wall behind you
|
||||||
|
if(keyDown(SDLK_DOWN))
|
||||||
|
{
|
||||||
|
if(worldMap[int(posX - dirX * moveSpeed)][int(posY)] == false) posX -= dirX * moveSpeed;
|
||||||
|
if(worldMap[int(posX)][int(posY - dirY * moveSpeed)] == false) posY -= dirY * moveSpeed;
|
||||||
|
}
|
||||||
|
//rotate to the right
|
||||||
|
if(keyDown(SDLK_RIGHT))
|
||||||
|
{
|
||||||
|
//both camera direction and camera plane must be rotated
|
||||||
|
double oldDirX = dirX;
|
||||||
|
dirX = dirX * cos(-rotSpeed) - dirY * sin(-rotSpeed);
|
||||||
|
dirY = oldDirX * sin(-rotSpeed) + dirY * cos(-rotSpeed);
|
||||||
|
double oldPlaneX = planeX;
|
||||||
|
planeX = planeX * cos(-rotSpeed) - planeY * sin(-rotSpeed);
|
||||||
|
planeY = oldPlaneX * sin(-rotSpeed) + planeY * cos(-rotSpeed);
|
||||||
|
}
|
||||||
|
//rotate to the left
|
||||||
|
if(keyDown(SDLK_LEFT))
|
||||||
|
{
|
||||||
|
//both camera direction and camera plane must be rotated
|
||||||
|
double oldDirX = dirX;
|
||||||
|
dirX = dirX * cos(rotSpeed) - dirY * sin(rotSpeed);
|
||||||
|
dirY = oldDirX * sin(rotSpeed) + dirY * cos(rotSpeed);
|
||||||
|
double oldPlaneX = planeX;
|
||||||
|
planeX = planeX * cos(rotSpeed) - planeY * sin(rotSpeed);
|
||||||
|
planeY = oldPlaneX * sin(rotSpeed) + planeY * cos(rotSpeed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
291
raycaster_textured.cpp
Normal file
|
@ -0,0 +1,291 @@
|
||||||
|
/*
|
||||||
|
Copyright (c) 2004-2019, Lode Vandevenne
|
||||||
|
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
|
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||||
|
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||||
|
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
|
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||||
|
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||||
|
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||||
|
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <cmath>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "quickcg.h"
|
||||||
|
using namespace QuickCG;
|
||||||
|
|
||||||
|
/*
|
||||||
|
g++ *.cpp -lSDL -O3 -W -Wall -ansi -pedantic
|
||||||
|
g++ *.cpp -lSDL
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#define screenWidth 640
|
||||||
|
#define screenHeight 480
|
||||||
|
#define texWidth 64
|
||||||
|
#define texHeight 64
|
||||||
|
#define mapWidth 24
|
||||||
|
#define mapHeight 24
|
||||||
|
|
||||||
|
int worldMap[mapWidth][mapHeight]=
|
||||||
|
{
|
||||||
|
{4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,7,7,7,7,7,7,7,7},
|
||||||
|
{4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,0,0,0,0,0,0,7},
|
||||||
|
{4,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7},
|
||||||
|
{4,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7},
|
||||||
|
{4,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,7,0,0,0,0,0,0,7},
|
||||||
|
{4,0,4,0,0,0,0,5,5,5,5,5,5,5,5,5,7,7,0,7,7,7,7,7},
|
||||||
|
{4,0,5,0,0,0,0,5,0,5,0,5,0,5,0,5,7,0,0,0,7,7,7,1},
|
||||||
|
{4,0,6,0,0,0,0,5,0,0,0,0,0,0,0,5,7,0,0,0,0,0,0,8},
|
||||||
|
{4,0,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,7,7,1},
|
||||||
|
{4,0,8,0,0,0,0,5,0,0,0,0,0,0,0,5,7,0,0,0,0,0,0,8},
|
||||||
|
{4,0,0,0,0,0,0,5,0,0,0,0,0,0,0,5,7,0,0,0,7,7,7,1},
|
||||||
|
{4,0,0,0,0,0,0,5,5,5,5,0,5,5,5,5,7,7,7,7,7,7,7,1},
|
||||||
|
{6,6,6,6,6,6,6,6,6,6,6,0,6,6,6,6,6,6,6,6,6,6,6,6},
|
||||||
|
{8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4},
|
||||||
|
{6,6,6,6,6,6,0,6,6,6,6,0,6,6,6,6,6,6,6,6,6,6,6,6},
|
||||||
|
{4,4,4,4,4,4,0,4,4,4,6,0,6,2,2,2,2,2,2,2,3,3,3,3},
|
||||||
|
{4,0,0,0,0,0,0,0,0,4,6,0,6,2,0,0,0,0,0,2,0,0,0,2},
|
||||||
|
{4,0,0,0,0,0,0,0,0,0,0,0,6,2,0,0,5,0,0,2,0,0,0,2},
|
||||||
|
{4,0,0,0,0,0,0,0,0,4,6,0,6,2,0,0,0,0,0,2,2,0,2,2},
|
||||||
|
{4,0,6,0,6,0,0,0,0,4,6,0,0,0,0,0,5,0,0,0,0,0,0,2},
|
||||||
|
{4,0,0,5,0,0,0,0,0,4,6,0,6,2,0,0,0,0,0,2,2,0,2,2},
|
||||||
|
{4,0,6,0,6,0,0,0,0,4,6,0,6,2,0,0,5,0,0,2,0,0,0,2},
|
||||||
|
{4,0,0,0,0,0,0,0,0,4,6,0,6,2,0,0,0,0,0,2,0,0,0,2},
|
||||||
|
{4,4,4,4,4,4,4,4,4,4,1,1,1,2,2,2,2,2,2,3,3,3,3,3}
|
||||||
|
};
|
||||||
|
|
||||||
|
Uint32 buffer[screenHeight][screenWidth];
|
||||||
|
|
||||||
|
int main(int /*argc*/, char */*argv*/[])
|
||||||
|
{
|
||||||
|
double posX = 22.0, posY = 11.5; //x and y start position
|
||||||
|
double dirX = -1.0, dirY = 0.0; //initial direction vector
|
||||||
|
double planeX = 0.0, planeY = 0.66; //the 2d raycaster version of camera plane
|
||||||
|
|
||||||
|
double time = 0; //time of current frame
|
||||||
|
double oldTime = 0; //time of previous frame
|
||||||
|
|
||||||
|
std::vector<Uint32> texture[8];
|
||||||
|
for(int i = 0; i < 8; i++) texture[i].resize(texWidth * texHeight);
|
||||||
|
|
||||||
|
screen(screenWidth,screenHeight, 0, "Raycaster");
|
||||||
|
|
||||||
|
//generate some textures
|
||||||
|
#if 0
|
||||||
|
for(int x = 0; x < texWidth; x++)
|
||||||
|
for(int y = 0; y < texHeight; y++)
|
||||||
|
{
|
||||||
|
int xorcolor = (x * 256 / texWidth) ^ (y * 256 / texHeight);
|
||||||
|
//int xcolor = x * 256 / texWidth;
|
||||||
|
int ycolor = y * 256 / texHeight;
|
||||||
|
int xycolor = y * 128 / texHeight + x * 128 / texWidth;
|
||||||
|
texture[0][texWidth * y + x] = 65536 * 254 * (x != y && x != texWidth - y); //flat red texture with black cross
|
||||||
|
texture[1][texWidth * y + x] = xycolor + 256 * xycolor + 65536 * xycolor; //sloped greyscale
|
||||||
|
texture[2][texWidth * y + x] = 256 * xycolor + 65536 * xycolor; //sloped yellow gradient
|
||||||
|
texture[3][texWidth * y + x] = xorcolor + 256 * xorcolor + 65536 * xorcolor; //xor greyscale
|
||||||
|
texture[4][texWidth * y + x] = 256 * xorcolor; //xor green
|
||||||
|
texture[5][texWidth * y + x] = 65536 * 192 * (x % 16 && y % 16); //red bricks
|
||||||
|
texture[6][texWidth * y + x] = 65536 * ycolor; //red gradient
|
||||||
|
texture[7][texWidth * y + x] = 128 + 256 * 128 + 65536 * 128; //flat grey texture
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
//generate some textures
|
||||||
|
unsigned long tw, th;
|
||||||
|
loadImage(texture[0], tw, th, "pics/eagle.png");
|
||||||
|
loadImage(texture[1], tw, th, "pics/redbrick.png");
|
||||||
|
loadImage(texture[2], tw, th, "pics/purplestone.png");
|
||||||
|
loadImage(texture[3], tw, th, "pics/greystone.png");
|
||||||
|
loadImage(texture[4], tw, th, "pics/bluestone.png");
|
||||||
|
loadImage(texture[5], tw, th, "pics/mossy.png");
|
||||||
|
loadImage(texture[6], tw, th, "pics/wood.png");
|
||||||
|
loadImage(texture[7], tw, th, "pics/colorstone.png");
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//start the main loop
|
||||||
|
while(!done())
|
||||||
|
{
|
||||||
|
for(int x = 0; x < w; x++)
|
||||||
|
{
|
||||||
|
//calculate ray position and direction
|
||||||
|
double cameraX = 2 * x / (double)w - 1; //x-coordinate in camera space
|
||||||
|
double rayDirX = dirX + planeX*cameraX;
|
||||||
|
double rayDirY = dirY + planeY*cameraX;
|
||||||
|
|
||||||
|
//which box of the map we're in
|
||||||
|
int mapX = int(posX);
|
||||||
|
int mapY = int(posY);
|
||||||
|
|
||||||
|
//length of ray from current position to next x or y-side
|
||||||
|
double sideDistX;
|
||||||
|
double sideDistY;
|
||||||
|
|
||||||
|
//length of ray from one x or y-side to next x or y-side
|
||||||
|
double deltaDistX = (rayDirX == 0) ? 1e30 : std::abs(1 / rayDirX);
|
||||||
|
double deltaDistY = (rayDirY == 0) ? 1e30 : std::abs(1 / rayDirY);
|
||||||
|
double perpWallDist;
|
||||||
|
|
||||||
|
//what direction to step in x or y-direction (either +1 or -1)
|
||||||
|
int stepX;
|
||||||
|
int stepY;
|
||||||
|
|
||||||
|
int hit = 0; //was there a wall hit?
|
||||||
|
int side; //was a NS or a EW wall hit?
|
||||||
|
|
||||||
|
//calculate step and initial sideDist
|
||||||
|
if(rayDirX < 0)
|
||||||
|
{
|
||||||
|
stepX = -1;
|
||||||
|
sideDistX = (posX - mapX) * deltaDistX;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
stepX = 1;
|
||||||
|
sideDistX = (mapX + 1.0 - posX) * deltaDistX;
|
||||||
|
}
|
||||||
|
if(rayDirY < 0)
|
||||||
|
{
|
||||||
|
stepY = -1;
|
||||||
|
sideDistY = (posY - mapY) * deltaDistY;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
stepY = 1;
|
||||||
|
sideDistY = (mapY + 1.0 - posY) * deltaDistY;
|
||||||
|
}
|
||||||
|
//perform DDA
|
||||||
|
while (hit == 0)
|
||||||
|
{
|
||||||
|
//jump to next map square, either in x-direction, or in y-direction
|
||||||
|
if(sideDistX < sideDistY)
|
||||||
|
{
|
||||||
|
sideDistX += deltaDistX;
|
||||||
|
mapX += stepX;
|
||||||
|
side = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
sideDistY += deltaDistY;
|
||||||
|
mapY += stepY;
|
||||||
|
side = 1;
|
||||||
|
}
|
||||||
|
//Check if ray has hit a wall
|
||||||
|
if(worldMap[mapX][mapY] > 0) hit = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Calculate distance of perpendicular ray (Euclidean distance would give fisheye effect!)
|
||||||
|
if(side == 0) perpWallDist = (sideDistX - deltaDistX);
|
||||||
|
else perpWallDist = (sideDistY - deltaDistY);
|
||||||
|
|
||||||
|
//Calculate height of line to draw on screen
|
||||||
|
int lineHeight = (int)(h / perpWallDist);
|
||||||
|
|
||||||
|
|
||||||
|
int pitch = 100;
|
||||||
|
|
||||||
|
//calculate lowest and highest pixel to fill in current stripe
|
||||||
|
int drawStart = -lineHeight / 2 + h / 2 + pitch;
|
||||||
|
if(drawStart < 0) drawStart = 0;
|
||||||
|
int drawEnd = lineHeight / 2 + h / 2 + pitch;
|
||||||
|
if(drawEnd >= h) drawEnd = h - 1;
|
||||||
|
|
||||||
|
//texturing calculations
|
||||||
|
int texNum = worldMap[mapX][mapY] - 1; //1 subtracted from it so that texture 0 can be used!
|
||||||
|
|
||||||
|
//calculate value of wallX
|
||||||
|
double wallX; //where exactly the wall was hit
|
||||||
|
if(side == 0) wallX = posY + perpWallDist * rayDirY;
|
||||||
|
else wallX = posX + perpWallDist * rayDirX;
|
||||||
|
wallX -= floor((wallX));
|
||||||
|
|
||||||
|
//x coordinate on the texture
|
||||||
|
int texX = int(wallX * double(texWidth));
|
||||||
|
if(side == 0 && rayDirX > 0) texX = texWidth - texX - 1;
|
||||||
|
if(side == 1 && rayDirY < 0) texX = texWidth - texX - 1;
|
||||||
|
|
||||||
|
// TODO: an integer-only bresenham or DDA like algorithm could make the texture coordinate stepping faster
|
||||||
|
// How much to increase the texture coordinate per screen pixel
|
||||||
|
double step = 1.0 * texHeight / lineHeight;
|
||||||
|
// Starting texture coordinate
|
||||||
|
double texPos = (drawStart - pitch - h / 2 + lineHeight / 2) * step;
|
||||||
|
for(int y = drawStart; y < drawEnd; y++)
|
||||||
|
{
|
||||||
|
// Cast the texture coordinate to integer, and mask with (texHeight - 1) in case of overflow
|
||||||
|
int texY = (int)texPos & (texHeight - 1);
|
||||||
|
texPos += step;
|
||||||
|
Uint32 color = texture[texNum][texHeight * texY + texX];
|
||||||
|
//make color darker for y-sides: R, G and B byte each divided through two with a "shift" and an "and"
|
||||||
|
if(side == 1) color = (color >> 1) & 8355711;
|
||||||
|
buffer[y][x] = color;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
drawBuffer(buffer[0]);
|
||||||
|
for(int y = 0; y < h; y++) for(int x = 0; x < w; x++) buffer[y][x] = 0; //clear the buffer instead of cls()
|
||||||
|
//timing for input and FPS counter
|
||||||
|
oldTime = time;
|
||||||
|
time = getTicks();
|
||||||
|
double frameTime = (time - oldTime) / 1000.0; //frametime is the time this frame has taken, in seconds
|
||||||
|
print(1.0 / frameTime); //FPS counter
|
||||||
|
redraw();
|
||||||
|
|
||||||
|
//speed modifiers
|
||||||
|
double moveSpeed = frameTime * 5.0; //the constant value is in squares/second
|
||||||
|
double rotSpeed = frameTime * 3.0; //the constant value is in radians/second
|
||||||
|
|
||||||
|
readKeys();
|
||||||
|
//move forward if no wall in front of you
|
||||||
|
if(keyDown(SDLK_UP))
|
||||||
|
{
|
||||||
|
if(worldMap[int(posX + dirX * moveSpeed)][int(posY)] == false) posX += dirX * moveSpeed;
|
||||||
|
if(worldMap[int(posX)][int(posY + dirY * moveSpeed)] == false) posY += dirY * moveSpeed;
|
||||||
|
}
|
||||||
|
//move backwards if no wall behind you
|
||||||
|
if(keyDown(SDLK_DOWN))
|
||||||
|
{
|
||||||
|
if(worldMap[int(posX - dirX * moveSpeed)][int(posY)] == false) posX -= dirX * moveSpeed;
|
||||||
|
if(worldMap[int(posX)][int(posY - dirY * moveSpeed)] == false) posY -= dirY * moveSpeed;
|
||||||
|
}
|
||||||
|
//rotate to the right
|
||||||
|
if(keyDown(SDLK_RIGHT))
|
||||||
|
{
|
||||||
|
//both camera direction and camera plane must be rotated
|
||||||
|
double oldDirX = dirX;
|
||||||
|
dirX = dirX * cos(-rotSpeed) - dirY * sin(-rotSpeed);
|
||||||
|
dirY = oldDirX * sin(-rotSpeed) + dirY * cos(-rotSpeed);
|
||||||
|
double oldPlaneX = planeX;
|
||||||
|
planeX = planeX * cos(-rotSpeed) - planeY * sin(-rotSpeed);
|
||||||
|
planeY = oldPlaneX * sin(-rotSpeed) + planeY * cos(-rotSpeed);
|
||||||
|
}
|
||||||
|
//rotate to the left
|
||||||
|
if(keyDown(SDLK_LEFT))
|
||||||
|
{
|
||||||
|
//both camera direction and camera plane must be rotated
|
||||||
|
double oldDirX = dirX;
|
||||||
|
dirX = dirX * cos(rotSpeed) - dirY * sin(rotSpeed);
|
||||||
|
dirY = oldDirX * sin(rotSpeed) + dirY * cos(rotSpeed);
|
||||||
|
double oldPlaneX = planeX;
|
||||||
|
planeX = planeX * cos(rotSpeed) - planeY * sin(rotSpeed);
|
||||||
|
planeY = oldPlaneX * sin(rotSpeed) + planeY * cos(rotSpeed);
|
||||||
|
}
|
||||||
|
if(keyDown(SDLK_ESCAPE))
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
13
sample-macros.fnl
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
(fn incf [value ?by]
|
||||||
|
`(set ,value (+ ,value (or ,?by 1))))
|
||||||
|
|
||||||
|
(fn decf [value ?by]
|
||||||
|
`(set ,value (- ,value (or ,?by 1))))
|
||||||
|
|
||||||
|
(fn with [t keys ?body]
|
||||||
|
`(let [,keys ,t]
|
||||||
|
(if ,?body
|
||||||
|
,?body
|
||||||
|
,keys)))
|
||||||
|
|
||||||
|
{: incf : decf : with}
|
29
scripts/update-fennel.sh
Executable file
|
@ -0,0 +1,29 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
|
||||||
|
libdir=$1
|
||||||
|
release=$2
|
||||||
|
|
||||||
|
if [ "$#" = 0 ]; then
|
||||||
|
echo -e "Usage: update-fennel.sh libdir [release]\n\n\
|
||||||
|
Example: ./update-fennel.sh \$(pwd)/../lib/ 1.2.0"
|
||||||
|
exit 0;
|
||||||
|
fi
|
||||||
|
|
||||||
|
cd /tmp/
|
||||||
|
git clone https://github.com/bakpakin/Fennel.git
|
||||||
|
cd Fennel
|
||||||
|
|
||||||
|
|
||||||
|
if [ $release ]
|
||||||
|
then
|
||||||
|
|
||||||
|
git checkout tags/$release
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
make
|
||||||
|
|
||||||
|
cp fennel fennel.lua $libdir
|
||||||
|
|
||||||
|
rm -rf /tmp/Fennel/
|
35
state.fnl
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
; Handles game-play stateful information
|
||||||
|
(local pi math.pi)
|
||||||
|
|
||||||
|
; The Player table holds all the player stuff
|
||||||
|
; The Inventory table holds the player's inventory
|
||||||
|
; [x y] coordinates, [d]irection, [f]ield-of-view
|
||||||
|
; [o]xygen and [p]ower
|
||||||
|
(var player {:x 3.5 :y 3.5 :d 0 :f (/ pi 3)
|
||||||
|
:o 100 :p 45})
|
||||||
|
(var inventory [])
|
||||||
|
|
||||||
|
; The Map table holds the map
|
||||||
|
(var map [])
|
||||||
|
|
||||||
|
{:getPlayer (fn getPlayer [] player)
|
||||||
|
:getX (fn getX [] player.x)
|
||||||
|
:setX (fn setX [x] (set player.x x))
|
||||||
|
:getY (fn getY [] player.y)
|
||||||
|
:setY (fn setY [y] (set player.y y))
|
||||||
|
:getD (fn getD [] player.d)
|
||||||
|
:setD (fn setD [d] (set player.d d))
|
||||||
|
:getX (fn getX [] player.x)
|
||||||
|
:getO (fn getO [] player.o)
|
||||||
|
:setO (fn setO [x] (set player.o x))
|
||||||
|
:modO (fn modO [x]
|
||||||
|
(if (< (+ player.o x) 0)
|
||||||
|
(set player.o 0)
|
||||||
|
(set player.o (+ player.o x))))
|
||||||
|
:getP (fn getP [] player.p)
|
||||||
|
:setP (fn setP [x] (set player.p x))
|
||||||
|
:modP (fn modP [x]
|
||||||
|
(if (< (+ player.p x) 0)
|
||||||
|
(set player.p 0)
|
||||||
|
(set player.p (+ player.p x))))
|
||||||
|
}
|
BIN
textures/walls/bluestone.png
Executable file
After Width: | Height: | Size: 2.5 KiB |
BIN
textures/walls/colorstone.png
Executable file
After Width: | Height: | Size: 3.3 KiB |
BIN
textures/walls/greystone.png
Executable file
After Width: | Height: | Size: 3.6 KiB |
BIN
textures/walls/mossy.png
Executable file
After Width: | Height: | Size: 4.2 KiB |
BIN
textures/walls/purplestone.png
Executable file
After Width: | Height: | Size: 4.3 KiB |
BIN
textures/walls/redbrick.png
Executable file
After Width: | Height: | Size: 3 KiB |
BIN
textures/walls/wood.png
Executable file
After Width: | Height: | Size: 1.4 KiB |
49
wrap.fnl
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
(local fennel (require :lib.fennel))
|
||||||
|
(local repl (require :lib.stdio))
|
||||||
|
(local canvas (let [(w h) (love.window.getMode)]
|
||||||
|
(love.graphics.newCanvas w h)))
|
||||||
|
|
||||||
|
(var scale 1)
|
||||||
|
|
||||||
|
;; set the first mode
|
||||||
|
(var (mode mode-name) nil)
|
||||||
|
|
||||||
|
(fn set-mode [new-mode-name ...]
|
||||||
|
(set (mode mode-name) (values (require new-mode-name) new-mode-name))
|
||||||
|
(when mode.activate
|
||||||
|
(match (pcall mode.activate ...)
|
||||||
|
(false msg) (print mode-name "activate error" msg))))
|
||||||
|
|
||||||
|
(fn love.load [args]
|
||||||
|
; THIS IS WHERE WE SET THE START MODE
|
||||||
|
; ###################################
|
||||||
|
; (set-mode :ray-cast-vectors)
|
||||||
|
(set-mode :mode-intro)
|
||||||
|
; (set-mode :raycaster)
|
||||||
|
; ###################################
|
||||||
|
(canvas:setFilter "nearest" "nearest")
|
||||||
|
(when (~= :web (. args 1)) (repl.start)))
|
||||||
|
|
||||||
|
(fn safely [f]
|
||||||
|
(xpcall f #(set-mode :error-mode mode-name $ (fennel.traceback))))
|
||||||
|
|
||||||
|
(fn love.draw []
|
||||||
|
;; the canvas allows you to get sharp pixel-art style scaling; if you
|
||||||
|
;; don't want that, just skip that and call mode.draw directly.
|
||||||
|
(love.graphics.setCanvas canvas)
|
||||||
|
(love.graphics.clear)
|
||||||
|
(love.graphics.setColor 1 1 1)
|
||||||
|
(safely mode.draw)
|
||||||
|
(love.graphics.setCanvas)
|
||||||
|
(love.graphics.setColor 1 1 1)
|
||||||
|
(love.graphics.draw canvas 0 0 0 scale scale))
|
||||||
|
|
||||||
|
(fn love.update [dt]
|
||||||
|
(when mode.update
|
||||||
|
(safely #(mode.update dt set-mode))))
|
||||||
|
|
||||||
|
(fn love.keypressed [key]
|
||||||
|
(if (and (love.keyboard.isDown "lctrl" "rctrl" "capslock") (= key "q"))
|
||||||
|
(love.event.quit)
|
||||||
|
;; add what each keypress should do in each mode
|
||||||
|
(safely #(mode.keypressed key set-mode))))
|