Skip to content

Commit

Permalink
switch to deer spritesheet
Browse files Browse the repository at this point in the history
  • Loading branch information
coolbutuseless committed May 31, 2024
1 parent 496d0dc commit be161db
Show file tree
Hide file tree
Showing 25 changed files with 216 additions and 75 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ doc
Meta
pkgdown
working
man/figures/logo/
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ Suggests:
knitr,
rmarkdown
Config/testthat/edition: 3
Copyright: The included 'deer' spritesheet is by Calciumtrice and licensed under CC BY 3.0. See 'inst/LICENSE-deer.txt'
for more details.
VignetteBuilder: knitr
13 changes: 8 additions & 5 deletions R/data-sets.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Sprite sheet for some animated characters
#' Sprite sheet for an animated deer
#'
#' A sprite sheet for animated characters. This spritesheet is in the
#' public domain.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"king"
"deer"

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Locations of 16 sprites within the \code{king} spritesheet
#' Locations of 15 sprites within the \code{deer} spritesheet
#'
#' List of 16 numeric vectors. Each vector consistes of (x, y, width, height)
#' List of 15 numeric vectors. Each vector consistes of (x, y, width, height)
#' location of a sprite within the spritesheet.
#'
#' This 'deer' spritesheet is by Calciumtrice and licensed under CC BY 3.0.
#' See 'inst/LICENSE-deer.txt' for more details.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"king_locs"
"deer_loc"

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' A list of 36 isometric tiles in nativeRaster format
Expand Down
71 changes: 46 additions & 25 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,48 @@ if (FALSE) {
}
```

# nara <img src="man/figures/logo-nara.png" align="right" width="300" />

```{r echo=FALSE, eval=FALSE}
grid.newpage()
back <- read_png("man/figures/logo-nara-deer.png")
N <- 100
count <- 1
for (i in c(1:10, rep(10, 10))) {
filename <- sprintf("man/figures/logo/logo%02i.png", count); count <- count + 1
png(filename, 480, 480)
fore <- nr_new(N, N, colour_to_integer('#ffffff00'))
grid.raster(back)
nr_blit2(fore, N/2 - 32/2, N/1.75 - 32/2, deer, deer_loc[[i]])
grid.raster(fore, interpolate = FALSE)
dev.off()
}
# convert -delay 30 *.png logo1.gif
# gifsicle -O3 --colors 16 --lossy=30 -o logo.gif logo1.gif
```





# nara <img src="man/figures/logo.gif" align="right" width="300" />

<!-- badges: start -->
![](https://img.shields.io/badge/cool-useless-green.svg)
[![R-CMD-check](https://github.com/coolbutuseless/nara/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/coolbutuseless/nara/actions/workflows/R-CMD-check.yaml)
<!-- badges: end -->

`{nara}` provides tools for working with R's `nativeRaster` image format to
`{nara}` provides tools for wordeer with R's `nativeRaster` image format to
enable fast double-buffered graphics rendering.

### Why?

`nativeRaster` buffers are fast enough to use for rendering at speed >30 frames-per-second.
This makes them useful for games and other interactive applications.

### Example

An example graphics demo is this non-playable version of pacman created and running in R in realtime [(see the vignette)](https://coolbutuseless.github.io/package/nara/articles/pacman.html):

<img src="man/figures/pacman.png" width="50%" />


# Details

Expand Down Expand Up @@ -135,9 +156,9 @@ coords <- expand.grid(y = seq(0, h-1) * 30 + 1, x = seq(0, w-1) * 30 + 1)
nr_rect(nr, x = coords$x, y = coords$y, w = 27, h = 27, fill = colours)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Draw a bunch of kings
# Draw a bunch of deers
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
nr_blit2(nr, x = sample(300, 15), y = sample(200, 15), king, king_locs[[1]])
nr_blit2(nr, x = sample(300, 15), y = sample(200, 15), deer, deer_loc[[1]])
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -169,22 +190,22 @@ grid.raster(nr, interpolate = FALSE)

## Static Rendering: Displaying Sprites

Included with `{nara}` are 16 frames of an animated king character - see `king` data.
Included with `{nara}` are 16 frames of an animated deer character - see `deer` data.

These frames are consolidated into a single image called a *spritesheet* and `king_locs`
These frames are consolidated into a single image called a *spritesheet* and `deer_loc`
contains the coordiates of 16 sprites within that image.

#### Blit the first `king` frame onto a native raster canvas.
#### Blit the first `deer` frame onto a native raster canvas.

```{r fig.height = 2}
library(grid)
nr <- nr_new(100, 30, 'grey80')
nr_blit2(nr, 2, 1, king, king_locs[[1]])
nr_blit2(nr, 2, 1, deer, deer_loc[[1]])
grid.raster(nr, interpolate = FALSE)
```

#### Dynamic (realtime) Rendering: Animated King
#### Dynamic (realtime) Rendering: Animated deer

The reason to use `{nara}` is that operations are fast enough that `nativeRaster`
can be used as an *in-memory* buffer for a double-bufferred rendering system.
Expand All @@ -193,7 +214,7 @@ can be used as an *in-memory* buffer for a double-bufferred rendering system.
one buffer being shown to the user, and the other existing in memory as a
place to render.

In this example, the `king` sprite is rendered to a `nativeRaster` image. This
In this example, the `deer` sprite is rendered to a `nativeRaster` image. This
*in-memory* buffer is then displayed to the user using `grid.raster()`.

By altering the position and animation frame every time the kind is shown,
Expand All @@ -217,15 +238,15 @@ nr <- nr_new(100, 30, 'grey80')
# Clear, blit and render => animation!
for (i in -30:110) {
nr_fill(nr, 'grey80') # Clear the nativeRaster
nr_blit2(nr, i, 1, king, king_locs[[((i/2) %% 8) + 1]]) # copy king to nativeRaster
nr_blit2(nr, i, 1, deer, deer_loc[[((i/3) %% 5) + 11]]) # copy deer to nativeRaster
grid.raster(nr, interpolate = FALSE) # copy nativeRaster to screen
Sys.sleep(0.03) # Stop animation running too fast.
}
```

#### Live screen recording

<img src="man/figures/king.gif" />
<img src="man/figures/deer.gif" />



Expand Down Expand Up @@ -265,21 +286,21 @@ vx <- sample(seq(-5, 5), N, replace = TRUE)
vy <- sample(seq(-5, 5)[-6], N, replace = TRUE)
# Create an empty nativeraster with a grey background
nr <- nr_new(w, h, 'grey80')
nr <- nr_new(w, h, 'white')
for (frame in 1:1000) {
# Clear the nativeraster and blit in all the kings
nr_fill(nr, 'grey80')
nr_blit2(nr, x, y, king, king_locs[[(frame) %% 16 + 1]])
# Clear the nativeraster and blit in all the deers
nr_fill(nr, 'white')
nr_blit2(nr, x, y, deer, deer_loc[[(frame) %% 5 + 11]])
# Draw the nativeraster to screen
dev.hold()
grid.raster(nr, interpolate = FALSE)
dev.flush()
# Update the position and velocity of each king
# Kings move at constant velocity and bounce off the sides of the image
# Update the position and velocity of each deer
# deers move at constant velocity and bounce off the sides of the image
x <- x + vx
y <- y + vy
vx <- ifelse(x > (w - 25)| x < 1, -vx, vx)
Expand Down Expand Up @@ -369,7 +390,7 @@ nr_rect(
)
ras <- nr_to_raster(nr)
ras[] <- 'yellow' # looking up named colours at render time is SLOOOOOW
ras[] <- 'yellow' # loodeer up named colours at render time is SLOOOOOW
x11(type = 'dbcairo', antialias = 'none')
dev.control('inhibit')
Expand Down
54 changes: 23 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@

<!-- README.md is generated from README.Rmd. Please edit that file -->

# nara <img src="man/figures/logo-nara.png" align="right" width="300" />
# nara <img src="man/figures/logo.gif" align="right" width="300" />

<!-- badges: start -->

![](https://img.shields.io/badge/cool-useless-green.svg)
[![R-CMD-check](https://github.com/coolbutuseless/nara/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/coolbutuseless/nara/actions/workflows/R-CMD-check.yaml)
<!-- badges: end -->

`{nara}` provides tools for working with R’s `nativeRaster` image format
`{nara}` provides tools for wordeer with R’s `nativeRaster` image format
to enable fast double-buffered graphics rendering.

### Why?
Expand All @@ -18,14 +18,6 @@ to enable fast double-buffered graphics rendering.
\>30 frames-per-second.
This makes them useful for games and other interactive applications.

### Example

An example graphics demo is this non-playable version of pacman created
and running in R in realtime [(see the
vignette)](https://coolbutuseless.github.io/package/nara/articles/pacman.html):

<img src="man/figures/pacman.png" width="50%" />

# Details

`{nara}`:
Expand Down Expand Up @@ -112,9 +104,9 @@ coords <- expand.grid(y = seq(0, h-1) * 30 + 1, x = seq(0, w-1) * 30 + 1)
nr_rect(nr, x = coords$x, y = coords$y, w = 27, h = 27, fill = colours)

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Draw a bunch of kings
# Draw a bunch of deers
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
nr_blit2(nr, x = sample(300, 15), y = sample(200, 15), king, king_locs[[1]])
nr_blit2(nr, x = sample(300, 15), y = sample(200, 15), deer, deer_loc[[1]])


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -142,29 +134,29 @@ nr_text(nr, x = 180, y = 1, str = "Hello #RStats", fontsize = 16)
grid.raster(nr, interpolate = FALSE)
```

<img src="man/figures/README-unnamed-chunk-2-1.png" width="100%" />
<img src="man/figures/README-unnamed-chunk-3-1.png" width="100%" />

## Static Rendering: Displaying Sprites

Included with `{nara}` are 16 frames of an animated king character - see
`king` data.
Included with `{nara}` are 16 frames of an animated deer character - see
`deer` data.

These frames are consolidated into a single image called a *spritesheet*
and `king_locs` contains the coordiates of 16 sprites within that image.
and `deer_loc` contains the coordiates of 16 sprites within that image.

#### Blit the first `king` frame onto a native raster canvas.
#### Blit the first `deer` frame onto a native raster canvas.

``` r
library(grid)

nr <- nr_new(100, 30, 'grey80')
nr_blit2(nr, 2, 1, king, king_locs[[1]])
nr_blit2(nr, 2, 1, deer, deer_loc[[1]])
grid.raster(nr, interpolate = FALSE)
```

<img src="man/figures/README-unnamed-chunk-3-1.png" width="100%" />
<img src="man/figures/README-unnamed-chunk-4-1.png" width="100%" />

#### Dynamic (realtime) Rendering: Animated King
#### Dynamic (realtime) Rendering: Animated deer

The reason to use `{nara}` is that operations are fast enough that
`nativeRaster` can be used as an *in-memory* buffer for a
Expand All @@ -174,7 +166,7 @@ double-bufferred rendering system.
with one buffer being shown to the user, and the other existing in
memory as a place to render.

In this example, the `king` sprite is rendered to a `nativeRaster`
In this example, the `deer` sprite is rendered to a `nativeRaster`
image. This *in-memory* buffer is then displayed to the user using
`grid.raster()`.

Expand All @@ -200,15 +192,15 @@ nr <- nr_new(100, 30, 'grey80')
# Clear, blit and render => animation!
for (i in -30:110) {
nr_fill(nr, 'grey80') # Clear the nativeRaster
nr_blit2(nr, i, 1, king, king_locs[[((i/2) %% 8) + 1]]) # copy king to nativeRaster
nr_blit2(nr, i, 1, deer, deer_loc[[((i/3) %% 5) + 11]]) # copy deer to nativeRaster
grid.raster(nr, interpolate = FALSE) # copy nativeRaster to screen
Sys.sleep(0.03) # Stop animation running too fast.
}
```

#### Live screen recording

<img src="man/figures/king.gif" />
<img src="man/figures/deer.gif" />

## Multi-Ball

Expand Down Expand Up @@ -243,21 +235,21 @@ vx <- sample(seq(-5, 5), N, replace = TRUE)
vy <- sample(seq(-5, 5)[-6], N, replace = TRUE)

# Create an empty nativeraster with a grey background
nr <- nr_new(w, h, 'grey80')
nr <- nr_new(w, h, 'white')


for (frame in 1:1000) {
# Clear the nativeraster and blit in all the kings
nr_fill(nr, 'grey80')
nr_blit2(nr, x, y, king, king_locs[[(frame) %% 16 + 1]])
# Clear the nativeraster and blit in all the deers
nr_fill(nr, 'white')
nr_blit2(nr, x, y, deer, deer_loc[[(frame) %% 5 + 11]])

# Draw the nativeraster to screen
dev.hold()
grid.raster(nr, interpolate = FALSE)
dev.flush()

# Update the position and velocity of each king
# Kings move at constant velocity and bounce off the sides of the image
# Update the position and velocity of each deer
# deers move at constant velocity and bounce off the sides of the image
x <- x + vx
y <- y + vy
vx <- ifelse(x > (w - 25)| x < 1, -vx, vx)
Expand Down Expand Up @@ -310,7 +302,7 @@ for (y in seq(350, 0, -30)) {
grid.raster(nr, interpolate = FALSE)
```

<img src="man/figures/README-unnamed-chunk-6-1.png" width="100%" />
<img src="man/figures/README-unnamed-chunk-7-1.png" width="100%" />

## Coordinate System

Expand Down Expand Up @@ -342,7 +334,7 @@ nr_rect(
)

ras <- nr_to_raster(nr)
ras[] <- 'yellow' # looking up named colours at render time is SLOOOOOW
ras[] <- 'yellow' # loodeer up named colours at render time is SLOOOOOW

x11(type = 'dbcairo', antialias = 'none')
dev.control('inhibit')
Expand Down
Binary file added data-raw/deer female calciumtrice.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit be161db

Please sign in to comment.