git repos / blockattack-game

blame: source/code/sago/SagoSprite.cpp

normal view · raw

82da8a1c sago007 2015-12-19 18:00 1
/*
47fef8e8 sago007 2016-11-24 21:37 2
Copyright (c) 2015 Poul Sander
82da8a1c sago007 2015-12-19 18:00 3
47fef8e8 sago007 2016-11-24 21:37 4
Permission is hereby granted, free of charge, to any person
47fef8e8 sago007 2016-11-24 21:37 5
obtaining a copy of this software and associated documentation files
47fef8e8 sago007 2016-11-24 21:37 6
(the "Software"), to deal in the Software without restriction,
47fef8e8 sago007 2016-11-24 21:37 7
including without limitation the rights to use, copy, modify, merge,
47fef8e8 sago007 2016-11-24 21:37 8
publish, distribute, sublicense, and/or sell copies of the Software,
47fef8e8 sago007 2016-11-24 21:37 9
and to permit persons to whom the Software is furnished to do so,
47fef8e8 sago007 2016-11-24 21:37 10
subject to the following conditions:
82da8a1c sago007 2015-12-19 18:00 11
47fef8e8 sago007 2016-11-24 21:37 12
The above copyright notice and this permission notice shall be
47fef8e8 sago007 2016-11-24 21:37 13
included in all copies or substantial portions of the Software.
82da8a1c sago007 2015-12-19 18:00 14
47fef8e8 sago007 2016-11-24 21:37 15
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
47fef8e8 sago007 2016-11-24 21:37 16
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
47fef8e8 sago007 2016-11-24 21:37 17
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
47fef8e8 sago007 2016-11-24 21:37 18
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
47fef8e8 sago007 2016-11-24 21:37 19
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
47fef8e8 sago007 2016-11-24 21:37 20
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
47fef8e8 sago007 2016-11-24 21:37 21
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
47fef8e8 sago007 2016-11-24 21:37 22
SOFTWARE.
82da8a1c sago007 2015-12-19 18:00 23
*/
82da8a1c sago007 2015-12-19 18:00 24
82da8a1c sago007 2015-12-19 18:00 25
#include "SagoSprite.hpp"
9d259921 sago007 2015-12-23 18:46 26
#include <iostream>
82da8a1c sago007 2015-12-19 18:00 27
82da8a1c sago007 2015-12-19 18:00 28
namespace sago {
82da8a1c sago007 2015-12-19 18:00 29
82da8a1c sago007 2015-12-19 18:00 30
struct SagoSprite::SagoSpriteData {
1de94c36 sago007 2017-04-20 19:38 31
	TextureHandler tex;
55ea55dd sago007 2018-03-24 16:07 32
	SDL_Rect imgCord = {};
55ea55dd sago007 2018-03-24 16:07 33
	SDL_Rect origin = {};
82da8a1c sago007 2015-12-19 18:00 34
	int aniFrames = 0;
82da8a1c sago007 2015-12-19 18:00 35
	int aniFrameTime = 0;
82da8a1c sago007 2015-12-19 18:00 36
};
82da8a1c sago007 2015-12-19 18:00 37
c6bde3e3 sago007 2015-12-22 16:56 38
SagoSprite::SagoSprite() {
c6bde3e3 sago007 2015-12-22 16:56 39
	data = new SagoSpriteData();
c6bde3e3 sago007 2015-12-22 16:56 40
}
c6bde3e3 sago007 2015-12-22 16:56 41
82da8a1c sago007 2015-12-19 18:00 42
SagoSprite::SagoSprite(const SagoDataHolder& texHolder, const std::string& texture,const SDL_Rect& initImage,const int animationFrames, const int animationFrameLength) {
82da8a1c sago007 2015-12-19 18:00 43
	data = new SagoSpriteData();
6082686b sago007 2017-04-22 08:39 44
	data->tex = texHolder.getTextureHandler(texture);
82da8a1c sago007 2015-12-19 18:00 45
	data->imgCord = initImage;
82da8a1c sago007 2015-12-19 18:00 46
	data->aniFrames = animationFrames;
82da8a1c sago007 2015-12-19 18:00 47
	data->aniFrameTime = animationFrameLength;
82da8a1c sago007 2015-12-19 18:00 48
}
82da8a1c sago007 2015-12-19 18:00 49
1d2e2129 sago007 2015-12-23 15:41 50
SagoSprite::SagoSprite(const SagoSprite& base) : data(new SagoSpriteData(*base.data)) {
53203200 sago007 2015-12-25 13:30 51
c6bde3e3 sago007 2015-12-22 16:56 52
}
82da8a1c sago007 2015-12-19 18:00 53
1d2e2129 sago007 2015-12-23 15:41 54
SagoSprite& SagoSprite::operator=(const SagoSprite& base) {
1d2e2129 sago007 2015-12-23 15:41 55
	*data = *base.data;
53203200 sago007 2015-12-25 13:30 56
	return *this;
c6bde3e3 sago007 2015-12-22 16:56 57
}
82da8a1c sago007 2015-12-19 18:00 58
82da8a1c sago007 2015-12-19 18:00 59
SagoSprite::~SagoSprite() {
82da8a1c sago007 2015-12-19 18:00 60
	delete data;
82da8a1c sago007 2015-12-19 18:00 61
}
82da8a1c sago007 2015-12-19 18:00 62
c6bde3e3 sago007 2015-12-22 16:56 63
void SagoSprite::Draw(SDL_Renderer* target, Sint32 frameTime, int x, int y) const {
68313e95 sago007 2016-09-28 16:52 64
	DrawScaled(target, frameTime, x, y, data->imgCord.w, data->imgCord.h);
68313e95 sago007 2016-09-28 16:52 65
}
68313e95 sago007 2016-09-28 16:52 66
68313e95 sago007 2016-09-28 16:52 67
void SagoSprite::DrawScaled(SDL_Renderer* target, Sint32 frameTime, int x, int y, int w, int h) const {
1de94c36 sago007 2017-04-20 19:38 68
	if (!data->tex.get()) {
161a010c sago007 2016-05-06 14:00 69
		std::cerr << "Texture is null!\n";
9d259921 sago007 2015-12-23 18:46 70
	}
82da8a1c sago007 2015-12-19 18:00 71
	SDL_Rect rect = data->imgCord;
82da8a1c sago007 2015-12-19 18:00 72
	rect.x+=rect.w*((frameTime/data->aniFrameTime)%data->aniFrames);
82da8a1c sago007 2015-12-19 18:00 73
	SDL_Rect pos = rect;
55ea55dd sago007 2018-03-24 16:07 74
	pos.x = x - this->data->origin.x;
55ea55dd sago007 2018-03-24 16:07 75
	pos.y = y - this->data->origin.y;
68313e95 sago007 2016-09-28 16:52 76
	if (w > 0) {
68313e95 sago007 2016-09-28 16:52 77
		pos.w = w;
68313e95 sago007 2016-09-28 16:52 78
	}
68313e95 sago007 2016-09-28 16:52 79
	if (h > 0) {
68313e95 sago007 2016-09-28 16:52 80
		pos.h = h;
68313e95 sago007 2016-09-28 16:52 81
	}
1de94c36 sago007 2017-04-20 19:38 82
	SDL_RenderCopy(target, data->tex.get(), &rect, &pos);
82da8a1c sago007 2015-12-19 18:00 83
}
82da8a1c sago007 2015-12-19 18:00 84
c6bde3e3 sago007 2015-12-22 16:56 85
void SagoSprite::Draw(SDL_Renderer* target, Sint32 frameTime, int x, int y, const SDL_Rect& part) const {
82da8a1c sago007 2015-12-19 18:00 86
	SDL_Rect rect = data->imgCord;
82da8a1c sago007 2015-12-19 18:00 87
	rect.x+=rect.w*((frameTime/data->aniFrameTime)%data->aniFrames);
82da8a1c sago007 2015-12-19 18:00 88
	rect.x += part.x;
82da8a1c sago007 2015-12-19 18:00 89
	rect.y += part.y;
82da8a1c sago007 2015-12-19 18:00 90
	rect.w = part.w;
82da8a1c sago007 2015-12-19 18:00 91
	rect.h = part.h;
82da8a1c sago007 2015-12-19 18:00 92
	SDL_Rect pos = rect;
55ea55dd sago007 2018-03-24 16:07 93
	pos.x = x - this->data->origin.x;
55ea55dd sago007 2018-03-24 16:07 94
	pos.y = y - this->data->origin.y;
1de94c36 sago007 2017-04-20 19:38 95
	SDL_RenderCopy(target, data->tex.get(), &rect, &pos);
82da8a1c sago007 2015-12-19 18:00 96
}
82da8a1c sago007 2015-12-19 18:00 97
55ea55dd sago007 2018-03-24 16:07 98
void SagoSprite::DrawProgressive(SDL_Renderer* target, float progress, int x, int y) const {
55ea55dd sago007 2018-03-24 16:07 99
	Sint32 frameNumber = progress*data->aniFrames;
55ea55dd sago007 2018-03-24 16:07 100
	Sint32 frameTime = frameNumber*data->aniFrameTime;
55ea55dd sago007 2018-03-24 16:07 101
	Draw(target, frameTime, x, y);
55ea55dd sago007 2018-03-24 16:07 102
}
55ea55dd sago007 2018-03-24 16:07 103
c6bde3e3 sago007 2015-12-22 16:56 104
void SagoSprite::DrawBounded(SDL_Renderer* target, Sint32 frameTime, int x, int y, const SDL_Rect& bounds) const {
c6bde3e3 sago007 2015-12-22 16:56 105
	SDL_Rect rect = data->imgCord;
c6bde3e3 sago007 2015-12-22 16:56 106
	rect.x+=rect.w*((frameTime/data->aniFrameTime)%data->aniFrames);
c6bde3e3 sago007 2015-12-22 16:56 107
	SDL_Rect pos = rect;
c6bde3e3 sago007 2015-12-22 16:56 108
	pos.x = x;
c6bde3e3 sago007 2015-12-22 16:56 109
	pos.y = y;
c6bde3e3 sago007 2015-12-22 16:56 110
	if (pos.x > bounds.x+bounds.w) {
c6bde3e3 sago007 2015-12-22 16:56 111
		return;
c6bde3e3 sago007 2015-12-22 16:56 112
	}
c6bde3e3 sago007 2015-12-22 16:56 113
	if (pos.y > bounds.y+bounds.h) {
c6bde3e3 sago007 2015-12-22 16:56 114
		return;
c6bde3e3 sago007 2015-12-22 16:56 115
	}
c6bde3e3 sago007 2015-12-22 16:56 116
	if (pos.x+pos.w < bounds.x) {
c6bde3e3 sago007 2015-12-22 16:56 117
		return;
c6bde3e3 sago007 2015-12-22 16:56 118
	}
c6bde3e3 sago007 2015-12-22 16:56 119
	if (pos.y+pos.h < bounds.y) {
c6bde3e3 sago007 2015-12-22 16:56 120
		return;
c6bde3e3 sago007 2015-12-22 16:56 121
	}
c6bde3e3 sago007 2015-12-22 16:56 122
	if (pos.x < bounds.x) {
c6bde3e3 sago007 2015-12-22 16:56 123
		Sint16 absDiff = bounds.x-pos.x;
c6bde3e3 sago007 2015-12-22 16:56 124
		pos.x+=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 125
		rect.x+=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 126
		pos.w-=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 127
		rect.w-=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 128
	}
c6bde3e3 sago007 2015-12-22 16:56 129
	if (pos.y < bounds.y) {
c6bde3e3 sago007 2015-12-22 16:56 130
		Sint16 absDiff = bounds.y-pos.y;
c6bde3e3 sago007 2015-12-22 16:56 131
		pos.y+=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 132
		rect.y+=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 133
		pos.h-=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 134
		rect.h-=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 135
	}
c6bde3e3 sago007 2015-12-22 16:56 136
	if (pos.x+pos.w > bounds.x+bounds.w) {
c6bde3e3 sago007 2015-12-22 16:56 137
		Sint16 absDiff = pos.x+pos.w-(bounds.x+bounds.w);
c6bde3e3 sago007 2015-12-22 16:56 138
		pos.w -= absDiff;
c6bde3e3 sago007 2015-12-22 16:56 139
		rect.w -= absDiff;
c6bde3e3 sago007 2015-12-22 16:56 140
	}
c6bde3e3 sago007 2015-12-22 16:56 141
	if (pos.y+pos.h > bounds.y+bounds.h) {
c6bde3e3 sago007 2015-12-22 16:56 142
		Sint16 absDiff = pos.y+pos.h-(bounds.y+bounds.h);
c6bde3e3 sago007 2015-12-22 16:56 143
		pos.h -= absDiff;
c6bde3e3 sago007 2015-12-22 16:56 144
		rect.h -= absDiff;
c6bde3e3 sago007 2015-12-22 16:56 145
	}
c6bde3e3 sago007 2015-12-22 16:56 146
1de94c36 sago007 2017-04-20 19:38 147
	SDL_RenderCopy(target, data->tex.get(), &rect, &pos);
c6bde3e3 sago007 2015-12-22 16:56 148
}
c6bde3e3 sago007 2015-12-22 16:56 149
82da8a1c sago007 2015-12-19 18:00 150
void SagoSprite::SetOrigin(const SDL_Rect& newOrigin) {
82da8a1c sago007 2015-12-19 18:00 151
	data->origin = newOrigin;
82da8a1c sago007 2015-12-19 18:00 152
}
82da8a1c sago007 2015-12-19 18:00 153
1d2e2129 sago007 2015-12-23 15:41 154
int SagoSprite::GetWidth() const {
1d2e2129 sago007 2015-12-23 15:41 155
	return data->imgCord.w;
1d2e2129 sago007 2015-12-23 15:41 156
}
1d2e2129 sago007 2015-12-23 15:41 157
int SagoSprite::GetHeight() const {
1d2e2129 sago007 2015-12-23 15:41 158
	return data->imgCord.h;
1d2e2129 sago007 2015-12-23 15:41 159
}
1d2e2129 sago007 2015-12-23 15:41 160
82da8a1c sago007 2015-12-19 18:00 161
}  //namespace sago