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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - Ivan

Pages: [1]
1
Graphics / How to fix white square problem when used vectors?
« on: August 09, 2013, 04:54:25 pm »
Hi, first of all I know the "White square problem" described in the sprite tutorial. I understand it.

In my case, I create a Player class with a sprite and texture members. In main code, when  a user press M key, create a new instance of Player and add it into a vector. I known that the elements of a vector move in memory when the vector change and then lost the reference of the sprite texture.

Now, my question is how I can fix it? I tried with add setTexture every time I draw, but I don't know if its a good solution.

I attach an screenshoot about I'm saying.



I wrote a minimal example. This is the code, can download here.
I use SFML-2.1 with Code:Blocks (Windows 8 Pro, 32bits)

Player.h
//Player class

#ifndef PLAYER_H
#define PLAYER_H

#include <SFML/Graphics.hpp>
#include <iostream>

class Player
{
    public:
        Player(sf::Vector2i position, sf::Color colorSpr,  sf::String filenameSpr);
        ~Player();
        void Draw(sf::RenderWindow &Window);

        sf::Sprite sprPlayer;
        sf::Texture texturePlayer;
        int sprWidth, sprHeight;

    protected:
    private:
};

#endif
 

Player.cpp
#include "Player.h"

//constructor
Player::Player(sf::Vector2i position, sf::Color colorSpr,  sf::String filenameSpr)
{
    if(!texturePlayer.loadFromFile(filenameSpr, sf::IntRect(0, 0, 32, 32)))
        std::cout << "Error, can't load SpriteSheet " << std::endl;

    sprPlayer.setTexture(texturePlayer);
    sprPlayer.setColor(colorSpr);
    sprWidth = sprPlayer.getTextureRect().width;
    sprHeight = sprPlayer.getTextureRect().height;
    sprPlayer.setPosition(position.x * sprWidth, position.y * sprHeight);
}

//destructor
Player::~Player()
{
}

//draw player
void Player::Draw(sf::RenderWindow &Window)
{
    //sprPlayer.setTexture(texturePlayer); //<- with this it works
    Window.draw(sprPlayer);
}
 

Main.cpp

#include <SFML/Graphics.hpp>
#include <vector>
#include "Player.h"

const int maxPlayers = 10;

//colors for players
sf::Color pColors[maxPlayers] = {sf::Color(255, 0, 0, 255), sf::Color(0, 255, 0, 255),
                                 sf::Color(100, 100, 0, 255), sf::Color(0, 0, 255, 255),
                                 sf::Color(80, 200, 120, 255), sf::Color(100, 100, 155, 255),
                                 sf::Color(123, 160, 90, 255), sf::Color(200, 0, 255, 255),
                                 sf::Color(254, 195, 172, 255), sf::Color(255, 255, 255, 255)
                                   };
sf::String spriteSheets[maxPlayers] = {"punky2.png", "punky.png", "punky.png", "punky.png", "punky.png",
                                       "punky.png", "punky.png", "punky.png", "punky.png", "punky.png"
                                      };
int main()
{
    sf::Vector2i playerPosition(0, 0);
    int numRivals = 0;

    std::vector <Player> rivals;
    rivals.reserve(maxPlayers);

    sf::RenderWindow Window(sf::VideoMode(640, 480, 32), "Test");
    Window.setKeyRepeatEnabled(false);

    //create player
    Player hero(playerPosition, pColors[0], spriteSheets[0]);

    while (Window.isOpen())
    {
        sf::Event event;
        while (Window.pollEvent(event))
        {
            switch (event.type)
            {
            case sf::Event::Closed:
                {
                    Window.close();
                    break;
                }
            case sf::Event::KeyPressed:
                if (event.key.code == sf::Keyboard::Escape)
                {
                    Window.close();
                }
                else if (event.key.code == sf::Keyboard::Right)
                {
                    hero.sprPlayer.move(3, 0);
                }
                else if (event.key.code == sf::Keyboard::Left)
                {
                    hero.sprPlayer.move(-3, 0);
                }
                else if (event.key.code == sf::Keyboard::Down)
                {
                    hero.sprPlayer.move(0, 3);
                }
                else if (event.key.code == sf::Keyboard::Up)
                {
                    hero.sprPlayer.move(0, -3);
                }
                else if (event.key.code == sf::Keyboard::M)
                {
                    if (numRivals < maxPlayers - 1)
                    {
                        numRivals++;
                        Player rival(sf::Vector2i(3 + numRivals, 0), pColors[numRivals], spriteSheets[numRivals]);
                        rivals.push_back(rival);
                    }
                }
                break;
           }
        }

        //draw stuff
        hero.Draw(Window);
        if (numRivals > 0)
        {
            for (int k = 0; k < rivals.size(); k++)
            {
                rivals[k].Draw(Window);
            }
        }

        Window.display();
        Window.clear(sf::Color(255, 255, 0, 255));
    }
    return 0;
}
 


2
Network / [SOLVED]How to limit the number of TCP clients
« on: August 06, 2013, 05:44:51 pm »
I’m making a simple game with TCP Client/Server communication. In the server I have a TcpListener and SocketSelector.

I want to limit the number of clients that can connect to the server to 4. My question is in SFML as I can tell to the server that if have 4 clients, do not accept more connection requests and stop listening new connections? I try with listener.close() but then does not receive data from the existing 4 players.

Any idea? Thanks.

3
General discussions / Thank You
« on: April 30, 2013, 11:28:09 pm »
Hi, I just discovered SFML some weeks ago and I still learning this great API.
Today I have seen the new website and downloaded the final 2.0 release. It looks awesome. Thank you Laurent for your work.

#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600, 32), "Hello SFML forum");

    sf::Text message;
    sf::Font font;
    if (!font.loadFromFile("Font1.ttf"))
        return EXIT_FAILURE;

    message.setString("Hello SFML forum users, I'm Ivan");
    message.setFont(font);
    message.setCharacterSize(16);
    message.setColor(sf::Color(255, 255, 0, 255));
    message.setPosition(sf::Vector2f(20, 20));

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed ||
                event.key.code == sf::Keyboard::Escape)
                window.close();
        }

        window.clear(sf::Color::Black);
        window.draw(message);
        window.display();
    }

    return EXIT_SUCCESS;
}

Pages: [1]
anything