Add value sprite to items.

Completely untested
This commit is contained in:
Linus Probert 2019-03-04 15:00:35 +01:00
parent 486c1bb641
commit 959fae1730
5 changed files with 50 additions and 12 deletions

View File

@ -100,23 +100,45 @@ create_item(const char *path0, const char *path1, SDL_Rect clip, void (*cb)(Item
return 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 * 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 *item = create_item(path0, path1, clip, cb);
item->price = price; 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); 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; return item;
} }

View File

@ -109,3 +109,9 @@ position_to_tile_pos(const Position *p)
p->y - (p->y % TILE_DIMENSION) 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);
}

View File

@ -71,4 +71,10 @@ position_in_roommatrix(const Position*);
Position Position
position_to_tile_pos(const Position*); position_to_tile_pos(const Position*);
/*
* Add two positions
*/
Position
position_add(const Position*, const Position*);
#endif // POSITION_H_ #endif // POSITION_H_

View File

@ -30,7 +30,8 @@ sprite_create_default(void)
s->textures[1] = NULL; s->textures[1] = NULL;
s->clip = (SDL_Rect) { 0, 0, 0, 0 }; s->clip = (SDL_Rect) { 0, 0, 0, 0 };
s->destroyTextures = false; s->destroyTextures = false;
s->pos = (Position) { 0, 0 }; s->pos = POS(0, 0);
s->offset = POS(0, 0);
s->dim = DEFAULT_DIMENSION; s->dim = DEFAULT_DIMENSION;
s->angle = 0; s->angle = 0;
s->rotationPoint = (SDL_Point) { 0, 0 }; s->rotationPoint = (SDL_Point) { 0, 0 };
@ -169,6 +170,8 @@ sprite_render(Sprite *s, Camera *cam)
else else
cameraPos = s->pos; cameraPos = s->pos;
cameraPos = position_add(&cameraPos, &s->offset);
SDL_Rect box = { SDL_Rect box = {
cameraPos.x, cameraPos.y, s->dim.width, s->dim.height cameraPos.x, cameraPos.y, s->dim.width, s->dim.height
}; };

View File

@ -41,6 +41,7 @@ typedef struct Sprite {
SDL_Rect clip; SDL_Rect clip;
bool destroyTextures; bool destroyTextures;
Position pos; Position pos;
Position offset;
Dimension dim; Dimension dim;
double angle; double angle;
SDL_Point rotationPoint; SDL_Point rotationPoint;