source: python-game/game.py@ 1af0f6d

Last change on this file since 1af0f6d was 1af0f6d, checked in by Dmitry Portnoy <dmp1488@…>, 7 years ago

Fix the issue where the 3d scene takes on the color of the 2d scene

  • Property mode set to 100644
File size: 4.4 KB
RevLine 
[e79c833]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, state):
11 #surface.fill((0, 0, 0))
12
13 #glEnable (GL_BLEND); glBlendFunc (GL_ONE, GL_ONE);
14
[1af0f6d]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
[e79c833]18 if state:
19 renderOverlay(overlay, font)
[1af0f6d]20 glPushAttrib(GL_ALL_ATTRIB_BITS)
[e79c833]21 projectOverlay(surface, overlay)
[1af0f6d]22 glPopAttrib(GL_ALL_ATTRIB_BITS)
[e79c833]23 else:
[1af0f6d]24 glPushAttrib(GL_ALL_ATTRIB_BITS)
[e79c833]25 render3d(surface)
[1af0f6d]26 glPopAttrib(GL_ALL_ATTRIB_BITS)
[e79c833]27
28
29def 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
39def projectOverlay(surface, overlay):
40
41 glPushMatrix()
[1af0f6d]42
43 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
44
[e79c833]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
81def 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
122system = platform.system()
123
124if 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)
131elif system == 'Linux':
132 print("Linux detected")
133
134 import tkinter
135 root = tkinter.Tk()
136 w = root.winfo_screenwidth()
137 h = root.winfo_screenheight()
138elif 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)
145else:
146 print("Unknown OS: " + system)
147
148print("Detected native resolution: {:d}x{:d}".format(w, h))
149
150pygame.init()
151
152if w == 0 and h == 0:
153 print("Not using fullscreen")
154 gameDisplay = pygame.display.set_mode((800, 600))
155else:
156 gameDisplay = pygame.display.set_mode((w, h), pygame.FULLSCREEN|pygame.DOUBLEBUF|pygame.OPENGL)
157
158overlay = pygame.Surface((w, h))
159
160pygame.display.set_caption('Space Game')
161
162clock = pygame.time.Clock()
163
164font = pygame.font.SysFont("comicsansms", 48)
165
166running = True
167state = False
168
169zNear = 1.0/math.tan(math.radians(45.0/2))
170gluPerspective(45, (w/h), zNear, 500.0)
171
172'''
173# Use glFrustum to focus the camera at a point other than the screen center
174zNear = 1.0
175glFrustum(-1.5, 0.5, -1.0, 1.0, zNear, 500.0)
176glTranslatef(0.0,0.0,-5.0)
177'''
178
179while 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
196pygame.quit()
197print("Game ended")
198quit()
Note: See TracBrowser for help on using the repository browser.