git repos / blockattack-game

blame: source/code/sago/SagoSprite.cpp

normal view · raw

82da8a1c sago007 2015-12-19 18:00 1
/*
82da8a1c sago007 2015-12-19 18:00 2
  Copyright (c) 2015 Poul Sander
82da8a1c sago007 2015-12-19 18:00 3
82da8a1c sago007 2015-12-19 18:00 4
  Permission is hereby granted, free of charge, to any person
82da8a1c sago007 2015-12-19 18:00 5
  obtaining a copy of this software and associated documentation files
82da8a1c sago007 2015-12-19 18:00 6
  (the "Software"), to deal in the Software without restriction,
82da8a1c sago007 2015-12-19 18:00 7
  including without limitation the rights to use, copy, modify, merge,
82da8a1c sago007 2015-12-19 18:00 8
  publish, distribute, sublicense, and/or sell copies of the Software,
82da8a1c sago007 2015-12-19 18:00 9
  and to permit persons to whom the Software is furnished to do so,
82da8a1c sago007 2015-12-19 18:00 10
  subject to the following conditions:
82da8a1c sago007 2015-12-19 18:00 11
82da8a1c sago007 2015-12-19 18:00 12
  The above copyright notice and this permission notice shall be
82da8a1c sago007 2015-12-19 18:00 13
  included in all copies or substantial portions of the Software.
82da8a1c sago007 2015-12-19 18:00 14
82da8a1c sago007 2015-12-19 18:00 15
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
82da8a1c sago007 2015-12-19 18:00 16
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
82da8a1c sago007 2015-12-19 18:00 17
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
82da8a1c sago007 2015-12-19 18:00 18
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
82da8a1c sago007 2015-12-19 18:00 19
  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
82da8a1c sago007 2015-12-19 18:00 20
  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
82da8a1c sago007 2015-12-19 18:00 21
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
82da8a1c sago007 2015-12-19 18:00 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 {
c6bde3e3 sago007 2015-12-22 16:56 31
	SDL_Texture* tex = nullptr;
fd1a6361 sago007 2016-10-02 13:31 32
	SDL_Rect imgCord{};
fd1a6361 sago007 2016-10-02 13:31 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();
82da8a1c sago007 2015-12-19 18:00 44
	data->tex = texHolder.getTexturePtr(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 {
9d259921 sago007 2015-12-23 18:46 68
	if (!data->tex) {
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;
82da8a1c sago007 2015-12-19 18:00 74
	pos.x = x;
82da8a1c sago007 2015-12-19 18:00 75
	pos.y = 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
	}
82da8a1c sago007 2015-12-19 18:00 82
	SDL_RenderCopy(target, data->tex, &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;
82da8a1c sago007 2015-12-19 18:00 93
	pos.x = x;
82da8a1c sago007 2015-12-19 18:00 94
	pos.y = y;
82da8a1c sago007 2015-12-19 18:00 95
	SDL_RenderCopy(target, data->tex, &rect, &pos);
82da8a1c sago007 2015-12-19 18:00 96
}
82da8a1c sago007 2015-12-19 18:00 97
c6bde3e3 sago007 2015-12-22 16:56 98
void SagoSprite::DrawBounded(SDL_Renderer* target, Sint32 frameTime, int x, int y, const SDL_Rect& bounds) const {
c6bde3e3 sago007 2015-12-22 16:56 99
	SDL_Rect rect = data->imgCord;
c6bde3e3 sago007 2015-12-22 16:56 100
	rect.x+=rect.w*((frameTime/data->aniFrameTime)%data->aniFrames);
c6bde3e3 sago007 2015-12-22 16:56 101
	SDL_Rect pos = rect;
c6bde3e3 sago007 2015-12-22 16:56 102
	pos.x = x;
c6bde3e3 sago007 2015-12-22 16:56 103
	pos.y = y;
c6bde3e3 sago007 2015-12-22 16:56 104
	if (pos.x > bounds.x+bounds.w) {
c6bde3e3 sago007 2015-12-22 16:56 105
		return;
c6bde3e3 sago007 2015-12-22 16:56 106
	}
c6bde3e3 sago007 2015-12-22 16:56 107
	if (pos.y > bounds.y+bounds.h) {
c6bde3e3 sago007 2015-12-22 16:56 108
		return;
c6bde3e3 sago007 2015-12-22 16:56 109
	}
c6bde3e3 sago007 2015-12-22 16:56 110
	if (pos.x+pos.w < bounds.x) {
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+pos.h < bounds.y) {
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 < bounds.x) {
c6bde3e3 sago007 2015-12-22 16:56 117
		Sint16 absDiff = bounds.x-pos.x;
c6bde3e3 sago007 2015-12-22 16:56 118
		pos.x+=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 119
		rect.x+=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 120
		pos.w-=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 121
		rect.w-=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 122
	}
c6bde3e3 sago007 2015-12-22 16:56 123
	if (pos.y < bounds.y) {
c6bde3e3 sago007 2015-12-22 16:56 124
		Sint16 absDiff = bounds.y-pos.y;
c6bde3e3 sago007 2015-12-22 16:56 125
		pos.y+=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 126
		rect.y+=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 127
		pos.h-=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 128
		rect.h-=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 129
	}
c6bde3e3 sago007 2015-12-22 16:56 130
	if (pos.x+pos.w > bounds.x+bounds.w) {
c6bde3e3 sago007 2015-12-22 16:56 131
		Sint16 absDiff = pos.x+pos.w-(bounds.x+bounds.w);
c6bde3e3 sago007 2015-12-22 16:56 132
		pos.w -= absDiff;
c6bde3e3 sago007 2015-12-22 16:56 133
		rect.w -= absDiff;
c6bde3e3 sago007 2015-12-22 16:56 134
	}
c6bde3e3 sago007 2015-12-22 16:56 135
	if (pos.y+pos.h > bounds.y+bounds.h) {
c6bde3e3 sago007 2015-12-22 16:56 136
		Sint16 absDiff = pos.y+pos.h-(bounds.y+bounds.h);
c6bde3e3 sago007 2015-12-22 16:56 137
		pos.h -= absDiff;
c6bde3e3 sago007 2015-12-22 16:56 138
		rect.h -= absDiff;
c6bde3e3 sago007 2015-12-22 16:56 139
	}
c6bde3e3 sago007 2015-12-22 16:56 140
c6bde3e3 sago007 2015-12-22 16:56 141
	SDL_RenderCopy(target, data->tex, &rect, &pos);
c6bde3e3 sago007 2015-12-22 16:56 142
}
c6bde3e3 sago007 2015-12-22 16:56 143
82da8a1c sago007 2015-12-19 18:00 144
void SagoSprite::SetOrigin(const SDL_Rect& newOrigin) {
82da8a1c sago007 2015-12-19 18:00 145
	data->origin = newOrigin;
82da8a1c sago007 2015-12-19 18:00 146
}
82da8a1c sago007 2015-12-19 18:00 147
1d2e2129 sago007 2015-12-23 15:41 148
int SagoSprite::GetWidth() const {
1d2e2129 sago007 2015-12-23 15:41 149
	return data->imgCord.w;
1d2e2129 sago007 2015-12-23 15:41 150
}
1d2e2129 sago007 2015-12-23 15:41 151
int SagoSprite::GetHeight() const {
1d2e2129 sago007 2015-12-23 15:41 152
	return data->imgCord.h;
1d2e2129 sago007 2015-12-23 15:41 153
}
1d2e2129 sago007 2015-12-23 15:41 154
82da8a1c sago007 2015-12-19 18:00 155
}  //namespace sago