-
Notifications
You must be signed in to change notification settings - Fork 75
Refactor text rendering code #204
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
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 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.
Contributor
Author
|
I have tested openGL 3 rendering as well as 2 (by setting |
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.
Only leave the ruby specific data translation in the ruby.c file
Contributor
Author
blacktm
approved these changes
Aug 7, 2021
This was referenced Aug 7, 2021
Closed
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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_DrawTextureingl3.cas well asruby2d_texture_ext_draw.The updated
rendermethod 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:
Update (6th of August)
Ready for review :)