1 | import pygame, OpenGL, math
|
---|
2 |
|
---|
3 | from OpenGL.GLU import *
|
---|
4 | from OpenGL.GL import *
|
---|
5 |
|
---|
6 | import platform
|
---|
7 |
|
---|
8 | [w, h] = [0, 0]
|
---|
9 |
|
---|
10 | def render(surface, overlay, font, state):
|
---|
11 | #surface.fill((0, 0, 0))
|
---|
12 |
|
---|
13 | #glEnable (GL_BLEND); glBlendFunc (GL_ONE, GL_ONE);
|
---|
14 |
|
---|
15 | # I should really figure out which attributes in the 2D rendering
|
---|
16 | # break the 3d rendering and reset those back instead of resetting
|
---|
17 | # all attributes
|
---|
18 | if state:
|
---|
19 | renderOverlay(overlay, font)
|
---|
20 | glPushAttrib(GL_ALL_ATTRIB_BITS)
|
---|
21 | projectOverlay(surface, overlay)
|
---|
22 | glPopAttrib(GL_ALL_ATTRIB_BITS)
|
---|
23 | else:
|
---|
24 | glPushAttrib(GL_ALL_ATTRIB_BITS)
|
---|
25 | render3d(surface)
|
---|
26 | glPopAttrib(GL_ALL_ATTRIB_BITS)
|
---|
27 |
|
---|
28 |
|
---|
29 | def renderOverlay(overlay, font):
|
---|
30 | #pygame.draw.rect(overlay, (0, 0, 255), pygame.Rect(0, 0, w, h))
|
---|
31 | #pygame.draw.circle(overlay, (255, 0, 0), (int(w/2), int(h/2)), 540)
|
---|
32 | #overlay.set_colorkey((255,0,0))
|
---|
33 |
|
---|
34 | text = font.render("2D drawing", True, (0, 128, 0))
|
---|
35 |
|
---|
36 | pygame.draw.rect(overlay, (0, 128, 255), pygame.Rect(30, 30, 60, 60))
|
---|
37 | overlay.blit(text, (500, 100))
|
---|
38 |
|
---|
39 | def projectOverlay(surface, overlay):
|
---|
40 |
|
---|
41 | glPushMatrix()
|
---|
42 |
|
---|
43 | glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
|
---|
44 |
|
---|
45 | glTranslatef(0.0,0.0,-zNear)
|
---|
46 |
|
---|
47 | textureData = pygame.image.tostring(overlay, "RGBA", 1)
|
---|
48 | width = overlay.get_width()
|
---|
49 | height = overlay.get_height()
|
---|
50 |
|
---|
51 | glEnable(GL_TEXTURE_2D)
|
---|
52 |
|
---|
53 | glBindTexture(GL_TEXTURE_2D, glGenTextures(1))
|
---|
54 | # maybe use GL_RGBA here instead
|
---|
55 | #glPixelStorei(GL_UNPACK_ALIGNMENT,1)
|
---|
56 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData)
|
---|
57 |
|
---|
58 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
|
---|
59 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
|
---|
60 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
|
---|
61 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
|
---|
62 |
|
---|
63 | glBegin(GL_QUADS)
|
---|
64 |
|
---|
65 | glTexCoord2f(0.0, 0.0)
|
---|
66 | glVertex2f(-(w/h), -1)
|
---|
67 |
|
---|
68 | glTexCoord2f(1.0, 0.0)
|
---|
69 | glVertex2f((w/h), -1)
|
---|
70 |
|
---|
71 | glTexCoord2f(1.0, 1.0)
|
---|
72 | glVertex2f((w/h), 1)
|
---|
73 |
|
---|
74 | glTexCoord2f(0.0, 1.0)
|
---|
75 | glVertex2f(-(w/h), 1)
|
---|
76 |
|
---|
77 | glEnd()
|
---|
78 |
|
---|
79 | glPopMatrix()
|
---|
80 |
|
---|
81 | def render3d(surface):
|
---|
82 | vertices = (
|
---|
83 | (1, -1, -1),
|
---|
84 | (1, 1, -1),
|
---|
85 | (-1, 1, -1),
|
---|
86 | (-1, -1, -1),
|
---|
87 | (1, -1, 1),
|
---|
88 | (1, 1, 1),
|
---|
89 | (-1, -1, 1),
|
---|
90 | (-1, 1, 1)
|
---|
91 | )
|
---|
92 | edges = (
|
---|
93 | (0,1),
|
---|
94 | (0,3),
|
---|
95 | (0,4),
|
---|
96 | (2,1),
|
---|
97 | (2,3),
|
---|
98 | (2,7),
|
---|
99 | (6,3),
|
---|
100 | (6,4),
|
---|
101 | (6,7),
|
---|
102 | (5,1),
|
---|
103 | (5,4),
|
---|
104 | (5,7)
|
---|
105 | )
|
---|
106 |
|
---|
107 | glPushMatrix()
|
---|
108 |
|
---|
109 | glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
|
---|
110 |
|
---|
111 | glTranslatef(0.0,0.0,-2*zNear)
|
---|
112 | glColor3f(1.0, 1.0, 1.0)
|
---|
113 |
|
---|
114 | glBegin(GL_LINES)
|
---|
115 | for edge in edges:
|
---|
116 | for vertex in edge:
|
---|
117 | glVertex3fv(vertices[vertex])
|
---|
118 | glEnd()
|
---|
119 |
|
---|
120 | glPopMatrix()
|
---|
121 |
|
---|
122 | system = platform.system()
|
---|
123 |
|
---|
124 | if system == 'Windows':
|
---|
125 | print("Windows detected")
|
---|
126 |
|
---|
127 | import ctypes
|
---|
128 | user32 = ctypes.windll.user32
|
---|
129 | w = user32.GetSystemMetrics(0)
|
---|
130 | h = user32.GetSystemMetrics(1)
|
---|
131 | elif system == 'Linux':
|
---|
132 | print("Linux detected")
|
---|
133 |
|
---|
134 | import tkinter
|
---|
135 | root = tkinter.Tk()
|
---|
136 | w = root.winfo_screenwidth()
|
---|
137 | h = root.winfo_screenheight()
|
---|
138 | elif system == 'Darwin':
|
---|
139 | print("Mac detected")
|
---|
140 |
|
---|
141 | from AppKit import NSScreen
|
---|
142 | res = NSScreen.mainScreen().frame().size
|
---|
143 | w = int(res.width)
|
---|
144 | h = int(res.height)
|
---|
145 | else:
|
---|
146 | print("Unknown OS: " + system)
|
---|
147 |
|
---|
148 | print("Detected native resolution: {:d}x{:d}".format(w, h))
|
---|
149 |
|
---|
150 | pygame.init()
|
---|
151 |
|
---|
152 | if w == 0 and h == 0:
|
---|
153 | print("Not using fullscreen")
|
---|
154 | gameDisplay = pygame.display.set_mode((800, 600))
|
---|
155 | else:
|
---|
156 | gameDisplay = pygame.display.set_mode((w, h), pygame.FULLSCREEN|pygame.DOUBLEBUF|pygame.OPENGL)
|
---|
157 |
|
---|
158 | overlay = pygame.Surface((w, h))
|
---|
159 |
|
---|
160 | pygame.display.set_caption('Space Game')
|
---|
161 |
|
---|
162 | clock = pygame.time.Clock()
|
---|
163 |
|
---|
164 | font = pygame.font.SysFont("comicsansms", 48)
|
---|
165 |
|
---|
166 | running = True
|
---|
167 | state = False
|
---|
168 |
|
---|
169 | zNear = 1.0/math.tan(math.radians(45.0/2))
|
---|
170 | gluPerspective(45, (w/h), zNear, 500.0)
|
---|
171 |
|
---|
172 | '''
|
---|
173 | # Use glFrustum to focus the camera at a point other than the screen center
|
---|
174 | zNear = 1.0
|
---|
175 | glFrustum(-1.5, 0.5, -1.0, 1.0, zNear, 500.0)
|
---|
176 | glTranslatef(0.0,0.0,-5.0)
|
---|
177 | '''
|
---|
178 |
|
---|
179 | while running:
|
---|
180 | for event in pygame.event.get():
|
---|
181 | if event.type == pygame.QUIT:
|
---|
182 | running = False
|
---|
183 | elif event.type == pygame.KEYDOWN:
|
---|
184 | if event.key == pygame.K_ESCAPE:
|
---|
185 | running = False
|
---|
186 | elif event.key == pygame.K_SPACE:
|
---|
187 | state = not state
|
---|
188 |
|
---|
189 | #print(event)
|
---|
190 |
|
---|
191 | render(gameDisplay, overlay, font, state)
|
---|
192 |
|
---|
193 | pygame.display.flip()
|
---|
194 | clock.tick(60)
|
---|
195 |
|
---|
196 | pygame.quit()
|
---|
197 | print("Game ended")
|
---|
198 | quit()
|
---|