So I'm working on this tank game and since I'm new to sfml I have several questions.
My first question is is it posible to have a background image and show the part that is undernith a circle and leave everything else black? Because I have those CircleShapes and for now I am calculating what part of the map is sapoust to be undernith the circle and I just don't like it and will like it even less when I add some more circles.
Second question, when you compile and run that code you will notice that cursors circle goes all the way on the right and bottom, but only taches the top and left edge. That is because I had to block it from going closer, because if it goes closer it simply dosen't display from some reason. Dose anyoe have any idea how to fix that?
And third, is it posible to set circle so that the part in the center is brightest and as you go furter from the center it becomes darker? Or will I need to use multiple circles and their outlines?
My code till now.
Map.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;
}