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

Author Topic: sf::RenderWindow App - Call it from a C++ Class?  (Read 2863 times)

0 Members and 1 Guest are viewing this topic.

JeZ-l-Lee

  • Jr. Member
  • **
  • Posts: 80
    • ICQ Messenger - 223180991
    • MSN Messenger - JeZLee@Live.com
    • AOL Instant Messenger - SLNTHERO@aol.com
    • View Profile
    • http://www.SilentHeroProductions.com
    • Email
sf::RenderWindow App - Call it from a C++ Class?
« on: February 04, 2009, 02:44:08 pm »
Hi,

How would I call:
"sf::RenderWindow App" from within a C++ class structure ?

I declare in the header:
sf::RenderWindow App;

And have this in the Class constructor:
sf::RenderWindow App(sf::VideoMode(800, 600), "SFML window");

It builds ok, but crashes on Window creation???


Here is the source:

Class Header File:
Code: [Select]
// Visuals header File...

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

class Visuals
{
public:

Visuals(void);
virtual ~Visuals(void);

sf::RenderWindow App;

sf::Font Arial;

int LoadFontsIntoMemory(void);
void UnloadFontsFromMemory(void);

void DrawTextOnScreenBuffer(char text[256]);
};


Class C++ File:
Code: [Select]
// Visuals C++ File...

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

#include "Visuals.h"

//----------------------------------------------------------------------------------------------------------
Visuals::Visuals(void)
{
sf::RenderWindow App(sf::VideoMode(800, 600), "SFML window");
}
//----------------------------------------------------------------------------------------------------------
Visuals::~Visuals(void)
{

}
//----------------------------------------------------------------------------------------------------------
int Visuals::LoadFontsIntoMemory(void)
{
if (!Arial.LoadFromFile("arial.ttf"))
return EXIT_FAILURE;

return EXIT_SUCCESS;
}
//----------------------------------------------------------------------------------------------------------
void Visuals::UnloadFontsFromMemory(void)
{
delete &Arial;
}
//----------------------------------------------------------------------------------------------------------
void Visuals::DrawTextOnScreenBuffer(char text[256])
{
sf::String Text2(text, Arial, 50);
App.Draw(Text2);
delete &Text2;
}
//----------------------------------------------------------------------------------------------------------


main.cpp file
Code: [Select]
// SFML PerfecT+EnginE Version 5.0 GT

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

#include "Visuals.h"

Visuals *visuals;

int main()
{
visuals = new Visuals();

// sf::RenderWindow App(sf::VideoMode(800, 600), "SFML window");



// Load a sprite to display
sf::Image Image;

if (!Image.LoadFromFile("cute_image2.bmp"))
return EXIT_FAILURE;

Image.CreateMaskFromColor(sf::Color(0, 255, 0, 255), 0);

#define NumberOfSprites 400
sf::Sprite Sprites[NumberOfSprites];
for (uint16_t index = 0; index < NumberOfSprites; index++)
Sprites[index].SetImage(Image);
/*
// Create a graphical string to display
sf::Font Arial;
if (!Arial.LoadFromFile("arial.ttf"))
return EXIT_FAILURE;
sf::String Text("Hello SFML", Arial, 50);
*/
// Load a music to play
sf::Music Music;
if (!Music.OpenFromFile("nice_music.ogg"))
return EXIT_FAILURE;

// Play the music
Music.Play();

// Globals to be moves later:
sf::Clock MyClock;
float MilliSeconds;
float NextFrameMS;
float NextSecond = MyClock.GetElapsedTime() + 1;
uint16_t FramesPerSecond = 0;
uint16_t FPS_Array[10];
for (uint8_t index = 0; index < 10; index++)  FPS_Array[index] = 0;
uint16_t FPS_ArrayIndex = 0;

char VariableText[256];

float SpriteRotation = 0;

// For testing graphic engine speed:
for (uint16_t index = 0; index < NumberOfSprites; index++)
{
Sprites[index].SetX(sf::Randomizer::Random(0.f, 640.f));
Sprites[index].SetY(sf::Randomizer::Random(0.f, 640.f));
}

if (!visuals->LoadFontsIntoMemory())
return EXIT_FAILURE;
//===============================================================================================
// Start the game loop
while (visuals->App.IsOpened())
{
MilliSeconds = MyClock.GetElapsedTime();
NextFrameMS  = MilliSeconds + .01667;

FramesPerSecond++;

if (MilliSeconds >= NextSecond)
{
FPS_Array[FPS_ArrayIndex] = FramesPerSecond;

FramesPerSecond = 0;
NextSecond = MyClock.GetElapsedTime() + 1;

FPS_ArrayIndex++;
if (FPS_ArrayIndex > 9)  FPS_ArrayIndex = 0;
}

// Process events
sf::Event Event;
while (visuals->App.GetEvent(Event))
{
// Close window : exit
if (Event.Type == sf::Event::Closed)
visuals->App.Close();
}

// Clear screen
visuals->App.Clear();

SpriteRotation++;
if (SpriteRotation > 359)  SpriteRotation = 0;

//Testing...
for (uint16_t index = 0; index < NumberOfSprites; index++)
{
Sprites[index].SetCenter(Sprites[index].GetSize().x / 2, Sprites[index].GetSize().y / 2);
Sprites[index].SetRotation(SpriteRotation);
// Draw the sprite
visuals->App.Draw(Sprites[index]);
}

uint16_t total = 0;
for (uint16_t index = 0; index < 10; index++)
{
total = total + FPS_Array[index];
}
total = total / 10;
sprintf(VariableText, "%d", total);
sf::String Text(VariableText, visuals->Arial, 50);
// visuals->DrawTextOnScreenBuffer("Hi there :)");

// Update the window
visuals->App.Display();

MilliSeconds = MyClock.GetElapsedTime();
if (NextFrameMS > MilliSeconds) sf::Sleep(NextFrameMS - MilliSeconds);
}
//===============================================================================================

visuals->UnloadFontsFromMemory();

delete visuals;

return EXIT_SUCCESS;
}
// A 100% By JeZ+Lee and mattmatteh!



Thanks in advance!!!


JeZ+Lee
SLNTHERO@aol.com
Silent Hero Productions(R)
Video Game Design Studio
http://www.SilentHeroProductions.com
JeZ+Lee
Silent Hero Productions(R)
Video Game Design Studio

http://www.SilentHeroProductions.com

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
sf::RenderWindow App - Call it from a C++ Class?
« Reply #1 on: February 04, 2009, 03:06:20 pm »
My god! I strongly recommend that you learn C++ first :)
Laurent Gomila - SFML developer

JeZ-l-Lee

  • Jr. Member
  • **
  • Posts: 80
    • ICQ Messenger - 223180991
    • MSN Messenger - JeZLee@Live.com
    • AOL Instant Messenger - SLNTHERO@aol.com
    • View Profile
    • http://www.SilentHeroProductions.com
    • Email
sf::RenderWindow App - Call it from a C++ Class?
« Reply #2 on: February 04, 2009, 03:09:52 pm »
Quote from: "Laurent"
My god! I strongly recommend that you learn C++ first :)



Sorry, yes I am learning C++ as I go...
If someone can tell me why it does not work then I will be happy :)


JeZ+Lee
SLNTHERO@aol.com
Silent Hero Productions(R)
Video Game Design Studio
http://www.SilentHeroProductions.com
JeZ+Lee
Silent Hero Productions(R)
Video Game Design Studio

http://www.SilentHeroProductions.com

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
sf::RenderWindow App - Call it from a C++ Class?
« Reply #3 on: February 04, 2009, 06:42:13 pm »
Because your code is wrong... ;)

Seriouly, it's C-- what you type, without getting offensive. Please don't start by using SFML when you're absolutely new to C++ programming -- and I guess you are, regarding to your code you posted here.

You didn't even read the tutorials, did you? There're tons of simple examples of how you open windows correctly..