Tutorial 6: More about Sprites & Entities
In this tutorial you will learn more about sprites and entities.
Before starting this tutorial be sure to setup an empty project with the Setup Tutorial.
What will be on this tutorial?
This tutorial will highlight some useful functions related to sprites and entities, and how to use them. Note: these are only functions we did not cover in previous tutorials!
It's very reasonable that you will want to apply some default settings to all the sprites you are about to create. if you set Sprite::Defaults, it will give default values to all new sprites you create after the change. for example:
That's it. :)
Now that you know more about sprites and entities, time to learn about animators!
Ness::Sprite::Defaults.alpha_channels = true;
Ness::Color finalCol = sprite->get_absolute_transformations().color;
sprite->set_source_from_sprite_sheet(Ness::Pointi(1, 1), Ness::Sizei(3, 4), true);
will make all newly created sprites begin with blending mode equal to BLEND (support opacity and texture alpha channels).
check out the code to see more default options.
Calling this function will return the absolute transformations that apply on this entity (this applies on every entity, not just sprite). if the object requires an update (parent node changed), it will also update the internal state of the entity.
for example, the following code:
will return the final color of the sprite, after multiplied with all the parents.
If you are worrying about efficiency, you should not. once a node changes, all its son entities and their son entities learn that they need an update. once rendered, they will update themselves automatically. when calling 'get_absolute_gransformations()' if the entity is already up-to-date, it will just return its internal state. if it needs an update, it will update itself but when rendering next - it will not need additional update.
If from any reason you do not wish to change the internal state of the entity, you can always call the const version of the function, which only return the internal state (which may not include recent changes if didn't have the chance to update).
note that there are some functions to get some specific absolute transformations. for example, get_absolute_size(), which returns the size of the entity multiplied by its absolute scale. the other 'get_absolute_xxx' functions are only helpers, you can calculate all these values by using 'get_absolute_transformations()'.
Without changing the internal state of the sprite, this function returns a rectangle representing the last region this entity was rendered upon. for example, if an entity was last rendered on final position of (100, 100) & final size of (50, 50), this function will return rectangle with those exact values. NOTE: this does not include camera position!
Did you know? you can render entities outside a scene, by calling their render() function directly. you can even provide it with a camera object. for example:
This is a very useful function to disable/enable the rendering of a specific entity or node. you can disable an entire node and all its son nodes and entities, or disable just a single entity. for example:
This function will answer a simple question - is this entity currently visible on screen? this function takes into account the visibility of the entity itself, it's opacity, and weather or not it's inside screen. this function can also get a camera to take into consideration. for example:
will tell you if the given sprite is seeing by the player with the given camera.
Note: nodes also support this function! they will just call recursively all their son nodes and entities and if at least one of them is really visible, the entire node will be considered as visible as well.
You might find the need to attach your own personal data to a renderable entity. for example, you might want to link a specific sprite to an instance of a class you have. to do so, ness-engine allows you to tie any void pointer to any renderable object (including nodes):
will attach the pointer of myClass to the entity. later you can get it with:
for safety reasons, when you get your own data and cast it to your desired data type, consider using dynamic_cast.
note: user_data defaults to nullptr, so you can easily check if such data exists or not.
This is a very useful function to help you set the source rectangle of your sprite from a texture sheet. in case you don't remember: the source rect is the part you "copy" from the texture and render on screen.
how it works? let's say you have the following texture sheet:
as you can see it has 4 rows (y axis) and 3 colums (x axis). this function helps you set the source rectangle to a specific animation step. for example, you might want to pick step (1,1), which is the middle of the walking-right animation.
the function get the following params:
step: Ness::Pointi instance that represent the x and y index of the step you want to pick
stepsCount: Ness::Sizei instance representing how many rows and colums the sprite sheet has
setSize: if true, will also set the size of the sprite (target rectangle) to match the animation step
will set the example above to the middle of the walking right animation.
Note: while this function is useful to pick specific steps or objects from a sprite sheet, there are better ways to do sprite animations, which we will learn in next tutorial about animators.