git repos / blockattack-game

source/code/CppSdlImageHolder.cpp

browsing at commit = f3a1637d5a24c4b44564fd54911fbe2fccfc2e7c

raw · blame · history

/*
===========================================================================
blockattack - Block Attack - Rise of the Blocks
Copyright (C) 2005-2013 Poul Sander

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see http://www.gnu.org/licenses/

Source information and contacts persons can be found at
http://blockattack.sf.net
===========================================================================
*/

#include "CppSdlImageHolder.hpp"
#include "SDL_image.h"

namespace CppSdl
{

CppSdlImageHolder::CppSdlImageHolder()
{
	data = NULL;
}

CppSdlImageHolder::CppSdlImageHolder(std::string filename)
{
	data = IMG_Load(filename.c_str());
	if(!data)
	{
		//Here we should throw an exception
		CppSdlException e(IMAGE,CPPSDL_ERROR_MISSINGFILE,"Could not read file \""+filename+"\"");
		throw e;
	}
	SDL_GetClipRect(data,&area);
	OptimizeForBlit();
}

CppSdlImageHolder::CppSdlImageHolder(char* rawdata, int datasize)
{
	SDL_RWops *rw = SDL_RWFromMem (rawdata, datasize);

	//The above might fail and return null.
	if (!rw) {
		CppSdlException e(IMAGE,CPPSDL_ERROR_NULLPOINTER, "Could not read raw data");
		throw e;
	}

	data = IMG_Load_RW(rw,true); //the second argument tells the function to free RWops

	if (!data) {
		CppSdlException e(IMAGE,CPPSDL_ERROR_DATA,"Could not convert raw data to image");
		throw e;
	}

	SDL_GetClipRect(data,&area);
	OptimizeForBlit();
}

CppSdlImageHolder::~CppSdlImageHolder()
{
	MakeNull();
}

SDL_Surface* CppSdlImageHolder::GetRawDataInsecure()
{
	Initialized();
	return data;
}

Uint32 CppSdlImageHolder::GetWidth()
{
	if (IsNull())
		return 0;
	return area.w;
}

Uint32 CppSdlImageHolder::GetHeight()
{
	if(IsNull())
		return 0;
	return area.h;
}

void CppSdlImageHolder::PaintTo(SDL_Surface* target, int x, int y)
{
	static SDL_Rect dest; //static for reuse
	if(IsNull())
		return;
	dest.x = x;
	dest.y = y;
	SDL_BlitSurface(data,&area, target,&dest);
}

void CppSdlImageHolder::OptimizeForBlit(bool allowAlpha)
{
	static SDL_Surface *tmp;
	Initialized();
	if(allowAlpha)
		tmp = SDL_DisplayFormatAlpha(data);
	else
		tmp = SDL_DisplayFormat(data);
	SDL_FreeSurface(data);
	data = tmp;
}

void CppSdlImageHolder::Initialized()
{
	if(data == NULL)
		throw(CppSdlException(IMAGE,CPPSDL_ERROR_NULLPOINTER,"ImageHolder used uninitialized!"));
}

bool CppSdlImageHolder::IsNull()
{
	if(data == NULL ) {
		return true;
	}
	return false;
}

void CppSdlImageHolder::MakeNull()
{
	if(IsNull())
		return;
	SDL_FreeSurface(data);
	data = NULL;
}

}