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