diff options
Diffstat (limited to 'examples/simplegame/main.cpp')
-rw-r--r-- | examples/simplegame/main.cpp | 103 |
1 files changed, 79 insertions, 24 deletions
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; } } |