[e79c833] | 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]
|
---|
[2876668] | 9 | fullscreen = True
|
---|
[e79c833] | 10 |
|
---|
[c8cc13f] | 11 | def render(surface, overlay, font, showOverlay):
|
---|
[e79c833] | 12 | #glEnable (GL_BLEND); glBlendFunc (GL_ONE, GL_ONE);
|
---|
| 13 |
|
---|
[b2d384b] | 14 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 15 |
|
---|
[c8cc13f] | 16 | render3d(surface)
|
---|
| 17 |
|
---|
| 18 | if showOverlay:
|
---|
[e79c833] | 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))
|
---|
[c8cc13f] | 27 | overlay.fill((0, 0, 0, 255))
|
---|
[e79c833] | 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))
|
---|
[b2d384b] | 32 | pygame.draw.rect(overlay, (128, 255, 0), pygame.Rect(300, 300, 1000, 1000))
|
---|
| 33 |
|
---|
[e79c833] | 34 | overlay.blit(text, (500, 100))
|
---|
| 35 |
|
---|
| 36 | def projectOverlay(surface, overlay):
|
---|
| 37 |
|
---|
| 38 | glPushMatrix()
|
---|
[1af0f6d] | 39 |
|
---|
[b2d384b] | 40 | glColor3f(1.0, 1.0, 1.0)
|
---|
[1af0f6d] | 41 |
|
---|
[e79c833] | 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))
|
---|
[c8cc13f] | 51 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData)
|
---|
[e79c833] | 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()
|
---|
[b2d384b] | 73 |
|
---|
| 74 | glDisable(GL_TEXTURE_2D)
|
---|
[e79c833] | 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()
|
---|
[b2d384b] | 105 |
|
---|
[114fd09] | 106 | glColor3f(0.5, 0.5, 1.0)
|
---|
[e79c833] | 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 |
|
---|
[2876668] | 148 | if (w == 0 and h == 0) or not fullscreen:
|
---|
[e79c833] | 149 | print("Not using fullscreen")
|
---|
[2876668] | 150 | [w, h] = [800, 600]
|
---|
| 151 | gameDisplay = pygame.display.set_mode((w, h), pygame.DOUBLEBUF|pygame.OPENGL)
|
---|
[185faa0] | 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)
|
---|
[e79c833] | 156 | else:
|
---|
| 157 | gameDisplay = pygame.display.set_mode((w, h), pygame.FULLSCREEN|pygame.DOUBLEBUF|pygame.OPENGL)
|
---|
| 158 |
|
---|
[c8cc13f] | 159 | # Look into pygame.HWSURFACE for hardware-accelerated surfaces
|
---|
| 160 | overlay = pygame.Surface((w, h), pygame.SRCALPHA)
|
---|
[e79c833] | 161 |
|
---|
| 162 | pygame.display.set_caption('Space Game')
|
---|
| 163 |
|
---|
| 164 | clock = pygame.time.Clock()
|
---|
| 165 |
|
---|
[114fd09] | 166 | font = pygame.font.Font(None, 36)
|
---|
[e79c833] | 167 |
|
---|
| 168 | running = True
|
---|
[c8cc13f] | 169 | showOverlay = False
|
---|
[e79c833] | 170 |
|
---|
| 171 | zNear = 1.0/math.tan(math.radians(45.0/2))
|
---|
[b2d384b] | 172 |
|
---|
| 173 | # not really need here since there are no previous
|
---|
| 174 | # matrix modifications, but keeping it for reference
|
---|
| 175 | #glLoadIdentity()
|
---|
[e79c833] | 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:
|
---|
[c8cc13f] | 193 | showOverlay = not showOverlay
|
---|
[e79c833] | 194 |
|
---|
[c8cc13f] | 195 | render(gameDisplay, overlay, font, showOverlay)
|
---|
[e79c833] | 196 |
|
---|
| 197 | pygame.display.flip()
|
---|
| 198 | clock.tick(60)
|
---|
| 199 |
|
---|
| 200 | pygame.quit()
|
---|
| 201 | print("Game ended")
|
---|
| 202 | quit()
|
---|