[sfml] Math help?

I'm not sure if this is the place for this, but I am struggling with doing the math for a game I am making.

I am making code to fill the screen(Stretch the tiles to it fits into the game screen)

Here's my code:
 
  setScale(_gameBorder.width / (_tileSize.x*_width), _gameBorder.height / (_tileSize.y*_height) );


which is:

width of the game screen / (amount in pixels of each tile*the amount of tiles)
to stretch the width and the same thing for the height.

The result I'm trying to get out of this equation is the size of the game screen divided by the size of all of the tile map, so I can put this in the setScale(Which sets the scale, 1 - same size, 10 - 10 times bigger ect)

the result I get is this: http://imgur.com/Nd5sR3K
the green and blue picture should take up the yellow bit, which should not be visible.

I'd be happy to give more code(Or all of the code if necessary, but I don't want to embarrass myself :P) in order to give more detail.

Side note: My suspicion is that is may be a rounding error somewhere
Last edited on
Hey, perhaps you could try the suggestion from here ? -
http://stackoverflow.com/questions/30203708/stretch-sfsprite-across-entire-window
I couldn't do this because my object is not a sprite, it is it's own class; thus has no getLocalBounds function.

Would it be of any use to share the class code?
Last edited on
How are you loading in the tile? Where are you saving it? If not a sprite.

Would it be of any use to share the class code?

Yes.
It is not a single tile, but a tile Map. I copied the actual code for the vertex array from the SFML wiki, but I wrote everything else.

Sorry it is probably sloppy code

TileMap.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#pragma once
#include <SFML/Graphics.hpp>

//Types of tiles
enum TileID { GRASS, WATER, NUMBER_OF_TYPES};

//Fill style: FILL-Strech to fill screen	| REPEAT-Don't strech	| NONE-No effects
enum FillStyle { FILL, REPEAT, NONE};

class TileMap : public sf::Drawable, public sf::Transformable
{
public:
	TileMap();
	void set_variables(int width, int height, const TileID *level, sf::Vector2u tileSize, FillStyle fillStyle);
	void create();
	
	void draw(sf::RenderWindow &window);
	~TileMap();
private:

	//Draw
	virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;

	//Fill Stylep
	FillStyle _fillStyle;

	//Amount of tiles width and height
	int _width;
	int _height;

	//Tile size
	sf::Vector2u _tileSize;

	//Texture Size
	sf::Vector2i _textureSize;

	//Level data
	const TileID *_level;

	//Vertex Array
	sf::VertexArray _vertex;

	//Texture
	sf::Texture _texture;

	//Game Border
	sf::VideoMode _gameBorder;

};




TileMap.cpp - lines 43 through 74 were copied and pasted, and changed to fit my needs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "TileMap.h"
#include <SFML/Graphics.hpp>
#include "GameEngine.h"
#include <iostream>
GameEngine engine;
extern GameEngine engine;


TileMap::TileMap()
{
}

void TileMap::set_variables(int width, int height, const TileID * level, sf::Vector2u tileSize, FillStyle fillStyle)
{
	//Load texture
	_texture.loadFromFile("./set.png");

	//Find size of each tile in texture | total size of texture devided by the number of different textures
	_textureSize.x = (_texture.getSize().x) / NUMBER_OF_TYPES;
	_textureSize.y = _texture.getSize().y;

	//Set other variables
	_width = width;
	_height = height;
	_level = level; 
	_fillStyle = fillStyle;
	_tileSize = tileSize;

	//If set to fill screen ignore tile size
	if (_fillStyle == FILL)
	{
		_tileSize.x = _textureSize.x;
		_tileSize.y = _textureSize.y;
	}

	//Get size of game screen from engine (Sizes of everything are relitive to this as a way of handling resolutions)
	_gameBorder = engine.getGameBorder();
}


void TileMap::create()
{
	//Set up vertex array
	_vertex.setPrimitiveType(sf::Quads);
	_vertex.resize(_width * _height * 4);

	 for (unsigned int i = 0; i < _width; ++i)
	 {
        for (unsigned int j = 0; j < _height; ++j)
        {
            // get the current tile number
            int tileNumber = _level[i + j * _width];

            // find its position in the tileset texture
            int tu = tileNumber % ( _texture.getSize().x / _textureSize.x );
            int tv = tileNumber / ( _texture.getSize().x / _textureSize.y );

            // get a pointer to the current tile's quad
            sf::Vertex* quad = &_vertex[(i + j * _width) * 4];

            // define its 4 corners
            quad[0].position = sf::Vector2f(i * _textureSize.x, j * _textureSize.y);
            quad[1].position = sf::Vector2f((i + 1) * _textureSize.x, j * _textureSize.y);
            quad[2].position = sf::Vector2f((i + 1) * _textureSize.x, (j + 1) * _textureSize.y);
            quad[3].position = sf::Vector2f(i * _textureSize.x, (j + 1) * _textureSize.y);

            // define its 4 texture coordinates
            quad[0].texCoords = sf::Vector2f(tu * _textureSize.x, tv * _textureSize.y);
            quad[1].texCoords = sf::Vector2f((tu + 1) * _textureSize.x, tv * _textureSize.y);
            quad[2].texCoords = sf::Vector2f((tu + 1) * _textureSize.x, (tv + 1) * _textureSize.y);
            quad[3].texCoords = sf::Vector2f(tu * _textureSize.x, (tv + 1) * _textureSize.y);
        }
	}
}


void TileMap::draw(sf::RenderWindow &window)
{
	if (_fillStyle == FILL)
	{
		setScale(_gameBorder.width / (_tileSize.x*_width), _gameBorder.height / (_tileSize.y*_height));
	}
	window.draw(*this);

}

TileMap::~TileMap()
{
}

void TileMap::draw(sf::RenderTarget & target, sf::RenderStates states) const
{
	//Apply transforfmation
	states.transform *= getTransform();

	// apply the tileset texture
	states.texture = &_texture;

	// draw the vertex array
	target.draw(_vertex, states);
}
Last edited on
Im going on a limb here since I can't know for sure, but The problem might not be that the green is as big as the screen and therefore covering it. But maybe it is as big as the screen, it is just not centered, so bits the bit that is missing from the bottom is the result of it going out of the screen up to the top?

Something like this, if it makes any sense to you - https://gyazo.com/d45ead473f1d4475cb38b2162bf1f099
Unfortunately this is not the case. I manually set the origin(Top left corner) of the object to be 0,0) and nothing happened. Just to check I moved the whole object down and right 10 pixels and got this, which means that it is infact smaller than it should be:

http://imgur.com/VFIPEQU

Try changing from -

setScale(_gameBorder.width / (_tileSize.x*_width), _gameBorder.height / (_tileSize.y*_height));

to -

setScale((double)_gameBorder.width / (_tileSize.x*_width), (double)_gameBorder.height / (_tileSize.y*_height));
This worked! I can't believe I didn't try it. I feel stupid now.

As a side note, are there any improvements I can make to the code? Like bad practices I shouldn't do that I did(Like if I used goto)
Last edited on
This worked! I can't believe I didn't try it. I feel stupid now.

Glad it worked out =)


I would say, not to use C-style enum and instead use C++ style enum class

Video explaining the difference -
https://www.youtube.com/watch?v=Pxvvr5FAWxg
Last edited on
Thanks for the advice! It is quite useful for my current situation because I have 3-4 enums overall.

I have one more question, right now I am using "NUMBER_OF_TYPES" to find the amount of entries in the enum; for some reason this feels "cheaty" to me, is there a proper way to find this out?
I have one more question, right now I am using "NUMBER_OF_TYPES" to find the amount of entries in the enum; for some reason this feels "cheaty" to me, is there a proper way to find this out?

Sorry but you're gonna have to explain more for the purpose of "NUMBER_OF_TYPES". I can see that it is an enum which holds the value "2" since it's in the third place. But why is it used to represent the amount of different textures?
I use it in the line: _textureSize.x = (_texture.getSize().x) / NUMBER_OF_TYPES;

which gets the width of the texture set and divides by the number of different textures in the image, which finds the width(and therefore height, as the textures are square) of each texture

There is no issue, it was just a formatting question. I have a more pressing issue now ( I am unlucky)
The tiles work now, but they make a mysterious blue line which I didn't notice at first. I'm not sure if it is a problem with SFML, the vertex array or a slight rounding error. Or even something else because the lines are 1 pixel wide

http://imgur.com/l2wvx5w
Topic archived. No new replies allowed.