Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Loading a platformer world

Author
Message
Mister Fuzzy
13
Years of Service
User Offline
Joined: 27th Feb 2011
Location: XNA
Posted: 16th Oct 2011 03:58 Edited at: 17th Oct 2011 02:03
Okay, so I'm making a platformer game with a friend in which the player, a knight who likes to beatbox, has to save the rock-goddess Princess Cheesecake from the clutches of evil in Pastryland (It's such a stupid plot that it's actually fun! ). However, my method of loading levels is, well, error-prone. My code loads several tile images and a reference map image: The reference is read pixel-by-pixel, and when the code sees a RED pixel, a solid block sprite is created in the corresponding location on the screen. However, different graphics cards read the images slightly differently, and the image ends up with extra blocks here and there, a row missing, or not showing them at all. And since the tiles are all sprites, scrolling them is a pain: Even when it does scroll, it moves slower than butt! Does anybody have an easier way to load tile maps in a way that's easy to edit them outside of the compiler, or make them display on a background layer instead of sprites? (The only reason I used images to map the tiles in the first place was because of its ease-of-editing...)
DeadTomGC
14
Years of Service
User Offline
Joined: 11th Aug 2010
Location: LU
Posted: 17th Oct 2011 22:21
I assign each type of block an ID number and then store all the information for the level in a text file that has a block type, location, and size for each block. Then I made a level designer so that I would not have to type all these values in manually.

Here is the code for the level loader and a sample level is attached.

Of course you need an image for each of the ID's which come first on each line of the level file. Oh, and I have been keeping my levels in a folder called levels, so that explains the
.
Also, here is the code for my level designer.
Main.cpp:

methods.h

Also attached is a readme for the designer.
Good luck with designer though the code is quite messy. You'd be better off making your own for the most part than trying to interpret mine. I don't even bother with it anymore. It just works.
Considering that though, you could just use my designer!

Important: To use any of this source code you must run in release mode.


Attachments

Login to view attachments
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 17th Oct 2011 23:09
I was just typing a similar suggestion, to store the level as numbers in a text file, like this:

3 2 7 7 1 0 5
3 3 1 2 0 0 5

and so on, where the numbers mean the kind of tile you want to put at that position, or zero for empty.

Quote: "different graphics cards read the images slightly differently"


I was surprised at that statement. Whatever the graphics card is, if a pixel is red, then it should be red. Can it be that the problem is rather an image compressing algorythm that alters the colour of pixels when you save the image from the editor? Or maybe transparency, anti-aliasing, or a problem with how your read / compare the pixel values from the image, or maybe how you load it? (dbLoadImage has an extra parameter that can tell the system to preserve the pixel-perfect image.) I'm just guessing without knowing the source code. The "image map" is not a bad idea and maybe you can still make it work.

About the slow scrolling: is the map much bigger than the screen? I once made a side-scoller demo that re-uses a limited number of sprites to display only those tiles that are currently visible on the screen. It should be faster than always updating the whole map. The disadvantage is that there is no one-to-one relationship between sprites and tiles, which might cause some difficulty if you check collisions and want to find out which tile the player collided with. I will post the link to that demo if you are interested.

Another way would be not to create sprites for tiles. Only create one master sprite for each tile, and display them with dbPasteSprite. Then they are pasted as images. This should be fast if you only paste those sprites whose coordinates are within the visible window. The disadvantage is that you completely lose the possibility to use dbSpriteCollision.

I don't have practice in 2D games, so I can't really judge what's fast or slow. I once tried to make a relatively big map out of tiles and even scrolling worked at a tolerable pace, only collision checks with a lot of tiles was slow. As soon as I reduced the collision tests to only those sprites that are in the vicinity of the player, it got much faster, so it's worth to look at that point as well if you already have collisions in the program.
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 17th Oct 2011 23:16 Edited at: 17th Oct 2011 23:29
I can't leave this code without comment:



Don't initialize a text letter-by-letter (not even mentioning that I don't think this works correctly for any number bigger than 9). Why not this?



I think you can use simple forward slash in path names, they must be doubled only if they are backslashes, but if it doesn't work, exchange for //. The main point is that numbers can easily be formatted into a text string with sprintf.
DeadTomGC
14
Years of Service
User Offline
Joined: 11th Aug 2010
Location: LU
Posted: 18th Oct 2011 01:32 Edited at: 18th Oct 2011 01:36
Thanks for the tip! I've had a class in Matlab that went over a nearly identical command, but I had no idea there was a command like that for C++.

It's true that my original code does not work for numbers greater than 9, but why?


Red Eye
15
Years of Service
User Offline
Joined: 15th Oct 2008
Location:
Posted: 18th Oct 2011 01:50
Quote: "It's true that my original code does not work for numbers greater than 9, but why?"


What's the difference between a 9 and a 10?

The element in array[element] may only consist of one element. Yet 10 has 2. 1 and 0.
DeadTomGC
14
Years of Service
User Offline
Joined: 11th Aug 2010
Location: LU
Posted: 18th Oct 2011 04:46
Oh, I found my mistake. I had been planning on the number being larger than one digit but just did my math wrong.


Login to post a reply

Server time is: 2024-10-02 21:39:07
Your offset time is: 2024-10-02 21:39:07