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"
82da8a1c sago007 2015-12-19 18:00 26
82da8a1c sago007 2015-12-19 18:00 27
namespace sago {
82da8a1c sago007 2015-12-19 18:00 28
82da8a1c sago007 2015-12-19 18:00 29
struct SagoSprite::SagoSpriteData {
c6bde3e3 sago007 2015-12-22 16:56 30
	SDL_Texture* tex = nullptr;
82da8a1c sago007 2015-12-19 18:00 31
	SDL_Rect imgCord;
82da8a1c sago007 2015-12-19 18:00 32
	SDL_Rect origin = {};
82da8a1c sago007 2015-12-19 18:00 33
	int aniFrames = 0;
82da8a1c sago007 2015-12-19 18:00 34
	int aniFrameTime = 0;
82da8a1c sago007 2015-12-19 18:00 35
};
82da8a1c sago007 2015-12-19 18:00 36
c6bde3e3 sago007 2015-12-22 16:56 37
SagoSprite::SagoSprite() {
c6bde3e3 sago007 2015-12-22 16:56 38
	data = new SagoSpriteData();
c6bde3e3 sago007 2015-12-22 16:56 39
}
c6bde3e3 sago007 2015-12-22 16:56 40
82da8a1c sago007 2015-12-19 18:00 41
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 42
	data = new SagoSpriteData();
82da8a1c sago007 2015-12-19 18:00 43
	data->tex = texHolder.getTexturePtr(texture);
82da8a1c sago007 2015-12-19 18:00 44
	data->imgCord = initImage;
82da8a1c sago007 2015-12-19 18:00 45
	data->aniFrames = animationFrames;
82da8a1c sago007 2015-12-19 18:00 46
	data->aniFrameTime = animationFrameLength;
82da8a1c sago007 2015-12-19 18:00 47
}
82da8a1c sago007 2015-12-19 18:00 48
c6bde3e3 sago007 2015-12-22 16:56 49
SagoSprite::SagoSprite(const SagoSprite& base) : data(new SagoSpriteData(*other.data)) {
c6bde3e3 sago007 2015-12-22 16:56 50
	
c6bde3e3 sago007 2015-12-22 16:56 51
}
82da8a1c sago007 2015-12-19 18:00 52
c6bde3e3 sago007 2015-12-22 16:56 53
SagoSprite& operator=(const SagoSprite& base) {
c6bde3e3 sago007 2015-12-22 16:56 54
	*data = *other.data;
c6bde3e3 sago007 2015-12-22 16:56 55
    return *this;
c6bde3e3 sago007 2015-12-22 16:56 56
}
82da8a1c sago007 2015-12-19 18:00 57
82da8a1c sago007 2015-12-19 18:00 58
SagoSprite::~SagoSprite() {
82da8a1c sago007 2015-12-19 18:00 59
	delete data;
82da8a1c sago007 2015-12-19 18:00 60
}
82da8a1c sago007 2015-12-19 18:00 61
c6bde3e3 sago007 2015-12-22 16:56 62
void SagoSprite::Draw(SDL_Renderer* target, Sint32 frameTime, int x, int y) const {
82da8a1c sago007 2015-12-19 18:00 63
	SDL_Rect rect = data->imgCord;
82da8a1c sago007 2015-12-19 18:00 64
	rect.x+=rect.w*((frameTime/data->aniFrameTime)%data->aniFrames);
82da8a1c sago007 2015-12-19 18:00 65
	SDL_Rect pos = rect;
82da8a1c sago007 2015-12-19 18:00 66
	pos.x = x;
82da8a1c sago007 2015-12-19 18:00 67
	pos.y = y;
82da8a1c sago007 2015-12-19 18:00 68
	SDL_RenderCopy(target, data->tex, &rect, &pos);
82da8a1c sago007 2015-12-19 18:00 69
}
82da8a1c sago007 2015-12-19 18:00 70
c6bde3e3 sago007 2015-12-22 16:56 71
void SagoSprite::Draw(SDL_Renderer* target, Sint32 frameTime, int x, int y, const SDL_Rect& part) const {
82da8a1c sago007 2015-12-19 18:00 72
	SDL_Rect rect = data->imgCord;
82da8a1c sago007 2015-12-19 18:00 73
	rect.x+=rect.w*((frameTime/data->aniFrameTime)%data->aniFrames);
82da8a1c sago007 2015-12-19 18:00 74
	rect.x += part.x;
82da8a1c sago007 2015-12-19 18:00 75
	rect.y += part.y;
82da8a1c sago007 2015-12-19 18:00 76
	rect.w = part.w;
82da8a1c sago007 2015-12-19 18:00 77
	rect.h = part.h;
82da8a1c sago007 2015-12-19 18:00 78
	SDL_Rect pos = rect;
82da8a1c sago007 2015-12-19 18:00 79
	pos.x = x;
82da8a1c sago007 2015-12-19 18:00 80
	pos.y = y;
82da8a1c sago007 2015-12-19 18:00 81
	SDL_RenderCopy(target, data->tex, &rect, &pos);
82da8a1c sago007 2015-12-19 18:00 82
}
82da8a1c sago007 2015-12-19 18:00 83
c6bde3e3 sago007 2015-12-22 16:56 84
void SagoSprite::DrawBounded(SDL_Renderer* target, Sint32 frameTime, int x, int y, const SDL_Rect& bounds) const {
c6bde3e3 sago007 2015-12-22 16:56 85
	SDL_Rect rect = data->imgCord;
c6bde3e3 sago007 2015-12-22 16:56 86
	rect.x+=rect.w*((frameTime/data->aniFrameTime)%data->aniFrames);
c6bde3e3 sago007 2015-12-22 16:56 87
	SDL_Rect pos = rect;
c6bde3e3 sago007 2015-12-22 16:56 88
	pos.x = x;
c6bde3e3 sago007 2015-12-22 16:56 89
	pos.y = y;
c6bde3e3 sago007 2015-12-22 16:56 90
	if (pos.x > bounds.x+bounds.w) {
c6bde3e3 sago007 2015-12-22 16:56 91
		return;
c6bde3e3 sago007 2015-12-22 16:56 92
	}
c6bde3e3 sago007 2015-12-22 16:56 93
	if (pos.y > bounds.y+bounds.h) {
c6bde3e3 sago007 2015-12-22 16:56 94
		return;
c6bde3e3 sago007 2015-12-22 16:56 95
	}
c6bde3e3 sago007 2015-12-22 16:56 96
	if (pos.x+pos.w < bounds.x) {
c6bde3e3 sago007 2015-12-22 16:56 97
		return;
c6bde3e3 sago007 2015-12-22 16:56 98
	}
c6bde3e3 sago007 2015-12-22 16:56 99
	if (pos.y+pos.h < bounds.y) {
c6bde3e3 sago007 2015-12-22 16:56 100
		return;
c6bde3e3 sago007 2015-12-22 16:56 101
	}
c6bde3e3 sago007 2015-12-22 16:56 102
	if (pos.x < bounds.x) {
c6bde3e3 sago007 2015-12-22 16:56 103
		Sint16 absDiff = bounds.x-pos.x;
c6bde3e3 sago007 2015-12-22 16:56 104
		pos.x+=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 105
		rect.x+=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 106
		pos.w-=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 107
		rect.w-=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 108
	}
c6bde3e3 sago007 2015-12-22 16:56 109
	if (pos.y < bounds.y) {
c6bde3e3 sago007 2015-12-22 16:56 110
		Sint16 absDiff = bounds.y-pos.y;
c6bde3e3 sago007 2015-12-22 16:56 111
		pos.y+=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 112
		rect.y+=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 113
		pos.h-=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 114
		rect.h-=absDiff;
c6bde3e3 sago007 2015-12-22 16:56 115
	}
c6bde3e3 sago007 2015-12-22 16:56 116
	if (pos.x+pos.w > bounds.x+bounds.w) {
c6bde3e3 sago007 2015-12-22 16:56 117
		Sint16 absDiff = pos.x+pos.w-(bounds.x+bounds.w);
c6bde3e3 sago007 2015-12-22 16:56 118
		pos.w -= absDiff;
c6bde3e3 sago007 2015-12-22 16:56 119
		rect.w -= absDiff;
c6bde3e3 sago007 2015-12-22 16:56 120
	}
c6bde3e3 sago007 2015-12-22 16:56 121
	if (pos.y+pos.h > bounds.y+bounds.h) {
c6bde3e3 sago007 2015-12-22 16:56 122
		Sint16 absDiff = pos.y+pos.h-(bounds.y+bounds.h);
c6bde3e3 sago007 2015-12-22 16:56 123
		pos.h -= absDiff;
c6bde3e3 sago007 2015-12-22 16:56 124
		rect.h -= absDiff;
c6bde3e3 sago007 2015-12-22 16:56 125
	}
c6bde3e3 sago007 2015-12-22 16:56 126
c6bde3e3 sago007 2015-12-22 16:56 127
	SDL_RenderCopy(target, data->tex, &rect, &pos);
c6bde3e3 sago007 2015-12-22 16:56 128
}
c6bde3e3 sago007 2015-12-22 16:56 129
82da8a1c sago007 2015-12-19 18:00 130
void SagoSprite::SetOrigin(const SDL_Rect& newOrigin) {
82da8a1c sago007 2015-12-19 18:00 131
	data->origin = newOrigin;
82da8a1c sago007 2015-12-19 18:00 132
}
82da8a1c sago007 2015-12-19 18:00 133
82da8a1c sago007 2015-12-19 18:00 134
}  //namespace sago