Update code example to match new sdl2.nim syntax (#13924)
* Update code example to match new sdl2.nim syntax Signed-off-by: Hieu Luu Danh <hieu@vivu.asia> * Modify on recommendation of @alaviss - Removed trailing whitespaces - Detailed how to compile libnimhcr and libnimrtl - Fixed some logic in example code * Modify following recommendations of @alaviss - Rewording so that it conforms to Windows/Linux/MacOS Signed-off-by: Hieu Luu Danh <hieu@vivu.asia>
This commit is contained in:
parent
59aeaa1c98
commit
4476d8a2e0
1 changed files with 43 additions and 25 deletions
68
doc/hcr.rst
68
doc/hcr.rst
|
|
@ -26,39 +26,40 @@ To install SDL2 you can use ``nimble install sdl2``.
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
|
|
||||||
# logic.nim
|
# logic.nim
|
||||||
import sdl2/sdl
|
import sdl2
|
||||||
|
|
||||||
#*** import the hotcodereloading stdlib module ***
|
#*** import the hotcodereloading stdlib module ***
|
||||||
import hotcodereloading
|
import hotcodereloading
|
||||||
|
|
||||||
var runGame*: bool = true
|
var runGame*: bool = true
|
||||||
var window: Window
|
var window: WindowPtr
|
||||||
var renderer: Renderer
|
var renderer: RendererPtr
|
||||||
|
var evt = sdl2.defaultEvent
|
||||||
|
|
||||||
proc init*() =
|
proc init*() =
|
||||||
discard sdl.init(INIT_EVERYTHING)
|
discard sdl2.init(INIT_EVERYTHING)
|
||||||
window = createWindow("testing", WINDOWPOS_UNDEFINED.cint, WINDOWPOS_UNDEFINED.cint, 640, 480, 0'u32)
|
window = createWindow("testing", SDL_WINDOWPOS_UNDEFINED.cint, SDL_WINDOWPOS_UNDEFINED.cint, 640, 480, 0'u32)
|
||||||
assert(window != nil, $sdl.getError())
|
assert(window != nil, $sdl2.getError())
|
||||||
renderer = createRenderer(window, -1, RENDERER_SOFTWARE)
|
renderer = createRenderer(window, -1, RENDERER_SOFTWARE)
|
||||||
assert(renderer != nil, $sdl.getError())
|
assert(renderer != nil, $sdl2.getError())
|
||||||
|
|
||||||
proc destroy*() =
|
proc destroy*() =
|
||||||
destroyRenderer(renderer)
|
destroyRenderer(renderer)
|
||||||
destroyWindow(window)
|
destroyWindow(window)
|
||||||
|
|
||||||
var posX = 1
|
var posX: cint = 1
|
||||||
var posY = 0
|
var posY: cint = 0
|
||||||
var dX = 1
|
var dX: cint = 1
|
||||||
var dY = 1
|
var dY: cint = 1
|
||||||
|
|
||||||
proc update*() =
|
proc update*() =
|
||||||
for evt in events():
|
while pollEvent(evt):
|
||||||
if evt.kind == QUIT:
|
if evt.kind == QuitEvent:
|
||||||
runGame = false
|
runGame = false
|
||||||
break
|
break
|
||||||
if evt.kind == KEY_DOWN:
|
if evt.kind == KeyDown:
|
||||||
if evt.key.keysym.scancode == SCANCODE_ESCAPE: runGame = false
|
if evt.key.keysym.scancode == SDL_SCANCODE_ESCAPE: runGame = false
|
||||||
elif evt.key.keysym.scancode == SCANCODE_F9:
|
elif evt.key.keysym.scancode == SDL_SCANCODE_F9:
|
||||||
#*** reload this logic.nim module on the F9 keypress ***
|
#*** reload this logic.nim module on the F9 keypress ***
|
||||||
performCodeReload()
|
performCodeReload()
|
||||||
|
|
||||||
|
|
@ -71,14 +72,14 @@ To install SDL2 you can use ``nimble install sdl2``.
|
||||||
if posY >= 480: dY = -2
|
if posY >= 480: dY = -2
|
||||||
if posY <= 0: dY = +2
|
if posY <= 0: dY = +2
|
||||||
|
|
||||||
discard renderer.setRenderDrawColor(0, 0, 255, 255)
|
discard renderer.setDrawColor(0, 0, 255, 255)
|
||||||
discard renderer.renderClear()
|
discard renderer.clear()
|
||||||
discard renderer.setRenderDrawColor(255, 128, 128, 0)
|
discard renderer.setDrawColor(255, 128, 128, 0)
|
||||||
|
|
||||||
var rect: Rect(x: posX - 25, y: posY - 25, w: 50, h: 50)
|
var rect: Rect = (x: posX - 25, y: posY - 25, w: 50.cint, h: 50.cint)
|
||||||
discard renderer.renderFillRect(rect.addr)
|
discard renderer.fillRect(rect)
|
||||||
delay(16)
|
delay(16)
|
||||||
renderer.renderPresent()
|
renderer.present()
|
||||||
|
|
||||||
|
|
||||||
.. code-block:: nim
|
.. code-block:: nim
|
||||||
|
|
@ -111,11 +112,11 @@ Now start the program and KEEP it running!
|
||||||
|
|
||||||
For example, change the line::
|
For example, change the line::
|
||||||
|
|
||||||
discard renderer.setRenderDrawColor(255, 128, 128, 0)
|
discard renderer.setDrawColor(255, 128, 128, 0)
|
||||||
|
|
||||||
into::
|
into::
|
||||||
|
|
||||||
discard renderer.setRenderDrawColor(255, 255, 128, 0)
|
discard renderer.setDrawColor(255, 255, 128, 0)
|
||||||
|
|
||||||
(This will change the color of the rectangle.)
|
(This will change the color of the rectangle.)
|
||||||
|
|
||||||
|
|
@ -187,7 +188,24 @@ Native code targets
|
||||||
Native projects using the hot code reloading option will be implicitly
|
Native projects using the hot code reloading option will be implicitly
|
||||||
compiled with the `-d:useNimRtl` option and they will depend on both
|
compiled with the `-d:useNimRtl` option and they will depend on both
|
||||||
the ``nimrtl`` library and the ``nimhcr`` library which implements the
|
the ``nimrtl`` library and the ``nimhcr`` library which implements the
|
||||||
hot code reloading run-time.
|
hot code reloading run-time. Both libraries can be found in the ``lib``
|
||||||
|
folder of Nim and can be compiled into dynamic libraries to satisfy
|
||||||
|
runtime demands of the example code above. An example of compiling
|
||||||
|
``nimhcr.nim`` and ``nimrtl.nim`` when the source dir of Nim is installed
|
||||||
|
with choosenim follows.
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
# Unix/MacOS
|
||||||
|
# Make sure you are in the directory containing your .nim files
|
||||||
|
$ cd your-source-directory
|
||||||
|
|
||||||
|
# Compile two required files and set their output directory to current dir
|
||||||
|
$ nim c --outdir:$PWD ~/.choosenim/toolchains/nim-#devel/lib/nimhcr.nim
|
||||||
|
$ nim c --outdir:$PWD ~/.choosenim/toolchains/nim-#devel/lib/nimrtl.nim
|
||||||
|
|
||||||
|
# verify that you have two files named libnimhcr and libnimrtl in your
|
||||||
|
# source directory (.dll for Windows, .so for Unix, .dylib for MacOS)
|
||||||
|
|
||||||
All modules of the project will be compiled to separate dynamic link
|
All modules of the project will be compiled to separate dynamic link
|
||||||
libraries placed in the ``nimcache`` directory. Please note that during
|
libraries placed in the ``nimcache`` directory. Please note that during
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue