Skip to content

Instantly share code, notes, and snippets.

@oakwhiz
Created March 23, 2013 08:54
Show Gist options
  • Save oakwhiz/5227057 to your computer and use it in GitHub Desktop.
Save oakwhiz/5227057 to your computer and use it in GitHub Desktop.
Renders triangles in increasing number until fps drops too much
from __future__ import division
from random import uniform
from pyglet import clock, font, image, window
from pyglet.gl import *
class glBeginEnd:
def __init__(self, gl_enum):
self.gl_enum = gl_enum
def __enter__(self):
glBegin(self.gl_enum)
return self.gl_enum
def __exit__(self, type, value, traceback):
glEnd()
class Entity(object):
def __init__(self, id, size, x, y, rot):
self.id = id
self.size = size
self.x = x
self.y = y
self.rot = rot
def draw(self):
glLoadIdentity()
glTranslatef(self.x, self.y, 0.0)
glRotatef(self.rot, 0, 0, 1)
glScalef(self.size, self.size, 1.0)
if (self.rot % 5 < 1):
glPolygonMode( GL_FRONT_AND_BACK, GL_LINE )
with glBeginEnd(GL_TRIANGLES):
glColor4f(1.0, 0.0, 0.0, 0.0)
glVertex2f(0.0, 0.5)
glColor4f(0.0, 0.0, 1.0, 1.0)
glVertex2f(0.2, -0.5)
glColor4f(0.0, 0.0, 1.0, 1.0)
glVertex2f(-0.2, -0.5)
glPolygonMode( GL_FRONT_AND_BACK, GL_FILL )
class World(object):
def __init__(self):
self.ents = {}
self.nextEntId = 0
#clock.schedule_interval(self.spawnEntity, 0.02)
def spawnEntity(self, dt):
size = uniform(1.0, 100.0)
x = uniform(-100.0, 100.0)
y = uniform(-100.0, 100.0)
rot = uniform(0.0, 360.0)
ent = Entity(self.nextEntId, size, x, y, rot)
self.ents[ent.id] = ent
self.nextEntId += 1
return ent
def tick(self):
if (clock.get_fps() >= 60):
self.spawnEntity(0)
for ent in self.ents.values():
ent.rot += 10.0 / ent.size
def draw(self):
glClear(GL_COLOR_BUFFER_BIT)
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
for ent in self.ents.values():
ent.draw()
class Camera(object):
def __init__(self, win, x=0.0, y=0.0, rot=0.0, zoom=1.0):
self.win = win
self.x = x
self.y = y
self.rot = rot
self.zoom = zoom
def worldProjection(self):
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
widthRatio = self.win.width / self.win.height
gluOrtho2D(
-self.zoom * widthRatio,
self.zoom * widthRatio,
-self.zoom,
self.zoom)
def hudProjection(self):
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluOrtho2D(0, self.win.width, 0, self.win.height)
class Hud(object):
def __init__(self, win):
self.win = win
self.helv = font.load('Ubuntu', self.win.width / 15.0)
self.setCount(0)
self.fps = clock.ClockDisplay(font=self.helv)
def setCount(self,count):
self.count = count
self.text = font.Text(
self.helv,
str(self.count)+' objects',
x=self.win.width / 2,
y=self.win.height / 2,
halign=font.Text.CENTER,
valign=font.Text.CENTER,
color=(1, 1, 1, 0.5),
)
def draw(self):
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
self.text.draw()
self.fps.draw()
class App(object):
def __init__(self):
self.world = World()
self.win = window.Window(fullscreen=True, vsync=False)
self.win.set_exclusive_mouse()
self.win.set_exclusive_keyboard()
self.win.set_vsync(False)
self.camera = Camera(self.win, zoom=100.0)
self.hud = Hud(self.win)
clock.set_fps_limit(None)
def mainLoop(self):
while not self.win.has_exit:
self.win.dispatch_events()
self.world.tick()
self.camera.worldProjection()
self.world.draw()
self.camera.hudProjection()
self.hud.setCount(len(self.world.ents.values()))
self.hud.draw()
clock.tick()
self.win.flip()
app = App()
app.mainLoop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment