2018-02-16 18:11:26 +01:00
|
|
|
/*
|
|
|
|
* BreakHack - A dungeone crawler RPG
|
|
|
|
* Copyright (C) 2018 Linus Probert <linus.probert@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2017-12-21 12:22:37 +01:00
|
|
|
#include <stdlib.h>
|
2017-11-30 21:00:47 +01:00
|
|
|
#include "sprite.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2018-01-31 20:59:55 +01:00
|
|
|
static Sprite*
|
|
|
|
sprite_create_default(void)
|
2017-11-30 21:00:47 +01:00
|
|
|
{
|
|
|
|
Sprite *s = ec_malloc(sizeof(Sprite));
|
2017-12-19 23:30:58 +01:00
|
|
|
s->textures[0] = NULL;
|
|
|
|
s->textures[1] = NULL;
|
2018-01-31 20:59:55 +01:00
|
|
|
s->clip = (SDL_Rect) { 0, 0, 0, 0 };
|
2017-12-19 23:30:58 +01:00
|
|
|
s->destroyTextures = false;
|
|
|
|
s->pos = (Position) { 0, 0 };
|
2018-02-23 23:53:52 +01:00
|
|
|
s->dim = DEFAULT_DIMENSION;
|
2018-03-25 23:30:26 +02:00
|
|
|
s->angle = 0;
|
|
|
|
s->rotationPoint = (SDL_Point) { 0, 0 };
|
|
|
|
s->flip = SDL_FLIP_NONE;
|
2017-12-11 08:23:30 +01:00
|
|
|
s->renderTimer = timer_create();
|
2017-12-19 23:30:58 +01:00
|
|
|
s->texture_index = 0;
|
2017-12-22 15:15:40 +01:00
|
|
|
s->fixed = false;
|
2018-02-23 11:10:50 +01:00
|
|
|
s->animate = true;
|
2018-01-31 13:52:11 +01:00
|
|
|
s->hidden = false;
|
2018-03-10 22:04:03 +01:00
|
|
|
s->onRender = NULL;
|
2017-11-30 21:00:47 +01:00
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2018-01-31 20:59:55 +01:00
|
|
|
Sprite*
|
2018-02-23 23:53:52 +01:00
|
|
|
sprite_create(void)
|
2017-11-30 21:00:47 +01:00
|
|
|
{
|
|
|
|
return sprite_create_default();
|
|
|
|
}
|
|
|
|
|
2017-12-11 08:23:30 +01:00
|
|
|
void
|
|
|
|
sprite_load_texture(Sprite *sprite,
|
|
|
|
char *path,
|
|
|
|
int index,
|
|
|
|
SDL_Renderer *renderer)
|
2017-11-30 21:00:47 +01:00
|
|
|
{
|
2017-12-11 08:23:30 +01:00
|
|
|
if (index > 1)
|
|
|
|
fatal("in sprite_load_texture() index out of bounds");
|
|
|
|
|
|
|
|
if (sprite->destroyTextures && sprite->textures[index] != NULL) {
|
|
|
|
texture_destroy(sprite->textures[index]);
|
|
|
|
sprite->textures[index] = NULL;
|
2017-11-30 21:00:47 +01:00
|
|
|
}
|
|
|
|
|
2017-12-15 15:03:29 +01:00
|
|
|
sprite->textures[index] = texture_create();
|
|
|
|
texture_load_from_file(sprite->textures[index], path, renderer);
|
2017-12-11 08:23:30 +01:00
|
|
|
sprite->destroyTextures = true;
|
|
|
|
}
|
|
|
|
|
2018-05-15 14:12:38 +02:00
|
|
|
void sprite_load_text_texture(Sprite *sprite, char * path, int index, int size, int outline)
|
2018-01-31 20:59:55 +01:00
|
|
|
{
|
|
|
|
if (index > 1)
|
|
|
|
fatal("in sprite_load_texture() index out of bounds");
|
|
|
|
|
|
|
|
if (sprite->destroyTextures && sprite->textures[index] != NULL) {
|
|
|
|
texture_destroy(sprite->textures[index]);
|
|
|
|
sprite->textures[index] = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
sprite->textures[index] = texture_create();
|
2018-05-15 14:12:38 +02:00
|
|
|
texture_load_font(sprite->textures[index], path, size, outline);
|
2018-01-31 20:59:55 +01:00
|
|
|
sprite->destroyTextures = true;
|
|
|
|
}
|
|
|
|
|
2017-12-11 08:23:30 +01:00
|
|
|
void
|
|
|
|
sprite_set_texture(Sprite *s, Texture *t, int index)
|
|
|
|
{
|
|
|
|
if (index > 1)
|
|
|
|
fatal("in sprite_set_texture() index out of bounds");
|
|
|
|
if (s->destroyTextures)
|
|
|
|
fatal("in sprite_set_texture() sprite contains loaded textures");
|
|
|
|
|
|
|
|
s->textures[index] = t;
|
2017-11-30 21:00:47 +01:00
|
|
|
}
|
|
|
|
|
2018-03-10 22:04:03 +01:00
|
|
|
void
|
|
|
|
sprite_render(Sprite *s, Camera *cam)
|
2017-11-30 21:00:47 +01:00
|
|
|
{
|
2018-01-31 13:52:11 +01:00
|
|
|
if (s->hidden)
|
|
|
|
return;
|
2018-03-10 22:04:03 +01:00
|
|
|
if (s->onRender)
|
|
|
|
s->onRender(s);
|
2018-02-23 11:10:50 +01:00
|
|
|
if (s->textures[1] && s->animate) {
|
2017-12-13 23:20:54 +01:00
|
|
|
if (!timer_started(s->renderTimer))
|
|
|
|
timer_start(s->renderTimer);
|
2017-12-11 08:23:30 +01:00
|
|
|
|
2017-12-13 23:20:54 +01:00
|
|
|
if (timer_get_ticks(s->renderTimer) > 300) {
|
|
|
|
timer_stop(s->renderTimer);
|
|
|
|
timer_start(s->renderTimer);
|
|
|
|
s->texture_index = s->texture_index == 0 ? 1 : 0;
|
|
|
|
}
|
2017-12-11 08:23:30 +01:00
|
|
|
}
|
|
|
|
|
2017-12-22 15:15:40 +01:00
|
|
|
Position cameraPos;
|
|
|
|
if (!s->fixed)
|
|
|
|
cameraPos = camera_to_camera_position(cam, &s->pos);
|
|
|
|
else
|
|
|
|
cameraPos = s->pos;
|
|
|
|
|
2018-02-23 23:53:52 +01:00
|
|
|
SDL_Rect box = {
|
|
|
|
cameraPos.x, cameraPos.y, s->dim.width, s->dim.height
|
|
|
|
};
|
|
|
|
|
2018-05-06 06:19:59 +02:00
|
|
|
if ((s->clip.w && s->clip.h) || s->angle != 0 || s->flip != SDL_FLIP_NONE) {
|
2018-03-25 23:30:26 +02:00
|
|
|
texture_render_clip_ex(s->textures[s->texture_index],
|
|
|
|
&box,
|
|
|
|
&s->clip,
|
|
|
|
s->angle,
|
|
|
|
&s->rotationPoint,
|
|
|
|
s->flip,
|
|
|
|
cam);
|
|
|
|
} else if (s->clip.w && s->clip.h) {
|
2018-01-31 20:59:55 +01:00
|
|
|
texture_render_clip(s->textures[s->texture_index],
|
2018-02-23 23:53:52 +01:00
|
|
|
&box,
|
2018-01-31 20:59:55 +01:00
|
|
|
&s->clip,
|
|
|
|
cam);
|
2018-03-25 23:30:26 +02:00
|
|
|
} else {
|
2018-01-31 20:59:55 +01:00
|
|
|
texture_render(s->textures[s->texture_index],
|
2018-02-23 23:53:52 +01:00
|
|
|
&box,
|
2018-01-31 20:59:55 +01:00
|
|
|
cam);
|
|
|
|
}
|
2017-11-30 21:00:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void sprite_destroy(Sprite *sprite)
|
|
|
|
{
|
2017-12-11 08:23:30 +01:00
|
|
|
if (sprite->destroyTextures) {
|
|
|
|
if (sprite->textures[0])
|
|
|
|
texture_destroy(sprite->textures[0]);
|
|
|
|
if (sprite->textures[1])
|
|
|
|
texture_destroy(sprite->textures[1]);
|
|
|
|
}
|
|
|
|
timer_destroy(sprite->renderTimer);
|
2017-11-30 21:00:47 +01:00
|
|
|
free(sprite);
|
|
|
|
}
|