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

Pages: [1] 2
1
General / Re: Pong.exe keeps crashing, C::B issue or C++ issue?
« on: May 17, 2012, 05:49:16 pm »
Yeah, the debugger fixed it. Didn't even think of that... Oops.

2
General / Pong.exe keeps crashing, C::B issue or C++ issue?
« on: May 17, 2012, 04:52:22 pm »
So whenever I run the following code, get a message from windows saying that pong.exe has stopped working. Is this because of bad coding, or is this because of Code::Blocks?

// Program: Pong
// Created By: CW Stoneman
// Studio: Jumping Lemmings Studios
// Date Created: 2-28-12
// Version: 0.0
// Last Updated: 4-25-12
// Version Notes:

// Description: A pong clone to figure out how to use SFML.

// To do list:
// Figure out why it won't display anything

// Headers to include
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <cmath>

using namespace std;

// Global constants
const float PI = 3.14159f;                           // Pi
const int RESETSCORE = 10;               // The score at which the game is over
const int STARTINGX1 = 40;                  // The starting x position for
                                                                            // paddle 1
const int TEXTY1 = 20;                              // The y coordinate for the
                                                                            // player names
const int TEXTY2 = 50;                              // The y coordinate for the
                                                                            // scores
const int TEXTX1 = 100;                            // The x coordinate for the
                                                                            // First score
const int TEXTX2 = 540;                            // The x coordinate for the
                                                                            // second score
const double PADDLESPEED = 2.5; // The speed at which the paddles move
const double AICHANGE = 0.75;         // The adjustment for the AI. AI's speed =
                                                                            // (other player's score/2)* AICHANGE
const int PADDLEHEIGHT = 75;           // Height of the paddles
const int PADDLEWIDTH = 20;             // Width of the paddles
const int BALLSIZE = 25;                         // Size of the ball

// Function Prototypes
int main();

// Class Prototypes
class Ball;
class Paddle;
class Game;

class Ball
// The class for the ball
{
    // Declare the variables
    public:
        sf::Shape circle;          // The paddle's sprite
        double speed;          // The speed of the ball
        double ballAngle;    // Points one of four directions for the
                                                 // ball
        bool colliding;            // If the ball is colliding or not

    // Function prototypes
    public:
        void loadResources(sf::RenderWindow&);
        void changePosition(sf::RenderWindow&);
        void reset();
        void update();
        int checkCollision(double, double, double, double, double, double,
                           sf::RenderWindow&);
        void bounce(int,double,double,bool);
};

void Ball::loadResources(sf::RenderWindow& App)
// Loads the ball's resources
{
    circle = sf::Shape::Circle(App.GetView().GetRect().GetWidth() /2,
                               App.GetView().GetRect().GetHeight() /2, 12.5,
                               sf::Color(125,234,19));
}

void Ball::changePosition(sf::RenderWindow& App)
// Moves the ball
{
    float Factor = speed * App.GetFrameTime();
    circle.Move(cos(ballAngle) * Factor, sin(ballAngle) * Factor);
}

void Ball::reset()
// Resets the ball
{
    do
    {
       // Make sure the ball initial angle is not too much vertical
        ballAngle = sf::Randomizer::Random(0.f, 2 * PI);
    } while (abs(cos(ballAngle)) < 0.7f);
    speed = 400.f;
}

int Ball::checkCollision(double xPosition1, double yMin1, double yMax1,
                           double xPosition2, double yMin2,double yMax2,
                           sf::RenderWindow& App)
// Checks if the ball needs to bounce, and returns true if the player has \
// scored.
{
    int scored;
    if (circle.GetPosition().x < 0)

    {
        scored = 1;
    }
    else if (circle.GetPosition().x > App.GetView().GetRect().GetWidth() )
    {
        scored = 2;
    }
    if (circle.GetPosition().y < 0.f)
         {
            ballAngle = -ballAngle;
            circle.SetY(0.1f);
         }
    else if ((circle.GetPosition().x == xPosition1) &&
             (circle.GetPosition().y > yMin1) &&
              (circle.GetPosition().y < yMax1))
    {
        bounce(90,xPosition1,PADDLEWIDTH, TRUE);
    }
    else if ((circle.GetPosition().x == xPosition2) &&
             (circle.GetPosition().y > yMin2) &&
              (circle.GetPosition().y < yMax2))
    {
        bounce(-90,xPosition2,PADDLEWIDTH, TRUE);
    }
    return scored;
}

void Ball::bounce(int angle, double paddlePositionx, double paddleSize,
                  bool paddle)
// Check the collisions between the ball and the paddles
{
    if (paddle)
        {
            angle = PI - angle;
            circle.SetX(paddlePositionx + paddleSize + 0.1f);
        }
        else
        {
            angle = PI - angle;
            circle.SetX(paddlePositionx - paddleSize - 0.1f);
        }
    }

class Paddle
// Class for the paddles
{
    //. Declare the variables
    public:
        sf::Shape rectangle; // The sprite for the paddle
        float paddleSpeed; // The paddle's speed

    // Function Prototypes
    public:
        void loadResources(float,sf::RenderWindow&);
        void changePosition(sf::RenderWindow&, sf::Clock&);
        void reset(float,sf::RenderWindow&);
        void update();
        void move(Ball, int, sf::RenderWindow&);
        void move(sf::RenderWindow&, bool);
};

void Paddle::move(Ball theBall, int otherScore, sf::RenderWindow& App)
// Moves the AI Paddle
{
    paddleSpeed = ((otherScore/2)/AICHANGE) + 1;
    if (theBall.circle.GetPosition().x > 0)
    {
        rectangle.Move(0,paddleSpeed*App.GetFrameTime());
    }
    else
    {
        rectangle.Move(0,-paddleSpeed*App.GetFrameTime());
    }
}

void Paddle::loadResources(float xcoordinate, sf::RenderWindow& App)
// Loads the resources for the paddle
{
     rectangle = sf::Shape::Rectangle((xcoordinate)-12.5, (App.GetView().GetRect().GetHeight() /2)-37.5,
                                      (xcoordinate)+12.5,(App.GetView().GetRect().GetHeight() /2)+37.5,
                                      sf::Color(255, 21, 13));
}

void Paddle::move(sf::RenderWindow& App, bool direction)
// Moves the player's paddle
{
    if (direction == TRUE)
    // Move it up
    {
        rectangle.Move(0.f, paddleSpeed * App.GetFrameTime());
    }
    else if (direction == FALSE)
    // Move down
    {
        rectangle.Move(0.f, -paddleSpeed * App.GetFrameTime());
    }
}

void Paddle::reset(float startingx,sf::RenderWindow& App)
// Resets the paddle
{
    rectangle.SetPosition(startingx, App.GetView().GetRect().GetHeight() /2);
    paddleSpeed = 350;
}

class Game
// The main class with the game and all its variables
{
    // Declare the variables
    public:
        Ball theBall;                      // The ball
        Paddle paddle1;            // The first paddle
        Paddle paddle2;            // The second paddle
        int score1;                        // The first score
        int score2;                        // The second score
        sf::Font mechaBold;      // The font used for displaying the scores
        sf::String score1Text;    // The text for the first score
        sf::String score2Text;    // The text for the second score
        sf::String player1Text;   // The text for the first player
        sf::String player2Text;   // The text for the second player
        sf::String status;               // The game's status

    // Function Prototypes
    public:
        void loadResources(sf::RenderWindow&);
        void reset(sf::RenderWindow&);
        void update(sf::RenderWindow&, bool);
        void score(int, sf::RenderWindow&);
        void newRound(sf::RenderWindow&);
};

void Game::update(sf::RenderWindow& App, bool direction)
{
    int scored;
    double Factor = theBall.speed * App.GetFrameTime();
    theBall.circle.Move(cos(theBall.ballAngle)* Factor,
                        sin(theBall.ballAngle)*Factor);

    // Convert the scores into strings.
    // This is a huge pain in the butt.
    string Result;
    ostringstream convert;
    convert << score1;
    Result = convert.str();
    score1Text.SetText(Result);
    convert << score2;
    Result = convert.str();
    score2Text.SetText(Result);

    paddle2.move(theBall,score1,App);

    //  Check the ball for collisions
    scored = theBall.checkCollision(
                           paddle1.rectangle.GetPosition().x+12.5,
                           paddle1.rectangle.GetPosition().y-37.5,
                           paddle1.rectangle.GetPosition().y+37.5,
                           paddle2.rectangle.GetPosition().x-12.5,
                           paddle2.rectangle.GetPosition().y-37.5,
                           paddle2.rectangle.GetPosition().y+37.5, App);
    if (scored > 0)
    {
        score(scored, App);
    }
}

void Game::score(int scored,sf::RenderWindow& App)
// Scores for the players
{
    if (scored == 1)
    {
        score1++;
        newRound(App);
        status.SetText("Player 1 just scored!");
    }
    else if (scored == 2)
    {
        score2++;
        newRound(App);
        status.SetText("Player 2 just scored!");
    }
}

void Game::newRound(sf::RenderWindow& App)
// Starts another round
{
    theBall.reset();
    paddle1.reset(STARTINGX1,App);
    paddle2.reset(App.GetView().GetRect().GetWidth()-40, App);
}

void Game::loadResources(sf::RenderWindow& App)
// Loads the game's resources, and  all the subclassse's resources
{
    // Load from a font file on disk
    if (!mechaBold.LoadFromFile("Mecha_Bold.ttf"))
    {
        App.Close();
        cout << "Font didn't load";
    }
    paddle1.loadResources(STARTINGX1, App);
    paddle2.loadResources(App.GetView().GetRect().GetWidth()-40, App);
    theBall.loadResources(App);
}

void Game::reset(sf::RenderWindow& App)
// Starts a new game
{
    score1 = 0;
    score2 = 0;

    player1Text.SetText("Player 1");
    player1Text.SetFont(mechaBold);
    player1Text.SetSize(18);
    player1Text.SetPosition(TEXTX1,TEXTY1);
    player2Text.SetText("Player 2");
    player2Text.SetFont(mechaBold);
    player2Text.SetSize(18);
    player2Text.SetPosition(TEXTX2,TEXTY1);
    score1Text.SetFont(mechaBold);
    score1Text.SetSize(50);
    score1Text.SetPosition(TEXTX1,TEXTY2);
    score2Text.SetFont(mechaBold);
    score2Text.SetSize(50);
    score2Text.SetPosition(TEXTX2,TEXTY2);

    status.SetPosition(App.GetView().GetRect().GetHeight()/2,App.GetView().GetRect().GetHeight() -100);
    status.SetSize(18);
    status.SetFont(mechaBold);
    status.SetText("Game reset.");

    // Convert the scores into strings.
    // This is a huge pain in the butt.
    string Result;
    ostringstream convert;
    convert << score1;
    Result = convert.str();
    score1Text.SetText(Result);
    convert << score2;
    Result = convert.str();
    score2Text.SetText(Result);

    theBall.reset();
    paddle1.reset(STARTINGX1, App);
    paddle2.reset(App.GetView().GetRect().GetWidth()-40, App);
    loadResources(App);

}

int main()
{
    // Declare the variables
    sf::Clock Clock;       // The clock
    Game theGame;    // The game with everything in it
    sf::Event Event;       // The events for the game to get
    bool direction;        // The direction of the player's paddle. True for up,
                                          // false for down.

    // Create the main rendering window
    sf::RenderWindow App(sf::VideoMode(App.GetView().GetRect().GetWidth()  , App.GetView().GetRect().GetHeight() , 32),
                         "Pong by CW");

    // Function prototypes
    void draw(sf::RenderWindow&, Game);

    theGame.reset(App);

    // Set the framerate so that it doesn't go too fast
    App.SetFramerateLimit(40);

    // Start game loop
    while (App.IsOpened())
    {
        // Process events
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
            {
                App.Close();
            }
            else if ((Event.Key.Code == sf::Key::Up) &&
            (theGame.paddle1.rectangle.GetPosition().y > -App.GetView().GetRect().GetHeight() /2))
            {
                theGame.paddle1.rectangle.Move(0.f,
                                               -theGame.paddle1.paddleSpeed *
                                               App.GetFrameTime());
                                               cout << theGame.paddle1.rectangle.GetPosition().y << endl;
            }
            else if ((Event.Key.Code == sf::Key::Down) &&
            (theGame.paddle1.rectangle.GetPosition().y < App.GetView().GetRect().GetHeight() /2))
            {
                theGame.paddle1.rectangle.Move(0.f, theGame.paddle1.paddleSpeed*
                                               App.GetFrameTime());
            }
            else if (Event.Key.Code == sf::Key::R)
            {
                theGame.reset(App);
            }
            else if (Event.Key.Code == sf::Key::F1)
            {
                sf::Image Screen = App.Capture();
                Screen.SaveToFile("screenshot.jpg");
                theGame.status.SetText("Screenshot captured.");
            }
        }
        // Update the game
        theGame.update(App, direction);
        draw(App, theGame);
    }
    return EXIT_SUCCESS;
}

void draw(sf::RenderWindow& App, Game theGame)
// Draws all the stuff
{
    // Draw everything
    App.Clear(sf::Color(0,0,35));
    App.Draw(theGame.theBall.circle);
    App.Draw(theGame.paddle1.rectangle);
    App.Draw(theGame.paddle2.rectangle);
    App.Draw(theGame.player1Text);
    App.Draw(theGame.player2Text);
    App.Draw(theGame.score1Text);
    App.Draw(theGame.score2Text);
    App.Draw(theGame.status);
    App.Display();
}


As always, any help is greatly appreciated.

3
General / Re: Compiling Issues, Not Sure What to Do
« on: May 03, 2012, 04:11:50 pm »
I'm not sure how you've set up your IDE and it also seems to work but you normaly use #include <SFML/foo.hpp> and not #include "SFML/foo.hpp".
Also don't use the backslash, since it's Windows specific.

Okay, changing that.

For line 90 and 123, you need to define the interface with references. You don't want (and can't) to copy the window, you just want to use the existing window.
void changePosition(sf::RenderWindow&, sf::Clock&);

Editing that, too.

And for line 217 and 221: sf::Key::foo isn't an event type. The events are stored in sf::Event::.

Why do you throw exceptions when you don't catch them? (line 67, 119, 172, 176)

Have you programed in other languages before?
1. I was trying to have an error thing. I don't know how to do that in c++.
2. I have, but this is my first time doing anything outside of the shell/console.

I hope you don't expect something to be drawn, since you don't draw anything. ;)
... Oops  :-[

I'll post the new code later, sfml uploads seems to be down.

4
General / Re: Compiling Issues, Not Sure What to Do
« on: May 02, 2012, 05:39:30 pm »
https://legacy.sfmluploads.org/file/133

Here's the source. And as for SFML 2, I'm currently finishing up an independent programming class and don't have enough time to update. I probably will once I'm done with this class.

5
General / Compiling Issues, Not Sure What to Do
« on: May 01, 2012, 04:14:18 pm »
Compiling: main.cpp
C:\Users\Owner\Documents\Programming\Pong\main.cpp: In function 'int main()':
C:\Users\Owner\Documents\Programming\Pong\main.cpp:217:45: warning: comparison between 'enum sf::Event::EventType' and 'enum sf::Key::Code' [-Wenum-compare]
C:\Users\Owner\Documents\Programming\Pong\main.cpp:217:74: warning: comparison between 'enum sf::Event::EventType' and 'enum sf::Key::Code' [-Wenum-compare]
In file included from C:\Users\Owner\Documents\Programming\SFML-1.6\include/SFML\Window.hpp:37:0,
                 from C:\Users\Owner\Documents\Programming\Pong\main.cpp:15:
C:\Users\Owner\Documents\Programming\SFML-1.6\include/SFML/System/NonCopyable.hpp: In copy constructor 'sf::Window::Window(const sf::Window&)':
C:\Users\Owner\Documents\Programming\SFML-1.6\include/SFML/System/NonCopyable.hpp:57:5: error: 'sf::NonCopyable::NonCopyable(const sf::NonCopyable&)' is private
C:\Users\Owner\Documents\Programming\SFML-1.6\include/SFML/Window/Window.hpp:55:16: error: within this context
In file included from C:\Users\Owner\Documents\Programming\SFML-1.6\include/SFML\Window.hpp:35:0,
                 from C:\Users\Owner\Documents\Programming\Pong\main.cpp:15:
C:\Users\Owner\Documents\Programming\SFML-1.6\include/SFML/System/NonCopyable.hpp: In copy constructor 'sf::Input::Input(const sf::Input&)':
C:\Users\Owner\Documents\Programming\SFML-1.6\include/SFML/System/NonCopyable.hpp:57:5: error: 'sf::NonCopyable::NonCopyable(const sf::NonCopyable&)' is private
C:\Users\Owner\Documents\Programming\SFML-1.6\include/SFML/Window/Input.hpp:44:16: error: within this context
In file included from C:\Users\Owner\Documents\Programming\SFML-1.6\include/SFML\Window.hpp:37:0,
                 from C:\Users\Owner\Documents\Programming\Pong\main.cpp:15:
C:\Users\Owner\Documents\Programming\SFML-1.6\include/SFML/Window/Window.hpp: In copy constructor 'sf::Window::Window(const sf::Window&)':
C:\Users\Owner\Documents\Programming\SFML-1.6\include/SFML/Window/Window.hpp:55:16: note: synthesized method 'sf::Input::Input(const sf::Input&)' first required here
In file included from C:\Users\Owner\Documents\Programming\SFML-1.6\include/SFML\Graphics.hpp:38:0,
                 from C:\Users\Owner\Documents\Programming\Pong\main.cpp:17:
C:\Users\Owner\Documents\Programming\SFML-1.6\include/SFML/Graphics/RenderWindow.hpp: In copy constructor 'sf::RenderWindow::RenderWindow(const sf::RenderWindow&)':
C:\Users\Owner\Documents\Programming\SFML-1.6\include/SFML/Graphics/RenderWindow.hpp:45:16: note: synthesized method 'sf::Window::Window(const sf::Window&)' first required here
C:\Users\Owner\Documents\Programming\Pong\main.cpp: In function 'int main()':
C:\Users\Owner\Documents\Programming\Pong\main.cpp:219:58: note: synthesized method 'sf::RenderWindow::RenderWindow(const sf::RenderWindow&)' first required here
C:\Users\Owner\Documents\Programming\Pong\main.cpp:123:6: error:   initializing argument 1 of 'void Paddle::changePosition(sf::RenderWindow, sf::Clock)'
C:\Users\Owner\Documents\Programming\Pong\main.cpp:221:45: warning: comparison between 'enum sf::Event::EventType' and 'enum sf::Key::Code' [-Wenum-compare]

Sooo.... Lots of errors and I have no clue what they're about. Help, thanks in advance? :-\

6
General / Re: Procedure Entry Point?
« on: April 25, 2012, 04:29:03 pm »
Looks like that was where the problem was.

Thanks for being so patient, it's nice to know that not all computer forums are just a bunch of trolls who prey on any newbie who comes by looking for help. Definitely using this again.

7
General / Re: Procedure Entry Point?
« on: April 25, 2012, 04:07:12 pm »
How do I do use that? I can upload a dll, but how do i find which one my system depends on? Is it the codeblocks.dll file?

8
General / Re: Procedure Entry Point?
« on: April 24, 2012, 03:56:33 pm »
Deleted everything with SFML in it, re-downloaded it and started from scratch, with the exception of my program... Same error. What did I do wrong?

9
General / Re: Procedure Entry Point?
« on: April 20, 2012, 05:54:05 pm »
Yep, you're right. Capital S.

10
General / Re: Procedure Entry Point?
« on: April 20, 2012, 03:57:56 pm »
What's your version of MinGW/gcc?
4.6.2, I'm pretty sure.

And is the error message in your first post an exact copy and paste, or did you write it manually?

It's manually written because c::b wouldn't let me copy and paste it, but it's exact.

11
General / Re: Procedure Entry Point?
« on: April 20, 2012, 02:36:07 pm »

12
General / Re: Procedure Entry Point?
« on: April 20, 2012, 02:44:47 am »
I PM'ed it to you.

13
General / Re: Procedure Entry Point?
« on: April 19, 2012, 04:03:09 pm »
ftp://2012cstoneman@llstudents.org@llstudents.org/SFML/sfml-graphics.dll

This should be it, assuming I uploaded it right.

14
General / Re: Procedure Entry Point?
« on: April 18, 2012, 03:54:21 pm »
That might be it... Where would I look to clean that up?

Edit: That said, everything I've been doing since I switched to C::B has been with 1.6, so I'm wondering what could have gone wrong...

15
General / Re: Procedure Entry Point?
« on: April 18, 2012, 02:25:49 pm »
And... How would I do that? I'm sorry but I'm just clueless at this.

Pages: [1] 2
anything