0
votes

I'm thinking of developing a game where two players play against each other in a "world" but each have their own viewport following their character (split screen) but I'm not sure how to implement it. This is for Cocos2d.

Ideally, the game events would occur in its own CCLayer and the code simply draws different sections of it onto different viewports. However, this doesn't seem to be possible with Cocos2d.

The other solution I thought about was having two different layers and keep each in sync. That would work and be easier to implement the viewport code but doesn't seem scalable.

The last idea I had was to override the draw code for the game layer and manually draw it using OpenGL functions but I wouldn't know where to start.

What would be the best way to do this?

1

1 Answers

1
votes

Alright, after a bit more research, I found this thread which uses glViewport to set up the viewports and then calling [super visit] for each viewport.

However, that didn't work well for me because for some reason, each viewport had its image stretched - I'm guessing the viewports had messed up something internal to Cocos2d.

I ended up redoing viewports almost completely using Cocos2d functions by positioning the layer, calling [super visit] and repositioning and calling [super visit] again. By combining this with glScissor, I can mimic viewports

For future reference, here's the snippet of code that I used:

-(void) visit
{
    glEnable(GL_SCISSOR_TEST);
    glPushMatrix();

    CGPoint point = ((CCNode*)[toFollow objectAtIndex:1]).position;
    self.anchorPoint = ccp(point.x/self.contentSize.width, point.y/self.contentSize.height);
    self.rotation = 180.0f;

    point = ((CCNode*)[toFollow objectAtIndex:1]).position;
    self.position = ccpAdd(ccp(bounds.width/2,bounds.height/4*3),ccp(-point.x, -point.y));
    glScissor(0,bounds.height/2,bounds.width,bounds.height/2);
    [super visit];

    glPopMatrix();


    glPushMatrix();
    self.anchorPoint = CGPointZero;
    self.rotation = 0.0f;

    point = ((CCNode*)[toFollow objectAtIndex:0]).position;

    self.position = ccpAdd(ccp(bounds.width/2,bounds.height/4),ccp(-point.x, -point.y));
    glScissor(0,0,bounds.width,bounds.height/2);
    [super visit];

    glPopMatrix();
    glDisable(GL_SCISSOR_TEST);
    //self.rotation = 0.0f;
    //[super visit];
}