From 1bdb33e1a61641fc27715df5e3422bd21067223a Mon Sep 17 00:00:00 2001 From: h4570 Date: Sat, 30 Jul 2022 17:00:22 +0200 Subject: [PATCH] demo map --- demo/inc/states/intro/intro_state_type.hpp | 1 - ...fo_state.hpp => intro_press_key_state.hpp} | 19 +++- demo/src/states/intro/intro_state.cpp | 6 +- .../intro/states/intro_press_key_state.cpp | 99 +++++++++++++++++++ .../states/intro/states/intro_tyra_state.cpp | 2 +- .../intro/states/intro_war_info_state.cpp | 57 ----------- engine/inc/file/file_utils.hpp | 3 + engine/inc/math/vec2.hpp | 22 +++++ engine/src/file/file_utils.cpp | 4 + engine/src/math/vec2.cpp | 49 +++++++++ 10 files changed, 195 insertions(+), 67 deletions(-) rename demo/inc/states/intro/states/{intro_war_info_state.hpp => intro_press_key_state.hpp} (67%) create mode 100644 demo/src/states/intro/states/intro_press_key_state.cpp delete mode 100644 demo/src/states/intro/states/intro_war_info_state.cpp diff --git a/demo/inc/states/intro/intro_state_type.hpp b/demo/inc/states/intro/intro_state_type.hpp index 8ad2374..d61d958 100644 --- a/demo/inc/states/intro/intro_state_type.hpp +++ b/demo/inc/states/intro/intro_state_type.hpp @@ -15,7 +15,6 @@ namespace Demo { enum IntroStateType { STATE_PS2DEV, STATE_TYRA, - STATE_WAR_INFO, STATE_PRESS_KEY, STATE_INTRO_END }; diff --git a/demo/inc/states/intro/states/intro_war_info_state.hpp b/demo/inc/states/intro/states/intro_press_key_state.hpp similarity index 67% rename from demo/inc/states/intro/states/intro_war_info_state.hpp rename to demo/inc/states/intro/states/intro_press_key_state.hpp index a2b7284..d6acbc1 100644 --- a/demo/inc/states/intro/states/intro_war_info_state.hpp +++ b/demo/inc/states/intro/states/intro_press_key_state.hpp @@ -18,13 +18,14 @@ using Tyra::Sprite; using Tyra::Texture; using Tyra::Timer; +using Tyra::Vec2; namespace Demo { -class IntroWarInfoState : public State { +class IntroPressKeyState : public State { public: - IntroWarInfoState(Engine* t_engine); - ~IntroWarInfoState(); + IntroPressKeyState(Engine* t_engine); + ~IntroPressKeyState(); const IntroStateType& getState() const { return state; } @@ -38,12 +39,20 @@ class IntroWarInfoState : public State { IntroStateType onFinish(); private: + void updateMap(); + IntroStateType state; bool _wantFinish; bool initialized; - Texture* warInfoTexture; - Sprite* warInfoSprite; + const static u8 mapRows = 3; + const static u8 mapCols = 3; + + Vec2 mapPosition; + u8 mapDirection; + + Texture* mapTextures[mapRows][mapCols]; + Sprite* mapSprites[mapRows][mapCols]; }; } // namespace Demo diff --git a/demo/src/states/intro/intro_state.cpp b/demo/src/states/intro/intro_state.cpp index 000a82b..31d04d2 100644 --- a/demo/src/states/intro/intro_state.cpp +++ b/demo/src/states/intro/intro_state.cpp @@ -14,14 +14,14 @@ #include "states/intro/states/intro_ps2dev_state.hpp" #include "states/intro/states/intro_tyra_state.hpp" -#include "states/intro/states/intro_war_info_state.hpp" +#include "states/intro/states/intro_press_key_state.hpp" using Tyra::FileUtils; namespace Demo { IntroState::IntroState(Engine* t_engine) - : State(t_engine), stateManager(STATE_WAR_INFO) { + : State(t_engine), stateManager(STATE_PRESS_KEY) { state = STATE_INTRO; _wantFinish = false; } @@ -36,7 +36,7 @@ void IntroState::onStart() { stateManager.add(new IntroPs2DevState(engine)); stateManager.add(new IntroTyraState(engine)); - stateManager.add(new IntroWarInfoState(engine)); + stateManager.add(new IntroPressKeyState(engine)); } void IntroState::update() { stateManager.update(); } diff --git a/demo/src/states/intro/states/intro_press_key_state.cpp b/demo/src/states/intro/states/intro_press_key_state.cpp new file mode 100644 index 0000000..0170956 --- /dev/null +++ b/demo/src/states/intro/states/intro_press_key_state.cpp @@ -0,0 +1,99 @@ +/* +# ______ ____ ___ +# | \/ ____| |___| +# | | | \ | | +#----------------------------------------------------------------------- +# Copyright 2022, tyra - https://github.com/h4570/tyra +# Licenced under Apache License 2.0 +# Sandro Sobczyński +*/ + +#include "states/intro/states/intro_press_key_state.hpp" +#include "loaders/texture/png_loader.hpp" +#include "file/file_utils.hpp" +#include "debug/debug.hpp" +#include +#include "thread/threading.hpp" +#include "math/math.hpp" + +using Tyra::FileUtils; +using Tyra::Math; +using Tyra::PngLoader; +using Tyra::Threading; + +namespace Demo { + +IntroPressKeyState::IntroPressKeyState(Engine* t_engine) + : State(t_engine), mapPosition(0.0F, 0.0F) { + state = STATE_PRESS_KEY; + mapDirection = 0; + _wantFinish = false; +} + +IntroPressKeyState::~IntroPressKeyState() { + if (!initialized) return; + + for (u8 i = 0; i < mapRows; i++) + for (u8 j = 0; j < mapCols; j++) { + engine->renderer.core.texture.repository.free(mapTextures[i][j]->getId()); + delete mapSprites[i][j]; + } +} + +void IntroPressKeyState::onStart() { + for (u8 i = 0; i < mapRows; i++) { + for (u8 j = 0; j < mapCols; j++) { + mapSprites[i][j] = new Sprite; + mapSprites[i][j]->size.set(512.0F, 512.0F); + mapSprites[i][j]->position.set(i * 512.0F, j * 512.0F); + + std::string filename = + "map-" + std::to_string(i) + "-" + std::to_string(j); + std::string path = "intro/map/" + filename + ".png"; + + mapTextures[i][j] = engine->renderer.core.texture.repository.add( + FileUtils::fromCwd(path)); + mapTextures[i][j]->addLink(mapSprites[i][j]->getId()); + } + } + + initialized = true; +} + +void IntroPressKeyState::update() { + engine->renderer.beginFrame(); + + if (mapDirection == 0 && mapPosition.x >= 512.0F * (mapRows - 1) && + mapPosition.y >= 512.0F * (mapCols - 1)) { + mapDirection = 1; + } else if (mapDirection == 1 && mapPosition.x <= 0.0F && + mapPosition.y <= 0.0F) { + mapDirection = 0; + } + + if (mapDirection == 0) { + mapPosition += .5F; + } else if (mapDirection == 1) { + mapPosition -= .5F; + } + + updateMap(); + Threading::switchThread(); + + for (u8 i = 0; i < mapRows; i++) + for (u8 j = 0; j < mapCols; j++) + engine->renderer.renderer2D.render(mapSprites[i][j]); + + engine->renderer.endFrame(); +} + +IntroStateType IntroPressKeyState::onFinish() { return STATE_INTRO_END; } + +void IntroPressKeyState::updateMap() { + for (u8 i = 0; i < mapRows; i++) + for (u8 j = 0; j < mapCols; j++) { + mapSprites[i][j]->position.x = i * 512.0F - mapPosition.x; + mapSprites[i][j]->position.y = j * 512.0F - mapPosition.y; + } +} +} // namespace Demo diff --git a/demo/src/states/intro/states/intro_tyra_state.cpp b/demo/src/states/intro/states/intro_tyra_state.cpp index c767299..f2650d8 100644 --- a/demo/src/states/intro/states/intro_tyra_state.cpp +++ b/demo/src/states/intro/states/intro_tyra_state.cpp @@ -170,6 +170,6 @@ void IntroTyraState::renderFillers() { engine->renderer.renderer2D.render(fillerSprite); } -IntroStateType IntroTyraState::onFinish() { return STATE_WAR_INFO; } +IntroStateType IntroTyraState::onFinish() { return STATE_PRESS_KEY; } } // namespace Demo diff --git a/demo/src/states/intro/states/intro_war_info_state.cpp b/demo/src/states/intro/states/intro_war_info_state.cpp deleted file mode 100644 index 30b4973..0000000 --- a/demo/src/states/intro/states/intro_war_info_state.cpp +++ /dev/null @@ -1,57 +0,0 @@ -/* -# ______ ____ ___ -# | \/ ____| |___| -# | | | \ | | -#----------------------------------------------------------------------- -# Copyright 2022, tyra - https://github.com/h4570/tyra -# Licenced under Apache License 2.0 -# Sandro Sobczyński -*/ - -#include "states/intro/states/intro_war_info_state.hpp" -#include "loaders/texture/png_loader.hpp" -#include "file/file_utils.hpp" -#include "debug/debug.hpp" -#include "thread/threading.hpp" -#include "math/math.hpp" - -using Tyra::FileUtils; -using Tyra::Math; -using Tyra::PngLoader; -using Tyra::Threading; - -namespace Demo { - -IntroWarInfoState::IntroWarInfoState(Engine* t_engine) : State(t_engine) { - state = STATE_WAR_INFO; - _wantFinish = false; -} - -IntroWarInfoState::~IntroWarInfoState() { - if (!initialized) return; -} - -void IntroWarInfoState::onStart() { - warInfoSprite = new Sprite; - warInfoSprite->size.set(512.0F, 512.0F); - warInfoSprite->position.set(0.0F, 0.0F); - - warInfoTexture = engine->renderer.core.texture.repository.add( - FileUtils::fromCwd("intro/war_info.png")); - warInfoTexture->addLink(warInfoSprite->getId()); - - initialized = true; -} - -void IntroWarInfoState::update() { - engine->renderer.beginFrame(); - - Threading::switchThread(); - - engine->renderer.renderer2D.render(warInfoSprite); - - engine->renderer.endFrame(); -} -IntroStateType IntroWarInfoState::onFinish() { return STATE_PRESS_KEY; } - -} // namespace Demo diff --git a/engine/inc/file/file_utils.hpp b/engine/inc/file/file_utils.hpp index 704cdb0..16b0c0f 100644 --- a/engine/inc/file/file_utils.hpp +++ b/engine/inc/file/file_utils.hpp @@ -10,6 +10,8 @@ #pragma once +#include + namespace Tyra { class FileUtils { @@ -18,6 +20,7 @@ class FileUtils { ~FileUtils(); static std::string getCwd(); + static std::string fromCwd(const std::string& relativePath); static std::string fromCwd(const char* relativePath); const char* getElfName() const { return elfName; }; diff --git a/engine/inc/math/vec2.hpp b/engine/inc/math/vec2.hpp index bace816..a28d10e 100644 --- a/engine/inc/math/vec2.hpp +++ b/engine/inc/math/vec2.hpp @@ -35,6 +35,28 @@ class Vec2 { Vec2(); ~Vec2(); + void operator=(const Vec2& v); + + Vec2 operator+(const Vec2& v) const; + Vec2 operator-(const Vec2& v) const; + Vec2 operator*(const Vec2& v) const; + Vec2 operator/(const Vec2& v) const; + + Vec2 operator+(const float& v) const; + Vec2 operator-(const float& v) const; + Vec2 operator*(const float& v) const; + Vec2 operator/(const float& v) const; + + void operator+=(const Vec2& v); + void operator-=(const Vec2& v); + void operator*=(const Vec2& v); + void operator/=(const Vec2& v); + + void operator+=(const float& v); + void operator-=(const float& v); + void operator*=(const float& v); + void operator/=(const float& v); + void set(const float& t_x, const float& t_y); void set(const Vec2& v); void rotate(const float& t_angle, const float& t_x, const float& t_y); diff --git a/engine/src/file/file_utils.cpp b/engine/src/file/file_utils.cpp index b1ce44c..14eba20 100644 --- a/engine/src/file/file_utils.cpp +++ b/engine/src/file/file_utils.cpp @@ -56,6 +56,10 @@ std::string FileUtils::getCwd() { return result; } +std::string FileUtils::fromCwd(const std::string& relativePath) { + return fromCwd(relativePath.c_str()); +} + std::string FileUtils::fromCwd(const char* file) { auto cwd = getCwd(); return cwd + file; diff --git a/engine/src/math/vec2.cpp b/engine/src/math/vec2.cpp index 808180d..0125d3a 100644 --- a/engine/src/math/vec2.cpp +++ b/engine/src/math/vec2.cpp @@ -54,6 +54,55 @@ void Vec2::rotate(const float& t_angle, const float& t_x, const float& t_y) { y = ynew + t_y; } +void Vec2::operator=(const Vec2& v) { + x = v.x; + y = v.y; +} + +Vec2 Vec2::operator+(const Vec2& v) const { return Vec2(x + v.x, y + v.y); } +Vec2 Vec2::operator-(const Vec2& v) const { return Vec2(x - v.x, y - v.y); } +Vec2 Vec2::operator*(const Vec2& v) const { return Vec2(x * v.x, y * v.y); } +Vec2 Vec2::operator/(const Vec2& v) const { return Vec2(x / v.x, y / v.y); } + +Vec2 Vec2::operator+(const float& v) const { return Vec2(x + v, y + v); } +Vec2 Vec2::operator-(const float& v) const { return Vec2(x - v, y - v); } +Vec2 Vec2::operator*(const float& v) const { return Vec2(x * v, y * v); } +Vec2 Vec2::operator/(const float& v) const { return Vec2(x / v, y / v); } + +void Vec2::operator+=(const Vec2& v) { + x += v.x; + y += v.y; +} +void Vec2::operator-=(const Vec2& v) { + x -= v.x; + y -= v.y; +} +void Vec2::operator*=(const Vec2& v) { + x *= v.x; + y *= v.y; +} +void Vec2::operator/=(const Vec2& v) { + x /= v.x; + y /= v.y; +} + +void Vec2::operator+=(const float& v) { + x += v; + y += v; +} +void Vec2::operator-=(const float& v) { + x -= v; + y -= v; +} +void Vec2::operator*=(const float& v) { + x *= v; + y *= v; +} +void Vec2::operator/=(const float& v) { + x /= v; + y /= v; +} + void Vec2::print() const { auto text = getPrint(nullptr); printf("%s\n", text.c_str());