C++ > cGraphicEngine.cpp

TitrecGraphicEngine.cpp
Postée le14-02-2009
Affichée744
Mini-lien
Description

By ElHuron

EtatNe contient pas d'erreurs. Ne contient pas d'erreurs.
Code d'insertion
Options
Afficher les numéros de lignes  Mettre la source en plein ecran  Selectionner la source  Partager sur Facebook 
Téléchargement Telecharger en format txt  Telecharger en format pdf  Telecharger en format cpp
Plein ecran
#include "graphicEngine.hpp"

cGraphicEngine::cGraphicEngine (const int& width, const int& height, const char* title)
{
        mApplication = new sf::RenderWindow (sf::VideoMode(width, height), title);
        mBackgroundColor = sf::Color (0, 0, 0);
       
        incSprite = 1;
        incString = -1;
        return;
}

cGraphicEngine::~cGraphicEngine (void)
{
        delete mApplication;
        return;
}

void cGraphicEngine::setBackgroundColor (int red, int green, int blue)
{
        controlColor (&red);
        controlColor (&green);
        controlColor (&blue);
       
        mBackgroundColor = sf::Color (red, green, blue);
        return;
}

void cGraphicEngine::controlColor (int* color)
{
        if (*color < 0)
                *color = 0;
        else if (*color > 255)
                *color = 255;
        return;
}

bool cGraphicEngine::updateEvent (void)
{
        bool back = true;
       
        sf::Event event;
        while (mApplication->GetEvent(event))
                if (event.Type == sf::Event::Closed)
                        back = false;
       
        mEventHandler.updateEvent (mApplication);
       
        return back;
}

void cGraphicEngine::frame (void)
{
        mApplication->Clear (mBackgroundColor);
        for (int i=0 ; i<(incSprite-1) ; i++)
                if (!hideSpriteList[i])
                        mApplication->Draw (spriteList[i]);
        for (int i=0 ; i<(-incString-1) ; i++)
                if (!hideStringList[i])
                        mApplication->Draw (stringList[i]);
        mApplication->Display ();
        return;
}

int cGraphicEngine::addSprite (const char* spriteName, float posX, float posY)
{
        sf::Image image;
        if (!image.LoadFromFile(spriteName))
                exit (EXIT_FAILURE);
        imageList.push_front (image);
        spriteList.push_front (sf::Sprite(imageList[incSprite-1], sf::Vector2f (posX, posY)));
        hideSpriteList.push_front (false);
        return incSprite++;
}

int cGraphicEngine::addString (const char* textName, const char* fontName, const int& size, const int& posX, const int& posY)
{
        sf::Font font;
        if (strlen(fontName) == 0)
                font = sf::Font::GetDefaultFont ();
        else if (!font.LoadFromFile(fontName))
                exit (EXIT_FAILURE);
        fontList.push_front (font);
        sf::String string = sf::String(textName, fontList[-incString-1], size);
        string.SetPosition (sf::Vector2f (posX, posY));
        stringList.push_front (string);
        hideStringList.push_front (false);
        return incString--;
}

void cGraphicEngine::hideSurface (const int& ref, const bool& hide)
{
        if (ref > 0)
        {
                if (ref > incSprite)
                        exit (EXIT_FAILURE);
                hideSpriteList[ref-1] = hide;
        }
        else
        {
                if (ref < incString)
                        exit (EXIT_FAILURE);
                hideStringList[-ref-1] = hide;
        }
        return;
}

void cGraphicEngine::moveSurface (const int& ref, const float& moveX, const float& moveY)
{
        if (ref > 0)
        {
                if (ref > incSprite)
                        exit (EXIT_FAILURE);
                spriteList[ref-1].Move (moveX, moveY);
        }
        else
        {
                if (ref < incString)
                        exit (EXIT_FAILURE);
                stringList[-ref-1].Move (moveX, moveY);
        }
        return;
}

void cGraphicEngine::deleteSurfaces (void)
{
        spriteList.clear (); hideSpriteList.clear (); imageList.clear();
        stringList.clear (); hideStringList.clear (); fontList.clear();
        incSprite = 1;
        incString = -1;
        return;
}