-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
72 lines (57 loc) · 1.98 KB
/
main.py
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
from settings import *
import moderngl as mgl
import pygame as pg
import sys
from shader_program import ShaderProgram
from scene import Scene
from player import Player
from textures import Textures
class VoxelEngine:
def __init__(self):
pg.init()
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)
pg.display.gl_set_attribute(pg.GL_DEPTH_SIZE, 24)
pg.display.set_mode(WIN_RES, flags=pg.OPENGL | pg.DOUBLEBUF)
self.ctx = mgl.create_context()
self.ctx.enable(flags=mgl.DEPTH_TEST | mgl.CULL_FACE | mgl.BLEND)
self.ctx.gc_mode = 'auto'
self.clock = pg.time.Clock()
self.delta_time = 0
self.time = 0
pg.event.set_grab(True)
pg.mouse.set_visible(False)
self.is_running = True
self.on_init()
def on_init(self):
self.textures = Textures(self)
self.player = Player(self)
self.shader_program = ShaderProgram(self)
self.scene = Scene(self)
def update(self):
self.player.update()
self.shader_program.update()
self.scene.update()
self.delta_time = self.clock.tick()
self.time = pg.time.get_ticks() * 0.001
pg.display.set_caption(f'{self.clock.get_fps() :.0f}')
def render(self):
self.ctx.clear(color=BG_COLOUR)
self.scene.render()
pg.display.flip()
def handle_events(self):
for event in pg.event.get():
# add escape menu
if event.type == pg.QUIT or (event.type == pg.KEYDOWN and event.key == pg.K_ESCAPE):
self.is_running = False
def run(self):
while self.is_running:
self.handle_events()
self.update()
self.render()
pg.quit()
sys.exit()
if __name__ == '__main__':
app = VoxelEngine()
app.run()