diff --git a/src/item_builder.c b/src/item_builder.c index 8de1d13..b5dc442 100644 --- a/src/item_builder.c +++ b/src/item_builder.c @@ -100,23 +100,45 @@ create_item(const char *path0, const char *path1, SDL_Rect clip, void (*cb)(Item return item; } +static Sprite * +create_number_subsprite(SDL_Color fg, + SDL_Color outline, + const char *format, + double value) +{ + Sprite *sprite = sprite_create(); + sprite_load_text_texture(sprite, "GUI/SDS_8x8.ttf", 0, 8, 1); + char priceLabel[10]; + m_sprintf(priceLabel, 10, format, value); + texture_load_from_text(sprite->textures[0], + priceLabel, + fg, + outline, + builder->renderer); + + sprite->dim = sprite->textures[0]->dim; + + return sprite; +} + static Item * -create_priced_item(double price, const char *path0, const char *path1, SDL_Rect clip, void (*cb)(Item *, Player*)) +create_priced_item(double price, + const char *path0, + const char *path1, + SDL_Rect clip, + void (*cb)(Item *, Player*)) { Item *item = create_item(path0, path1, clip, cb); item->price = price; - Sprite *priceSprite = sprite_create(); - sprite_load_text_texture(priceSprite, "GUI/SDS_8x8.ttf", 0, 8, 1); - char priceLabel[10]; - m_sprintf(priceLabel, 10, "$%.0f", item->price); - texture_load_from_text(priceSprite->textures[0], - priceLabel, - C_YELLOW, - C_BLACK, - builder->renderer); - priceSprite->dim = priceSprite->textures[0]->dim; + Sprite *priceSprite = create_number_subsprite(C_YELLOW, C_BLACK, "$%.0f", item->price); linkedlist_append(&item->subsprites, priceSprite); + + Sprite *valueSprite = create_number_subsprite(C_BLUE, C_BLACK, "$.1f", item->value); + valueSprite->offset.x = item->sprite->dim.width - valueSprite->dim.width; + valueSprite->offset.y = item->sprite->dim.height - valueSprite->dim.height; + linkedlist_append(&item->subsprites, valueSprite); + return item; } diff --git a/src/position.c b/src/position.c index 3c2721f..f939221 100644 --- a/src/position.c +++ b/src/position.c @@ -109,3 +109,9 @@ position_to_tile_pos(const Position *p) p->y - (p->y % TILE_DIMENSION) }; } + +Position +position_add(const Position *a, const Position *b) +{ + return POS(a->x + b->x, a->y + b->y); +} diff --git a/src/position.h b/src/position.h index 330af4a..7f24991 100644 --- a/src/position.h +++ b/src/position.h @@ -71,4 +71,10 @@ position_in_roommatrix(const Position*); Position position_to_tile_pos(const Position*); +/* + * Add two positions + */ +Position +position_add(const Position*, const Position*); + #endif // POSITION_H_ diff --git a/src/sprite.c b/src/sprite.c index 835a3a2..fa00986 100644 --- a/src/sprite.c +++ b/src/sprite.c @@ -30,7 +30,8 @@ sprite_create_default(void) s->textures[1] = NULL; s->clip = (SDL_Rect) { 0, 0, 0, 0 }; s->destroyTextures = false; - s->pos = (Position) { 0, 0 }; + s->pos = POS(0, 0); + s->offset = POS(0, 0); s->dim = DEFAULT_DIMENSION; s->angle = 0; s->rotationPoint = (SDL_Point) { 0, 0 }; @@ -169,6 +170,8 @@ sprite_render(Sprite *s, Camera *cam) else cameraPos = s->pos; + cameraPos = position_add(&cameraPos, &s->offset); + SDL_Rect box = { cameraPos.x, cameraPos.y, s->dim.width, s->dim.height }; diff --git a/src/sprite.h b/src/sprite.h index c72f6b2..05a3670 100644 --- a/src/sprite.h +++ b/src/sprite.h @@ -41,6 +41,7 @@ typedef struct Sprite { SDL_Rect clip; bool destroyTextures; Position pos; + Position offset; Dimension dim; double angle; SDL_Point rotationPoint;