Hi All
I found the vector drawing stuff as I was looking through this group.
As it was just what I was after I thought I'd have a play. I want to
draw the lines with the mouse and I've managed to do that - most of
the time; cool.
I realise the code is newish and with no documentation I am probably
using it in a way not intended or advised. However there are a couple
of odd things I'd like to ask. The only show stopper for my app is
number 1.
1) Code works but will occassionally crash whinging
File "c:\Python25\lib\site-packages\cocos2d-0.3.0-py2.5.egg\cocos
\draw.py", line 287, in build_vbo
list(bottom) + list(top) + list(far)
TypeError: 'NoneType' object is not iterable
This happens mostly when drawing the line slowly. And doesn't seem to
be related to the number of lines that have been drawn, ie it is just
as likelt on the first line as on the 20th on
2) Seems like a draw canvas must have something on it. When I create a
canvas with no lines on it I tend to get the following error when I
start to draw a line, occassionally I get to the 2nd line before it
happens.
File "c:\python25\lib\site-packages\pyglet\graphics
\vertexdomain.py", line 219, in _safe_alloc
return self.allocator.alloc(count)
File "c:\python25\lib\site-packages\pyglet\graphics\allocation.py",
line 145,in alloc
assert size > 0
AssertionError
My work around to this is to always create a canvas with a 1 pixel
long line on it.
3) My initial code just added one line after another to the same
canvas but as more lines were added it got slower and slower, line
being drawn a long way behind the mouse. My solution was to create a
new draw canvas for every line.
Now this works fine, or appears to but it seems untidy. Is there a
better way?
4) Draw a few lines, preferably with shart direction changes, and
you'll see that there are gaps generated in either the endcaps or
discontinuities on the joins.
I've included my code below. I've simplified it as much as I can to
show the problems. It assumes pyglet and cocos are installed. It works
for
pyglet 1.1.1
cocos 0.3.0
Python 2.5.2.2 (Active Python)
On Windows XP
I hope there aren't to many stupid mistakes but I've no doubt there
are some. I'm happy for comments on better ways to do things.
Any help or advice appreciated.
Regards
john
----------------- code below here ---------------
# Draw vector lines with the mouse
#
# Creates a new layer for each line in an attempt to remove the lag
when drawing
# many lines.
import cocos
from cocos.director import director
from cocos.sprite import Sprite
from cocos import draw
import pyglet
import random
ri = random.randint
class vline(object):
"Line class that has a border of a different color to the interior
of the line"
def __init__(self, width, innerColor, borderColor, endcap =
draw.ROUND_CAP, join = draw.ROUND_JOIN):
self._width = width
self._borderColor = borderColor
self._innerColor = innerColor
self._endcap = endcap
self._join = join
self._points = []
def addPoint(self, pt):
# add a point to the line
self._points.append(pt)
def draw(self, canvas):
# set the line constants
canvas.set_endcap( self._endcap )
canvas.set_join( self._join )
# draw the outer line
canvas.set_color( (self._borderColor) )
canvas.set_stroke_width( self._width )
# draw lines
canvas.move_to( self._points[0] )
for p in self._points[1:]:
canvas.line_to( p )
# draw the inner line
canvas.set_color( (self._innerColor) )
canvas.set_stroke_width( self._width/2 )
# draw lines
canvas.move_to( self._points[0] )
for p in self._points[1:]:
canvas.line_to( p )
def __len__(self):
return len(self._points)
def __getitem__(self,key):
return self._points[key]
class TestFigure(draw.Canvas):
def __init__(self):
super( TestFigure, self ).__init__()
self._lines = []
self._borderColor = (255, 255, 0, 128)
self._innerColor = (0, 255, 128, 255)
self._currLine = None
# canvas appears to require something on it or it may
generates an exception.
# so add a 1 pixel long line
self._currWidth = 1
line = vline(self._currWidth, self._innerColor,
self._borderColor)
line.addPoint((0,0))
line.addPoint((0, 1))
self._lines.append (line)
# set the current width to something sensible
self._currWidth = 20
def render(self):
if self._currLine is not None:
if len (self._currLine) > 1:
self._currLine.draw(self)
for l in self._lines:
l.draw(self)
def addPointClean(self, pt,):
"Add a point without marking the canvas as dirty"
if self._currLine is not None:
self._currLine.addPoint(pt)
def addPoint(self, pt,):
if self._currLine is not None:
self._currLine.addPoint(pt)
self._dirty = True
def startLine(self, startPt):
# called when a new line is started
self._currLine = vline(self._currWidth, self._innerColor,
self._borderColor)
self.addPoint(startPt)
def endLine(self, endPt):
# called when a new line is started
if endPt != self._currLine[len(self._currLine) - 1]:
self.addPointClean(endPt)
self._lines.append(self._currLine)
self._currLine = None
self._dirty = True
class TestLayer(cocos.layer.Layer):
is_event_handler = True
def __init__(self):
super( TestLayer, self ).__init__()
self._figures = []
def addFigure(self):
self._currFigure = TestFigure()
self.add( self._currFigure )
self._figures.append(self._currFigure)
def on_mouse_press(self, x, y, button, modifiers):
if button == pyglet.window.mouse.LEFT:
# create a new Test figure each time we start a new line
# otherwise the lines get further and further behind the
mouse
# movements as more and more lines are drawn each refresh.
self.addFigure()
self._currFigure.startLine((x,y))
def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
self._currFigure.addPoint((x,y))
def on_mouse_release(self, x, y, button, modifiers):
if button == pyglet.window.mouse.LEFT:
self._currFigure.endLine((x,y))
if __name__ == "__main__":
director.init()
test_layer = TestLayer ()
main_scene = cocos.scene.Scene (test_layer)
director.run (main_scene)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"cocos2d discuss" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/cocos-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---