• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Cryusaki

SFML Class Sprite Loading Function Help

8 posts in this topic

So I created a basic pong game in SFML using a single main.cpp file. Now I want to better organize the game so I created a seperate class to handle loading in the sprites, displaying them and then destroying them. I wanted to keep everything simple so I made sure the loading in file was under Public: 

This is a line of code I use to load in a sprite 

    sf::Texture paddle;
    if(!paddle.loadFromFile("Paddle.png"))
        std::cout << "Could not load paddle." << std::endl;
    sf::Sprite sPaddle;
    sPaddle.setTexture(paddle); 

So in the main program I make a call to that function however when I debug and run the program it says that sPaddle was not declared in this scope. The function was called directly after declaring my variables and initializing the screen. Also the function is in the public section of the class. The line of code that is trying to call sPaddle specifically 

sPaddle.setPosition(0, SCREENHEIGHT/2 - (PADDLEHEIGHT/2));

comes directly after. What is wrong?

I never uploaded code before so if you need to look at it just let me know, Thanks smile.png

Edited by Cryusaki
0

Share this post


Link to post
Share on other sites

The seperate class to load, display and destroy the sprites is my form a Sprite Manager class, instead of having a wall of text in the main main.cpp file that loads all the files in I want to move it to another class and call that method (sorry I think I used the word function incorrectly in my last post) just as a way to sort the code up visually and mentally. The class will have a seperate methods to handle displaying at the end of the loop and then kill the images once the loop has ended completely. Not during the loop

 

Also my bad about the snippit of code that is showing the sprite to texture bit. I left out a line, embarassing :S

It should be edited back in now but incase it isnt here is the revised version

    sf::Texture paddle; // Here is the missing line
    if(!paddle.loadFromFile("Paddle.png"))
        std::cout << "Could not load paddle." << std::endl;
    sf::Sprite sPaddle;
    sPaddle.setTexture(paddle);

I have not taken an official programming language courses so my terminoligy is unsurprisingly poor, all my knowledge comes from free online help all over the web. Good to know though!

    sPaddle.setPosition(0, SCREENHEIGHT/2 - (PADDLEHEIGHT/2));  // Line 31

And ther Error message that is coming up in the build log is saying 

|31|error: 'sPaddle' was not declared in this scope|
Edited by Cryusaki
0

Share this post


Link to post
Share on other sites

That's still not enough code to solve your problem. I need to see the Sprite Manager class, and the part of the code (the entire function) where you create the Sprite Manager class, and the part of the code (the entire function) where you are using the Sprite Manager class to retrieve the the sprite and set it. I don't mind if it's too messy - I can read through the mess - but if you cut out pieces of your actual code before posting then it makes it harder for us to find the problem, and instead we have to guess what the problem is.
 
For example, I need to see the entire function that contains this line: (in addition to the entire Sprite Manager class).

sPaddle.setPosition(0, SCREENHEIGHT/2 - (PADDLEHEIGHT/2));

This forum's post editor has a button near the type that says 'code', and you can copy+paste your code into the box that it pops up. Just paste everything into it, even multiple files worth of text, and I'll sort it out and find the problem.

htk0.jpg

 

I have not taken and official programming language courses so my terminoligy is unsurprisingly poor, all my knowledge comes from free online help all over the web. Good to know though!

Same here! I'm entirely internet and book taught. I'm not trying to nitpick your terminology, just pointing out that if you are getting a 'not declared in that scope' message, than you can't truly be running/executing or debugging the code, because that's a compiler error not a runtime error - which is an important clue in solving the problem.

Terminology is something learned over time, piece by piece. Whether you say method or function doesn't bother me, because it's not related to the problem you are having. I get terminology wrong all the time. happy.png

Edited by Servant of the Lord
1

Share this post


Link to post
Share on other sites

Main.cpp

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <iostream>
#include "SpriteManager.h"

int main()
{
    SpriteManager SpriteMan;

    const int PADDLEWIDTH = 30, PADDLEHEIGHT = 90;
    const int BALLWIDTH = 30, BALLHEIGHT = 30;
    const int SCREENWIDTH = 800, SCREENHEIGHT = 600;

    float ballvelx = -0.75, ballvely = 0;
    float bpdelta = 0;
    int p1Score = 0, p2Score = 0;

    bool bLeft = true;

    float frameCounter = 0, switchFrame = 100, frameSpeed = 500;

    sf::RenderWindow window(sf::VideoMode(800, 600), "Pong Clone");

    SpriteMan.loadSprites();

    window.setKeyRepeatEnabled(false);

    sf::Clock clock;
    sf::Time time;

    sPaddle.setPosition(0, SCREENHEIGHT/2 - (PADDLEHEIGHT/2));
    sComputer.setPosition(SCREENWIDTH-PADDLEWIDTH , SCREENHEIGHT/2 - (PADDLEHEIGHT/2));
    sBall.setPosition(SCREENWIDTH/2 - (BALLWIDTH/2) , SCREENHEIGHT/2 - (BALLHEIGHT/2));

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

    switch(Event.type)
    {
        case sf::Event::KeyPressed:
            if(Event.key.code == sf::Keyboard::Up)
                sPaddle.move(0, -1);
            else if(Event.key.code == sf::Keyboard::Down)
                sPaddle.move(0, 1);
    }

            // Collision Check
        // Paddle/Screen Collision
        if(sPaddle.getPosition().y <= 0)
            sPaddle.setPosition(0,0);
        else if(sPaddle.getPosition().y + PADDLEHEIGHT >= SCREENHEIGHT)
            sPaddle.setPosition(0,SCREENHEIGHT - PADDLEHEIGHT);
        // Ball/Screen Collision
        if(sBall.getPosition().y <= 0)
            ballvely = ballvely * -1;
        else if(sBall.getPosition().y + BALLHEIGHT >= SCREENHEIGHT)
            ballvely = ballvely * -1;
        // Scoring
        else if(sBall.getPosition().x <= 0)
        {
            p2Score++;
            ballvelx = 0.75;
            ballvely = 0;
            sBall.setPosition(SCREENWIDTH/2 - (BALLWIDTH/2) , SCREENHEIGHT/2 - (BALLHEIGHT/2));
            std::cout << "Player 2 Scores!" << std::endl;
        }
        else if(sBall.getPosition().x + BALLWIDTH >= SCREENWIDTH)
        {
            p1Score++;
            ballvelx = -0.75;
            ballvely = 0;
            sBall.setPosition(SCREENWIDTH/2 - (BALLWIDTH/2) , SCREENHEIGHT/2 - (BALLHEIGHT/2));
            std::cout << "Player 1 Scores!" << std::endl;
        }
        // Ball/Paddle Collision
        else if(sBall.getPosition().x <= 30 &&
                sBall.getPosition().y + BALLHEIGHT >= sPaddle.getPosition().y &&
                sBall.getPosition().y <= sPaddle.getPosition().y + PADDLEHEIGHT)
        {
            bpdelta = (sBall.getPosition().y + BALLHEIGHT) - sPaddle.getPosition().y;
            std::cout << "BPDelta: " << bpdelta << std::endl;
            ballvelx = ballvelx * -1;
            if(bpdelta < 60)
            {
                ballvely = 1 / (bpdelta / 10);
                std::cout << "Delta <= 60" << std::endl;
                std::cout << ballvely << std::endl;
                if(ballvely > 0)
                    ballvely = ballvely + -1;
            }
            else if (bpdelta > 60)
            {
                bpdelta = bpdelta - 60;
                ballvely = bpdelta / 100;
                std::cout << "Delta > 60" << std::endl;
                std::cout << ballvely << std::endl;
            }
            else
                ballvely = 0;
        }
        // Ball/Computer Logic
            if(sBall.getPosition().x + BALLWIDTH >= SCREENWIDTH-PADDLEWIDTH &&
               sBall.getPosition().y + BALLHEIGHT >= sComputer.getPosition().y &&
               sBall.getPosition().y <= sComputer.getPosition().y + PADDLEHEIGHT)
            {
                bpdelta = (sBall.getPosition().y + BALLHEIGHT) - sComputer.getPosition().y;
                std::cout << "Computer Collide" << std::endl;
                ballvelx = ballvelx * -1;
                    if(bpdelta < 60)
                    {
                        ballvely = 1 / (bpdelta / 10);
                        if(ballvely > 0)
                            ballvely = ballvely + -1;
                    }
                    else if (bpdelta > 60)
                    {
                        bpdelta = bpdelta - 60;
                        ballvely = bpdelta / 100;
                    }
                    else if (bpdelta == 60)
                        ballvely = 0;
            }


            // Ball Logic
        sBall.move(ballvelx, ballvely);

            // AI Logic
        if(sBall.getPosition().y + (BALLHEIGHT/2) < sComputer.getPosition().y + (PADDLEHEIGHT/2))
            sComputer.move(0, -0.25);
        else
            sComputer.move(0, 0.25);

            // Clock Work
        frameCounter += clock.restart().asSeconds();
        if(frameCounter >= switchFrame)
            frameCounter = 0;

            // Drawing Screen
        window.clear();

        window.draw(sBackground);
        window.draw(sComputer);
        window.draw(sPaddle);
        window.draw(sBall);

        window.display();
    }

    return 0;
}

SpriteManager.h

#ifndef SPRITEMANAGER_H_INCLUDED
#define SPRITEMANAGER_H_INCLUDED

#include <iostream>

class SpriteManager
{
private:
public:
    void loadSprites();
};


#endif // SPRITEMANAGER_H_INCLUDED

SpriteManager.cpp

#include "SpriteManager.h"

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

void SpriteManager::loadSprites()
{
    sf::Texture background;
    if(!background.loadFromFile("Board.png"))
        std::cout << "Could not load background." << std::endl;
    sf::Sprite sBackground;
    sBackground.setTexture(background);

    sf::Texture paddle;
    if(!paddle.loadFromFile("Paddle.png"))
        std::cout << "Could not load paddle." << std::endl;
    sf::Sprite sPaddle;
    sPaddle.setTexture(paddle);

    sf::Sprite sComputer;
    sComputer.setTexture(paddle);

    sf::Texture ball;
    if(!ball.loadFromFile("Ball.png"))
        std::cout << "Could not load ball." << std::endl;
    sf::Sprite sBall;
    sBall.setTexture(ball);
}

0

Share this post


Link to post
Share on other sites

Glad to help!

 

Since functions are one scope, they also exist in a parent scope that is called the 'global' scope. You *can* put variables in the global scope:

int myGlobalVariable = 0;
 
void myFunction()
{
      int myLocalVariable; //A variable in the local scope of this function.
     
      myGlobalVariable = 200; //Accessing the variable in the global scope.
}
 
class MyClass
{
     public:
     int myMemberVariable; //A variable belonging to the scope of this class
};

 

To make it available to other files, you have to 'extern' it in a header file.

//MyGlobals.h
extern int myGlobalVariable; //Lets source files that #include this header become aware of the existence of the variable.
 
//MyGlobals.cpp
int myGlobalVariable = 0; //The actual variable.
 
//main.cpp
#include "MyGlobals.h"
 
void myFunction()
{
    myGlobalVariable = 357; //Works fine.
}

 

While this is available in C++, and while it's very convenient, it's actually a bad programming practice to get into except when used in extreme moderation. There are a number of ways it can bite you when your programs grow to larger sizes, so it's best to, when starting out programming, pretend that feature doesn't exist. mellow.png

2

Share this post


Link to post
Share on other sites

After about a half hour of intense trouble shooting I have found out my simple error, sorry for this now useless bump :P

Edited by Cryusaki
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0