diff options
author | Yann Herklotz <ymherklotz@gmail.com> | 2018-01-06 11:30:24 +0000 |
---|---|---|
committer | Yann Herklotz <ymherklotz@gmail.com> | 2018-01-06 11:30:24 +0000 |
commit | c7090180503f263c60ec34844992e0e8d4bea85a (patch) | |
tree | 6ecc5b2e16856db49de056738b36e1ba103d3049 /examples | |
parent | cf4c73f2a75b470a4d4c4167105f92bc46f1926c (diff) | |
parent | 07012cf0982d3f86aebe83b5bdc4a67332c635da (diff) | |
download | YAGE-c7090180503f263c60ec34844992e0e8d4bea85a.tar.gz YAGE-c7090180503f263c60ec34844992e0e8d4bea85a.zip |
Merge branch 'develop'
Diffstat (limited to 'examples')
37 files changed, 506 insertions, 50 deletions
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 16b20795..156b6a34 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,2 +1,10 @@ -add_executable(simplegame simplegame/main.cpp) -target_link_libraries(simplegame yage) +function(make_example name) + + file(GLOB SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${name}/*.cpp) + add_executable(${name} ${SOURCES}) + target_link_libraries(${name} yage) + +endfunction(make_example) + +make_example(simplegame) +make_example(shooter) diff --git a/examples/resources/breast_black.png b/examples/resources/breast_black.png Binary files differnew file mode 100644 index 00000000..180b292f --- /dev/null +++ b/examples/resources/breast_black.png diff --git a/examples/resources/bullet.png b/examples/resources/bullet.png Binary files differnew file mode 100644 index 00000000..89e6289b --- /dev/null +++ b/examples/resources/bullet.png diff --git a/examples/resources/colourshader.frag b/examples/resources/colourshader.frag new file mode 100644 index 00000000..e49d1ebb --- /dev/null +++ b/examples/resources/colourshader.frag @@ -0,0 +1,10 @@ +#version 450 + +layout(location = 0) in vec4 fragment_colour; + +layout(location = 0) out vec4 colour; + +void main() +{ + colour = fragment_colour; +} diff --git a/examples/resources/colourshader.vert b/examples/resources/colourshader.vert new file mode 100644 index 00000000..e5c10d77 --- /dev/null +++ b/examples/resources/colourshader.vert @@ -0,0 +1,15 @@ +#version 450 + +layout(location = 0) in vec2 vertex_position; +layout(location = 1) in vec4 vertex_colour; + +layout(location = 0) out vec4 fragment_colour; + +uniform mat4 P; + +void main() +{ + gl_Position = vec4(vertex_position, 0.f, 1.f); + + fragment_colour = vertex_colour; +} diff --git a/examples/resources/container.jpg b/examples/resources/container.jpg Binary files differnew file mode 100644 index 00000000..d07bee4e --- /dev/null +++ b/examples/resources/container.jpg diff --git a/examples/simplegame/dngn_blood_fountain.png b/examples/resources/dngn_blood_fountain.png Binary files differindex 7214fd47..7214fd47 100644 --- a/examples/simplegame/dngn_blood_fountain.png +++ b/examples/resources/dngn_blood_fountain.png diff --git a/examples/resources/fighter_fd.png b/examples/resources/fighter_fd.png Binary files differnew file mode 100644 index 00000000..ea576b4d --- /dev/null +++ b/examples/resources/fighter_fd.png diff --git a/examples/resources/fighter_fl.png b/examples/resources/fighter_fl.png Binary files differnew file mode 100644 index 00000000..69b677ca --- /dev/null +++ b/examples/resources/fighter_fl.png diff --git a/examples/resources/fighter_md.png b/examples/resources/fighter_md.png Binary files differnew file mode 100644 index 00000000..1b333abb --- /dev/null +++ b/examples/resources/fighter_md.png diff --git a/examples/resources/fighter_ml.png b/examples/resources/fighter_ml.png Binary files differnew file mode 100644 index 00000000..74dca840 --- /dev/null +++ b/examples/resources/fighter_ml.png diff --git a/examples/resources/healer_fd.png b/examples/resources/healer_fd.png Binary files differnew file mode 100644 index 00000000..138e7c48 --- /dev/null +++ b/examples/resources/healer_fd.png diff --git a/examples/resources/healer_fl.png b/examples/resources/healer_fl.png Binary files differnew file mode 100644 index 00000000..8360cfb9 --- /dev/null +++ b/examples/resources/healer_fl.png diff --git a/examples/resources/healer_md.png b/examples/resources/healer_md.png Binary files differnew file mode 100644 index 00000000..20ce4d2f --- /dev/null +++ b/examples/resources/healer_md.png diff --git a/examples/resources/healer_ml.png b/examples/resources/healer_ml.png Binary files differnew file mode 100644 index 00000000..28377d6b --- /dev/null +++ b/examples/resources/healer_ml.png diff --git a/examples/resources/learnopenglshader.frag b/examples/resources/learnopenglshader.frag new file mode 100644 index 00000000..30919459 --- /dev/null +++ b/examples/resources/learnopenglshader.frag @@ -0,0 +1,12 @@ +#version 450 core +out vec4 FragColor; + +in vec4 ourColor; +in vec2 TexCoord; + +uniform sampler2D ourTexture; + +void main() +{ + FragColor = texture(ourTexture, TexCoord); +} diff --git a/examples/resources/learnopenglshader.vert b/examples/resources/learnopenglshader.vert new file mode 100644 index 00000000..7b24b5d7 --- /dev/null +++ b/examples/resources/learnopenglshader.vert @@ -0,0 +1,14 @@ +#version 450 core +layout (location = 0) in vec2 aPos; +layout (location = 1) in vec4 aColor; +layout (location = 2) in vec2 aTexCoord; + +out vec4 ourColor; +out vec2 TexCoord; + +void main() +{ + gl_Position = vec4(aPos, 0.0, 1.0); + ourColor = aColor; + TexCoord = aTexCoord; +} diff --git a/examples/resources/mage_fd.png b/examples/resources/mage_fd.png Binary files differnew file mode 100644 index 00000000..b6a692f7 --- /dev/null +++ b/examples/resources/mage_fd.png diff --git a/examples/resources/mage_fl.png b/examples/resources/mage_fl.png Binary files differnew file mode 100644 index 00000000..66f5d2e5 --- /dev/null +++ b/examples/resources/mage_fl.png diff --git a/examples/resources/mage_md.png b/examples/resources/mage_md.png Binary files differnew file mode 100644 index 00000000..40838087 --- /dev/null +++ b/examples/resources/mage_md.png diff --git a/examples/resources/mage_ml.png b/examples/resources/mage_ml.png Binary files differnew file mode 100644 index 00000000..d559fb56 --- /dev/null +++ b/examples/resources/mage_ml.png diff --git a/examples/resources/ranger_fd.png b/examples/resources/ranger_fd.png Binary files differnew file mode 100644 index 00000000..e574cb1f --- /dev/null +++ b/examples/resources/ranger_fd.png diff --git a/examples/resources/ranger_fl.png b/examples/resources/ranger_fl.png Binary files differnew file mode 100644 index 00000000..9bc31719 --- /dev/null +++ b/examples/resources/ranger_fl.png diff --git a/examples/resources/ranger_md.png b/examples/resources/ranger_md.png Binary files differnew file mode 100644 index 00000000..5803cbe6 --- /dev/null +++ b/examples/resources/ranger_md.png diff --git a/examples/resources/ranger_ml.png b/examples/resources/ranger_ml.png Binary files differnew file mode 100644 index 00000000..e2e528b4 --- /dev/null +++ b/examples/resources/ranger_ml.png diff --git a/examples/resources/textureshader.frag b/examples/resources/textureshader.frag new file mode 100644 index 00000000..ae12f4b0 --- /dev/null +++ b/examples/resources/textureshader.frag @@ -0,0 +1,16 @@ +#version 330 + +in vec2 fragment_position; +in vec4 fragment_colour; +in vec2 fragment_uv; + +out vec4 colour; + +uniform sampler2D texture_sampler; + +void main() +{ + vec4 texture_colour = texture(texture_sampler, fragment_uv); + + colour = texture_colour * fragment_colour; +} diff --git a/examples/simplegame/textureshader.vert b/examples/resources/textureshader.vert index 3277d8b0..447c68d4 100644 --- a/examples/simplegame/textureshader.vert +++ b/examples/resources/textureshader.vert @@ -1,23 +1,20 @@ -#version 450 +#version 330 layout(location = 0) in vec2 vertex_position; layout(location = 1) in vec4 vertex_colour; layout(location = 2) in vec2 vertex_uv; -layout(location = 0) out vec2 fragment_position; -layout(location = 1) out vec4 fragment_colour; -layout(location = 2) out vec2 fragment_uv; +out vec2 fragment_position; +out vec4 fragment_colour; +out vec2 fragment_uv; uniform mat4 P; void main() { - gl_Position.xy = (P*vec4(vertex_position, 0.0, 1.0)).xy; - gl_Position.z = 0.0; - gl_Position.w = 1.0; + gl_Position = vec4((P*vec4(vertex_position, 0.f, 1.f)).xy, 0.f, 1.f); fragment_position = vertex_position; fragment_colour = vertex_colour; fragment_uv = vec2(vertex_uv.x, 1-vertex_uv.y); - } diff --git a/examples/resources/wall.jpg b/examples/resources/wall.jpg Binary files differnew file mode 100644 index 00000000..49631987 --- /dev/null +++ b/examples/resources/wall.jpg diff --git a/examples/resources/wall.png b/examples/resources/wall.png Binary files differnew file mode 100644 index 00000000..3eb2cd6e --- /dev/null +++ b/examples/resources/wall.png diff --git a/examples/shooter/bullet.cpp b/examples/shooter/bullet.cpp new file mode 100644 index 00000000..c589b4db --- /dev/null +++ b/examples/shooter/bullet.cpp @@ -0,0 +1,33 @@ +#include "bullet.h" + +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_); +} + +glm::vec4 Bullet::position() const +{ + return bound_; +} diff --git a/examples/shooter/bullet.h b/examples/shooter/bullet.h new file mode 100644 index 00000000..19430fda --- /dev/null +++ b/examples/shooter/bullet.h @@ -0,0 +1,24 @@ +#ifndef EXAMPLES_SHOOTER_BULLET_H +#define EXAMPLES_SHOOTER_BULLET_H + +#include <yage/yage.h> + +#include "direction.h" + +class Bullet : public yage::Drawable +{ +public: + Bullet(const glm::vec4 &bound, Direction dir, float speed, float depth = 0.f); + + 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 new file mode 100644 index 00000000..e38bf53b --- /dev/null +++ b/examples/shooter/main.cpp @@ -0,0 +1,169 @@ +#include <yage/yage.h> + +#include "bullet.h" +#include "direction.h" +#include "player.h" + +#include <memory> +#include <vector> + +using std::cout; + +int main(int argc, char **argv) +{ + yage::Window window; + window.create("Shooter example", 800, 600); + + std::vector<std::unique_ptr<Bullet>> bullets; + + 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; + } + + if (window.keyPressed(yage::key::RIGHT)) { + bullets.push_back(std::make_unique<Bullet>( + 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<Bullet>( + 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<Bullet>( + 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<Bullet>( + 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]); + + camera.update(shader); + + window.clearBuffer(); + player.draw(sp); + + for (auto &&bullet : bullets) { + bullet->draw(sp); + } + + sp.render(); + + window.swapBuffer(); + } +} diff --git a/examples/shooter/player.cpp b/examples/shooter/player.cpp new file mode 100644 index 00000000..dab743a9 --- /dev/null +++ b/examples/shooter/player.cpp @@ -0,0 +1,72 @@ +#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) +{ + 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), 1); + 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), 1); + 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; +} + +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 new file mode 100644 index 00000000..8b5121c5 --- /dev/null +++ b/examples/shooter/player.h @@ -0,0 +1,36 @@ +#ifndef EXAMPLE_SHOOTER_PLAYER_H +#define EXAMPLE_SHOOTER_PLAYER_H + +#include <yage/yage.h> + +#include "direction.h" + +enum class Action { + IDLE, + MOVING, +}; + +class Player : public yage::Drawable +{ +public: + Player(const glm::vec4 &bound, const yage::Texture &texture); + + void setTexture(const yage::Texture &texture); + + void draw(yage::SpriteBatch &sp); + + void move(Direction direction); + void idle(); + void look(Direction direction); + + // simple getters + glm::vec4 position() const; +private: + glm::vec4 bound_; + yage::Texture texture_; + Direction direction_; + Action action_; + int speed_; +}; + +#endif diff --git a/examples/simplegame/main.cpp b/examples/simplegame/main.cpp index a631e45b..ed6b78f3 100644 --- a/examples/simplegame/main.cpp +++ b/examples/simplegame/main.cpp @@ -8,6 +8,8 @@ #include <yage.cpp> +#include <cstdlib> +#include <ctime> #include <iostream> using std::cout; @@ -16,42 +18,95 @@ using namespace yage; int main() { + + Logger logger; + srand(time(nullptr)); Window window; - GlslProgram program; + window.create("Simple Game", 1920, 1080); - window.create("Simple Game", 800, 640); + Shader textureProgram("examples/resources/textureshader.vert", + "examples/resources/textureshader.frag"); SpriteBatch sp; - program.compileShadersFromFile("examples/simplegame/textureshader.vert", - "examples/simplegame/textureshader.frag"); - program.linkShaders(); - Texture fountain = ResourceManager::getTexture( - "examples/simplegame/dngn_blood_fountain.png"); - - cout << "texture: " << fountain.width << ", " << fountain.height << '\n'; + "examples/resources/dngn_blood_fountain.png"); + Texture breast_plate = + ResourceManager::getTexture("examples/resources/breast_black.png"); - Camera camera(800, 640); + Texture brick = ResourceManager::getTexture("examples/resources/wall.png"); - while (!window.shouldClose()) { - window.clearBuffer(); + yLogDebug << "texture: " << brick.width << ", " << brick.height; - program.use(); - camera.update(program); + Camera camera(1920, 1080); + textureProgram.use(); + textureProgram.setUniform("texture_sampler", 0); - glActiveTexture(GL_TEXTURE0); + double prev_time = glfwGetTime(); + double final_time = 0; + double diff = 0; + double fps = 0; + int i = 0; + double time; - GLint texture_location = program.getUniformLocation("texture_sampler"); - glUniform1i(texture_location, 0); + while (!window.shouldClose()) { + window.clearBuffer(); + Texture texture = fountain; - sp.draw({0.f, 0.f, 64.f, 64.f}, {0, 0, 1, 1}, fountain.id, - Colour(255, 0, 255, 255), 0); + window.pollEvents(); + if (window.keyPressed(yage::key::SPACE)) { + cout << "Pressed A" << '\n'; + } + if (window.keyPressed(yage::key::E)) { + texture = breast_plate; + } + if (window.keyPressed(yage::key::EQUAL)) { + camera.zoom(0.1f); + } + if (window.keyPressed(yage::key::MINUS)) { + camera.zoom(-0.1f); + } + if (window.keyPressed(yage::key::LEFT)) { + camera.move({-5.f, 0.f}); + } + if (window.keyPressed(yage::key::RIGHT)) { + camera.move({5.f, 0.f}); + } + if (window.keyPressed(yage::key::UP)) { + camera.move({0.f, 5.f}); + } + if (window.keyPressed(yage::key::DOWN)) { + camera.move({0.f, -5.f}); + } + + camera.update(textureProgram); + + int amount = 10; + time = glfwGetTime(); + for (int i = 0; i < 1920 / amount; i++) { + for (int j = 0; j < 1080 / amount; j++) + sp.draw({(float)(amount * i), (float)(amount * j), + (float)amount, (float)amount}, + {0.f, 0.f, 1.f, 1.f}, texture.id, + Colour(rand() % 256, rand() % 256, rand() % 256, 255), + 0); + } + + sp.draw({50, 50, 100, 100}, {0, 0, 1, 1}, brick.id, + Colour(255, 255, 255, 255), 1); + yLogDebug << "draw: " << glfwGetTime() - time; + + time = glfwGetTime(); sp.render(); - - glBindTexture(GL_TEXTURE_2D, 0); - program.unuse(); - + yLogDebug << "render: " << glfwGetTime() - time; window.swapBuffer(); - window.pollEvents(); + + if (i == 0) { + final_time = glfwGetTime(); + diff = final_time - prev_time; + prev_time = final_time; + fps = 1 / diff * 30; + yLogInfo << "fps: " << fps; + } + i = (i + 1) % 30; } } diff --git a/examples/simplegame/textureshader.frag b/examples/simplegame/textureshader.frag deleted file mode 100644 index ef728b04..00000000 --- a/examples/simplegame/textureshader.frag +++ /dev/null @@ -1,16 +0,0 @@ -#version 450 - -layout(location = 0) in vec2 fragment_position; -layout(location = 1) in vec4 fragment_colour; -layout(location = 2) in vec2 fragment_uv; - -out vec4 colour; - -uniform sampler2D texture_sampler; - -void main() -{ - vec4 texture_color = texture(texture_sampler, fragment_uv); - - colour = texture_color * fragment_colour; -} |