Oh God so many tiles!

Anything related in any way to game development as a whole is welcome here. Tell us about your game, grace us with your project, show us your new YouTube video, etc.

Moderator: PC Supremacists

User avatar
spirit bomb!
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 17
Joined: Wed Jan 06, 2010 11:28 pm

Oh God so many tiles!

Post by spirit bomb! »

This is a subject that I believe many people on these forums have experience with...

Suppose I have a tile based game. And suppose the character can move, but he really stays in the same spot on the screen, as the entire world moves in the opposite direction he is running in (Kinda like the space ship in FUTURAMA). So the problem is... I am keeping track of every single tile, on and off screen. Every time the character moves, the (x,y) of every single tile needs to be adjusted. This is eating my frame-rate ferociously, because of the ridiculous amount of processes involved. Has anyone experienced this problem, or have any good solution?

Possible Solutions???

-Smaller World.
-Make every layer into one single bitmap. <- wouldn't have any idea how to implement this b.s. but it would be cool


Any halp is awesome, and well appriciated.
circular reasoning works because circular reasoning works because circular reasoning works because circular reasoning works because
circular reasoning works because circular reasoning works because circular reasoning works because circular reasoning works because
pritam
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 991
Joined: Thu Nov 13, 2008 3:16 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Amiga, PSOne, NDS
Programming Language of Choice: C++
Location: Sweden

Re: Oh God so many tiles!

Post by pritam »

I think a possible solution would be to have an offset for your tilemap, something like this.

Code: Select all

int tmOffsetX;
int tmOffsetY;
And then when you render your tiles you just add them to the X and Y parameters of your draw function. Hope it works out.
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: Oh God so many tiles!

Post by eatcomics »

and if your tiles aren't on screen, don't draw them :D
Image
User avatar
zeid
Chaos Rift Junior
Chaos Rift Junior
Posts: 201
Joined: Fri Apr 24, 2009 11:58 pm

Re: Oh God so many tiles!

Post by zeid »

if your tiles aren't on screen, don't draw them :D
The other calculations barely cost anything compared to the cost of rendering. Always try and draw as little as possible.

If you want to get crazy with optimisation you could consider grouping the tiles in a quad-tree like fashion.
Axolotl Pop!
Image
Or, try it for free.

For many more free games online visit www.sam-zeid.com
User avatar
cypher1554R
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1124
Joined: Sun Jun 22, 2008 5:06 pm

Re: Oh God so many tiles!

Post by cypher1554R »

Also, instead of doing user defined per frame calculations for every tile, just move the moving sprite and follow it with camera (change view matrix accordingly).
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Oh God so many tiles!

Post by Falco Girgis »

spirit bomb! wrote:This is a subject that I believe many people on these forums have experience with...

Suppose I have a tile based game. And suppose the character can move, but he really stays in the same spot on the screen, as the entire world moves in the opposite direction he is running in (Kinda like the space ship in FUTURAMA). So the problem is... I am keeping track of every single tile, on and off screen. Every time the character moves, the (x,y) of every single tile needs to be adjusted. This is eating my frame-rate ferociously, because of the ridiculous amount of processes involved. Has anyone experienced this problem, or have any good solution?

Possible Solutions???

-Smaller World.
-Make every layer into one single bitmap. <- wouldn't have any idea how to implement this b.s. but it would be cool


Any halp is awesome, and well appriciated.
What? Dude, when a scene is rendered, hundreds to thousands of render calls are made. The only difference between a scrolling tile game and nonscrolling is an integer offset. The calculation amounts to a simple difference in the x and y. So that's two subtractions per render call...

It sounds to me like your bottleneck is RENDERING not CALCULATING. If you are RENDERING a million tiles that aren't even on the screen (especially with SDL or software rendering) then yeah--shit can get hella slow. I'm sure that you're rendering your tiles with something like:

Code: Select all

for(y = 0; y < map_height; ++y)
    for(int x = 0; x < map_width; ++x)
        DrawSprite(map[y][x]);
Just write your loop a little more intelligently so that you aren't rendering EVERY tile, just the ones that need to be in the scene.
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: Oh God so many tiles!

Post by eatcomics »

so basically what falco is saying is, make it so if your tile's x < 0 && y < 0, or x > screenwidth && y > screenheight, don't draw the tile :D
Image
User avatar
mv2112
Chaos Rift Junior
Chaos Rift Junior
Posts: 240
Joined: Sat Feb 20, 2010 4:15 am
Current Project: Java Tower Defence Game
Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
Programming Language of Choice: C/++, Java
Location: /usr/home/mv2112
Contact:

Re: Oh God so many tiles!

Post by mv2112 »

I draw all the tiles under the character onto a bitmap and only manually render tiles above the player, and i only blit a clip of the map bitmap to the screen. I also use a camera that tells the engine where to clip the map so i don't render the whole thing.
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: Oh God so many tiles!

Post by eatcomics »

yet he manually called a destructor...
Image
User avatar
mv2112
Chaos Rift Junior
Chaos Rift Junior
Posts: 240
Joined: Sat Feb 20, 2010 4:15 am
Current Project: Java Tower Defence Game
Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
Programming Language of Choice: C/++, Java
Location: /usr/home/mv2112
Contact:

Re: Oh God so many tiles!

Post by mv2112 »

eatcomics wrote:yet he manually called a destructor...
:evil: Why the hell is it such bad practice to call destructors? Who says it is bad practice? Why does it matter? :evil:

Ahh, i feel better now...
:mrgreen:
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Oh God so many tiles!

Post by XianForce »

mv2112 wrote:
eatcomics wrote:yet he manually called a destructor...
:evil: Why the hell is it such bad practice to call destructors? Who says it is bad practice? Why does it matter? :evil:

Ahh, i feel better now...
:mrgreen:
Erm, I'd say Bjarne Stroustrup would say so...

Destructors are automatically called. So when you manually call it, it will actually be called twice most likely. So let's say you had this:

Code: Select all

SomeDestructorExample::~SomeDestructorExample()
{
delete pSomeDynamicallyAllocatedMemory;
}
And later the destructor is also automatically called... Then you run into things like segfaults and access violations...

That's probably why it's a bad practice... It's also just plain out unnecessary...
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Oh God so many tiles!

Post by dandymcgee »

XianForce wrote: Destructors are automatically called. So when you manually call it, it will actually be called twice most likely. So let's say you had this:

Code: Select all

SomeDestructorExample::~SomeDestructorExample()
{
delete pSomeDynamicallyAllocatedMemory;
}
And later the destructor is also automatically called... Then you run into things like segfaults and access violations...
Not if you have good coding habits. ;)

Code: Select all

SomeDestructorExample::~SomeDestructorExample()
{
    delete pSomeDynamicallyAllocatedMemory;
    pSomeDynamicallyAllocatedMemory = NULL;
}
XianForce wrote: It's also just plain out unnecessary...
This.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Oh God so many tiles!

Post by Ginto8 »

dandymcgee wrote:Not if you have good coding habits. ;)

Code: Select all

SomeDestructorExample::~SomeDestructorExample()
{
    delete pSomeDynamicallyAllocatedMemory;
    pSomeDynamicallyAllocatedMemory = NULL;
}
It would have to be more like this:

Code: Select all

SomeDestructorExample::~SomeDestructorExample()
{
    if(pSomeDynamicallyAllocatedMemory) {
        delete pSomeDynamicallyAllocatedMemory;
        pSomeDynamicallyAllocatedMemory = NULL;
    }
}
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Oh God so many tiles!

Post by dandymcgee »

Ginto8 wrote:
dandymcgee wrote:Not if you have good coding habits. ;)

Code: Select all

SomeDestructorExample::~SomeDestructorExample()
{
    delete pSomeDynamicallyAllocatedMemory;
    pSomeDynamicallyAllocatedMemory = NULL;
}
It would have to be more like this:

Code: Select all

SomeDestructorExample::~SomeDestructorExample()
{
    if(pSomeDynamicallyAllocatedMemory) {
        delete pSomeDynamicallyAllocatedMemory;
        pSomeDynamicallyAllocatedMemory = NULL;
    }
}
Actually, that's redundant.

http://opensource.devx.com/tips/Tip/14443
Null Pointers and Delete

C++ guarantees that operator delete checks its argument for null-ness. If the argument is 0, the delete expression has no effect. In other words, deleting a null pointer is a safe (yet useless) operation. There is no need to check the pointer for null-ness before passing it to delete:


if (p) // useless; delete already checks for a null value
delete(p);
;)
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Oh God so many tiles!

Post by XianForce »

But what Ginto posted is still good practice. Because what if it ISN'T NULL? I use that practice all the time =p
Post Reply