Is there a low level drawing API in the sparrow framework? I was looking for drawLine() method, couldn't find it!
Drawing API
(8 posts) (4 voices)-
Posted 1 year ago #
-
Hi, I recently started using Sparrow too.
As it appears, there is currently no drawing API in Sparrow as you describe. There's some information and suggestions on how to do it (using SPImage) in this topic:
http://forum.sparrow-framework.org/topic/drawing#post-424On a side note, I've been working on a new simple class called SPLine. It's based on SPQuad and is initialized with 2 coordinates. It will draw a line between them. But it doesn't work yet, but will inform as soon as it does!
Posted 1 year ago # -
Thx so much jx, I will work on some other solutions and update here if I come up with something useful.
Posted 1 year ago # -
I made a working SPLine but it's a real hack ..maybe it could help you get yours working ..:)
Spline.h
#import <Foundation/Foundation.h> #import "SPDisplayObject.h" @interface SPLine : SPDisplayObject { @protected float paX; float paY; float pbX; float pbY; float mRed; float mGreen; float mBlue; @private } @property (nonatomic, assign) uint color; - (id)initWithPoint1:(SPPoint *)p1 p2:(SPPoint *)p2; -(void)setPoint1:(SPPoint *)point; -(void)setPoint2:(SPPoint *)point; - (void)setColorRed:(float)red green:(float)green blue:(float)blue; + (SPLine*)lineWithPoint1:(SPPoint *)p1 p2:(SPPoint *)p2;SPline.m
#import "SPLine.h" #import "SPRectangle.h" #import "SPMacros.h" #import "SPPoint.h" @implementation SPLine - (id)initWithPoint1:(SPPoint *)p1 p2:(SPPoint *)p2 { if (self = [super init]) { // mPointA = [SPPoint pointWithX:p1.x y:p1.y]; // mPointB = [SPPoint pointWithX:p2.x y:p2.y]; paX = p1.x; paY = p1.y; pbX = p2.x; pbY = p2.y; mRed = 0.0; mGreen = 0.0; mBlue = 0.0; // self.color = SP_BLACK; } return self; } -(void)setPoint1:(SPPoint *)point { // mPointA = [SPPoint pointWithX:point.x y:point.y]; paX = point.x; paY = point.y; } -(void)setPoint2:(SPPoint *)point { // mPointB = [SPPoint pointWithX:point.x y:point.y]; pbX = point.x; pbY = point.y; } - (void)setColorRed:(float)red green:(float)green blue:(float)blue { mRed = red; mGreen = green; mBlue = blue; } - (id)init { return [self initWithPoint1:[SPPoint pointWithX:0 y:0] p2:[SPPoint pointWithX:0 y:0]]; } - (SPRectangle*)boundsInSpace:(SPDisplayObject*)targetCoordinateSpace { SPMatrix *transformationMatrix = [self transformationMatrixToSpace:targetCoordinateSpace]; SPPoint *point = [[SPPoint alloc] init]; float coords[] = { 0.0f, 0.0f, paX, paY,paX, 0.0, 0.0f, pbY }; float minX = FLT_MAX, maxX = -FLT_MAX, minY = FLT_MAX, maxY = -FLT_MAX; for (int i=0; i<4; ++i) { point.x = coords[2*i]; point.y = coords[2*i+1]; SPPoint *transformedPoint = [transformationMatrix transformPoint:point]; float tfX = transformedPoint.x; float tfY = transformedPoint.y; minX = MIN(minX, tfX); maxX = MAX(maxX, tfX); minY = MIN(minY, tfY); maxY = MAX(maxY, tfY); } [point release]; return [SPRectangle rectangleWithX:minX y:minY width:maxX-minX height:maxY-minY]; } + (SPLine*)lineWithPoint1:(SPPoint *)p1 p2:(SPPoint *)p2 { return [[[SPLine alloc] initWithPoint1:p1 p2:p2] autorelease]; } @endSPRendering.m
@implementation SPLine (Rendering) - (void)render:(SPRenderSupport *)support; { // If this method is called from a subclass, it has most probably bound a texture (on purpose). // But if this is a 'real' quad, we have to disable any texture. if (self->isa == [SPLine class]) [support bindTexture:nil]; static GLfloat line[4]; line[0] = paX; line[1] = paY; line[2] = pbX; line[3] = pbY; glColor4f(mRed, mGreen, mBlue, self.alpha); glLineWidth(4.0); glVertexPointer(2, GL_FLOAT, 0, line); glEnableClientState(GL_VERTEX_ARRAY); glDrawArrays(GL_LINES, 0, 2); glDisableClientState(GL_VERTEX_ARRAY); } @endPosted 1 year ago # -
Wow, great community here, thanks so much Mike, I will try it soon.
Posted 1 year ago # -
Yes, it works, thanks Mike. One suggestion, since SPLine is a SPDisplayObject, we can override the render method:
So, I just implemented it like following within the SPLine.m
-(void)render:(SPRenderSupport *)support { [support bindTexture:nil]; static GLfloat line[4]; line[0] = paX; line[1] = paY; line[2] = pbX; line[3] = pbY; glColor4f(mRed, mGreen, mBlue, self.alpha); glLineWidth(4.0); glVertexPointer(2, GL_FLOAT, 0, line); glEnableClientState(GL_VERTEX_ARRAY); glDrawArrays(GL_LINES, 0, 2); glDisableClientState(GL_VERTEX_ARRAY); }Posted 1 year ago # -
Kudos to you, guys! That's a great example on how to create a custom display object =)
I can give you an additional hint, if that's of any help. You can save the color in just one unsigned integer, like SPQuad and SPImage do. There are some macros that help you in that task:
unsigned int red_color = SP_COLOR(255, 0, 0); // equivalent: red_color = 0xff0000; unsigned int red_part = SP_COLOR_PART_RED(red_color); // -> 255 unsigned int green_part = SP_COLOR_PART_GREEN(green_color); // -> 0 unsigned int blue_part = SP_COLOR_PART_BLUE(blue_color); // -> 0For the "glColor4f" call, you just have to divide the color part by 255.0f before sending it to OpenGL.
Posted 1 year ago # -
ah thanks for the tips guys!
I like the render tip also because then I don't have to worry about adding code to sparrow's codePosted 1 year ago #
Reply
You must log in to post.