From 7b95e3a9eacf296f215c73e5d8ad9090a24adb20 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Thu, 4 Jan 2018 21:36:30 +0000 Subject: [Engine] Now using stb_image to laod all kinds of textures. --- examples/shooter/bullet.cpp | 31 ++++++++++++++++++++++++++++--- examples/shooter/bullet.h | 11 +++++++++-- examples/shooter/direction.h | 11 +++++++++++ examples/shooter/main.cpp | 36 +++++++++++++++++++++++++++++------- examples/shooter/player.cpp | 11 ++++++++--- examples/shooter/player.h | 12 ++++-------- 6 files changed, 89 insertions(+), 23 deletions(-) create mode 100644 examples/shooter/direction.h (limited to 'examples/shooter') diff --git a/examples/shooter/bullet.cpp b/examples/shooter/bullet.cpp index 55ce3f6e..c589b4db 100644 --- a/examples/shooter/bullet.cpp +++ b/examples/shooter/bullet.cpp @@ -1,8 +1,33 @@ #include "bullet.h" -Bullet::Bullet(const glm::vec4 &bound) : bound_(bound) {} +Bullet::Bullet(const glm::vec4 &bound, Direction dir, float speed, float depth) + : bound_(bound), dir_(dir), speed_(speed), depth_(depth) +{ +} + +void Bullet::draw(yage::SpriteBatch &sp) +{ + switch(dir_) { + case Direction::UP: + bound_.y += speed_; + break; + case Direction::DOWN: + bound_.y -= speed_; + break; + case Direction::LEFT: + bound_.x -= speed_; + break; + case Direction::RIGHT: + bound_.x += speed_; + break; + } + sp.draw( + bound_, {0, 0, 1, 1}, + yage::ResourceManager::getTexture("examples/resources/bullet.png").id, + yage::Colour(255, 255, 255, 255), depth_); +} -void Bullet::draw(yage::SpriteBatch &sp) const +glm::vec4 Bullet::position() const { - sp.draw(bound_, {0, 0, 1, 1}, yage::ResourceManager::getTexture("examples/resources/bullet.png").id, yage::Colour(255, 255, 255, 255), 0); + return bound_; } diff --git a/examples/shooter/bullet.h b/examples/shooter/bullet.h index 1d372af1..19430fda 100644 --- a/examples/shooter/bullet.h +++ b/examples/shooter/bullet.h @@ -3,15 +3,22 @@ #include +#include "direction.h" + class Bullet : public yage::Drawable { public: - Bullet(const glm::vec4 &bound); + Bullet(const glm::vec4 &bound, Direction dir, float speed, float depth = 0.f); - void draw(yage::SpriteBatch &sp) const; + void draw(yage::SpriteBatch &sp); + // getters + glm::vec4 position() const; private: glm::vec4 bound_; + Direction dir_; + float speed_; + float depth_; }; #endif diff --git a/examples/shooter/direction.h b/examples/shooter/direction.h new file mode 100644 index 00000000..010c6b54 --- /dev/null +++ b/examples/shooter/direction.h @@ -0,0 +1,11 @@ +#ifndef EXAMPLES_SHOOTER_DIRECTION_H +#define EXAMPLES_SHOOTER_DIRECTION_H + +enum class Direction { + LEFT, + DOWN, + RIGHT, + UP, +}; + +#endif diff --git a/examples/shooter/main.cpp b/examples/shooter/main.cpp index c63c0cab..e38bf53b 100644 --- a/examples/shooter/main.cpp +++ b/examples/shooter/main.cpp @@ -1,10 +1,11 @@ #include #include "bullet.h" +#include "direction.h" #include "player.h" -#include #include +#include using std::cout; @@ -13,7 +14,7 @@ int main(int argc, char **argv) yage::Window window; window.create("Shooter example", 800, 600); - std::vector> objects; + std::vector> bullets; yage::Shader shader("examples/resources/textureshader.vert", "examples/resources/textureshader.frag"); @@ -69,7 +70,6 @@ int main(int argc, char **argv) auto textures = male_l; Player player({400, 300, 48 * 2, 64 * 2}, textures.front()); - objects.push_back(std::make_unique(glm::vec4(400, 300, 25, 25))); while (!window.shouldClose()) { window.pollEvents(); @@ -122,11 +122,33 @@ int main(int argc, char **argv) } if (window.keyPressed(yage::key::RIGHT)) { - objects.push_back(std::make_unique(glm::vec4(player.position().x, player.position().y, 25, 25))); + bullets.push_back(std::make_unique( + glm::vec4(player.position().x + player.position().z / 2.f, + player.position().y + player.position().w / 2.f, 25, + 25), + Direction::RIGHT, 10.f)); + player.look(Direction::RIGHT); } else if (window.keyPressed(yage::key::DOWN)) { - + bullets.push_back(std::make_unique( + glm::vec4(player.position().x + player.position().z / 2.f, + player.position().y + player.position().w / 2.f, 25, + 25), + Direction::DOWN, 10.f, 2)); + player.look(Direction::DOWN); } else if (window.keyPressed(yage::key::LEFT)) { + bullets.push_back(std::make_unique( + glm::vec4(player.position().x + player.position().z / 2.f, + player.position().y + player.position().w / 2.f, 25, + 25), + Direction::LEFT, 10.f, 2)); + player.look(Direction::LEFT); } else if (window.keyPressed(yage::key::UP)) { + bullets.push_back(std::make_unique( + glm::vec4(player.position().x + player.position().z / 2.f, + player.position().y + player.position().w / 2.f, 25, + 25), + Direction::UP, 10.f)); + player.look(Direction::UP); } player.setTexture(textures[i]); @@ -136,8 +158,8 @@ int main(int argc, char **argv) window.clearBuffer(); player.draw(sp); - for(auto &&object : objects) { - object->draw(sp); + for (auto &&bullet : bullets) { + bullet->draw(sp); } sp.render(); diff --git a/examples/shooter/player.cpp b/examples/shooter/player.cpp index b2b85cfd..dab743a9 100644 --- a/examples/shooter/player.cpp +++ b/examples/shooter/player.cpp @@ -10,7 +10,7 @@ void Player::setTexture(const yage::Texture &texture) { texture_ = texture; } -void Player::draw(yage::SpriteBatch &sp) const +void Player::draw(yage::SpriteBatch &sp) { static int time = 0; static int iteration = 0; @@ -21,7 +21,7 @@ void Player::draw(yage::SpriteBatch &sp) const case Action::IDLE: sp.draw(bound_, {width, static_cast(direction_) * height, width, height}, - texture_.id, yage::Colour(255, 255, 255, 255), 0); + texture_.id, yage::Colour(255, 255, 255, 255), 1); break; case Action::MOVING: if(time % 15 == 0) { @@ -29,7 +29,7 @@ void Player::draw(yage::SpriteBatch &sp) const } sp.draw(bound_, {iteration * 2 * width, static_cast(direction_) * height, width, height}, - texture_.id, yage::Colour(255, 255, 255, 255), 0); + texture_.id, yage::Colour(255, 255, 255, 255), 1); time = (time + 1) % 59; break; } @@ -61,6 +61,11 @@ void Player::idle() action_ = Action::IDLE; } +void Player::look(Direction direction) +{ + direction_ = direction; +} + glm::vec4 Player::position() const { return bound_; diff --git a/examples/shooter/player.h b/examples/shooter/player.h index b8042e71..8b5121c5 100644 --- a/examples/shooter/player.h +++ b/examples/shooter/player.h @@ -1,14 +1,9 @@ #ifndef EXAMPLE_SHOOTER_PLAYER_H #define EXAMPLE_SHOOTER_PLAYER_H -#include "yage/yage.h" +#include -enum class Direction { - LEFT, - DOWN, - RIGHT, - UP, -}; +#include "direction.h" enum class Action { IDLE, @@ -22,10 +17,11 @@ public: void setTexture(const yage::Texture &texture); - void draw(yage::SpriteBatch &sp) const; + void draw(yage::SpriteBatch &sp); void move(Direction direction); void idle(); + void look(Direction direction); // simple getters glm::vec4 position() const; -- cgit