Wednesday, November 2, 2016

HUD!




















main.cpp
-----------------------------------------------------------------------------------------------
#include <SFML/Graphics.hpp>
#include "FuelBar.h"


using namespace sf;

int main()
{
    RenderWindow window(VideoMode(270, 480), "HUD!", Style::Close);
   
    //////////////////////////////////////////////
    FuelBar mFuelBar;
    mFuelBar.setPos(20.f, 300.f);
   
    while (window.isOpen())
    {
        Event event;
        while (window.pollEvent(event))
        {
            if (event.type == Event::Closed)
                window.close();
        }

        window.clear();

        /////////////////////////////////////////////
        for (Sprite n : mFuelBar.get()) {
            window.draw(n);
        }

        window.display();
    }

    return 0;
}

BarBackground.cpp
--------------------------------------------------------------------------------------
#include <SFML/Graphics.hpp>
#include <string>
#include "BarBackground.h"


BarBackground::BarBackground(string fileName)
{
    mTex.loadFromFile(fileName);
    mSpr.setTexture(mTex);
    mSpr.setPosition(0, 0);
}


BarBackground::~BarBackground()
{
}

void BarBackground::setPos(float x, float y)
{
    mSpr.setPosition(x, y);
}

Sprite& BarBackground::get()
{
   
    return mSpr;
}

BarBackground.h
-----------------------------------------------------------------------------
#pragma once

using namespace std;
using namespace sf;

class BarBackground
{
public:
    BarBackground(string);
    ~BarBackground();

    void setPos(float, float);
    Sprite& get();

private:
    Texture mTex;
    Sprite mSpr;
};

BarIcon.cpp
-------------------------------------------------------------------------------------
#include <SFML/Graphics.hpp>
#include <string>
#include "BarIcon.h"


BarIcon::BarIcon(string fileName)
{
    mTex.loadFromFile(fileName);
    mSpr.setTexture(mTex);
    mSpr.setPosition(0, 0);
}


BarIcon::~BarIcon()
{
}

void BarIcon::setPos(float x, float y)
{
    mSpr.setPosition(x, y);
}

Sprite& BarIcon::get()
{

    return mSpr;
}








BarIcon.h
-------------------------------------------------------------------
#pragma once

using namespace std;
using namespace sf;

class BarIcon
{
public:
    BarIcon(string);
    ~BarIcon();

    void setPos(float, float);
    Sprite& get();

private:
    Texture mTex;
    Sprite mSpr;
};

BarFuel.cpp
--------------------------------------------------------------------------
#include <SFML/Graphics.hpp>
#include "FuelBar.h"
#include <string>


FuelBar::FuelBar() : mBarBackground("graphics/hud.png"), mBarIcon("graphics/fuel.png")
{
    fuel = 100.f;

}


FuelBar::~FuelBar()
{
}

void FuelBar::setPos(float x, float y)
{
    // Background
    mBarBackground.setPos(x, y);

    // Status


    // lIcon
    mBarIcon.setPos(x + 5, y + 5);
}

vector<Sprite>& FuelBar::get()
{

    mFuelSprites.push_back(mBarBackground.get());
    mFuelSprites.push_back(mBarIcon.get());

    return mFuelSprites;
       
}

void FuelBar::updateFuelBar()
{

}

BarFuel.h
-----------------------------------------------------------------------------
#include "BarBackground.h"
#include "BarIcon.h"
#pragma once


using namespace std;
using namespace sf;

class FuelBar
{
public:
    FuelBar();
    ~FuelBar();

    void setPos(float, float);
    vector<Sprite>& get();

    void updateFuelBar();

private:
    float fuel;
    vector<Sprite> mFuelSprites;

    BarBackground mBarBackground;
    BarIcon mBarIcon;

};