So, I have this image (the map) loaded, then puted into a texture.
Image map;
if(!map.loadFromFile("Map.jpg")) return 1;
const unsigned mapW = map.getSize().x, mapH = map.getSize().y;
Texture Map;
Map.loadFromImage(map, IntRect(0,0,mapW,mapH));
Then I added that texture to CircleShapes, one is in the middle and the other is where the mouse is.
CircleShape tankView(tankV);
tankView.setTexture(&Map);
tankView.setPosition(vMode.width/2-tankV, vMode.height/2-tankV);
CircleShape curView(cursorV);
curView.setTexture(&Map);
In the while loop I get XY of the mouse and see if the player moved (w,a,s,d) and acording to that I setPosition of the cursor's CircleShape and setTextureRect (part of the texture) that should be displayed in each of the CircleShapes.
All that works well, but I have a problem, the picture of the map is to small and when I tried to resise the image to a apropriete size I found out that there is a limit for the texture's picture size and it is way smaller that what I need. So I tried to put smaller width/height in setTextureRect, but then cursor's and centered CircleShape didn't overlap correctly and the cursors background looked like it moves when mouse moves. So, how do I zoom in?
My first idea was to have the background, which would be hidden and when a CircleShape is over it it gets displayed, what would be much easyer to work with, but I have no idea how to do that or is it even posible?
Ok, so, I tryed to use just texture without image and it works...
I'm gona send you my code and you'll see what I mean when you sompile it.
Map.jpg (http://paratime.ca/images/fantasy/dungeon-a-020.jpg)
#include <SFML/Graphics.hpp>
using namespace sf;
int main()
{
const unsigned tankV = 150, cursorV = 100;
const unsigned speed = 10;
VideoMode vMode(1024, 768, 32);
RenderWindow window(vMode, "Tank Wars", Style::Fullscreen);
window.setMouseCursorVisible(false);
window.setFramerateLimit(60);
Texture Map;
if(!Map.loadFromFile("Map.jpg")) return 1;
Map.setSmooth(true);
const unsigned mapW = Map.getSize().x, mapH = Map.getSize().y;
unsigned positionX = mapW/2, positionY = mapH/2, cursorX = positionX-200, cursorY = positionY-200;
CircleShape tankView(tankV);
tankView.setTexture(&Map);
tankView.setPosition(vMode.width/2-tankV, vMode.height/2-tankV);
tankView.setFillColor(Color(255,255,255,200));
CircleShape curView(cursorV);
curView.setTexture(&Map);
curView.setFillColor(Color(255,255,255,200));
while(window.isOpen())
{
Event event;
while(window.pollEvent(event))
{
switch(event.type)
{
case Event::MouseMoved:
if(event.mouseMove.x > cursorV)
cursorX = event.mouseMove.x;
else cursorX = cursorV;
if(event.mouseMove.y > cursorV)
cursorY = event.mouseMove.y;
else cursorY = cursorV;
break;
case Event::Closed:
window.close();
break;
case Event::KeyPressed:
switch(event.key.code)
{
case Keyboard::Up:
case Keyboard::W:
if (positionY > 100)
positionY -= speed;
break;
case Keyboard::Down:
case Keyboard::S:
if (positionY < mapH-100)
positionY += speed;
break;
case Keyboard::Left:
case Keyboard::A:
if (positionX > 100)
positionX -= speed;
break;
case Keyboard::Right:
case Keyboard::D:
if (positionX < mapW-100)
positionX += speed;
break;
case Keyboard::Escape:
window.close();
break;
default: break;
}
break;
default:
break;
}
}
curView.setTextureRect(IntRect(positionX - vMode.width/2 + cursorX - cursorV,
positionY - vMode.height/2 + cursorY - cursorV,
cursorV*2, cursorV*2));
curView.setPosition(cursorX-cursorV, cursorY-cursorV);
tankView.setTextureRect(IntRect(positionX - tankV, positionY - tankV,
tankV*2, tankV*2));
window.clear(Color(0,0,0));
window.draw(curView);
window.draw(tankView);
window.display();
}
return 0;
}