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

Pages: [1] 2
1
System / Not understanding threads in SFML?
« on: March 23, 2017, 06:07:06 am »
I was reading that in a Mac OS X, i'm only able to run window and handle events within the main thread. I'm not sure i understand that! According to the wiki, the main thread is main function. What classifies another form of thread? I havent touch that topic of threads in my C++ book, nor do i think they talk about it.

Here is my code and how i have it orientated.
//
//  Engine.hpp
//  RetroPongGame
//
//  Created by Jonathan Vazquez on 3/22/17.
//  Copyright © 2017 Jonathan Vazquez. All rights reserved.
//

#ifndef Engine_hpp
#define Engine_hpp

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

class Engine{

    public:
        Engine();
        ~Engine();
   
        void gameLoop();
        void eventHandler();
        void render();
       
   
    private:
        std::string winTitle;
        unsigned int winHeight, winWidth;
        sf::RenderWindow window;
   
};

#endif /* Engine_hpp */
 

//
//  Engine.cpp
//  RetroPongGame
//
//  Created by Jonathan Vazquez on 3/22/17.
//  Copyright © 2017 Jonathan Vazquez. All rights reserved.
//

#include "Engine.hpp"

Engine::Engine(): winTitle("Retro Pong Game v2.0"),winHeight(800),winWidth(1200){
    window.create(sf::VideoMode(winWidth, winHeight), winTitle, sf::Style::Titlebar | sf::Style::Close);
    window.setVerticalSyncEnabled(true);
}
Engine::~Engine(){
}

void Engine::gameLoop(){
    while(window.isOpen()){
        eventHandler();
    }
   
    // Clear screen
    window.clear();
   
    //rendering
    render();
   
    // Update the window
    window.display();
}

void Engine::eventHandler(){
    // Process events
    sf::Event event;
    while (window.pollEvent(event))
    {
        // Close window: exit
        if (event.type == sf::Event::Closed)
            window.close();
       
        // Escape pressed: exit
        if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
            window.close();
    }
}

void Engine::render(){
    // Draw the sprite
    //window.draw(sprite);
   
    // Draw the string
    // window.draw(text);
}

#include "Engine.hpp"

// Here is a small helper for you! Have a look.
#include "ResourcePath.hpp"

int main(int, char const**)
{
   
    Engine engine;
    engine.gameLoop();
   
    return EXIT_SUCCESS;
}

Which is the main thread and which isnt? Is the thread classified by a function or by extra files?

2
General discussions / How to use SFML without xcode on Mac?
« on: March 16, 2017, 08:00:52 am »
I'm used to using IDEs and other software related tools. However, due to my previous computer science classes, i been enjoying using a simple text editor and a compiler.

So that been said im still relatively new to using the terminal (Mac Sierra OS).
How can i compile and link sfml to my main.cpp file?
any tips would be appreciated thanks.

I can compile a file and execute single files
g++ main.cpp -o main
./main

However, i dont know how to do it while having external Library

3
General / Having a hard time drawing a private sf::RectangleShape?
« on: February 14, 2015, 11:21:13 pm »
I'm trying to recreate another pong game. Why you might ask, well because as i learn C++ i'm learning better design, etc. I don't practice C++ regularly but more like once every 2 weeks because of school and work! So i would like some help and perhaps no hard comments. I've managed to make it work before but can't seem to remember! So this is what i'm doing:

1) created a class named Entities that contains a constructor with specific parameters properties for sf::RectangleShape, sf::Circle, etc.
2) I then created 4 variables of data type sf::RectangleShape, sf::Circle, etc. as private member variables.
3) i created 4 getFunctions that will return the variables.

Entities.h
#include <SFML/Graphics.hpp>
#include "ResourcePath.hpp"

class Entities
{
public:
    sf::RectangleShape rect;
    sf::CircleShape circle;
    sf::Text text;
    sf::Font font;
   
public:
    Entities();
    Entities(sf::Vector2f size, sf::Vector2f position, sf::Color color);
    Entities(int radius, sf::Vector2f position, sf::Color color);
    Entities(std::string message, int charSize, sf::Vector2f position, sf::Font font, sf::Color color);
   
    sf::RectangleShape getRectangle();
    sf::CircleShape getCircle();
    sf::Text getText();
    sf::Font getFont();
   
   
};
#endif /* defined(__PongClone__entities__) */
 

Entities.cpp

#include "entities.h"

Entities::Entities()
{
    font.loadFromFile(resourcePath() + "");
}

Entities::Entities(sf::Vector2f size, sf::Vector2f position, sf::Color color)
{
    rect.setSize(size);
    rect.setPosition(position);
    rect.setFillColor(color);
}
Entities::Entities(int radius, sf::Vector2f position, sf::Color color)
{
    circle.setRadius(radius);
    circle.setPosition(position);
    circle.setFillColor(color);
}
Entities::Entities(std::string message, int charSize, sf::Vector2f position, sf::Font font, sf::Color color)
{
    text.setString(message);
    text.setCharacterSize(charSize);
    text.setPosition(position);
    text.setFont(font);
    text.setColor(color);
}

 sf::RectangleShape Entities::getRectangle()
{
    return rect;
}
sf::CircleShape Entities::getCircle()
{
    return circle;
}
sf::Text Entities::getText()
{
    return text;
}

sf::Font Entities::getFont()
{
    return font;
}
 

Now i have another class which will create specific objects. For example, i created a leftpaddle object like this:
Entities leftPaddle(Entities(sf::Vector2f(100,100), sf::Vector2f(200,200), sf::Color::White));

I've got all that working but when i try to draw the object i get and error like this:
"invalid use of non-static data member 'rect'"

ive tried
window->draw(getRectangle().leftPaddle);

but none of it works, also sorry for the wrong programming lingo!

Thanks for anyone willing to help and for your time spent!

4
Window / Where to place Window and Events using xCode?
« on: October 23, 2014, 09:27:26 pm »
were exactly do i place the events and window ? I'm using mac, xCode and was explicitly told to have both the event and the window together, but must they be together in the main.cpp function or can i place them in a class of its own in another .cpp file? Would that work, if i wanted to stay in 1 thread! !

5
General / Using Code::Block For Linux?
« on: July 14, 2014, 03:05:05 am »
So i finally started on my Introduction to Linux, for my College Class! We were demanded to use a Virtual Machine with Linux but me been the person that i am, decided to install a fresh version of Linux Mint on an Unused Hard drive for my laptop! Now the thing is, i'm not 100% familiar with Linux nor Code::Block! I'm learning by little the world of linux and so far i love it! Now my first time using C::B! I was wondering, how can i make the SFML project have the update of SFML 2.1 rather than 1.6 and 2.0! How can i update it? Most of those projects dont have any updated libraries! How can i update all those libraries to they're most recent version of Programming !

Please be gentle with me and information! Thank You !!!!

6
General / Not sure how to install SFML into Eclipse CDT - Mac OS X
« on: June 21, 2014, 03:46:49 am »
I've been programming using xCode 5 in my macbook pro laptop. However, i've been looking into using the Eclipse CDT IDE for no real reason but personal use. How can i install the SFML Framework using Eclipse CDT in my MacBook Pro? Should or is the same as any of the tutorials this site offers or is there a total different process? I'm very curious and would appreciate the help !
Thanks

7
SFML projects / Anyone want to build a 2D game with me?
« on: June 12, 2014, 07:23:44 pm »
Hello everyone, I was wondering if someone would like to work on a 2D Zelda like RPG game for fun and for learning purposes. Your level of C++ and SFML isn't necessary, as long as you know basics that's all that counts. We can both learn and build a 2D game!

As for the name of the game, I don't have one yet!
I would like a Zelda like game, but rather than playing with Zelda, you'll be prompted the option of choosing either Warrior, Archer, or Mage!

The game will be a bit long and tedious but like I said, it's just a fun project we can start and have it on github to work on! I'm looking for 2 other members ! Anyone interested be sure to add me on skype AndreeU17

Thanks guys for reading this and hopefully we'll see you guys soon

8
Graphics / Question about Images and Windows?
« on: June 10, 2014, 06:02:19 am »
So i was playing this game http://www.solarus-games.org/ Which is a zelda clone using C++! And i stumble across a very confusing perspective. A while back i was creating my own clone of pong, i wanted to implement a starter window that allow the user to toggle between either playing "1 player" or "2 player", however, because i code using xcode, window threading was a problem but while i played this game i realize that the window wasnt what was been switched (From start window to game window), it was the images. So in conclusion, is that how programmers get to have like a main title window were it says "New Game" etc. If so how can i simple draw the images, then remove the images from displaying?

9
Graphics / What is the best way to draw textures?
« on: June 10, 2014, 02:14:42 am »
I read the tutorial for sprite but i found a few things hard to understand. Before i go and reread it, i'd like to ask a few questions of the top of my head.

A texture is just a specific design, the entity is a specific shape, now the sprite is just a texture laid onto a shape.
Now i was wondering, would it be more convenient if instead of having numerous of textures in my resource folder, have one bit texture that contains multiple textures inside of it like a sprite sheet? Also lets say i did have a sprite sheet, how can i implicitly obtain a specific texture for a sprite? Would i need to use the sf::initRec (Not sure how it was spelled), then specify a value of were the texture is locate?

I'm just a bit confused as to how all this works, also what is the best way if i was trying to save idk graphics or save speed(images load faster)?

10
Graphics / Can sf::Font font be in a Constructor parameter?
« on: June 05, 2014, 09:25:53 am »
Not quite sure how this works, i only have such a huge knowledge in c++ (Just kidding, constantly studying).
But i created a class, withing the class i did this:
class Game
{
private:
        sf::RectangleShape rectangle;
        sf::CircleShape circle;
        sf::Text text;
public:
        Game(sf::Vector2f size, sf::Vector2f position, sf::Color color)
        {
                rectangle.setSize(size);
                rectangle.setPosition(position);
                rectangle.setFillColor(color);
        }
        Game(float radius, sf::Vector2f position, sf::Color color)
        {
                circle.setRadius(radius);
                circle.setPosition(position);
                circle.setFillColor(color);
        }
        Game(/*sf::Font loadFont ,*/std::string title, int characterSize, sf::Vector2f position, sf::Color color)
        {
                //text.setFont(loadFont);
                text.setString(title);
                text.setCharacterSize(characterSize);
                text.setPosition(position);
                text.setColor(color);
        }

        //function for converting string
        //function for updating ball
        //function for random number
        //function for Colission
        //function for computer AI
};
 

The thing is, i want to add sf::Font font as one of the parameters since it has a method called, setFont that would work perfectly with the text constructor. However, i'm not sure how to call it or set it up in the class. I first inserted, sf::Font font, as one of the parameters and called it using text.setFont(font); but once i was in the main function, i didnt know what to put in the parameter for font:
Game startTitle(Game(/*sf::Font loadFont ,*/"Start", 24, sf::Vector2f(0,0), sf::Color::White));
 
I though i could simply use, loadFromFile("file.png"); but that doesnt work, how can i work around this? Also i was wondering that with private variable members, the best way to call them into main would be to use getter/setter methods, correct?

11
General / Not sure what this error means?
« on: June 04, 2014, 08:25:48 am »
I keep getting everytime i run my new Window, problems in loading. I run it and when the new Window pops out, it stops responding and i get an error saying that the Thread 1 is: sf::Window::isOpen() const:
Which i my perspective means that the my second While(gameWindow.isOpen()){} is running constantly without stop even though i inserted an if statement to test whether the user pressed either escape or close to close the program! I was wondering do i need to seperate events?

I just want an explanation for that error thats all! Everything else i can figure out myself, just a small vivid detail about what that error means in english!

Thanks Also i use a MAC OS X SFML, xCode 5 in case your curious!

12
Window / How to make it press a button?
« on: June 04, 2014, 02:51:56 am »
I was wondering, if i could make a rectangle has a button, then have the user click on the specific button to start a game since SFML doesnt, include buttons (at least i think they dont)?

I was wondering that if the Event for MouseButtonPress will work but how do i check that the left mouse button click anywhere inside the rectangle?
I dont have any code to show since i dont know were to start ? ANy help would be appreciated!

13
SFML projects / Finished my First Pong Game using SFML (Mac OS X)!
« on: June 01, 2014, 07:57:32 pm »
So after a month of pure coding and asking everyone for basic questions, i manage to fully complete the game even though it still needs a ton more improvements which i will still keep implementing until i have a decent game with no bugs!

I'm a beginner in SFML and C++, if you find any other way i can improve my code, please let me know! Anything is worth it even bad comments, anything that can help me improve this!

Please offer any critics and improvements needed, Here is a few things i need to work on:
1) smooth movement for the paddles as well as the ball
2) create a splash screen
3) work on any other fixes to improve the coding
4) Implement a Computer AI
5) Implement Textures and Sprite for better look and feel
6)......Anything that you guys request!



Here is the source code:
/*
    Retro Clone Pong Game by Jonathan A Vazquez
    Version 2.0
 
 */


#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <ResourcePath.hpp> //Helper method resourcepath() for files (Images, Sounds, fonts)
#include <cmath>
#include <string>


//Window Settings
int windowWidth{600};
int windowHeight{400};
std::string windowTitle{"RetroPong"};

//Creating the Left and Right Paddle Position and Size
float leftPaddleSize_X{15};
float leftPaddleSize_Y{75};
float leftPaddlePos_X{5};
float leftPaddlePos_Y{(windowHeight / 2) - (leftPaddleSize_Y / 2)};
float rightPaddleSize_X{leftPaddleSize_X};
float rightPaddleSize_Y{leftPaddleSize_Y};
float rightPaddlePos_X{575};
float rightPaddlePos_Y{(windowHeight / 2) - (rightPaddleSize_Y / 2)};

//Creating the PongBall Properties
float ballPos_X{300};
float ballPos_Y{200};
float ballRadius{10};
float ballSpeed{0.1};
float ballDir_X{1.0};
float ballDir_Y{1.0};

//keeping the scoreboard
int playerScore{0};
int opponentScore{0};

int main()
{
    // Create the main window as well as all the necessary shapes and text
    sf::RenderWindow window(sf::VideoMode(windowWidth, windowHeight), windowTitle);
    sf::Font font;
    sf::Text playerTextScore, opponentTextScore;
    sf::RectangleShape bgLeftBox, bgRightBox, leftPaddle, rightPaddle;
    sf::CircleShape pongBall;
   
    //Setting the Right and Left Background Boxes
    bgLeftBox.setSize(sf::Vector2f(windowWidth/2, windowHeight));
    bgLeftBox.setPosition(sf::Vector2f(0, 0));
    bgLeftBox.setFillColor(sf::Color::White);
    bgRightBox.setSize(sf::Vector2f(windowWidth/2, windowHeight));
    bgRightBox.setPosition(sf::Vector2f(windowWidth / 2, 0));
    bgRightBox.setFillColor(sf::Color::Black);
   
    //Import the Font and create a Score Text for the user
    std::string resourcePath(void);
    font.loadFromFile(resourcePath() + "boxfont.ttf");
    playerTextScore.setFont(font);
    opponentTextScore.setFont(font);
    playerTextScore.setCharacterSize(72);
    opponentTextScore.setCharacterSize(72);
    playerTextScore.setColor(sf::Color::Black);
    opponentTextScore.setColor(sf::Color::White);
    opponentTextScore.setPosition(sf::Vector2f(450 ,0));
    playerTextScore.setPosition(sf::Vector2f(125, 0));
    playerTextScore.setString("0");
    opponentTextScore.setString("0");

    //Converting integer to strings
    //std::string s = to_string(playerScore);
   
    //Setting the Right and Left Paddle
    leftPaddle.setSize(sf::Vector2f(leftPaddleSize_X, leftPaddleSize_Y));
    leftPaddle.setPosition(sf::Vector2f(leftPaddlePos_X, leftPaddlePos_Y));
    leftPaddle.setFillColor(sf::Color::Black);
    rightPaddle.setSize(sf::Vector2f(rightPaddleSize_X, rightPaddleSize_Y));
    rightPaddle.setPosition(sf::Vector2f(rightPaddlePos_X, rightPaddlePos_Y)); //total window is 600, size 15, position is 5
    rightPaddle.setFillColor(sf::Color::White);
   
    //Creating the ball and its properties
    pongBall.setRadius(ballRadius);
    pongBall.setPosition(ballPos_X, ballPos_Y);
    pongBall.setFillColor(sf::Color::White);

                             
    // Start the game loop
    while (window.isOpen())
    {
        //Updating the movement of the ball and changing its color in the game loop
        //Moving Ball
        ballPos_X += ballSpeed * ballDir_X;
        ballPos_Y += ballSpeed * ballDir_Y;
        pongBall.setPosition(ballPos_X, ballPos_Y);
       
        ///Changing the Ball Color depending on location
        if(pongBall.getPosition().x < (windowWidth / 2))
        {
            pongBall.setFillColor(sf::Color::Black);
        }else
        {
            pongBall.setFillColor(sf::Color::White);
        }
       
        //Wall Rebounce Both Top and Bottom
        if(pongBall.getPosition().y > windowHeight)
        {
            ballDir_Y = -fabs(ballDir_Y);
        }else if(pongBall.getPosition().y < 0)
        {
            ballDir_Y = fabs(ballDir_Y);
        }
       
        //Collision Detection of the left and Right Paddle
        if(pongBall.getPosition().x < leftPaddle.getSize().x + leftPaddle.getPosition().x &&
           pongBall.getPosition().x > leftPaddle.getPosition().x &&
           pongBall.getPosition().y < leftPaddle.getPosition().y + leftPaddle.getSize().y &&
           pongBall.getPosition().y > leftPaddle.getPosition().y ) //Left Paddle
        {
            ballDir_X = fabs(ballDir_X);
           
        }else if(pongBall.getPosition().x < 0)
        {
            ballPos_X = windowWidth / 2;
            ballPos_Y = windowHeight / 2;
            pongBall.setPosition(ballPos_X, ballPos_Y);
            ballDir_X = fabs(ballDir_X);
            ++opponentScore;
            std::string opponentScoreText = std::to_string(opponentScore);
            opponentTextScore.setString(opponentScoreText);
            if(opponentScore == 7)
            {
                //Print out Opponent Win and stop the ball from moving
                opponentTextScore.setColor(sf::Color::Red);
                opponentTextScore.setCharacterSize(20);
                opponentTextScore.setPosition(sf::Vector2f(0, windowHeight / 2));
                opponentTextScore.setString("Opponent Won the Match, Thanks for playing");
                window.clear();
                window.close();
            }
        }
        if(pongBall.getPosition().x <  rightPaddle.getPosition().x + rightPaddle.getSize().x &&
           pongBall.getPosition().x > rightPaddle.getPosition().x &&
           pongBall.getPosition().y < rightPaddle.getSize().y + rightPaddle.getPosition().y &&
           pongBall.getPosition().y > rightPaddle.getPosition().y) //Right Paddle
        {
            ballDir_X = -fabs(ballDir_X);
        }else if(pongBall.getPosition().x > windowWidth)
        {
            ballPos_X  = windowWidth / 2;
            ballPos_Y = windowHeight / 2;
            pongBall.setPosition(ballPos_X, ballPos_Y);
            ballDir_X = -fabs(ballDir_X);
            ++playerScore;
            std::string playerScoreText = std::to_string(playerScore);
            playerTextScore.setString(playerScoreText);
            if(playerScore == 7)
            {
                //Print out Player Win and stop the ball from moving
                playerTextScore.setColor(sf::Color::Red);
                playerTextScore.setCharacterSize(20);
                playerTextScore.setPosition(sf::Vector2f(0, windowHeight / 2));
                playerTextScore.setString("Player Won the Match, Thanks for playing");
                window.clear();
                window.close();
            }
        }
       
       
        // Process events
        sf::Event event;
        while (window.pollEvent(event))
        {
            // Close window : exit
            if (event.type == sf::Event::Closed) {
                // Clear screen
                window.clear(sf::Color::White);
                window.close();
            }
            //To Move the Left Paddle Event
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
            {
                leftPaddle.move(sf::Vector2f(0, -12));
            }else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
            {
                leftPaddle.move(sf::Vector2f(0, 12));
            }
            //To move the right Paddle Event
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::I))
            {
                rightPaddle.move(sf::Vector2f(0, -12));
            }else if(sf::Keyboard::isKeyPressed(sf::Keyboard::K))
            {
                rightPaddle.move(sf::Vector2f(0, 12));
            }
        }

        // Draw the sprite
        window.draw(bgLeftBox);
        window.draw(bgRightBox);
        window.draw(playerTextScore);
        window.draw(opponentTextScore);
        window.draw(leftPaddle);
        window.draw(rightPaddle);
        window.draw(pongBall);

        // Update the window
        window.display();
    }
}
 

Here is a picture of what it looks like

14
Graphics / How about getting the Font in xCode 5?
« on: June 01, 2014, 05:22:27 am »
I'm not sure if i understand how to get a font from my folder using SFML in xCode 5 for Mac OS X.
I know that i must include
std::string resourcePath(void);
but how exactly must i get a font?
i use the font.loadFromFile(/sansation.tff);
but that wont work! How else can i get the font to load
    //Import the Font and create a Score Text for the user
    std::string resourcePath(void);
    font.loadFromFile("RetroPong/Resource/sansation.ttf");
    playerTextScore.setFont(font);
    opponentTextScore.setFont(font);
    playerTextScore.setCharacterSize(24);
    opponentTextScore.setCharacterSize(24);
    playerTextScore.setColor(sf::Color::Black);
    opponentTextScore.setColor(sf::Color::White);
    opponentTextScore.setPosition(sf::Vector2f(450 ,0));
    playerTextScore.setPosition(sf::Vector2f(150, 0));
    playerTextScore.setString("Player Score");
    opponentTextScore.setString("Opponent Score");
 

Also the font is placed here
RetroPong/BoxFont.tff (the only thing that is include here is RetroPong.xcodeproj and the RetroPong Folder)
and in RetroPong/RetroPong/BoxFont.tff (Main.cpp is also include in this directory, as well as everything else)

15
General / Installing SFML 2.1 onto xCode 5 (Mac)?
« on: May 27, 2014, 03:37:19 am »
So i managed to do everything as normal, according to the tutorial for installing SFML 2.1 onto xCode 5

The only part i don't understand is this:

"SFML is shipped with an install.sh script that will install everything for you. It will also make sure that previous versions of SFML, especially frameworks, are not removed from your system so that your applications can still be executed without modifications. You just need to open a terminal in the directory of the downloaded SDK and run ./install.sh. The script will then prompt for your password and install everything in the right place."

How do i install the necessary stuff needed when using the install script and terminal?
I understand programming but i have no idea about using the Terminal preferably the mac terminal.
The sdk is in this directory:
/Users/jonathanvazquez/Documents/Programming Development/C++ Development/SFML2.1Clang
and the install script is in:
/Users/jonathanvazquez/Documents/Programming Development/C++ Development/SFML2.1Clang/install.sh

But when i insert that into terminal, it says, No such directory, etc.

How can i install it?

Pages: [1] 2
anything