Moved 'clip' from texture to sprite.
This commit is contained in:
parent
80ab804e07
commit
c4d142860c
4
TODO.txt
4
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
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -8,7 +8,6 @@
|
|||
typedef struct {
|
||||
Sprite *sprite;
|
||||
Stats stats;
|
||||
SDL_Rect clip;
|
||||
} Monster;
|
||||
|
||||
Monster* monster_create();
|
||||
|
|
12
src/player.c
12
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;
|
||||
|
|
14
src/sprite.c
14
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)
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
|
||||
typedef struct Sprite_t {
|
||||
Texture* textures[2];
|
||||
SDL_Rect clip;
|
||||
bool destroyTextures;
|
||||
Position pos;
|
||||
Timer *renderTimer;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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_
|
||||
|
|
Loading…
Reference in New Issue