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

Pages: [1]
1
Graphics / getting color for rectangle shape
« on: April 26, 2018, 12:21:42 am »
I would like to access the color of a rectangle shape I'm drawing but I don't see anything in the api docs that does that, so how would I do this.

2
Window / Can't draw vector of pointers to window.
« on: April 10, 2018, 10:24:07 pm »
I've been trying to make a vector of objects, so that I can draw all of them to the screen, so I have been using pointers to do that, but when I try to de-pointer the  pointers, it just throws all kinds of errors. Here is my code

3
General / Object vector not re-adding items
« on: April 06, 2018, 02:31:57 am »
I am currently making a snake game in c++, but have encountered this problem. In order to make my snake, I have been using a vector full of objects of rectangles, and drawing them to where my snake goes. However, I have run into a problem. At first, my snake would be infinite as I would keep drawing more rectangles to put into the vector. However, when I try to clear the vector at the bottom of my program, none of the rectangles get redrawn, and I'm left with a tiny snake on the screen.

4
Graphics / movement not being drawn
« on: April 05, 2018, 01:24:47 am »
I am currently making a snake game in sfml, however, whenever i try to change the position of the snake, the snake just stops moving, what is a good fix to this problem?


main.cpp
#include <SFML/Graphics.hpp>
#include <iostream>
#include "write.h"
#include <SFML/Graphics/Font.hpp>
#include "menu.h"
#include "snake.h"
#include "wait.h"
int main()
{
sf::RenderWindow windows(sf::VideoMode::getDesktopMode(), "SFML works!",sf::Style::None);
sf::Font fonts;
sf::Clock gameClock;
windows.setKeyRepeatEnabled(false);
if(!fonts.loadFromFile("assets/bit.ttf"))
{

std::cerr << "Error loading font" << std::endl;
}

std::string gameScreen = "Main Menu";
 Snake snake1(10,100,500,0);
snake1.drawSnake(windows);
    while(windows.isOpen())
    {
        sf::Event event;
       
        while (windows.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                windows.close();
            }
            if(event.type == sf::Event::KeyPressed)
            {
            if(event.key.code == sf::Keyboard::Left)
            {
                if(gameScreen == "Play Game" )
                {

                    snake1.left();
                   
                }
            }
            }
            if(event.key.code == sf::Keyboard::Escape)
            {
             
                 windows.close();
            }
           
        }
     
       
       
       
      if(gameScreen == "Main Menu")
      {
        gameScreen =  buildMenu(fonts,windows);

      }
       
       if(gameScreen == "Play Game")
       {
       
         snake1.moveForward(windows);
         wait(10,gameClock);
       }
    }
     return 0;
   
    }

snake.cpp
#include <SFML/Graphics.hpp>
#include <iostream>
#include <SFML/Graphics/Font.hpp>
#include "snake.h"
#include "wait.h"
#include "math.h"

Snake::Snake( int s, int gx, int gy, int num)          //size,color,x,y)
{
   size = s;
   x = gx;
    y=gy;
  facing = num;
}


void Snake::drawSnake(sf::RenderWindow& window)
{
sf::RectangleShape rectangle2;
rectangle2.setSize(sf::Vector2f(size*10, 20));
rectangle2.setOutlineColor(sf::Color::Transparent);
rectangle2.setOutlineThickness(10);
rectangle2.setPosition(x,y);
rectangle2.setFillColor(sf::Color::White);
window.clear();
window.draw(rectangle2);
window.display();
}
void Snake::moveForward(sf::RenderWindow& window)
{
    Snake::drawSnake(window);
   

  if(cos(facing) == 1)
    {
     x=x+1;

    }
    else if(sin(facing) == 1){
   
    y=y+1;
    }
    else if(cos(facing) == -1)
    {
   
   x=x-1;
   std::cout << "hola";
    }
    else if(sin(facing) == -1)
    {

        y = y-1;
    }

   
}

void Snake::left()
{

    facing = facing + 90;
    std::cout<<facing;
}

void Snake::right()
{
facing = facing -90;
}
 
   

   

5
Window / Drawings not being updated to window
« on: April 04, 2018, 04:37:08 am »
I am trying to create a snake game in SFML, and I can't figure out what is happening because the window is not updating with the new position of the snake. Here is my code.

main.cpp
#include <SFML/Graphics.hpp>
#include <iostream>
#include "write.h"
#include <SFML/Graphics/Font.hpp>
#include "menu.h"
#include "snake.h"
int main()
{
sf::RenderWindow windows(sf::VideoMode::getDesktopMode(), "SFML works!",sf::Style::None);
sf::Font fonts;

if(!fonts.loadFromFile("assets/bit.ttf"))
{

std::cerr << "Error loading font" << std::endl;
}

std::string gameScreen = "Main Menu";
    while(windows.isOpen())
    {
        sf::Event event;
       
        while (windows.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                windows.close();
            }
            if(event.type == sf::Event::KeyPressed)
            {
            }
            if(event.key.code == sf::Keyboard::Escape)
            {
             
                 windows.close();
            }
           
            if(event.type == sf::Event::MouseMoved)
            {


            }
           
        }
     
       
       
       
      if(gameScreen == "Main Menu")
      {
        gameScreen =  buildMenu(fonts,windows);

      }
       
       if(gameScreen == "Play Game")
       {
         Snake snake1(10,100,500);
         snake1.drawSnake(windows);
         snake1.moveForward(windows);

       }
    }
     return 0;
   
    }

   

   Snake.cpp
#include <SFML/Graphics.hpp>
#include <iostream>
#include <SFML/Graphics/Font.hpp>
#include "snake.h"

Snake::Snake( int s, int gx, int gy)          //size,color,x,y)
{
   size = s;
   x = gx;
    y=gy;

}


void Snake::drawSnake(sf::RenderWindow& window)
{
sf::RectangleShape rectangle2;
rectangle2.setSize(sf::Vector2f(size*10, 20));
rectangle2.setOutlineColor(sf::Color::Transparent);
rectangle2.setOutlineThickness(10);
rectangle2.setPosition(x,y);
rectangle2.setFillColor(sf::Color::White);
window.clear();
window.draw(rectangle2);
window.display();
}
void Snake::moveForward(sf::RenderWindow& window)
{
    Snake::drawSnake(window);
    x = x+1;
}

 

snake.h
#ifndef SNAKE_H
#define SNAKE_H
class Snake
{
    int size;
   int x;
   int y;
public:
Snake(int s, int gx,int gy);
void moveForward(sf::RenderWindow& window);
//left();
//right();
void drawSnake(sf::RenderWindow& window);
};


#endif
 

6
System / How to run a function until an event happens
« on: April 03, 2018, 10:18:11 pm »
I'm creating a game, where I'm drawing a main menu, however, when I try and clear the screen in the function that draws the main menu, it just gets redrawn by the game loop. I also can't draw it only once, as my main menu has some gui animations that I want to keep. So how would i redraw the main menu over and over until the user clicks on one of the buttons.

Here is my code

main.cpp
#include <SFML/Graphics.hpp>
#include <iostream>
#include "write.h"
#include <SFML/Graphics/Font.hpp>
#include "menu.h"
int main()
{
sf::RenderWindow windows(sf::VideoMode::getDesktopMode(), "SFML works!",sf::Style::None);
sf::Font fonts;

if(!fonts.loadFromFile("assets/bit.ttf"))
{

std::cerr << "Error loading font" << std::endl;
}


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


            }
           
        }
     
       
       
       

         buildMenu(fonts,windows);

       
       
   

    }

    return 0;
}

menu.cpp
#include <SFML/Graphics.hpp>
#include <iostream>
#include "write.h"
#include <SFML/Graphics/Font.hpp>
#include "menu.h"
void buildMenu(sf::Font font, sf::RenderWindow& window)
{


sf::Color dgreen(0x54ff9fff);
sf::Color green(0x240fe2ff);
sf::Color bluegreen(0x00e64dff);
sf::Color red(0xff3436ff);
sf::Color cyan(0x1b990dff);
sf::RectangleShape rectangle2;
rectangle2.setSize(sf::Vector2f(850, 130));
rectangle2.setOutlineColor(dgreen);
rectangle2.setOutlineThickness(10);
rectangle2.setPosition(10, 740 );
rectangle2.setFillColor(sf::Color::Transparent);
sf::RectangleShape rectangle1;
rectangle1.setSize(sf::Vector2f(850, 130));
rectangle1.setOutlineColor(dgreen);
rectangle1.setOutlineThickness(10);
rectangle1.setPosition(10, 540 );
rectangle1.setFillColor(sf::Color::Transparent);
sf::RectangleShape rectangle;
rectangle.setSize(sf::Vector2f(850, 130));
rectangle.setOutlineColor(dgreen);
rectangle.setOutlineThickness(10);
rectangle.setPosition(10, 340 );
rectangle.setFillColor(sf::Color::Transparent);
sf::Vector2i mousepos = sf::Mouse::getPosition();
float x = mousepos.x;
float y = mousepos.y;

if(((850 > x) && (x > 10)) && ((470> y) && (y > 340)))
{

rectangle.setOutlineColor(red);

}

if(((850 > x) && (x > 10)) && ((670> y) && (y > 500)))
{

rectangle1.setOutlineColor(cyan);

}

if(((850 > x) && (x > 10)) && ((870> y) && (y > 700)))
{

rectangle2.setOutlineColor(green);

}



if((((850 > x) && (x > 10)) && ((870> y) && (y > 700)))&& (sf::Mouse::isButtonPressed(sf::Mouse::Left)))
{

window.clear();
window.display();
}
if((((850 > x) && (x > 10)) && ((670> y) && (y > 500)))&& (sf::Mouse::isButtonPressed(sf::Mouse::Left)))
{

window.clear();
window.display();
}
if((((850 > x) && (x > 10)) && ((470> y) && (y > 300)))&& (sf::Mouse::isButtonPressed(sf::Mouse::Left)))
{

window.clear();
window.display();
}


window.clear();
window.draw(print("Snake++", font,250,0,0, bluegreen));
window.draw(print("Play Game",font,150,25,300,bluegreen));
window.draw(print("Settings",font,150,25,500,bluegreen));
window.draw(print("Exit",font,150,25,700,bluegreen));
window.draw(rectangle);
window.draw(rectangle1);
window.draw(rectangle2);
window.display();

}

7
Graphics / Bundling multiple drawable objects into a returnable object
« on: March 31, 2018, 09:46:59 pm »
I am interested in creating a separate function to draw my menu screen in a game I'm making, how would I accomplish this in sfml and what would i need the function to return?

8
Graphics / Window opens then closes really fast
« on: March 23, 2018, 03:00:02 am »
I'm building a game in Sfml, and I want to print text to the display screen easier so I created a function that does it for me, but, whenever I run the function, the window opens then closes really fast, not allowing me to see the results. However, when I don't use the function to print text to the console, nothing happens, I was wondering what would help me resolve this problem

This really should be in the window section, but I can't move the question.

Here is my Code

Main.cpp
#include <SFML/Graphics.hpp>
#include <iostream>
#include "write.h"
#include <SFML/Graphics/Font.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode::getDesktopMode(), "SFML works!");
    sf::Font fonts;
if(!fonts.loadFromFile("bit.ttf"))
{

std::cerr << "Error loading font" << std::endl;
return EXIT_SUCCESS;
}
sf::Text word;
word.setFont(fonts);
word.setString("Hello World");
word.setFillColor(sf::Color::Red);
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();
        window.draw(print("aiden"));
        window.display();
    }

    return 0;
}
write.h
#ifndef WRITE_H
#define WRITE_H
#include <SFML/Graphics.hpp>
#include <SFML/Graphics/Font.hpp>

sf::Text print(std::string text);



#endif

write.cpp
#include "write.h"
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Graphics/Font.hpp>

sf::Text print(std::string text)       // std::string font)
{
sf::Font fonts;
if(!fonts.loadFromFile("bit.ttf"))
{

std::cerr << "Error loading font" << std::endl;
}
sf::Text word;
word.setFont(fonts);
word.setString(text);
word.setFillColor(sf::Color::White);
return word;
}
 

9
Graphics / Font library not being linked to mingw
« on: March 18, 2018, 01:55:27 am »
Setup
Mingw
Windows 10

Problem
Error undefined reference to `_imp___ZN2sf4Font12loadFromFileERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE'
Can't figure out the problem because the .ttf file is in the folder that the program i'm compiling is in.

Command I used
g++ main.cpp -IC:\Development\SFML-2.3.1\include -LC:\Development\SFML-2.3.1\lib -lsfml-graphics -lsfml-window -lsfml-system -o a.exe

Code
#include <SFML/Graphics.hpp>
#include <iostream>
//#include "intro.h"

int main()
{
   sf::RenderWindow Window(sf::VideoMode::getDesktopMode(),"Hello Window");
   
   sf::Font font;
   if(!font.loadFromFile("bit.ttf"))
   {
       std::cout << "No Work";
   }
   
   sf::Text text("hello", font);
   text.setCharacterSize(30);
   text.setStyle(sf::Text::Bold);
   text.setColor(sf::Color::Red);
   
    while(Window.isOpen())
    {
     
      sf::Event Event;
      while(Window.pollEvent(Event))
      {

          if(Event.type == sf::Event::Closed)
          {

              Window.close();
          }

      }

      // Declare and load a font


// Create a text

// Draw it
      Window.draw(text);
      Window.clear(sf::Color());
      Window.display();
    }
}
 

Pages: [1]