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

Tilesets #201

Merged
merged 11 commits into from
Jul 20, 2021
Merged

Tilesets #201

merged 11 commits into from
Jul 20, 2021

Conversation

mariovisic
Copy link
Contributor

@mariovisic mariovisic commented Jul 17, 2021

A tileset implementation

TODO:

  • Complete the code for openGL ES, 1.2 and 1.3
  • Add commenting documentation to the code
  • Fix code formatting / consistency with existing ruby2d code
  • Add ability to set width/height of the original tileset, to scale the tile size, same how sprites work
  • Add ability to rotate tiles (same as sprites)
  • Add ability to mirror tiles (same as sprites)
  • Look into performance of the code, we may need to pre-render out the tiles when they're defined, rather than currently we render the entire image for every tile and then crop what we need, this may be slow for large tilesets.
  • Add a way to remove or clear all set tiles
  • Add direct draw code

@mariovisic
Copy link
Contributor Author

mariovisic commented Jul 18, 2021

A note on performance, I tested an extreme case with a 1024 x 576 window size, using a large 4032 × 3024 tileset image. I Then selected a random 100 16x16 tiles, to use and filled the entire 1024x576 window with tiles, so 2304 tiles in total.

On my M1 laptop I see around ~ 44% CPU usage from the process and ~ 31% GPU usage shown in activity monitor. This seems 'acceptible' as it should still hit 60fps on a machine half as powerful in an extreme case (i'd expect a typical tileset to be smaller and tiles to be larger). but performance could definitely be improved by avoiding rendering the entire image each time.

@mariovisic
Copy link
Contributor Author

Code to test performance:

require 'ruby2d'

GRID_SIZE = 16

set background: 'white'
set width: GRID_SIZE * 64
set height: GRID_SIZE * 36

tileset = Tileset.new('/path/to/very/large/image.jpeg', tile_width: GRID_SIZE, tile_height: GRID_SIZE, padding: 0, spacing: 0)

100.times { |i| tileset.define_tile(i.to_s, rand(252),rand(189)) }

64.times do |i|
  36.times do |j|
    tileset.set_tile(rand(100).to_s, [
      {x: GRID_SIZE * i,  y: GRID_SIZE * j}
    ])
  end
end

show

@blacktm
Copy link
Member

blacktm commented Jul 18, 2021

On a performance note, we actually haven’t implemented batch rendering for textures yet (here’s what it looks like for triangles), so that should dramatically speed things up when we do through reduced draw calls.

@mariovisic
Copy link
Contributor Author

mariovisic commented Jul 20, 2021

Output from test/tileset.rb: 😂

The numbers aren't exactly the same size so this output is expected 👌

tileset test output

@mariovisic mariovisic marked this pull request as ready for review July 20, 2021 08:09
mariovisic and others added 11 commits July 20, 2021 14:09
Multiple tiles are being rendered from the same tileset successfully.
I believe some of the values may be mixed up and so the rendering seems
a bit broken at the moment. It's squashing an image rather than cropping
it I believe.
The 4 texture x/y points are meant to be floats scaled to the width/height of
the image, so fixed that. Also

I was incorrectly using `INT2NUM` rather than `NUM2INT` which was giving weird
random numbers instead of the passed values, fixed that also :)
Also rename some variables in `Tileset` for clarity
@blacktm blacktm merged commit 468dd20 into ruby2d:main Jul 20, 2021
@mariovisic mariovisic deleted the tilesets branch July 20, 2021 22:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants