First commit.

This commit is contained in:
Alberto Torres 2024-08-23 17:08:24 +02:00
commit d2e5b94f2b
10 changed files with 296 additions and 0 deletions

8
.gitignore vendored Normal file
View file

@ -0,0 +1,8 @@
build/*
!build/web/
build/web/*
!build/web/game.html
tmp/
*.blend1

85
README.md Normal file
View file

@ -0,0 +1,85 @@
# Template project for Myou Engine
## Features
* A `nim.cfg` ready to compile to desktop, Android, iOS, and web through
Emscripten.
* Helper scripts for using the C files for Android and iOS (in Android Studio
and XCode respectively).
* A `game.html` file with just a canvas that fills the whole window.
* A simple python server that supports CORS (so you can load assets remotely),
and COOP/COEP (so multithreaded Emscripten builds can work).
* An empty `example.blend` that is just a default cube saved in Blender 4.1.
## How to use
* Have the code of Myou Engine somewhere. In this example it expects it to be in
`../myou-engine` (that is, in the parent folder of this project).
* If you have the engine somewhere other than `../myou-engine`, change the path
in `nim.cfg`. For example, if you have it in `./libs/myou_engine`, then change
this:
`path:"../myou-engine/libs/packages"`
by this:
`path:"./libs/myou_engine/libs/packages"`
Note that we add `/libs/packages` so that the engine can use all of its
dependencies without relying on a package manager.
* Have [Nimskull](https://github.com/nim-works/nimskull) installed in your path
(here we'll assume it's just `nim`). In the future it will work with Nim (and
other languages as well) through a built version of the engine.
### Desktop version
* To compile and run for desktop just run
`nim c -r src/main.nim`
The output will be in `build/game.exe` or `build/game`.
* To compile a Windows version from Linux or MacOS, run:
`nim c -d:mingw src/main.nim`
You can add `-r` to run it, it will try to use Wine if you have it
installed.
### Web version
* To compile a web version with Emscripten, run:
`nim c -d:emscripten src/main.nim`
Output files will be in `build/web/`
* Run `python scripts/simple.server.py`
* Open http://localhost:8003/build/web/game.html
### Mobile version
* To compile it for Android:
`nim c --os:android --cpu:arm64 src/main.nim`
Output C files will be in `tmp/android/`
_(TODO: instructions on how to use files in Android Studio)_
* To compile it for iOS:
`nim c --os:ios src/main.nim`
Output C files will be in `tmp/android/`
_(TODO: instructions on how to use files in XCode)_
## License
Add your own license and replace this text. Until then you can consider this
CC0. You can use the files in this repository for any purpose whatsoever.

BIN
assets/example.blend Normal file

Binary file not shown.

30
build/web/game.html Normal file
View file

@ -0,0 +1,30 @@
<!doctype html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- CHANGE THIS -->
<title>My Game</title>
<style>body {
font-family: arial;
margin: 0;
padding: none;
overflow: hidden;
}
canvas.emscripten {
border: 0px none; background-color: black;
width: 100vw; height: 100vh;
}
</style>
</head>
<body>
<canvas class="emscripten" id="canvas" oncontextmenu="event.preventDefault()" tabindex=-1></canvas>
<script type='text/javascript'>
var Module = {
canvas: canvas
};
</script>
<!-- CHANGE THIS IF YOU CHANGE THE OUTPUT NAME -->
<script async type="text/javascript" src="game.js"></script>
</body>
</html>

78
nim.cfg Normal file
View file

@ -0,0 +1,78 @@
path:"../myou-engine/libs/packages/"
out:"build/game"
@if release:
d:lto
@else:
debugger:native
# floatChecks:on
@if not myouUseAR and not emscripten:
d:myouUseRenderdoc
@end
@end
@if android:
c # generate c files, don't compile them
nimcache:"tmp/android"
d:myouUseNativeActivity
d:opengl_es
d:androidNDK
noMain:on
@if amd64:
nimcache:"tmp/androidx64"
@end
@end
@if ios:
c # generate c files, don't compile them
cpu:arm64
nimcache:"tmp/ios"
d:opengl_es
noMain:on
@end
@if emscripten:
passL:"-o build/web/game.js"
d:opengl_es
noMain:on
nimcache:"tmp/emscripten"
os:linux
cpu:wasm32
cc:clang
clang.exe:emcc
clang.linkerexe:emcc
clang.cpp.exe:emcc
clang.cpp.linkerexe:emcc
# d:noSignalHandler
@if release:
passC:"-O3 -flto"
passL:"-O3 -flto --closure 1"
@else:
passC:"-gsource-map -O0"
passL:"-gsource-map -O0"
# embedsrc
@end
passL:"-sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2"
# passL:"-sEXPORTED_FUNCTIONS=_main,_NimMain,_glfmMain"
passL:"-sFETCH=1"
passL:"-sINITIAL_MEMORY=268435456"
passL:"-sSTACK_SIZE=262144"
passL:"-sALLOW_MEMORY_GROWTH"
@end
@if sanitize:
cc:clang
d:useMalloc
passC:"-fno-omit-frame-pointer"
passC:"-mno-omit-leaf-frame-pointer"
passC:"-fsanitize=address"
passL:"-fsanitize=address"
gc:arc
threads:off
@end
@if mingw:
passL:"-static"
@end

16
scripts/simple_server.py Executable file
View file

@ -0,0 +1,16 @@
#!/usr/bin/env python3
# encoding: utf-8
"""Use instead of `python3 -m http.server` when you need CORS and SharedArrayBuffer"""
from http.server import HTTPServer, SimpleHTTPRequestHandler
class CORSRequestHandler(SimpleHTTPRequestHandler):
def end_headers(self):
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Cross-Origin-Opener-Policy', 'same-origin')
self.send_header('Cross-Origin-Embedder-Policy', 'require-corp')
self.send_header('Cache-Control', 'no-store, no-cache, must-revalidate')
return super(CORSRequestHandler, self).end_headers()
httpd = HTTPServer(('', 8003), CORSRequestHandler)
httpd.serve_forever()

View file

@ -0,0 +1,10 @@
# TODO: port this to nimscript
# TODO: modify the CMake file instead of copying to clipboard
# Build for android like this:
# nim c -c --os:android --cpu:arm64 src/main.nim
# and then run this script to update the files in CMakeLists.txt
# (the result will be copied to clipboard)
OUT_NAME=game
cat tmp/android/${OUT_NAME}.json|grep '\.\(c\|cpp\)",' | sed -e 's/,$//' -e 's/@/\\@/g' | xsel -b

View file

@ -0,0 +1,33 @@
# TODO: port this to nimscript
# Build for ios like this:
# nim c -c --os:ios --cpu:arm64 src/main.nim
# then run this script to copy the missing files
# and then remove and add the folder again in xcode
# If there are old files that are no longer used
# you have to delete the cache first:
# rm tmp/ios/* -rf
# then build and follow the above instructions
OUT_NAME=game
cd tmp/ios
DIR="$(pwd)"
# copy header files
(cat "$OUT_NAME.json" |
grep -- -I/|
grep -o -- '-I/[^ ]*' |
grep -v '^-I/$' |
sed 's@^..@@' | sort | uniq |
xargs -d'\n' -I {} find {} -iname "*.h" |
xargs -d'\n' -I {} cp "{}" .
)
# copy c files outside cache
echo ignore any error below saying "*.h" does not exist
(cat "$OUT_NAME.json" |
grep '\.\(c\|cpp\|m\)",' |
grep -v "\"$DIR" |
sed -e 's/",$//' -e 's/^\s*"//' -e 's/\\"/"/g' |
xargs -d'\n' -I {} sh -c 'cp "{}" "$(dirname {})/"*.h .'
)

24
scripts/watch.sh Normal file
View file

@ -0,0 +1,24 @@
# TODO: port this to nimscript
cd "$(dirname $0)/.."
[ $NIM ] || NIM="${HOME}/nimskull/bin/nim"
OUT_NAME=game
MAIN_FILE=src/main.nim
MAIN_FILE_NAME=main
JSON="$HOME/.cache/nimskull/$MAIN_FILE_NAME_d/$OUT_NAME.json"
function build(){
$NIM c "$MAIN_FILE" && (pkill -9 game; build/game &)
}
function file_list(){
if [ -e $JSON ]; then
cat $JSON | grep '/@m.*\.nim\.c.o",' | sed -e 's_.*@m\(.*\).c.o",_src/\1_' -e 's_@s_/_g'
fi
find -iname "*.nim"
find -iname "*.blend"
}
for p in $(file_list);do echo $p;done
build
while inotifywait -e modify $(file_list) ;do
build
done

12
src/main.nim Normal file
View file

@ -0,0 +1,12 @@
import myou_engine
var myou = newMyouEngine(1280, 720, "My game")
# The example file is just Blender's default cube
myou.loadScene("assets/example1.blend", proc(scene: Scene) =
scene.enable_all()
)
myou.run()