ConsoleHelper.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright 2009 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <iostream>
  5. #include <stdarg.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #define CON_BLACK 0
  9. #define CON_RED 1
  10. #define CON_GREEN 2
  11. #define CON_YELLOW 3
  12. #define CON_BLUE 4
  13. #define CON_MAGENTA 5
  14. #define CON_CYAN 6
  15. #define CON_WHITE 7
  16. #define CON_BRIGHT 8
  17. #define CON_BRIGHT_BLACK CON_BLACK | CON_BRIGHT
  18. #define CON_BRIGHT_RED CON_RED | CON_BRIGHT
  19. #define CON_BRIGHT_GREEN CON_GREEN | CON_BRIGHT
  20. #define CON_BRIGHT_YELLOW CON_YELLOW | CON_BRIGHT
  21. #define CON_BRIGHT_BLUE CON_BLUE | CON_BRIGHT
  22. #define CON_BRIGHT_MAGENTA CON_MAGENTA | CON_BRIGHT
  23. #define CON_BRIGHT_CYAN CON_CYAN | CON_BRIGHT
  24. #define CON_BRIGHT_WHITE CON_WHITE | CON_BRIGHT
  25. inline void CON_Printf(const int x, const int y, const char* fmt, ...)
  26. {
  27. char tmpbuf[255];
  28. va_list marker;
  29. va_start(marker, fmt);
  30. vsprintf(tmpbuf, fmt, marker);
  31. va_end(marker);
  32. printf("\x1b[%d;%dH%s", y, x, tmpbuf);
  33. }
  34. inline void CON_SetColor(u8 foreground, u8 background = CON_BLACK)
  35. {
  36. u8 bright = foreground & CON_BRIGHT ? 1 : 0;
  37. if (bright)
  38. foreground &= ~CON_BRIGHT;
  39. printf("\x1b[%d;%d;%dm", 30 + foreground, bright, 40 + background);
  40. }
  41. inline void CON_Clear()
  42. {
  43. // Escape code to clear the whole screen.
  44. printf("\x1b[2J");
  45. }
  46. // libogc's clear escape codes are crappy
  47. inline void CON_BlankRow(const int y)
  48. {
  49. int columns = 0, rows = 0;
  50. CON_GetMetrics(&columns, &rows);
  51. char blank[columns];
  52. std::fill_n(blank, columns, ' ');
  53. blank[columns - 1] = '\0';
  54. CON_Printf(0, y, "%s", blank);
  55. }
  56. #define CON_PrintRow(x, y, ...) \
  57. { \
  58. CON_BlankRow(y); \
  59. CON_Printf(x, y, __VA_ARGS__); \
  60. }