This commit is contained in:
h4570
2022-07-30 17:00:22 +02:00
parent ff2967c7e5
commit 1bdb33e1a6
10 changed files with 195 additions and 67 deletions
@@ -15,7 +15,6 @@ namespace Demo {
enum IntroStateType { enum IntroStateType {
STATE_PS2DEV, STATE_PS2DEV,
STATE_TYRA, STATE_TYRA,
STATE_WAR_INFO,
STATE_PRESS_KEY, STATE_PRESS_KEY,
STATE_INTRO_END STATE_INTRO_END
}; };
@@ -18,13 +18,14 @@
using Tyra::Sprite; using Tyra::Sprite;
using Tyra::Texture; using Tyra::Texture;
using Tyra::Timer; using Tyra::Timer;
using Tyra::Vec2;
namespace Demo { namespace Demo {
class IntroWarInfoState : public State<IntroStateType> { class IntroPressKeyState : public State<IntroStateType> {
public: public:
IntroWarInfoState(Engine* t_engine); IntroPressKeyState(Engine* t_engine);
~IntroWarInfoState(); ~IntroPressKeyState();
const IntroStateType& getState() const { return state; } const IntroStateType& getState() const { return state; }
@@ -38,12 +39,20 @@ class IntroWarInfoState : public State<IntroStateType> {
IntroStateType onFinish(); IntroStateType onFinish();
private: private:
void updateMap();
IntroStateType state; IntroStateType state;
bool _wantFinish; bool _wantFinish;
bool initialized; bool initialized;
Texture* warInfoTexture; const static u8 mapRows = 3;
Sprite* warInfoSprite; const static u8 mapCols = 3;
Vec2 mapPosition;
u8 mapDirection;
Texture* mapTextures[mapRows][mapCols];
Sprite* mapSprites[mapRows][mapCols];
}; };
} // namespace Demo } // namespace Demo
+3 -3
View File
@@ -14,14 +14,14 @@
#include "states/intro/states/intro_ps2dev_state.hpp" #include "states/intro/states/intro_ps2dev_state.hpp"
#include "states/intro/states/intro_tyra_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; using Tyra::FileUtils;
namespace Demo { namespace Demo {
IntroState::IntroState(Engine* t_engine) IntroState::IntroState(Engine* t_engine)
: State(t_engine), stateManager(STATE_WAR_INFO) { : State(t_engine), stateManager(STATE_PRESS_KEY) {
state = STATE_INTRO; state = STATE_INTRO;
_wantFinish = false; _wantFinish = false;
} }
@@ -36,7 +36,7 @@ void IntroState::onStart() {
stateManager.add(new IntroPs2DevState(engine)); stateManager.add(new IntroPs2DevState(engine));
stateManager.add(new IntroTyraState(engine)); stateManager.add(new IntroTyraState(engine));
stateManager.add(new IntroWarInfoState(engine)); stateManager.add(new IntroPressKeyState(engine));
} }
void IntroState::update() { stateManager.update(); } void IntroState::update() { stateManager.update(); }
@@ -0,0 +1,99 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#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 <string>
#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
@@ -170,6 +170,6 @@ void IntroTyraState::renderFillers() {
engine->renderer.renderer2D.render(fillerSprite); engine->renderer.renderer2D.render(fillerSprite);
} }
IntroStateType IntroTyraState::onFinish() { return STATE_WAR_INFO; } IntroStateType IntroTyraState::onFinish() { return STATE_PRESS_KEY; }
} // namespace Demo } // namespace Demo
@@ -1,57 +0,0 @@
/*
# ______ ____ ___
# | \/ ____| |___|
# | | | \ | |
#-----------------------------------------------------------------------
# Copyright 2022, tyra - https://github.com/h4570/tyra
# Licenced under Apache License 2.0
# Sandro Sobczyński <sandro.sobczynski@gmail.com>
*/
#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
+3
View File
@@ -10,6 +10,8 @@
#pragma once #pragma once
#include <string>
namespace Tyra { namespace Tyra {
class FileUtils { class FileUtils {
@@ -18,6 +20,7 @@ class FileUtils {
~FileUtils(); ~FileUtils();
static std::string getCwd(); static std::string getCwd();
static std::string fromCwd(const std::string& relativePath);
static std::string fromCwd(const char* relativePath); static std::string fromCwd(const char* relativePath);
const char* getElfName() const { return elfName; }; const char* getElfName() const { return elfName; };
+22
View File
@@ -35,6 +35,28 @@ class Vec2 {
Vec2(); 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 float& t_x, const float& t_y);
void set(const Vec2& v); void set(const Vec2& v);
void rotate(const float& t_angle, const float& t_x, const float& t_y); void rotate(const float& t_angle, const float& t_x, const float& t_y);
+4
View File
@@ -56,6 +56,10 @@ std::string FileUtils::getCwd() {
return result; return result;
} }
std::string FileUtils::fromCwd(const std::string& relativePath) {
return fromCwd(relativePath.c_str());
}
std::string FileUtils::fromCwd(const char* file) { std::string FileUtils::fromCwd(const char* file) {
auto cwd = getCwd(); auto cwd = getCwd();
return cwd + file; return cwd + file;
+49
View File
@@ -54,6 +54,55 @@ void Vec2::rotate(const float& t_angle, const float& t_x, const float& t_y) {
y = ynew + 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 { void Vec2::print() const {
auto text = getPrint(nullptr); auto text = getPrint(nullptr);
printf("%s\n", text.c_str()); printf("%s\n", text.c_str());