aboutsummaryrefslogtreecommitdiffstats
path: root/examples/simplegame/main.cpp
blob: bea3bc8d3cf9ea50dcec14b30763f986581c22f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/** ---------------------------------------------------------------------------
 * @file: simplegame.cpp
 *
 * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
 * MIT License, see LICENSE file for more details.
 * ----------------------------------------------------------------------------
 */

#include <yage.cpp>

#include <cstdlib>
#include <ctime>
#include <iostream>

using std::cout;

using namespace yage;

int main()
{
    yage::init();
    Logger logger;
    srand(time(nullptr));
    Window window;
    window.create("Simple Game", 1920, 1080);

    Shader textureProgram("examples/resources/textureshader.vert",
                          "examples/resources/textureshader.frag");
    SpriteBatch sp;

    Texture fountain = ResourceManager::getTexture(
        "examples/resources/dngn_blood_fountain.png");
    Texture breast_plate =
        ResourceManager::getTexture("examples/resources/breast_black.png");

    Texture brick = ResourceManager::getTexture("examples/resources/wall.png");

    yLogDebug << "texture: " << brick.width << ", " << brick.height;

    Camera camera(1920, 1080);
    textureProgram.use();
    textureProgram.setUniform("texture_sampler", 0);

    double prev_time  = glfwGetTime();
    double final_time = 0;
    double diff       = 0;
    double fps        = 0;
    int i             = 0;
    double time;

    while (!window.shouldClose()) {
        window.clearBuffer();
        Texture texture = fountain;

        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();
        yLogDebug << "render: " << glfwGetTime() - time;
        window.swapBuffer();

        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;
    }
    yage::quit();
}