forked from chloekoee/conways_garden
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
97 lines (79 loc) · 2.97 KB
/
main.py
File metadata and controls
97 lines (79 loc) · 2.97 KB
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
from controls import Controls
from constants.settings import *
import moderngl as mgl
import pygame as pg
import sys
from perspective.player import Player
from scene import Scene
from shader_program import ShaderProgram
from textures import Textures
class Engine:
def __init__(self):
pg.init()
## Dynamically calculate display size
info = pg.display.Info()
screen_w, screen_h = info.current_w, info.current_h
self.resolution = glm.vec2(int(screen_w * 1.0), int(screen_h * 1.0))
self.cx, self.cy = screen_w // 2, screen_h // 2
self.aspect_ratio = self.resolution.x / self.resolution.y
pg.display.gl_set_attribute(pg.GL_CONTEXT_MAJOR_VERSION, 3)
pg.display.gl_set_attribute(pg.GL_CONTEXT_MINOR_VERSION, 3)
pg.display.gl_set_attribute(
pg.GL_CONTEXT_PROFILE_MASK, pg.GL_CONTEXT_PROFILE_CORE
)
## Set value of 24 bits for depth buffer
pg.display.gl_set_attribute(pg.GL_DEPTH_SIZE, 24)
## Create open GL context
pg.display.set_mode(self.resolution, flags=pg.OPENGL | pg.DOUBLEBUF)
self.ctx = mgl.create_context()
self.ctx.enable(flags=mgl.DEPTH_TEST | mgl.BLEND | mgl.CULL_FACE)
self.ctx.depth_func = "<="
## Enable automatic garbage collection of unused Open GL objects
self.ctx.gc_mode = "auto"
## Time keeping
self.clock = pg.time.Clock()
self.delta_time = 0
self.time = 0
self.time_since_last_step = 0
cx, cy = self.resolution.x // 2, self.resolution.y // 2
pg.event.set_grab(True)
pg.mouse.set_visible(False)
pg.mouse.set_pos((self.cx, self.cy))
self.is_running = True
self.paused = False
self.on_init()
pg.display.set_caption(f"{self.scene.nca.step :.0f}")
def on_init(self):
self.textures = Textures(self)
self.controls = Controls(self)
self.player = Player(self)
self.shader_program = ShaderProgram(self)
self.scene = Scene(self)
def update(self):
self.shader_program.update()
self.scene.update()
self.player.update()
self.delta_time = self.clock.tick()
self.time = pg.time.get_ticks() * 0.001
if not self.scene.nca.frozen:
if self.time - self.time_since_last_step > SECONDS_PER_STEP:
self.time_since_last_step = self.time
self.scene.nca.take_step()
pg.display.set_caption(f"{self.scene.nca.step :.0f}")
def on_render(self):
self.ctx.clear(color=BG_COLOR)
self.scene.render()
pg.display.flip()
def run(self):
while self.is_running:
self.controls.poll()
self.controls.handle_global()
if not self.paused:
self.controls.apply(self.player)
self.update()
self.on_render()
pg.quit()
sys.exit()
if __name__ == "__main__":
app = Engine()
app.run()