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

Author Topic: SFML Game Development (book) adding tiles  (Read 3160 times)

0 Members and 1 Guest are viewing this topic.

SFMLNewGuy

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
SFML Game Development (book) adding tiles
« on: April 27, 2020, 02:21:32 am »
Hello,

I've been reading over this book, which I think the source code is actually on the SFML Github so that makes me think someone here made it. For those who have read it, I was wondering how someone might implement a tile system using the same engine the book walks you through. Preferably, it would be nice to see source code with the same engine customized with tiles and not overly done with other features (kept basic). I've read all the SFML books, but this is the only one that actually goes into implementing gameplay more complex and going past just the engine (forgot about the side scroller By Example does). So I've decided to read this again with more attention. One of my major problems is learning how to take, say, take this example, and expand on it my own way (or RPG genre from Airplane scroller). SFML Game Development By Example is one of my favorite books, but trying to add on to it is a bit complex still, which is why I came back to this book which implements a nice little engine and shows how to do gameplay content in a more straightforward way.

I figured I could make a tile system using the SceneNode. But I'm not exactly sure if that's a good way to do it. I'm not sure if it would involve changing too much. When I read Procedural Generation Content for C++, it had a Tile struct (which I'd consider a node because of the parent pointer) and allowed you to do pathfinding easy. Which is something I really don't want to make difficult, because I've had trouble implementing pathfinding outside of the way they show it.

I made a hardcoded example and I got tiles to appear instead of the desert scrolling background. But I want to implement this in a class that would be the most efficient way. The reason I'm hesitant is because of how the SceneNode interacts with the airplanes in a tree-like behavior. It's not exactly the same way I have learned to do tiles (from books and myself). For clarity, I would be changing the whole genre of the book.

I don't want to continue to ramble on, but any help would be appreciated (especially, with examples). I hope this makes sense.

Thank you! Be safe and have a nice day.

tldr; Using SFML Game Development, by Artur Moreira how can I implement a tile system efficiently and if anyone has examples using the concepts from this book I'd greatly appreciate it.

NOTE: I'm not talking about SFML Game Development By Example! Although, it is my favorite book!

EDIT: I did see a thread similar to this one here, but it didn't provide me with any help.
« Last Edit: April 27, 2020, 02:23:30 am by SFMLNewGuy »

SFMLNewGuy

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Re: SFML Game Development (book) adding tiles
« Reply #1 on: May 13, 2020, 04:27:59 am »
For those curious, I did speak to the author of SFML Game Development (Jan Haller) and he provided me with excellent information regarding adding a tile system. He suggested I provide the answer to my own link. This is what he recommends for those interested or possibly have similar goals.
Quote

From Jan Haller:

1. Make a data model (i.e. class/struct) for a tile. A tile may contain:
       
  • the tile type (Grass, Dirt, Trees, Water, ...) -- preferably a C++11 enum class
  • if you want, a variant (if you have multiple Grass variants, to make it look less regular) as an integer
  • the position (integer x/y coordinates, which express the index in the tile map) -- this is not needed inside the tile class, but can simplify things
  • if needed, extra topological information, such as elevation/height
  • possibly a reference to game elements, such as a structure constructed on it

 6. Make a data model for a tile map. A tile map is a collection of tiles (std::vector), together with its size.
    Design a nice TileMap class with encapsulation. Some challenges:
       
  • Tile& operator[] (sf::Vector2i tileCoord) to access a tile at given index (+ const overload)
  • accessors for size
  • bounds checking with assert
  • an easy way to iterate over all tiles, e.g. a method taking std::function which is applied to each tile
  • constructor with size + possibly tile generator function

 6. Create some tile graphics. Make sure each has the same square size, and store all in a single .png file.
    Load it as sf::Texture.

 7. Draw your tilemap using sf::VertexArray. This class can efficiently batch draw calls, so you won't need to draw each tile separately, resulting in considerable speedup.
    The vertex array can reference the texture you just loaded and is effectively an alternative to sf::Sprite.
    Choose the texture rect depending on tile type and variant.
    Consult SFML tutorial and documentation to get familiar with those (we also have some vertex array examples in the book, under Particles).

  8. Once your tile map is populated (steps 1+2) and rendered (step 4), it's time to integrate it into the
     book's code. Clone the official repo and start either at the latest chapter (10), or 9 if you don't need
     network.
     Add a new node class, which contains your TileMap instance and a vertex array. Override the
     draw() function to draw the vertex array. Override update() if you want to change the tilemap over time,
     or implement logic (such as collision with certain tiles).

Hope that helps as a start!
Jan

Additionally, when I asked if I should derive TileMap from SceneNode:

Quote
No, I would keep them separate. So you end up with 4 types:
  • enum class TileType
  • class Tile
  • class TileMap
  • class TileNode (or TileMapNode) -- this one will  hold a TileMap + sf::VertexArray instance.

Thanks again Jan!
« Last Edit: May 13, 2020, 04:31:19 am by SFMLNewGuy »