source: python-game/game.py@ 2876668

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

Make windowed mode work again and add a flag to enable it

  • 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]
9fullscreen = True
10
11def 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
23def 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
36def 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
78def 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
118system = platform.system()
119
120if 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)
127elif system == 'Linux':
128 print("Linux detected")
129
130 import tkinter
131 root = tkinter.Tk()
132 w = root.winfo_screenwidth()
133 h = root.winfo_screenheight()
134elif 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)
141else:
142 print("Unknown OS: " + system)
143
144print("Detected native resolution: {:d}x{:d}".format(w, h))
145
146pygame.init()
147
148if (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)
152else:
153 gameDisplay = pygame.display.set_mode((w, h), pygame.FULLSCREEN|pygame.DOUBLEBUF|pygame.OPENGL)
154
155# Look into pygame.HWSURFACE for hardware-accelerated surfaces
156overlay = pygame.Surface((w, h), pygame.SRCALPHA)
157
158pygame.display.set_caption('Space Game')
159
160clock = pygame.time.Clock()
161
162font = pygame.font.Font(None, 36)
163
164running = True
165showOverlay = False
166
167zNear = 1.0/math.tan(math.radians(45.0/2))
168
169# not really need here since there are no previous
170# matrix modifications, but keeping it for reference
171#glLoadIdentity()
172gluPerspective(45, (w/h), zNear, 500.0)
173
174'''
175# Use glFrustum to focus the camera at a point other than the screen center
176zNear = 1.0
177glFrustum(-1.5, 0.5, -1.0, 1.0, zNear, 500.0)
178glTranslatef(0.0,0.0,-5.0)
179'''
180
181while running:
182 for event in pygame.event.get():
183 if event.type == pygame.QUIT:
184 running = False
185 elif event.type == pygame.KEYDOWN:
186 if event.key == pygame.K_ESCAPE:
187 running = False
188 elif event.key == pygame.K_SPACE:
189 showOverlay = not showOverlay
190
191 render(gameDisplay, overlay, font, showOverlay)
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.