94 lines
3.1 KiB
C++
Raw Normal View History

2024-07-05 02:33:29 -04:00
#include <vector>
#include <iomanip>
#include <sstream>
#include <symbols/minecraft.h>
#include <libreborn/libreborn.h>
#include <mods/misc/misc.h>
#include <mods/feature/feature.h>
#include <mods/input/input.h>
#include <mods/version/version.h>
#include <mods/fps/fps.h>
#include <mods/init/init.h>
// Get Debug Information
static std::string to_string_with_precision(const double x, const int precision) {
std::stringstream stream;
stream << std::fixed << std::setprecision(precision) << x;
return stream.str();
}
static int debug_precision = 3;
static std::vector<std::string> get_debug_info(const Minecraft *minecraft) {
std::vector<std::string> info;
// Version
info.push_back(std::string("MCPI ") + version_get());
// FPS
info.push_back("FPS: " + to_string_with_precision(fps, debug_precision));
2024-09-30 00:45:53 -04:00
// Seed
if (minecraft->level) {
info.push_back("");
info.push_back("Seed: " + std::to_string(minecraft->level->data.seed));
}
2024-07-05 02:33:29 -04:00
// X/Y/Z
if (minecraft->player) {
info.push_back("");
float x = minecraft->player->x;
float y = minecraft->player->y - minecraft->player->height_offset;
float z = minecraft->player->z;
2024-07-15 03:05:05 -04:00
minecraft->command_server->pos_translator.to(x, y, z);
2024-07-05 02:33:29 -04:00
info.push_back("X: " + to_string_with_precision(x, debug_precision));
info.push_back("Y: " + to_string_with_precision(y, debug_precision));
info.push_back("Z: " + to_string_with_precision(z, debug_precision));
}
// Return
return info;
}
// Render Text With Background
2024-09-22 19:28:51 -04:00
static constexpr uint32_t debug_background_color = 0x90505050;
static constexpr int debug_text_color = 0xe0e0e0;
2024-07-05 02:33:29 -04:00
static void render_debug_line(Gui *gui, std::string &line, const int x, const int y) {
// Draw Background
2024-08-22 23:43:32 -04:00
const int width = gui->minecraft->font->width(line);
2024-07-05 02:33:29 -04:00
if (width == 0) {
return;
}
2024-09-30 13:13:02 -04:00
int x1 = x - 1;
int y1 = y - 1;
int x2 = x + width;
int y2 = y + line_height;
gui->fill(x1, y1, x2, y2, debug_background_color);
2024-07-05 02:33:29 -04:00
// Draw Text
2024-07-15 03:05:05 -04:00
gui->minecraft->font->draw(line, float(x), float(y), debug_text_color);
2024-07-05 02:33:29 -04:00
}
// Draw Debug Information
static bool debug_info_shown = false;
2024-09-22 19:28:51 -04:00
static constexpr int debug_margin = 2;
static constexpr int debug_line_padding = 1;
2024-07-05 02:33:29 -04:00
static void Gui_renderDebugInfo_injection(__attribute__((unused)) Gui_renderDebugInfo_t original, Gui *self) {
if (debug_info_shown) {
std::vector<std::string> info = get_debug_info(self->minecraft);
int y = debug_margin;
for (std::string &line : info) {
render_debug_line(self, line, debug_margin, y);
y += line_height;
y += debug_line_padding;
}
}
}
// Init
void init_f3() {
if (feature_has("F3 Debug Information", server_disabled)) {
overwrite_calls(Gui_renderDebugInfo, Gui_renderDebugInfo_injection);
2024-09-22 21:21:53 -04:00
misc_run_on_game_key_press([](__attribute__((unused)) Minecraft *minecraft, const int key) {
2024-07-05 02:33:29 -04:00
if (key == MC_KEY_F3) {
debug_info_shown = !debug_info_shown;
return true;
} else {
return false;
}
});
}
}