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"
33f4e975 sago007 2015-11-14 21:49 26
#include <stdexcept>
3db6ed5d sago007 2010-12-07 17:19 27
c53e6443 sago007 2012-04-17 11:04 28
namespace CppSdl
c53e6443 sago007 2012-04-17 11:04 29
{
c53e6443 sago007 2012-04-17 11:04 30
c53e6443 sago007 2012-04-17 11:04 31
CppSdlImageHolder::CppSdlImageHolder()
c53e6443 sago007 2012-04-17 11:04 32
{
c53e6443 sago007 2012-04-17 11:04 33
	data = NULL;
c53e6443 sago007 2012-04-17 11:04 34
}
c53e6443 sago007 2012-04-17 11:04 35
c53e6443 sago007 2012-04-17 11:04 36
CppSdlImageHolder::CppSdlImageHolder(std::string filename)
c53e6443 sago007 2012-04-17 11:04 37
{
c53e6443 sago007 2012-04-17 11:04 38
	data = IMG_Load(filename.c_str());
c53e6443 sago007 2012-04-17 11:04 39
	if(!data)
c53e6443 sago007 2012-04-17 11:04 40
	{
c53e6443 sago007 2012-04-17 11:04 41
		//Here we should throw an exception
33f4e975 sago007 2015-11-14 21:49 42
		throw std::runtime_error(std::string("Could not read file \""+filename+"\""));
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) {
33f4e975 sago007 2015-11-14 21:49 54
		throw std::runtime_error(std::string("Could not read raw data"));
c53e6443 sago007 2012-04-17 11:04 55
	}
c53e6443 sago007 2012-04-17 11:04 56
c53e6443 sago007 2012-04-17 11:04 57
	data = IMG_Load_RW(rw,true); //the second argument tells the function to free RWops
c53e6443 sago007 2012-04-17 11:04 58
f3a1637d sago007 2015-11-14 21:32 59
	if (!data) {
33f4e975 sago007 2015-11-14 21:49 60
		throw std::runtime_error("Could not convert raw data to image");
c53e6443 sago007 2012-04-17 11:04 61
	}
c53e6443 sago007 2012-04-17 11:04 62
c53e6443 sago007 2012-04-17 11:04 63
	SDL_GetClipRect(data,&area);
c53e6443 sago007 2012-04-17 11:04 64
	OptimizeForBlit();
c53e6443 sago007 2012-04-17 11:04 65
}
c53e6443 sago007 2012-04-17 11:04 66
c53e6443 sago007 2012-04-17 11:04 67
CppSdlImageHolder::~CppSdlImageHolder()
c53e6443 sago007 2012-04-17 11:04 68
{
c53e6443 sago007 2012-04-17 11:04 69
	MakeNull();
3db6ed5d sago007 2010-12-07 17:19 70
}
3db6ed5d sago007 2010-12-07 17:19 71
3db6ed5d sago007 2010-12-07 17:19 72
SDL_Surface* CppSdlImageHolder::GetRawDataInsecure()
3db6ed5d sago007 2010-12-07 17:19 73
{
c53e6443 sago007 2012-04-17 11:04 74
	Initialized();
c53e6443 sago007 2012-04-17 11:04 75
	return data;
3db6ed5d sago007 2010-12-07 17:19 76
}
3db6ed5d sago007 2010-12-07 17:19 77
3db6ed5d sago007 2010-12-07 17:19 78
Uint32 CppSdlImageHolder::GetWidth()
3db6ed5d sago007 2010-12-07 17:19 79
{
f3a1637d sago007 2015-11-14 21:32 80
	if (IsNull())
c53e6443 sago007 2012-04-17 11:04 81
		return 0;
c53e6443 sago007 2012-04-17 11:04 82
	return area.w;
3db6ed5d sago007 2010-12-07 17:19 83
}
3db6ed5d sago007 2010-12-07 17:19 84
3db6ed5d sago007 2010-12-07 17:19 85
Uint32 CppSdlImageHolder::GetHeight()
3db6ed5d sago007 2010-12-07 17:19 86
{
c53e6443 sago007 2012-04-17 11:04 87
	if(IsNull())
c53e6443 sago007 2012-04-17 11:04 88
		return 0;
c53e6443 sago007 2012-04-17 11:04 89
	return area.h;
3db6ed5d sago007 2010-12-07 17:19 90
}
3db6ed5d sago007 2010-12-07 17:19 91
c53e6443 sago007 2012-04-17 11:04 92
void CppSdlImageHolder::PaintTo(SDL_Surface* target, int x, int y)
c53e6443 sago007 2012-04-17 11:04 93
{
c53e6443 sago007 2012-04-17 11:04 94
	static SDL_Rect dest; //static for reuse
c53e6443 sago007 2012-04-17 11:04 95
	if(IsNull())
c53e6443 sago007 2012-04-17 11:04 96
		return;
c53e6443 sago007 2012-04-17 11:04 97
	dest.x = x;
c53e6443 sago007 2012-04-17 11:04 98
	dest.y = y;
c53e6443 sago007 2012-04-17 11:04 99
	SDL_BlitSurface(data,&area, target,&dest);
c53e6443 sago007 2012-04-17 11:04 100
}
c53e6443 sago007 2012-04-17 11:04 101
c53e6443 sago007 2012-04-17 11:04 102
void CppSdlImageHolder::OptimizeForBlit(bool allowAlpha)
c53e6443 sago007 2012-04-17 11:04 103
{
c53e6443 sago007 2012-04-17 11:04 104
	static SDL_Surface *tmp;
c53e6443 sago007 2012-04-17 11:04 105
	Initialized();
c53e6443 sago007 2012-04-17 11:04 106
	if(allowAlpha)
c53e6443 sago007 2012-04-17 11:04 107
		tmp = SDL_DisplayFormatAlpha(data);
c53e6443 sago007 2012-04-17 11:04 108
	else
c53e6443 sago007 2012-04-17 11:04 109
		tmp = SDL_DisplayFormat(data);
c53e6443 sago007 2012-04-17 11:04 110
	SDL_FreeSurface(data);
c53e6443 sago007 2012-04-17 11:04 111
	data = tmp;
c53e6443 sago007 2012-04-17 11:04 112
}
c53e6443 sago007 2012-04-17 11:04 113
c53e6443 sago007 2012-04-17 11:04 114
void CppSdlImageHolder::Initialized()
c53e6443 sago007 2012-04-17 11:04 115
{
33f4e975 sago007 2015-11-14 21:49 116
	if(data == NULL) {
33f4e975 sago007 2015-11-14 21:49 117
		throw std::runtime_error("ImageHolder used uninitialized!");
33f4e975 sago007 2015-11-14 21:49 118
	}
c53e6443 sago007 2012-04-17 11:04 119
}
c53e6443 sago007 2012-04-17 11:04 120
c53e6443 sago007 2012-04-17 11:04 121
bool CppSdlImageHolder::IsNull()
c53e6443 sago007 2012-04-17 11:04 122
{
f3a1637d sago007 2015-11-14 21:32 123
	if(data == NULL ) {
c53e6443 sago007 2012-04-17 11:04 124
		return true;
f3a1637d sago007 2015-11-14 21:32 125
	}
f3a1637d sago007 2015-11-14 21:32 126
	return false;
c53e6443 sago007 2012-04-17 11:04 127
}
c53e6443 sago007 2012-04-17 11:04 128
c53e6443 sago007 2012-04-17 11:04 129
void CppSdlImageHolder::MakeNull()
c53e6443 sago007 2012-04-17 11:04 130
{
c53e6443 sago007 2012-04-17 11:04 131
	if(IsNull())
c53e6443 sago007 2012-04-17 11:04 132
		return;
f3a1637d sago007 2015-11-14 21:32 133
	SDL_FreeSurface(data);
f3a1637d sago007 2015-11-14 21:32 134
	data = NULL;
8d488d32 sago007 2011-04-17 13:02 135
}
8d488d32 sago007 2011-04-17 13:02 136
8d488d32 sago007 2011-04-17 13:02 137
}
1970-01-01 00:00 138