-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapviewer.py
145 lines (104 loc) · 3.84 KB
/
mapviewer.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
# -*- coding: utf-8 -*-
from __future__ import annotations
import asyncio
import logging
import os
from utils.config import CONFIG
from utils.profiler import LineProfiler
if 'PYGLFW_LIBRARY' not in os.environ:
os.environ['PYGLFW_LIBRARY'] = os.path.join(
os.path.abspath(os.path.dirname(__file__)), 'glfw3.dll')
import OpenGL.GL as gl
import numpy as np
import glfw
import imgui
from imgui.integrations.glfw import GlfwRenderer
from gui import StarboundMap
from map.renderer import WorldRenderer
from utils import asyncjob, application
CONFIG_SECTION = 'window'
CONFIG_WIDTH = 'width'
CONFIG_HEIGHT = 'height'
class G:
"""
GLFW state
"""
minimized: bool = False
line_profiler: LineProfiler = None
@staticmethod
def on_minimization_event(is_minimized):
G.minimized = is_minimized
def impl_glfw_init(config):
width = config.getint(CONFIG_WIDTH, fallback=800)
height = config.getint(CONFIG_HEIGHT, fallback=600)
window_name = "Starbound Map"
logging.info("GLFW ver: %s", glfw.get_version_string())
if not glfw.init():
logging.fatal("Could not initialize OpenGL context")
exit(1)
# OS X supports only forward-compatible core profiles from 3.2
glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 4)
glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, gl.GL_TRUE)
# Create a windowed mode window and its OpenGL context
window = glfw.create_window(
int(width), int(height), window_name, None, None
)
glfw.make_context_current(window)
glfw.set_window_iconify_callback(
window, lambda _, minimized: G.on_minimization_event(minimized))
if not window:
glfw.terminate()
logging.fatal("Could not initialize Window")
exit(1)
logging.info("OpenGL ver: %s", gl.glGetString(gl.GL_VERSION))
logging.info("GLSL ver: %s", gl.glGetString(gl.GL_SHADING_LANGUAGE_VERSION))
glfw.set_window_size_limits(window,
minwidth=64,
minheight=64,
maxwidth=glfw.DONT_CARE,
maxheight=glfw.DONT_CARE)
return window
async def async_main(window, config):
await asyncjob.start_worker()
imgui_impl = GlfwRenderer(window)
renderer = WorldRenderer()
app = StarboundMap(renderer)
frame_size = np.ones(2)
while not glfw.window_should_close(window):
glfw.poll_events()
if G.minimized: # do not render zero sized frame
continue
imgui_impl.process_inputs()
frame_size = np.array(glfw.get_framebuffer_size(window))
gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, 0)
gl.glViewport(0, 0, frame_size[0], frame_size[1])
gl.glClearColor(0.1, 0.1, 0.2, 1)
gl.glClear(gl.GL_COLOR_BUFFER_BIT)
app.gui()
glfw.swap_buffers(window)
await asyncio.sleep(0)
config[CONFIG_WIDTH] = str(int(frame_size[0]))
config[CONFIG_HEIGHT] = str(int(frame_size[1]))
imgui_impl.shutdown()
imgui.shutdown()
def main():
if application.development:
logging.debug("Detected dev environment")
if not CONFIG.has_section(CONFIG_SECTION):
CONFIG.add_section(CONFIG_SECTION)
config = CONFIG[CONFIG_SECTION]
window = impl_glfw_init(config)
asyncio.run(async_main(window, config))
glfw.terminate()
CONFIG.save()
if __name__ == "__main__":
# Clear default handler in case it's generated by emitting logs in our dependencies
logging.root.handlers = []
logging.basicConfig(
format='%(pathname)s:%(lineno)d:\n%(asctime)-15s | %(levelname)7s | %(message)s ',
level=logging.DEBUG)
# from utils.profiler import MemoryProfiler
# with MemoryProfiler():
main()