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

Author Topic: Need help used separate void functions  (Read 3623 times)

0 Members and 1 Guest are viewing this topic.

AdventWolf

  • Jr. Member
  • **
  • Posts: 54
    • View Profile
Need help used separate void functions
« on: September 07, 2010, 12:43:36 am »
I'm having problems with separating my programs into separate functions.

Such as having a function that display's text from a string stored earlier in the program:

Code: [Select]
#include <SFML/Graphics.hpp>
#include <iostream>

void DisplayText();

int main()
{
    // Create main window
    sf::RenderWindow App(sf::VideoMode(326, 600), "Title");

    // Create a graphical string
    sf::String Hello;
    Hello.SetText("Hello!");
    Hello.SetColor(sf::Color(0, 128, 128));
    Hello.SetPosition(100.f, 100.f);
    Hello.SetSize(30.f);

    // Start game loop
    while (App.IsOpened())
    {
        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();
        }
DisplayText();
        App.Display();
    }
std::cout << "DONE" << std::endl;
std::cin.get();
return 0;
}

void DisplayText()
{
App.Draw(Hello);
}


What would I need to include so that the function includes the variables that I need to read?

Thanks.

TheMagician

  • Newbie
  • *
  • Posts: 7
    • View Profile
Need help used separate void functions
« Reply #1 on: September 07, 2010, 02:28:33 am »
Use a class:

Code: [Select]
#include <SFML/Graphics.hpp>

class Application
{
private:
sf::RenderWindow Window;
sf::String Hello;

void DrawStuff();
public:
Application();
void Run();
};

Application::Application()
{
Window.Create(sf::VideoMode(326, 600), "Title");

Hello.SetText("Hello!");
Hello.SetColor(sf::Color(0, 128, 128));
Hello.SetPosition(100.f, 100.f);
Hello.SetSize(30.f);
}

void Application::DrawStuff()
{
Window.Draw(Hello);
}

void Application::Run()
{
while (Window.IsOpened())
    {
        sf::Event Event;
        while (Window.GetEvent(Event))
        {
            if (Event.Type == sf::Event::Closed)
                Window.Close();
        }
 
DrawStuff();

        Window.Display();
    }
}

int main()
{
Application Game;
Game.Run();

return 0;
}


I didn't test it so it might have some bugs, but you get the idea.

AdventWolf

  • Jr. Member
  • **
  • Posts: 54
    • View Profile
Need help used separate void functions
« Reply #2 on: September 07, 2010, 02:57:35 am »
Sweet, I will take a look at it later. Thanks man!

I'm fairly familiar with classes, usually I just use structures.

AdventWolf

  • Jr. Member
  • **
  • Posts: 54
    • View Profile
Need help used separate void functions
« Reply #3 on: September 07, 2010, 07:02:26 am »
Now that I've got to mess with it again, I've got another question.

I want to make different classes to manage different game functions and link all the separate classes to a primary class to manage the other classes.

Such as this:
I have the same code as posted before, but an added Sprite Manager Class, to load the images. Then I link it with the Application Class, and I load the image, but my Spr_Mgr is undeclared.

Code: [Select]

#include <SFML/Graphics.hpp

//////////////////////
//Declare Sprite Class
//////////////////////
class SpriteManager
{
private:
sf::Image Image1;
public:
void LoadPicture();
};

////////////////////////
//Sprite Class Functions
////////////////////////
void SpriteManager::LoadPicture()
{
if( !Image1.LoadFromFile("Image1.png") ){}
sf::Sprite Image_1(Image1);
Image_1.SetPosition(150,150);
}

////////////////////
//Declare Game Class
////////////////////
class Application
{
private:
   sf::RenderWindow Window;
   sf::String Hello;

   void DrawStuff();
public:
   Application();
   void Run();
   SpriteManager Spr_Mgr(); //Create Sprite Manager for Application
};

//////////////////////
//Game Class Functions
//////////////////////
Application::Application()
{
   Window.Create(sf::VideoMode(400, 600), "Title");

   Spr_Mgr.LoadPictures(); //Load Pictures with Sprite Manager - ERROR, Spr_Mgr not recognized!!
   Hello.SetText("Hello!");
   Hello.SetColor(sf::Color(0, 128, 128));
   Hello.SetPosition(100.f, 100.f);
   Hello.SetSize(30.f);
}

void Application::DrawStuff()
{
   Window.Draw(Image_1); //Display the Picture
   Window.Draw(Hello);
}

void Application::Run()
{
   while (Window.IsOpened())
    {
        sf::Event Event;
        while (Window.GetEvent(Event))
        {
            if (Event.Type == sf::Event::Closed)
                Window.Close();
        }
 
        DrawStuff();
        Window.Display();
    }
}
///////////
//Main Game
///////////
int main()
{
   Application Game;
   Game.Run();

   return 0;
}
///END///


Any help is appreciated  :D

TheMagician

  • Newbie
  • *
  • Posts: 7
    • View Profile
Need help used separate void functions
« Reply #4 on: September 07, 2010, 07:53:14 am »
Code: [Select]

class Application
{
public:
   SpriteManager Spr_Mgr(); //Create Sprite Manager for Application
};


This is a function declaration. You are declaring a member function called Spr_Mgr that returns SpriteManager object. Get rid of the () to declare an object called Spr_Mgr of type SpriteManager.

You should also declare it private because you don't need to call it through an instantiated Application object, like in the main() function.

Code: [Select]

Application::Application()
{
   Spr_Mgr.LoadPictures(); //Load Pictures with Sprite Manager - ERROR, Spr_Mgr not recognized!!
}


This occurs due to the above mistake.

Code: [Select]

void Application::DrawStuff()
{
   Window.Draw(Image_1); //Display the Picture
}


You might want to read up on encapsulation and class access specifiers. You're trying to call the object Image_1 declared within the Application class. Your SpriteManager class would have to either have a member function that returns a reference or pointer to your Image_1 object or declare the Image_1 object as public and then call it like Spr_Mgr.Image_1.

Anyway, your SpriteManager class design won't work well in the long run because you will have a lot redundant code. There are readily available resource managers in the wiki you may want to take a look at: http://www.sfml-dev.org/wiki/en/sources.

/me waits for corrections from the experts.

AdventWolf

  • Jr. Member
  • **
  • Posts: 54
    • View Profile
Need help used separate void functions
« Reply #5 on: September 07, 2010, 09:20:00 pm »
Thanks for the links, after messing with it a little bit more and looking at some examples I understand how to set it up now :D.