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

Author Topic: The 2D Game - drawing a tilemap with a shader  (Read 13097 times)

0 Members and 1 Guest are viewing this topic.

Dbug

  • Newbie
  • *
  • Posts: 5
    • View Profile
The 2D Game - drawing a tilemap with a shader
« on: December 10, 2012, 09:12:12 pm »
Hi,

I started working on a small indy game in my spare time, and decided to document the progress on a blog, but in a way that can be useful for other people.

I started by a series of article explaining how to draw a tilemap efficiently, the final article was posted yesterday with actual sfml code and shader:

http://www.the2dgame.com/index?page=articles&ref=ART9

Hope you like it, don't hesitate to comment, for good or bad.

Thanks.

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: The 2D Game - drawing a tilemap with a shader
« Reply #1 on: December 10, 2012, 10:09:29 pm »
The effect and method is great even though it's not rocket science and people did this before but this is first time I see that in SFML, I was going to read through the source and shader codes but I've noticed the
http://creativecommons.org/licenses/by-sa/3.0/ license. The share alike part sounds bad.
Back to C++ gamedev with SFML in May 2023

cire

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Re: The 2D Game - drawing a tilemap with a shader
« Reply #2 on: December 11, 2012, 01:16:11 am »
http://blog.tojicode.com/2012/07/sprite-tile-maps-on-gpu.html#more

Gives a decent overview of it and provides the code (although not C++, nor SFML, the shaders are glsl and it's fairly easy to convert) with a permissive license.

Dbug

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: The 2D Game - drawing a tilemap with a shader
« Reply #3 on: December 11, 2012, 09:26:41 am »
All the license means really is that if you made a change to the system, to make it perhaps more efficient or bug free, you are supposed to make the changes available to the public.

It does not mean your whole project has to be given away: Just publishing the modifications you did to these particular parts of code is enough. No need to publish your whole project as open source or whatever.

At least that's the way I interpret it, I don't see that as very restrictive.

I know this code is not rocket science, but it appears that most people are still using for loops or vertex buffers to draw tilemaps, so obviously the method is not that well known by most people.

My objective was just to make a minimal demo to make it easy to understand. I don't pretend I invented the method :)


FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: The 2D Game - drawing a tilemap with a shader
« Reply #4 on: January 13, 2013, 08:33:46 am »
Did you solve the problem of loading new map pieces during player movement around the map?
It seems to be simply too slow, takes way over one 60th of a second.
Back to C++ gamedev with SFML in May 2023

N1ghtly

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
Re: The 2D Game - drawing a tilemap with a shader
« Reply #5 on: January 14, 2013, 07:07:41 pm »
Your website is absolutely awesome!
I read all your articles, keep it up!

Dbug

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: The 2D Game - drawing a tilemap with a shader
« Reply #6 on: January 15, 2013, 07:01:31 pm »
Did you solve the problem of loading new map pieces during player movement around the map?
It seems to be simply too slow, takes way over one 60th of a second.
Well, it's a matter of granularity, typically a 256x256 tilemap texture resolves as a 8192x8192 texture when rendered (using 32x32 tiles).
Uploading to vram a 256x256 pixels texture should be very fast, many games are doing that all the time.

At most you need 4 tilemap textures at the same time on screen, when you do transitions from a sector to another one from a corner.

You need a way to predict the direction of the player and make sure you upload the tilemaps before you need them, a possibility would be to use the position toward the center coordinate of the tilemap on which the player is located as a way to know which ones to load.

I made my tilemap editor in sfml, and I'm painting tiles directly on the texture and upload them on the graphic card immediately and has not noticed any drop in frame rate doing that, so there must be something different in the way you did it. Could you paste some code?

Your website is absolutely awesome!
I read all your articles, keep it up!
Glad you like them :)
I'm currently busy with some other stuff I promised to other people, but as soon as I am done with these duties I will go back to my sfml stuff with some new articles.

Thanks!


FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: The 2D Game - drawing a tilemap with a shader
« Reply #7 on: February 01, 2013, 01:25:52 am »
Is it supposed to work for size different than 256x? Your example breaks for me if I change all 256s to 128s.
I am working on shader that'll support any data and tile textures and with 32x32 tiles.
I'll make a thread if I succeed.
Is it enough for your copyright if I paste a comment like that in my class code and publish my class and shaders?
        //The code on which this class implementation and shader code is based are subject to following copyright notice:

        //
        // Copyright (c) 2012 by Mickaël Pointier.
        // This work is made available under the terms of the Creative Commons Attribution-ShareAlike 3.0 Unported license,
        // http://creativecommons.org/licenses/by-sa/3.0/.
        //

        //Which is not included in shader source literals below to save space in final executable -FRex

Edit:
I think I might have done it, I need to add some things like mixing alpha of map with alpha of tile, using green channel to provide more data up to 32x32 spreadsheet and maybe use blue channel for something,
I'd appreciate anyone downloading and testing that exe(it's called SoundEngine by my mistake..):
https://docs.google.com/file/d/0B8dEkQw1a4WvVF96RW90WDhnRDg/edit?usp=sharing

In attachment there is sceen of correct output.

[attachment deleted by admin]
« Last Edit: February 01, 2013, 02:36:12 am by FRex »
Back to C++ gamedev with SFML in May 2023

Dbug

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: The 2D Game - drawing a tilemap with a shader
« Reply #8 on: February 02, 2013, 10:36:43 pm »
Is it supposed to work for size different than 256x? Your example breaks for me if I change all 256s to 128s.
I am working on shader that'll support any data and tile textures and with 32x32 tiles.
Yes it is possible. There are actually two ways to achieve that:
- Modify the shader constants so the computations to find the tile and texture work for the particular size
- Modify the shader code to get the textures dimensions that are passed as parameters

The first is easy but not flexible, the second will auto-magically  works whatever sizes you give.

I had a prototype using the second method but it made the code very ugly and complex for no particular reason since in my case I had decided to use 256x256 tilemaps and sizes that are 32x32 tiles.

I'll make a thread if I succeed.
Is it enough for your copyright if I paste a comment like that in my class code and publish my class and shaders?
        //The code on which this class implementation and shader code is based are subject to following copyright notice:

        //
        // Copyright (c) 2012 by Mickaël Pointier.
        // This work is made available under the terms of the Creative Commons Attribution-ShareAlike 3.0 Unported license,
        // http://creativecommons.org/licenses/by-sa/3.0/.
        //

        //Which is not included in shader source literals below to save space in final executable -FRex
I guess that's fine, perhaps just add a link to my website? Like for example:
// Copyright (c) 2012 by Mickaël Pointier. (http://www.the2dgame.com)

Have fun!
(I tried your executable, it works fine here)

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: The 2D Game - drawing a tilemap with a shader
« Reply #9 on: February 02, 2013, 10:53:03 pm »
Quote
Yes it is possible. There are actually two ways to achieve that:
It doesn't work with other sizes as it is and that was my question.

I'll post the class to the wiki soon-ish.(Or not, seems I still have a bug in it).
Edit2: I'm getting shoddy, there was no bug, I confused row major order with column major order.
« Last Edit: February 02, 2013, 11:53:07 pm by FRex »
Back to C++ gamedev with SFML in May 2023