Skip to content

Instantly share code, notes, and snippets.

@XorTroll
Last active July 12, 2018 19:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save XorTroll/94b542e3d5a8164a10742df0ca4f052b to your computer and use it in GitHub Desktop.
Save XorTroll/94b542e3d5a8164a10742df0ca4f052b to your computer and use it in GitHub Desktop.
Easy C++ libnx printing functions/macros
/*
Easy C++ libnx printing functions/macros by XorTroll!
Be free to include this in your console homebrew app.
Examples:
- Print simple text (no color):
Print("text", CONSOLE_RESET);
- Print text, and print two lines after it:
LPrint("text", CONSOLE_RESET, 2);
- Print error text:
Print(Error "This is an error!", CONSOLE_RESET);
- Print over the same line:
Print(Over "NewText", CONSOLE_RESET);
- Options menu example:
// Three options
vector<string> opts;
opts.push_back("Option 1");
opts.push_back("Option 2");
opts.push_back("Option 3");
// Title's color is green, selected option's one blue and the other's one default color
int selectedindex = OptMenu("Select an option!", opts, CONSOLE_GREEN, CONSOLE_BLUE, CONSOLE_RESET);
if(selectedindex == -1)
{
// Back (B) was pressed, no option selected, better to check this to avoid errors
}
else
{
// Get the string by the index
string selected = opts[selected]; // "Option 1", "Option 2" or "Option 3"
}
*/
#include <iostream>
#include <ctime>
#include <cmath>
#include <functional>
#include <cstdio>
#include <array>
#include <iterator>
#include <string.h>
#include <vector>
#include <algorithm>
#include <cstring>
#include <limits>
#include <stdio.h>
#include <cstdint>
#include <fstream>
#include <cstdlib>
#include <sstream>
#include <string>
#include <malloc.h>
#include <time.h>
#include <cstring>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#include <random>
#include <switch.h>
using namespace std;
// Macro to print over the previous line: Over "sample" -> "\rsample"
#define Over string("\r") +
// Macro to log text as error, adding red error prefix.
#define Error string(CONSOLE_RED) + string("ERROR: ") +
// Better way to call console colors, defined as const char ptrs in libnx macros
typedef const char *ConsoleColor;
// Print text with color
void Print(string Text, ConsoleColor Color)
{
cout << Color << Text << CONSOLE_RESET;
}
// Print text with color, plus line
void LPrint(string Text, ConsoleColor Color, int LineNo)
{
cout << Color << Text << CONSOLE_RESET;
for(int i = 0; i < LineNo; i++) cout << endl;
}
// Print text with color, after printing a number of spaces
void SPrint(string Text, ConsoleColor Color, int SpaceNo)
{
for(int i = 0; i < SpaceNo; i++) cout << " ";
cout << Color << Text << CONSOLE_RESET;
}
// Combining lines and spaces
void SLPrint(string Text, ConsoleColor Color, int SpaceNo, int LineNo)
{
for(int i = 0; i < SpaceNo; i++) cout << " ";
cout << Color << Text << CONSOLE_RESET;
for(int i = 0; i < LineNo; i++) cout << endl;
}
// Combining lines and spaces #2
void LSPrint(string Text, ConsoleColor Color, int SpaceNo, int LineNo)
{
for(int i = 0; i < LineNo; i++) cout << endl;
for(int i = 0; i < SpaceNo; i++) cout << " ";
cout << Color << Text << CONSOLE_RESET;
}
// Combining lines and spaces #3
void LSLPrint(string Text, ConsoleColor Color, int SpaceNo, int LineNo)
{
for(int i = 0; i < LineNo; i++) cout << endl;
for(int i = 0; i < SpaceNo; i++) cout << " ";
cout << Color << Text << CONSOLE_RESET;
for(int i = 0; i < LineNo; i++) cout << endl;
}
// Clear console
void Clear()
{
consoleClear();
}
// Show a menu and wait until the user selects an option, then return that option's id, but -1 is returned if back (B) is pressed
int OptMenu(string Title, vector<string> Opts, ConsoleColor TitleColor, ConsoleColor SelectedOptColor, ConsoleColor OtherOptColor)
{
int selected = 0;
Clear();
LSLPrint(Title, TitleColor, 2, 2);
Print("\n", CONSOLE_RESET);
for(int i = 0; i < Opts.size(); i++)
{
if(i == selected) SLPrint(Opts[i], SelectedOptColor, 4, 1);
else SLPrint(Opts[i], OtherOptColor, 4, 1);
}
while(true)
{
gfxFlushBuffers();
gfxSwapBuffers();
gfxWaitForVsync();
hidScanInput();
int k = hidKeysDown(CONTROLLER_P1_AUTO);
if(k & KEY_A)
{
Clear();
return selected;
}
else if(k & KEY_UP)
{
if(selected > 0) selected--;
else selected = Opts.size() - 1;
Clear();
LSLPrint(Title, TitleColor, 2, 2);
Print("\n", CONSOLE_RESET);
for(int i = 0; i < Opts.size(); i++)
{
if(i == selected) SLPrint(Opts[i], SelectedOptColor, 4, 1);
else SLPrint(Opts[i], OtherOptColor, 4, 1);
}
}
else if(k & KEY_DOWN)
{
if(selected < Opts.size() - 1) selected++;
else selected = 0;
Clear();
LSLPrint(Title, TitleColor, 2, 2);
Print("\n", CONSOLE_RESET);
for(int i = 0; i < Opts.size(); i++)
{
if(i == selected) SLPrint(Opts[i], SelectedOptColor, 4, 1);
else SLPrint(Opts[i], OtherOptColor, 4, 1);
}
}
else if(k & KEY_B)
{
Clear();
return -1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment