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

Author Topic: <Tank Wars> need help with multiple grafic things  (Read 4376 times)

0 Members and 1 Guest are viewing this topic.

zoran404

  • Newbie
  • *
  • Posts: 41
    • View Profile
<Tank Wars> need help with multiple grafic things
« on: March 17, 2013, 05:03:30 pm »
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;
}

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: <Tank Wars> need help with multiple grafic things
« Reply #1 on: March 17, 2013, 05:52:13 pm »
I'm not quite sure if I've understood your question correctly, but it seems that you want exactly what my Flashlight example does, right?
Obviously it's just an example to show how one can do things like that and you'll have to adapt it to your game.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

zoran404

  • Newbie
  • *
  • Posts: 41
    • View Profile
Re: <Tank Wars> need help with multiple grafic things
« Reply #2 on: March 17, 2013, 07:29:13 pm »
I'll take a look at it when I get back...

zoran404

  • Newbie
  • *
  • Posts: 41
    • View Profile
Re: <Tank Wars> need help with multiple grafic things
« Reply #3 on: March 18, 2013, 12:26:00 am »
I have gone trught that code I got the idea how to do that. But I'm gona continue this later.

But one thig confuses me about your program. In the comments it says flashlght is a circle, but I don't see where in the code you said it is circle and when I run it it displays almost a line and it only displays if my mouse is on the bottom.

By the way, in parameters I had to write
-mwindows
"C:/Program Files/Dev-Cpp/lib/libsfml-graphics.a"
"C:/Program Files/Dev-Cpp/lib/libsfml-window.a"
"C:/Program Files/Dev-Cpp/lib/libsfml-system.a"
but how do I write it like I did with -mwindows ?