diff options
author | Yann Herklotz <ymherklotz@gmail.com> | 2017-12-31 18:00:01 +0000 |
---|---|---|
committer | Yann Herklotz <ymherklotz@gmail.com> | 2017-12-31 18:00:01 +0000 |
commit | 34908f108ad7c2ee6cff96491a0bc40381477424 (patch) | |
tree | f58c6bdde485d07a7136f78055240bab923bd4a6 /examples/shooter | |
parent | 943e3a5bc98ebcc2aa1b1d576700f7c4010c143c (diff) | |
download | YAGE-34908f108ad7c2ee6cff96491a0bc40381477424.tar.gz YAGE-34908f108ad7c2ee6cff96491a0bc40381477424.zip |
[Test] Benchmarking the engine and trying to optimize.
Diffstat (limited to 'examples/shooter')
-rw-r--r-- | examples/shooter/main.cpp | 108 | ||||
-rw-r--r-- | examples/shooter/player.cpp | 62 | ||||
-rw-r--r-- | examples/shooter/player.h | 37 |
3 files changed, 206 insertions, 1 deletions
diff --git a/examples/shooter/main.cpp b/examples/shooter/main.cpp index 12cf218d..d2727877 100644 --- a/examples/shooter/main.cpp +++ b/examples/shooter/main.cpp @@ -1,6 +1,6 @@ #include <yage/yage.h> -#include "glad/glad.h" +#include "player.h" using std::cout; @@ -12,9 +12,115 @@ int main(int argc, char **argv) yage::Shader shader("examples/resources/textureshader.vert", "examples/resources/textureshader.frag"); + std::vector<yage::Texture> male_l = { + yage::ResourceManager::getTexture("examples/resources/fighter_ml.png", + 3, 4), + yage::ResourceManager::getTexture("examples/resources/ranger_ml.png", 3, + 4), + yage::ResourceManager::getTexture("examples/resources/mage_ml.png", 3, + 4), + yage::ResourceManager::getTexture("examples/resources/healer_ml.png", 3, + 4)}; + std::vector<yage::Texture> female_l = { + yage::ResourceManager::getTexture("examples/resources/fighter_fl.png", + 3, 4), + yage::ResourceManager::getTexture("examples/resources/ranger_fl.png", 3, + 4), + yage::ResourceManager::getTexture("examples/resources/mage_fl.png", 3, + 4), + yage::ResourceManager::getTexture("examples/resources/healer_fl.png", 3, + 4)}; + std::vector<yage::Texture> male_d = { + yage::ResourceManager::getTexture("examples/resources/fighter_md.png", + 3, 4), + yage::ResourceManager::getTexture("examples/resources/ranger_md.png", 3, + 4), + yage::ResourceManager::getTexture("examples/resources/mage_md.png", 3, + 4), + yage::ResourceManager::getTexture("examples/resources/healer_md.png", 3, + 4)}; + std::vector<yage::Texture> female_d = { + yage::ResourceManager::getTexture("examples/resources/fighter_fd.png", + 3, 4), + yage::ResourceManager::getTexture("examples/resources/ranger_fd.png", 3, + 4), + yage::ResourceManager::getTexture("examples/resources/mage_fd.png", 3, + 4), + yage::ResourceManager::getTexture("examples/resources/healer_fd.png", 3, + 4)}; + + yage::SpriteBatch sp; + yage::Camera camera(800, 600); + + int i = 0; + int j = 0; + bool space_pressed = false; + bool c_pressed = false; + + shader.use(); + shader.setUniform("texture_sampler", 0); + + auto textures = male_l; + + Player player({400, 300, 48 * 2, 64 * 2}, textures.front()); + while (!window.shouldClose()) { window.pollEvents(); + + if (window.keyPressed(yage::key::D)) { + player.move(Direction::RIGHT); + } + if (window.keyPressed(yage::key::S)) { + player.move(Direction::DOWN); + } + if (window.keyPressed(yage::key::A)) { + player.move(Direction::LEFT); + } + if (window.keyPressed(yage::key::W)) { + player.move(Direction::UP); + } + if (!window.keyPressed(yage::key::D) && + !window.keyPressed(yage::key::S) && + !window.keyPressed(yage::key::A) && + !window.keyPressed(yage::key::W)) { + player.idle(); + } + if (window.keyPressed(yage::key::SPACE) && !space_pressed) { + space_pressed = true; + i = (i + 1) % textures.size(); + } + if (!window.keyPressed(yage::key::SPACE)) { + space_pressed = false; + } + if (window.keyPressed(yage::key::C) && !c_pressed) { + c_pressed = true; + j = (j + 1) % 4; + switch (j) { + case 0: + textures = male_l; + break; + case 1: + textures = male_d; + break; + case 2: + textures = female_l; + break; + case 3: + textures = female_d; + break; + } + } + if (!window.keyPressed(yage::key::C)) { + c_pressed = false; + } + + player.setTexture(textures[i]); + + camera.update(shader); + window.clearBuffer(); + player.draw(sp); + sp.render(); window.swapBuffer(); } diff --git a/examples/shooter/player.cpp b/examples/shooter/player.cpp new file mode 100644 index 00000000..2d0148e6 --- /dev/null +++ b/examples/shooter/player.cpp @@ -0,0 +1,62 @@ +#include "player.h" + +Player::Player(const glm::vec4 &bound, const yage::Texture &texture) + : bound_(bound), texture_(texture), direction_(Direction::DOWN), + action_(Action::IDLE), speed_(4) +{ +} + +void Player::setTexture(const yage::Texture &texture) { + texture_ = texture; +} + +void Player::draw(yage::SpriteBatch &sp) const +{ + static int time = 0; + static int iteration = 0; + float width = 1.f / static_cast<float>(texture_.x); + float height = 1.f / static_cast<float>(texture_.y); + + switch (action_) { + case Action::IDLE: + sp.draw(bound_, + {width, static_cast<int>(direction_) * height, width, height}, + texture_.id, yage::Colour(255, 255, 255, 255), 0); + break; + case Action::MOVING: + if(time % 15 == 0) { + iteration = (iteration + 1) % 2; + } + sp.draw(bound_, + {iteration * 2 * width, static_cast<int>(direction_) * height, width, height}, + texture_.id, yage::Colour(255, 255, 255, 255), 0); + time = (time + 1) % 59; + break; + } +} + +void Player::move(Direction direction) +{ + direction_ = direction; + action_ = Action::MOVING; + + switch (direction_) { + case Direction::LEFT: + bound_.x -= speed_; + break; + case Direction::DOWN: + bound_.y -= speed_; + break; + case Direction::RIGHT: + bound_.x += speed_; + break; + case Direction::UP: + bound_.y += speed_; + break; + } +} + +void Player::idle() +{ + action_ = Action::IDLE; +} diff --git a/examples/shooter/player.h b/examples/shooter/player.h new file mode 100644 index 00000000..72af7c34 --- /dev/null +++ b/examples/shooter/player.h @@ -0,0 +1,37 @@ +#ifndef EXAMPLE_SHOOTER_PLAYER_H +#define EXAMPLE_SHOOTER_PLAYER_H + +#include "yage/yage.h" + +enum class Direction { + LEFT, + DOWN, + RIGHT, + UP, +}; + +enum class Action { + IDLE, + MOVING, +}; + +class Player +{ +public: + Player(const glm::vec4 &bound, const yage::Texture &texture); + + void setTexture(const yage::Texture &texture); + + void draw(yage::SpriteBatch &sp) const; + + void move(Direction direction); + void idle(); +private: + glm::vec4 bound_; + yage::Texture texture_; + Direction direction_; + Action action_; + int speed_; +}; + +#endif |