diff --git a/TODO.txt b/TODO.txt index 942568f..a538234 100644 --- a/TODO.txt +++ b/TODO.txt @@ -4,8 +4,8 @@ x Add enemies (generated through lua) x Monster object created x Lua bindings for creation x Making some better generation and randomeness -- Move "texture clip" from texture to sprite -- Hitting enemies +x Move "clip" from texture to sprite +o Hitting enemies - Moving enemies - Lower levels - XP diff --git a/src/map_lua.c b/src/map_lua.c index 7958ea5..f51b4b4 100644 --- a/src/map_lua.c +++ b/src/map_lua.c @@ -182,7 +182,7 @@ l_add_monster(lua_State *L) lua_pop(L, 4); 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 }; sprite_set_texture(monster->sprite, texture1, 0); sprite_set_texture(monster->sprite, texture2, 1); diff --git a/src/monster.c b/src/monster.c index 91095b7..c60bd70 100644 --- a/src/monster.c +++ b/src/monster.c @@ -6,15 +6,13 @@ monster_create() { Monster *m = ec_malloc(sizeof(Monster)); m->sprite = sprite_create(); - m->clip = (SDL_Rect) { 0, 0, 16, 16 }; + m->sprite->clip = (SDL_Rect) { 0, 0, 16, 16 }; return m; } void 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); } diff --git a/src/monster.h b/src/monster.h index 0ef9e6d..6cbd3ae 100644 --- a/src/monster.h +++ b/src/monster.h @@ -8,7 +8,6 @@ typedef struct { Sprite *sprite; Stats stats; - SDL_Rect clip; } Monster; Monster* monster_create(); diff --git a/src/player.c b/src/player.c index 8e95471..e6f8b9a 100644 --- a/src/player.c +++ b/src/player.c @@ -24,7 +24,7 @@ has_collided(Sprite *sprite, RoomMatrix *matrix) static void move_left(Sprite *sprite, RoomMatrix *matrix) { - sprite->textures[0]->clip.y = 16; + sprite->clip.y = 16; sprite->pos.x -= TILE_DIMENSION; if (has_collided(sprite, matrix)) sprite->pos.x += TILE_DIMENSION; @@ -33,7 +33,7 @@ move_left(Sprite *sprite, RoomMatrix *matrix) static void move_right(Sprite *sprite, RoomMatrix *matrix) { - sprite->textures[0]->clip.y = 32; + sprite->clip.y = 32; sprite->pos.x += TILE_DIMENSION; if (has_collided(sprite, matrix)) sprite->pos.x -= TILE_DIMENSION; @@ -42,7 +42,7 @@ void move_right(Sprite *sprite, RoomMatrix *matrix) static void move_up(Sprite *sprite, RoomMatrix *matrix) { - sprite->textures[0]->clip.y = 48; + sprite->clip.y = 48; sprite->pos.y -= TILE_DIMENSION; if (has_collided(sprite, matrix)) sprite->pos.y += TILE_DIMENSION; @@ -51,7 +51,7 @@ void move_up(Sprite *sprite, RoomMatrix *matrix) static void move_down(Sprite *sprite, RoomMatrix *matrix) { - sprite->textures[0]->clip.y = 0; + sprite->clip.y = 0; sprite->pos.y += TILE_DIMENSION; if (has_collided(sprite, matrix)) sprite->pos.y -= TILE_DIMENSION; @@ -81,7 +81,7 @@ void handle_player_input(Sprite *sprite, RoomMatrix *matrix, SDL_Event *event) move_down(sprite, matrix); break; } - sprite->textures[0]->clip.x = 16*step; + sprite->clip.x = 16*step; if (step == 3) step = 0; else @@ -121,7 +121,7 @@ player_create(class_t class, SDL_Renderer *renderer) sprite_load_texture(player->sprite, asset, 0, renderer); 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) { TILE_DIMENSION, TILE_DIMENSION }; player->sprite->handle_event = &handle_player_input; diff --git a/src/sprite.c b/src/sprite.c index deff83a..3afe6a5 100644 --- a/src/sprite.c +++ b/src/sprite.c @@ -7,7 +7,14 @@ Sprite* sprite_create_default() Position pos = { 0, 0 }; 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(); @@ -62,7 +69,10 @@ void sprite_render(Sprite *s, Camera *cam) } 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) diff --git a/src/sprite.h b/src/sprite.h index 89bdd03..33c0ea6 100644 --- a/src/sprite.h +++ b/src/sprite.h @@ -11,6 +11,7 @@ typedef struct Sprite_t { Texture* textures[2]; + SDL_Rect clip; bool destroyTextures; Position pos; Timer *renderTimer; diff --git a/src/texture.c b/src/texture.c index 08b44de..767c2c3 100644 --- a/src/texture.c +++ b/src/texture.c @@ -16,10 +16,6 @@ Texture* texture_create(const char *path, SDL_Renderer *renderer) newTexture = ec_malloc(sizeof(Texture)); newTexture->dim.height = surface->h; 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, surface); if (newTexture->texture == NULL) @@ -45,9 +41,32 @@ texture_render(Texture *texture, Position *p, Camera *cam) texture->dim.height }; + SDL_Rect clip = (SDL_Rect) { + 0, + 0, + texture->dim.width, + texture->dim.height + }; + SDL_RenderCopy(cam->renderer, 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); } diff --git a/src/texture.h b/src/texture.h index f2e7230..a1249e6 100644 --- a/src/texture.h +++ b/src/texture.h @@ -9,13 +9,14 @@ typedef struct { SDL_Texture *texture; Dimension dim; - SDL_Rect clip; } Texture; Texture* texture_create(const char *path, SDL_Renderer *renderer); void texture_render(Texture*, Position*, Camera*); +void texture_render_clip(Texture*, Position*, SDL_Rect*, Camera*); + void texture_destroy(Texture *texture); #endif // TEXTURE_H_