5
« on: July 16, 2009, 07:30:42 pm »
Thanks a lot for your answers, but my problem remains.
When I use the code you gave, dunce, everything compiles neatly whithout any error or warning, but when I launch the application, the texts don't show up.
I'll give you my C++ codes (some comments are in french, sorry about that). Here is my main.cpp :
//main.cpp
// Les Includes de SFML
#include <iostream>
// L'Include de Snake
#include "Game.h"
// La fonction principale du programe
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
//int main(int argc, char **argv)
//int main()
{
Game SnSt2Game;
SnSt2Game.init();
SnSt2Game.loop();
std::cout << "Loop ended !" << std::cout;
return EXIT_SUCCESS;
}
//Game.h
// Defines
#define MAX_GAMEMODES 50
// Protection contre les inclusions multiples
#ifndef GAME_H
#define GAME_H
#include <iostream>
#include <fstream>
#include <SFML/Graphics.hpp>
#include "GameMode.h"
class Game
{
public:
// Méthodes
Game();
~Game();
void init();
void loop();
protected:
// Attributs
sf::RenderWindow window;
GameMode *gamemode[TOTALWORKINGMODES];
int active_gamemode;
};
#endif
//Game.cpp
#include "Game.h"
Game::Game() : window(sf::VideoMode::GetDesktopMode(), "SnakeSteaks 2", sf::Style::Fullscreen)
{
}
void Game::init()
{
for(int i = 0 ; i < TOTALWORKINGMODES ; i++)
{
gamemode[i] = new GameMode(window, i);
gamemode[i]->init();
}
active_gamemode = 0;
}
void Game::loop()
{
while(active_gamemode != EXITMODE)
{
std::cout << "Looping..." << std::endl;
window.Clear(sf::Color(255,255,255));
// Handle events
int new_mode = gamemode[active_gamemode]->handle_events();
if(new_mode != DEFAULTMODE && active_gamemode != EXITMODE)
{
active_gamemode = new_mode;
}
gamemode[active_gamemode]->update();
gamemode[active_gamemode]->refresh();
if(!(window.IsOpened()))active_gamemode = EXITMODE;
std::cout << "Game Mode : " << active_gamemode << std::endl;
window.Display();
}
}
Game::~Game()
{
for(int i = 0 ; i < TOTALWORKINGMODES ; i++)
{
delete gamemode[i];
}
}
//GameMode.h
// Defines
#define MAXSAMEOBJECTS 100
#define TOTALWORKINGMODES 1
#define EXITMODE -2
#define DEFAULTMODE -1
#define MENUMODE 0
#define WINDOW_X float(window.GetWidth())
#define WINDOW_Y float(window.GetHeight())
#define PERCENT_X * float(window.GetWidth()) / 100.f
#define PERCENT_Y * float(window.GetHeight()) / 100.f
// Protection contre les inclusions multiples
#ifndef GAMEMODE_H
#define GAMEMODE_H
#include <SFML/Graphics.hpp>
#include <iostream>
//#include "MyString.h"
class GameMode
{
public:
// Méthodes
GameMode(sf::RenderWindow &refwindow, int new_id);
void init();
int handle_events();
void handle_event(sf::Event &events);
void update();
void refresh();
~GameMode();
protected:
// Attributs
int mode_id;
int game_mode;
std::string name;
sf::Image img_fond;
sf::Sprite fond;
sf::Image image[MAXSAMEOBJECTS];
sf::Sprite sprite[MAXSAMEOBJECTS];
sf::String *texte[MAXSAMEOBJECTS];
sf::Font fonte[MAXSAMEOBJECTS];
sf::RenderWindow &window;
};
#endif
//GameMode.cpp
#include "GameMode.h"
GameMode::GameMode(sf::RenderWindow &refwindow, int new_id) : window(refwindow)
{
fonte[0].LoadFromFile("res/img/default.ttf");
for(int i = 0; i < MAXSAMEOBJECTS ; i++)
{
texte[i] = new sf::String("", fonte[0]);
}
switch(new_id)
{
case MENUMODE:
name = "Menu";
break;
default:
std::cout << "Error : Undefined mode " << new_id << std::endl;
name = "Error";
break;
}
mode_id = new_id;
}
void GameMode::init()
{
switch(mode_id)
{
case MENUMODE:
std::cout << "Initialising Menu" << std::endl;
// Loading resources
if (!img_fond.LoadFromFile("res/img/fond.jpg"))
{
std::cout << "Problem loading image." << std::endl;
}
else
{
std::cout << "Loading successful !" << std::endl;
}
// Using resources
fond.SetImage(img_fond);
fond.Resize(WINDOW_X, WINDOW_Y);
//fond.SetPosition(0.f, 0.f);
window.Draw(fond);
texte[0]->SetText("Solo");
texte[0]->SetSize(20);
texte[0]->SetColor(sf::Color::Black);
texte[0]->SetPosition((5 PERCENT_X),(10 PERCENT_Y));
texte[1]->SetText("Multi");
texte[1]->SetSize(20);
texte[1]->SetColor(sf::Color::Black);
texte[1]->SetPosition((5 PERCENT_X),(50 PERCENT_Y));
break;
default:
break;
}
}
int GameMode::handle_events()
{
game_mode = DEFAULTMODE;
sf::Event events;
while (window.GetEvent(events) && game_mode != EXITMODE)
{
if (events.Type == sf::Event::Closed)
game_mode = EXITMODE;
handle_event(events);
}
if(game_mode == EXITMODE)window.Close();
if(!(window.IsOpened()))game_mode = EXITMODE;
return game_mode;
}
void GameMode::handle_event(sf::Event &events)
{
switch(mode_id)
{
case MENUMODE:
if (events.Type == sf::Event::KeyPressed)
{
// Esc or Q : Quitter
if (events.Key.Code == sf::Key::Escape || events.Key.Code == sf::Key::Q)
game_mode = EXITMODE;
}
break;
default:
break;
}
}
void GameMode::update()
{
switch(mode_id)
{
case MENUMODE:
std::cout << "Nothing to update" << std::endl;
break;
default:
break;
}
}
void GameMode::refresh()
{
switch(mode_id)
{
case MENUMODE:
std::cout << "Drawing menu background..." << std::endl;
window.Draw(fond);
for(int i = 0 ; i < 2 ; i++)
{
window.Draw(*texte[i]);
}
window.Draw(*texte[0]);
break;
default:
break;
}
}
GameMode::~GameMode()
{
for(int i = 0 ; i < MAXSAMEOBJECTS ; i++)
{
delete texte[i];
}
}
Thanks a lot if you can help me !