Skip to content

Commit

Permalink
split to AppDraw class
Browse files Browse the repository at this point in the history
  • Loading branch information
cryham committed Apr 8, 2017
1 parent 18b6c00 commit 0b37da3
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 64 deletions.
42 changes: 5 additions & 37 deletions source/App.h
Original file line number Diff line number Diff line change
@@ -1,60 +1,28 @@
#pragma once
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include "AppDraw.h"
#include "Settings.h"


class App
class App : public AppDraw
{
public:
// main
//--------------------------
App();

bool Init();

bool KeyDown(const sf::Event::KeyEvent& key);
void Graph(); // draw graphics
void Gui(); // draw gui
void Gui(); // draw Gui


// vars
//--------------------------
float dt = 1.f; // frame delta time

int iFontH = 18; // font height


// sfml vars
//--------------------------
sf::RenderWindow* pWindow = nullptr;
sf::Sprite* pBackgr = nullptr;

sf::Font* pFont = nullptr;
sf::Text text;

sf::String s;
sf::Color clr;


// gui util -----
void SetupGuiClr();
void Sep(int y); // dummy separator
void Line(bool dark = false); //--


// sfml drawing utils

// set text color
//--------------------------
void Clr(sf::Uint8 r, sf::Uint8 g, sf::Uint8 b)
{
clr = sf::Color(r,g,b);
}

// write out text, from s
int Txt(int x, int y);

// fill rectangle
void Rect(int x, int y,
int sx, int sy,
sf::Uint8 r, sf::Uint8 g, sf::Uint8 b);
};
36 changes: 36 additions & 0 deletions source/AppDraw.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "AppDraw.h"


// draw utils
//------------------------------------------------------------------

// write out text
int AppDraw::Txt(int x, int y, bool draw)
{
text.setString(str);
//text.setStyle(bold ? sf::Text::Bold : sf::Text::Regular);
text.setColor(clr);
text.setPosition(x, y);
if (draw) pWindow->draw(text);
return text.getLocalBounds().width; // advance x pos
}

// clear rect
void AppDraw::Rect(int x, int y, int sx, int sy,
sf::Uint8 r, sf::Uint8 g, sf::Uint8 b)
{
pBackgr->setScale(sx-x, sy-y);
pBackgr->setPosition(x, y);
pBackgr->setColor(sf::Color(b, g, r));
pWindow->draw(*pBackgr);
}

// frame rect, inefficient
void AppDraw::Frame(int x, int y, int sx, int sy, int d,
sf::Uint8 r, sf::Uint8 g, sf::Uint8 b)
{
Rect(x, y, sx-d, y+d, r, g, b); // top
Rect(x, sy-d, sx-d, sy, r, g, b); // bottom
Rect(x, y, x+d, sy-d, r, g, b); // left
Rect(sx-d,y, sx, sy, r, g, b); // right
}
40 changes: 40 additions & 0 deletions source/AppDraw.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>


class AppDraw
{
public:

// sfml draw
//--------------------------
sf::RenderWindow* pWindow = nullptr;
sf::Sprite* pBackgr = nullptr;

sf::Font* pFont = nullptr;
sf::Text text;

sf::String str;
sf::Color clr;
bool bold = false;

float dt = 1.f; // frame delta time


// set text color
//--------------------------
void Clr(sf::Uint8 r, sf::Uint8 g, sf::Uint8 b)
{
clr = sf::Color(r,g,b);
}

// write out text, from s
// returns width, x advance
int Txt(int x, int y, bool draw=true);

// fill rect
void Rect(int x, int y, int sx, int sy, sf::Uint8 r, sf::Uint8 g, sf::Uint8 b);
// frame rect
void Frame(int x, int y, int sx, int sy, int d, sf::Uint8 r, sf::Uint8 g, sf::Uint8 b);
};
2 changes: 1 addition & 1 deletion source/App_Graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void App::Graph()

// test
Clr(155,215,255);
s = "Fps: " + f2s(1/dt,1,3);
str = "Fps: " + f2s(1/dt,1,3);
Txt(10, 0);

}
26 changes: 0 additions & 26 deletions source/App_Init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,9 @@ App::App()
}

// Init
//------------------------------------------------------------------
bool App::Init()
{
SetupGuiClr();

return true;
}


// utils
//------------------------------------------------------------------

// write out text
int App::Txt(int x, int y)
{
text.setString(s);
text.setColor(clr);
text.setPosition(x, y);
pWindow->draw(text);
return text.getLocalBounds().width; // advance x pos
}

// fill rect
void App::Rect(int x, int y,
int sx, int sy,
sf::Uint8 r, sf::Uint8 g, sf::Uint8 b)
{
pBackgr->setScale(sx-x, sy-y);
pBackgr->setPosition(x, y);
pBackgr->setColor(sf::Color(b, g, r));
pWindow->draw(*pBackgr);
}

0 comments on commit 0b37da3

Please sign in to comment.