summaryrefslogtreecommitdiff
path: root/txtgameengine/__main__.py
blob: 1ebc309d02bb82eea0e4db3937566ef3c1747147 (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
import numpy as np
from PIL import Image

from .scenes import SceneTxtGameApp, Scene
from OpenGL.GL import *
from pathlib import Path

shader_path = Path(__file__).parent / 'builtin_shaders'


class TriangleScene(Scene):
    TRIANGLE_DATA = [
        -1.0, -1.0,
        1.0, -1.0,
        0.0, 1.0,
    ]

    def on_enter(self):
        self.default_shaders = self.app.shaders.load_shaders(
            str(shader_path / 'basic/vertex.glsl'), str(shader_path / 'basic/fragment.glsl'))

        self.tri_buffer = self.app.render.setup_buffer(
            np.array(self.TRIANGLE_DATA, np.float32))
        self.t = 0

    def update(self, delta):
        self.t += delta
        if self.t > 10:
            self.push_scene(EvilTriangleScene)
            self.t = 0
        with self.default_shaders:
            self.app.render.triangle(self.tri_buffer)


class EvilTriangleScene(TriangleScene):
    TRIANGLE_DATA = [
        -1.0, 1.0,
        1.0, 1.0,
        0.0, -1.0,
    ]

    def update(self, delta):
        super().update(delta)
        self.t = 0


class TextureScene(Scene):
    def on_enter(self):
        self.texture_shaders = self.app.shaders.load_shaders(str(shader_path / 'texture/vertex.glsl'),
                                                             str(shader_path / 'texture/fragment.glsl'))
        self.sampler_location = self.app.shaders.get_uniform_location(self.texture_shaders, 'textureSampler')
        self.texture = self.app.render.setup_texture_from_pil(Image.open('test_image.png'))
        self.triangle = self.app.render.setup_buffer(
            np.array([
                -1.0, 1.0,
                1.0, 1.0,
                -1.0, -1.0,
            ], np.float32))
        self.uvs = self.app.render.setup_buffer(
            np.array([
                0, 0,
                1, 0,
                0, 1,
            ], np.float32))

    def update(self, delta: float):
        with self.texture_shaders:
            self.app.render.textured_triangle(self.sampler_location, self.texture, self.triangle, self.uvs)


class TestApp(SceneTxtGameApp):
    MAIN_SCENE_T = TextureScene

    def init(self):
        super().init()
        self.render.setup_vertex_arrays()
        # self.platform.check_debug()


if __name__ == '__main__':
    a = TestApp((640, 480), "OpenGL window")
    a.start()