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 - Alidro

Pages: [1]
1
Graphics / creating a decent camera movement
« on: September 16, 2017, 05:31:04 pm »
Hello,
so i've been trying to create a smooth and nice camera with the sf::View in sfml. Currently, i have a camera which moves when the Player leaves a specific area (the Player can move inside a box but if he gets out of it the camera will follow him). It is working as i wished, but its not smooth. The code is:
#include "Camera.h"



Camera::Camera()
{
        view.setSize(384, 216);
        view.setCenter(0, 0);
}

void Camera::Render(RenderWindow &window)
{
        window.setView(view);
}

void Camera::Update(FloatRect object, Vector2f objectVelocity)
{

        if (object.left + object.width > view.getCenter().x + radius)
        {
                if (viewX < objectVelocity.x)
                        viewX = viewX + camSpeed;
        }
        else if (object.left < view.getCenter().x - radius)
        {
                if (viewX > objectVelocity.x)
                        viewX = viewX - camSpeed;
        }
        else
        {
                if (viewX < -camSpeed * 0.9)
                {
                        viewX = viewY + camSpeed * 0.9;
                }
                else if (viewX > camSpeed * 0.9)
                {
                        viewX = viewY - camSpeed * 0.9;
                }
                else
                        viewX = 0;
        }
       
        if (object.top + object.height > view.getCenter().y + radius / 1.5)
        {
                if (viewY < objectVelocity.y)
                        viewY = viewY + camSpeed;
        }
        else if (object.top < view.getCenter().y - radius / 1.5)
        {
                if (viewY > objectVelocity.y)
                        viewY = viewY - camSpeed;
        }
        else
        {
                if (viewY < -camSpeed * 0.9)
                {
                        viewY = viewY + camSpeed * 0.9;
                }
                else  if (viewY > camSpeed * 0.9)
                {
                        viewY = viewY - camSpeed * 0.9;
                }
                else
                        viewY = 0;
        }
        view.move(viewX, viewY);
       
}
in the cpp file and the h file:
#include <SFML\Graphics.hpp>
#include <iostream>

using namespace sf;

class Camera
{
public:
        Camera();
        ~Camera();
        void Render(RenderWindow &window);
        void Update(FloatRect object, Vector2f objectVelocity);
private:
        View view;
        int radius = 60;
        float viewY = 0, viewX = 0;
        float camSpeed = 2;

};

 

The Problem seems to be the camera moving a bit faster than the player, then camera passes the player and slows down, this happening inside a fraction of a second causes the camera to look stuttering. But I dont know how to solve this Problem.

2
Window / How do I use sf::Event in a class
« on: July 18, 2017, 07:55:32 pm »
Hello,
I am pretty sure there is a solution easy to find to this if I were better at the C++ in General but  I'm not.
I wanted to create a class, which contains text input. I wanted to do this with sf::Event::TextEntered but i dont know how to use it in a class since i am not able to use my sf::Event there because i declared it in main(). I dont have any code to post here because i have absolutely no idea.
I did not find anything useful for me in the Documentation.

Thank you for every good advice.

3
Network / UdpSocket::send seems to set the ip to 0.0.0.0
« on: May 25, 2017, 09:34:34 pm »
Hello there!
I just got into the Network module of sfml. i wanted to create a small game so i used UdpSocket.
Sadly i havent been able to send anything properly and after some Investigation i noticed my ip address is set to 0.0.0.0 after i use  .send(). In this case i use , just to test it, the same ip address for both Client and Server. Heres the Code:
#include <iostream>
#include <SFML\Graphics.hpp>
#include <SFML\Network.hpp>

using namespace sf;

int main()
{
        UdpSocket socket;
        IpAddress ip= IpAddress::getLocalAddress();
        Packet packet;
        unsigned short port = 1234;
        RectangleShape player1;
        RectangleShape player2;
        char sc;
        Vector2f player2pos;
        player1.setSize(Vector2f(20, 20));
        player2.setSize(Vector2f(20, 20));
        player1.setFillColor(Color::Blue);
        player2.setFillColor(Color::Red);
        std::cout << "[s] server, [c]client" << std::endl;
        std::cin >> sc;
        if (sc == 'c')
        {
                std::cout << "Server ip address" << std::endl;
                //std::cin >> ip;
                socket.bind(1235);

        }
        if (sc == 's')
        {
                socket.bind(1234);
        }

        bool update = false;
       
        socket.setBlocking(false);

        RenderWindow window(VideoMode(800, 600), "Window");
        while (window.isOpen())
        {
                if (sc == 'c')
                {
                        packet << player1.getPosition().x << player1.getPosition().y;

                        if (socket.send(packet, ip, 1234))
                                std::cout << "client sent data to " << ip.toString() << std::endl;
                        socket.receive(packet, ip, port);
                        packet >> player2pos.x >> player2pos.y;
                }
                if (sc == 's')
                {
                        if (socket.receive(packet, ip, port))
                                std::cout << "recieved data from " << ip.toString()  << std::endl;
                        packet >> player2pos.x >> player2pos.y;
                        packet << player1.getPosition().x << player1.getPosition().y;
                        socket.send(packet, ip, 1235);
                }
                Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == Event::Closed)
                        {
                                window.close();
                        }
                        else if (event.type == Event::GainedFocus)
                        {
                                update = true;
                        }
                        else if (event.type == Event::LostFocus)
                        {
                                update = false;
                        }
                }


                if (update)
                {
                        if (Keyboard::isKeyPressed(Keyboard::A))
                                player1.move(-1.f, 0.f);
                        else if (Keyboard::isKeyPressed(Keyboard::D))
                                player1.move(1.f, 0.f);
                        else if (Keyboard::isKeyPressed(Keyboard::S))
                                player1.move(0.f, 1.f);
                        else if (Keyboard::isKeyPressed(Keyboard::W))
                                player1.move(0.f, -1.f);
                }
                player2.setPosition(player2pos);

                window.clear();
                window.draw(player1);
                window.draw(player2);
                window.display();
        }
        return 0;
}

4
General / problem with bool, if(), maybe with rendering
« on: March 20, 2017, 04:49:28 pm »
Hello,
i wanted to create a startscreen for my game and i also wanted to include an options screen.
I dont know if this is a good attempt, but it worked until the point i wanted to add the options screen.
Heres the code:
//startscreen class:
#include "Startscreen.h"

Startscreen::Startscreen(RenderWindow &window)
{
        playButtonTexture.loadFromFile("Textures/Spiel1Button v.3.png");
        playButton.buttonSprite.setTexture(playButtonTexture, 1);
        playButton.buttonSprite.setOrigin(playButtonTexture.getSize().x / 2, playButtonTexture.getSize().y / 2);
        playButton.buttonSprite.setPosition(window.getSize().x / 2, window.getSize().y / 2.25);
        playButton.setText("Play");

        optionsButtonTexture.loadFromFile("Textures/Spiel1Button v.3.png");
        optionsButton.buttonSprite.setTexture(optionsButtonTexture, 1);
        optionsButton.buttonSprite.setOrigin(optionsButtonTexture.getSize().x / 2, optionsButtonTexture.getSize().y / 2);
        optionsButton.buttonSprite.setPosition(window.getSize().x / 2, playButton.buttonSprite.getPosition().y + 110);
        optionsButton.setText("Options");

        exitButtonTexture.loadFromFile("Textures/Spiel1Button v.3.png");
        exitButton.buttonSprite.setTexture(exitButtonTexture, 1);
        exitButton.buttonSprite.setOrigin(playButtonTexture.getSize().x / 2, playButtonTexture.getSize().y / 2);
        exitButton.buttonSprite.setPosition(window.getSize().x / 2, optionsButton.buttonSprite.getPosition().y + 110);
        exitButton.setText("Exit");

        resolutionButtonHigherTexture.loadFromFile("Textures/Spiel1Button v.4.png");
        resolutionButtonHigher.buttonSprite.setTexture(resolutionButtonHigherTexture, 1);
        resolutionButtonHigher.buttonSprite.setOrigin(resolutionButtonHigherTexture.getSize().x / 2 , playButtonTexture.getSize().y / 2);
        resolutionButtonHigher.buttonSprite.setPosition(window.getSize().x / 2 + 20, 300);
        resolutionButtonHigher.setText("");

       
        startBackgroundTexture.loadFromFile("Textures/Spiel1Hintergrund ohne Erde schlicht.png");
        startBackground.backgroundSprite.setTexture(startBackgroundTexture, 1);
        startBackground.backgroundSprite.setOrigin(startBackgroundTexture.getSize().x / 2, startBackgroundTexture.getSize().y / 2);
        startBackground.backgroundSprite.setPosition(window.getSize().x / 2, window.getSize().y / 2);


        headline1.setString("S");
        headline1.setOrigin(0, 150);
        headline1.setFont(font.Calibri);
        headline1.setCharacterSize(300);
        headline2.setString("pace");
        headline2.setOrigin(0, 150);
        headline2.setFont(font.Calibri);
        headline2.setCharacterSize(300);

        headline1.setPosition((window.getSize().x / 2) - ((headline1.getGlobalBounds().width + headline2.getGlobalBounds().width) / 1.9 ) , playButton.buttonSprite.getPosition().y - 250);
        headline2.setPosition(headline1.getPosition().x + headline1.getGlobalBounds().width * 1.2, headline1.getPosition().y);


        startActive = true;
        optionsActive = false;

}

void Startscreen::Render(RenderWindow &window)
{
        startBackground.render(window);
        if (startActive == true)
        {
        playButton.render(window);
        exitButton.render(window);
        optionsButton.render(window);
        window.draw(headline1);
        window.draw(headline2);
        }
       
        if (optionsActive)
        {
                resolutionButtonHigher.render(window);
        }
       
       
}

void Startscreen::Update(RenderWindow &window)
{
        if (optionsButton.isPressed(window) || playButton.isPressed(window))
        {
                startActive = false;
        }
        if (startActive)
        {
        playButton.update(window);
        exitButton.update(window);
        optionsButton.update(window);
        }
       
       


        if (exitButton.isPressed(window) == 1)
        {
                window.close();
        }




        if (optionsButton.isPressed(window))
        {
                optionsActive = true;
        }
       
        if (optionsActive = true)
        {
                resolutionButtonHigher.update(window);
        }
}

 
in the main class there are only Startscreen::update and Startscreen:render executed by now.
if i click options or play, as i wanted, the buttons and Text all dissapear, but they are still updated, so if i click for example "exit", the program still quits.
Also the "resolutionButtonHigher" is rendered from the start, and is updated from the start, although "optionsActive" and "startActive" are false by default.
the "button.isPressed" returns 1 if leftclicked, and 0 if not.
of course the "render" just draws the objects to the screen.

I think this has to be some careless mistake, so sorry if i were too idiotic.

5
General / detect a mouseclick on a Sprite
« on: March 14, 2017, 07:11:19 pm »
Hello,

although i think there are already posts for this topic, i just couldnt find exactly what i wanted.

So i want to detect if the mouse is colliding with a Sprite. This question may seem idiotic, but i just want an easy way to detect that. I tried to do it with xy positions or with globalbounds of the sprite but it all didnt worked as i wanted.

6
Graphics / Problem with .loadFromFile()
« on: March 08, 2017, 05:00:40 pm »
Hey there,

i am new to SFML and also to C++ kind of so dont expect me to know anything ;).
I did not quite understand how exactly the .loadFromFile() works with folders, I tried for example:
Font Arial;
Arial.loadFromFile("arial.ttf");
 
It worked as i had Arial.ttf in the same folder as the .exe and also in subfolders when i use Visual Studio to start the Program. But when I start the .exe manually, it could not load the files when in subfolders.

Sorry for any mistakes, I am German, so also feel free to write in german if wanted to and possible.




 

Pages: [1]