Moved 'clip' from texture to sprite.

This commit is contained in:
Linus Probert 2017-12-14 12:01:05 +01:00
parent 80ab804e07
commit c4d142860c
9 changed files with 49 additions and 21 deletions

View File

@ -4,8 +4,8 @@ x Add enemies (generated through lua)
x Monster object created x Monster object created
x Lua bindings for creation x Lua bindings for creation
x Making some better generation and randomeness x Making some better generation and randomeness
- Move "texture clip" from texture to sprite x Move "clip" from texture to sprite
- Hitting enemies o Hitting enemies
- Moving enemies - Moving enemies
- Lower levels - Lower levels
- XP - XP

View File

@ -182,7 +182,7 @@ l_add_monster(lua_State *L)
lua_pop(L, 4); lua_pop(L, 4);
monster = monster_create(); monster = monster_create();
monster->clip = (SDL_Rect) { clip_x, clip_y, 16, 16 }; monster->sprite->clip = (SDL_Rect) { clip_x, clip_y, 16, 16 };
monster->sprite->pos = (Position) { x, y }; monster->sprite->pos = (Position) { x, y };
sprite_set_texture(monster->sprite, texture1, 0); sprite_set_texture(monster->sprite, texture1, 0);
sprite_set_texture(monster->sprite, texture2, 1); sprite_set_texture(monster->sprite, texture2, 1);

View File

@ -6,15 +6,13 @@ monster_create()
{ {
Monster *m = ec_malloc(sizeof(Monster)); Monster *m = ec_malloc(sizeof(Monster));
m->sprite = sprite_create(); m->sprite = sprite_create();
m->clip = (SDL_Rect) { 0, 0, 16, 16 }; m->sprite->clip = (SDL_Rect) { 0, 0, 16, 16 };
return m; return m;
} }
void void
monster_render(Monster *m, Camera *cam) monster_render(Monster *m, Camera *cam)
{ {
m->sprite->textures[0]->clip = m->clip;
m->sprite->textures[1]->clip = m->clip;
sprite_render(m->sprite, cam); sprite_render(m->sprite, cam);
} }

View File

@ -8,7 +8,6 @@
typedef struct { typedef struct {
Sprite *sprite; Sprite *sprite;
Stats stats; Stats stats;
SDL_Rect clip;
} Monster; } Monster;
Monster* monster_create(); Monster* monster_create();

View File

@ -24,7 +24,7 @@ has_collided(Sprite *sprite, RoomMatrix *matrix)
static void static void
move_left(Sprite *sprite, RoomMatrix *matrix) move_left(Sprite *sprite, RoomMatrix *matrix)
{ {
sprite->textures[0]->clip.y = 16; sprite->clip.y = 16;
sprite->pos.x -= TILE_DIMENSION; sprite->pos.x -= TILE_DIMENSION;
if (has_collided(sprite, matrix)) if (has_collided(sprite, matrix))
sprite->pos.x += TILE_DIMENSION; sprite->pos.x += TILE_DIMENSION;
@ -33,7 +33,7 @@ move_left(Sprite *sprite, RoomMatrix *matrix)
static static
void move_right(Sprite *sprite, RoomMatrix *matrix) void move_right(Sprite *sprite, RoomMatrix *matrix)
{ {
sprite->textures[0]->clip.y = 32; sprite->clip.y = 32;
sprite->pos.x += TILE_DIMENSION; sprite->pos.x += TILE_DIMENSION;
if (has_collided(sprite, matrix)) if (has_collided(sprite, matrix))
sprite->pos.x -= TILE_DIMENSION; sprite->pos.x -= TILE_DIMENSION;
@ -42,7 +42,7 @@ void move_right(Sprite *sprite, RoomMatrix *matrix)
static static
void move_up(Sprite *sprite, RoomMatrix *matrix) void move_up(Sprite *sprite, RoomMatrix *matrix)
{ {
sprite->textures[0]->clip.y = 48; sprite->clip.y = 48;
sprite->pos.y -= TILE_DIMENSION; sprite->pos.y -= TILE_DIMENSION;
if (has_collided(sprite, matrix)) if (has_collided(sprite, matrix))
sprite->pos.y += TILE_DIMENSION; sprite->pos.y += TILE_DIMENSION;
@ -51,7 +51,7 @@ void move_up(Sprite *sprite, RoomMatrix *matrix)
static static
void move_down(Sprite *sprite, RoomMatrix *matrix) void move_down(Sprite *sprite, RoomMatrix *matrix)
{ {
sprite->textures[0]->clip.y = 0; sprite->clip.y = 0;
sprite->pos.y += TILE_DIMENSION; sprite->pos.y += TILE_DIMENSION;
if (has_collided(sprite, matrix)) if (has_collided(sprite, matrix))
sprite->pos.y -= TILE_DIMENSION; sprite->pos.y -= TILE_DIMENSION;
@ -81,7 +81,7 @@ void handle_player_input(Sprite *sprite, RoomMatrix *matrix, SDL_Event *event)
move_down(sprite, matrix); move_down(sprite, matrix);
break; break;
} }
sprite->textures[0]->clip.x = 16*step; sprite->clip.x = 16*step;
if (step == 3) if (step == 3)
step = 0; step = 0;
else else
@ -121,7 +121,7 @@ player_create(class_t class, SDL_Renderer *renderer)
sprite_load_texture(player->sprite, asset, 0, renderer); sprite_load_texture(player->sprite, asset, 0, renderer);
player->sprite->pos = (Position) { TILE_DIMENSION, TILE_DIMENSION }; player->sprite->pos = (Position) { TILE_DIMENSION, TILE_DIMENSION };
player->sprite->textures[0]->clip = (SDL_Rect) { 0, 0, 16, 16 }; player->sprite->clip = (SDL_Rect) { 0, 0, 16, 16 };
player->sprite->textures[0]->dim = (Dimension) { player->sprite->textures[0]->dim = (Dimension) {
TILE_DIMENSION, TILE_DIMENSION }; TILE_DIMENSION, TILE_DIMENSION };
player->sprite->handle_event = &handle_player_input; player->sprite->handle_event = &handle_player_input;

View File

@ -7,7 +7,14 @@ Sprite* sprite_create_default()
Position pos = { 0, 0 }; Position pos = { 0, 0 };
Sprite *s = ec_malloc(sizeof(Sprite)); Sprite *s = ec_malloc(sizeof(Sprite));
*s = (Sprite) { { NULL, NULL }, false, pos, NULL, 0, NULL }; *s = (Sprite) {
{ NULL, NULL },
(SDL_Rect) { 0, 0, 16, 16 },
false, pos,
NULL,
0,
NULL
};
s->renderTimer = timer_create(); s->renderTimer = timer_create();
@ -62,7 +69,10 @@ void sprite_render(Sprite *s, Camera *cam)
} }
Position cameraPos = camera_to_camera_position(cam, &s->pos); Position cameraPos = camera_to_camera_position(cam, &s->pos);
texture_render(s->textures[s->texture_index], &cameraPos, cam); texture_render_clip(s->textures[s->texture_index],
&cameraPos,
&s->clip,
cam);
} }
void sprite_destroy(Sprite *sprite) void sprite_destroy(Sprite *sprite)

View File

@ -11,6 +11,7 @@
typedef struct Sprite_t { typedef struct Sprite_t {
Texture* textures[2]; Texture* textures[2];
SDL_Rect clip;
bool destroyTextures; bool destroyTextures;
Position pos; Position pos;
Timer *renderTimer; Timer *renderTimer;

View File

@ -16,10 +16,6 @@ Texture* texture_create(const char *path, SDL_Renderer *renderer)
newTexture = ec_malloc(sizeof(Texture)); newTexture = ec_malloc(sizeof(Texture));
newTexture->dim.height = surface->h; newTexture->dim.height = surface->h;
newTexture->dim.width = surface->w; newTexture->dim.width = surface->w;
newTexture->clip.x = 0;
newTexture->clip.y = 0;
newTexture->clip.w = surface->w;
newTexture->clip.h = surface->h;
newTexture->texture = SDL_CreateTextureFromSurface(renderer, newTexture->texture = SDL_CreateTextureFromSurface(renderer,
surface); surface);
if (newTexture->texture == NULL) if (newTexture->texture == NULL)
@ -45,9 +41,32 @@ texture_render(Texture *texture, Position *p, Camera *cam)
texture->dim.height texture->dim.height
}; };
SDL_Rect clip = (SDL_Rect) {
0,
0,
texture->dim.width,
texture->dim.height
};
SDL_RenderCopy(cam->renderer, SDL_RenderCopy(cam->renderer,
texture->texture, texture->texture,
&texture->clip, &clip,
&draw_box);
}
void
texture_render_clip(Texture *texture, Position *p, SDL_Rect *clip, Camera *cam)
{
SDL_Rect draw_box = (SDL_Rect) {
p->x,
p->y,
texture->dim.width,
texture->dim.height
};
SDL_RenderCopy(cam->renderer,
texture->texture,
clip,
&draw_box); &draw_box);
} }

View File

@ -9,13 +9,14 @@
typedef struct { typedef struct {
SDL_Texture *texture; SDL_Texture *texture;
Dimension dim; Dimension dim;
SDL_Rect clip;
} Texture; } Texture;
Texture* texture_create(const char *path, SDL_Renderer *renderer); Texture* texture_create(const char *path, SDL_Renderer *renderer);
void texture_render(Texture*, Position*, Camera*); void texture_render(Texture*, Position*, Camera*);
void texture_render_clip(Texture*, Position*, SDL_Rect*, Camera*);
void texture_destroy(Texture *texture); void texture_destroy(Texture *texture);
#endif // TEXTURE_H_ #endif // TEXTURE_H_