From c6a022e2db951215dcbf294950d53e03bcfe74ff Mon Sep 17 00:00:00 2001 From: Linus Probert Date: Mon, 4 Mar 2019 20:32:09 +0100 Subject: [PATCH] Moved text sprite logic to sprite_util --- CMakeLists.txt | 1 + src/artifact.c | 26 ++++++++++++++++++++ src/artifact.h | 5 ++++ src/item_builder.c | 57 ++++++++++---------------------------------- src/main.c | 2 ++ src/player.c | 11 +++++++++ src/sprite_util.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++ src/sprite_util.h | 32 +++++++++++++++++++++++++ 8 files changed, 149 insertions(+), 44 deletions(-) create mode 100644 src/sprite_util.c create mode 100644 src/sprite_util.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 02e1f7f..1ccbbe9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -177,6 +177,7 @@ add_executable(breakhack src/texture src/screenresolution src/sprite + src/sprite_util src/util src/player src/map diff --git a/src/artifact.c b/src/artifact.c index b2d8a6c..6459f2b 100644 --- a/src/artifact.c +++ b/src/artifact.c @@ -113,6 +113,26 @@ artifact_create_random(Player *p, Uint8 level) return a; } +void +artifact_add_price(Artifact *a, unsigned int price, SDL_Renderer *renderer) +{ + + Sprite *sprite = sprite_create(); + sprite_load_text_texture(sprite, "GUI/SDS_8x8.ttf", 0, 8, 1); + char priceLabel[9]; + m_sprintf(priceLabel, 9, "$%d", price); + texture_load_from_text(sprite->textures[0], + priceLabel, + C_YELLOW, + C_BLACK, + renderer); + + sprite->dim = sprite->textures[0]->dim; + + a->price = price; + a->priceSprite = sprite; +} + Sprite * artifact_sprite_for(MagicalEffect effect) { @@ -175,9 +195,11 @@ artifact_create(MagicalEffect effect) { Artifact *a = ec_malloc(sizeof(Artifact)); a->sprite = artifact_sprite_for(effect); + a->priceSprite = NULL; a->sprite->dim = GAME_DIMENSION; a->collected = false; a->level = 1; + a->price = 0; artifact_set_effect(a, effect); return a; } @@ -198,11 +220,15 @@ artifact_render(Artifact *a, Camera *cam) pos.x += 4; pos.y += 4; particle_engine_sparkle(pos, (Dimension) { 24, 24 }, C_PURPLE, false); + if (a->priceSprite) + sprite_render(a->priceSprite, cam); } void artifact_destroy(Artifact *a) { sprite_destroy(a->sprite); + if (a->priceSprite) + sprite_destroy(a->priceSprite); free(a); } diff --git a/src/artifact.h b/src/artifact.h index 511c3b0..17f841f 100644 --- a/src/artifact.h +++ b/src/artifact.h @@ -40,10 +40,12 @@ typedef struct ArtifactInfo { typedef struct Artifact { Sprite *sprite; + Sprite *priceSprite; MagicalEffect effect; ArtifactInfo info; bool collected; int level; + unsigned int price; } Artifact; Sprite * @@ -52,6 +54,9 @@ artifact_sprite_for(MagicalEffect); Artifact * artifact_create_random(Player*, Uint8 level); +void +artifact_add_price(Artifact*, unsigned int price, SDL_Renderer *renderer); + Artifact * artifact_create(MagicalEffect); diff --git a/src/item_builder.c b/src/item_builder.c index 9e0043e..4ebad69 100644 --- a/src/item_builder.c +++ b/src/item_builder.c @@ -27,6 +27,7 @@ #include "random.h" #include "texturecache.h" #include "sprite.h" +#include "sprite_util.h" static ItemBuilder *builder = NULL; @@ -79,48 +80,6 @@ pickup_dagger(Item *item, Player *player) gui_log("You collect a dagger"); } -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[9]; - m_sprintf(priceLabel, 9, format, value); - texture_load_from_text(sprite->textures[0], - priceLabel, - fg, - outline, - builder->renderer); - - sprite->dim = sprite->textures[0]->dim; - - return sprite; -} - -static Sprite * -create_small_number_subsprite(SDL_Color fg, - SDL_Color outline, - const char *format, - double value) -{ - Sprite *sprite = sprite_create(); - sprite_load_text_texture(sprite, "GUI/SDS_6x6.ttf", 0, 6, 1); - char priceLabel[9]; - m_sprintf(priceLabel, 9, 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_item(const char *path0, const char *path1, SDL_Rect clip, void (*cb)(Item*, Player*)) { @@ -152,7 +111,12 @@ create_priced_item(double price, Item *item = create_item(path0, path1, clip, cb); item->price = price; - Sprite *priceSprite = create_number_subsprite(C_YELLOW, C_BLACK, "$.0f", item->price); + Sprite *priceSprite = sprite_util_create_text_sprite("GUI/SDS_8x8.ttf", + 8, + C_YELLOW, + C_BLACK, + "$%.0f", + price); linkedlist_append(&item->subsprites, priceSprite); return item; @@ -264,7 +228,12 @@ item_builder_build_item(ItemKey key, int level) } if (item->value != 1) { - Sprite *valueSprite = create_small_number_subsprite(C_BLUE, C_BLACK, "%g", item->value); + Sprite *valueSprite = sprite_util_create_text_sprite("GUI/SDS_8x8.ttf", + 8, + C_BLUE, + C_BLACK, + "%g", + 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); diff --git a/src/main.c b/src/main.c index 1759779..c873b58 100644 --- a/src/main.c +++ b/src/main.c @@ -55,6 +55,7 @@ #include "tooltip.h" #include "gamecontroller.h" #include "time.h" +#include "sprite_util.h" #ifdef STEAM_BUILD #include "steam/steamworks_api_wrapper.h" @@ -308,6 +309,7 @@ initGame(void) initViewports(0); input_init(&input); texturecache_init(gRenderer); + sprite_util_init(gRenderer); gCamera = camera_create(gRenderer); gRoomMatrix = roommatrix_create(); gGui = gui_create(gCamera); diff --git a/src/player.c b/src/player.c index 3b08a42..c6acd29 100644 --- a/src/player.c +++ b/src/player.c @@ -751,9 +751,20 @@ player_has_artifact(Player *p, MagicalEffect effect) void player_add_artifact(Player *p, Artifact *a) { + if (a->price > p->gold) { + gui_log("You don't have enough gold to buy a %s", + a->info.name); + return; + } + if (a->collected) return; + if (a->price) { + gui_log("You pay %d gold for %s", a->price, a->info.name); + p->gold -= a->price; + } + mixer_play_effect(MAGIC_PICKUP); a->collected = true; diff --git a/src/sprite_util.c b/src/sprite_util.c new file mode 100644 index 0000000..35cf9c3 --- /dev/null +++ b/src/sprite_util.c @@ -0,0 +1,59 @@ +/* + * BreakHack - A dungeone crawler RPG + * Copyright (C) 2018 Linus Probert + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "sprite_util.h" +#include "util.h" + +static struct { + SDL_Renderer *renderer; +} utilData; + +void +sprite_util_init(SDL_Renderer *renderer) +{ + utilData.renderer = renderer; +} + +Sprite * +sprite_util_create_text_sprite(const char *fontPath, + int size, + SDL_Color fg, + SDL_Color outline, + const char *fmt, + ...) +{ + va_list args; + Sprite *sprite = sprite_create(); + + sprite_load_text_texture(sprite, fontPath, 0, size, 1); + char priceLabel[10]; + + va_start(args, fmt); + m_vsprintf(priceLabel, 10, fmt, args); + va_end(args); + + texture_load_from_text(sprite->textures[0], + priceLabel, + fg, + outline, + utilData.renderer); + + sprite->dim = sprite->textures[0]->dim; + + return sprite; +} diff --git a/src/sprite_util.h b/src/sprite_util.h new file mode 100644 index 0000000..ed21948 --- /dev/null +++ b/src/sprite_util.h @@ -0,0 +1,32 @@ +/* + * BreakHack - A dungeone crawler RPG + * Copyright (C) 2018 Linus Probert + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "sprite.h" + +void +sprite_util_init(SDL_Renderer *renderer); + +Sprite * +sprite_util_create_text_sprite(const char *fontPath, + int size, + SDL_Color fg, + SDL_Color outline, + const char *fmt, + ...); + +