Skip to content

Commit

Permalink
Check in all of my changes; the R langauge integration has made good …
Browse files Browse the repository at this point in the history
…strides
  • Loading branch information
bryce-carson committed Sep 26, 2024
1 parent e307f75 commit 0d41518
Show file tree
Hide file tree
Showing 6 changed files with 869 additions and 155 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ include(cmake/squirrel.cmake)
include(cmake/pocketpy.cmake)
include(cmake/quickjs.cmake)
include(cmake/janet.cmake)
include(cmake/r.cmake)

include(cmake/core.cmake)
include(cmake/wave.cmake)
Expand Down
53 changes: 53 additions & 0 deletions cmake/r.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#################################################
# (R) R.h Rinternals.h Rembedded.h Rexts.h etc. #
#################################################
option(BUILD_WITH_R "R Enabled" ${BUILD_WITH_ALL})
message("BUILD_WITH_R: ${BUILD_WITH_R}")

if(BUILD_WITH_R AND NOT N3DS AND NOT BAREMETALAPI)
if(NOT BUILD_STATIC)
if(NOT DEFINED ENV{R_HOME} AND DEFINED PREFIX)
set(R_HOME "${PREFIX}/lib64/R")
endif()

if(NOT DEFINED ENV{LD_LIBRARY_PATH} AND NOT LD_LIBRARY_PATH)
set(LD_LIBRARY_PATH "${R_HOME}/lib:${PREFIX}/lib/jvm/jre/lib/server")
else()
# Add these entries to the this *PATH variable
set(LD_LIBRARY_PATH "${LD_LIBRARY_PATH}:${PREFIX}/lib64/R/lib:${PREFIX}/lib/jvm/jre/lib/server")
endif()

if(NOT DEFINED ENV{R_SHARE_DIR} AND DEFINED ENV{PREFIX})
set(R_SHARE_DIR "${PREFIX}/share/R")
else()
set(R_SHARE_DIR ENV{R_SHARE_DIR})
endif()

if(NOT DEFINED ENV{R_DOC_DIR} AND DEFINED ENV{PREFIX})
set(R_DOC_DIR "${PREFIX}/share/doc/R")
else()
set(R_DOC_DIR ENV{R_DOC_DIR})
endif()

if(DEFINED ENV{PREFIX} OR DEFINED PREFIX)
set(R_SRC "${R_HOME}/lib")
list(APPEND R_SRC "${PREFIX}/lib/jvm/jre/lib/server" R_SHARE_DIR R_DOC_DIR)
endif()

list(APPEND R_SRC ${CMAKE_SOURCE_DIR}/src/api/r.c)

add_library(r ${TIC_RUNTIME} ${R_SRC})
set_target_properties(r PROPERTIES PREFIX "")
endif()

target_link_libraries(r PRIVATE runtime)

set_target_properties(r PROPERTIES LINKER_LANGUAGE CXX)
target_include_directories(r
PUBLIC ${R_DIR}
PRIVATE
${CMAKE_SOURCE_DIR}/include
${CMAKE_SOURCE_DIR}/src
)

endif()
91 changes: 91 additions & 0 deletions demos/bunny/rbenchmark.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
## title: Bunnymark in Python
## author: Bryce Carson
## desc: Benchmarking tool to see how many bunnies can fly around the screen, using R
## input: gamepad
## script: r
## version: 0.0.1

screenWidth <- 240
screenHeight <- 136
toolbarHeight <- 6
t <- 0

new_bunny <- function() {
velocityRUnif <- \() runif(1, -100.0, 100.0) / 60.0
xV <- velocityRUnif()
yV <- velocityRUnif()

newBunny <-
structure(sqrt(xV^2 + yV^2),
width = 26,
height = 32,
x = sample(0:(screenWidth - width), 1),
y = sample(0:(screenHeight - height), 1),
speed_x = xV,
speed_y = yV,
sprite = 1,
class = "Bunny"
)
newBunny
}

draw_bunny <- function(bunny) {
## stopifnot(is(bunny, "Bunny"))
with(attributes(bunny),
t80.spr(sprite, x, y, 1, 1, 0, 0, 4, 4))
}

update_bunny <- function(bunny) {
## stopifnot(is(bunny, "Bunny"))
attr(bunny, "x") <- attr(bunny, "x") + attr(bunny, "speed_x")
attr(bunny, "y") <- attr(bunny, "y") + attr(bunny, "speed_y")

if (attr(bunny, "x") + attr(bunny, "width") > screenWidth) {
attr(bunny, "x") <- screenWidth - attr(bunny, "width")
attr(bunny, "speed_x") <- attr(bunny, "speed_x") * -1
}

if (attr(bunny, "y") + attr(bunny, "height") > screenHeight) {
attr(bunny, "y") <- screenHeight - attr(bunny, "height")
attr(bunny, "speed_y") <- attr(bunny, "speed_y") * -1
}

if (attr(bunny, "x") < 0) {
attr(bunny, "x") <- 0
attr(bunny, "speed_x") <- attr(bunny, "speed_x") * -1.0
}

if (attr(bunny, "y") < toolbarHeight) {
attr(bunny, "y") <- toolbarHeight
attr(bunny, "speed_y") <- attr(bunny, "speed_y") * -1.0
}
}

## FIXME: this removes the attributes. S3 classes need to define special
## methods and generics to work with various primitive classes and generics
## like list. Consider data.frame, which is a list, but which does not lose
## its attributes when you make a list of data.frames.
bunnies <- list(new_bunny())

## <TILES>
## 001:11111100111110dd111110dc111110dc111110dc111110dc111110dd111110dd
## 002:00011110ddd0110dccd0110dccd0110dccd0110dccd0110dcddd00dddddddddd
## 003:00001111dddd0111cccd0111cccd0111cccd0111cccd0111dcdd0111dddd0111
## 004:1111111111111111111111111111111111111111111111111111111111111111
## 017:111110dd111110dd111110dd111110dd10000ddd1eeeeddd1eeeeedd10000eed
## 018:d0ddddddd0ddddddddddddddddd0000dddddccddddddccdddddddddddddddddd
## 019:0ddd01110ddd0111dddd0111dddd0111ddddd000ddddddddddddddddddddd000
## 020:1111111111111111111111111111111101111111d0111111d011111101111111
## 033:111110ee111110ee111110ee111110ee111110ee111110ee111110ee111110ee
## 034:dddcccccddccccccddccccccddccccccddccccccdddcccccdddddddddddddddd
## 035:dddd0111cddd0111cddd0111cddd0111cddd0111dddd0111dddd0111dddd0111
## 036:1111111111111111111111111111111111111111111111111111111111111111
## 049:111110ee111110ee111110ee111110ee111110ee111110ee111110ee11111100
## 050:dddeeeeeddeeeeeed00000000111111101111111011111110111111111111111
## 051:eddd0111eedd01110eed011110ee011110ee011110ee011110ee011111001111
## 052:1111111111111111111111111111111111111111111111111111111111111111
## </TILES>

## <PALETTE>
## 000:1a1c2c5d275db13e53ef7d57ffcd75a7f07038b76425717929366f3b5dc941a6f673eff7f4f4f494b0c2566c86333c57
## </PALETTE>
57 changes: 57 additions & 0 deletions demos/rdemo.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
## title: game title
## author: game developer, email, etc.
## desc: short description
## site: website link
## license: MIT License (change this to your license of choice)
## version: 0.1
## script: r

t <- 0
x <- 96
y <- 24

`makeopfn` <- \(f) \(x) f(x, 1); inc <- makeopfn(`+`); dec <- makeopfn(`-`)

TIC <- function() {
mapply(.f = \(b, o) if (btn(b)) o,
.x = 0:3,
.y = list(dec(y), inc(y),
dec(x), inc(x)))
t80.cls(13)
t80.spr(id = 1 + (t %% 60) / 30 * 2,
scale = 3,
x, y,
colorkey = 14,
w = 2, h = 2)
t80.print("HELLO WORLD!", 84, 84)
inc(t)
}

## <TILES>
## 001:eccccccccc888888caaaaaaaca888888cacccccccacc0ccccacc0ccccacc0ccc
## 002:ccccceee8888cceeaaaa0cee888a0ceeccca0ccc0cca0c0c0cca0c0c0cca0c0c
## 003:eccccccccc888888caaaaaaaca888888cacccccccacccccccacc0ccccacc0ccc
## 004:ccccceee8888cceeaaaa0cee888a0ceeccca0cccccca0c0c0cca0c0c0cca0c0c
## 017:cacccccccaaaaaaacaaacaaacaaaaccccaaaaaaac8888888cc000cccecccccec
## 018:ccca00ccaaaa0ccecaaa0ceeaaaa0ceeaaaa0cee8888ccee000cceeecccceeee
## 019:cacccccccaaaaaaacaaacaaacaaaaccccaaaaaaac8888888cc000cccecccccec
## 020:ccca00ccaaaa0ccecaaa0ceeaaaa0ceeaaaa0cee8888ccee000cceeecccceeee
## </TILES>

## <WAVES>
## 000:00000000ffffffff00000000ffffffff
## 001:0123456789abcdeffedcba9876543210
## 002:0123456789abcdef0123456789abcdef
## </WAVES>

## <SFX>
## 000:000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000304000000000
## </SFX>

## <PALETTE>
## 000:1a1c2c5d275db13e53ef7d57ffcd75a7f07038b76425717929366f3b5dc941a6f673eff7f4f4f494b0c2566c86333c57
## </PALETTE>

## <TRACKS>
## 000:100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
## </TRACKS>
Loading

0 comments on commit 0d41518

Please sign in to comment.