GsGraphics.cpp 7.55 KB
Newer Older
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
/*
 * GsGraphics.cpp
 *
 *  Created on: 26.08.2009
 *      Author: gerstrong
 */

#include "GsGraphics.h"
#include <base/GsLogging.h>
#include <base/utils/StringUtils.h>
#include <base/CInput.h>

#include <base/video/GsEffectController.h>

#include "GsFont.h"
#include "GsTilemap.h"
#include "GsSprite.h"
#include "GsBitmap.h"
#include "GsPalette.h"
#include "GsCursor.h"


///
// Creation Routines
///
void GsGraphics::createEmptyTilemaps(const size_t num)
{
	freeTilemap();
    Tilemap.resize(num);
}

void GsGraphics::createEmptySprites(const size_t numVar,
                                    const size_t numSprites)
{
	freeSprites();
    GsSprite sprite;

    std::vector<GsSprite> spriteVec;

    spriteVec.assign(numSprites, sprite);

    mSprite.assign(numVar, spriteVec);
}

45 46 47 48 49
void GsGraphics::appendSpriteVec(const std::vector<GsSprite> &spriteVec)
{
    mSprite.push_back(spriteVec);
}

50 51 52 53 54
void GsGraphics::appendBitmapVec(const std::vector<GsBitmap> &bmpVec)
{
    mBitmap.push_back(bmpVec);
}

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
void GsGraphics::dumpSprites()
{
    for(size_t i = 0; i < mSprite[0].size(); i++)
	{
        GsSprite &thisSprite = mSprite[0][i];

        //const auto thisSfc = thisSprite.Surface().getSDLSurface();
        auto &thisSfc = thisSprite.Surface();

		// Temporary for testing!!
		std::string fname = "sprite";
		fname += itoa(i);
		fname += ".bmp";                
        SDL_SaveBMP(thisSfc.getSDLSurface(), fname.c_str());
	}
}


void GsGraphics::optimizeSprites()
{    
    for(auto &spriteVec : mSprite)
    {
        for(auto &sprite : spriteVec)
        {            
            sprite.optimizeSurface();
        }
    }    
}

84 85
void GsGraphics::createEmptyBitmaps(const size_t numVar,
                                    Uint16 num_bmps)
86
{
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
    for(auto &bitmap : mBitmap)
    {
        freeBitmaps(bitmap);
    }

    mBitmap.clear();

    for(size_t i=0 ; i<numVar ; i++)
    {
        std::vector<GsBitmap> bitmapVec;

        GsBitmap bitmap;
        bitmapVec.assign(num_bmps, bitmap);

        mBitmap.push_back( bitmapVec );
    }
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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
}

void GsGraphics::createEmptyMaskedBitmaps(Uint16 num_bmps)
{
	freeBitmaps(maskedBitmap);
    GsBitmap bitmap;
	maskedBitmap.assign(num_bmps, bitmap);
}

void GsGraphics::createEmptyMisGsBitmaps(Uint16 num_bmps)
{
    freeBitmaps(miscGsBitmap);
    GsBitmap bitmap;
    miscGsBitmap.assign(num_bmps, bitmap);
}



void GsGraphics::createEmptyFontmaps(Uint8 num_fonts)
{
	freeFonts();
    Font.assign(num_fonts, GsFont());
}

/*void GsGraphics::createEmptyCursorMap(SDL_Surface *surface)
{
	freeCursor();
    mpCursor.reset(new GsCursor(surface));
}*/

/**
 * Draws some digits using galaxy style
 */
void GsGraphics::drawDigits(const std::string& text, Uint16 x, Uint16 y, SDL_Surface *blitsurface)
{
    GsTilemap &Tilemap = getTileMap(2);

	for(Uint16 i=0 ; i<text.size() ; i++)
	{
		const char c = text[i];
		Tilemap.drawTile(blitsurface, x+i*8, y, (c != ' ') ? 43+c-'1' : 41);
	}
}

/**
 * Draw one digits using galaxy style
 */
void GsGraphics::drawDigit(const char c, const Uint16 x, const Uint16 y, SDL_Surface *blitsurface)
{
    GsTilemap &Tilemap = getTileMap(2);
	Tilemap.drawTile(blitsurface, x, y, c);
}


///
// Destructors
///
/*void GsGraphics::freeCursor()
{
	mpCursor.release();
}*/
void GsGraphics::freeTilemap()
{
    if( !Tilemap.empty() )
        Tilemap.clear();
}
void GsGraphics::freeFonts()
{
  while ( !Font.empty() )
    Font.pop_back();
}

175 176 177 178 179 180
void GsGraphics::freeBitmaps(std::vector<GsBitmap> &bitmapVec)
{    
    while ( !bitmapVec.empty() )
    {
        bitmapVec.pop_back();
    }
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
}

void GsGraphics::freeSprites()
{
    if( !mSprite.empty() )
        mSprite.clear();
}

void GsGraphics::copyTileToSprite( const int var, Uint16 t, Uint16 s, Uint16 ntilestocopy )
{
	SDL_Rect src_rect, dst_rect;

    auto tileMapPtr = Tilemap.at(1).getSDLSurface();
	
	src_rect.w = src_rect.h = 16;
	dst_rect.w = dst_rect.h = 16;

    mSprite[var][s].setSize( 16, 16*ntilestocopy );
    mSprite[var][s].createSurface( tileMapPtr->flags, Palette.m_Palette );
	
	for(Uint8 i=0 ; i<ntilestocopy ; i++)
	{
		src_rect.x = 16*((t+i)%13);
		src_rect.y = 16*((t+i)/13);
		
		dst_rect.x = 0;
		dst_rect.y = 16*i;

        //auto spriteSfc = mSprite[var][s].Surface().getSDLSurface();
        //BlitSurface( tileMapPtr, &src_rect, spriteSfc.get(), &dst_rect);

        auto &spriteSfc = mSprite[var][s].Surface();
        BlitSurface( tileMapPtr, &src_rect, spriteSfc.getSDLSurface(), &dst_rect);
	}
}

// draw an empty dialog box, for youseeinyourmind(), etc.
// Maybe this should go to GsFont, because it's drawn by fonts...
void GsGraphics::drawDialogBox(SDL_Surface *DialogSurface,
                               int x1, int y1,
                               int w, int h,
                               Uint32 colour)
{
	int x,y,i,j;
	SDL_Rect rect;
	rect.x = x1*8; rect.w = (w+1)*8;
	rect.y = y1*8; rect.h = (h+1)*8;
	
	SDL_FillRect(DialogSurface, &rect, colour);

	Font[0].drawCharacter(DialogSurface, 1, x1*8, y1*8);
	Font[0].drawCharacter(DialogSurface, 3, (x1+w)*8, y1*8);
	for(x=(x1*8)+8,i=0;i<w-1;i++)
	{
		Font[0].drawCharacter(DialogSurface, 2, x, y1*8);
		x+=8;
	}
	y=(y1+1)*8;
	for(j=0;j<h-2;j++)
	{
		for(x=(x1*8),i=0;i<=w;i++)
		{
			if (i==0) Font[0].drawCharacter(DialogSurface, 4, x, y);
			else if (i==w) Font[0].drawCharacter(DialogSurface, 5, x, y);
			x+=8;
		}
		y+=8;
	}
    for(x=(x1*8),i=0;i<=w;i++)
    {
		if (i==0) Font[0].drawCharacter(DialogSurface, 6, x, y);
		else if (i==w) Font[0].drawCharacter(DialogSurface, 8, x, y);
		else Font[0].drawCharacter(DialogSurface, 7, x, y);
		x+=8;
    }
}

///
// Getters
///

int GsGraphics::getNumSprites(const int var)
{   return mSprite[var].size(); }

GsTilemap &GsGraphics::getTileMap(size_t tilemap)
{   return Tilemap.at(tilemap); }

std::vector<GsTilemap> &GsGraphics::getTileMaps()
{   return Tilemap; }

271 272 273 274 275 276
void GsGraphics::setBitmapNameForIdx(const std::string name,
                                     const int idx)
{
    mBitmapNameToIdx[name] = idx;
}

277 278 279 280
GsBitmap &GsGraphics::getBitmapFromId(const int var,
                                      const int slot)
//{   return mBitmap[var][slot];    }
{   return mBitmap[0][slot];   }
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297

GsBitmap &GsGraphics::getMaskedBitmap(Uint16 slot)
{   return maskedBitmap[slot];   }

GsBitmap &GsGraphics::getMiscGsBitmap(Uint16 slot)
{   return miscGsBitmap[slot];   }


GsSprite &GsGraphics::getSprite(const int var, const int slot)
{   return mSprite[var][slot];  }

std::vector<GsSprite> &GsGraphics::getSpriteVec(const int var)
{   return mSprite[var];    }

GsSprite &GsGraphics::getSpecialSpriteRef(const std::string &name)
{   return mSpecialSpriteMap[name];    }

298 299
GsFont &GsGraphics::getFont(Uint8 index)
{    return Font.at(index); }
300

301
GsBitmap *GsGraphics::getBitmapFromStr(const size_t sprVar,
302
                                       const std::string &name) const
303
{
304
    const size_t bmpVecIdx = sprVar;
305

306
    auto it = mBitmapNameToIdx.find(name);
307

308 309 310 311 312 313
    if(it != mBitmapNameToIdx.end())
    {
        const int idx = it->second;
        auto &bitmap = mBitmap[bmpVecIdx][size_t(idx)];
        return const_cast<GsBitmap*>(&bitmap);
    }
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347

	std::string error = "Ooops! Wrong TextID ";
	error += "name";
	error += "used!";
	gLogging.textOut(error);

    return nullptr;
}

GsSprite *GsGraphics::getSprite(const int var, const std::string &name) const
{
	std::string s_name;
    for(unsigned int i=0 ; i<mSprite[var].size() ; i++)
	{
        s_name = mSprite[var][i].getName();

		if(s_name == name)
        {
            return const_cast<GsSprite*>(&(mSprite[var][i]));
        }
	}

    return nullptr;
}

///
// Process Routines
///



GsGraphics::~GsGraphics()
{
	freeBitmaps(maskedBitmap);
348

349 350 351 352
	freeSprites();
	freeFonts();
	freeTilemap();
}