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

Author Topic: Issue with my rectangles not having their color filled.  (Read 1702 times)

0 Members and 1 Guest are viewing this topic.

CWS99

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Issue with my rectangles not having their color filled.
« on: July 27, 2017, 06:14:42 am »
Just as a project I created a color picker. The idea is that their are four buttons differing in color and when you click one, it will become brighter. My issue is that when I try to change the color using the fill with color function it doesn't change from how it was initialized. It compiles fine and recognizes which color i'm clicking as well. If anyone could lend an eye it would be much appreciated. Im sorry for the number of files.

Link: https://github.com/ViableEnd/SFMLColorPicker.git

main.cpp
#include "game.h"

int main()
{
  game game;
  return 0;
}
 

game.h
#ifndef GAME_H
#define GAME_H

#include "window.h"
#include "graphic.h"
#include "logic.h"

class game
{
public:
  game();
private:

};
#endif
 

game.cpp
#include "game.h"

game::game()
{
  window app(800, 800, "Color Picker");
  graphic display;
  logic check;

  display.initialSetup();
 

  while(app.isOpen())
  {
    while(app.anyEvent())
    {
      if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
      {
        check.buttonCheck(app.getMousePos());
      }

      if(app.eventType() == sf::Event::Closed)
        app.closeWin();
    }

    app.clearWin();

    app.drawWin(display.getShape(0));
    app.drawWin(display.getShape(1));
    app.drawWin(display.getShape(2));
    app.drawWin(display.getShape(3));
    app.drawWin(display.getShape(4));


    app.displayWin();
  }
}
 

window.h
#ifndef WINDOW_H
#define WINDOW_H

#include <string>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>

class window
{
public:
  window(int x, int y, std::string title);
  bool isOpen();
  bool anyEvent();

  sf::Event::EventType eventType();
  void closeWin();
  sf::Vector2i getMousePos();

  void clearWin();
  void drawWin(sf::RectangleShape entity);
  void displayWin();

private:
  sf::RenderWindow win;
  sf::Event event;
};

#endif
 

window.cpp
#include "window.h"

window::window(int x, int y, std::string title)
{
  win.create(sf::VideoMode(x, y), title);
}

bool window::isOpen()
{
  bool o = win.isOpen();
  return o;
}

bool window::anyEvent()
{
  bool e = win.pollEvent(event);
  return e;
}


sf::Event::EventType window::eventType()
{
  sf::Event::EventType eType = event.type;
  return eType;
}

void window::closeWin()
{
  win.close();
}

sf::Vector2i window::getMousePos()
{
  sf::Vector2i localPosition = sf::Mouse::getPosition(win);
  return localPosition;
}


void window::clearWin()
{
  win.clear(sf::Color::Black);
}

void window::drawWin(sf::RectangleShape entity)
{
  win.draw(entity);
}

void window::displayWin()
{
  win.display();
}
 

graphic.h
#ifndef GRAPHIC_H
#define GRAPHIC_H

#include <string>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>

class graphic
{
public:
    void initialSetup();
    sf::RectangleShape getShape(int id);
    void changeColor(int id);
private:
    sf::RectangleShape borderBox;                   //0
    sf::RectangleShape buttonRed;                   //1
    sf::RectangleShape buttonGreen;                 //2
    sf::RectangleShape buttonBlue;                  //3
    sf::RectangleShape buttonYellow;                //4s
};
#endif
 

graphic.cpp
#include "graphic.h"


void graphic::initialSetup()
{
    //Set up border box
    borderBox.setPosition(200, 200);
    borderBox.setSize(sf::Vector2f(400, 400));
    borderBox.setFillColor(sf::Color(54, 64, 76));

    //Set up red button
    buttonRed.setPosition(240, 240);
    buttonRed.setSize(sf::Vector2f(140, 140));
    buttonRed.setFillColor(sf::Color(153, 0, 0));

    //Set up green button
    buttonGreen.setPosition(420, 240);
    buttonGreen.setSize(sf::Vector2f(140, 140));
    buttonGreen.setFillColor(sf::Color(0, 153, 0));

    //Set up blue button
    buttonBlue.setPosition(240, 420);
    buttonBlue.setSize(sf::Vector2f(140, 140));
    buttonBlue.setFillColor(sf::Color(0, 0, 153));

    //Set up yellow button
    buttonYellow.setPosition(420, 420);
    buttonYellow.setSize(sf::Vector2f(140, 140));
    buttonYellow.setFillColor(sf::Color(153, 153, 0));
}

sf::RectangleShape graphic::getShape(int id)
{
    switch(id)
    {
        case 0 :
            return borderBox;
            break;
        case 1 :
            return buttonRed;
            break;
        case 2 :
            return buttonGreen;
            break;
        case 3 :
            return buttonBlue;
            break;
        case 4 :
            return buttonYellow;
            break;
    }
}

void graphic::changeColor(int id)
{
    switch(id)
    {
        case 0 :
            break;
        case 1 :
            buttonRed.setFillColor(sf::Color(255, 0, 0));
            break;
        case 2 :
            buttonGreen.setFillColor(sf::Color(0, 255, 0));
            break;
        case 3 :
            buttonBlue.setFillColor(sf::Color(0, 0, 255));
            break;
        case 4 :
            buttonYellow.setFillColor(sf::Color(255, 255, 0));
            break;
    }
   
}
 

logic.h
#ifndef LOGIC_H
#define LOGIC_H

#include <SFML/Window.hpp>

class logic
{
public:
    void buttonCheck(sf::Vector2i mousePos);
private:

};

#endif
 

logic.cpp
#include "logic.h"

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

/*Should not directly reference numbered positions
  but im not taking the time to change it*/

void logic::buttonCheck(sf::Vector2i mousePos)
{
    graphic color;
    if( ((mousePos.x >= 200) && (mousePos.x <= 340)) && ((mousePos.y >= 200) && (mousePos.y <= 340)) )
    {
        /* RED */
        color.changeColor(1);
        std::cout << "RED X" << mousePos.x << " Y" << mousePos.y << std::endl;
    }
    else if( ((mousePos.x >= 420) && (mousePos.x <= 560)) && ((mousePos.y >= 200) && (mousePos.y <= 340)) )
    {
        /* GREEN */
        color.changeColor(2);
        std::cout << "GREEN X" << mousePos.x << " Y" << mousePos.y << std::endl;
    }
    else if( ((mousePos.x >= 200) && (mousePos.x <= 340)) && ((mousePos.y >= 420) && (mousePos.y <= 560)) )
    {
        /* BLUE */
        color.changeColor(3);
        std::cout << "BLUE X" << mousePos.x << " Y" << mousePos.y << std::endl;
    }
    else if( ((mousePos.x >= 420) && (mousePos.x <= 560)) && ((mousePos.y >= 420) && (mousePos.y <= 560)) )
    {
        /* YELLOW */
        color.changeColor(4);
        std::cout << "YELLOW X" << mousePos.x << " Y" << mousePos.y << std::endl;
    }
}
 

AlejandroCoria

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
    • alejandrocoria.games
    • Email
Re: Issue with my rectangles not having their color filled.
« Reply #1 on: July 27, 2017, 07:40:02 am »
In the function logic::buttonCheck you are creating a local variable of type graphic instead of using the same one of game.

CWS99

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Issue with my rectangles not having their color filled.
« Reply #2 on: July 27, 2017, 07:56:24 am »
In the function logic::buttonCheck you are creating a local variable of type graphic instead of using the same one of game.

Thank you so much kind sir. Im sorry for wasting your time on a simple mistake. Have a good evening. ;D

 

anything