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
50c81b4b sago007 2020-09-19 15:21 28
#ifndef M_PI
50c81b4b sago007 2020-09-19 15:21 29
// M_PI is a custom extension that most C++ toolchains provide. Out Windows compiler does not provide it.
50c81b4b sago007 2020-09-19 15:21 30
# define M_PI 3.14159265358979323846
50c81b4b sago007 2020-09-19 15:21 31
#endif
50c81b4b sago007 2020-09-19 15:21 32
82da8a1c sago007 2015-12-19 18:00 33
namespace sago {
82da8a1c sago007 2015-12-19 18:00 34
82da8a1c sago007 2015-12-19 18:00 35
struct SagoSprite::SagoSpriteData {
1de94c36 sago007 2017-04-20 19:38 36
	TextureHandler tex;
55ea55dd sago007 2018-03-24 16:07 37
	SDL_Rect imgCord = {};
55ea55dd sago007 2018-03-24 16:07 38
	SDL_Rect origin = {};
82da8a1c sago007 2015-12-19 18:00 39
	int aniFrames = 0;
82da8a1c sago007 2015-12-19 18:00 40
	int aniFrameTime = 0;
82da8a1c sago007 2015-12-19 18:00 41
};
82da8a1c sago007 2015-12-19 18:00 42
c6bde3e3 sago007 2015-12-22 16:56 43
SagoSprite::SagoSprite() {
c6bde3e3 sago007 2015-12-22 16:56 44
	data = new SagoSpriteData();
c6bde3e3 sago007 2015-12-22 16:56 45
}
c6bde3e3 sago007 2015-12-22 16:56 46
82da8a1c sago007 2015-12-19 18:00 47
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 48
	data = new SagoSpriteData();
6082686b sago007 2017-04-22 08:39 49
	data->tex = texHolder.getTextureHandler(texture);
82da8a1c sago007 2015-12-19 18:00 50
	data->imgCord = initImage;
82da8a1c sago007 2015-12-19 18:00 51
	data->aniFrames = animationFrames;
82da8a1c sago007 2015-12-19 18:00 52
	data->aniFrameTime = animationFrameLength;
82da8a1c sago007 2015-12-19 18:00 53
}
82da8a1c sago007 2015-12-19 18:00 54
1d2e2129 sago007 2015-12-23 15:41 55
SagoSprite::SagoSprite(const SagoSprite& base) : data(new SagoSpriteData(*base.data)) {
53203200 sago007 2015-12-25 13:30 56
c6bde3e3 sago007 2015-12-22 16:56 57
}
82da8a1c sago007 2015-12-19 18:00 58
1d2e2129 sago007 2015-12-23 15:41 59
SagoSprite& SagoSprite::operator=(const SagoSprite& base) {
1d2e2129 sago007 2015-12-23 15:41 60
	*data = *base.data;
53203200 sago007 2015-12-25 13:30 61
	return *this;
c6bde3e3 sago007 2015-12-22 16:56 62
}
82da8a1c sago007 2015-12-19 18:00 63
82da8a1c sago007 2015-12-19 18:00 64
SagoSprite::~SagoSprite() {
82da8a1c sago007 2015-12-19 18:00 65
	delete data;
82da8a1c sago007 2015-12-19 18:00 66
}
82da8a1c sago007 2015-12-19 18:00 67
c6bde3e3 sago007 2015-12-22 16:56 68
void SagoSprite::Draw(SDL_Renderer* target, Sint32 frameTime, int x, int y) const {
68313e95 sago007 2016-09-28 16:52 69
	DrawScaled(target, frameTime, x, y, data->imgCord.w, data->imgCord.h);
68313e95 sago007 2016-09-28 16:52 70
}
68313e95 sago007 2016-09-28 16:52 71
84b6e374 sago007 2020-09-15 18:13 72
void SagoSprite::DrawRotated(SDL_Renderer* target, Sint32 frameTime, int x, int y, const double angleRadian) const {
84b6e374 sago007 2020-09-15 18:13 73
	SDL_Point center = {this->data->origin.x, this->data->origin.y};
84b6e374 sago007 2020-09-15 18:13 74
	DrawScaledAndRotated(target, frameTime, x, y, data->imgCord.w, data->imgCord.h, angleRadian, &center, SDL_FLIP_NONE);
84b6e374 sago007 2020-09-15 18:13 75
}
84b6e374 sago007 2020-09-15 18:13 76
68313e95 sago007 2016-09-28 16:52 77
void SagoSprite::DrawScaled(SDL_Renderer* target, Sint32 frameTime, int x, int y, int w, int h) const {
84b6e374 sago007 2020-09-15 18:13 78
	DrawScaledAndRotated(target, frameTime, x, y, w, h, 0.0, nullptr, SDL_FLIP_NONE);
84b6e374 sago007 2020-09-15 18:13 79
}
84b6e374 sago007 2020-09-15 18:13 80
84b6e374 sago007 2020-09-15 18:13 81
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 82
	if (!data->tex.get()) {
161a010c sago007 2016-05-06 14:00 83
		std::cerr << "Texture is null!\n";
9d259921 sago007 2015-12-23 18:46 84
	}
82da8a1c sago007 2015-12-19 18:00 85
	SDL_Rect rect = data->imgCord;
82da8a1c sago007 2015-12-19 18:00 86
	rect.x+=rect.w*((frameTime/data->aniFrameTime)%data->aniFrames);
82da8a1c sago007 2015-12-19 18:00 87
	SDL_Rect pos = rect;
55ea55dd sago007 2018-03-24 16:07 88
	pos.x = x - this->data->origin.x;
55ea55dd sago007 2018-03-24 16:07 89
	pos.y = y - this->data->origin.y;
68313e95 sago007 2016-09-28 16:52 90
	if (w > 0) {
68313e95 sago007 2016-09-28 16:52 91
		pos.w = w;
68313e95 sago007 2016-09-28 16:52 92
	}
68313e95 sago007 2016-09-28 16:52 93
	if (h > 0) {
68313e95 sago007 2016-09-28 16:52 94
		pos.h = h;
68313e95 sago007 2016-09-28 16:52 95
	}
84b6e374 sago007 2020-09-15 18:13 96
	double angleDegress = angleRadian/M_PI*180.0;
84b6e374 sago007 2020-09-15 18:13 97
	SDL_RenderCopyEx(target, data->tex.get(), &rect, &pos, angleDegress, center, flip);
82da8a1c sago007 2015-12-19 18:00 98
}
82da8a1c sago007 2015-12-19 18:00 99
c6bde3e3 sago007 2015-12-22 16:56 100
void SagoSprite::Draw(SDL_Renderer* target, Sint32 frameTime, int x, int y, const SDL_Rect& part) const {
82da8a1c sago007 2015-12-19 18:00 101
	SDL_Rect rect = data->imgCord;
82da8a1c sago007 2015-12-19 18:00 102
	rect.x+=rect.w*((frameTime/data->aniFrameTime)%data->aniFrames);
82da8a1c sago007 2015-12-19 18:00 103
	rect.x += part.x;
82da8a1c sago007 2015-12-19 18:00 104
	rect.y += part.y;
82da8a1c sago007 2015-12-19 18:00 105
	rect.w = part.w;
82da8a1c sago007 2015-12-19 18:00 106
	rect.h = part.h;
82da8a1c sago007 2015-12-19 18:00 107
	SDL_Rect pos = rect;
55ea55dd sago007 2018-03-24 16:07 108
	pos.x = x - this->data->origin.x;
55ea55dd sago007 2018-03-24 16:07 109
	pos.y = y - this->data->origin.y;
1de94c36 sago007 2017-04-20 19:38 110
	SDL_RenderCopy(target, data->tex.get(), &rect, &pos);
82da8a1c sago007 2015-12-19 18:00 111
}
82da8a1c sago007 2015-12-19 18:00 112
55ea55dd sago007 2018-03-24 16:07 113
void SagoSprite::DrawProgressive(SDL_Renderer* target, float progress, int x, int y) const {
55ea55dd sago007 2018-03-24 16:07 114
	Sint32 frameNumber = progress*data->aniFrames;
55ea55dd sago007 2018-03-24 16:07 115
	Sint32 frameTime = frameNumber*data->aniFrameTime;
55ea55dd sago007 2018-03-24 16:07 116
	Draw(target, frameTime, x, y);
55ea55dd sago007 2018-03-24 16:07 117
}
55ea55dd sago007 2018-03-24 16:07 118
c6bde3e3 sago007 2015-12-22 16:56 119
void SagoSprite::DrawBounded(SDL_Renderer* target, Sint32 frameTime, int x, int y, const SDL_Rect& bounds) const {
c6bde3e3 sago007 2015-12-22 16:56 120
	SDL_Rect rect = data->imgCord;
c6bde3e3 sago007 2015-12-22 16:56 121
	rect.x+=rect.w*((frameTime/data->aniFrameTime)%data->aniFrames);
c6bde3e3 sago007 2015-12-22 16:56 122
	SDL_Rect pos = rect;
c6bde3e3 sago007 2015-12-22 16:56 123
	pos.x = x;
c6bde3e3 sago007 2015-12-22 16:56 124
	pos.y = y;
c6bde3e3 sago007 2015-12-22 16:56 125
	if (pos.x > bounds.x+bounds.w) {
c6bde3e3 sago007 2015-12-22 16:56 126
		return;
c6bde3e3 sago007 2015-12-22 16:56 127
	}
c6bde3e3 sago007 2015-12-22 16:56 128
	if (pos.y > bounds.y+bounds.h) {
c6bde3e3 sago007 2015-12-22 16:56 129
		return;
c6bde3e3 sago007 2015-12-22 16:56 130
	}
c6bde3e3 sago007 2015-12-22 16:56 131
	if (pos.x+pos.w < bounds.x) {
c6bde3e3 sago007 2015-12-22 16:56 132
		return;
c6bde3e3 sago007 2015-12-22 16:56 133
	}
c6bde3e3 sago007 2015-12-22 16:56 134
	if (pos.y+pos.h < bounds.y) {
c6bde3e3 sago007 2015-12-22 16:56 135
		return;
c6bde3e3 sago007 2015-12-22 16:56 136
	}
c6bde3e3 sago007 2015-12-22 16:56 137
	if (pos.x < bounds.x) {
c6bde3e3 sago007 2015-12-22 16:56 138
		Sint16 absDiff = bounds.x-pos.x;
c6bde3e3 sago007 2015-12-22 16:56 139
		pos.x+=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 140
		rect.x+=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 141
		pos.w-=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 142
		rect.w-=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 143
	}
c6bde3e3 sago007 2015-12-22 16:56 144
	if (pos.y < bounds.y) {
c6bde3e3 sago007 2015-12-22 16:56 145
		Sint16 absDiff = bounds.y-pos.y;
c6bde3e3 sago007 2015-12-22 16:56 146
		pos.y+=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 147
		rect.y+=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 148
		pos.h-=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 149
		rect.h-=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 150
	}
c6bde3e3 sago007 2015-12-22 16:56 151
	if (pos.x+pos.w > bounds.x+bounds.w) {
c6bde3e3 sago007 2015-12-22 16:56 152
		Sint16 absDiff = pos.x+pos.w-(bounds.x+bounds.w);
c6bde3e3 sago007 2015-12-22 16:56 153
		pos.w -= absDiff;
c6bde3e3 sago007 2015-12-22 16:56 154
		rect.w -= absDiff;
c6bde3e3 sago007 2015-12-22 16:56 155
	}
c6bde3e3 sago007 2015-12-22 16:56 156
	if (pos.y+pos.h > bounds.y+bounds.h) {
c6bde3e3 sago007 2015-12-22 16:56 157
		Sint16 absDiff = pos.y+pos.h-(bounds.y+bounds.h);
c6bde3e3 sago007 2015-12-22 16:56 158
		pos.h -= absDiff;
c6bde3e3 sago007 2015-12-22 16:56 159
		rect.h -= absDiff;
c6bde3e3 sago007 2015-12-22 16:56 160
	}
c6bde3e3 sago007 2015-12-22 16:56 161
1de94c36 sago007 2017-04-20 19:38 162
	SDL_RenderCopy(target, data->tex.get(), &rect, &pos);
c6bde3e3 sago007 2015-12-22 16:56 163
}
c6bde3e3 sago007 2015-12-22 16:56 164
82da8a1c sago007 2015-12-19 18:00 165
void SagoSprite::SetOrigin(const SDL_Rect& newOrigin) {
82da8a1c sago007 2015-12-19 18:00 166
	data->origin = newOrigin;
82da8a1c sago007 2015-12-19 18:00 167
}
82da8a1c sago007 2015-12-19 18:00 168
1d2e2129 sago007 2015-12-23 15:41 169
int SagoSprite::GetWidth() const {
1d2e2129 sago007 2015-12-23 15:41 170
	return data->imgCord.w;
1d2e2129 sago007 2015-12-23 15:41 171
}
1d2e2129 sago007 2015-12-23 15:41 172
int SagoSprite::GetHeight() const {
1d2e2129 sago007 2015-12-23 15:41 173
	return data->imgCord.h;
1d2e2129 sago007 2015-12-23 15:41 174
}
1d2e2129 sago007 2015-12-23 15:41 175
50c81b4b sago007 2020-09-19 15:21 176
}  //namespace sago
1970-01-01 00:00 177