4 changed files with 182 additions and 14 deletions
-
42field.py
-
121glField.py
-
24glMain.py
-
9rule.py
@ -0,0 +1,121 @@ |
|||
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() |
|||
|
|||
|
|||
|
@ -0,0 +1,24 @@ |
|||
from field import * |
|||
from glField import * |
|||
|
|||
def main(): |
|||
f = Field((160, 90)) |
|||
r = rule.Rule(2) |
|||
f.setRule(r) |
|||
f.setValue(2, 2, 1) |
|||
|
|||
screen = GLField(160, 90, 800,600, False) |
|||
|
|||
screen.start() |
|||
|
|||
counter = 0 |
|||
|
|||
while screen.is_alive(): |
|||
f.update() |
|||
c = f.getUpdatedValues() |
|||
screen.appendChanges(c) |
|||
print("master thread #" + str(counter)) |
|||
counter += 1 |
|||
|
|||
if __name__ == '__main__': |
|||
main() |
Write
Preview
Loading…
Cancel
Save
Reference in new issue