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

Author Topic: [SOLVED] Re-use existing window  (Read 2783 times)

0 Members and 1 Guest are viewing this topic.

lmendes1979

  • Newbie
  • *
  • Posts: 2
    • View Profile
[SOLVED] Re-use existing window
« on: May 06, 2009, 01:18:22 pm »
Hi all,

I'm no expert in C++ but I have experience in other languages with similar syntax, so probably my problem relies only in C++ newbie syndrome.

The problem relies on the fact that I have one window defined as static, that is used from different classes (e.g Intro, Game).
I believe that would be that most logical thing to do, and not defining 1 window for each section/class.
The window is created perfectly and everything goes fine until the next class uses it (1.Intro, 2.Game).
If I don't redefine the window again in the Game class, it will freeze and not render anything to the window.
If I do redefine the window again in the Game class, then it will work, but by creating a new window instance.

Bellow the code I'm using for further analysis, didn't include it all since I don't believe its relevant.
Seriously I'm not trying to "hide" my amazing coding skills :D

main.cpp
Code: [Select]
// main.cpp
#include "cCore.h"

int main(){
// Core
::Core Core;
// Initialize game core
if(Core.Initialize()){
::Intro Intro;
if(Intro.Initialize()) Intro.Play();
::Game Game;
if(Game.Initialize()) Game.Play();
} else {
// Core initialization failed
printf("Could not create window!");
}

}


cCore.h
Code: [Select]
// cCore.h
#ifndef _CCORE_H_
#define _CCORE_H_

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

#include "globals.h"
(...)


globals.h
Code: [Select]

// globals.h
#ifndef _GLOBALS_H_
#define _GLOBALS_H_

// Global vars
static enum GameSection {SECTION_INTRO,SECTION_MENU,SECTION_GAME};
static GameSection Section;
static sf::RenderWindow App;
static int screenWidth=640;
static int screenHeight=480;
static int screenDepth=32;
static char *screenTitle="TEST v0.01";

#endif


cIntro.cp
Code: [Select]
// cIntro.cpp
#include "cCore.h"

// Init game
bool Intro::Initialize(){
::App.Create(sf::VideoMode(::screenWidth,::screenHeight,::screenDepth),::screenTitle);
// Load images
if(!this->imageLogoSadbyte.LoadFromFile("data/images/SadByteLogo.png")) return false;
if(!this->imageLogoXploit.LoadFromFile("data/images/XploitLogo.png")) return false;
// Load sounds
if(!this->sampleSadbyte.LoadFromFile("data/sounds/SadByteBoo.wav")) return false;
this->soundLogo.SetBuffer(sampleSadbyte);
return true;
}
(...)


cGame.cpp
Code: [Select]
// cGame.cpp
#include "cCore.h"

bool Game::Initialize(){
// Create window
::App.Create(sf::VideoMode(640,480),"Xploit v0.01");
// Load fonts
if(!this->fontVerdanaBold.LoadFromFile("data/fonts/arial.ttf")) return false;
// Load images
if(!this->imageShip.LoadFromFile("data/images/Ship.png")) return false;
this->spriteShip.SetImage(this->imageShip);
return true;
}
(...)

SamuraiCrow

  • Newbie
  • *
  • Posts: 40
    • Yahoo Instant Messenger - samuraileumas
    • View Profile
[SOLVED] Re-use existing window
« Reply #1 on: May 07, 2009, 11:07:00 pm »
I think the problem is that you're declaring your "globals" in a .h file rather than a .cpp file.  If you need to access them from elsewhere, use the extern directive in the .h file to allow them to link to the ones in the .cpp file.  Also, you don't need to mark globals as static.  They should be static automatically if they are truly global.

lmendes1979

  • Newbie
  • *
  • Posts: 2
    • View Profile
[SOLVED] Re-use existing window
« Reply #2 on: May 09, 2009, 08:32:10 am »
Yes, this was definitly a newbie issue.

Just needed to create one extra globals.cpp to declare the variables there, or even declare them in main.cpp and use the extern directive in the globals.h.

Thanks SamuraiCrow