Fast 2d rendering - CopyBits

Moderator
Posts: 133
Joined: 2008.05
Post: #1
My game is a 2d top down game that uses Copybits to do it's drawing. I'm having a problem making it run real fast, I know that the code is slow, I just don't know how to make it any better Smile

I have an event loop timer that fires every millisecond(too much, every 1/10 of a second?) that does the animation and drawing and such. The animation and drawing handler is pretty slow, basically because I have a constantly scrolling map that keeps the player centered until the map can't move any further, and redraw the map to the screen from the offscreen buffer each time. I don't know if it would make sense to check to see how much of the map needs to be redrawn to the screen, since it's moving all of the time.

While I'm testing it, I have all the animations and the players and such movign at the same time, which would never really happen, but it might, so might as well test it this way.

The function:
Code:
static pascal void AnimateAndRender (EventLoopTimerRef  theTimer, void* userData)
{
    MightyMan* blah = (MightyMan*) userData;
    MonsterGenerator* mGen = blah->theLevel.monsterGenerators;
    SpritePtr sprite;
    Rect dirty;
/*Animation*/
    //Monster generators
    while(mGen != NULL)
    {
        sprite = mGen->GetSprite();
        sprite->GetRect(&dirty);
        blah->DirtyRectangle(&dirty);
        
        mGen->UpdateGenerator(mGen);
        
        mGen = mGen->next;
    }
    mGen = blah->theLevel.monsterGenerators; //reset to draw
    
    //Player
    sprite = blah->thePlayer.GetSprite();
    sprite->GetRect(&dirty);
    blah->DirtyRectangle(&dirty);
    
    sprite->Animate();
    
    
    // Monsters - needs to be done.
/*Dirty Rect fix*/
    blah->theLevel.FixDirty();
/*Drawing*/
    while(mGen!=NULL)
    {
        sprite = mGen->GetSprite();
        sprite->Draw(blah->theLevel.mapStorage);
        mGen = mGen->next;
    }
    sprite = blah->thePlayer.GetSprite();
    sprite->Draw(blah->theLevel.mapStorage);


/*Copy buffer*/
    blah->DrawMap();
}

Any help speeding this up would be appreciated Smile
Quote this message in a reply
Member
Posts: 177
Joined: 2002.08
Post: #2
First, the quick answer: don't bother implementing your own double-buffering, Quartz is already doing that for you and you may be doing an unnecessary extra copybits. (I don't know if this really applies to you, since I don't know what blah->DrawMap(); does.)

Secondly, the long answer: You'll get far better performance by using OpenGL, but it's very different from Mac OS-native graphics so it would be a good bit of work to switch :/
Quote this message in a reply
Moderator
Posts: 133
Joined: 2008.05
Post: #3
Well, DrawMap puts the offscreen map into the backbuffer of the window, then flushes it.

I was considering OpenGL but figured I'd save that for another project, although I've used it before I want to do one game like this Smile
Quote this message in a reply
Member
Posts: 177
Joined: 2002.08
Post: #4
That "flush" is a copy. You could be drawing directly into the window buffer.
Quote this message in a reply
Moderator
Posts: 133
Joined: 2008.05
Post: #5
Well, now that I think about it... is there a toolbox function to draw directly to the screen, or am I going to have write it myself?
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #6
It is generally not a good idea to draw directly to the screen. What you should do is draw into the window's buffer, which on Mac OS X, is done for you by default.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  CopyBits vs CopyMask Muffinking 4 3,151 Nov 27, 2002 01:42 PM
Last Post: henryj