You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
3.2 KiB

import pygame
from OpenGL.GL import *
from pygame.locals import *
import threading
class GLField(threading.Thread):
def __init__(self, w, h, win_w, win_h, fullscreen = False):
super(GLField, self).__init__()
self.input = []
self.changed_values = []
self.inputLock = threading.Lock()
self.queue = []
self.fullscreen = fullscreen
self.w = w
self.h = h
self.win_w = win_w
self.win_h = win_h
self.rect_w = self.win_w / self.w
self.rect_h = self.win_h / self.h
self.colors = []
self.FPS = 30
self.drawBuffer = []
def initFieldColors(self):
for i in range(self.w):
self.colors.append([])
for j in range(self.h):
rgba = [float(j) / float(self.h), float(self.w - j) / float(self.w),
float(i) / float(self.h), 0.15]
self.colors[i].append(rgba)
def resize(self):
glViewport(0,0, self.win_w, self.win_h)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0.0, self.w * self.rect_w, self.h * self.rect_h, 0.0, -6.0, 0.0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glClearColor(0.0, 0.0, 0.0, 0.0)
def drawQuadInternally(self, pX, pY):
r,g,b,a = self.colors[pY][pX]
glColor4f(a*r, a*g, a*b, 1.0)
y = pY * self.rect_w
x = pX * self.rect_h
glVertex3f(x,y,0.0)
glVertex3f(x + self.rect_w - 1.0, y, 0.0)
glVertex3f(x + self.rect_w - 1.0, y + self.rect_h - 1.0, 0.0)
glVertex3f(x, y + self.rect_h - 1.0, 0.0)
def appendChanges(self, changes):
self.inputLock.acquire()
self.changed_values += changes
self.inputLock.release()
def draw(self):
self.inputLock.acquire()
for c in self.changed_values:
i,j,v = c
self.colors[i][j][3] = v
self.queue.append((i,j))
self.inputLock.release()
glBegin(GL_QUADS)
for i,j in self.queue + self.drawBuffer:
self.drawQuadInternally(j,i)
glEnd()
self.drawBuffer = self.queue
self.queue = []
self.changed_values = []
def run(self):
pygame.init()
video_flags = OPENGL | HWSURFACE | DOUBLEBUF | FULLSCREEN if self.fullscreen else OPENGL | HWSURFACE | DOUBLEBUF
dinfo = pygame.display.Info()
window_w = dinfo.current_w if self.fullscreen else self.win_w
window_h = dinfo.current_h if self.fullscreen else self.win_h
self.win_w = window_w
self.win_h = window_h
self.rect_w = self.win_w / self.w
self.rect_h = self.win_h / self.h
pygame.display.set_mode((self.win_w, self.win_h), video_flags)
self.initFieldColors()
self.resize()
clock = pygame.time.Clock()
counter = 0
while True:
print("render main #" + str(counter))
counter += 1
ticktime = clock.tick(self.FPS)
# print ticktime
event = pygame.event.poll()
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
break
self.draw()
pygame.display.flip()