git repos / blockattack-game

blame: source/code/CppSdlImageHolder.cpp

normal view · raw

c53e6443 sago007 2012-04-17 11:04 1
/*
af35c39d sago007 2013-11-16 16:37 2
===========================================================================
af35c39d sago007 2013-11-16 16:37 3
blockattack - Block Attack - Rise of the Blocks
af35c39d sago007 2013-11-16 16:37 4
Copyright (C) 2005-2013 Poul Sander
af35c39d sago007 2013-11-16 16:37 5
af35c39d sago007 2013-11-16 16:37 6
This program is free software: you can redistribute it and/or modify
af35c39d sago007 2013-11-16 16:37 7
it under the terms of the GNU General Public License as published by
af35c39d sago007 2013-11-16 16:37 8
the Free Software Foundation, either version 2 of the License, or
af35c39d sago007 2013-11-16 16:37 9
(at your option) any later version.
af35c39d sago007 2013-11-16 16:37 10
af35c39d sago007 2013-11-16 16:37 11
This program is distributed in the hope that it will be useful,
af35c39d sago007 2013-11-16 16:37 12
but WITHOUT ANY WARRANTY; without even the implied warranty of
af35c39d sago007 2013-11-16 16:37 13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
af35c39d sago007 2013-11-16 16:37 14
GNU General Public License for more details.
af35c39d sago007 2013-11-16 16:37 15
af35c39d sago007 2013-11-16 16:37 16
You should have received a copy of the GNU General Public License
af35c39d sago007 2013-11-16 16:37 17
along with this program.  If not, see http://www.gnu.org/licenses/
af35c39d sago007 2013-11-16 16:37 18
af35c39d sago007 2013-11-16 16:37 19
Source information and contacts persons can be found at
af35c39d sago007 2013-11-16 16:37 20
http://blockattack.sf.net
af35c39d sago007 2013-11-16 16:37 21
===========================================================================
af35c39d sago007 2013-11-16 16:37 22
*/
3db6ed5d sago007 2010-12-07 17:19 23
3db6ed5d sago007 2010-12-07 17:19 24
#include "CppSdlImageHolder.hpp"
3db6ed5d sago007 2010-12-07 17:19 25
#include "SDL_image.h"
3db6ed5d sago007 2010-12-07 17:19 26
c53e6443 sago007 2012-04-17 11:04 27
namespace CppSdl
c53e6443 sago007 2012-04-17 11:04 28
{
c53e6443 sago007 2012-04-17 11:04 29
c53e6443 sago007 2012-04-17 11:04 30
CppSdlImageHolder::CppSdlImageHolder()
c53e6443 sago007 2012-04-17 11:04 31
{
c53e6443 sago007 2012-04-17 11:04 32
	data = NULL;
c53e6443 sago007 2012-04-17 11:04 33
}
c53e6443 sago007 2012-04-17 11:04 34
c53e6443 sago007 2012-04-17 11:04 35
CppSdlImageHolder::CppSdlImageHolder(std::string filename)
c53e6443 sago007 2012-04-17 11:04 36
{
c53e6443 sago007 2012-04-17 11:04 37
	data = IMG_Load(filename.c_str());
c53e6443 sago007 2012-04-17 11:04 38
	if(!data)
c53e6443 sago007 2012-04-17 11:04 39
	{
c53e6443 sago007 2012-04-17 11:04 40
		//Here we should throw an exception
c53e6443 sago007 2012-04-17 11:04 41
		CppSdlException e(IMAGE,CPPSDL_ERROR_MISSINGFILE,"Could not read file \""+filename+"\"");
c53e6443 sago007 2012-04-17 11:04 42
		throw e;
c53e6443 sago007 2012-04-17 11:04 43
	}
c53e6443 sago007 2012-04-17 11:04 44
	SDL_GetClipRect(data,&area);
c53e6443 sago007 2012-04-17 11:04 45
	OptimizeForBlit();
c53e6443 sago007 2012-04-17 11:04 46
}
c53e6443 sago007 2012-04-17 11:04 47
c53e6443 sago007 2012-04-17 11:04 48
CppSdlImageHolder::CppSdlImageHolder(char* rawdata, int datasize)
c53e6443 sago007 2012-04-17 11:04 49
{
c53e6443 sago007 2012-04-17 11:04 50
	SDL_RWops *rw = SDL_RWFromMem (rawdata, datasize);
c53e6443 sago007 2012-04-17 11:04 51
c53e6443 sago007 2012-04-17 11:04 52
	//The above might fail and return null.
f3a1637d sago007 2015-11-14 21:32 53
	if (!rw) {
c53e6443 sago007 2012-04-17 11:04 54
		CppSdlException e(IMAGE,CPPSDL_ERROR_NULLPOINTER, "Could not read raw data");
c53e6443 sago007 2012-04-17 11:04 55
		throw e;
c53e6443 sago007 2012-04-17 11:04 56
	}
c53e6443 sago007 2012-04-17 11:04 57
c53e6443 sago007 2012-04-17 11:04 58
	data = IMG_Load_RW(rw,true); //the second argument tells the function to free RWops
c53e6443 sago007 2012-04-17 11:04 59
f3a1637d sago007 2015-11-14 21:32 60
	if (!data) {
c53e6443 sago007 2012-04-17 11:04 61
		CppSdlException e(IMAGE,CPPSDL_ERROR_DATA,"Could not convert raw data to image");
c53e6443 sago007 2012-04-17 11:04 62
		throw e;
c53e6443 sago007 2012-04-17 11:04 63
	}
c53e6443 sago007 2012-04-17 11:04 64
c53e6443 sago007 2012-04-17 11:04 65
	SDL_GetClipRect(data,&area);
c53e6443 sago007 2012-04-17 11:04 66
	OptimizeForBlit();
c53e6443 sago007 2012-04-17 11:04 67
}
c53e6443 sago007 2012-04-17 11:04 68
c53e6443 sago007 2012-04-17 11:04 69
CppSdlImageHolder::~CppSdlImageHolder()
c53e6443 sago007 2012-04-17 11:04 70
{
c53e6443 sago007 2012-04-17 11:04 71
	MakeNull();
3db6ed5d sago007 2010-12-07 17:19 72
}
3db6ed5d sago007 2010-12-07 17:19 73
3db6ed5d sago007 2010-12-07 17:19 74
SDL_Surface* CppSdlImageHolder::GetRawDataInsecure()
3db6ed5d sago007 2010-12-07 17:19 75
{
c53e6443 sago007 2012-04-17 11:04 76
	Initialized();
c53e6443 sago007 2012-04-17 11:04 77
	return data;
3db6ed5d sago007 2010-12-07 17:19 78
}
3db6ed5d sago007 2010-12-07 17:19 79
3db6ed5d sago007 2010-12-07 17:19 80
Uint32 CppSdlImageHolder::GetWidth()
3db6ed5d sago007 2010-12-07 17:19 81
{
f3a1637d sago007 2015-11-14 21:32 82
	if (IsNull())
c53e6443 sago007 2012-04-17 11:04 83
		return 0;
c53e6443 sago007 2012-04-17 11:04 84
	return area.w;
3db6ed5d sago007 2010-12-07 17:19 85
}
3db6ed5d sago007 2010-12-07 17:19 86
3db6ed5d sago007 2010-12-07 17:19 87
Uint32 CppSdlImageHolder::GetHeight()
3db6ed5d sago007 2010-12-07 17:19 88
{
c53e6443 sago007 2012-04-17 11:04 89
	if(IsNull())
c53e6443 sago007 2012-04-17 11:04 90
		return 0;
c53e6443 sago007 2012-04-17 11:04 91
	return area.h;
3db6ed5d sago007 2010-12-07 17:19 92
}
3db6ed5d sago007 2010-12-07 17:19 93
c53e6443 sago007 2012-04-17 11:04 94
void CppSdlImageHolder::PaintTo(SDL_Surface* target, int x, int y)
c53e6443 sago007 2012-04-17 11:04 95
{
c53e6443 sago007 2012-04-17 11:04 96
	static SDL_Rect dest; //static for reuse
c53e6443 sago007 2012-04-17 11:04 97
	if(IsNull())
c53e6443 sago007 2012-04-17 11:04 98
		return;
c53e6443 sago007 2012-04-17 11:04 99
	dest.x = x;
c53e6443 sago007 2012-04-17 11:04 100
	dest.y = y;
c53e6443 sago007 2012-04-17 11:04 101
	SDL_BlitSurface(data,&area, target,&dest);
c53e6443 sago007 2012-04-17 11:04 102
}
c53e6443 sago007 2012-04-17 11:04 103
c53e6443 sago007 2012-04-17 11:04 104
void CppSdlImageHolder::OptimizeForBlit(bool allowAlpha)
c53e6443 sago007 2012-04-17 11:04 105
{
c53e6443 sago007 2012-04-17 11:04 106
	static SDL_Surface *tmp;
c53e6443 sago007 2012-04-17 11:04 107
	Initialized();
c53e6443 sago007 2012-04-17 11:04 108
	if(allowAlpha)
c53e6443 sago007 2012-04-17 11:04 109
		tmp = SDL_DisplayFormatAlpha(data);
c53e6443 sago007 2012-04-17 11:04 110
	else
c53e6443 sago007 2012-04-17 11:04 111
		tmp = SDL_DisplayFormat(data);
c53e6443 sago007 2012-04-17 11:04 112
	SDL_FreeSurface(data);
c53e6443 sago007 2012-04-17 11:04 113
	data = tmp;
c53e6443 sago007 2012-04-17 11:04 114
}
c53e6443 sago007 2012-04-17 11:04 115
c53e6443 sago007 2012-04-17 11:04 116
void CppSdlImageHolder::Initialized()
c53e6443 sago007 2012-04-17 11:04 117
{
c53e6443 sago007 2012-04-17 11:04 118
	if(data == NULL)
c53e6443 sago007 2012-04-17 11:04 119
		throw(CppSdlException(IMAGE,CPPSDL_ERROR_NULLPOINTER,"ImageHolder used uninitialized!"));
c53e6443 sago007 2012-04-17 11:04 120
}
c53e6443 sago007 2012-04-17 11:04 121
c53e6443 sago007 2012-04-17 11:04 122
bool CppSdlImageHolder::IsNull()
c53e6443 sago007 2012-04-17 11:04 123
{
f3a1637d sago007 2015-11-14 21:32 124
	if(data == NULL ) {
c53e6443 sago007 2012-04-17 11:04 125
		return true;
f3a1637d sago007 2015-11-14 21:32 126
	}
f3a1637d sago007 2015-11-14 21:32 127
	return false;
c53e6443 sago007 2012-04-17 11:04 128
}
c53e6443 sago007 2012-04-17 11:04 129
c53e6443 sago007 2012-04-17 11:04 130
void CppSdlImageHolder::MakeNull()
c53e6443 sago007 2012-04-17 11:04 131
{
c53e6443 sago007 2012-04-17 11:04 132
	if(IsNull())
c53e6443 sago007 2012-04-17 11:04 133
		return;
f3a1637d sago007 2015-11-14 21:32 134
	SDL_FreeSurface(data);
f3a1637d sago007 2015-11-14 21:32 135
	data = NULL;
8d488d32 sago007 2011-04-17 13:02 136
}
8d488d32 sago007 2011-04-17 13:02 137
8d488d32 sago007 2011-04-17 13:02 138
}
1970-01-01 00:00 139