blobwarsAttrition/src/world/hud.c

405 lines
8.9 KiB
C
Raw Normal View History

2018-01-21 12:26:02 +01:00
/*
Copyright (C) 2018 Parallel Realities
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 2
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, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "hud.h"
2018-02-04 16:37:19 +01:00
static void drawHealth(void);
static void drawPower(void);
static void drawOxygen(void);
static void drawInventory(void);
2018-02-24 17:00:30 +01:00
static void drawBossHealth(void);
2018-02-04 16:37:19 +01:00
2018-01-21 12:26:02 +01:00
static int messageTime;
2018-02-06 09:16:30 +01:00
static int infoMessageTime;
2018-01-21 12:26:02 +01:00
static char message[MAX_DESCRIPTION_LENGTH];
static int messageType;
2018-02-04 08:50:54 +01:00
static SDL_Color messageColor;
2018-01-21 12:26:02 +01:00
static char infoMessage[MAX_DESCRIPTION_LENGTH];
2018-02-07 08:51:25 +01:00
static Texture *atlasTexture;
static Atlas *healthIcon;
static Atlas *powerIcon;
static Atlas *oxygenIcon;
2018-01-21 12:26:02 +01:00
void initHud(void)
{
2018-12-22 22:53:19 +01:00
messageTime = 0;
2018-01-21 12:26:02 +01:00
messageType = MSG_STANDARD;
2018-02-04 08:50:54 +01:00
strcpy(message, "");
messageColor = colors.white;
2018-02-07 08:51:25 +01:00
atlasTexture = getTexture("gfx/atlas/atlas.png");
healthIcon = getImageFromAtlas("gfx/hud/health.png");
powerIcon = getImageFromAtlas("gfx/hud/power.png");
oxygenIcon = getImageFromAtlas("gfx/hud/oxygen.png");
2018-01-21 12:26:02 +01:00
}
2018-02-02 20:10:12 +01:00
void doHud(void)
2018-01-21 12:26:02 +01:00
{
2018-02-06 09:16:30 +01:00
if (--messageTime <= 0)
2018-01-21 12:26:02 +01:00
{
messageType = MSG_STANDARD;
2018-01-21 12:26:02 +01:00
messageTime = 0;
2018-02-28 09:29:30 +01:00
strcpy(message, "");
2018-01-21 12:26:02 +01:00
}
2018-02-06 09:16:30 +01:00
if (--infoMessageTime <= 0)
{
infoMessageTime = 0;
}
2018-01-21 12:26:02 +01:00
}
2018-02-03 17:22:04 +01:00
void drawHud(void)
{
2018-02-06 09:16:30 +01:00
int x, y, h;
2018-02-04 08:50:54 +01:00
2018-02-04 16:37:19 +01:00
drawHealth();
drawPower();
drawOxygen();
2018-04-01 18:41:45 +02:00
drawText(10, 82, 16, TA_LEFT, colors.white, app.strings[ST_WEAPON], getWeaponName(world.bob->weaponType));
2018-02-19 20:15:07 +01:00
if (app.config.inventory)
{
drawInventory();
}
2018-02-04 10:50:07 +01:00
2018-02-24 17:00:30 +01:00
if (world.isBossActive)
{
drawBossHealth();
}
else if (messageTime > 0)
2018-02-04 08:50:54 +01:00
{
2018-12-22 22:53:19 +01:00
drawRect(0, app.config.winHeight - 32, app.config.winWidth, 32, 0, 0, 0, 200);
2018-02-04 08:50:54 +01:00
2018-12-22 22:53:19 +01:00
drawText(app.config.winWidth / 2, app.config.winHeight - 26, 16, TA_CENTER, messageColor, message);
2018-02-04 08:50:54 +01:00
}
2018-02-06 09:16:30 +01:00
if (infoMessageTime > 0)
{
2018-12-22 23:46:38 +01:00
app.textWidth = 500;
2018-02-06 09:16:30 +01:00
h = getWrappedTextHeight(infoMessage, 20) + 20;
2018-12-22 22:53:19 +01:00
drawRect((app.config.winWidth / 2) - 300, 40, 600, h, 0, 0, 0, 168);
drawOutlineRect((app.config.winWidth / 2) - 300, 40, 600, h, 192, 192, 192, 255);
drawText(app.config.winWidth / 2, 50, 20, TA_CENTER, colors.white, infoMessage);
2018-12-22 23:46:38 +01:00
app.textWidth = 0;
2018-02-06 09:16:30 +01:00
}
2018-02-04 08:50:54 +01:00
if (dev.debug)
{
x = -camera.x + world.bob->x + (world.bob->w / 2);
y = -camera.y + world.bob->y - world.bob->h;
drawText(x, y, 14, TA_CENTER, colors.white, "[%.0f, %.0f]", world.bob->x / MAP_TILE_SIZE, world.bob->y / MAP_TILE_SIZE);
}
2018-02-03 17:22:04 +01:00
}
2018-02-04 16:37:19 +01:00
static void drawHealth(void)
{
2018-04-14 17:54:45 +02:00
int w, health, healthMax;
2018-02-23 20:25:46 +01:00
2018-04-14 17:54:45 +02:00
health = MAX(0, world.bob->health);
healthMax = MIN(world.bob->healthMax, 50);
2018-02-04 16:37:19 +01:00
blitRect(atlasTexture->texture, 17, 17, &healthIcon->rect, 1);
2018-02-04 16:37:19 +01:00
w = healthMax * 12;
drawRect(35, 8, w, 18, 0, 64, 0, 255);
2018-04-14 17:54:45 +02:00
w *= ((health * 1.0) / world.bob->healthMax);
drawRect(35, 8, w, 18, 0, 225, 0, 255);
2018-04-14 17:54:45 +02:00
if (world.frameCounter % 60 < 30 && getPercent(health, world.bob->healthMax) <= 33)
2018-02-09 23:32:57 +01:00
{
drawRect(35, 8, w, 18, 255, 225, 255, 255);
2018-02-09 23:32:57 +01:00
}
w = healthMax * 12;
drawOutlineRect(35, 8, w, 18, 0, 0, 0, 255);
2018-04-14 17:54:45 +02:00
drawText(35 + w + 5, 7, 14, TA_LEFT, colors.white, "%d", health);
2018-02-04 16:37:19 +01:00
}
static void drawPower(void)
{
float w, powerMax;
powerMax = MIN(world.bob->powerMax, 50);
blitRect(atlasTexture->texture, 17, 42, &powerIcon->rect, 1);
w = powerMax * 12;
2018-02-04 16:37:19 +01:00
drawRect(35, 33, w, 18, 64, 32, 0, 255);
2018-02-04 16:37:19 +01:00
w *= (world.bob->power / world.bob->powerMax);
2018-02-04 16:37:19 +01:00
drawRect(35, 33, w, 18, 225, 112, 0, 255);
w = powerMax * 12;
drawOutlineRect(35, 33, w, 18, 0, 0, 0, 255);
drawText(35 + w + 5, 32, 14, TA_LEFT, colors.white, "%.1f", world.bob->power);
}
static void drawOxygen(void)
{
int w;
2018-02-04 16:37:19 +01:00
blitRect(atlasTexture->texture, 17, 67, &oxygenIcon->rect, 1);
w = MAX_OXYGEN / 5;
drawRect(35, 58, w, 18, 0, 32, 64, 255);
w = world.bob->oxygen / 5;
drawRect(35, 58, w, 18, 0, 112, 225, 255);
2018-02-18 12:52:40 +01:00
if (world.frameCounter % 60 < 30 && getPercent(world.bob->oxygen, MAX_OXYGEN) <= 33)
{
drawRect(35, 58, w, 18, 255, 225, 255, 255);
2018-02-18 12:52:40 +01:00
}
w = MAX_OXYGEN / 5;
drawOutlineRect(35, 58, w, 18, 0, 0, 0, 255);
2018-03-04 14:36:11 +01:00
drawText(35 + w + 5, 57, 14, TA_LEFT, colors.white, "%.1fs", (world.bob->oxygen * 1.0) / FPS);
}
static void drawInventory(void)
{
2018-02-07 08:51:25 +01:00
int x, y, i, size, mid;
float w, h, d;
2018-02-17 17:53:30 +01:00
SDL_Rect *r;
size = 45;
2018-02-07 08:51:25 +01:00
mid = size / 2;
2018-12-22 22:53:19 +01:00
x = app.config.winWidth - 350;
y = 5;
for (i = 0 ; i < MAX_ITEMS ; i++)
{
if (i > 0 && i % (MAX_ITEMS / 2) == 0)
{
y += (size + 5);
2018-12-22 22:53:19 +01:00
x = app.config.winWidth - 350;
}
drawRect(x, y, size, size, 0, 0, 0, 128);
drawOutlineRect(x, y, size, size, 255, 255, 255, 255);
2018-02-07 08:51:25 +01:00
if (world.bob->items[i] != NULL)
{
r = getCurrentFrame(world.bob->items[i]->sprite[0]);
2018-02-17 17:53:30 +01:00
w = r->w;
h = r->h;
2018-02-07 08:51:25 +01:00
if (w > 40 || h > 40)
{
d = 40;
d /= (w > h) ? w : h;
w *= d;
h *= d;
}
2018-02-07 08:51:25 +01:00
2018-02-17 17:53:30 +01:00
blitRectScaled(atlasTexture->texture, x + mid, y + mid, w, h, r, 1);
2018-02-07 08:51:25 +01:00
}
x += (size + 5);
}
2018-02-04 16:37:19 +01:00
}
2018-02-03 17:22:04 +01:00
2018-02-24 17:00:30 +01:00
static void drawBossHealth(void)
{
float percent;
2018-12-22 22:53:19 +01:00
int w, x;
2018-02-24 17:00:30 +01:00
percent = world.boss->health;
percent /= world.boss->healthMax;
w = MAX(500 * percent, 0);
2018-12-22 22:53:19 +01:00
x = (app.config.winWidth - 500) / 2;
x += 100;
drawRect(0, app.config.winHeight - 32, app.config.winWidth, 32, 0, 0, 0, 200);
2018-02-24 17:00:30 +01:00
2018-12-22 22:53:19 +01:00
drawText(x, app.config.winHeight - 28, 16, TA_RIGHT, colors.white, world.boss->name);
2018-02-24 17:00:30 +01:00
2018-12-22 22:53:19 +01:00
drawRect(x + 10, app.config.winHeight - 24, w, 16, 255, 0, 0, 255);
drawOutlineRect(x + 10, app.config.winHeight - 24, 500, 16, 192, 192, 192, 255);
2018-02-24 17:00:30 +01:00
}
2018-12-22 22:53:19 +01:00
void drawMissionStatus(int showFirePrompt)
2018-02-07 23:49:42 +01:00
{
Objective *o;
2018-05-02 08:55:55 +02:00
int y, x, w, h, size, mid, i, textSize, lineSpacing;
2018-02-07 23:49:42 +01:00
float rw, rh, d;
SDL_Color c;
2018-02-17 17:53:30 +01:00
SDL_Rect *r;
2018-02-07 23:49:42 +01:00
char *status;
2018-12-22 22:53:19 +01:00
drawRect(0, 0, app.config.winWidth, app.config.winHeight, 0, 0, 0, 128);
SDL_SetRenderTarget(app.renderer, app.uiBuffer);
2018-02-07 23:49:42 +01:00
w = 800;
2018-02-12 20:15:03 +01:00
h = 550;
2018-12-22 22:53:19 +01:00
x = (UI_WIDTH - w) / 2;
2018-02-07 23:49:42 +01:00
2018-12-22 22:53:19 +01:00
drawRect(x, (UI_HEIGHT - h) / 2, w, h, 0, 0, 0, 128);
drawOutlineRect(x, (UI_HEIGHT - h) / 2, w, h, 255, 255, 255, 200);
2018-02-07 23:49:42 +01:00
2018-12-22 22:53:19 +01:00
drawText(UI_WIDTH / 2, 100, 40, TA_CENTER, colors.white, app.strings[ST_OBJECTIVES]);
2018-02-07 23:49:42 +01:00
2018-02-12 20:15:03 +01:00
y = 180;
2018-05-02 08:55:55 +02:00
textSize = 24;
lineSpacing = 55;
if (world.numObjectives > 5)
{
textSize -= (world.numObjectives - 5) * 2;
lineSpacing -= (world.numObjectives - 5) * 8;
}
2018-02-07 23:49:42 +01:00
for (o = world.objectiveHead.next ; o != NULL ; o = o->next)
{
2018-02-08 09:21:37 +01:00
c = o->required ? colors.red : colors.white;
2018-04-01 18:41:45 +02:00
status = app.strings[ST_INCOMPLETE];
2018-02-07 23:49:42 +01:00
if (o->currentValue >= o->targetValue)
2018-02-07 23:49:42 +01:00
{
c = colors.green;
2018-04-01 18:41:45 +02:00
status = app.strings[ST_COMPLETE];
2018-02-07 23:49:42 +01:00
}
2018-05-02 08:55:55 +02:00
drawText(x + 20, y, textSize, TA_LEFT, c, o->description);
2018-12-22 22:53:19 +01:00
drawText(UI_WIDTH / 2 + 100, y, textSize, TA_LEFT, c, "%d / %d", MIN(o->currentValue, o->targetValue), o->targetValue);
2018-05-02 08:55:55 +02:00
drawText(x + w - 20, y, textSize, TA_RIGHT, c, status);
2018-02-07 23:49:42 +01:00
2018-05-02 08:55:55 +02:00
y += lineSpacing;
2018-02-07 23:49:42 +01:00
}
size = 60;
mid = size / 2;
2018-12-22 22:53:19 +01:00
y = (((UI_HEIGHT - h) / 2) + h) - 165;
2018-02-12 20:15:03 +01:00
2018-12-22 22:53:19 +01:00
x = ((UI_WIDTH - w) / 2) + 90;
2018-02-07 23:49:42 +01:00
for (i = 0 ; i < MAX_ITEMS ; i++)
{
if (i > 0 && i % (MAX_ITEMS / 2) == 0)
{
y += (size + 20);
2018-12-22 22:53:19 +01:00
x = ((UI_WIDTH - w) / 2) + 90;
2018-02-07 23:49:42 +01:00
}
drawRect(x, y, size, size, 0, 0, 0, 128);
drawOutlineRect(x, y, size, size, 255, 255, 255, 255);
if (world.bob->items[i] != NULL)
{
r = getCurrentFrame(world.bob->items[i]->sprite[0]);
2018-02-17 17:53:30 +01:00
rw = r->w;
rh = r->h;
2018-02-07 23:49:42 +01:00
if (rw > 40 || rh > 40)
{
d = 40;
d /= (rw > rh) ? rw : rh;
rw *= d;
rh *= d;
}
2018-02-07 23:49:42 +01:00
2018-02-17 17:53:30 +01:00
blitRectScaled(atlasTexture->texture, x + mid, y + mid, rw, rh, r, 1);
2018-02-07 23:49:42 +01:00
if (world.bob->items[i]->value > 0)
{
drawText(x + size - 5, y, 14, TA_RIGHT, colors.white, "%d", world.bob->items[i]->value);
}
2018-02-07 23:49:42 +01:00
}
x += (size + 30);
}
2018-12-22 22:53:19 +01:00
if (showFirePrompt)
{
drawText(UI_WIDTH / 2, UI_HEIGHT - 80, 24, TA_CENTER, colors.white, app.strings[ST_PRESS_FIRE]);
}
SDL_SetRenderTarget(app.renderer, app.backBuffer);
2018-02-07 23:49:42 +01:00
}
2018-02-03 14:07:56 +01:00
void setGameplayMessage(int newMessageType, const char *format, ...)
2018-01-21 12:26:02 +01:00
{
2018-02-03 14:07:56 +01:00
char newMessage[MAX_DESCRIPTION_LENGTH];
va_list args;
memset(&newMessage, '\0', sizeof(newMessage));
va_start(args, format);
vsprintf(newMessage, format, args);
va_end(args);
2018-02-25 13:36:12 +01:00
if (strlen(newMessage) > 0 && newMessageType >= messageType && strcmp(message, newMessage))
2018-01-21 12:26:02 +01:00
{
STRNCPY(message, newMessage, MAX_DESCRIPTION_LENGTH);
messageType = newMessageType;
messageTime = FPS * 3;
2018-02-03 17:22:04 +01:00
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "%s", message);
2018-02-04 08:50:54 +01:00
switch (messageType)
{
case MSG_STANDARD:
case MSG_GAMEPLAY:
messageColor = colors.white;
break;
case MSG_PROGRESS:
messageColor = colors.cyan;
break;
case MSG_OBJECTIVE:
messageColor = colors.green;
break;
}
2018-01-21 12:26:02 +01:00
}
}
void showInfoMessage(char *newInfoMessage)
{
STRNCPY(infoMessage, newInfoMessage, MAX_DESCRIPTION_LENGTH);
2018-02-06 09:16:30 +01:00
infoMessageTime = FPS / 4;
2018-01-21 12:26:02 +01:00
}