source: python-game/game.py@ 3511899

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

Add basic OpenGL (along with the ability to still use 2d pygame functions) and convert game.py to UNIX-style line endings

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