source: python-game/game.py@ a7d1e5e

Last change on this file since a7d1e5e was c8cc13f, checked in by Dmitry Portnoy <dmp1488@…>, 8 years ago

Fix the bug with the incorrect color for the 3d rendering and slightly restructure the rendering code

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