Merge pull request #1 from CodePanter/master

Implemented threads and a forced framerate.
This commit is contained in:
JanKlopper 2016-03-03 21:59:18 +01:00
commit 67a9085262

View file

@ -11,10 +11,13 @@ __author__ = "Jan Klopper <jan@underdark.nl>"
import pygame import pygame
import socket import socket
import struct import struct
import Queue
import threading
import time
def main(): def main():
"""Runs a pixelvloed server""" """Runs a pixelvloed server"""
PixelVloed(debug=True) PixelVloed()
class PixelVloed(object): class PixelVloed(object):
"""PixelVloed server class""" """PixelVloed server class"""
@ -23,10 +26,15 @@ class PixelVloed(object):
"""Init the pixelVloed server""" """Init the pixelVloed server"""
self.debug = debug self.debug = debug
self.pixeloffset = 2 self.pixeloffset = 2
self.fps = 30
self.screen = None self.screen = None
self.canvas() self.canvas()
self.set_title() self.set_title()
self.Socket(udp_ip, udp_port) self.queue = Queue.Queue()
self.receivethread = threading.Thread(target=self.Socket, args=(udp_ip, udp_port), name="ReceiveThread")
self.drawthread = threading.Thread(target=self.CanvasUpdate, name="DrawThread")
self.receivethread.start()
self.drawthread.start()
@staticmethod @staticmethod
def set_title(text=None): def set_title(text=None):
@ -36,7 +44,7 @@ class PixelVloed(object):
title += ' ' + text title += ' ' + text
pygame.display.set_caption(title) pygame.display.set_caption(title)
def canvas(self, width=640, height=480): def canvas(self, width=768, height=1366):
"""Init the pygame canvas""" """Init the pygame canvas"""
pygame.init() pygame.init()
self.screen = pygame.display.set_mode((width, height)) self.screen = pygame.display.set_mode((width, height))
@ -56,26 +64,39 @@ class PixelVloed(object):
sock.bind((ipaddress, port)) sock.bind((ipaddress, port))
while True: while True:
data, addr = sock.recvfrom(1024) self.queue.put(sock.recvfrom(1024))
preamble = struct.unpack_from("<?", data)[0] self.queue.join()
protocol = struct.unpack_from("<B", data, 1)[0]
pixellength = 7 #xx,yy,r,g,b def CanvasUpdate(self):
if preamble: lasttime = time.time()
if self.debug: while True:
print 'rgba mode for %s:%d' % (addr[0], addr[1]) currenttime = time.time()
pixellength = 8 #xx,yy,r,g,b,a self.Draw()
pixelcount = (len(data)-1) / pixellength if currenttime - lasttime >= 1 / self.fps:
pygame.display.flip()
lasttime = time.time()
def Draw(self):
data, addr = self.queue.get()
self.queue.task_done()
preamble = struct.unpack_from("<?", data)[0]
protocol = struct.unpack_from("<B", data, 1)[0]
pixellength = 7 #xx,yy,r,g,b
if preamble:
if self.debug: if self.debug:
print '%d pixels received, protocol V %d ' % (pixelcount, protocol) print 'rgba mode for %s:%d' % (addr[0], addr[1])
for i in xrange(0, pixelcount): pixellength = 8 #xx,yy,r,g,b,a
pixel = struct.unpack_from( pixelcount = (len(data)-1) / pixellength
("<2H4B" if preamble else "<2H3B"), if self.debug:
data, print '%d pixels received, protocol V %d ' % (pixelcount, protocol)
self.pixeloffset + (i*pixellength)) for i in xrange(0, pixelcount):
if self.debug: pixel = struct.unpack_from(
print pixel ("<2H4B" if preamble else "<2H3B"),
self.Pixel(*pixel) data,
pygame.display.flip() self.pixeloffset + (i*pixellength))
if self.debug:
print pixel
self.Pixel(*pixel)
if __name__ == '__main__': if __name__ == '__main__':
main() main()