1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
// First Allegro Program
#include <allegro5\allegro.h>
#include <allegro5\allegro_font.h>
#include <allegro5\allegro_ttf.h>
#include <allegro5\allegro_native_dialog.h>
#include <iostream>
#include <string>
ALLEGRO_DISPLAY * display = NULL;
struct RGB { unsigned char RGB_red, RGB_green, RGB_blue; };
struct SCREEN { int W, H; };
SCREEN Screen_Max;
void Initialise_Display();
void Process_Font(std::string);
void Read_And_Process_Text(ALLEGRO_FONT *);
void Print_Text(ALLEGRO_FONT *, std::string, RGB, SCREEN);
void Destroy_Display();
int main (int argc, char **argv)
{
std::string font_name("Times_New_Roman");
Initialise_Display();
Process_Font(font_name);
Destroy_Display();
return 0;
}
void Initialise_Display()
{
if (!al_init())
{
al_show_native_message_box(display, "Error 101", "A problem occured.", "Display could not be initialised.", NULL, NULL);
exit(-1);
}
display = al_create_display(720,480);
if (!display)
{
al_show_native_message_box(display, "Error 102", "A problem occured.", "Display could not be shown.", NULL, NULL);
exit(-1);
}
Screen_Max.W = al_get_display_width(display);
Screen_Max.H = al_get_display_height(display);
}
void Process_Font(std::string font)
{
std::string type(".ttf");
std::string font_name = font + type;
const char * _font_name = font_name.c_str();
al_init_font_addon();
al_init_ttf_addon();
ALLEGRO_FONT * font18 = al_load_font(_font_name,18,0);
ALLEGRO_FONT * font24 = al_load_font(_font_name,24,0);
ALLEGRO_FONT * font32 = al_load_font(_font_name,32,0);
char _continue;
do
{
system("cls");
short option;
std::cout << "Which font size would you like to use?" << std::endl;
std::cout << " 1. Times New Roman 18pt." << std::endl;
std::cout << " 2. Times New Roman 24pt." << std::endl;
std::cout << " 3. Times New Roman 32pt." << std::endl;
std::cout << " Your option: "; std::cin >> option;
switch (option)
{
case 1:
Read_And_Process_Text(font18);
break;
case 2:
Read_And_Process_Text(font24);
break;
case 3:
Read_And_Process_Text(font32);
break;
default:
if (option < 1 || option > 3)
exit(-1); // if I don't add the whole 'if', it doesn't break, allowing me to insert values such as '24' or so
break;
}
std::cout << std::endl << "Would you like to print another text? (Y/N)" << std::endl;
std::cin >> _continue;
}while (toupper(_continue) == 'Y');
al_destroy_font(font18);
al_destroy_font(font24);
al_destroy_font(font32);
}
void Read_And_Process_Text(ALLEGRO_FONT * _font)
{
RGB color;
SCREEN screen;
std::string text;
std::cout << "Give the text: ";
getline(std::cin, text); // Always skips reading
std::cout << "What proportion of red/green/blue will the color of the text have? (0-255)" << std::endl;
std::cout << " (0-255) Red: "; std::cin >> color.RGB_red;
std::cout << " (0-255) Green: "; std::cin >> color.RGB_green; // depending on the value of 'red', it is sometimes skipped
std::cout << " (0-255) Blue: "; std::cin >> color.RGB_blue; // depending on the value of 'red' and 'green', it is sometimes skipped
std::cout << std::endl;
std::cout << "Where exactly on the screen would you like to place the text? (x,y) coordinates: " << std::endl;
std::cout << "x = "; std::cin >> screen.W; // sometimes skipped
std::cout << "y = "; std::cin >> screen.H; // sometimes skipped
try
{
if (screen.W < 0 || screen.H < 0 || screen.W > Screen_Max.W || screen.H > Screen_Max.H)
{
throw "Error. Invalid input.";
}
}
catch(std::string error)
{
std::cout << error;
}
Print_Text(_font, text, color, screen);
}
void Print_Text(ALLEGRO_FONT * _font, std::string _text, RGB _color, SCREEN _screen)
{
const char * __text = _text.c_str();
al_draw_text(_font, al_map_rgb(_color.RGB_red, _color.RGB_green, _color.RGB_blue), _screen.W, _screen.H, 0, __text);
al_flip_display(); // instead of showing the text (which it doesn't read in the read function), it turns the whole display to black
}
void Destroy_Display()
{
al_destroy_display(display);
}
|