1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
#include "graphiccore.h"
namespace Magi
{
SDL_Surface *Image ( std::string file ) {
SDL_Surface *temp = NULL , *image = NULL;
temp = IMG_Load ( file.c_str() );
if ( temp != NULL ) {
image = SDL_DisplayFormat ( temp );
SDL_FreeSurface ( temp );
}
return image;
}
void Graph::Clean()
{
if ( gGraph.size() > 0 )
{
std::vector<gImage>::iterator it;
for (it = gGraph.begin(); it < gGraph.end(); it++ )
{
SDL_FreeSurface( (*it).Img );
}
}
}
void Graph::Add( std::string img_name , std::string id )
{
if ( gMap[id] == 0 )
{
gImage tmp;
tmp.Img = Image( img_name );
gGraph.push_back( tmp );
gMap[id] = gGraph.size();
}
}
void Graph::Draw( std::string id , SDL_Surface *canvas , SDL_Rect rRect )
{
int idd;
idd = gMap[id];
if (idd > 0)
SDL_BlitSurface( gGraph[idd-1].Img , NULL , canvas , &rRect );
}
// adding text controls
void Text::Clean()
{
if ( tFont.size() > 0 )
{
std::vector<fFont>::iterator it;
for (it = tFont.begin(); it < tFont.end(); it++ )
{
TTF_CloseFont( (*it).font );
}
}
if ( tText.size() > 0 )
{
std::vector<gImage>::iterator itt;
for (itt = tText.begin(); itt < tText.end(); itt++ )
{
SDL_FreeSurface( (*itt).Img );
}
}
}
void Text::AddFont( std::string Font , int fontsize ,std::string id )
{
if (fMap[id] == 0)
{
fFont tmp;
tmp.font = TTF_OpenFont( Font.c_str() , fontsize );
if (tmp.font == NULL) { printf(" Null font"); }
tFont.push_back( tmp );
fMap[id] = tFont.size();
TTF_CloseFont( tmp.font );
}
}
void Text::SetFont( std::string Fontid )
{
if (fMap[Fontid] > 0)
{
currentfont = Fontid;
}
}
void Text::AddText( std::string text , std::string id , std::string fontid )
{
SDL_Color fcolor = { 255 ,255 ,255 };
if ( tMap[id] == 0 )
{
int idd;
idd = fMap[fontid];
gImage tmp;
if ( idd > 0 )
{
tmp.Img = TTF_RenderText_Solid( tFont[idd-1].font , text.c_str() , fcolor );
}
else
{
idd = fMap[currentfont];
tmp.Img = TTF_RenderText_Solid( tFont[idd-1].font , text.c_str() , fcolor );
}
tText.push_back( tmp );
tMap[id] = tText.size();
SDL_FreeSurface( tmp.Img );
}
}
void Text::DrawText( std::string idtxt , SDL_Surface *canvas , SDL_Rect rRect )
{
int idd , idmap;
idd = tMap[idtxt];
if ( idd > 0 )
{
SDL_BlitSurface( tText[idd-1].Img , NULL , canvas , &rRect );
}
else
{
idmap = fMap[currentfont];
SDL_Surface *tmp = NULL;
SDL_Color fcolor = { 255 ,255 ,255 };
tmp = TTF_RenderText_Solid( tFont[idmap-1].font , idtxt.c_str() , fcolor );
SDL_BlitSurface( tmp , NULL , canvas , &rRect );
SDL_FreeSurface ( tmp );
}
}
// added features
SDL_Rect Rect( int x, int y ) {
SDL_Rect rect;
rect.x = x;
rect.y = y;
return rect;
}
SDL_Rect Rect( int x, int y , int w , int h ) {
SDL_Rect rect;
rect.x = x;
rect.y = y;
rect.h = h;
rect.w = w;
return rect;
}
}
|