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
84b6e374 sago007 2020-09-15 18:13 67
void SagoSprite::DrawRotated(SDL_Renderer* target, Sint32 frameTime, int x, int y, const double angleRadian) const {
84b6e374 sago007 2020-09-15 18:13 68
	SDL_Point center = {this->data->origin.x, this->data->origin.y};
84b6e374 sago007 2020-09-15 18:13 69
	DrawScaledAndRotated(target, frameTime, x, y, data->imgCord.w, data->imgCord.h, angleRadian, &center, SDL_FLIP_NONE);
84b6e374 sago007 2020-09-15 18:13 70
}
84b6e374 sago007 2020-09-15 18:13 71
68313e95 sago007 2016-09-28 16:52 72
void SagoSprite::DrawScaled(SDL_Renderer* target, Sint32 frameTime, int x, int y, int w, int h) const {
84b6e374 sago007 2020-09-15 18:13 73
	DrawScaledAndRotated(target, frameTime, x, y, w, h, 0.0, nullptr, SDL_FLIP_NONE);
84b6e374 sago007 2020-09-15 18:13 74
}
84b6e374 sago007 2020-09-15 18:13 75
84b6e374 sago007 2020-09-15 18:13 76
void SagoSprite::DrawScaledAndRotated(SDL_Renderer* target, Sint32 frameTime, int x, int y, int w, int h, const double angleRadian, const SDL_Point* center, const SDL_RendererFlip flip) const {
1de94c36 sago007 2017-04-20 19:38 77
	if (!data->tex.get()) {
161a010c sago007 2016-05-06 14:00 78
		std::cerr << "Texture is null!\n";
9d259921 sago007 2015-12-23 18:46 79
	}
82da8a1c sago007 2015-12-19 18:00 80
	SDL_Rect rect = data->imgCord;
82da8a1c sago007 2015-12-19 18:00 81
	rect.x+=rect.w*((frameTime/data->aniFrameTime)%data->aniFrames);
82da8a1c sago007 2015-12-19 18:00 82
	SDL_Rect pos = rect;
55ea55dd sago007 2018-03-24 16:07 83
	pos.x = x - this->data->origin.x;
55ea55dd sago007 2018-03-24 16:07 84
	pos.y = y - this->data->origin.y;
68313e95 sago007 2016-09-28 16:52 85
	if (w > 0) {
68313e95 sago007 2016-09-28 16:52 86
		pos.w = w;
68313e95 sago007 2016-09-28 16:52 87
	}
68313e95 sago007 2016-09-28 16:52 88
	if (h > 0) {
68313e95 sago007 2016-09-28 16:52 89
		pos.h = h;
68313e95 sago007 2016-09-28 16:52 90
	}
84b6e374 sago007 2020-09-15 18:13 91
	double angleDegress = angleRadian/M_PI*180.0;
84b6e374 sago007 2020-09-15 18:13 92
	SDL_RenderCopyEx(target, data->tex.get(), &rect, &pos, angleDegress, center, flip);
82da8a1c sago007 2015-12-19 18:00 93
}
82da8a1c sago007 2015-12-19 18:00 94
c6bde3e3 sago007 2015-12-22 16:56 95
void SagoSprite::Draw(SDL_Renderer* target, Sint32 frameTime, int x, int y, const SDL_Rect& part) const {
82da8a1c sago007 2015-12-19 18:00 96
	SDL_Rect rect = data->imgCord;
82da8a1c sago007 2015-12-19 18:00 97
	rect.x+=rect.w*((frameTime/data->aniFrameTime)%data->aniFrames);
82da8a1c sago007 2015-12-19 18:00 98
	rect.x += part.x;
82da8a1c sago007 2015-12-19 18:00 99
	rect.y += part.y;
82da8a1c sago007 2015-12-19 18:00 100
	rect.w = part.w;
82da8a1c sago007 2015-12-19 18:00 101
	rect.h = part.h;
82da8a1c sago007 2015-12-19 18:00 102
	SDL_Rect pos = rect;
55ea55dd sago007 2018-03-24 16:07 103
	pos.x = x - this->data->origin.x;
55ea55dd sago007 2018-03-24 16:07 104
	pos.y = y - this->data->origin.y;
1de94c36 sago007 2017-04-20 19:38 105
	SDL_RenderCopy(target, data->tex.get(), &rect, &pos);
82da8a1c sago007 2015-12-19 18:00 106
}
82da8a1c sago007 2015-12-19 18:00 107
55ea55dd sago007 2018-03-24 16:07 108
void SagoSprite::DrawProgressive(SDL_Renderer* target, float progress, int x, int y) const {
55ea55dd sago007 2018-03-24 16:07 109
	Sint32 frameNumber = progress*data->aniFrames;
55ea55dd sago007 2018-03-24 16:07 110
	Sint32 frameTime = frameNumber*data->aniFrameTime;
55ea55dd sago007 2018-03-24 16:07 111
	Draw(target, frameTime, x, y);
55ea55dd sago007 2018-03-24 16:07 112
}
55ea55dd sago007 2018-03-24 16:07 113
c6bde3e3 sago007 2015-12-22 16:56 114
void SagoSprite::DrawBounded(SDL_Renderer* target, Sint32 frameTime, int x, int y, const SDL_Rect& bounds) const {
c6bde3e3 sago007 2015-12-22 16:56 115
	SDL_Rect rect = data->imgCord;
c6bde3e3 sago007 2015-12-22 16:56 116
	rect.x+=rect.w*((frameTime/data->aniFrameTime)%data->aniFrames);
c6bde3e3 sago007 2015-12-22 16:56 117
	SDL_Rect pos = rect;
c6bde3e3 sago007 2015-12-22 16:56 118
	pos.x = x;
c6bde3e3 sago007 2015-12-22 16:56 119
	pos.y = y;
c6bde3e3 sago007 2015-12-22 16:56 120
	if (pos.x > bounds.x+bounds.w) {
c6bde3e3 sago007 2015-12-22 16:56 121
		return;
c6bde3e3 sago007 2015-12-22 16:56 122
	}
c6bde3e3 sago007 2015-12-22 16:56 123
	if (pos.y > bounds.y+bounds.h) {
c6bde3e3 sago007 2015-12-22 16:56 124
		return;
c6bde3e3 sago007 2015-12-22 16:56 125
	}
c6bde3e3 sago007 2015-12-22 16:56 126
	if (pos.x+pos.w < bounds.x) {
c6bde3e3 sago007 2015-12-22 16:56 127
		return;
c6bde3e3 sago007 2015-12-22 16:56 128
	}
c6bde3e3 sago007 2015-12-22 16:56 129
	if (pos.y+pos.h < bounds.y) {
c6bde3e3 sago007 2015-12-22 16:56 130
		return;
c6bde3e3 sago007 2015-12-22 16:56 131
	}
c6bde3e3 sago007 2015-12-22 16:56 132
	if (pos.x < bounds.x) {
c6bde3e3 sago007 2015-12-22 16:56 133
		Sint16 absDiff = bounds.x-pos.x;
c6bde3e3 sago007 2015-12-22 16:56 134
		pos.x+=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 135
		rect.x+=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 136
		pos.w-=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 137
		rect.w-=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 138
	}
c6bde3e3 sago007 2015-12-22 16:56 139
	if (pos.y < bounds.y) {
c6bde3e3 sago007 2015-12-22 16:56 140
		Sint16 absDiff = bounds.y-pos.y;
c6bde3e3 sago007 2015-12-22 16:56 141
		pos.y+=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 142
		rect.y+=absDiff;
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
	if (pos.x+pos.w > bounds.x+bounds.w) {
c6bde3e3 sago007 2015-12-22 16:56 147
		Sint16 absDiff = pos.x+pos.w-(bounds.x+bounds.w);
c6bde3e3 sago007 2015-12-22 16:56 148
		pos.w -= absDiff;
c6bde3e3 sago007 2015-12-22 16:56 149
		rect.w -= absDiff;
c6bde3e3 sago007 2015-12-22 16:56 150
	}
c6bde3e3 sago007 2015-12-22 16:56 151
	if (pos.y+pos.h > bounds.y+bounds.h) {
c6bde3e3 sago007 2015-12-22 16:56 152
		Sint16 absDiff = pos.y+pos.h-(bounds.y+bounds.h);
c6bde3e3 sago007 2015-12-22 16:56 153
		pos.h -= absDiff;
c6bde3e3 sago007 2015-12-22 16:56 154
		rect.h -= absDiff;
c6bde3e3 sago007 2015-12-22 16:56 155
	}
c6bde3e3 sago007 2015-12-22 16:56 156
1de94c36 sago007 2017-04-20 19:38 157
	SDL_RenderCopy(target, data->tex.get(), &rect, &pos);
c6bde3e3 sago007 2015-12-22 16:56 158
}
c6bde3e3 sago007 2015-12-22 16:56 159
82da8a1c sago007 2015-12-19 18:00 160
void SagoSprite::SetOrigin(const SDL_Rect& newOrigin) {
82da8a1c sago007 2015-12-19 18:00 161
	data->origin = newOrigin;
82da8a1c sago007 2015-12-19 18:00 162
}
82da8a1c sago007 2015-12-19 18:00 163
1d2e2129 sago007 2015-12-23 15:41 164
int SagoSprite::GetWidth() const {
1d2e2129 sago007 2015-12-23 15:41 165
	return data->imgCord.w;
1d2e2129 sago007 2015-12-23 15:41 166
}
1d2e2129 sago007 2015-12-23 15:41 167
int SagoSprite::GetHeight() const {
1d2e2129 sago007 2015-12-23 15:41 168
	return data->imgCord.h;
1d2e2129 sago007 2015-12-23 15:41 169
}
1d2e2129 sago007 2015-12-23 15:41 170
82da8a1c sago007 2015-12-19 18:00 171
}  //namespace sago