Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Creating a tilemap from image pixel data?  (Read 2307 times)

0 Members and 1 Guest are viewing this topic.

Noob

  • Newbie
  • *
  • Posts: 15
    • View Profile
Creating a tilemap from image pixel data?
« on: February 09, 2014, 06:53:19 pm »
Ok so my game is coming along nicely and I'm at the point were I want to make an effective way of creating maps.

I know there is the read from a text file or section of code way that could have a bunch of numbers and/or letters to represent different tiles that I currently have loaded from a sprite sheet.
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0,
1, 1, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3,
0, 1, 0, 0, 2, 0, 3, 3, 3, 0, 1, 1, 1, 0, 0, 0,
0, 1, 1, 0, 3, 3, 3, 0, 0, 0, 1, 1, 1, 2, 0, 0,
0, 0, 1, 0, 3, 0, 2, 2, 0, 0, 1, 1, 1, 1, 2, 0,
2, 0, 1, 0, 3, 0, 2, 2, 2, 0, 1, 1, 1, 1, 1, 1,
0, 0, 1, 0, 3, 2, 2, 2, 0, 0, 0, 0, 1, 1, 1, 1,
But that is visually unappealing and cumbersome to work with if I or a team member wanted to alter/create a map.


Is there a way of loading an image file and checking each pixel of that image line by line and fill in my array of tiles based on the colour value of each pixel?

Example


I want it to be able to read and identify the color of each pixel and draw a tile based on what the colour code is. I.e. if colour = #bb00bb then draw water, etc.

Does SFML have anything that might help with this? Anyone know of any good methods of approaching this or know of someone else who has implemented this?
Killing goblins at Lumbridge. Windows 7 - 64 bit. AMD Radeon HD 6700. Using VS2012 Express, SFML 2.1 - Static.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Creating a tilemap from image pixel data?
« Reply #1 on: February 09, 2014, 07:17:06 pm »
sf::Image::getPixel(...);
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Creating a tilemap from image pixel data?
« Reply #2 on: February 09, 2014, 07:50:21 pm »
You might also consider using a map editor like this free one: Mappy
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Creating a tilemap from image pixel data?
« Reply #3 on: February 09, 2014, 09:11:26 pm »
There is an implementation of not exactly that but of using shader to draw using image like your first one and texture atlas. It's a weird method but it works even on my Intel GPU (without glsl 1.30, so I guess '#version 130' is redundant in there) and is interesting approach. You'd probably have to preprocess your map to have colors that it expects before you would be able to use this code because color value is index of tile in this code.

Dbug's original concept topic http://en.sfml-dev.org/forums/index.php?topic=9937.msg68212#msg68212
My second version(Shameless self-promotion) https://github.com/SFML/SFML/wiki/Source%3A-ShaderTileMap

That code of mine is old and might need cleaning up or modifying to fit your style, design, remove my (old) namespaces etc. ;)
It's not that good but it works. :P
« Last Edit: February 09, 2014, 09:15:06 pm by FRex »
Back to C++ gamedev with SFML in May 2023

 

anything