Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [Please Help] Allegro 5 Text File Output

This thread is locked; no one can reply to it. rss feed Print
[Please Help] Allegro 5 Text File Output
variancegears
Member #23,382
June 2022

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();
}

}

DanielH
Member #934
January 2001
avatar

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?

variancegears
Member #23,382
June 2022

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

DanielH
Member #934
January 2001
avatar

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?

variancegears
Member #23,382
June 2022

#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"

DanielH
Member #934
January 2001
avatar

Is your text file encoded?

also, increment the y value of the text draw as you go through draw text loop

variancegears
Member #23,382
June 2022

The text files are encoded as "ANSI" is this a problem?

DanielH
Member #934
January 2001
avatar

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.

#SelectExpand
1#include <vector> 2#include <string> 3#include <fstream> 4 5// untested 6bool loadFile(const std::string& filename, std::vector<std::string>& vec) 7{ 8 std::fstream myFile; 9 std::string line; 10 11 myFile.open(filename, std::ios::in); 12 13 if (myFile.is_open()) 14 { 15 while (std::getline(myFile, line)) 16 { 17 vec.push_back(line); 18 } 19 20 myFile.close(); 21 return true; 22 } 23 24 return false; 25}

#SelectExpand
1std::vector<std::string> vec; 2 3if (!loadFile("chapter1.txt", vec)) 4{ 5 return -1; 6} 7 8while (!running) 9{ 10al_clear_to_color(al_map_rgb(255, 0, 0)); 11 12for (auto c : vec) 13{ 14 al_draw_text(font25, al_map_rgb(WHITE), 0, 0, 0, c.c_str()); 15} 16 17al_flip_display(); 18}

variancegears
Member #23,382
June 2022

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 :)

DanielH
Member #934
January 2001
avatar

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
}

variancegears
Member #23,382
June 2022

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!

DanielH
Member #934
January 2001
avatar

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.

variancegears
Member #23,382
June 2022

Thank you for your help. I added that specific line, and the text does not display anymore

DanielH
Member #934
January 2001
avatar

It should.

What value is SCREEN_HEIGHT? Is it big enough to draw all the lines of text?

variancegears
Member #23,382
June 2022

Thank you for your help.

SCREEN_WIDTH is 800
SCREEN_HEIGHT is 600

DanielH
Member #934
January 2001
avatar

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?

variancegears
Member #23,382
June 2022

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?

DanielH
Member #934
January 2001
avatar

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

Go to: