Hello,
I am trying to make it so that I am to upload a text file and to then display the text onto the display window, but I can't figure out how to do so. When I execute the program, there is just the red screen but no text. This is what I have so far:
int main()
{
std::fstream myFile;
myFile.open("chapter1.txt", std::ios::in);
bool running = false;
al_init();
al_init_image_addon();
al_init_font_addon();
al_init_ttf_addon();
al_install_keyboard();
al_install_audio();
al_init_acodec_addon();
al_install_mouse();
al_reserve_samples(1);
ALLEGRO_DISPLAY* display = al_create_display(SCREEN_WIDTH, SCREEN_HEIGHT);
ALLEGRO_EVENT_QUEUE* event_queue = al_create_event_queue();
ALLEGRO_FONT* font25 = al_load_ttf_font("orbitron.ttf", 25, 0);???
al_register_event_source(event_queue, al_get_display_event_source(display));
while (!running)
{
al_clear_to_color(al_map_rgb(255, 0, 0));
if (myFile.is_open())
{
std::string line;
while (std::getline(myFile, line))
{
const char* c = str.c_str();
al_draw_text(font25, al_map_rgb(WHITE), 0, 0, 0, c);
}
myFile.close();
}
al_flip_display();
}
}
First thing, do not put a file load in the middle of your logic loop.
Second, did you verify everything was installed and loaded correctly?
Did your font even load?
Yes it has loaded and I can write regular text and it can be displayed on the display window. Just having issues with loading text from a file
If everything is loaded correctly even your text file, I can't determine anything else
wrong.
Unless something is wrong with WHITE. How is it defined?
#define WHITE 255, 255, 255
The WHITE macro works with no issue; as I am able to use it, for example,
al_draw_text(font25, al_map_rgb(WHITE), 0, 0, 0, "HEY");
Works perfectly fine and displays white text stating "HEY"
Is your text file encoded?
also, increment the y value of the text draw as you go through draw text loop
The text files are encoded as "ANSI" is this a problem?
Just making sure it didn't have a unicode char that was ending your string too early.
Sorry, I can't see what else it could be.
Just move your file load/read out of your main loop. Write a function that loads the file and puts it into a vector of strings. Then in your main loop, you can iterate through the vector.
Thank you very much, this worked! How can I make it so that instead of all of the text being congealed into one spot, I can make it look like this:
How many apples are in the bag?
A) 5 B) 6
C) 7 D) 8
Currently thats how it is formatted in the text file but all of the letters are bunched up. Thank you very much for your help
Please attach text file.
Also, what do you mean by "bunched up"?
All on the same line?
You need to set a y value and increment it each line
int y = 0; for (auto c : vec) { al_draw_text(font25, al_map_rgb(WHITE), 0, y, 0, c.c_str()); y += (al_get_font_line_height(font25) + 4); // 4 is extra padding between lines }
The letters on the display window from the text file are all overlapping each other, is what I mean. I have attached the text file and an example of what I mean, thank you!
You're drawing the text at the same y coordinate. change the y value every draw.
Increment it the height of the font plus a padding like I showed you above.
Thank you for your help. I added that specific line, and the text does not display anymore
It should.
What value is SCREEN_HEIGHT? Is it big enough to draw all the lines of text?
Thank you for your help.
SCREEN_WIDTH is 800
SCREEN_HEIGHT is 600
Something's not right.
Do you have int y = 0; outside the for loop or inside?
Can you post your main function code again for what you currently have?
Sorry, I had int y in the wrong place and now it works. Thank you very much for your help. I have one final question then I will stop bugging you. Let's say in my text file I have two questions. How do I make it so that each question is segmented to where it will only show question one, then once the user answers question one then it will show question two, and so forth?
There are a few ways?
If the number of answers are consistent, you could format the file as such:
question1
answer1
answer2
answer3
answer4
question2
answer1
answer2
answer3
answer4
Then you can print the question and the 4 answers. Then advance 5 lines to the next
If the number of questions vary, you can have a delimiter between question sets. They you can tell where each question ends and the new question starts.
question1
answer1
answer2
answer3
answer4
---
question2
answer1
answer2
---
question3
answer1
answer2
answer3