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
ecef5838 sago007 2015-11-24 20:01 28
namespace CppSdl {
c53e6443 sago007 2012-04-17 11:04 29
ecef5838 sago007 2015-11-24 20:01 30
CppSdlImageHolder::CppSdlImageHolder() {
acd79baf sago007 2015-11-22 19:37 31
	data = nullptr;
c53e6443 sago007 2012-04-17 11:04 32
}
c53e6443 sago007 2012-04-17 11:04 33
ecef5838 sago007 2015-11-24 20:01 34
CppSdlImageHolder::CppSdlImageHolder(std::string filename) {
c53e6443 sago007 2012-04-17 11:04 35
	data = IMG_Load(filename.c_str());
ecef5838 sago007 2015-11-24 20:01 36
	if (!data) {
c53e6443 sago007 2012-04-17 11:04 37
		//Here we should throw an exception
33f4e975 sago007 2015-11-14 21:49 38
		throw std::runtime_error(std::string("Could not read file \""+filename+"\""));
c53e6443 sago007 2012-04-17 11:04 39
	}
c53e6443 sago007 2012-04-17 11:04 40
	SDL_GetClipRect(data,&area);
c53e6443 sago007 2012-04-17 11:04 41
}
c53e6443 sago007 2012-04-17 11:04 42
ecef5838 sago007 2015-11-24 20:01 43
CppSdlImageHolder::CppSdlImageHolder(char* rawdata, int datasize) {
ecef5838 sago007 2015-11-24 20:01 44
	SDL_RWops* rw = SDL_RWFromMem (rawdata, datasize);
c53e6443 sago007 2012-04-17 11:04 45
c53e6443 sago007 2012-04-17 11:04 46
	//The above might fail and return null.
f3a1637d sago007 2015-11-14 21:32 47
	if (!rw) {
33f4e975 sago007 2015-11-14 21:49 48
		throw std::runtime_error(std::string("Could not read raw data"));
c53e6443 sago007 2012-04-17 11:04 49
	}
c53e6443 sago007 2012-04-17 11:04 50
c53e6443 sago007 2012-04-17 11:04 51
	data = IMG_Load_RW(rw,true); //the second argument tells the function to free RWops
c53e6443 sago007 2012-04-17 11:04 52
f3a1637d sago007 2015-11-14 21:32 53
	if (!data) {
33f4e975 sago007 2015-11-14 21:49 54
		throw std::runtime_error("Could not convert raw data to image");
c53e6443 sago007 2012-04-17 11:04 55
	}
c53e6443 sago007 2012-04-17 11:04 56
c53e6443 sago007 2012-04-17 11:04 57
	SDL_GetClipRect(data,&area);
c53e6443 sago007 2012-04-17 11:04 58
	OptimizeForBlit();
c53e6443 sago007 2012-04-17 11:04 59
}
c53e6443 sago007 2012-04-17 11:04 60
ecef5838 sago007 2015-11-24 20:01 61
CppSdlImageHolder::~CppSdlImageHolder() {
c53e6443 sago007 2012-04-17 11:04 62
	MakeNull();
3db6ed5d sago007 2010-12-07 17:19 63
}
3db6ed5d sago007 2010-12-07 17:19 64
ecef5838 sago007 2015-11-24 20:01 65
SDL_Surface* CppSdlImageHolder::GetRawDataInsecure() {
c53e6443 sago007 2012-04-17 11:04 66
	Initialized();
c53e6443 sago007 2012-04-17 11:04 67
	return data;
3db6ed5d sago007 2010-12-07 17:19 68
}
3db6ed5d sago007 2010-12-07 17:19 69
ecef5838 sago007 2015-11-24 20:01 70
Uint32 CppSdlImageHolder::GetWidth() {
acd79baf sago007 2015-11-22 19:37 71
	if (IsNull()) {
c53e6443 sago007 2012-04-17 11:04 72
		return 0;
acd79baf sago007 2015-11-22 19:37 73
	}
c53e6443 sago007 2012-04-17 11:04 74
	return area.w;
3db6ed5d sago007 2010-12-07 17:19 75
}
3db6ed5d sago007 2010-12-07 17:19 76
ecef5838 sago007 2015-11-24 20:01 77
Uint32 CppSdlImageHolder::GetHeight() {
ecef5838 sago007 2015-11-24 20:01 78
	if (IsNull()) {
c53e6443 sago007 2012-04-17 11:04 79
		return 0;
acd79baf sago007 2015-11-22 19:37 80
	}
c53e6443 sago007 2012-04-17 11:04 81
	return area.h;
3db6ed5d sago007 2010-12-07 17:19 82
}
3db6ed5d sago007 2010-12-07 17:19 83
ecef5838 sago007 2015-11-24 20:01 84
void CppSdlImageHolder::PaintTo(SDL_Surface* target, int x, int y) {
c53e6443 sago007 2012-04-17 11:04 85
	static SDL_Rect dest; //static for reuse
ecef5838 sago007 2015-11-24 20:01 86
	if (IsNull()) {
c53e6443 sago007 2012-04-17 11:04 87
		return;
acd79baf sago007 2015-11-22 19:37 88
	}
c53e6443 sago007 2012-04-17 11:04 89
	dest.x = x;
c53e6443 sago007 2012-04-17 11:04 90
	dest.y = y;
571b4d47 sago007 2015-12-05 16:02 91
	SDL_BlitSurface(data, &area, target,&dest);
c53e6443 sago007 2012-04-17 11:04 92
}
c53e6443 sago007 2012-04-17 11:04 93
c53e6443 sago007 2012-04-17 11:04 94
ecef5838 sago007 2015-11-24 20:01 95
void CppSdlImageHolder::Initialized() {
ecef5838 sago007 2015-11-24 20:01 96
	if (data == nullptr) {
33f4e975 sago007 2015-11-14 21:49 97
		throw std::runtime_error("ImageHolder used uninitialized!");
33f4e975 sago007 2015-11-14 21:49 98
	}
c53e6443 sago007 2012-04-17 11:04 99
}
c53e6443 sago007 2012-04-17 11:04 100
ecef5838 sago007 2015-11-24 20:01 101
bool CppSdlImageHolder::IsNull() {
ecef5838 sago007 2015-11-24 20:01 102
	if (data == nullptr ) {
c53e6443 sago007 2012-04-17 11:04 103
		return true;
f3a1637d sago007 2015-11-14 21:32 104
	}
f3a1637d sago007 2015-11-14 21:32 105
	return false;
c53e6443 sago007 2012-04-17 11:04 106
}
c53e6443 sago007 2012-04-17 11:04 107
ecef5838 sago007 2015-11-24 20:01 108
void CppSdlImageHolder::MakeNull() {
ecef5838 sago007 2015-11-24 20:01 109
	if (IsNull()) {
c53e6443 sago007 2012-04-17 11:04 110
		return;
acd79baf sago007 2015-11-22 19:37 111
	}
f3a1637d sago007 2015-11-14 21:32 112
	SDL_FreeSurface(data);
acd79baf sago007 2015-11-22 19:37 113
	data = nullptr;
8d488d32 sago007 2011-04-17 13:02 114
}
8d488d32 sago007 2011-04-17 13:02 115
8d488d32 sago007 2011-04-17 13:02 116
}
1970-01-01 00:00 117