Author Topic: Beginner question (SDL2 & C++): How to build the grid  (Read 10088 times)

Eagoth

  • Newcomer
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Beginner question (SDL2 & C++): How to build the grid
« on: February 16, 2017, 07:49:11 PM »
Hello, guys.

I've been following lazyfoo's SDL2 tutorials and I have in mind to try a little roguelike using SDL2 and C++ (no libtcod yet).

I'm pretty new to all this (even C++), and I wondered what's the best approach to create the grid where every tile of the map is displayed. It's, like, create a shitload of textures in an array? My initial idea is to have a png with all the ascii characters (like libtcod, actually), and then mask the png for each tile, but I'm not sure on how to start.

The idea is also to work later with REXPaint files, because I have some maps drawn there.

Thanks! And sorry if this is a little chaotic and noobish question (many more to follow) :þ.

Elronnd

  • Newcomer
  • Posts: 16
  • Karma: +0/-0
    • View Profile
    • NetHack and Slash'EM EU server
Re: Beginner question (SDL2 & C++): How to build the grid
« Reply #1 on: March 13, 2017, 06:44:28 AM »
It would probably make a lot more sense to use something like sdl-ttf -- https://www.libsdl.org/projects/SDL_ttf/docs/SDL_ttf.html -- which can handle that for you.  You have access to a lot more glyphs, and you don't have to do as much by hand.  https://github.com/Elronnd/roguelike-with-no-name/blob/master/interface/sdl-ttf.cc here's my sdl-ttf code.  It's kind of ugly, but it should give you the hang of how it works.  Be aware that if you do use sdl-ttf, you will have to do a lot of extra annoying work converting between sdl1 and sdl2 types -- sdl_ttf uses sdl1 types.
Wishes, wishes.  Wish in one hand and do something else in the other, and squeeze them both and see which comes true

 —Roger Zelazny, Nine Princes in Amber

Looks like a fish, moves like a fish, steers like a cow.

 —Douglas Adams, Hitchhiker's Guide to the Galaxy Fit the Fifth

Eagoth

  • Newcomer
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: Beginner question (SDL2 & C++): How to build the grid
« Reply #2 on: March 13, 2017, 10:05:50 AM »
Thanks, but I ended building a vector of SDL_Rect pointers and using REXSpeeder to read .xp files.

Now I'm trying to apply color changes to each tile (foreground and background), because you need (I think) to get the pixels from a surface and I only have the texture. Let's see if I can access the pixels in some way.

Krice

  • (Banned)
  • Rogueliker
  • ***
  • Posts: 2316
  • Karma: +0/-2
    • View Profile
    • Email
Re: Beginner question (SDL2 & C++): How to build the grid
« Reply #3 on: March 13, 2017, 12:55:00 PM »
Now I'm trying to apply color changes to each tile (foreground and background), because you need (I think) to get the pixels from a surface and I only have the texture. Let's see if I can access the pixels in some way.

With SDL2 it's a bad idea to change the surface on fly. The surface in SDL2 (SDL_Surface) is only a stage (data storage for pixels) where you can easily draw stuff on that surface and when it's ready create a texture out of it, and the idea is that texture is static (not changed). So if you want things like colors for fonts then you have to create font sets for each color in the texture and point to proper color of a font. It's easy and also faster than the alternative solution (draw fonts pixel at a time!).

The grid or 2D array (level) usually has two components. The actual level grid has only data about tiles (what the tiles is etc.) and graphical tiles are displayed using that data. So those two (displaying and map data) are separate or they should be.

Eagoth

  • Newcomer
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: Beginner question (SDL2 & C++): How to build the grid
« Reply #4 on: March 13, 2017, 01:05:18 PM »
What? Do you mean to add symbol variations for each color to the tileset.png? That sounds pretty crazy, what if I want hundreds of colors?