Last active
July 13, 2018 17:18
-
-
Save larsoner/580898778a2738887088ef12e256bfa2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import numpy as np | |
import pyglet | |
from pyglet.gl import (glBindTexture, glTexImage2D, GL_TEXTURE_2D, | |
GL_RGB, GL_UNSIGNED_BYTE) | |
import time | |
window = pyglet.window.Window() | |
data = np.full((1920, 1080, 3), 0, np.uint8) | |
image = pyglet.image.ImageData(data.shape[1], data.shape[0], 'RGB', | |
data.tostring()) | |
ball = pyglet.sprite.Sprite(image, x=50, y=50) | |
ii = 128 | |
@window.event | |
def on_draw(): | |
window.clear() | |
global ii | |
ii = (ii + 2) % 256 | |
data[:, :, 0].fill(ii) | |
# image.set_data('RGB', image.pitch, data.tostring()) | |
# ball.image = image | |
# id_ = image.texture.id | |
id_ = ball._texture.owner.id | |
glBindTexture(GL_TEXTURE_2D, id_) | |
glTexImage2D( | |
GL_TEXTURE_2D, 0, GL_RGB, data.shape[1], data.shape[0], 0, | |
GL_RGB, GL_UNSIGNED_BYTE, data.ctypes.data) | |
pyglet.gl.glFlush() | |
# image.blit(0, 0) | |
ball.draw() | |
@window.event | |
def on_key_press(symbol, key): | |
if symbol == pyglet.window.key.ESCAPE: | |
window.close() | |
t0 = time.time() | |
while not window.has_exit: | |
pyglet.clock.tick() | |
window.dispatch_events() | |
window.dispatch_event('on_draw') | |
window.flip() | |
this_time = time.time() | |
print(int(round(1. / (this_time - t0)))) | |
t0 = this_time |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment