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

Author Topic: Questions about making tiled game  (Read 4196 times)

0 Members and 1 Guest are viewing this topic.

MrOnlineCoder

  • Jr. Member
  • **
  • Posts: 87
    • View Profile
Questions about making tiled game
« on: September 17, 2016, 08:15:25 pm »
Hello. I am making tiled RPG game (all action will be on tiled map with top-down  view). So, questions:
1.How to draw and interact with large map (for example 1024*1024 tiles)
2.How to make mobs and player damagable? I mean how to watch when mob is attacked and when not (should I place this logic inside player, world class or somewhere)?

Tukimitzu

  • Full Member
  • ***
  • Posts: 117
  • Anti-Hero Member
    • View Profile
Re: Questions about making tiled game
« Reply #1 on: September 18, 2016, 06:21:49 pm »
Your questions are too broad, so I'll give you a broad answer:
1.1) How to draw:
A tile has at least 3 properties: width, height and a drawable (which can be a sprite, a colored rectangle, etc). A map is composed of multiple tiles, which can be represented by a 2 by 2 matrix:

int TileMap[width][height] =
{1, 0, 0,
 0, 2, 0,
 0, 0, 3};

Given that you know which tile should be drawn at each position of the map, your drawing function will iterate through each position, get that tile and draw at that position.

for (int i = 0; i < width; i++){
  for (int j = 0; j < height; j++){
    tile[i][j].setPosition(xOrigin + i*tileWidth, yOrigin + j*tileHeight);
    window.draw(tile[i][j]);
  }
}

1.2) and interact with a large map:
If by interact you mean edit it, there are many ways. See how this guy did it. For you that are just begining, hard-coding your tile matrix is the way to do it (just like I did at 1.1).
If you mean player interactions with tiles, like collision, somewhere in your game loop (which is a key expression that you should google), you will check if the tile is, let's say, solid, and if the player is inside that tile. If so, you will correct the player position, because he's not supposed to go there.

2.1) How to make mobs and player damageable?
What is damage? Damage is a decrease of health. What is health? Health is a number. If your mobs have these members defined inside their classes, it's just a matter of manipulating these values. Let's say the player loses health if he collides (touches) with a mob:

player.health -= enemy.damage;

2.2) I mean how to watch when mob is attacked and when not (should I place this logic inside player, world class or somewhere)?
Do it the way it feels more natural to you. Back to the game loop, you can make it one of 2 ways (and all the other ways I'm choosing to ignore):

while (game.isRunning()){
  for (GameObject& object : objects){
    updatePosition(object);
    updateHealth(object);
  }
}

while (game.isRunning()){
  for (GameObject& object : objects){
    object.updatePosition();
    object.updateHealth();
  }
}

First approach the object contains only data (position, health, speed, ...), and this data is updated by a third party. Second approach the object contains both data and the manipulators of this data.

MrOnlineCoder

  • Jr. Member
  • **
  • Posts: 87
    • View Profile
Re: Questions about making tiled game
« Reply #2 on: September 18, 2016, 09:12:49 pm »
Thank you. I will concretize my question about:
Drawing map
How to draw large map efficiently? I think drawing 1024*1024 = 1048576 tiles is too hard for GPU. How can I speed it up?

Tukimitzu

  • Full Member
  • ***
  • Posts: 117
  • Anti-Hero Member
    • View Profile
Re: Questions about making tiled game
« Reply #3 on: September 19, 2016, 01:20:13 am »
You only have to draw the tiles that are within your window sf::View boundaries.
Example:

int tileHeight = 32;
int tileWidth  = 32;
int worldHeight = 1024;    //in tiles
int worldWidth  = 1024;    //in tiles

double viewWidth  = 1280;
double viewHeight = 720;
double viewTop = 0.0;
double viewLeft = 600.0;

int viewHeightInTiles = viewHeight/tileHeight;
int viewWidthtInTiles = viewWidth/tileWidth;

// Note that you also want to draw the tiles that are partially shown in your view.
// That's why -1 here...
int i0 = max(0, viewLeft/tileWidth - 1);
int j0 = max(0, viewTop/tileHeight - 1);
// and +1 here
int iMax = min(worldWidth, i0 + viewWidthInTiles + 1);
int jMax = min(worldHeight, j0 + viewHeightInTiles + 1);

for (int i = i0; i < iMax; i++){
  for (int j = j0; j < jMax; j++){
    tile[i][j].setPosition(i*tileWidth, j*tileHeight);
    window.draw(tile[i][j]);
  }
}

No matter how big your world is, you will always draw a maximum of (viewWidthtInTiles + 1) * (viewHeightInTiles + 1).
« Last Edit: September 19, 2016, 01:23:12 am by Tukimitzu »

Yohdu

  • Newbie
  • *
  • Posts: 26
    • View Profile
    • Email
Re: Questions about making tiled game
« Reply #4 on: September 26, 2016, 09:01:26 pm »
There is a faster way, you can actually draw your 1024x1024 tiles in a single draw operation using vertices. Have a look at the tilemap example here : http://www.sfml-dev.org/tutorials/2.4/graphics-vertex-array.php

Tukimitzu

  • Full Member
  • ***
  • Posts: 117
  • Anti-Hero Member
    • View Profile
Re: Questions about making tiled game
« Reply #5 on: September 27, 2016, 03:50:32 pm »
Interesting... But is it always faster than multiple draw(sf::Sprite) calls? Or if your map is super duper large then it becomes more efficient to draw only the tiles within your sf::View?
« Last Edit: September 28, 2016, 04:19:15 am by Tukimitzu »

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: Questions about making tiled game
« Reply #6 on: September 27, 2016, 06:45:13 pm »
Generally, yes. As you said, it's multiple draw calls (using sprites) vs one draw call (vertex arrays).

And yes, if your vertex array is large enough, then culling becomes useful.

Yohdu

  • Newbie
  • *
  • Posts: 26
    • View Profile
    • Email
Re: Questions about making tiled game
« Reply #7 on: September 27, 2016, 07:57:34 pm »
Quote
Interesting... But is it always faster then multiple draw(sf::Sprite) calls? Or if your map is super duper large then it becomes more efficient to draw only the tiles within your sf::View?

I'm not an expert so I cannot answer with absolute certainty, but yes I would assume that at some point it would become more efficient to draw only the tiles in the sf::View.

But that's actually a question I've been asking to myself : doesn't sf::View mechanism already imply that only visible items are drawn ?

If not, there is still the possibility to draw what the sf::View sees in a single draw operation using vertices ;)

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: Questions about making tiled game
« Reply #8 on: September 27, 2016, 11:45:55 pm »
But that's actually a question I've been asking to myself : doesn't sf::View mechanism already imply that only visible items are drawn ?

What is made visible is done AFTER all drawing has been done. If you're not culling things yourself, the draw calls are still being made, with data still being sent to the GPU. If you cull before drawing, the essentially "useless" draw calls and data transfers are not done.

Tukimitzu

  • Full Member
  • ***
  • Posts: 117
  • Anti-Hero Member
    • View Profile
Re: Questions about making tiled game
« Reply #9 on: September 28, 2016, 08:45:47 pm »
Alright, good to know, thank you both.

 

anything