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

Pages: [1]
1
Graphics / emoji as output
« on: January 25, 2020, 02:15:46 am »
 ::)I was trying to check the sf::RectangleShape color using std::cout<<map.blocos.getFillColor().r<<std::endl; and the output is just "☺", can someone explain me how this happened and what i have to do to get the shape color?

Edit: i specifically want the color alpha, i was just trying new things and this happened. Also the RGB of the shape is (1, 0, 0)

2
General / Direction of collision
« on: January 17, 2020, 08:02:44 am »
So, in my project i used "Sprite.getGlobalBounds().intersects(sprite2.getGlobalBounds())" to see if the player was collinding with the floor, beeing able to jump again or not, but if it collides with the wall it will be able to jump again, so how can i make it to be able to identify the direction of the collisions?


Also i am not using perfect squares as walls or floors in this so i cant just subtract the position to know the direction...

3
General / Segmentation Fault
« on: January 10, 2020, 06:13:34 am »
So, i am making a 2d "game" on sfml and for the first time it gave me a error called  Segmentation Fault. When i run my code the window just opens and close quickly and the console say this error to me, here is my code:


main.cpp:

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

int main(){
        sf::RenderWindow janela(sf::VideoMode::getDesktopMode() , "titulo", sf::Style::Default);


        Player jogador;

        float tempo = 0.0f;
        sf::Clock clock;

        while(janela.isOpen()){
                tempo = clock.restart().asSeconds();

                sf::Event e;
                while(janela.pollEvent(e)){
                        if(e.type == sf::Event::Closed){
                                janela.close();}


                }


                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
                        jogador.corpo.move(0.1f,0);
                        jogador.walking = true;
                }

                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
                        jogador.corpo.move(-0.1f,0);
                        jogador.walking = true;
                }

                jogador.Anim(tempo);

                janela.clear(sf::Color(150, 150, 150));
                janela.draw(jogador.corpo);
                janela.display();

        }







        return 0;
}
 


Player.h:

#ifndef PLAYER_H
#define PLAYER_H
#include <SFML/Graphics.hpp>

class Player{
  public:
    Player();
    ~Player();
    sf::Sprite corpo;
    bool walking;
    std::vector<sf::Texture> texturas;


    void Anim(float tempo);


  private:
    int textura;
    float tempoTotal;
    int i;



};


#endif /* end of include guard:  */
 

Player.cpp

#include "Player.h"
#include <iostream>

Player::Player()
{
  textura = 0;
  corpo.setTexture(texturas[0]);
  corpo.setPosition(100.f, 100.f);
  corpo.setOrigin(250,250);
  corpo.setScale(0.2f,0.2f);

  tempoTotal = 0.0f;

  walking = false;
  texturas.reserve(10);

  for (i=0; i < texturas.size(); i++) {
    texturas[i].loadFromFile("Idle (" + std::to_string(i+1) + ").png");
    texturas[i].setSmooth(true);
  }

}

Player::~Player()
{

}

void Player::Anim(float tempo){
  tempoTotal += tempo;
  if (!walking){
    if(tempoTotal >= 0.1f){
      std::cout<<textura<<std::endl;
      tempoTotal = 0;
      textura ++;
      if(textura == texturas.size()){
        textura = 0; }

      corpo.setTexture(texturas[textura]);

    }
  }
  else{
    if(tempoTotal >= 0.3f){
      walking = false;
    }
  }
}
 

i will be really thankful if someone can help me, i am struggling with this error for a long time...



Sorry for my bad english btw :-[

4
Graphics / White rectangle issue
« on: December 27, 2019, 03:16:03 am »
Can someone explain me why is my code is outputitng an white rectangle instead of the properly texture?

Sorry for my bad english btw :-[

main.cpp:

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

int main(){
        sf::RenderWindow janela(sf::VideoMode(512, 512), "titulo", sf::Style::Default);
        sf::Texture texture, tex1, tex2, tex3, tex4;

        texture.loadFromFile("Idle (1).png");
        tex1.loadFromFile("Idle (2).png");
        tex2.loadFromFile("Idle (3).png");
        tex3.loadFromFile("Idle (4).png");
        tex4.loadFromFile("Idle (5).png");

        std::vector<sf::Texture> texturas;
        texturas.push_back(texture);texturas.push_back(tex1);texturas.push_back(tex2);texturas.push_back(tex3);texturas.push_back(tex4);




        Player jogador(texturas);
        //jogador.corpo.setTexture(texture);

        sf::Clock clock;
        sf::Time tempo;


        while(janela.isOpen()){
                sf::Event e;
                while(janela.pollEvent(e)){
                        if(e.type == sf::Event::Closed){
                                janela.close();}


                }


                tempo = clock.getElapsedTime();
                if(tempo.asSeconds() <=20){
                        std::cout<<tempo.asSeconds()<<std::endl;
                }

                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
                        jogador.corpo.move(0.1f,0);
                        std::cout<<jogador.corpo.getPosition().x;
                }
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
                        jogador.corpo.move(-0.1f,0);
                        std::cout<<jogador.corpo.getPosition().x;
                }
                if (sf::Mouse::isButtonPressed(sf::Mouse::Left)){
                        jogador.corpo.setPosition(sf::Mouse::getPosition(janela).x,sf::Mouse::getPosition(janela).y);
                }
                janela.clear(sf::Color(150, 150, 150));
                janela.draw(jogador.corpo);
                janela.display();

        }




        return 0;
}

Player.h

#ifndef PLAYER_H
#define PLAYER_H
#include <SFML/Graphics.hpp>

class Player{
  public:
    Player(std::vector<sf::Texture> texturas);
    ~Player();
    sf::Sprite corpo;


    void Anim();


  private:



};


#endif /* end of include guard:  */

Player.cpp:

#include "Player.h"

Player::Player(std::vector<sf::Texture> texturas)
{
  corpo.setTexture(texturas[0]);
  corpo.setPosition(20.f, 20.f);
  corpo.setOrigin(250,250);
  corpo.setScale(0.1f,0.1f);
}

Player::~Player()
{

}

void Player::Anim(){

}
 


Also i tried using pointers in the textures but i dont think i used it correctly  :-\



5
Graphics / Array of rectangles
« on: July 08, 2019, 03:02:54 am »
I am trying to create an array of sf::RectangleShape's. I create one but if i change something in the rec after throwing it inside my array(vector) the one inside the array wont change. Can someone help me to get the array to have on live objects

Here is my code(you can see that there is a rectangle that dont move and one that dont move, the one that does not move is the one in the array and the other one is the same Rect but not in the array)

#include <SFML/Graphics.hpp>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <vector>


int main(){
    srand(time(0));
    sf::Clock clock;

    char mov, ultmov;
    int spawnx, spawny, spawnx1, spawny1;
    float tamanho;

    sf::RenderWindow janela(sf::VideoMode(510, 510), "Snake Game", sf::Style::Close);
    sf::RectangleShape head(sf::Vector2f(10.0f, 10.0f));
    tamanho = head.getSize().x;

    while(spawnx%static_cast<int>(tamanho)!=0){
        spawnx = rand()%450;}
    while(spawny%static_cast<int>(tamanho)!=0){
        spawny = rand()%450;}

    head.setPosition(spawnx, spawny);

    sf::RectangleShape maca(sf::Vector2f(10.0f, 10.0f));

    loop:
    while(spawnx1%static_cast<int>(tamanho)!=0){
        spawnx1 = rand()%450;}
    while(spawny1%static_cast<int>(tamanho)!=0){
        spawny1 = rand()%450;}

    if (spawnx == spawnx1 && spawny ==spawny1){
        goto loop;}


    maca.setPosition(spawnx1, spawny1);
    maca.setFillColor(sf::Color::Red);

    std::vector<sf::RectangleShape> snake(200);
    snake.push_back(head);

    while(janela.isOpen()){
        sf::Event e;
        while(janela.pollEvent(e)){
            if(e.type == sf::Event::Closed){
                janela.close();

            }
        }

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W)&& ultmov!='s'){
            mov = 'w';}
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)&& ultmov!='d'){
            mov = 'a';}
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S)&& ultmov!='w'){
            mov = 's';}
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D) && ultmov!='a'){
            mov = 'd';}



        sf::Time elapsed1 = clock.getElapsedTime();
        if (elapsed1.asMilliseconds() >= 100){
            //std::cout<<"Funcao" <<std::endl;
            if(mov =='w'){
                head.move(0.0f, -10.0f);}
            if(mov =='a'){
                head.move(-10.0f, 0.0f);}
            if(mov =='s'){
                head.move(0.0f, 10.0f);}
            if(mov =='d'){
                head.move(10.0f, 0.0f);}
            ultmov = mov;
            clock.restart();}



        janela.clear(sf::Color(150, 150, 150));
        janela.draw(maca);
        for(int i=0;i<snake.size();i++){
            janela.draw(snake[i]);}
        janela.draw(head);
        janela.display();
    }






    return 0;
}

Sorry for my bad english btw

Pages: [1]