Hi, this is basically just a compressed version of this thread:
http://en.sfml-dev.org/forums/index.php?topic=15414.0i figured that this is better suited in this section of the forum.
So in short:
i want to split my window in three (well known) parts basically:
1) game region where the game itself is drawn e.g. the map, units and everything else
2) minimap in the typical left bottom corner
3) unitmenu in the bottom right part of the screen for the player to give commands to the units
i ignored the third point for now, so im messing around with sf::View to get what i want.
i was told that to have the minimap display the whole map i just need to set its view to the respective size of the whole map in pixel. This is what i did, but the map gets displaced that way, which i do not want to happen.
see for yourself:
when i do this i get this:
the view is shifted by some offset when i set the size of it to the size of the whole tilemap.
all i did was to add one line, which does what you told me:
sf::View mapview(app_window.getDefaultView());
sf::View minimap(app_window.getDefaultView());
//sf::View menu(app_window.getDefaultView());
mapview.setViewport(sf::FloatRect(0.f,0.f,1.f,0.75f));
minimap.setViewport(sf::FloatRect(0.f, 0.75, 0.5, 0.25));
minimap.setSize(20*32,20*32); // my tilemap is 20 x 20 with each tile having the size of 32 x 32 pixel
but i don't want this behaviour, i want the minimap to be drawn at the position specified in "setViewport" (well to be precise i want the map to be drawn at the top left corner of that viewport)
what i want is this (note: minimap does not show the whole map so it is not what im trying to achieve):
my code is the following:
#include <SFML/Graphics.hpp>
#include <iostream>
sf::Texture m_tileset;
sf::VertexArray m_vertices;
void setTilemap()
{
m_tileset.loadFromFile("Tileset.png");
m_vertices.setPrimitiveType(sf::Quads);
m_vertices.resize(20*20*4); // we use some dummy numbers to get a mapwidth > renderwindow_width
for (unsigned int i = 0; i < 20; ++i)
{
for (unsigned int j = 0; j < 20; ++j)
{
//get the current tile number
int tileNumber = 34;
// find its position in the tileset texture
int tu = tileNumber % (m_tileset.getSize().x / 32);
int tv = tileNumber / (m_tileset.getSize().x / 32);
// get a pointer to the current tile's quad
sf::Vertex* quad = &m_vertices[(i + j * 20) * 4];
// define its 4 corners
quad[0].position = sf::Vector2f(i * 32, j * 32);
quad[1].position = sf::Vector2f((i + 1) * 32, j * 32);
quad[2].position = sf::Vector2f((i + 1) * 32, (j + 1) * 32);
quad[3].position = sf::Vector2f(i * 32, (j + 1) * 32);
// define its 4 texture coordinates
quad[0].texCoords = sf::Vector2f(tu * 32, tv * 32);
quad[1].texCoords = sf::Vector2f((tu + 1) * 32, tv * 32);
quad[2].texCoords = sf::Vector2f((tu + 1) * 32, (tv + 1) * 32);
quad[3].texCoords = sf::Vector2f(tu * 32, (tv + 1) * 32);
}
}
}
int main() {
// Create the main SFML window
float width = 320.f;
float height = 420.f;
sf::RenderWindow app_window( sf::VideoMode(width,height), "SFGUI Canvas Example", sf::Style::Titlebar | sf::Style::Close );
// Start the game loop
app_window.setFramerateLimit(20);
setTilemap();
sf::RenderStates states;
states = &m_tileset;
sf::View mapview(app_window.getDefaultView());
sf::View minimap(app_window.getDefaultView());
//sf::View menu(app_window.getDefaultView());
mapview.setViewport(sf::FloatRect(0.f,0.f,1.f,0.75f));
minimap.setViewport(sf::FloatRect(0.f, 0.75, 0.5, 0.25));
minimap.setSize(20*32,20*32);
while ( app_window.isOpen() ) {
sf::Event event;
while ( app_window.pollEvent( event ) ) {
// Close window : exit
if ( event.type == sf::Event::Closed ) {
app_window.close();
}
}
app_window.clear();
app_window.setView(mapview);
app_window.draw(m_vertices,states);
app_window.setView(minimap);
app_window.draw(m_vertices,states);
app_window.display();
}
return EXIT_SUCCESS;
}
the used tileset can be found in the attachment.
why is the minimap displaced when i set its size to the mapsize? i expected (and observed) this behaviour when calling sf::View::zoom(), but not for sf::View::setSize().
Advice is greatly appreciated.