Skip to content
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

Refactor assets. #14

Merged
merged 12 commits into from
Jan 23, 2024
5 changes: 3 additions & 2 deletions example/assets/animations/explode_anim.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"duration": 0.25,
"asset_type": "AnimTimeline",
"duration": 0.250000,
"tiles": [
"0",
"1",
Expand All @@ -11,4 +12,4 @@
"7",
"8"
]
}
}
241 changes: 122 additions & 119 deletions example/assets/images/explode_atlas.json
Original file line number Diff line number Diff line change
@@ -1,122 +1,125 @@
{
"asset_type": "TextureAtlas",
"image": "images/explode_512x512.png",
"blocks": [
{
"id": "0",
"rect": {
"lt": [
0,
0
],
"rb": [
170,
170
]
"tile_sheet": {
"tiles": [
{
"id": "0",
"image_rect": {
"lt": [
0,
0
],
"rb": [
170,
170
]
}
},
{
"id": "1",
"image_rect": {
"lt": [
170,
0
],
"rb": [
340,
170
]
}
},
{
"id": "2",
"image_rect": {
"lt": [
340,
0
],
"rb": [
510,
170
]
}
},
{
"id": "3",
"image_rect": {
"lt": [
0,
170
],
"rb": [
170,
340
]
}
},
{
"id": "4",
"image_rect": {
"lt": [
170,
170
],
"rb": [
340,
340
]
}
},
{
"id": "5",
"image_rect": {
"lt": [
340,
170
],
"rb": [
510,
340
]
}
},
{
"id": "6",
"image_rect": {
"lt": [
0,
340
],
"rb": [
170,
510
]
}
},
{
"id": "7",
"image_rect": {
"lt": [
170,
340
],
"rb": [
340,
510
]
}
},
{
"id": "8",
"image_rect": {
"lt": [
340,
340
],
"rb": [
510,
510
]
}
}
},
{
"id": "1",
"rect": {
"lt": [
170,
0
],
"rb": [
340,
170
]
}
},
{
"id": "2",
"rect": {
"lt": [
340,
0
],
"rb": [
510,
170
]
}
},
{
"id": "3",
"rect": {
"lt": [
0,
170
],
"rb": [
170,
340
]
}
},
{
"id": "4",
"rect": {
"lt": [
170,
170
],
"rb": [
340,
340
]
}
},
{
"id": "5",
"rect": {
"lt": [
340,
170
],
"rb": [
510,
340
]
}
},
{
"id": "6",
"rect": {
"lt": [
0,
340
],
"rb": [
170,
510
]
}
},
{
"id": "7",
"rect": {
"lt": [
170,
340
],
"rb": [
340,
510
]
}
},
{
"id": "8",
"rect": {
"lt": [
340,
340
],
"rb": [
510,
510
]
}
}
]
}
]
}
}
3 changes: 2 additions & 1 deletion example/assets/images/pipe_128x128.9slice.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"asset_type": "Texture9Slice",
"image": "images/pipe_128x128.png",
"nine_slice": {
"n_left_top": [
Expand All @@ -10,4 +11,4 @@
1.000000
]
}
}
}
5 changes: 3 additions & 2 deletions example/flappy/src/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
#include <bave/core/time.hpp>
#include <bave/font/font.hpp>
#include <bave/font/text_height.hpp>
#include <bave/graphics/anim_timeline.hpp>
#include <bave/graphics/rgba.hpp>
#include <bave/graphics/sprite_anim.hpp>
#include <bave/graphics/texture_9slice.hpp>
#include <bave/graphics/texture_atlas.hpp>
#include <glm/vec2.hpp>
#include <memory>
Expand All @@ -21,7 +22,7 @@ struct Config {
std::shared_ptr<bave::AudioClip> jump_sfx{};

std::shared_ptr<bave::TextureAtlas> explode_atlas{};
std::optional<bave::SpriteAnim::Animation> explode_animation{};
std::shared_ptr<bave::AnimTimeline> explode_timeline{};
std::shared_ptr<bave::AudioClip> explode_sfx{};

bave::Rgba background_rgba_top{bave::Rgba::from(0x1c96c5ff)};
Expand Down
14 changes: 2 additions & 12 deletions example/flappy/src/flappy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

using bave::Action;
using bave::App;
using bave::Driver;
using bave::FocusChange;
using bave::Key;
using bave::KeyInput;
Expand All @@ -30,8 +29,6 @@ Flappy::Flappy(App& app) : Driver(app), m_game_view(app.get_render_device().rend
create_entities();
// create and setup the HUD (mainly text here).
setup_hud();

m_im_texture = bave::ImTexture{m_config.cloud_texture};
}

void Flappy::tick() {
Expand Down Expand Up @@ -84,12 +81,6 @@ void Flappy::tick() {
if (ImGui::Button(pause_text)) { m_paused = !m_paused; }

ImGui::Checkbox("force lag", &m_force_lag);

if (m_im_texture->get_id() != vk::DescriptorSet{}) {
ImGui::Separator();
glm::vec2 const size = m_im_texture->get_texture()->get_size();
ImGui::Image(m_im_texture->get_id(), {size.x, size.y});
}
}
ImGui::End();
}
Expand Down Expand Up @@ -181,7 +172,7 @@ void Flappy::load_assets() {
m_config.jump_sfx = loader.load_audio_clip("audio_clips/beep.wav");

m_config.explode_atlas = loader.load_texture_atlas("images/explode_atlas.json");
m_config.explode_animation = loader.load_sprite_animation("animations/explode_anim.json");
m_config.explode_timeline = loader.load_anim_timeline("animations/explode_anim.json");
m_config.explode_sfx = loader.load_audio_clip("audio_clips/explode.wav");

m_config.cloud_texture = loader.load_texture("images/cloud_256x128.png");
Expand All @@ -193,8 +184,7 @@ void Flappy::load_assets() {

void Flappy::create_entities() {
// explode animation.
m_explode = SpriteAnim{m_config.explode_atlas};
if (m_config.explode_animation) { m_explode->animation = *m_config.explode_animation; }
m_explode = SpriteAnim{m_config.explode_atlas, m_config.explode_timeline};
m_explode->repeat = false;

// player sprite.
Expand Down
4 changes: 1 addition & 3 deletions example/flappy/src/flappy.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once
#include <bave/driver.hpp>
#include <bave/graphics/sprite.hpp>
#include <bave/graphics/sprite_anim.hpp>
#include <bave/graphics/text.hpp>
#include <bave/imgui/im_texture.hpp>
#include <src/background.hpp>
#include <src/pipes.hpp>
#include <src/player.hpp>
Expand Down Expand Up @@ -66,8 +66,6 @@ class Flappy : public bave::Driver {

bool m_force_lag{};

std::optional<bave::ImTexture> m_im_texture{};

public:
// constructor needs to be public (or at least accessible by the game factory that's setup in the main target)
explicit Flappy(bave::App& app);
Expand Down
29 changes: 29 additions & 0 deletions lib/include/bave/asset_type.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once
#include <concepts>
#include <string_view>

namespace bave {
struct AnimTimeline;
class Texture9Slice;
class TextureAtlas;

namespace detail {
template <typename...>
constexpr auto false_v = false;
}

template <typename Type>
constexpr auto get_asset_type() -> std::string_view {
if constexpr (std::same_as<Type, AnimTimeline>) {
return "AnimTimeline";
} else if constexpr (std::same_as<Type, Texture9Slice>) {
return "Texture9Slice";
} else if constexpr (std::same_as<Type, TextureAtlas>) {
return "TextureAtlas";
}

else {
static_assert(detail::false_v<Type>, "unsupported asset type");
}
}
} // namespace bave
Loading