Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nim example #28

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lang/nim/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
nimcache/
nimblecache/
htmldocs/

.DS_Store
*.dylib
*.so
*.dll
21 changes: 21 additions & 0 deletions lang/nim/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 David Konsumer

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.
29 changes: 29 additions & 0 deletions lang/nim/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
This is an example libretro core, written in nim. This is a translation from [a basic software-rendering example](https://github.com/libretro/libretro-samples/tree/master/video/software/rendering) but you should be able to roughly translate any of the C examples into nim.

Variable/type names are mostly the same as with a C core, with one exception:

- Because nim disregards case in var-names, I had to rename keys like `RETROK_a` to `RETROK_LOWER_A`.

For more in-depth info, check out [Pmunch's blog post](https://peterme.net/dynamic-libraries-in-nim.html) as it was very helpful in figuring out how to put it all together.

## Usage

Put your core-code in `src/example_libretro.nim` and you can run these:

```
# build the libretro core
nimble core
```

You can run your core like this:

```
# mac
/Applications/RetroArch.app/Contents/MacOS/RetroArch -L libexample_libretro.dylib

# linux
retroarch -L libexample_libretro.so

# windows
retroarch -L libexample_libretro.dll
```
20 changes: 20 additions & 0 deletions lang/nim/example_libretro.nimble
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
version = "0.0.1"
author = "David Konsumer"
description = "Example libretro core"
license = "MIT"
srcDir = "src"
bin = @["example_libretro"]

requires "nim >= 1.6.10"

import os

task core, "Build libretro core":
selfExec("c -d:release --app:lib --noMain --gc:orc --outDir=. src/example_libretro.nim")

task clean, "Clean built files":
for file in listFiles("."):
let ext = splitFile(file).ext
if ext == ".dll" or ext == ".so" or ext == ".dylib":
echo "Deleting ", file
rmFile(file)
139 changes: 139 additions & 0 deletions lang/nim/src/example_libretro.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import bitops
import std/terminal
import example_libretro/libretro

const WIDTH = 320
const HEIGHT = 240
const FPS = 60
const SAMPLE_RATE = 48000

var video_cb: retro_video_refresh_t
var audio_cb: retro_audio_sample_t
var audio_batch_cb: retro_audio_sample_batch_t
var input_poll_cb: retro_input_poll_t
var environ_cb: retro_environment_t
var input_state_cb: retro_input_state_t

proc NimMain() {.cdecl, importc.}

# video framebuffer
var buf:ptr UncheckedArray[cuint]

# colors used for checkerboard
const color_r:uint32 = 0xff shl 16
const color_g:uint32 = 0xff shl 8

proc log_cb(level: retro_log_level, message: string) =
if level == RETRO_LOG_DEBUG:
stdout.styledWriteLine(fgBlue, "DEBUG: ", fgDefault, message)
elif level == RETRO_LOG_INFO:
stdout.styledWriteLine(fgYellow, "INFO: ", fgDefault, message)
elif level == RETRO_LOG_WARN:
stdout.styledWriteLine(bgMagenta, "WARN: ", fgDefault, message)
elif level == RETRO_LOG_ERROR:
stdout.styledWriteLine(fgRed, "ERROR: ", fgDefault, message)

proc retro_set_video_refresh*(cb: retro_video_refresh_t) {.cdecl,exportc,dynlib.} =
video_cb = cb

proc retro_set_audio_sample*(cb: retro_audio_sample_t) {.cdecl,exportc,dynlib.} =
audio_cb = cb

proc retro_set_audio_sample_batch*(cb: retro_audio_sample_batch_t) {.cdecl,exportc,dynlib.} =
audio_batch_cb = cb

proc retro_set_input_poll*(cb: retro_input_poll_t) {.cdecl,exportc,dynlib.} =
input_poll_cb = cb

proc retro_set_environment*(cb: retro_environment_t) {.cdecl,exportc,dynlib.} =
environ_cb = cb
var no_content:bool = true
discard cb(RETRO_ENVIRONMENT_SET_SUPPORT_NO_GAME, addr no_content)

proc retro_set_input_state*(cb: retro_input_state_t) {.cdecl,exportc,dynlib.} =
input_state_cb = cb

proc retro_init*() {.cdecl,exportc,dynlib.} =
NimMain()
var bufPtr0 = alloc0(sizeof(cuint) * WIDTH * HEIGHT)
buf = cast[ptr UncheckedArray[cuint]](bufPtr0)
log_cb(RETRO_LOG_DEBUG, "retro_init() called.")

proc retro_deinit*() {.cdecl,exportc,dynlib.} =
log_cb(RETRO_LOG_DEBUG, "retro_deinit() called.")
dealloc(buf)
GC_FullCollect()

proc retro_api_version*(): cuint {.cdecl,exportc,dynlib.} =
return RETRO_API_VERSION

proc retro_set_controller_port_device*(port: cuint; device: cuint) {.cdecl,exportc,dynlib.} =
log_cb(RETRO_LOG_DEBUG, "retro_set_controller_port_device() called.")
echo port, device

proc retro_get_system_info*(info: ptr retro_system_info) {.cdecl,exportc,dynlib.} =
info.library_name = "nim_example";
info.library_version = "v1"
info.need_fullpath = false
info.valid_extensions = nil # we don't use any ROMs

proc retro_get_system_av_info*(info: ptr retro_system_av_info) {.cdecl,exportc,dynlib.} =
info.timing.fps = FPS
info.timing.sample_rate = SAMPLE_RATE
info.geometry.base_width = WIDTH
info.geometry.base_height = HEIGHT
info.geometry.max_width = WIDTH
info.geometry.max_height = HEIGHT
info.geometry.aspect_ratio = WIDTH / HEIGHT

proc retro_reset*() {.cdecl,exportc,dynlib.} =
log_cb(RETRO_LOG_DEBUG, "retro_reset() called.")

proc retro_run*() {.cdecl,exportc,dynlib.} =
for y in 0 ..< HEIGHT:
let index_y = uint32 bitand((y shr 4), 1)
for x in 0 ..< WIDTH:
let b = ((y * WIDTH) + x)
let index_x = uint32 bitand((x shr 4), 1)
if bool bitxor(index_y, index_x):
buf[b] = color_r
else:
buf[b] = color_g
video_cb(buf, WIDTH, HEIGHT, (WIDTH shl 2))

proc retro_load_game*(info: ptr retro_game_info): bool {.cdecl,exportc,dynlib.} =
var fmt = RETRO_PIXEL_FORMAT_XRGB8888
if not environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, addr fmt):
log_cb(RETRO_LOG_INFO, "XRGB8888 is not supported.")
return false
return true

proc retro_unload_game*() {.cdecl,exportc,dynlib.} =
log_cb(RETRO_LOG_DEBUG, "retro_unload_game() called.")

proc retro_get_region*(): cuint {.cdecl,exportc,dynlib.} =
return 0 # NTSC

proc retro_load_game_special*(`type`: cuint; info: ptr retro_game_info; num: csize_t): bool {.cdecl,exportc,dynlib.} =
return true

proc retro_serialize_size*(): csize_t {.cdecl,exportc,dynlib.} =
return 0

proc retro_serialize*(data: pointer; size: csize_t): bool {.cdecl,exportc,dynlib.} =
return true

proc retro_unserialize*(data: pointer; size: csize_t): bool {.cdecl,exportc,dynlib.} =
return true

proc retro_get_memory_data*(id: cuint): pointer {.cdecl,exportc,dynlib.} =
discard

proc retro_get_memory_size*(id: cuint): csize_t {.cdecl,exportc,dynlib.} =
return 0

proc retro_cheat_reset*() {.cdecl,exportc,dynlib.} =
discard

proc retro_cheat_set*(index: cuint; enabled: bool; code: cstring) {.cdecl,exportc,dynlib.} =
discard
Loading