Moved text sprite logic to sprite_util

This commit is contained in:
Linus Probert 2019-03-04 20:32:09 +01:00
parent 06ca6c3e1e
commit c6a022e2db
8 changed files with 149 additions and 44 deletions

View File

@ -177,6 +177,7 @@ add_executable(breakhack
src/texture
src/screenresolution
src/sprite
src/sprite_util
src/util
src/player
src/map

View File

@ -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);
}

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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;

59
src/sprite_util.c Normal file
View File

@ -0,0 +1,59 @@
/*
* BreakHack - A dungeone crawler RPG
* Copyright (C) 2018 Linus Probert <linus.probert@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#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;
}

32
src/sprite_util.h Normal file
View File

@ -0,0 +1,32 @@
/*
* BreakHack - A dungeone crawler RPG
* Copyright (C) 2018 Linus Probert <linus.probert@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#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,
...);