Skip to content

Commit

Permalink
Improved sprite demo.
Browse files Browse the repository at this point in the history
  • Loading branch information
razterizer committed Oct 17, 2024
1 parent 8d2279d commit 1839c26
Showing 1 changed file with 157 additions and 19 deletions.
176 changes: 157 additions & 19 deletions Examples/SpriteHandler_examples.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,29 @@ namespace sprite_handler
Text tt;
SpriteHandler sprh;

clear_screen();
return_cursor();
hide_cursor();
rnd::srand_time();

auto* sprite0 = sprh.create_sprite("spaceship");
sprite0->layer_id = 4;
sprite0->init(4, 5);
sprite0->init_frame(0);
sprite0->set_sprite_chars(0,
' ', ' ', '_', ' ', ' ',
' ', '/', '#', '\\', ' ',
'[', '-', '-', '-', ']',
' ', '{', '#', '}', ' '
sprite0->set_sprite_chars_from_strings(0,
" _ ",
" /#\\ ",
"[---]",
" {#} "
);
sprite0->set_sprite_fg_colors(0,
Color::Default, Color::Default, Color::White, Color::Default, Color::Default,
Color::Default, Color::Red, Color::Magenta, Color::Red, Color::Default,
Color::Green, Color::Cyan, Color::Cyan, Color::Cyan, Color::Green,
Color::Default, Color::Yellow, Color::Magenta, Color::Yellow, Color::Default
0, 0, 16, 0, 0,
0, 10, 14, 10, 0,
11, 15, 15, 15, 11,
0, 12, 14, 12, 0
);
sprite0->set_sprite_bg_colors(0,
Color::Transparent2, Color::Transparent2, Color::Transparent2, Color::Transparent2, Color::Transparent2,
Color::Transparent2, Color::Transparent2, Color::DarkMagenta, Color::Transparent2, Color::Transparent2,
Color::Transparent2, Color::DarkCyan, Color::DarkCyan, Color::DarkCyan, Color::Transparent2,
Color::Transparent2, Color::Transparent2, Color::DarkMagenta, Color::Transparent2, Color::Transparent2
-2, -2, -2, -2, -2,
-2, -2, 14, -2, -2,
-2, 7, 7, 7, -2,
-2, -2, 14, -2, -2
);
sprite0->init_frame(1);
sprite0->set_sprite_chars(1,
Expand Down Expand Up @@ -71,7 +70,10 @@ namespace sprite_handler
return anim % 2;
};

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

auto* sprite1 = sprh.create_sprite("alien");
sprite1->layer_id = 5;
sprite1->init(2, 3);
sprite1->init_frame(0);
sprite1->set_sprite_chars(0,
Expand Down Expand Up @@ -107,18 +109,154 @@ namespace sprite_handler
return anim % 2;
};

for (int i = -3; i < 20; ++i)
// ///////////////////////////////////////////////////////////

std::array<std::tuple<Sprite*, float, float>, 16> asteroids;
for (int a_idx = 0; a_idx < asteroids.size(); ++a_idx)
{
for (int j = -5; j < 40; ++j)
auto* sprite2 = sprh.create_sprite("asteroid" + std::to_string(a_idx));
sprite2->pos.r = rnd::rand_int(0, sh.num_rows()-1);
sprite2->pos.c = rnd::rand_int(0, sh.num_cols()-1);
sprite2->layer_id = rnd::rand_select<int>({ 1, 3 });
sprite2->init(1, 1);
sprite2->init_frame(0);
sprite2->set_sprite_chars(0, '@');
sprite2->set_sprite_fg_colors(0, Color::DarkGray);
sprite2->set_sprite_bg_colors(0, Color::Transparent2);
// sprite, r_pos, r_vel.
asteroids[a_idx] =
{
sprite2,
static_cast<float>(sprite2->pos.r),
rnd::rand_float(0.8f, 2.f)
};
}

for (int s_idx = 0; s_idx < 16; ++s_idx)
{
auto* sprite3 = sprh.create_sprite("star" + std::to_string(s_idx));
sprite3->pos.r = rnd::rand_int(0, sh.num_rows()-1);
sprite3->pos.c = rnd::rand_int(0, sh.num_cols()-1);
sprite3->layer_id = 0;
sprite3->init(1, 1);
sprite3->init_frame(0);
char star_ch = rnd::rand_select<char>({ '.', '+' });
sprite3->set_sprite_chars(0, star_ch);
sprite3->set_sprite_fg_colors(0, rnd::rand_select<Color>({ Color::White, Color::White, Color::White, Color::White, Color::Yellow, Color::Yellow, Color::Red, Color::Blue, Color::Blue }));
sprite3->set_sprite_bg_colors(0, Color::Transparent2);
sprite3->init_frame(1);
sprite3->set_sprite_chars(1, star_ch);
sprite3->set_sprite_fg_colors(1, Color::Black);
sprite3->set_sprite_bg_colors(1, Color::Transparent2);
const int max_twinkle = 40;
int twinkle_offs = rnd::rand_int(0, max_twinkle);
sprite3->func_frame_to_texture = [twinkle_offs](int anim_frame)
{
return 1 - (anim_frame + twinkle_offs) % max_twinkle;
};
}

auto* sprite4 = sprh.create_sprite("background");
sprite4->layer_id = 2;
sprite4->init(sh.num_rows(), sh.num_cols());
sprite4->init_frame(0);
sprite4->set_sprite_chars_from_strings(0,
R"( )",
R"( )",
R"( )",
R"( )",
R"( )",
R"( )",
R"( )",
R"( )",
R"( )",
R"(::::::: )",
R"(~~~.~=.:::: )",
R"(ou.~~~~~,.~%%%% )",
R"(898o.~~~+~~~~o8%%% )",
R"(9883o..,~~~~ooO9Wm** )",
R"(8i8o.~~.~~~"%oOoO%8w** )",
R"(86o>+~~~~~%OooOOoww~<)oo )",
R"(8Oo+~+~~<%%OOo~~mm~~~/o\o )",
R"(3~~oo~~%%M%68OooO~mm~|(9\o )",
R"(3S0~o~~p%MM%99oOOo8ooo\6)o )",
R"(8%~o~~%WW%SSooOOOoooo~~~~~i )"
);
sprite4->set_sprite_fg_colors(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0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, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2
);

// ///////////////////////////////////////////////////////////
// LET's GO ! //
// ///////////////////////////////////////////////////////////

clear_screen();
return_cursor();
hide_cursor();

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

int anim_frame = (i + 3)*44 + (j + 5);
auto t = static_cast<float>(anim_frame)/(23.f*45.f);
sprite1->pos.c = math::roundI(20.f + 6.f*std::cos(math::c_2pi * 7 * t));
sprite1->pos.c = math::roundI(20.f + 6.f*std::cos(math::c_2pi * 7.13 * t));
sprite1->pos.r = math::roundI(10.f + 4*std::sin(math::c_2pi * 10 * t));

for (int a_idx = 0; a_idx < asteroids.size(); ++a_idx)
{
auto& [ast_sprite, r_pos, r_vel] = asteroids[a_idx];
r_pos += r_vel * dt;
if (r_pos >= static_cast<float>(sh.num_rows()))
r_pos = 0.f;
ast_sprite->pos.r = math::roundI(r_pos);
}

return_cursor();
sh.clear();
sprh.draw(sh, anim_frame);
Expand Down

0 comments on commit 1839c26

Please sign in to comment.