Fix sprites examples.

This commit is contained in:
Martín Lucas Golini
2026-07-23 01:50:59 -03:00
parent 373bd40e59
commit 24531b4411
2 changed files with 61 additions and 94 deletions
+55 -94
View File
@@ -1,97 +1,9 @@
#include <eepp/ee.hpp>
EE::Window::Window* win = NULL;
// Define a interpolation to control the Rock sprite angle
Interpolation1d RockAngle;
Interpolation1d PlanetAngle;
// Create a primitive drawer instance to draw the AABB of the Rock
Primitives P;
Sprite Rock;
Sprite Planet;
Sprite Monster;
// Define a user sprite event
static const Uint32 USER_SPRITE_EVENT = Sprite::SPRITE_EVENT_USER + 1;
// Get the sprite event callback
void spriteCallback( Uint32 Event, Sprite* Sprite, void* UserData ) {
// Sprite Animation entered the first frame?
if ( Event == Sprite::SPRITE_EVENT_FIRST_FRAME ) {
// Fire a user Event
Sprite->fireEvent( USER_SPRITE_EVENT );
} else if ( Event == USER_SPRITE_EVENT ) {
// Create an interpolation to change the angle of the sprite
Interpolation1d* RotationInterpolation = reinterpret_cast<Interpolation1d*>( UserData );
RotationInterpolation->clear();
RotationInterpolation->add( Sprite->getRotation() );
RotationInterpolation->add( Sprite->getRotation() + 45.f );
RotationInterpolation->setDuration( Milliseconds( 500 ) );
RotationInterpolation->setType(
Ease::BounceOut ); // Set the easing effect used for the interpolation
RotationInterpolation->start();
// Scale the sprite
if ( Sprite->getScale().x < 3 ) {
Sprite->setScale( Sprite->getScale() + 0.25f );
}
}
}
void mainLoop() {
// Clear the screen buffer
win->clear();
// Update the input
win->getInput()->update();
// Check if ESCAPE key is pressed
if ( win->getInput()->isKeyDown( KEY_ESCAPE ) ) {
// Close the window
win->close();
}
// Check if the D key was pressed
if ( win->getInput()->isKeyUp( KEY_D ) ) {
// Reverse the Rock animation
Rock.setReverseAnimation( !Rock.getReverseAnimation() );
}
// Update the angle interpolation
PlanetAngle.update( win->getElapsed() );
RockAngle.update( win->getElapsed() );
// Set the Planet and Rock angle from the interpolation
Planet.setRotation( PlanetAngle.getPosition() );
Rock.setRotation( RockAngle.getPosition() );
// Draw the static planet sprite
Planet.draw();
// Draw the animated Rock sprite
Rock.draw();
// Draw the monster animation
Monster.draw();
// Draw the Rock Axis-Aligned Bounding Box
P.setColor( Color( 255, 255, 255, 255 ) );
P.drawRectangle( Rock.getAABB() );
// Draw the Rock Quad
P.setColor( Color( 255, 0, 0, 255 ) );
P.drawQuad( Rock.getQuad() );
// Draw frame
win->display();
}
EE_MAIN_FUNC int main( int, char*[] ) {
// Create a new window
win = Engine::instance()->createWindow( WindowSettings( 640, 480, "eepp - Sprites" ),
ContextSettings( true ) );
auto win = Engine::instance()->createWindow( WindowSettings( 640, 480, "eepp - Sprites" ),
ContextSettings( true ) );
// Check if created
if ( win->isOpen() ) {
@@ -100,12 +12,18 @@ EE_MAIN_FUNC int main( int, char*[] ) {
// path.
FileSystem::changeWorkingDirectory( Sys::getProcessPath() );
// Load the rock texture
// Keep all graphics resources in this scope so they are released before Engine shutdown.
TexturePtr PlanetTex = TextureFactory::instance()->loadFromFile( "assets/sprites/7.png" );
TexturePtr RockTex = TextureFactory::instance()->loadFromFile( "assets/sprites/5.png" );
Sprite Rock;
Sprite Planet;
Sprite Monster;
Interpolation1d RockAngle;
Interpolation1d PlanetAngle;
Primitives P;
// Load a previously generated texture atlas that contains the TextureRegions needed to load
// an animated sprite
// an animated sprite.
TextureAtlasLoader Blindies( "assets/atlases/bnb.eta" );
// Create the animated rock spriteR
@@ -163,10 +81,53 @@ EE_MAIN_FUNC int main( int, char*[] ) {
PlanetAngle.start();
// Create a Event callback for the rock sprite
Rock.setEventsCallback( spriteCallback, &RockAngle );
Rock.setEventsCallback(
[userSpriteEvent = static_cast<Uint32>( Sprite::SPRITE_EVENT_USER + 1 )](
Uint32 event, Sprite* sprite, void* userData ) {
if ( event == Sprite::SPRITE_EVENT_FIRST_FRAME ) {
sprite->fireEvent( userSpriteEvent );
} else if ( event == userSpriteEvent ) {
auto* rotationInterpolation = static_cast<Interpolation1d*>( userData );
rotationInterpolation->clear();
rotationInterpolation->add( sprite->getRotation() );
rotationInterpolation->add( sprite->getRotation() + 45.f );
rotationInterpolation->setDuration( Milliseconds( 500 ) );
rotationInterpolation->setType( Ease::BounceOut );
rotationInterpolation->start();
if ( sprite->getScale().x < 3 )
sprite->setScale( sprite->getScale() + 0.25f );
}
},
&RockAngle );
// Application loop
win->runMainLoop( &mainLoop );
win->runMainLoop( [&] {
win->clear();
win->getInput()->update();
if ( win->getInput()->isKeyDown( KEY_ESCAPE ) )
win->close();
if ( win->getInput()->isKeyUp( KEY_D ) )
Rock.setReverseAnimation( !Rock.getReverseAnimation() );
PlanetAngle.update( win->getElapsed() );
RockAngle.update( win->getElapsed() );
Planet.setRotation( PlanetAngle.getPosition() );
Rock.setRotation( RockAngle.getPosition() );
Planet.draw();
Rock.draw();
Monster.draw();
P.setColor( Color( 255, 255, 255, 255 ) );
P.drawRectangle( Rock.getAABB() );
P.setColor( Color( 255, 0, 0, 255 ) );
P.drawQuad( Rock.getQuad() );
win->display();
} );
}
// Destroy the engine instance. Destroys all the windows and engine singletons.