[CB] Drawing tilemaps using viewWillDraw

Member
Posts: 370
Joined: 2002.04
Post: #1
What's wrong with this code? (it's using CocoaBlitz)
Code:
- (void)viewWillDraw:(NSNotification*)aNotification
{
    int x,y,w,h,cx,cy,dx,dy;
    NSMutableDictionary *images = [NSMutableDictionary dictionaryWithCapacity:16];
    NSRect viewRect = [surf viewRect];
    NSString *s;
    NSImage *img;
    NSRect src,dest;
    x = viewRect.origin.x / TSIZE;
    y = viewRect.origin.y / TSIZE;
    w = viewRect.size.width / TSIZE;
    h = viewRect.size.height / TSIZE;
    
    dx = 0;
    dy = 0;
    for(cx = x; cx < w+x; cx++,dx+=TSIZE){
    for(cy = y,dy = 0; cy < h+y; cy++,dy+=TSIZE){
        s = [self getTileX:cx Y:cy];
        img = [images objectForKey:s];
        if(img == nil){
        img = [NSImage imageNamed:s];
        [images setObject:img forKey:s];
        }
        //NSLog(@"drawing %@ (%@) at %d,%d",s,img,dx,dy);
        src = NSMakeRect(0,0,TSIZE,TSIZE);
        dest = NSMakeRect(dx,dy,TSIZE,TSIZE);
        [img drawInRect:dest fromRect:src operation:NSCompositeSourceOver fraction:1.0];
    }
    }
}

The commented NSLog statement shows that I'm drawing a valid image at a sensible coordinate. But still nothing shows. Do I have to do something special to get it to draw to the view before it draws the cells?

EDIT: Oh yeah, the reason that I posted this is because Kelvin seems to be MIA. No email, AIM, etc...

Did you ever wonder why we had to run for shelter when the promise of a brave new world unfurled beneath the clear blue sky?
Quote this message in a reply
Nibbie
Posts: 2
Joined: 2012.06
Post: #2
maybe an obvious question, but has the image 'img' been
added a to CBCell?

CBCell *cell;
[cell setTexture: img];

kevin
Quote this message in a reply
Moderator
Posts: 916
Joined: 2002.10
Post: #3
have you attached it to the your CBView?
Quote this message in a reply
Member
Posts: 370
Joined: 2002.04
Post: #4
hams: Why do I need a cbcell for this? The whole point is that I'm drawing directly to the CBView, behind the cells (as using cells for tiling like this seems to be ridiculously slow)

And sky: Attached? You mean set the delegate? I know it's being called because the NSLog statement prints out.


Hmmm, on re-reading my post I think I've been unclear about my intents. I'm trying to draw a tiled background behind all the cells, so that the background is behind (duh) the cells and doesn't use the CBCell interface (as the geometry will never change, and no rotations/stuff to warrant OpenGL)

Did you ever wonder why we had to run for shelter when the promise of a brave new world unfurled beneath the clear blue sky?
Quote this message in a reply
Moderator
Posts: 916
Joined: 2002.10
Post: #5
I did something like this, I just made a bunch of "layer" cbcells with nothing in them and attached to those layers the proper sprites. I believe this is how it SHOULD be done Wink
Quote this message in a reply
Member
Posts: 370
Joined: 2002.04
Post: #6
I've just had infinite trouble with large numbers of CBCells... I'd really like to get this working. I just don't see why it doesn't work as is- the drawing context (or whatever it's called) should be focused by the time I get to viewWillDraw, right?

Did you ever wonder why we had to run for shelter when the promise of a brave new world unfurled beneath the clear blue sky?
Quote this message in a reply
Nibbie
Posts: 2
Joined: 2012.06
Post: #7
i don't know for sure, but i believe that is incorrect.

what i think you need to do is composite the image and then attatch it to a cbcell.

look for a topic futher down about CB and tilemaps.
kelvin has example code of how to do this.

kevin
Quote this message in a reply
Member
Posts: 370
Joined: 2002.04
Post: #8
I know about that topic, but it takes too long to composite the image - a few minutes for a decently sized one. Unacceptable. I thought that this might be a better solution.

Did you ever wonder why we had to run for shelter when the promise of a brave new world unfurled beneath the clear blue sky?
Quote this message in a reply
Moderator
Posts: 916
Joined: 2002.10
Post: #9
ahhh... and you wanted to know about compositing like a fraction of the maximum size and such... so like compositing 100x100 at once or so...
alright, my advice to you.... let photoshop be your compositor Wink you can even use jpegs to compress your files
Quote this message in a reply
Member
Posts: 370
Joined: 2002.04
Post: #10
Well I want a map editor... and it'd still be big, unless I jpeg it into a blur...

Did you ever wonder why we had to run for shelter when the promise of a brave new world unfurled beneath the clear blue sky?
Quote this message in a reply
Nibbie
Posts: 2
Joined: 2012.06
Post: #11
ok here's what i'm planning on doing conceptually.

pick a threshold size for your display system. for example
say 512x512. by making this large enough you will
limit the number of cells to create the map.

on level load if your map tile is smaller in width or height
than the theshold size you composite a block of
tiles into a new texture which is about your threshold size.
you create CBCells then with the new composited images.
you then limit the number of CBCells needed for
the map to 100 or something.

i'm working on the code now, but haven't tested it yet,
but i'll post it if i get it working.

kevin
Quote this message in a reply
Member
Posts: 469
Joined: 2002.10
Post: #12
I'm lacking an internet connection at the moment; sorry for the lack of responses.

1) quartz drawing will not work in a CBView because the openGL buffer is flushed after the update.

2) the only way to get something into the view is to either make it a texture and attach it to a cbcell, or do custom openGL drawing in a cbcell subclass in CBCell's -drawRect: method

what I suggest for your case is to composite the map in another thread at load time (which you'll have to learn about multithreading to do) and then load it as a texture with -backgroundLoadImage:. This'll sidestep the lag issues you're having with loading. I don't know when I'll get another internet connection on my mac, but when I do I'll be more available for questions through AIM.

---Kelvin--
15.4" MacBook Pro revA
1.83GHz/2GB/250GB
Quote this message in a reply
Member
Posts: 370
Joined: 2002.04
Post: #13
Thanks. You may want to note that in the documentation - it seemed that this might work. I'll fiddle around with background loading using hams's idea. Thanks all.

Did you ever wonder why we had to run for shelter when the promise of a brave new world unfurled beneath the clear blue sky?
Quote this message in a reply
Member
Posts: 370
Joined: 2002.04
Post: #14
Unfortunately, while the tilemaps are loading (in a thread in the background) the game is unacceptably slow. What could I do to fix this?

Did you ever wonder why we had to run for shelter when the promise of a brave new world unfurled beneath the clear blue sky?
Quote this message in a reply
Member
Posts: 469
Joined: 2002.10
Post: #15
display a loading screen and progress bar? maybe some credits?

---Kelvin--
15.4" MacBook Pro revA
1.83GHz/2GB/250GB
Quote this message in a reply
Post Reply