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