Skip to content

Commit

Permalink
SpriteHandler_examples.h:
Browse files Browse the repository at this point in the history
* Added define USE_DYNAMICS_SYSTEM which if true showcases integration of the sprite position (centroid) as a function of its velocity and acceleration. Affects Sprite0 in example1 now.
* Added define DBG_DRAW_SPRITES which is a simple way of toggling the debug drawing in both examples.
  • Loading branch information
razterizer committed Oct 25, 2024
1 parent 92f8527 commit 2560bf3
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions Examples/SpriteHandler_examples.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
#pragma once
#include "../SpriteHandler.h"
#include "../ScreenUtils.h"
#include "../Dynamics/CollisionHandler.h"
#include "../Dynamics/DynamicsSystem.h"

//#define USE_DYNAMICS_SYSTEM
//#define DBG_DRAW_SPRITES

namespace sprite_handler
{
Expand All @@ -20,6 +24,9 @@ namespace sprite_handler
keyboard::KeyPressData kpd;
auto keyboard = std::make_unique<keyboard::StreamKeyboard>();

dynamics::DynamicsSystem dyn_sys;
dynamics::CollisionHandler coll_handler;

rnd::srand_time();

// //////////////////////////////
Expand Down Expand Up @@ -63,6 +70,9 @@ namespace sprite_handler
return anim % 2;
};

sprite0->pos = { 17, 8 };
dyn_sys.add_rigid_body(sprite0, { -15.f, 2.5f }, { 6.f, 0.f });

// ///////////////////////////////////////////////////////////

auto* sprite1 = sprh.create_bitmap_sprite("alien");
Expand Down Expand Up @@ -154,17 +164,18 @@ namespace sprite_handler
// ///////////////////////////////////////////////////////////
// LET's GO ! //
// ///////////////////////////////////////////////////////////



begin_screen();

float dt = 0.01f;
for (int i = -3; i < sh.num_rows(); ++i)
{
for (int j = -5; j < sh.num_cols(); ++j)
{
#ifndef USE_DYNAMICS_SYSTEM
sprite0->pos.r = i;
sprite0->pos.c = i%2==0 ? j : 35-j;
#endif

int anim_frame = (i + 3)*44 + (j + 5);
auto t = static_cast<float>(anim_frame)/(23.f*45.f);
Expand All @@ -180,17 +191,22 @@ namespace sprite_handler
ast_sprite->pos.r = math::roundI(r_pos);
}

#ifdef USE_DYNAMICS_SYSTEM
dyn_sys.update(0.02f, anim_frame);
#endif
return_cursor();
sh.clear();
sprh.draw(sh, anim_frame);
//sprh.draw_dbg(sh, anim_frame); // Uncomment to draw AABB.
#ifdef DBG_DRAW_SPRITES
sprh.draw_dbg(sh, anim_frame); // Uncomment to draw AABB.
#endif
sh.print_screen_buffer(Color::Black);
Delay::sleep(0'200'000);

kpd = keyboard->readKey();
auto key = keyboard::get_char_key(kpd);
auto lo_key = str::to_lower(key);
if (lo_key == 'q')
if (lo_key == 'q' || sprite0->pos.r > sh.num_rows())
goto quit;
}
}
Expand Down Expand Up @@ -243,7 +259,9 @@ namespace sprite_handler
return_cursor();
sh.clear();
sprh.draw(sh, anim_frame);
//sprh.draw_dbg(sh, anim_frame); // Uncomment to draw AABB.
#ifdef DBG_DRAW_SPRITES
sprh.draw_dbg(sh, anim_frame); // Uncomment to draw AABB.
#endif
sh.print_screen_buffer(Color::Black);
Delay::sleep(0'20'000);

Expand Down

0 comments on commit 2560bf3

Please sign in to comment.