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

Author Topic: Tank Wars  (Read 3571 times)

0 Members and 1 Guest are viewing this topic.

zoran404

  • Newbie
  • *
  • Posts: 41
    • View Profile
Tank Wars
« on: March 17, 2013, 11:25:55 am »
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?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Tank Wars
« Reply #1 on: March 17, 2013, 11:31:12 am »
Please post in the help section and use a more meaningful thread title. This section is to show projects.

Why don't you load the sf::Texture directly from the file, what do you need sf::Image for?

You can either scale the circle shape or zoom in with the view. Take a look at the SFML documentation to see how this exactly works. Probably, you also need to adapt the mouse cursor logic.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

zoran404

  • Newbie
  • *
  • Posts: 41
    • View Profile
Re: Tank Wars
« Reply #2 on: March 17, 2013, 03:08:13 pm »
I used the image so I could get it's size so I could set it to the texture.

I'll look into view...

And what do you mean about the mouse cursor logic? And while we are on the subject when cursor gets so close to the top or left corner, because the circle is around the cursor position, that the circle is kindowa sapoust to start displaying from negative coordinates (top-left pixel is on negative coordinats), so it just dosen't display at all, so I prevented it from going closer to the top or left than it's radius. But it there a way to make it go all the way to top and left like it dose on right and botton side?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Tank Wars
« Reply #3 on: March 17, 2013, 03:22:37 pm »
I used the image so I could get it's size so I could set it to the texture.
What do you need the size for? Why can't you use sf::Texture::loadFromFile()?

And what do you mean about the mouse cursor logic?
You need to make sure the cursor's position is still handled correctly with scaled shapes or zoomed views.

And while we are on the subject when cursor gets so close to the top or left corner, because the circle is around the cursor position, that the circle is kindowa sapoust to start displaying from negative coordinates (top-left pixel is on negative coordinats), so it just dosen't display at all
What does "kindowa sapoust" mean? If you want help, express yourself clearly.

There is no reason why the shape cannot have negative coordinates. There must be an error on your side, which I won't guess...
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

zoran404

  • Newbie
  • *
  • Posts: 41
    • View Profile
Re: Tank Wars
« Reply #4 on: March 17, 2013, 03:44:23 pm »
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

#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;
}
 

zoran404

  • Newbie
  • *
  • Posts: 41
    • View Profile
Re: Tank Wars
« Reply #5 on: March 17, 2013, 05:05:01 pm »
Moved here.

 

anything