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