def __init__(self, window, on_finished=None): global current_screen if current_screen: current_screen.end() current_screen = self handlers = {} global serial_number serial_number += 1 self.serial_number = serial_number self.log("__init__. large and in charge.") for k in dir(self): if k.startswith('on_'): handlers[k] = getattr(self, k) self.window = window self.on_finished = on_finished self.load() self.batch = pyglet.graphics.Batch() self.t = 0 self.clock = clock.Clock(time_function=self._time) clock.schedule(self._tick) self.start() self.log(">>>> push handlers >>>>") window.push_handlers(**handlers)
def test_fps(self): clock.set_default(clock.Clock()) self.assertTrue(clock.get_fps() == 0) for i in range(10): time.sleep(0.2) clock.tick() result = clock.get_fps() self.assertTrue(abs(result - 5.0) < 0.05)
def test_tick(self): clock.set_default(clock.Clock()) result = clock.tick() self.assertTrue(result == 0) time.sleep(1) result_1 = clock.tick() time.sleep(1) result_2 = clock.tick() self.assertTrue(abs(result_1 - 1.0) < 0.05) self.assertTrue(abs(result_2 - 1.0) < 0.05)
def test_fps_limit(self): clock.set_default(clock.Clock()) clock.set_fps_limit(20) self.assertTrue(clock.get_fps() == 0) t1 = time.time() clock.tick() # One to get it going for i in range(20): clock.tick() t2 = time.time() self.assertTrue(abs((t2 - t1) - 1.) < 0.05)
def test_schedule_multiple(self): clock.set_default(clock.Clock()) clock.schedule(self.callback) clock.schedule(self.callback) self.callback_count = 0 clock.tick() self.assertTrue(self.callback_count == 2) clock.tick() self.assertTrue(self.callback_count == 4)
def __init__(self, *a, **ka): ka.update(width=1024, height=768, caption="Embedded Test", resizable=False, vsync=False, screen=display.get_screens()[0]) super().__init__(*a, **ka) self.fps_display = FPSDisplay(self) self.set_minimum_size(1024, 768) glPolygonMode(GL_FRONT, GL_FILL) glPolygonMode(GL_BACK, GL_FILL) glLineWidth(3) glDisable(GL_DEPTH_TEST) glDisable(GL_LIGHTING) glClearColor(1, 1, 1, 1) glMatrixMode(GL_MODELVIEW) glLoadIdentity() glMatrixMode(GL_PROJECTION) glLoadIdentity() glFlush() self.hint_doc = pyglet.text.decode_html( '<h1>Server Starting...<h1><br><h2>Press <SPACE> to start test</h2><h3>IP:PORT->xxx.xxx.xxx.xxx:xxxx</h3><br>Current Clients:[]', ) self.hint_label = pyglet.text.layout.TextLayout(self.hint_doc, self.width, self.height, multiline=True, wrap_lines=True) ''' font_name='Times New Roman', font_size=15, x=self.width//2, y=self.height//2, multiline=True, width=self.width, anchor_x='center', anchor_y='center',''' self.pid_label = pyglet.text.Label('', font_name='Times New Roman', font_size=15, color=(0, 0, 0, 255), x=20, y=self.height - 310) self.fps_label = pyglet.text.Label('', font_name='Times New Roman', font_size=15, color=(0, 0, 0, 255), x=20, y=self.height - 330) self.init_all_reg() self.qr = None # clock.schedule(self.tick) self.a_pps = clock.Clock()
def test_unschedule(self): clock.set_default(clock.Clock()) clock.schedule(self.callback) result = clock.tick() self.assertTrue(result == self.callback_dt) self.callback_dt = None time.sleep(1) clock.unschedule(self.callback) result = clock.tick() self.assertTrue(self.callback_dt == None)
def test_schedule_once(self): self.clear() clock.set_default(clock.Clock()) clock.schedule_once(self.callback_1, 0.1) clock.schedule_once(self.callback_2, 0.35) clock.schedule_once(self.callback_3, 0.07) t = 0 while t < 1: t += clock.tick() self.assertTrue(self.callback_1_count == 1) self.assertTrue(self.callback_2_count == 1) self.assertTrue(self.callback_3_count == 1)
def test_schedule_interval(self): self.clear() clock.set_default(clock.Clock()) clock.schedule_interval(self.callback_1, 0.1) clock.schedule_interval(self.callback_2, 0.35) clock.schedule_interval(self.callback_3, 0.07) t = 0 while t < 2.04: # number chosen to avoid +/- 1 errors in div t += clock.tick() self.assertTrue(self.callback_1_count == int(t / 0.1)) self.assertTrue(self.callback_2_count == int(t / 0.35)) self.assertTrue(self.callback_3_count == int(t / 0.07))
def setUp(self): # since clock is global, # we initialize a new clock on every test clock.set_default(clock.Clock())
def main(): screen_width = 800 screen_height = 600 window = pyglet.window.Window(screen_width, screen_height) cparams = TextureParam(wrap = GL_CLAMP) buf = FrameBuffer(screen_width, screen_height, Surface(Surface.SURF_COLOUR, gl_tgt=GL_TEXTURE_RECTANGLE_ARB, params=cparams), Surface(Surface.SURF_DEPTH, gl_tgt=GL_TEXTURE_RECTANGLE_ARB, gl_fmt=GL_DEPTH_COMPONENT32_ARB, is_texture=True, is_mipmapped=False, params=cparams)) buf.init() buf.attach() buf.unbind() alpha_buf = FrameBuffer(screen_width, screen_height, Surface(Surface.SURF_COLOUR, gl_tgt=GL_TEXTURE_RECTANGLE_ARB, params = cparams)) alpha_buf.init() alpha_buf.attach() alpha_buf.unbind() buf_subsampled = FrameBuffer(200, 150, Surface(Surface.SURF_COLOUR, gl_tgt=GL_TEXTURE_RECTANGLE_ARB, params=cparams), Surface(Surface.SURF_DEPTH, params=cparams)) buf_subsampled.init() buf_subsampled.attach() buf_subsampled.unbind() buf_subsampled2 = FrameBuffer(200, 150, Surface(Surface.SURF_COLOUR, gl_tgt=GL_TEXTURE_RECTANGLE_ARB, params=cparams), Surface(Surface.SURF_DEPTH, params=cparams)) buf_subsampled2.init() buf_subsampled2.attach() buf_subsampled2.unbind() object = cube_array_list() downsampler=DownsamplerRect() noise=gaussNoise(32) blur=GaussianRect3() pass1=RenderDOFPass1() pass2=RenderDOFPass2() r = 0 clk = clock.Clock(60) while not window.has_exit: clk.tick() window.dispatch_events() buf.bind() glViewport(0, 0, screen_width, screen_height) r += 1 if r > 360: r = 0 renderScene(r, object) buf.unbind() downsample(buf.colourBuffer(0), buf_subsampled, downsampler, noise) gaussianBlur(buf_subsampled.colourBuffer(0), buf_subsampled2, buf_subsampled, 0.0, 0.0, 200.0, 150.0, blur) renderDOF(buf, alpha_buf, buf_subsampled, pass1, pass2, noise) #fps.draw(window, clk) window.flip() buf = None buf_subsampled = None
class Console: def __init__(self, width, height, globals=None, locals=None): self.font = pyglet.text.default_font_factory.get_font( 'bitstream vera sans mono', 12) self.lines = list() self.buffer = '' self.pre_buffer = '' self.prompt = '>>> ' self.prompt2 = '... ' self.globals = globals self.locals = locals self.write_pending = '' self.width, self.height = (width, height) self.max_lines = self.height / self.font.glyph_height - 1 self.write('pyglet command console\n') self.write('Version %s\n' % __version__) def on_key_press(self, symbol, modifiers): # TODO cursor control / line editing if modifiers & key.key.MOD_CTRL and symbol == key.key.C: self.buffer = '' self.pre_buffer = '' return if symbol == key.key.ENTER: self.write('%s%s\n' % (self.get_prompt(), self.buffer)) self.execute(self.pre_buffer + self.buffer) self.buffer = '' return if symbol == key.key.BACKSPACE: self.buffer = self.buffer[:-1] return return EVENT_UNHANDLED def on_text(self, text): if ' ' <= text <= '~': self.buffer += text if 0xae <= ord(text) <= 0xff: self.buffer += text def write(self, text): if self.write_pending: text = self.write_pending + text self.write_pending = '' if type(text) in (str, str): text = text.split('\n') if text[-1] != '': self.write_pending = text[-1] del text[-1] self.lines = [ pyglet.text.layout_text(line.strip(), font=self.font) for line in text ] + self.lines if len(self.lines) > self.max_lines: del self.lines[-1] def execute(self, input): old_stderr, old_stdout = sys.stderr, sys.stdout sys.stderr = sys.stdout = self try: c = code.compile_command(input, '<pyglet console>') if c is None: self.pre_buffer = '%s\n' % input else: self.pre_buffer = '' result = eval(c, self.globals, self.locals) if result is not None: self.write('%r\n' % result) except: traceback.print_exc() self.pre_buffer = '' sys.stderr = old_stderr sys.stdout = old_stdout def get_prompt(self): if self.pre_buffer: return self.prompt2 return self.prompt __last = None def draw(self): pyglet.text.begin() glPushMatrix() glTranslatef(0, self.height, 0) for line in self.lines[::-1]: line.draw() glTranslatef(0, -self.font.glyph_height, 0) line = self.get_prompt() + self.buffer if self.__last is None or line != self.__last[0]: self.__last = (line, pyglet.text.layout_text(line.strip(), font=self.font)) self.__last[1].draw() glPopMatrix() pyglet.text.end() from pyglet.window import * from pyglet.window.event import * from pyglet import clock w1 = Window(width=600, height=400) console = Console(w1.width, w1.height) w1.push_handlers(console) c = clock.Clock() glMatrixMode(GL_PROJECTION) glLoadIdentity() glOrtho(0, w1.width, 0, w1.height, -1, 1) glEnable(GL_COLOR_MATERIAL) glMatrixMode(GL_MODELVIEW) glClearColor(1, 1, 1, 1) while not w1.has_exit: c.set_fps(60) w1.dispatch_events() glClear(GL_COLOR_BUFFER_BIT) console.draw() w1.flip()
from pyglet import clock from ..annotations import skip_if_continuous_integration def sleep(seconds): """Busy sleep on the CPU which is very precise""" pyclock = clock.get_default() start = pyclock.time() while pyclock.time() - start < seconds: pass # since clock is global, we initialize a new clock on every test clock.set_default(clock.Clock()) def test_first_tick_is_delta_zero(): """ Tests that the first tick is dt = 0. """ dt = clock.tick() assert dt == 0 def test_start_at_zero_fps(): """ Tests that the default clock starts with zero fps. """
font=self.font)) self.__last[1].draw() glPopMatrix() pyglet.text.end() if __name__ == '__main__': from pyglet.window import * from pyglet.window.event import * from pyglet import clock w1 = Window(width=600, height=400) console = Console(w1.width, w1.height) w1.push_handlers(console) c = clock.Clock() glMatrixMode(GL_PROJECTION) glLoadIdentity() glOrtho(0, w1.width, 0, w1.height, -1, 1) glEnable(GL_COLOR_MATERIAL) glMatrixMode(GL_MODELVIEW) glClearColor(1, 1, 1, 1) while not w1.has_exit: c.set_fps(60) w1.dispatch_events() glClear(GL_COLOR_BUFFER_BIT) console.draw() w1.flip()
def newclock(): clock.set_default(clock.Clock()) yield clock
def __init__(self): self._has_exit_condition = threading.Condition() self.clock = clock.Clock() self.is_running = False
from pyglet.gl import * from pyglet import clock from layout import * from ctypes import * w = Window(width=400, height=200) layout = render_html('''<p style="font-size: 26px"><strong>hello</strong> <em>world</em></p>''') layout.render_device.width = 400 layout.render_device.height = 200 layout.layout() c = clock.Clock(10) glClearColor(1, 1, 1, 1) while not w.has_exit: c.tick() w.dispatch_events() glClear(GL_COLOR_BUFFER_BIT) glLoadIdentity() glTranslatef(10, 100, 0) #, (text.Align.center, text.Align.center)) layout.draw() w.flip()
def intersect(self, line): boundary = line.position x1, y1, x2, y2 = self.x, self.y, self.x + 2000 * self.dest_x, self.y + 2000 * self.dest_y #ray coords x3, y3, x4, y4 = boundary[0], boundary[1], boundary[2], boundary[3] det = ((x1 - x2) * (y3 - y4)) - ((y1 - y2) * (x3 - x4)) if det == 0: return None t = (((x1 - x3) * (y3 - y4)) - ((y1 - y3) * (x3 - x4))) / det u = -(((x1 - x2) * (y1 - y3)) - ((y1 - y2) * (x1 - x3))) / det if u >= 0 and u <= 1 and t >= 0 and t <= 1: return (int(x3 + u * (x4 - x3)), int(y3 + u * (y4 - y3))) return None fps = clock.Clock() window = pyglet.window.Window(500, 500) batch = pyglet.graphics.Batch() rays_batch = pyglet.graphics.Batch() ###Global vars### turn = [0] # how much to turn xy = [(250, 250)] # location of source w, a, s, d = False, False, False, False # pressed state of movement keys lines = [] #array of walls ### window borders ### lines.append( shapes.Line(0, 0, 0, 500, width=1, color=(0, 255, 255), batch=batch)) lines.append( shapes.Line(0, 0, 500, 0, width=1, color=(0, 255, 255), batch=batch))