-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
207 lines (163 loc) · 6.13 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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import pygame as pg
from PIL import Image
import moderngl as mgl
from datetime import datetime
import git
import sys
from model import *
from player import Player
from saveManager import SaveManager
from scene import Scene
from config import Config
from ui import UserInterface
from sounds import SoundEngine
from camera import Camera
from textures import TextureManager
from shaderProgram import ShaderProgramManager
class GraphicsEngine :
def __init__(self, windowSize=(1600, 900)) :
pg.init()
self.windowSize = windowSize
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.set_mode(self.windowSize, flags=pg.OPENGL | pg.DOUBLEBUF | pg.RESIZABLE)
#Mouse lock
pg.event.set_grab(True)
pg.mouse.set_visible(False)
#Detect and use existing OpenGL context
self.ctx = mgl.create_context()
self.ctx.enable(flags=mgl.DEPTH_TEST | mgl.CULL_FACE)
#Clock and time
self.clock = pg.time.Clock()
self.time = 0
self.deltaTime = 0
#Application
self.name = "VoxelEngine"
self.sourceCodeLink = "https://github.com/TriLinder/VoxelEngine"
self.commitHash = "UNKNOWN"
self.getCommitHash()
#Game info
self.gamePaused = True
self.inGame = False
#Config
self.config = Config(self)
#Camera
self.camera = Camera(self)
#Sound engine
self.sound = SoundEngine(self)
#Texture and shader managers
self.textureMan = TextureManager(self)
self.shaderMan = ShaderProgramManager(self)
#Save manager
self.saveMan = SaveManager(self)
#Scene
self.scene = Scene(self)
#User interface
self.ui = UserInterface(self)
self.pgEvents = []
#Player
self.player = Player(self)
pg.display.set_caption("Voxel Engine (UNKNOWN) | ??fps")
pg.display.set_icon(self.textureMan.iconTexture)
if self.config.fullscreen :
pg.display.toggle_fullscreen()
def getCommitHash(self) :
try :
repo = git.Repo()
except :
print('WARNING: git repository not found, please make sure you used the "git clone" command to clone the repository')
return
self.commitHash = repo.head.object.hexsha
def takeScreenshot(self, save=True, drawUi=True, playSound=True) :
#Play sound
if playSound :
self.sound.play("ui", "screenshot", force=True)
#Take screenshot
self.ui.surface.fill((0, 0, 0, 0))
self.ui.redrawNextFrame = drawUi
self.ui.redrawInTicks = 2
self.ui.writeToTexture()
self.render(flip=False)
data = self.ctx.fbo.read(viewport=self.windowSize, alignment=1)
image = Image.frombytes("RGB", self.windowSize, data)
image = image.transpose(Image.Transpose.FLIP_TOP_BOTTOM)
#Save the image
if save :
if not os.path.isdir("screenshots") :
os.mkdir("screenshots")
date = datetime.now().strftime("%Y-%m-%d %H_%M_%S")
foundFilename = False
index = 0
while not foundFilename :
if index == 0 :
filename = f"{date}.png"
else :
filename = f"{date}_{index}.png"
path = os.path.join("screenshots", filename)
if not os.path.isfile(path) :
foundFilename = True
else :
index += 1
image.save(path)
return image
def quit(self) :
print("Quiting!")
self.scene.destroy()
pg.quit()
sys.exit(0)
def checkEvents(self) :
self.pgEvents = pg.event.get()
for e in self.pgEvents :
if e.type == pg.QUIT or (e.type == pg.KEYDOWN and e.key == pg.K_DELETE) : #Quit the application
self.quit()
if e.type == pg.KEYDOWN and e.key == pg.K_ESCAPE and self.inGame : #Pause the game
if not self.ui.hideUi :
self.gamePaused = not self.gamePaused
self.ui.redrawNextFrame = True
if e.type == pg.KEYDOWN and e.key == pg.K_F11 : #Toggle fullscreen
pg.display.toggle_fullscreen()
self.ui.redrawInTicks = 2
self.config.fullscreen = not self.config.fullscreen
self.config.writeToFile()
if e.type == pg.KEYDOWN and e.key == pg.K_F1 : #Hide UI
if not self.gamePaused :
self.ui.hideUi = not self.ui.hideUi
if e.type == pg.KEYDOWN and e.key == pg.K_F2 : #Take screenshot
self.takeScreenshot()
if e.type == pg.WINDOWSIZECHANGED : #Resize camera and UI
self.windowSize = (e.x, e.y)
self.camera.aspectRatio = e.x / e.y
self.ui.resize()
def updateWindowCaption(self) :
fps = round(self.ui.app.clock.get_fps())
pg.display.set_caption(f"{self.name} ({self.commitHash[:7]}) | {fps}fps")
def render(self, flip=True) :
#Clear framebuffer
self.ctx.clear(color=((42/255, 42/255, 42/255)))
#Render the UI and the scene
self.scene.render()
self.ui.render()
if flip :
#Swap buffers
pg.display.flip()
def getTime(self) :
self.time = pg.time.get_ticks() / 1000
def run(self) :
self.render()
while True :
self.getTime()
self.checkEvents()
self.player.tick()
self.camera.update()
self.scene.tick()
self.ui.tick()
self.render()
self.deltaTime = self.clock.tick(self.config.fpsLimit)
self.updateWindowCaption()
if __name__ == "__main__" :
app = GraphicsEngine()
try :
app.run()
except KeyboardInterrupt :
app.quit()