aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2018-01-04 21:36:30 +0000
committerYann Herklotz <ymherklotz@gmail.com>2018-01-04 21:36:30 +0000
commit7b95e3a9eacf296f215c73e5d8ad9090a24adb20 (patch)
tree77125b5b30e08e48903e802ad4cdfddbb03074c1
parenta47649786fb94684c415b230669fbf5343cb7c5d (diff)
downloadYAGE-7b95e3a9eacf296f215c73e5d8ad9090a24adb20.tar.gz
YAGE-7b95e3a9eacf296f215c73e5d8ad9090a24adb20.zip
[Engine] Now using stb_image to laod all kinds of textures.
-rw-r--r--examples/resources/container.jpgbin0 -> 184939 bytes
-rw-r--r--examples/shooter/bullet.cpp31
-rw-r--r--examples/shooter/bullet.h11
-rw-r--r--examples/shooter/direction.h11
-rw-r--r--examples/shooter/main.cpp36
-rw-r--r--examples/shooter/player.cpp11
-rw-r--r--examples/shooter/player.h12
-rw-r--r--yage/core/imageloader.cpp36
-rw-r--r--yage/core/window.cpp11
-rw-r--r--yage/render/drawable.h2
10 files changed, 115 insertions, 46 deletions
diff --git a/examples/resources/container.jpg b/examples/resources/container.jpg
new file mode 100644
index 00000000..d07bee4e
--- /dev/null
+++ b/examples/resources/container.jpg
Binary files differ
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 <yage/yage.h>
+#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 <yage/yage.h>
#include "bullet.h"
+#include "direction.h"
#include "player.h"
-#include <vector>
#include <memory>
+#include <vector>
using std::cout;
@@ -13,7 +14,7 @@ int main(int argc, char **argv)
yage::Window window;
window.create("Shooter example", 800, 600);
- std::vector<std::unique_ptr<yage::Drawable>> objects;
+ std::vector<std::unique_ptr<Bullet>> 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<Bullet>(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<Bullet>(glm::vec4(player.position().x, player.position().y, 25, 25)));
+ 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]);
@@ -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<int>(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<int>(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 <yage/yage.h>
-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;
diff --git a/yage/core/imageloader.cpp b/yage/core/imageloader.cpp
index 7b34312d..5a60e83b 100644
--- a/yage/core/imageloader.cpp
+++ b/yage/core/imageloader.cpp
@@ -8,10 +8,10 @@
#include "imageloader.h"
#include "../data/texture.h"
+#include "logger.h"
+#include "stb_image.h"
#include <glad/glad.h>
-#include <yage/core/iomanager.h>
-#include <yage/core/picopng.h>
#include <iostream>
#include <stdexcept>
@@ -21,30 +21,22 @@ namespace yage
Texture ImageLoader::loadPng(const std::string &file_path)
{
- std::vector<unsigned char> in;
- std::vector<unsigned char> out;
- unsigned long width, height;
-
- if (!IoManager::readFileToBuffer(file_path, in)) {
- throw std::runtime_error("Failed to load '" + file_path +
- "' to buffer");
- }
-
- int error_code = decodePNG(out, width, height, &in[0], in.size());
- if (error_code != 0) {
- throw std::runtime_error("Failed to load '" + file_path +
- "' to png with error code" +
- std::to_string(error_code));
- }
+ int width, height, num_channels;
+ unsigned char *data =
+ stbi_load(file_path.c_str(), &width, &height, &num_channels, 0);
Texture texture(0, static_cast<int>(width), static_cast<int>(height));
- std::cout << "Geometry: " << texture.width << "x" << texture.height << "\n";
-
glGenTextures(1, &texture.id);
glBindTexture(GL_TEXTURE_2D, texture.id);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
- GL_UNSIGNED_BYTE, &out[0]);
+
+ if (num_channels == 4) {
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
+ GL_UNSIGNED_BYTE, data);
+ } else {
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGB,
+ GL_UNSIGNED_BYTE, data);
+ }
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
@@ -54,7 +46,7 @@ Texture ImageLoader::loadPng(const std::string &file_path)
glGenerateMipmap(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D, 0);
+ yLog << "Successfully loaded texture: " << file_path;
return texture;
}
diff --git a/yage/core/window.cpp b/yage/core/window.cpp
index 5db75074..711e1294 100644
--- a/yage/core/window.cpp
+++ b/yage/core/window.cpp
@@ -19,11 +19,20 @@ using std::runtime_error;
namespace yage
{
+namespace {
+
void key_callback(GLFWwindow *window, int key, int scanCode, int action,
int mods)
{
}
+void framebuffer_size_callback(GLFWwindow *window, int width, int height)
+{
+ glViewport(0, 0, width, height);
+}
+
+} // namespace
+
Window::Window() = default;
Window::~Window()
@@ -56,6 +65,8 @@ void Window::create(std::string window_name, int width, int height)
// set key callback
glfwSetKeyCallback(window_, key_callback);
+ // set resize callback
+ glfwSetFramebufferSizeCallback(window_, framebuffer_size_callback);
// set vsync on
glfwSwapInterval(1);
diff --git a/yage/render/drawable.h b/yage/render/drawable.h
index 8ef48f2d..c126bb09 100644
--- a/yage/render/drawable.h
+++ b/yage/render/drawable.h
@@ -9,7 +9,7 @@ namespace yage
class Drawable
{
public:
- virtual void draw(SpriteBatch &sp) const = 0;
+ virtual void draw(SpriteBatch &sp) = 0;
};
} // namespace yage