[pypy-svn] r28903 - in pypy/dist/pypy/translator/js/proxy/testme: . static/javascript templates
ericvrp at codespeak.net
ericvrp at codespeak.net
Sat Jun 17 09:42:18 CEST 2006
Author: ericvrp
Date: Sat Jun 17 09:42:15 2006
New Revision: 28903
Modified:
pypy/dist/pypy/translator/js/proxy/testme/controllers.py
pypy/dist/pypy/translator/js/proxy/testme/servermessage.py
pypy/dist/pypy/translator/js/proxy/testme/static/javascript/bnb.js
pypy/dist/pypy/translator/js/proxy/testme/templates/bnb.kid
Log:
Ingame animations visible.
Framerate with safari with server on localhost is about 5-10 fps.
Firefox slower, IE doesn't show the icons at the right location.
Keyboard input, audio and graphics transparancy are next.
Modified: pypy/dist/pypy/translator/js/proxy/testme/controllers.py
==============================================================================
--- pypy/dist/pypy/translator/js/proxy/testme/controllers.py (original)
+++ pypy/dist/pypy/translator/js/proxy/testme/controllers.py Sat Jun 17 09:42:15 2006
@@ -28,12 +28,16 @@
def sessionSocket(self, close=False):
sm = self.serverMessage()
if sm.socket is None:
+ player_id = 0 #XXX hardcoded for now
sm.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sm.socket.connect((self.host, self.port))
sm.socket.send(message(CMSG_PROTO_VERSION, 2)) #, version
sm.socket.send(message(CMSG_ENABLE_SOUND, 0)) #, has_sound
sm.socket.send(message(CMSG_ENABLE_MUSIC, 0)) #, has_music
sm.socket.send(message(CMSG_UDP_PORT, "\\")) #, port
+ sm.socket.send(message(CMSG_PING)) #so server starts sending data
+ sm.socket.send(message(CMSG_ADD_PLAYER, player_id))
+ sm.socket.send(message(CMSG_PLAYER_NAME, player_id, 'playername'))
#XXX todo: session.socket.close() after a timeout
return sm.socket
@@ -44,13 +48,8 @@
return dict()
@expose(format='json')
- def ping(self):
- self.sessionSocket().send(message(CMSG_PING))
- return self.recv()
-
- @expose(format='json')
def recv(self):
- #XXX hangs if not first sending a ping!
+ #XXX hangs if not first sending CMSG_PING!
sm = self.serverMessage()
size = 1024
data = sm.data + self.sessionSocket().recv(size)
@@ -85,6 +84,11 @@
# len_before, inline_frames, len(messages)))
return dict(messages=messages)
+ @expose(format='json')
+ def close(self):
+ self._close()
+ return dict()
+
def _close(self):
sessionid = session['_id']
if sessionid in self._serverMessage:
@@ -93,8 +97,3 @@
sm.socket.close()
del self._serverMessage[sessionid]
- @expose(format='json')
- def close(self):
- self._close()
- return dict()
-
Modified: pypy/dist/pypy/translator/js/proxy/testme/servermessage.py
==============================================================================
--- pypy/dist/pypy/translator/js/proxy/testme/servermessage.py (original)
+++ pypy/dist/pypy/translator/js/proxy/testme/servermessage.py Sat Jun 17 09:42:15 2006
@@ -38,6 +38,7 @@
_def_icon_queue = {}
base_gfx_dir = 'testme/static/images/'
base_gfx_url = 'static/images/'
+ gfx_extension = 'gif'
def __init__(self):
self.socket = None
@@ -89,7 +90,7 @@
def def_bitmap1(self, bitmap_code, data, *rest):
#log('def_bitmap1 bitmap_code=%d, data=%d bytes, colorkey=%s' % (
# bitmap_code, len(data), rest))
- gif_bitmap_filename = '%sbitmap%d.gif' % (self.gfx_dir, bitmap_code)
+ gif_bitmap_filename = '%sbitmap%d.%s' % (self.gfx_dir, bitmap_code, self.gfx_extension)
if exists(gif_bitmap_filename):
#log('CACHED:%s' % gif_bitmap_filename)
pass
@@ -118,7 +119,7 @@
def def_bitmap2(self, bitmap_code, fileid, *rest):
#log('def_bitmap2: bitmap_code=%d, fileid=%d, colorkey=%s' % (bitmap_code, fileid, rest))
- gif_bitmap_filename = '%sbitmap%d.gif' % (self.gfx_dir, bitmap_code)
+ gif_bitmap_filename = '%sbitmap%d.%s' % (self.gfx_dir, bitmap_code, self.gfx_extension)
if exists(gif_bitmap_filename):
#log('SKIP DATA_REQUEST:%s' % gif_bitmap_filename)
pass
@@ -135,8 +136,8 @@
#log('def_icon bitmap_code=%s, icon_code=%s, x=%s, y=%s, w=%s, h=%s, alpha=%s' %\
# (bitmap_code, icon_code, x,y,w,h, rest))
- bitmap_filename = '%sbitmap%d.gif' % (self.gfx_dir, bitmap_code)
- icon_filename = '%sicon%d.gif' % (self.gfx_dir, icon_code)
+ bitmap_filename = '%sbitmap%d.%s' % (self.gfx_dir, bitmap_code, self.gfx_extension)
+ icon_filename = '%sicon%d.%s' % (self.gfx_dir, icon_code, self.gfx_extension)
if exists(icon_filename):
#log('CACHED:%s' % icon_filename)
pass
@@ -154,7 +155,7 @@
self._def_icon_queue[bitmap_code].append((icon_code, x, y, w, h, rest))
return
- filename = '%sicon%d.gif' % (self.gfx_url, icon_code)
+ filename = '%sicon%d.%s' % (self.gfx_url, icon_code, self.gfx_extension)
return dict(type=PMSG_DEF_ICON, icon_code=icon_code, filename=filename, width=w, height=h)
def zpatch_file(self, fileid, position, data): #response to CMSG_DATA_REQUEST
Modified: pypy/dist/pypy/translator/js/proxy/testme/static/javascript/bnb.js
==============================================================================
--- pypy/dist/pypy/translator/js/proxy/testme/static/javascript/bnb.js (original)
+++ pypy/dist/pypy/translator/js/proxy/testme/static/javascript/bnb.js Sat Jun 17 09:42:15 2006
@@ -2,10 +2,10 @@
// init global data
playfield = DIV({'bgcolor':'red', 'width':42, 'height':42}); //updated with def_playfield
icon = {};
- x = 0; y = 0; //for filling the playfield with loaded icons
doc = currentDocument();
body = doc.body;
- sendPing();
+ zoom = 1;
+ receiveData();
}
function BnBColorToHexString(c) {
@@ -16,60 +16,81 @@
}
function handleServerResponse(json_doc) {
+ receiveData(); //do a new request a.s.a.p
for (var i in json_doc.messages) {
var msg = json_doc.messages[i];
if (msg.type == 'def_playfield') { //XXX refactor to helper functions
var bgcolor = BnBColorToHexString(msg.backcolor);
updateNodeAttributes(playfield,
{'bgcolor':bgcolor, 'width':msg.width, 'height':msg.height});
- replaceChildNodes(body, playfield);
+ //replaceChildNodes(body, playfield);
body.setAttribute('bgcolor', bgcolor); //XXX hack!
} else if (msg.type == 'def_icon') {
icon[msg.icon_code] = msg;
- //icon[msg.icon_code].image = new Image();
- //icon[msg.icon_code].image.src = msg.filename;
- /*
- var img = IMG({//'src':msg.filename,
- //'title':msg.filename,
- 'width':msg.width, 'height':msg.height,
- 'id':'icon_code'+msg.icon_code,
- 'style':'position:absolute; top:0px; left:0px;'});
- appendChildNodes(playfield, img);
- */
+ icon[msg.icon_code].width *= zoom;
+ icon[msg.icon_code].height *= zoom;
} else if (msg.type == 'inline_frame') { //msg.sounds, msg.sprites
- var images = [];
- for (var n in msg.sprites) {
- var sprite_data = msg.sprites[n];
- var icon_code = sprite_data[0];
- var x = sprite_data[1];
- var y = sprite_data[2];
- //var obj = $('icon_code'+icon_code);
- //obj.style.left = x + 'px';
- //obj.style.top = y + 'px';
- //obj.src = icon[icon_code].filename;
-
- //XXX need to test performance here. Maybe we should cash images and
- // reuse what's already attached to the playfield.
- // On the other hand this might not be bad and communication overhead
- // seems to be the killer anyway.
- if (icon_code in icon) {
- var img = IMG({'src':icon[icon_code].filename,
- 'width':icon[icon_code].width, 'height':icon[icon_code].height,
- 'style':'position:absolute; top:' + y + 'px; left:' + x + 'px;'});
- images.push(img);
+ if (!this.inline_frame_starttime) {
+ this.images = [];
+ this.max_images = 500;
+ for (var n = 0;n < this.max_images;n++) {
+ var img = IMG({
+ 'width':'0', 'height':'0',
+ 'style':'position:absolute; top:-1000px; left:0px;'});
+ this.images.push(img);
}
+ //replaceChildNodes(playfield, this.images);
+ replaceChildNodes(body, this.images);
+
+ this.inline_frame_starttime = new Date();
+ this.n_inline_frames = 0;
+ this.last_sprites = []
+ } else {
+ this.n_inline_frames++;
+ var fps = 1000 / ((new Date() - this.inline_frame_starttime) / this.n_inline_frames);
+ document.title = fps + " fps";
}
- replaceChildNodes(playfield, images);
- }
- else {
+
+ //XXX firefox isn't instant with changing img.src's!
+ //Plus it is very slow when changing the .src attribute
+ //So we might be better of keeping a list of animating images
+ //so we can just move those to the right location!
+
+ var sprite_data, icon_code, img, n;
+ for (n in msg.sprites) {
+ if (n >= this.max_sprites)
+ continue;
+ sprite_data = msg.sprites[n];
+ icon_code = sprite_data[0];
+ if (!(icon_code in icon)) {
+ sprite_data[0] = -100; //force draw when icon becomes avaliable
+ continue;
+ }
+ if (icon_code == this.last_sprites[0] &&
+ sprite_data[1] == this.last_sprites[1] &&
+ sprite_data[2] == this.last_sprites[2])
+ continue;
+ if (icon_code != this.last_sprites[0])
+ this.images[n].src = icon[icon_code].filename;
+ this.images[n].width = icon[icon_code].width;
+ this.images[n].height = icon[icon_code].height;
+ if (sprite_data[1] != this.last_sprites[1])
+ this.images[n].style.left = sprite_data[1] * zoom + 'px'
+ if (sprite_data[2] != this.last_sprites[2])
+ this.images[n].style.top = sprite_data[2] * zoom + 'px'
+ }
+ for (;n < this.max_images;n++) {
+ this.images[n].style.left = "-1000px";
+ }
+ this.last_sprites = msg.sprites;
+ } else {
logWarning('unknown msg.type: ' + msg.type + ', msg: ' + items(msg));
}
}
- sendPing();
}
-function sendPing() {
- loadJSONDoc('ping').addCallback(handleServerResponse);
+function receiveData() {
+ loadJSONDoc('recv').addCallback(handleServerResponse);
}
Modified: pypy/dist/pypy/translator/js/proxy/testme/templates/bnb.kid
==============================================================================
--- pypy/dist/pypy/translator/js/proxy/testme/templates/bnb.kid (original)
+++ pypy/dist/pypy/translator/js/proxy/testme/templates/bnb.kid Sat Jun 17 09:42:15 2006
@@ -1,7 +1,7 @@
<html>
<head>
- <script src="${std.tg_js}/MochiKit.js"/>
- <script src="static/javascript/bnb.js"/>
+ <script language="javascript" src="${std.tg_js}/MochiKit.js"/>
+ <script language="javascript" src="static/javascript/bnb.js"/>
</head>
<title>
BnB - the game
More information about the Pypy-commit
mailing list