forked from Zygahedron/Parabox-Editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdl2_start.py
156 lines (129 loc) · 4.7 KB
/
sdl2_start.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
# -*- coding: utf-8 -*-
import multiprocessing
# import glfw
from sdl2 import *
import ctypes
import OpenGL.GL as gl
import imgui
# from imgui.integrations.glfw import GlfwRenderer
from imgui.integrations.sdl2 import SDL2Renderer
import time
from editor import *
import style as imgui_style
class Key:
def __init__(self):
self.pressed = False
self.down = False
self.released = False
class Keyboard:
def __init__(self):
self.n = Key()
self.b = Key()
self.a = Key()
self.o = Key()
self.s = Key()
self.up = Key()
self.left = Key()
self.down = Key()
self.right = Key()
self.enter = Key()
self.map = {
SDL_SCANCODE_A: self.a,
SDL_SCANCODE_B: self.b,
SDL_SCANCODE_N: self.n,
SDL_SCANCODE_O: self.o,
SDL_SCANCODE_S: self.s,
SDL_SCANCODE_UP: self.up,
SDL_SCANCODE_DOWN: self.down,
SDL_SCANCODE_LEFT: self.left,
SDL_SCANCODE_RIGHT: self.right,
SDL_SCANCODE_RETURN: self.enter
}
keyboard = Keyboard()
def main():
global levels_folder
imgui.create_context()
window, gl_context = impl_pysdl2_init()
impl = SDL2Renderer(window)
style = imgui.get_style()
imgui_style.set(style)
editor = Editor()
running = True
event = SDL_Event()
last_time = time.time()
while running:
while SDL_PollEvent(ctypes.byref(event)) != 0:
if event.type == SDL_QUIT:
running = False
break
if event.type == SDL_KEYDOWN:
if event.key.keysym.scancode in keyboard.map:
keyboard.map[event.key.keysym.scancode].pressed = True
keyboard.map[event.key.keysym.scancode].down = True
if event.type == SDL_KEYUP:
if event.key.keysym.scancode in keyboard.map:
keyboard.map[event.key.keysym.scancode].released = True
keyboard.map[event.key.keysym.scancode].down = False
impl.process_event(event)
# glfw.poll_events()
impl.process_inputs()
if time.time() <= last_time:
continue
last_time = time.time()
if not SDL_GetWindowFlags(window) & SDL_WINDOW_MOUSE_FOCUS:
continue
imgui.new_frame()
if not editor.main_loop(keyboard):
break
for key in keyboard.map.values():
key.pressed = False
key.released = False
gl.glClearColor(0.1, 0.1, 0.1, 1)
gl.glClear(gl.GL_COLOR_BUFFER_BIT)
imgui.render()
impl.render(imgui.get_draw_data())
# glfw.swap_buffers(window)
SDL_GL_SwapWindow(window)
impl.shutdown()
# glfw.terminate()
SDL_GL_DeleteContext(gl_context)
SDL_DestroyWindow(window)
SDL_Quit()
def impl_pysdl2_init():
width, height = 800, 600
window_name = "Zygan's Parabox Editor"
if SDL_Init(SDL_INIT_EVERYTHING) < 0:
print("Error: SDL could not initialize! SDL Error: " + SDL_GetError().decode("utf-8"))
exit(1)
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1)
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24)
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8)
SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1)
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1)
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 16)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE)
SDL_SetHint(SDL_HINT_MAC_CTRL_CLICK_EMULATE_RIGHT_CLICK, b"1")
SDL_SetHint(SDL_HINT_VIDEO_HIGHDPI_DISABLED, b"1")
window = SDL_CreateWindow(window_name.encode('utf-8'),
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
width, height,
SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE)
if window is None:
print("Error: Window could not be created! SDL Error: " + SDL_GetError().decode("utf-8"))
exit(1)
gl_context = SDL_GL_CreateContext(window)
if gl_context is None:
print("Error: Cannot create OpenGL Context! SDL Error: " + SDL_GetError().decode("utf-8"))
exit(1)
SDL_GL_MakeCurrent(window, gl_context)
if SDL_GL_SetSwapInterval(1) < 0:
print("Warning: Unable to set VSync! SDL Error: " + SDL_GetError().decode("utf-8"))
# exit(1)
return window, gl_context
if __name__ == "__main__":
# pyinstaller infinite loop fix???
multiprocessing.freeze_support()
main()