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
62b87fe3 Poul Sander 2025-03-04 20:16 68
void SagoSprite::Draw(SDL_Renderer* target, Sint32 frameTime, int x, int y, SagoLogicalResize* resize) const {
62b87fe3 Poul Sander 2025-03-04 20:16 69
	DrawScaled(target, frameTime, x, y, data->imgCord.w, data->imgCord.h, resize);
68313e95 sago007 2016-09-28 16:52 70
}
68313e95 sago007 2016-09-28 16:52 71
62b87fe3 Poul Sander 2025-03-04 20:16 72
void SagoSprite::DrawRotated(SDL_Renderer* target, Sint32 frameTime, int x, int y, const double angleRadian, SagoLogicalResize* resize) const {
84b6e374 sago007 2020-09-15 18:13 73
	SDL_Point center = {this->data->origin.x, this->data->origin.y};
62b87fe3 Poul Sander 2025-03-04 20:16 74
	DrawScaledAndRotated(target, frameTime, x, y, data->imgCord.w, data->imgCord.h, angleRadian, &center, SDL_FLIP_NONE, resize);
84b6e374 sago007 2020-09-15 18:13 75
}
84b6e374 sago007 2020-09-15 18:13 76
62b87fe3 Poul Sander 2025-03-04 20:16 77
void SagoSprite::DrawScaled(SDL_Renderer* target, Sint32 frameTime, int x, int y, int w, int h, SagoLogicalResize* resize) const {
62b87fe3 Poul Sander 2025-03-04 20:16 78
	DrawScaledAndRotated(target, frameTime, x, y, w, h, 0.0, nullptr, SDL_FLIP_NONE, resize);
84b6e374 sago007 2020-09-15 18:13 79
}
84b6e374 sago007 2020-09-15 18:13 80
62b87fe3 Poul Sander 2025-03-04 20:16 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, SagoLogicalResize* resize) 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;
62b87fe3 Poul Sander 2025-03-04 20:16 97
	if (resize) {
62b87fe3 Poul Sander 2025-03-04 20:16 98
		resize->LogicalToPhysical(pos);
62b87fe3 Poul Sander 2025-03-04 20:16 99
	}
84b6e374 sago007 2020-09-15 18:13 100
	SDL_RenderCopyEx(target, data->tex.get(), &rect, &pos, angleDegress, center, flip);
82da8a1c sago007 2015-12-19 18:00 101
}
82da8a1c sago007 2015-12-19 18:00 102
62b87fe3 Poul Sander 2025-03-04 20:16 103
void SagoSprite::Draw(SDL_Renderer* target, Sint32 frameTime, int x, int y, const SDL_Rect& part, SagoLogicalResize* resize) const {
82da8a1c sago007 2015-12-19 18:00 104
	SDL_Rect rect = data->imgCord;
82da8a1c sago007 2015-12-19 18:00 105
	rect.x+=rect.w*((frameTime/data->aniFrameTime)%data->aniFrames);
82da8a1c sago007 2015-12-19 18:00 106
	rect.x += part.x;
82da8a1c sago007 2015-12-19 18:00 107
	rect.y += part.y;
82da8a1c sago007 2015-12-19 18:00 108
	rect.w = part.w;
82da8a1c sago007 2015-12-19 18:00 109
	rect.h = part.h;
82da8a1c sago007 2015-12-19 18:00 110
	SDL_Rect pos = rect;
55ea55dd sago007 2018-03-24 16:07 111
	pos.x = x - this->data->origin.x;
55ea55dd sago007 2018-03-24 16:07 112
	pos.y = y - this->data->origin.y;
62b87fe3 Poul Sander 2025-03-04 20:16 113
	if (resize) {
62b87fe3 Poul Sander 2025-03-04 20:16 114
		resize->LogicalToPhysical(pos);
62b87fe3 Poul Sander 2025-03-04 20:16 115
	}
1de94c36 sago007 2017-04-20 19:38 116
	SDL_RenderCopy(target, data->tex.get(), &rect, &pos);
82da8a1c sago007 2015-12-19 18:00 117
}
82da8a1c sago007 2015-12-19 18:00 118
62b87fe3 Poul Sander 2025-03-04 20:16 119
void SagoSprite::DrawProgressive(SDL_Renderer* target, float progress, int x, int y, SagoLogicalResize* resize) const {
55ea55dd sago007 2018-03-24 16:07 120
	Sint32 frameNumber = progress*data->aniFrames;
55ea55dd sago007 2018-03-24 16:07 121
	Sint32 frameTime = frameNumber*data->aniFrameTime;
62b87fe3 Poul Sander 2025-03-04 20:16 122
	Draw(target, frameTime, x, y, resize);
55ea55dd sago007 2018-03-24 16:07 123
}
55ea55dd sago007 2018-03-24 16:07 124
62b87fe3 Poul Sander 2025-03-04 20:16 125
void SagoSprite::DrawBounded(SDL_Renderer* target, Sint32 frameTime, int x, int y, const SDL_Rect& bounds, SagoLogicalResize* resize) const {
c6bde3e3 sago007 2015-12-22 16:56 126
	SDL_Rect rect = data->imgCord;
c6bde3e3 sago007 2015-12-22 16:56 127
	rect.x+=rect.w*((frameTime/data->aniFrameTime)%data->aniFrames);
c6bde3e3 sago007 2015-12-22 16:56 128
	SDL_Rect pos = rect;
c6bde3e3 sago007 2015-12-22 16:56 129
	pos.x = x;
c6bde3e3 sago007 2015-12-22 16:56 130
	pos.y = y;
c6bde3e3 sago007 2015-12-22 16:56 131
	if (pos.x > bounds.x+bounds.w) {
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 > bounds.y+bounds.h) {
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+pos.w < bounds.x) {
c6bde3e3 sago007 2015-12-22 16:56 138
		return;
c6bde3e3 sago007 2015-12-22 16:56 139
	}
c6bde3e3 sago007 2015-12-22 16:56 140
	if (pos.y+pos.h < bounds.y) {
c6bde3e3 sago007 2015-12-22 16:56 141
		return;
c6bde3e3 sago007 2015-12-22 16:56 142
	}
c6bde3e3 sago007 2015-12-22 16:56 143
	if (pos.x < bounds.x) {
c6bde3e3 sago007 2015-12-22 16:56 144
		Sint16 absDiff = bounds.x-pos.x;
c6bde3e3 sago007 2015-12-22 16:56 145
		pos.x+=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 146
		rect.x+=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 147
		pos.w-=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 148
		rect.w-=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 149
	}
c6bde3e3 sago007 2015-12-22 16:56 150
	if (pos.y < bounds.y) {
c6bde3e3 sago007 2015-12-22 16:56 151
		Sint16 absDiff = bounds.y-pos.y;
c6bde3e3 sago007 2015-12-22 16:56 152
		pos.y+=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 153
		rect.y+=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 154
		pos.h-=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 155
		rect.h-=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 156
	}
c6bde3e3 sago007 2015-12-22 16:56 157
	if (pos.x+pos.w > bounds.x+bounds.w) {
c6bde3e3 sago007 2015-12-22 16:56 158
		Sint16 absDiff = pos.x+pos.w-(bounds.x+bounds.w);
c6bde3e3 sago007 2015-12-22 16:56 159
		pos.w -= absDiff;
c6bde3e3 sago007 2015-12-22 16:56 160
		rect.w -= absDiff;
c6bde3e3 sago007 2015-12-22 16:56 161
	}
c6bde3e3 sago007 2015-12-22 16:56 162
	if (pos.y+pos.h > bounds.y+bounds.h) {
c6bde3e3 sago007 2015-12-22 16:56 163
		Sint16 absDiff = pos.y+pos.h-(bounds.y+bounds.h);
c6bde3e3 sago007 2015-12-22 16:56 164
		pos.h -= absDiff;
c6bde3e3 sago007 2015-12-22 16:56 165
		rect.h -= absDiff;
c6bde3e3 sago007 2015-12-22 16:56 166
	}
c6bde3e3 sago007 2015-12-22 16:56 167
62b87fe3 Poul Sander 2025-03-04 20:16 168
	if (resize) {
62b87fe3 Poul Sander 2025-03-04 20:16 169
		resize->LogicalToPhysical(pos);
62b87fe3 Poul Sander 2025-03-04 20:16 170
	}
1de94c36 sago007 2017-04-20 19:38 171
	SDL_RenderCopy(target, data->tex.get(), &rect, &pos);
c6bde3e3 sago007 2015-12-22 16:56 172
}
c6bde3e3 sago007 2015-12-22 16:56 173
82da8a1c sago007 2015-12-19 18:00 174
void SagoSprite::SetOrigin(const SDL_Rect& newOrigin) {
82da8a1c sago007 2015-12-19 18:00 175
	data->origin = newOrigin;
82da8a1c sago007 2015-12-19 18:00 176
}
82da8a1c sago007 2015-12-19 18:00 177
1d2e2129 sago007 2015-12-23 15:41 178
int SagoSprite::GetWidth() const {
1d2e2129 sago007 2015-12-23 15:41 179
	return data->imgCord.w;
1d2e2129 sago007 2015-12-23 15:41 180
}
1d2e2129 sago007 2015-12-23 15:41 181
int SagoSprite::GetHeight() const {
1d2e2129 sago007 2015-12-23 15:41 182
	return data->imgCord.h;
1d2e2129 sago007 2015-12-23 15:41 183
}
1d2e2129 sago007 2015-12-23 15:41 184
50c81b4b sago007 2020-09-19 15:21 185
}  //namespace sago
1970-01-01 00:00 186