Hello world
I want to print colored logo of SWTI and yellow text Hello world to middle of fullscreen window.
Start
I will begin by simple hello world, check that this code compiles and executes correctly. If not, refer to README and solve the issue before continuing.
#include <iostream>
#include "swti/swti.hpp"
int main()
{
std::cout << "Hello World!" << std::endl;
return 0;
}
Logo
I can create SWTI logo by using Cursor.printChar which prints a character at position, optionally with color. I will print individual characters of SWTI, each with different color and different position.
#include <iostream>
#include "swti/swti.hpp"
int main()
{
Cursor.printChar(0, 0, 'S', YELLOW);
Cursor.printChar(1, 0, 'W', LIGHTRED);
Cursor.printChar(2, 0, 'T', LIGHTBLUE);
Cursor.printChar(3, 0, 'I', LIGHTGREEN);
return 0;
}
Function
I can make a function that prints SWTI logo at any position. Then I can use Cursor.setPosition and Cursor.setColor to set position and color of next written text. I can write "Hello world".
#include <iostream>
#include "swti/swti.hpp"
void printLogo(int x, int y)
{
Cursor.printChar(x++, y, 'S', YELLOW);
Cursor.printChar(x++, y, 'W', LIGHTRED);
Cursor.printChar(x++, y, 'T', LIGHTBLUE);
Cursor.printChar(x++, y, 'I', LIGHTGREEN);
}
int main()
{
printLogo(0, 0);
Cursor.setPosition(5, 0);
Cursor.setColor(YELLOW);
std::cout << "Hello world!";
return 0;
}
Colors
Window.setDefaultColor changes our color pallete with different colors. I will use Window.setDefaultColor to change BLACK background to dark blue and set YELLOW to different HEX value.
#include <iostream>
#include "swti/swti.hpp"
void printLogo(int x, int y)
{
Cursor.printChar(x++, y, 'S', YELLOW);
Cursor.printChar(x++, y, 'W', LIGHTRED);
Cursor.printChar(x++, y, 'T', LIGHTBLUE);
Cursor.printChar(x++, y, 'I', LIGHTGREEN);
}
int main()
{
Window.setDefaultColor(BLACK, RGB(16, 20, 28));
Window.setDefaultColor(YELLOW, HEX(0xf5f118));
printLogo(0, 0);
Cursor.setPosition(5, 0);
Cursor.setColor(YELLOW);
std::cout << "Hello world!";
return 0;
}
Window functions
I calculate font size using Window.getScreenHeight and then use Cursor.setFontSize I call Window.setFullscreenBorderless to set window to fullsreen. Finally can factor out these functions to simple setup() function.
#include <iostream>
#include "swti/swti.hpp"
void setup()
{
Window.setDefaultColor(BLACK, RGB(16, 20, 28));
Window.setDefaultColor(YELLOW, HEX(0xf5f118));
int size = Window.getScreenHeight() / 15;
Cursor.setFontSize(size);
Window.setFullscreenBorderless();
Window.hideBlinking();
}
void printLogo(int x, int y)
{
Cursor.printChar(x++, y, 'S', YELLOW);
Cursor.printChar(x++, y, 'W', LIGHTRED);
Cursor.printChar(x++, y, 'T', LIGHTBLUE);
Cursor.printChar(x++, y, 'I', LIGHTGREEN);
}
int main()
{
setup();
printLogo(0, 0);
Cursor.setPosition(5, 0);
Cursor.setColor(YELLOW);
std::cout << "Hello world!";
return 0;
}
Full code
I can calculate middle position using Window.getColumns and Window.getRows. If I divide both numbers by two, I get middle and center. I will then substract some number to adjust it to our needs. Lastly, I will call Keyboard.waitUser(); to end the main and use Window.hideBlinking(); to stop white blinking cursor.
#include <iostream>
#include "swti/swti.hpp"
// set fullsreen, font size and colors
void setup()
{
Window.setDefaultColor(BLACK, RGB(16, 20, 28));
Window.setDefaultColor(YELLOW, HEX(0xf5f118));
int size = Window.getScreenHeight() / 15;
Cursor.setFontSize(size);
Window.setFullscreenBorderless();
Window.hideBlinking();
}
// simple function to print colorful logo
void printLogo(int x, int y)
{
Cursor.printChar(x++, y, 'S', YELLOW);
Cursor.printChar(x++, y, 'W', LIGHTRED);
Cursor.printChar(x++, y, 'T', LIGHTBLUE);
Cursor.printChar(x++, y, 'I', LIGHTGREEN);
}
// main function will be called first
int main()
{
setup();
int middle, center;
middle = Window.getColumns() / 2 - 8;
center = Window.getRows() / 2 - 2;
printLogo(middle, center);
Cursor.setPosition(middle + 5, center);
Cursor.setColor(YELLOW);
cout << "Hello world!";
Keyboard.waitUser();
return 0;
}