Skip to content

Conversation

@mariovisic
Copy link
Contributor

@mariovisic mariovisic commented Jul 25, 2021

A very rough refactoring of the code used to render text, this should
hopefully provide an idea of where we can go. There's lots of
improvements to be made and this doesn't seem to render just yet, but it
should be good enough to figure out if this is the direction we want to
go.

The main parts to look at which will be reusable for every renderable item that's backed by a texture is: R2D_GL_DrawTexture in gl3.c as well as ruby2d_texture_ext_draw.

The updated render method on the ruby text class shows how it can be called, the calculation of the verticies will be moved out to a seperate re-usable ruby class.

TODO:

  • Hold onto the sdl surface in the text class and use it to get the width/height when loading a text object!
  • Get the text width/height on creation, before rendering (this will come from the surface)
  • Cache fonts in the fonts class
  • Create three seperate openGL texture rendering functions (ES, 2 and 3)
  • Re-implmenet text updating
  • Allow text size to be changed
  • Check to ensure fonts and textures are free'd after their use is finished

Update (6th of August)

Ready for review :)

A very rough refactoring of the code used to render text, this should
hopefully provide an idea of where we can go. There's lots of
improvements to be made and this doesn't seem to render just yet, but it
should be good enough to figure out if this is the direction we want to
go.
The vertices ordering was wrong and I was passing the wrong size for
vertices.
The code is ported from the C code, it currently works, but the rotation
isn't working for some reason, will need to look into it.
We can manage the state of a text object in ruby, rather than in C.
The code was using the passed in x/y coordinates to rotate around,
rather than a fixed point in the centre of the image. This is now fixed
and text now rotates again :)
Now multiple text objects can use the same font, so we don't have to use
up a file handle for every single one, this should help with performance
and resources in general.

Also update the API around Text and the way we interact with fonts and
textures to be more idiomatic ruby. Initially I wasn't using a font
instance, rather just storing our C font object inside of text, but by
having a font instance it cleans up the interface from text-> font
nicely.

Also the responsibility of creating a texture from a font was previously
on the font class, now that responsibility is in the Texture class.
Adding `Texture.load_text` tidies up this API as well!
I have manually tested openGL2 and it works fine, I can't test openGL ES
as it's not currently working.

In this patch I've refactored the `Vertices` class in ruby as previously
it was tied to the openGL 3 code's vertices array structure, with
everything bundled together. Now it presents co-ordinates, texture
co-ordinates and color information all separately for the individual
rendering functions to use to make the structures they need.

Vertices no longer does anything with color information, that is now
directly passed by the `Text` class.
@mariovisic
Copy link
Contributor Author

I have tested openGL 3 rendering as well as 2 (by setting FORCE_GL2 to true). I can't test openGL ES as it's not working currently but I can't see why it wouldn't work.

The texture class now holds onto an SDL surface object (rgb pixel data
in RAM) when it's created, then later we create a texture based on that
(rgb pixel data but stored in GPU memory rather than RAM).

Now that we get a surface early on, we can use it to extract the
width/height of the font which fixes our tests which were broken from
before. It also allows us to easily re-size text by creating a new
texture.
The surface is then used to create a new texture object in ruby. Also
remove some unused code to update the content of a text object.
When we change the size value of our text, we need to create a new 'font'
object as well as creating a new texture which holds the pixel data of the text
drawn out.

I have moved these two things "creating the font" and "creating the texture"
into new private methods on the text class for reuse.
Before we were doing it 4 times, I don't think it's a very expensive
operation, but if we have hundreds or thousands of objects it all ads
up.
'free_text' isn't used any more as we do not have a ruby 2d text object in c. I
also moved the C code that creates an SDL surface into the text.c file, out
from ruby2d.c directly.
@mariovisic mariovisic marked this pull request as ready for review August 6, 2021 10:30
Only leave the ruby specific data translation in the ruby.c file
@mariovisic mariovisic changed the title Rough refactor text rendering code Refactor text rendering code Aug 6, 2021
@mariovisic
Copy link
Contributor Author

This is looking good, ready to go :)

This fixes #191 as well as #181 🎉

@blacktm blacktm merged commit 08c83fa into ruby2d:main Aug 7, 2021
@mariovisic mariovisic deleted the text_render_refactor branch August 11, 2021 01:21
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