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
	counter = new int;
c53e6443 sago007 2012-04-17 11:04 34
	(*counter)=1;
c53e6443 sago007 2012-04-17 11:04 35
}
c53e6443 sago007 2012-04-17 11:04 36
c53e6443 sago007 2012-04-17 11:04 37
CppSdlImageHolder::CppSdlImageHolder(std::string filename)
c53e6443 sago007 2012-04-17 11:04 38
{
c53e6443 sago007 2012-04-17 11:04 39
	data = IMG_Load(filename.c_str());
c53e6443 sago007 2012-04-17 11:04 40
	if(!data)
c53e6443 sago007 2012-04-17 11:04 41
	{
c53e6443 sago007 2012-04-17 11:04 42
		//Here we should throw an exception
c53e6443 sago007 2012-04-17 11:04 43
		CppSdlException e(IMAGE,CPPSDL_ERROR_MISSINGFILE,"Could not read file \""+filename+"\"");
c53e6443 sago007 2012-04-17 11:04 44
		throw e;
c53e6443 sago007 2012-04-17 11:04 45
	}
c53e6443 sago007 2012-04-17 11:04 46
	SDL_GetClipRect(data,&area);
c53e6443 sago007 2012-04-17 11:04 47
	OptimizeForBlit();
c53e6443 sago007 2012-04-17 11:04 48
	counter = new int;
c53e6443 sago007 2012-04-17 11:04 49
	*counter=1;
c53e6443 sago007 2012-04-17 11:04 50
}
c53e6443 sago007 2012-04-17 11:04 51
c53e6443 sago007 2012-04-17 11:04 52
CppSdlImageHolder::CppSdlImageHolder(const CppSdlImageHolder& orig)
c53e6443 sago007 2012-04-17 11:04 53
{
c53e6443 sago007 2012-04-17 11:04 54
	//Just take the data from the original. This is technically wrong but adds a little performance.
c53e6443 sago007 2012-04-17 11:04 55
	data = orig.data;
c53e6443 sago007 2012-04-17 11:04 56
	area = orig.area;
c53e6443 sago007 2012-04-17 11:04 57
	if(orig.counter)
c53e6443 sago007 2012-04-17 11:04 58
	{
c53e6443 sago007 2012-04-17 11:04 59
		counter = orig.counter;
c53e6443 sago007 2012-04-17 11:04 60
		(*counter)++;
c53e6443 sago007 2012-04-17 11:04 61
	}
c53e6443 sago007 2012-04-17 11:04 62
}
c53e6443 sago007 2012-04-17 11:04 63
c53e6443 sago007 2012-04-17 11:04 64
CppSdlImageHolder& CppSdlImageHolder::operator =(const CppSdlImageHolder& rhs)
c53e6443 sago007 2012-04-17 11:04 65
{
c53e6443 sago007 2012-04-17 11:04 66
	// Check for self-assignment!
c53e6443 sago007 2012-04-17 11:04 67
	if (this == &rhs)      // Same object?
c53e6443 sago007 2012-04-17 11:04 68
		return *this;        // Yes, so skip assignment, and just return *this.
c53e6443 sago007 2012-04-17 11:04 69
	MakeNull();
c53e6443 sago007 2012-04-17 11:04 70
	data = rhs.data;
c53e6443 sago007 2012-04-17 11:04 71
	area = rhs.area;
c53e6443 sago007 2012-04-17 11:04 72
	if(rhs.counter)
c53e6443 sago007 2012-04-17 11:04 73
	{
c53e6443 sago007 2012-04-17 11:04 74
		counter = rhs.counter;
c53e6443 sago007 2012-04-17 11:04 75
		(*counter)++;
c53e6443 sago007 2012-04-17 11:04 76
	}
c53e6443 sago007 2012-04-17 11:04 77
	return *this;
c53e6443 sago007 2012-04-17 11:04 78
}
c53e6443 sago007 2012-04-17 11:04 79
c53e6443 sago007 2012-04-17 11:04 80
CppSdlImageHolder::CppSdlImageHolder(char* rawdata, int datasize)
c53e6443 sago007 2012-04-17 11:04 81
{
c53e6443 sago007 2012-04-17 11:04 82
	SDL_RWops *rw = SDL_RWFromMem (rawdata, datasize);
c53e6443 sago007 2012-04-17 11:04 83
c53e6443 sago007 2012-04-17 11:04 84
	//The above might fail and return null.
c53e6443 sago007 2012-04-17 11:04 85
	if(!rw)
c53e6443 sago007 2012-04-17 11:04 86
	{
c53e6443 sago007 2012-04-17 11:04 87
		CppSdlException e(IMAGE,CPPSDL_ERROR_NULLPOINTER, "Could not read raw data");
c53e6443 sago007 2012-04-17 11:04 88
		throw e;
c53e6443 sago007 2012-04-17 11:04 89
	}
c53e6443 sago007 2012-04-17 11:04 90
c53e6443 sago007 2012-04-17 11:04 91
	data = IMG_Load_RW(rw,true); //the second argument tells the function to free RWops
c53e6443 sago007 2012-04-17 11:04 92
c53e6443 sago007 2012-04-17 11:04 93
	if(!data)
c53e6443 sago007 2012-04-17 11:04 94
	{
c53e6443 sago007 2012-04-17 11:04 95
		CppSdlException e(IMAGE,CPPSDL_ERROR_DATA,"Could not convert raw data to image");
c53e6443 sago007 2012-04-17 11:04 96
		throw e;
c53e6443 sago007 2012-04-17 11:04 97
	}
c53e6443 sago007 2012-04-17 11:04 98
c53e6443 sago007 2012-04-17 11:04 99
	SDL_GetClipRect(data,&area);
c53e6443 sago007 2012-04-17 11:04 100
	OptimizeForBlit();
c53e6443 sago007 2012-04-17 11:04 101
	counter = new int;
c53e6443 sago007 2012-04-17 11:04 102
	*counter = 1;
c53e6443 sago007 2012-04-17 11:04 103
}
c53e6443 sago007 2012-04-17 11:04 104
c53e6443 sago007 2012-04-17 11:04 105
CppSdlImageHolder::~CppSdlImageHolder()
c53e6443 sago007 2012-04-17 11:04 106
{
c53e6443 sago007 2012-04-17 11:04 107
	if(!counter)
c53e6443 sago007 2012-04-17 11:04 108
		return; //There are no counter, so already freed
c53e6443 sago007 2012-04-17 11:04 109
	MakeNull();
c53e6443 sago007 2012-04-17 11:04 110
	if(*counter == 0)
c53e6443 sago007 2012-04-17 11:04 111
	{
c53e6443 sago007 2012-04-17 11:04 112
		delete counter;
c53e6443 sago007 2012-04-17 11:04 113
		counter = NULL;
c53e6443 sago007 2012-04-17 11:04 114
	}
3db6ed5d sago007 2010-12-07 17:19 115
}
3db6ed5d sago007 2010-12-07 17:19 116
3db6ed5d sago007 2010-12-07 17:19 117
SDL_Surface* CppSdlImageHolder::GetRawDataInsecure()
3db6ed5d sago007 2010-12-07 17:19 118
{
c53e6443 sago007 2012-04-17 11:04 119
	Initialized();
c53e6443 sago007 2012-04-17 11:04 120
	return data;
3db6ed5d sago007 2010-12-07 17:19 121
}
3db6ed5d sago007 2010-12-07 17:19 122
3db6ed5d sago007 2010-12-07 17:19 123
Uint32 CppSdlImageHolder::GetWidth()
3db6ed5d sago007 2010-12-07 17:19 124
{
c53e6443 sago007 2012-04-17 11:04 125
	if(IsNull())
c53e6443 sago007 2012-04-17 11:04 126
		return 0;
c53e6443 sago007 2012-04-17 11:04 127
	return area.w;
3db6ed5d sago007 2010-12-07 17:19 128
}
3db6ed5d sago007 2010-12-07 17:19 129
3db6ed5d sago007 2010-12-07 17:19 130
Uint32 CppSdlImageHolder::GetHeight()
3db6ed5d sago007 2010-12-07 17:19 131
{
c53e6443 sago007 2012-04-17 11:04 132
	if(IsNull())
c53e6443 sago007 2012-04-17 11:04 133
		return 0;
c53e6443 sago007 2012-04-17 11:04 134
	return area.h;
3db6ed5d sago007 2010-12-07 17:19 135
}
3db6ed5d sago007 2010-12-07 17:19 136
3db6ed5d sago007 2010-12-07 17:19 137
void CppSdlImageHolder::Cut(Uint32 x, Uint32 y, Sint32 w = -1, Sint32 h = -1)
3db6ed5d sago007 2010-12-07 17:19 138
{
c53e6443 sago007 2012-04-17 11:04 139
	Initialized();
c53e6443 sago007 2012-04-17 11:04 140
	if(w<0)
c53e6443 sago007 2012-04-17 11:04 141
	{
c53e6443 sago007 2012-04-17 11:04 142
		w = GetWidth() - x;
c53e6443 sago007 2012-04-17 11:04 143
	}
c53e6443 sago007 2012-04-17 11:04 144
	if(h<0)
c53e6443 sago007 2012-04-17 11:04 145
	{
c53e6443 sago007 2012-04-17 11:04 146
		h = GetHeight() - y;
c53e6443 sago007 2012-04-17 11:04 147
	}
c53e6443 sago007 2012-04-17 11:04 148
	if(x+w>GetWidth())
c53e6443 sago007 2012-04-17 11:04 149
	{
c53e6443 sago007 2012-04-17 11:04 150
		//throw exception
c53e6443 sago007 2012-04-17 11:04 151
	}
c53e6443 sago007 2012-04-17 11:04 152
	if(y+h>GetHeight())
c53e6443 sago007 2012-04-17 11:04 153
	{
c53e6443 sago007 2012-04-17 11:04 154
		//throw exception
c53e6443 sago007 2012-04-17 11:04 155
	}
c53e6443 sago007 2012-04-17 11:04 156
	if(x<0 || x>GetWidth())
c53e6443 sago007 2012-04-17 11:04 157
	{
c53e6443 sago007 2012-04-17 11:04 158
		//throw exception
c53e6443 sago007 2012-04-17 11:04 159
	}
c53e6443 sago007 2012-04-17 11:04 160
	if(y<0 || y>GetHeight())
c53e6443 sago007 2012-04-17 11:04 161
	{
c53e6443 sago007 2012-04-17 11:04 162
		//throw exception
c53e6443 sago007 2012-04-17 11:04 163
	}
c53e6443 sago007 2012-04-17 11:04 164
	area.x += x;
c53e6443 sago007 2012-04-17 11:04 165
	area.y += y;
c53e6443 sago007 2012-04-17 11:04 166
	area.w = w;
c53e6443 sago007 2012-04-17 11:04 167
	area.h = h;
c53e6443 sago007 2012-04-17 11:04 168
}
c53e6443 sago007 2012-04-17 11:04 169
c53e6443 sago007 2012-04-17 11:04 170
void CppSdlImageHolder::PaintTo(SDL_Surface* target, int x, int y)
c53e6443 sago007 2012-04-17 11:04 171
{
c53e6443 sago007 2012-04-17 11:04 172
	static SDL_Rect dest; //static for reuse
c53e6443 sago007 2012-04-17 11:04 173
	if(IsNull())
c53e6443 sago007 2012-04-17 11:04 174
		return;
c53e6443 sago007 2012-04-17 11:04 175
	dest.x = x;
c53e6443 sago007 2012-04-17 11:04 176
	dest.y = y;
c53e6443 sago007 2012-04-17 11:04 177
	SDL_BlitSurface(data,&area, target,&dest);
c53e6443 sago007 2012-04-17 11:04 178
}
c53e6443 sago007 2012-04-17 11:04 179
c53e6443 sago007 2012-04-17 11:04 180
void CppSdlImageHolder::OptimizeForBlit(bool allowAlpha)
c53e6443 sago007 2012-04-17 11:04 181
{
c53e6443 sago007 2012-04-17 11:04 182
	static SDL_Surface *tmp;
c53e6443 sago007 2012-04-17 11:04 183
	Initialized();
c53e6443 sago007 2012-04-17 11:04 184
	if(allowAlpha)
c53e6443 sago007 2012-04-17 11:04 185
		tmp = SDL_DisplayFormatAlpha(data);
c53e6443 sago007 2012-04-17 11:04 186
	else
c53e6443 sago007 2012-04-17 11:04 187
		tmp = SDL_DisplayFormat(data);
c53e6443 sago007 2012-04-17 11:04 188
	SDL_FreeSurface(data);
c53e6443 sago007 2012-04-17 11:04 189
	data = tmp;
c53e6443 sago007 2012-04-17 11:04 190
}
c53e6443 sago007 2012-04-17 11:04 191
c53e6443 sago007 2012-04-17 11:04 192
void CppSdlImageHolder::Initialized()
c53e6443 sago007 2012-04-17 11:04 193
{
c53e6443 sago007 2012-04-17 11:04 194
	if(data == NULL)
c53e6443 sago007 2012-04-17 11:04 195
		throw(CppSdlException(IMAGE,CPPSDL_ERROR_NULLPOINTER,"ImageHolder used uninitialized!"));
c53e6443 sago007 2012-04-17 11:04 196
}
c53e6443 sago007 2012-04-17 11:04 197
c53e6443 sago007 2012-04-17 11:04 198
bool CppSdlImageHolder::IsNull()
c53e6443 sago007 2012-04-17 11:04 199
{
c53e6443 sago007 2012-04-17 11:04 200
	if(data == NULL || counter == NULL)
c53e6443 sago007 2012-04-17 11:04 201
		return true;
c53e6443 sago007 2012-04-17 11:04 202
	else
c53e6443 sago007 2012-04-17 11:04 203
		return false;
c53e6443 sago007 2012-04-17 11:04 204
}
c53e6443 sago007 2012-04-17 11:04 205
c53e6443 sago007 2012-04-17 11:04 206
void CppSdlImageHolder::MakeNull()
c53e6443 sago007 2012-04-17 11:04 207
{
c53e6443 sago007 2012-04-17 11:04 208
	if(IsNull())
c53e6443 sago007 2012-04-17 11:04 209
		return;
c53e6443 sago007 2012-04-17 11:04 210
	(*counter)--;
c53e6443 sago007 2012-04-17 11:04 211
	if(*counter == 0 && data != NULL)
c53e6443 sago007 2012-04-17 11:04 212
	{
c53e6443 sago007 2012-04-17 11:04 213
		SDL_FreeSurface(data);
c53e6443 sago007 2012-04-17 11:04 214
		data = NULL;
c53e6443 sago007 2012-04-17 11:04 215
	}
8d488d32 sago007 2011-04-17 13:02 216
}
8d488d32 sago007 2011-04-17 13:02 217
8d488d32 sago007 2011-04-17 13:02 218
}
1970-01-01 00:00 219