[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 |
|
---|
[1af0f6d] | 13 | # I should really figure out which attributes in the 2D rendering
|
---|
| 14 | # break the 3d rendering and reset those back instead of resetting
|
---|
| 15 | # all attributes
|
---|
[c8cc13f] | 16 | glPushAttrib(GL_ALL_ATTRIB_BITS)
|
---|
| 17 | render3d(surface)
|
---|
| 18 | glPopAttrib(GL_ALL_ATTRIB_BITS)
|
---|
| 19 |
|
---|
| 20 | if showOverlay:
|
---|
[e79c833] | 21 | renderOverlay(overlay, font)
|
---|
[c8cc13f] | 22 |
|
---|
[1af0f6d] | 23 | glPushAttrib(GL_ALL_ATTRIB_BITS)
|
---|
[e79c833] | 24 | projectOverlay(surface, overlay)
|
---|
[1af0f6d] | 25 | glPopAttrib(GL_ALL_ATTRIB_BITS)
|
---|
[e79c833] | 26 |
|
---|
| 27 |
|
---|
| 28 | def renderOverlay(overlay, font):
|
---|
| 29 | #pygame.draw.rect(overlay, (0, 0, 255), pygame.Rect(0, 0, w, h))
|
---|
| 30 | #pygame.draw.circle(overlay, (255, 0, 0), (int(w/2), int(h/2)), 540)
|
---|
| 31 | #overlay.set_colorkey((255,0,0))
|
---|
[c8cc13f] | 32 | overlay.fill((0, 0, 0, 255))
|
---|
[e79c833] | 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 |
|
---|
| 39 | def 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))
|
---|
[c8cc13f] | 54 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData)
|
---|
[e79c833] | 55 |
|
---|
| 56 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
|
---|
| 57 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
|
---|
| 58 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
|
---|
| 59 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
|
---|
| 60 |
|
---|
| 61 | glBegin(GL_QUADS)
|
---|
| 62 |
|
---|
| 63 | glTexCoord2f(0.0, 0.0)
|
---|
| 64 | glVertex2f(-(w/h), -1)
|
---|
| 65 |
|
---|
| 66 | glTexCoord2f(1.0, 0.0)
|
---|
| 67 | glVertex2f((w/h), -1)
|
---|
| 68 |
|
---|
| 69 | glTexCoord2f(1.0, 1.0)
|
---|
| 70 | glVertex2f((w/h), 1)
|
---|
| 71 |
|
---|
| 72 | glTexCoord2f(0.0, 1.0)
|
---|
| 73 | glVertex2f(-(w/h), 1)
|
---|
| 74 |
|
---|
| 75 | glEnd()
|
---|
| 76 |
|
---|
| 77 | glPopMatrix()
|
---|
| 78 |
|
---|
| 79 | def render3d(surface):
|
---|
| 80 | vertices = (
|
---|
| 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 | (-1, 1, 1)
|
---|
| 89 | )
|
---|
| 90 | edges = (
|
---|
| 91 | (0,1),
|
---|
| 92 | (0,3),
|
---|
| 93 | (0,4),
|
---|
| 94 | (2,1),
|
---|
| 95 | (2,3),
|
---|
| 96 | (2,7),
|
---|
| 97 | (6,3),
|
---|
| 98 | (6,4),
|
---|
| 99 | (6,7),
|
---|
| 100 | (5,1),
|
---|
| 101 | (5,4),
|
---|
| 102 | (5,7)
|
---|
| 103 | )
|
---|
| 104 |
|
---|
| 105 | glPushMatrix()
|
---|
| 106 |
|
---|
| 107 | glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
|
---|
| 108 |
|
---|
| 109 | glTranslatef(0.0,0.0,-2*zNear)
|
---|
| 110 | glColor3f(1.0, 1.0, 1.0)
|
---|
| 111 |
|
---|
| 112 | glBegin(GL_LINES)
|
---|
| 113 | for edge in edges:
|
---|
| 114 | for vertex in edge:
|
---|
| 115 | glVertex3fv(vertices[vertex])
|
---|
| 116 | glEnd()
|
---|
| 117 |
|
---|
| 118 | glPopMatrix()
|
---|
| 119 |
|
---|
| 120 | system = platform.system()
|
---|
| 121 |
|
---|
| 122 | if system == 'Windows':
|
---|
| 123 | print("Windows detected")
|
---|
| 124 |
|
---|
| 125 | import ctypes
|
---|
| 126 | user32 = ctypes.windll.user32
|
---|
| 127 | w = user32.GetSystemMetrics(0)
|
---|
| 128 | h = user32.GetSystemMetrics(1)
|
---|
| 129 | elif system == 'Linux':
|
---|
| 130 | print("Linux detected")
|
---|
| 131 |
|
---|
| 132 | import tkinter
|
---|
| 133 | root = tkinter.Tk()
|
---|
| 134 | w = root.winfo_screenwidth()
|
---|
| 135 | h = root.winfo_screenheight()
|
---|
| 136 | elif system == 'Darwin':
|
---|
| 137 | print("Mac detected")
|
---|
| 138 |
|
---|
| 139 | from AppKit import NSScreen
|
---|
| 140 | res = NSScreen.mainScreen().frame().size
|
---|
| 141 | w = int(res.width)
|
---|
| 142 | h = int(res.height)
|
---|
| 143 | else:
|
---|
| 144 | print("Unknown OS: " + system)
|
---|
| 145 |
|
---|
| 146 | print("Detected native resolution: {:d}x{:d}".format(w, h))
|
---|
| 147 |
|
---|
| 148 | pygame.init()
|
---|
| 149 |
|
---|
| 150 | if w == 0 and h == 0:
|
---|
| 151 | print("Not using fullscreen")
|
---|
| 152 | gameDisplay = pygame.display.set_mode((800, 600))
|
---|
| 153 | else:
|
---|
| 154 | gameDisplay = pygame.display.set_mode((w, h), pygame.FULLSCREEN|pygame.DOUBLEBUF|pygame.OPENGL)
|
---|
| 155 |
|
---|
[c8cc13f] | 156 | # Look into pygame.HWSURFACE for hardware-accelerated surfaces
|
---|
| 157 | overlay = pygame.Surface((w, h), pygame.SRCALPHA)
|
---|
[e79c833] | 158 |
|
---|
| 159 | pygame.display.set_caption('Space Game')
|
---|
| 160 |
|
---|
| 161 | clock = pygame.time.Clock()
|
---|
| 162 |
|
---|
| 163 | font = pygame.font.SysFont("comicsansms", 48)
|
---|
| 164 |
|
---|
| 165 | running = True
|
---|
[c8cc13f] | 166 | showOverlay = False
|
---|
[e79c833] | 167 |
|
---|
| 168 | zNear = 1.0/math.tan(math.radians(45.0/2))
|
---|
| 169 | gluPerspective(45, (w/h), zNear, 500.0)
|
---|
| 170 |
|
---|
| 171 | '''
|
---|
| 172 | # Use glFrustum to focus the camera at a point other than the screen center
|
---|
| 173 | zNear = 1.0
|
---|
| 174 | glFrustum(-1.5, 0.5, -1.0, 1.0, zNear, 500.0)
|
---|
| 175 | glTranslatef(0.0,0.0,-5.0)
|
---|
| 176 | '''
|
---|
| 177 |
|
---|
| 178 | while running:
|
---|
| 179 | for event in pygame.event.get():
|
---|
| 180 | if event.type == pygame.QUIT:
|
---|
| 181 | running = False
|
---|
| 182 | elif event.type == pygame.KEYDOWN:
|
---|
| 183 | if event.key == pygame.K_ESCAPE:
|
---|
| 184 | running = False
|
---|
| 185 | elif event.key == pygame.K_SPACE:
|
---|
[c8cc13f] | 186 | showOverlay = not showOverlay
|
---|
[e79c833] | 187 |
|
---|
[c8cc13f] | 188 | render(gameDisplay, overlay, font, showOverlay)
|
---|
[e79c833] | 189 |
|
---|
| 190 | pygame.display.flip()
|
---|
| 191 | clock.tick(60)
|
---|
| 192 |
|
---|
| 193 | pygame.quit()
|
---|
| 194 | print("Game ended")
|
---|
| 195 | quit()
|
---|