Prepared sprites for monster implementation.
Added a render function to texture
This commit is contained in:
parent
cdf4949eea
commit
9f71d23849
16
src/player.c
16
src/player.c
|
@ -16,7 +16,7 @@ bool has_collided(Sprite *sprite, RoomMatrix *matrix)
|
|||
static
|
||||
void move_left(Sprite *sprite, RoomMatrix *matrix)
|
||||
{
|
||||
sprite->texture->clip.y = 16;
|
||||
sprite->textures[0]->clip.y = 16;
|
||||
sprite->pos.x -= TILE_DIMENSION;
|
||||
if (has_collided(sprite, matrix))
|
||||
sprite->pos.x += TILE_DIMENSION;
|
||||
|
@ -25,7 +25,7 @@ void move_left(Sprite *sprite, RoomMatrix *matrix)
|
|||
static
|
||||
void move_right(Sprite *sprite, RoomMatrix *matrix)
|
||||
{
|
||||
sprite->texture->clip.y = 32;
|
||||
sprite->textures[0]->clip.y = 32;
|
||||
sprite->pos.x += TILE_DIMENSION;
|
||||
if (has_collided(sprite, matrix))
|
||||
sprite->pos.x -= TILE_DIMENSION;
|
||||
|
@ -34,7 +34,7 @@ void move_right(Sprite *sprite, RoomMatrix *matrix)
|
|||
static
|
||||
void move_up(Sprite *sprite, RoomMatrix *matrix)
|
||||
{
|
||||
sprite->texture->clip.y = 48;
|
||||
sprite->textures[0]->clip.y = 48;
|
||||
sprite->pos.y -= TILE_DIMENSION;
|
||||
if (has_collided(sprite, matrix))
|
||||
sprite->pos.y += TILE_DIMENSION;
|
||||
|
@ -43,7 +43,7 @@ void move_up(Sprite *sprite, RoomMatrix *matrix)
|
|||
static
|
||||
void move_down(Sprite *sprite, RoomMatrix *matrix)
|
||||
{
|
||||
sprite->texture->clip.y = 0;
|
||||
sprite->textures[0]->clip.y = 0;
|
||||
sprite->pos.y += TILE_DIMENSION;
|
||||
if (has_collided(sprite, matrix))
|
||||
sprite->pos.y -= TILE_DIMENSION;
|
||||
|
@ -69,7 +69,7 @@ void handle_player_input(Sprite *sprite, RoomMatrix *matrix, SDL_Event *event)
|
|||
move_down(sprite, matrix);
|
||||
break;
|
||||
}
|
||||
sprite->texture->clip.x = 16*step;
|
||||
sprite->textures[0]->clip.x = 16*step;
|
||||
if (step == 3)
|
||||
step = 0;
|
||||
else
|
||||
|
@ -100,10 +100,10 @@ Sprite* player_create(class_t class, SDL_Renderer *renderer)
|
|||
break;
|
||||
}
|
||||
|
||||
sprite_load_texture(player, asset, renderer);
|
||||
sprite_load_texture(player, asset, 0, renderer);
|
||||
player->pos = (Position) { TILE_DIMENSION, TILE_DIMENSION };
|
||||
player->texture->clip = (SDL_Rect) { 0, 0, 16, 16 };
|
||||
player->texture->dim = (Dimension) { TILE_DIMENSION, TILE_DIMENSION };
|
||||
player->textures[0]->clip = (SDL_Rect) { 0, 0, 16, 16 };
|
||||
player->textures[0]->dim = (Dimension) { TILE_DIMENSION, TILE_DIMENSION };
|
||||
player->handle_event = &handle_player_input;
|
||||
|
||||
return player;
|
||||
|
|
66
src/sprite.c
66
src/sprite.c
|
@ -7,7 +7,9 @@ Sprite* sprite_create_default()
|
|||
Position pos = { 0, 0 };
|
||||
|
||||
Sprite *s = ec_malloc(sizeof(Sprite));
|
||||
*s = (Sprite) { NULL, pos, NULL };
|
||||
*s = (Sprite) { { NULL, NULL }, false, pos, NULL, NULL };
|
||||
|
||||
s->renderTimer = timer_create();
|
||||
|
||||
return s;
|
||||
}
|
||||
|
@ -17,35 +19,61 @@ Sprite* sprite_create()
|
|||
return sprite_create_default();
|
||||
}
|
||||
|
||||
void sprite_load_texture(Sprite *sprite, char *path, SDL_Renderer *renderer)
|
||||
void
|
||||
sprite_load_texture(Sprite *sprite,
|
||||
char *path,
|
||||
int index,
|
||||
SDL_Renderer *renderer)
|
||||
{
|
||||
if (sprite->texture != NULL) {
|
||||
texture_destroy(sprite->texture);
|
||||
sprite->texture = NULL;
|
||||
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->texture = texture_create(path, renderer);
|
||||
sprite->textures[index] = texture_create(path, renderer);
|
||||
sprite->destroyTextures = true;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
void sprite_render(Sprite *s, Camera *cam)
|
||||
{
|
||||
Position cameraPos = camera_to_camera_position(cam, &s->pos);
|
||||
SDL_Rect draw_box = (SDL_Rect) {
|
||||
cameraPos.x,
|
||||
cameraPos.y,
|
||||
s->texture->dim.width,
|
||||
s->texture->dim.height
|
||||
};
|
||||
static int t_index = 0;
|
||||
|
||||
SDL_RenderCopy(cam->renderer,
|
||||
s->texture->texture,
|
||||
&s->texture->clip,
|
||||
&draw_box);
|
||||
if (!timer_started(s->renderTimer))
|
||||
timer_start(s->renderTimer);
|
||||
|
||||
if (timer_get_ticks(s->renderTimer) > 300) {
|
||||
timer_stop(s->renderTimer);
|
||||
timer_start(s->renderTimer);
|
||||
if (s->textures[1])
|
||||
t_index = t_index == 0 ? 1 : 0;
|
||||
}
|
||||
|
||||
Position cameraPos = camera_to_camera_position(cam, &s->pos);
|
||||
texture_render(s->textures[t_index], &cameraPos, cam);
|
||||
}
|
||||
|
||||
void sprite_destroy(Sprite *sprite)
|
||||
{
|
||||
if (sprite->texture)
|
||||
texture_destroy(sprite->texture);
|
||||
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);
|
||||
free(sprite);
|
||||
}
|
||||
|
|
|
@ -7,16 +7,21 @@
|
|||
#include "position.h"
|
||||
#include "camera.h"
|
||||
#include "roommatrix.h"
|
||||
#include "timer.h"
|
||||
|
||||
typedef struct Sprite_t {
|
||||
Texture *texture;
|
||||
Texture* textures[2];
|
||||
bool destroyTextures;
|
||||
Position pos;
|
||||
Timer *renderTimer;
|
||||
void (*handle_event)(struct Sprite_t*, RoomMatrix*, SDL_Event*);
|
||||
} Sprite;
|
||||
|
||||
Sprite* sprite_create();
|
||||
|
||||
void sprite_load_texture(Sprite *, char *path, SDL_Renderer *);
|
||||
void sprite_load_texture(Sprite *, char *path, int index, SDL_Renderer *);
|
||||
|
||||
void sprite_set_texture(Sprite *, Texture *, int index);
|
||||
|
||||
void sprite_render(Sprite*, Camera*);
|
||||
|
||||
|
|
|
@ -9,7 +9,7 @@ Texture* texture_create(const char *path, SDL_Renderer *renderer)
|
|||
|
||||
if (surface == NULL)
|
||||
{
|
||||
printf("Failed to load texture (%s): %s\n", path, IMG_GetError());
|
||||
printf("[!!] Failed to load texture (%s): %s\n", path, IMG_GetError());
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -18,13 +18,13 @@ Texture* texture_create(const char *path, SDL_Renderer *renderer)
|
|||
newTexture->dim.width = surface->w;
|
||||
newTexture->clip.x = 0;
|
||||
newTexture->clip.y = 0;
|
||||
newTexture->clip.h = surface->h;
|
||||
newTexture->clip.w = surface->w;
|
||||
newTexture->clip.h = surface->h;
|
||||
newTexture->texture = SDL_CreateTextureFromSurface(renderer,
|
||||
surface);
|
||||
if (newTexture->texture == NULL)
|
||||
{
|
||||
printf("Failed to create texture (%s): %s\n",
|
||||
printf("[!!] Failed to create texture (%s): %s\n",
|
||||
path,
|
||||
SDL_GetError());
|
||||
}
|
||||
|
@ -35,6 +35,22 @@ Texture* texture_create(const char *path, SDL_Renderer *renderer)
|
|||
return newTexture;
|
||||
}
|
||||
|
||||
void
|
||||
texture_render(Texture *texture, Position *p, Camera *cam)
|
||||
{
|
||||
SDL_Rect draw_box = (SDL_Rect) {
|
||||
p->x,
|
||||
p->y,
|
||||
texture->dim.width,
|
||||
texture->dim.height
|
||||
};
|
||||
|
||||
SDL_RenderCopy(cam->renderer,
|
||||
texture->texture,
|
||||
&texture->clip,
|
||||
&draw_box);
|
||||
}
|
||||
|
||||
void texture_destroy(Texture *texture)
|
||||
{
|
||||
SDL_DestroyTexture(texture->texture);
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
|
||||
#include <SDL2/SDL.h>
|
||||
#include "dimension.h"
|
||||
#include "position.h"
|
||||
#include "camera.h"
|
||||
|
||||
typedef struct {
|
||||
SDL_Texture *texture;
|
||||
|
@ -12,6 +14,8 @@ typedef struct {
|
|||
|
||||
Texture* texture_create(const char *path, SDL_Renderer *renderer);
|
||||
|
||||
void texture_render(Texture*, Position*, Camera*);
|
||||
|
||||
void texture_destroy(Texture *texture);
|
||||
|
||||
#endif // TEXTURE_H_
|
||||
|
|
Loading…
Reference in New Issue