fixed wasm compatibility
This commit is contained in:
6
nim.cfg
6
nim.cfg
@@ -15,10 +15,10 @@
|
|||||||
cpu = "i386"
|
cpu = "i386"
|
||||||
@if wasm:
|
@if wasm:
|
||||||
passC = "-s WASM=1 -Iemscripten"
|
passC = "-s WASM=1 -Iemscripten"
|
||||||
passL = "-s WASM=1 -Lemscripten -s ALLOW_MEMORY_GROWTH=1 "
|
passL = "-s WASM=1 -Lemscripten -s ALLOW_MEMORY_GROWTH=1"
|
||||||
@elif asmjs:
|
@elif asmjs:
|
||||||
passC = "-s ASM_JS=1 -Iemscripten" #-s USE_PTHREADS=1
|
passC = "-s ASM_JS=1 -Iemscripten"
|
||||||
passL = "-s ASM_JS=1 -Lemscripten" #-s ALLOW_MEMORY_GROWTH=1"
|
passL = "-s ASM_JS=1 -Lemscripten"
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@if release:
|
@if release:
|
||||||
|
35
resources/index.html
Normal file
35
resources/index.html
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
||||||
|
</head>
|
||||||
|
<style>
|
||||||
|
* { margin: 0; padding: 0;}
|
||||||
|
|
||||||
|
body, html { height:100%; }
|
||||||
|
|
||||||
|
#canvas {
|
||||||
|
position:absolute;
|
||||||
|
width:100%;
|
||||||
|
height:100%;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<!-- Create the canvas that the C++ code will draw into -->
|
||||||
|
<canvas id="canvas" oncontextmenu="event.preventDefault()"></canvas>
|
||||||
|
|
||||||
|
<!-- Allow the C++ to access the canvas element -->
|
||||||
|
<script type='text/javascript'>
|
||||||
|
var Module = {
|
||||||
|
canvas: (function() { return document.getElementById('canvas'); })()
|
||||||
|
};
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<!-- Add the javascript glue code (index.js) as generated by Emscripten -->
|
||||||
|
<script src="sdlife.js"></script>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
50
src/hello_sdl.nim
Normal file
50
src/hello_sdl.nim
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
import sdl2, sdl2/gfx
|
||||||
|
import geo2d
|
||||||
|
|
||||||
|
|
||||||
|
when defined(wasm):
|
||||||
|
{.emit: "#include <emscripten.h>".}
|
||||||
|
proc emscripten_set_main_loop_arg*(loopFunction: proc(ctx : pointer) {.cdecl.}, ctx : pointer, fps : cint, simulate_infinite_loop : cint) {.importc.}
|
||||||
|
|
||||||
|
discard sdl2.init(INIT_EVERYTHING)
|
||||||
|
|
||||||
|
var
|
||||||
|
window: WindowPtr
|
||||||
|
renderer: RendererPtr
|
||||||
|
|
||||||
|
window = createWindow("Game of Life in SDL",
|
||||||
|
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
|
||||||
|
640, 480,
|
||||||
|
SDL_WINDOW_SHOWN or SDL_WINDOW_RESIZABLE)
|
||||||
|
|
||||||
|
renderer = createRenderer(window, -1, Renderer_Accelerated or Renderer_PresentVsync or Renderer_TargetTexture)
|
||||||
|
|
||||||
|
type Context = object
|
||||||
|
renderer : RendererPtr
|
||||||
|
|
||||||
|
proc mainloop(arg : pointer) {.cdecl.} =
|
||||||
|
let windowSize = block:
|
||||||
|
var w, h : cint
|
||||||
|
window.getSize(w, h)
|
||||||
|
Rect2d(tl: newP2d(0f32, 0f32), br: newP2d(w.float32, h.float32))
|
||||||
|
|
||||||
|
let ctx = cast[ptr[Context]](arg)
|
||||||
|
let renderer = ctx.renderer
|
||||||
|
|
||||||
|
renderer.setDrawColor(255, 0, 0, 255)
|
||||||
|
renderer.clear()
|
||||||
|
let ticks = getTicks()
|
||||||
|
var r : Rect;
|
||||||
|
r.x = ((ticks.float64 / 3000 * windowSize.width).cuint mod windowSize.width.cuint).cint
|
||||||
|
r.y = 50;
|
||||||
|
r.w = 50;
|
||||||
|
r.h = 50;
|
||||||
|
renderer.setDrawColor(0, 255, 255)
|
||||||
|
renderer.fillRect(r)
|
||||||
|
renderer.present()
|
||||||
|
|
||||||
|
var ctx = Context(renderer : renderer)
|
||||||
|
emscripten_set_main_loop_arg(mainloop, addr(ctx), -1, 1)
|
||||||
|
|
||||||
|
destroy renderer
|
||||||
|
destroy window
|
@@ -1,4 +1,4 @@
|
|||||||
include options
|
import options
|
||||||
import sdl2, sdl2/gfx
|
import sdl2, sdl2/gfx
|
||||||
import grid
|
import grid
|
||||||
import geo2d
|
import geo2d
|
||||||
@@ -6,6 +6,10 @@ import mmath/smatrix
|
|||||||
import mmath/svector
|
import mmath/svector
|
||||||
from nwo/utils import box
|
from nwo/utils import box
|
||||||
|
|
||||||
|
when defined(wasm):
|
||||||
|
{.emit: "#include <emscripten.h>".}
|
||||||
|
proc emscripten_set_main_loop_arg*(loopFunction: proc(ctx : pointer) {.cdecl.}, ctx : pointer, fps : cint, simulate_infinite_loop : cint) {.importc.}
|
||||||
|
|
||||||
discard sdl2.init(INIT_EVERYTHING)
|
discard sdl2.init(INIT_EVERYTHING)
|
||||||
|
|
||||||
proc drawLine(renderer : RendererPtr, p0, p1 : P2d) = renderer.drawLine(p0.x.cint, p0.y.cint, p1.x.cint, p1.y.cint)
|
proc drawLine(renderer : RendererPtr, p0, p1 : P2d) = renderer.drawLine(p0.x.cint, p0.y.cint, p1.x.cint, p1.y.cint)
|
||||||
@@ -27,8 +31,8 @@ var
|
|||||||
evt = sdl2.defaultEvent
|
evt = sdl2.defaultEvent
|
||||||
quit = false
|
quit = false
|
||||||
freeze = true
|
freeze = true
|
||||||
fpsman: FpsManager
|
# fpsman: FpsManager
|
||||||
fpsman.init
|
# fpsman.init
|
||||||
|
|
||||||
let side = 5
|
let side = 5
|
||||||
let
|
let
|
||||||
@@ -44,14 +48,14 @@ block:
|
|||||||
next_grid = box(grid)
|
next_grid = box(grid)
|
||||||
|
|
||||||
let bb = newRect2d(0f32, 0f32, float32(side * pixelx), float32(side * pixely))
|
let bb = newRect2d(0f32, 0f32, float32(side * pixelx), float32(side * pixely))
|
||||||
var step = 3
|
var step : uint = 3
|
||||||
var elapsed_time_sec = 0
|
var elapsed_time : uint = 0
|
||||||
var
|
var
|
||||||
xform : X2d = identity[3, float32]()
|
xform : X2d = identity[3, float32]()
|
||||||
uxform : X2d = identity[3, float32]()
|
uxform : X2d = identity[3, float32]()
|
||||||
pan_start : Option[P2d] = none[P2d]()
|
pan_start : Option[P2d] = none[P2d]()
|
||||||
|
|
||||||
while not quit:
|
proc main_loop(arg : pointer) {.cdecl.} =
|
||||||
let windowSize = block:
|
let windowSize = block:
|
||||||
var w, h : cint
|
var w, h : cint
|
||||||
window.getSize(w, h)
|
window.getSize(w, h)
|
||||||
@@ -73,7 +77,7 @@ while not quit:
|
|||||||
of 's'.int:
|
of 's'.int:
|
||||||
next_step(current_grid[], next_grid[])
|
next_step(current_grid[], next_grid[])
|
||||||
of '['.int:
|
of '['.int:
|
||||||
if step > 1:
|
if step > 0u:
|
||||||
step -= 1
|
step -= 1
|
||||||
of ']'.int:
|
of ']'.int:
|
||||||
if step < 10:
|
if step < 10:
|
||||||
@@ -121,7 +125,8 @@ while not quit:
|
|||||||
else:
|
else:
|
||||||
discard
|
discard
|
||||||
|
|
||||||
let dt = fpsman.getFramerate() / 1000
|
# let dt = fpsman.getFramerate() / 1000
|
||||||
|
# let dt = 10
|
||||||
|
|
||||||
render.setDrawColor 0,0,0,255
|
render.setDrawColor 0,0,0,255
|
||||||
render.clear
|
render.clear
|
||||||
@@ -153,18 +158,27 @@ while not quit:
|
|||||||
while y <= bb.height:
|
while y <= bb.height:
|
||||||
render.drawLine(newP2d(bb.left, y) * xform, newP2d(bb.width, y) * xform)
|
render.drawLine(newP2d(bb.left, y) * xform, newP2d(bb.width, y) * xform)
|
||||||
y += side.float32
|
y += side.float32
|
||||||
|
|
||||||
render.setDrawColor 255,255,255,255
|
render.setDrawColor 255,255,255,255
|
||||||
for i in 0..<current_grid.columns:
|
for i in 0..<current_grid.columns:
|
||||||
for j in 0..<current_grid.rows:
|
for j in 0..<current_grid.rows:
|
||||||
if current_grid[][i, j]:
|
if current_grid[][i, j]:
|
||||||
render.fillRect(newRect2d((i * side).float32, (j * side).float32, side.float32, side.float32) * xform)
|
render.fillRect(newRect2d((i * side).float32, (j * side).float32, side.float32, side.float32) * xform)
|
||||||
render.present
|
render.present
|
||||||
fpsman.delay
|
# fpsman.delay
|
||||||
let current_time_sec = fpsman.getFramecount() div step
|
# delay(0)
|
||||||
if not freeze and current_time_sec > elapsed_time_sec:
|
let current_time = getTicks().uint
|
||||||
elapsed_time_sec = current_time_sec
|
if not freeze and current_time - elapsed_time > step * 50:
|
||||||
|
elapsed_time = current_time
|
||||||
next_step(current_grid[], next_grid[])
|
next_step(current_grid[], next_grid[])
|
||||||
|
|
||||||
|
|
||||||
|
when defined(wasm):
|
||||||
|
emscripten_set_main_loop_arg(mainloop, nil, -1, 1)
|
||||||
|
else:
|
||||||
|
while not quit:
|
||||||
|
main_loop(nil)
|
||||||
|
delay(0)
|
||||||
|
|
||||||
destroy render
|
destroy render
|
||||||
destroy window
|
destroy window
|
Reference in New Issue
Block a user