Here is a small, mostly skeletal example of OOP with SFML:
#include "Game.h"
Game::Game(void)
{
App=NULL;
// Create the main rendering window
App = new sf::RenderWindow(sf::VideoMode(800, 640, 32), "TileMap Example");
//hide the mouse cursor in the window
App->ShowMouseCursor(false);
GameState = STARTUP;
stateTime = 0;
the_map = new Map(App);
}
Game::~Game(void)
{
delete the_map;
delete App;
}
void Game::Run(void)
{
while (App->IsOpened())
{
// Process events
while (App->GetEvent(Event))
{
// Close window : exit
if (Event.Type == sf::Event::Closed)
{
App->Close();
return;
}
}
//perform our game logic
Logic(App->GetFrameTime());
// Display window contents on screen
App->Display();
}
}
void Game::DrawLevel(void)
{
the_map->DrawMap();
}
void Game::DoInput(void)
{
}
void Game::DoPhysics(float time_passed)
{
}
void Game::Logic(float time_passed)
{
switch(GameState)
{
case STARTUP:
{
the_map->SetScreen(0, 0, 10, 8);
GameState = LOAD1;
}
break;
case LOAD1:
{
the_map->LoadMap("data/Level1.txt");
GameState = PLAY1;
}
break;
case PLAY1:
{
DoInput();
DoPhysics(App->GetFrameTime());
DrawLevel();
}
break;
default:
break;
}//end switch
}
The Map class saves a pointer to the RenderWindow so it can draw it's sprites..the header file for the Map class might look like this:
#include <SFML/graphics.hpp>
#include <vector>
#include <string>
#include <fstream>
using namespace std;
class Map
{
private:
int rows, cols; //the # of rows and columns in the map array
sf::RenderWindow *App;
sf::Image *tilepic;
sf::Sprite *tilesheet;
float origin_x, origin_y; // pixel offsets of where to start drawing the map on the screen
int num_tiles_across_screen; //how many tiles to display across
int num_tiles_down_screen; //how many tiles to display down
vector<vector<int>> *map; //our 2D map array
int cell_width, cell_height; //dimensions of a single tile, in pixels
int num_tiles_across_tilesheet;//how many tiles are in a row on the tilesheet
public:
void SetScreen(float x, float y, int tiles_across, int tiles_down);
bool LoadMap(string fname);
bool SaveMap(string fname);
void DrawTile(int tile_num, float x, float y);
void DrawMap(void);
void FreeMem(void);
Map(sf::RenderWindow *the_App);
~Map(void);
};
The above is missing lots of stuff, but it gives you the idea of how you could organize your game. Given a Game class like that, the main.cpp becomes simply:
#include "Game.h"
int main()
{
// Create the Game
Game game;
// Start game loop
game.Run();
return EXIT_SUCCESS;
}