[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]
|
---|
| 9 |
|
---|
[c8cc13f] | 10 | def render(surface, overlay, font, showOverlay):
|
---|
[e79c833] | 11 | #glEnable (GL_BLEND); glBlendFunc (GL_ONE, GL_ONE);
|
---|
| 12 |
|
---|
[b2d384b] | 13 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 14 |
|
---|
[c8cc13f] | 15 | render3d(surface)
|
---|
| 16 |
|
---|
| 17 | if showOverlay:
|
---|
[e79c833] | 18 | renderOverlay(overlay, font)
|
---|
| 19 | projectOverlay(surface, overlay)
|
---|
| 20 |
|
---|
| 21 |
|
---|
| 22 | def 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))
|
---|
[c8cc13f] | 26 | overlay.fill((0, 0, 0, 255))
|
---|
[e79c833] | 27 |
|
---|
| 28 | text = font.render("2D drawing", True, (0, 128, 0))
|
---|
| 29 |
|
---|
| 30 | pygame.draw.rect(overlay, (0, 128, 255), pygame.Rect(30, 30, 60, 60))
|
---|
[b2d384b] | 31 | pygame.draw.rect(overlay, (128, 255, 0), pygame.Rect(300, 300, 1000, 1000))
|
---|
| 32 |
|
---|
[e79c833] | 33 | overlay.blit(text, (500, 100))
|
---|
| 34 |
|
---|
| 35 | def projectOverlay(surface, overlay):
|
---|
| 36 |
|
---|
| 37 | glPushMatrix()
|
---|
[1af0f6d] | 38 |
|
---|
[b2d384b] | 39 | glColor3f(1.0, 1.0, 1.0)
|
---|
[1af0f6d] | 40 |
|
---|
[e79c833] | 41 | glTranslatef(0.0,0.0,-zNear)
|
---|
| 42 |
|
---|
| 43 | textureData = pygame.image.tostring(overlay, "RGBA", 1)
|
---|
| 44 | width = overlay.get_width()
|
---|
| 45 | height = overlay.get_height()
|
---|
| 46 |
|
---|
| 47 | glEnable(GL_TEXTURE_2D)
|
---|
| 48 |
|
---|
| 49 | glBindTexture(GL_TEXTURE_2D, glGenTextures(1))
|
---|
[c8cc13f] | 50 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData)
|
---|
[e79c833] | 51 |
|
---|
| 52 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
|
---|
| 53 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
|
---|
| 54 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
|
---|
| 55 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
|
---|
| 56 |
|
---|
| 57 | glBegin(GL_QUADS)
|
---|
| 58 |
|
---|
| 59 | glTexCoord2f(0.0, 0.0)
|
---|
| 60 | glVertex2f(-(w/h), -1)
|
---|
| 61 |
|
---|
| 62 | glTexCoord2f(1.0, 0.0)
|
---|
| 63 | glVertex2f((w/h), -1)
|
---|
| 64 |
|
---|
| 65 | glTexCoord2f(1.0, 1.0)
|
---|
| 66 | glVertex2f((w/h), 1)
|
---|
| 67 |
|
---|
| 68 | glTexCoord2f(0.0, 1.0)
|
---|
| 69 | glVertex2f(-(w/h), 1)
|
---|
| 70 |
|
---|
| 71 | glEnd()
|
---|
[b2d384b] | 72 |
|
---|
| 73 | glDisable(GL_TEXTURE_2D)
|
---|
[e79c833] | 74 |
|
---|
| 75 | glPopMatrix()
|
---|
| 76 |
|
---|
| 77 | def render3d(surface):
|
---|
| 78 | vertices = (
|
---|
| 79 | (1, -1, -1),
|
---|
| 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 | )
|
---|
| 88 | edges = (
|
---|
| 89 | (0,1),
|
---|
| 90 | (0,3),
|
---|
| 91 | (0,4),
|
---|
| 92 | (2,1),
|
---|
| 93 | (2,3),
|
---|
| 94 | (2,7),
|
---|
| 95 | (6,3),
|
---|
| 96 | (6,4),
|
---|
| 97 | (6,7),
|
---|
| 98 | (5,1),
|
---|
| 99 | (5,4),
|
---|
| 100 | (5,7)
|
---|
| 101 | )
|
---|
| 102 |
|
---|
| 103 | glPushMatrix()
|
---|
[b2d384b] | 104 |
|
---|
| 105 | glColor3f(1.0, 0.0, 0.0)
|
---|
[e79c833] | 106 |
|
---|
| 107 | glTranslatef(0.0,0.0,-2*zNear)
|
---|
| 108 |
|
---|
| 109 | glBegin(GL_LINES)
|
---|
| 110 | for edge in edges:
|
---|
| 111 | for vertex in edge:
|
---|
| 112 | glVertex3fv(vertices[vertex])
|
---|
| 113 | glEnd()
|
---|
| 114 |
|
---|
| 115 | glPopMatrix()
|
---|
| 116 |
|
---|
| 117 | system = platform.system()
|
---|
| 118 |
|
---|
| 119 | if system == 'Windows':
|
---|
| 120 | print("Windows detected")
|
---|
| 121 |
|
---|
| 122 | import ctypes
|
---|
| 123 | user32 = ctypes.windll.user32
|
---|
| 124 | w = user32.GetSystemMetrics(0)
|
---|
| 125 | h = user32.GetSystemMetrics(1)
|
---|
| 126 | elif system == 'Linux':
|
---|
| 127 | print("Linux detected")
|
---|
| 128 |
|
---|
| 129 | import tkinter
|
---|
| 130 | root = tkinter.Tk()
|
---|
| 131 | w = root.winfo_screenwidth()
|
---|
| 132 | h = root.winfo_screenheight()
|
---|
| 133 | elif system == 'Darwin':
|
---|
| 134 | print("Mac detected")
|
---|
| 135 |
|
---|
| 136 | from AppKit import NSScreen
|
---|
| 137 | res = NSScreen.mainScreen().frame().size
|
---|
| 138 | w = int(res.width)
|
---|
| 139 | h = int(res.height)
|
---|
| 140 | else:
|
---|
| 141 | print("Unknown OS: " + system)
|
---|
| 142 |
|
---|
| 143 | print("Detected native resolution: {:d}x{:d}".format(w, h))
|
---|
| 144 |
|
---|
| 145 | pygame.init()
|
---|
| 146 |
|
---|
| 147 | if w == 0 and h == 0:
|
---|
| 148 | print("Not using fullscreen")
|
---|
| 149 | gameDisplay = pygame.display.set_mode((800, 600))
|
---|
| 150 | else:
|
---|
| 151 | gameDisplay = pygame.display.set_mode((w, h), pygame.FULLSCREEN|pygame.DOUBLEBUF|pygame.OPENGL)
|
---|
| 152 |
|
---|
[c8cc13f] | 153 | # Look into pygame.HWSURFACE for hardware-accelerated surfaces
|
---|
| 154 | overlay = pygame.Surface((w, h), pygame.SRCALPHA)
|
---|
[e79c833] | 155 |
|
---|
| 156 | pygame.display.set_caption('Space Game')
|
---|
| 157 |
|
---|
| 158 | clock = pygame.time.Clock()
|
---|
| 159 |
|
---|
| 160 | font = pygame.font.SysFont("comicsansms", 48)
|
---|
| 161 |
|
---|
| 162 | running = True
|
---|
[c8cc13f] | 163 | showOverlay = False
|
---|
[e79c833] | 164 |
|
---|
| 165 | zNear = 1.0/math.tan(math.radians(45.0/2))
|
---|
[b2d384b] | 166 |
|
---|
| 167 | # not really need here since there are no previous
|
---|
| 168 | # matrix modifications, but keeping it for reference
|
---|
| 169 | #glLoadIdentity()
|
---|
[e79c833] | 170 | gluPerspective(45, (w/h), zNear, 500.0)
|
---|
| 171 |
|
---|
| 172 | '''
|
---|
| 173 | # Use glFrustum to focus the camera at a point other than the screen center
|
---|
| 174 | zNear = 1.0
|
---|
| 175 | glFrustum(-1.5, 0.5, -1.0, 1.0, zNear, 500.0)
|
---|
| 176 | glTranslatef(0.0,0.0,-5.0)
|
---|
| 177 | '''
|
---|
| 178 |
|
---|
| 179 | while 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:
|
---|
[c8cc13f] | 187 | showOverlay = not showOverlay
|
---|
[e79c833] | 188 |
|
---|
[c8cc13f] | 189 | render(gameDisplay, overlay, font, showOverlay)
|
---|
[e79c833] | 190 |
|
---|
| 191 | pygame.display.flip()
|
---|
| 192 | clock.tick(60)
|
---|
| 193 |
|
---|
| 194 | pygame.quit()
|
---|
| 195 | print("Game ended")
|
---|
| 196 | quit()
|
---|