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.


Messages - Ettur

Pages: [1]
1
Graphics / Re: Understanding sf::Color
« on: April 27, 2020, 05:27:57 pm »
Yes, sf::UInt8 is unsigned char, so it is printed as characters, and you have to cast to integer to get proper numbers.

Color::toInteger() returns the components packed together in a 32-bits integer. You'd have to print as hexadecimal to see the groups of bytes clearly.

To get separate RGB values, stick to the first solution, don't use toInteger().

Thank you!!!

2
Graphics / Understanding sf::Color
« on: April 27, 2020, 02:16:54 pm »
Need a bit clarification on sf::Color,

I used getPixel from image to save pixel color:

sf::Color color = image.getPixel(280, 275);

now as I understand Color has properties r g b a to get Uint8 as corresponding values..
Then, when i wanted to see what those numbers area i tried cout:

cout << color.r << endl;
cout << color.g << endl;
cout << color.b << endl;

At this point im expecting to see numbers that correspond to R,G,B,A values of that color, however console window shows some weird symbols instead of numbers ???

then I tried to cast them as ints

cout << (int)color.r << endl;
cout << (int)color.g << endl;
cout << (int)color.b << endl;

now I saw numbers, are those numbers now actual rgb values of given pixel?

Then I also noticed sf::Color has toInteger() method.

So I tried:

cout << color.toInteger() << endl;

which does give me a sequence of numbers, HOWEVER, this sequence is vastly different from from individual values I see from (int) cast of r g b values.

pardon if it's something really simple & obvious but help me understand how can I get r g b a numeric values from sf::Color variable.

3
Graphics / Line of sight / Fog drawing with SFML
« on: March 21, 2020, 06:18:36 pm »
Hi,

I would like to implement a feature so that player would have a certain line of sight visible in front of him. For example a triangular area where one vertex is at player and other two would go to distance of screen length at an angle between them.

As an idea, I thought to make a rectangle with alpha 0.5 to cover entire screen and then a triangle shaped area that would show through the rectangle but I cant think of how would I make it or is that idea even possible?

I guess one option would be to drawn 0.5 alpha area pixel by pixel and I could calculate the area of that visible triangle shape and make those pixels transparent? That means drawing every pixel on screen for every frame, sounds a bit too much hardware usage ?

Or is there some way I am not thinking it would be possible to do?

If you have played DOTA2 , thats the sort of mechanism Im aiming for. Later on I would like to implement structures also that player cant see through but for starters id be happy to understand simple fog reveal.

I tryed to google line of sight and fog of war on google but I didnt find quite the thing I was aiming for. If anyone has experience with this could you share ideas? Or if you know resource / tutorials where I could get help / hints id appreciate it very much :)

4
Graphics / Re: Problem with drawing shapes
« on: March 21, 2020, 12:09:08 pm »
Thanks again!

Problem was so obvious its ridiculous :D I got a couple different projects open with similar code , I kept working on one while trying to figure out these issues.
I pasted here correctly, but my other window where I was working these issues out, I had mistakenly declared Player player; inside main loop !!!!
If I looked close enough I could see player move 0.1f ... but then in the following loop Player would be made a new in original position.

Again in the code above Player player; is made an instance of before game loop, but in my other file it was not.

Thank you! I continue working on my project :)

5
Graphics / Re: Problem with drawing shapes
« on: March 20, 2020, 12:39:57 pm »
Thank you!
It's my C++ understanding, very low.
I managed to get them drawn now! It was in the constructor to just define them correctly.

Could you also help me understand how to make methods for keyboard input and sight rotation to work in main method?

As of now  they are not reacting to input when I call them as class methods. I would think the event scope is different now, might have something to do with it.?

6
Graphics / Problem with drawing shapes
« on: March 20, 2020, 03:44:21 am »
Hi,

Im quite new to C++ and SFML. I was following some tutorials to draw shapes and get keyboard input to make those shapes move. I got everything working nice, at that point I had everything declared and executed in main() method.

Then I thought to make my code more modular and OOP, as soon as I did that -> placed shapes in Player class and keyboard input as a method in Player class and then called those methods exactly where that code was previously in main() method, shapes wont draw.

It must be something simple, there is no errors, but I cant figure it out. Am hoping someone could point out what is wrong. Drawing shapes in main still works as I tried to draw circle and its fine, just drawing from my Player class seems faulty.

Heres my code:

main() method

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

using namespace std;


int main() {

        sf::RenderWindow window1(sf::VideoMode(1024, 768), "Tutorial", sf::Style::Close | sf::Style::Resize);
        Player player;

        while (window1.isOpen())
        {
                sf::Event evnt1;

                while (window1.pollEvent(evnt1))
                {
                        switch (evnt1.type) {
                        case sf::Event::Closed:
                                window1.close();
                                break;
                        }
                }

                window1.clear(sf::Color(0, 150, 150));
                window1.draw(player.player);
                window1.draw(player.sigth);
                player.movePlayer();
                sf::Vector2i mousePos = sf::Mouse::getPosition(window1);
                player.sigthRotation(mousePos);
                window1.display();

        }

        return 0;
}
 

And Player class
Player.h

#pragma once
#include <SFML/Graphics.hpp>
#include <math.h>
#define PI 3.14159265

class Player
{
public:
        sf::CircleShape player;
        sf::RectangleShape sigth;
       
        Player();
        void movePlayer();
        void sigthRotation(sf::Vector2i mousePos);
};
 


and Player.cpp

#include "Player.h"

Player::Player() {
        sf::CircleShape player (25);
        sf::RectangleShape sigth (sf::Vector2f(150.0f, 2.0f));

        player.setPosition(206.0f, 206.0f);
        sigth.setPosition(231.0f, 231.0f);
        player.setFillColor(sf::Color::Blue);
       
}

void Player::movePlayer() {

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
        {
                player.move(-0.1f, 0.0f);
                sigth.move(-0.1f, 0.0f);

        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
        {
                player.move(0.1f, 0.0f);
                sigth.move(0.1f, 0.0f);
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
        {
                player.move(0.0f, -0.1f);
                sigth.move(0.0f, -0.1f);
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
        {
                player.move(0.0f, 0.1f);
                sigth.move(0.0f, 0.1f);
        }
}

void Player::sigthRotation(sf::Vector2i mousePos) {
        sf::Vector2f linePos = sigth.getPosition();


        float dx = linePos.x - mousePos.x;
        float dy = linePos.y - mousePos.y;
        double rotation = (atan2(dy, dx)) * 180 / PI;

        sigth.setRotation((float)rotation + 180);
}

 

Pages: [1]
anything