Skip to content

Update le2d to v0.2.0 #13

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified ext/src.zip
Binary file not shown.
6 changes: 5 additions & 1 deletion src/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ constexpr auto context_ci = le::Context::CreateInfo{
};
} // namespace

App::App() : m_context(context_ci), m_data_loader(le::FileDataLoader::upfind("assets")) { bind_services(); }
App::App() : m_context(context_ci), m_data_loader(le::FileDataLoader::upfind("assets")), m_asset_loader(m_context.create_asset_loader(&m_data_loader)) {
bind_services();
}

void App::run() {
auto game = Game{&m_services};
Expand All @@ -40,5 +42,7 @@ void App::bind_services() {
// m_data_loader is bound to both the interface and the concrete class for use through either type.
m_services.bind<le::IDataLoader>(&m_data_loader);
m_services.bind<le::FileDataLoader>(&m_data_loader);

m_services.bind(&m_asset_loader);
}
} // namespace miracle
1 change: 1 addition & 0 deletions src/app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class App {

le::Context m_context;
le::FileDataLoader m_data_loader{};
le::AssetLoader m_asset_loader{};

le::ServiceLocator m_services{};
};
Expand Down
7 changes: 3 additions & 4 deletions src/enemy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
#include <le2d/renderer.hpp>
#include <le2d/service_locator.hpp>
#include <cstddef>
#include <optional>
#include "glm/vec2.hpp"
#include "le2d/texture.hpp"
#include <memory>
#include "le2d/resource/texture.hpp"

namespace miracle {
class Enemy {
Expand All @@ -35,7 +34,7 @@ class Enemy {

private:
gsl::not_null<le::ServiceLocator const*> m_services;
std::optional<le::Texture> m_texture;
std::unique_ptr<le::ITexture> m_texture;
le::drawable::Circle m_sprite{};
glm::vec2 m_target_pos{};
float m_move_speed{};
Expand Down
14 changes: 5 additions & 9 deletions src/game.cpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
#include <game.hpp>
#include <glm/gtx/norm.hpp>
#include <le2d/context.hpp>
#include <algorithm>
#include <cstddef>
#include <format>
#include <iterator>
#include <string>
#include <vector>
#include "enemy.hpp"
#include "kvf/time.hpp"
#include "le2d/asset_loader.hpp"
#include "le2d/data_loader.hpp"
#include "le2d/drawable/text.hpp"
#include "lighthouse.hpp"
#include "util/random.hpp"

namespace miracle {
Game::Game(gsl::not_null<le::ServiceLocator const*> services) : m_services(services), m_lighthouse(services), m_light(services) {
spawn_wave();
auto const& data_loader = services->get<le::IDataLoader>();
auto const& context = services->get<le::Context>();
auto const asset_loader = le::AssetLoader{&data_loader, &context};
m_font = asset_loader.load_font("fonts/specialElite.ttf");
auto const& asset_loader = services->get<le::AssetLoader>();
m_font = asset_loader.load<le::IFont>("fonts/specialElite.ttf");
if (!m_font) { throw std::runtime_error{"Failed to load font"}; }
}

void Game::on_cursor_pos(le::event::CursorPos const& cursor_pos) {
Expand Down Expand Up @@ -79,7 +75,7 @@ void Game::update_score(int points) {
m_score += points;
m_score_str.clear();
std::format_to(std::back_inserter(m_score_str), "Score: {}", m_score);
m_score_text.set_string(m_font, m_score_str);
m_score_text.set_string(*m_font, m_score_str);
}

void Game::update_health_text() {
Expand All @@ -95,7 +91,7 @@ void Game::update_health_text() {
std::format_to(std::back_inserter(m_health_str), "Health: {:.1f}", m_lighthouse.get_health());
}

m_health_text.set_string(m_font, m_health_str);
m_health_text.set_string(*m_font, m_health_str);
}

} // namespace miracle
6 changes: 3 additions & 3 deletions src/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
#include <le2d/event.hpp>
#include <le2d/renderer.hpp>
#include <le2d/service_locator.hpp>
#include <cstddef>
#include <memory>
#include "enemy.hpp"
#include "le2d/drawable/text.hpp"
#include "le2d/font.hpp"
#include "le2d/resource/font.hpp"
#include "light.hpp"
#include "lighthouse.hpp"

Expand All @@ -29,7 +29,7 @@ class Game {
Lighthouse m_lighthouse;
Light m_light;

le::Font m_font{};
std::unique_ptr<le::IFont> m_font{};
le::drawable::Text m_score_text{};
le::drawable::Text m_health_text{};
int m_score{};
Expand Down
5 changes: 2 additions & 3 deletions src/light.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#pragma once

#include "enemy.hpp"
#include "glm/vec2.hpp"
#include "gsl/pointers"
#include "le2d/drawable/shape.hpp"
#include "le2d/renderer.hpp"
#include "le2d/resource/texture.hpp"
#include "le2d/service_locator.hpp"
#include "le2d/texture.hpp"

namespace miracle {
class Light {
Expand All @@ -19,7 +18,7 @@ class Light {

private:
gsl::not_null<le::ServiceLocator const*> m_services;
std::optional<le::Texture> m_texture;
std::unique_ptr<le::ITexture> m_texture;
le::drawable::Circle m_sprite{};
float m_diameter{};
};
Expand Down
10 changes: 3 additions & 7 deletions src/lighthouse.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
#include <glm/gtc/matrix_transform.hpp>
#include <lighthouse.hpp>
#include "glm/gtx/norm.hpp"
#include "le2d/asset_loader.hpp"
#include "le2d/data_loader.hpp"

namespace miracle {
Lighthouse::Lighthouse(gsl::not_null<le::ServiceLocator const*> services) : m_services(services) {
m_sprite.create(m_hitbox_diameter);
auto const& data_loader = services->get<le::IDataLoader>();
auto const& context = services->get<le::Context>();
auto const asset_loader = le::AssetLoader{&data_loader, &context};
m_texture = asset_loader.load_texture("images/lighthouse.png");
m_sprite.texture = &m_texture.value();
auto const& asset_loader = services->get<le::AssetLoader>();
m_texture = asset_loader.load<le::ITexture>("images/lighthouse.png");
m_sprite.texture = m_texture.get();
}

void Lighthouse::rotate_towards_cursor(glm::vec2 cursor_pos) {
Expand Down
7 changes: 3 additions & 4 deletions src/lighthouse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
#include <le2d/event.hpp>
#include <le2d/renderer.hpp>
#include <le2d/service_locator.hpp>
#include <cstddef>
#include <optional>
#include <memory>
#include "enemy.hpp"
#include "le2d/texture.hpp"
#include "le2d/resource/texture.hpp"

namespace miracle {
class Lighthouse {
Expand All @@ -27,7 +26,7 @@ class Lighthouse {
gsl::not_null<le::ServiceLocator const*> m_services;
float m_hitbox_diameter{150.0f};
float m_visibility_diameter{250.0f};
std::optional<le::Texture> m_texture;
std::unique_ptr<le::ITexture> m_texture;
le::drawable::Circle m_sprite{};
float m_health{100};
};
Expand Down