JacksEscape/mapper_misc_render.cpp
2024-04-16 20:29:38 +08:00

78 lines
1.8 KiB
C++

#include "mapper_misc.h"
#include "app.h"
#include "entity.h"
#include "render_util.h"
#include "util/assert.h"
#include <stdio.h>
#include <math.h>
#include <easyx.h>
#ifdef __MINGW32__
#define NCHAR char
#else
#define NCHAR wchar_t
#endif
namespace {
static vector_Vector *tbuf;
void convert_text(const char *str) {
if (!tbuf)
tbuf = vector_Create(sizeof(NCHAR));
const NCHAR zero = 0;
vector_Clear(tbuf);
int len = strlen(str);
int i = 0;
while (i < len) {
NCHAR wc = str[i];
vector_Push(tbuf, &wc);
i++;
}
vector_Push(tbuf, &zero);
}
} // namespace
extern "C" void misc_render_Textbox(App *app, Entity *e, Vec2 entity_screen_pos, void *user) {
if (!e->misc || !e->misc->textbox) {
WARN("called on an entity without misc or misc.textbox", 0);
return;
}
misc_Textbox *t = e->misc->textbox;
// If we're too dim to see, quit early
if (t->progress < EPS)
return;
// Set the color
int rgb = (int)round(fminf(t->progress, 1.0f) * 255.0);
settextcolor(RGB(rgb, rgb, rgb));
// Fade the background from App background to Black
int r = app->clear_color & 0xff;
int g = (app->clear_color & 0xff00) >> 8;
int b = (app->clear_color & 0xff0000) >> 16;
setbkcolor(RGB((int)roundf(r * (1.0f - t->progress)), (int)roundf(g * (1.0f - t->progress)), (int)roundf(b * (1.0f - t->progress))));
// Compute the bounding rect
RECT rect;
ASSERT(e->position && "Textboxes must have a position component");
rect.left = (LONG)round(entity_screen_pos.x) - 20;
rect.right = (LONG)round(entity_screen_pos.x) + 20;
rect.top = (LONG)round(entity_screen_pos.y) + t->offset * 0.8;
// Convert & draw
// https://docs.easyx.cn/zh-cn/drawtext
convert_text(t->text);
drawtext((LPCTSTR)vector_Data(tbuf), &rect, DT_CENTER | DT_NOCLIP);
// Restore background to Black
setbkcolor(0);
}