I have a little 2D game made with Allegro, and I want to introduce strings during gameplay. For example, I've just made it so a key is needed to open a door, so I'd need some tutorial text to appear when the player collides with the door that tells them they need the key first.
This is just a learning experience for me, really - the key is pretty visible but I would like to learn how to handle this sort of messages especially for tutorial purposes in a bigger, better game.
Right now, during gameplay, I have running game_loop() (which handles player movement, collisions etc) and game_render(), which obviously runs after it. To introduce these messages, what I have successfully done so far is to add a global std::vector<const char*> to which I push_back messages whenever appropriate.
My message_renderer() then goes like so:
static double t = 0, c = 0; if (!messages.empty()) { for (unsigned int i = 0; i < messages.size(); i++) { al_draw_text(font18, al_map_rgb_f(255, 255, 255), wWidth / 2, wHeight - 50, ALLEGRO_ALIGN_CENTRE, messages[i]); if (t < 2) { t += get_dt(); c += get_dt(); if (c > 1) c = 1; } else { t = 0; c = 0; messages.pop_back(); } } } I then call this message_renderer() in my game_render() after I've drawn everything else, to ensure that the message appears on top.
Now, again, this does work - the message displays for 2 seconds at render time once it's added to the vector. However, this solution doesn't really seem particularly neat to me, since as far as I know there are standardised ways of dealing with these sort of events - I just don't know what they are!
So my question is - is my current method efficient enough? Can it be improved, in Allegro (or generally, in logic!) and if so, how?
Thank you in advance!
EDIT: I've modified the code since posting the question, so it should now behave like expected - showing a message for 3 seconds. It does so, but when I have two strings in the vector at the same time they overlap - I thought the timer would work as "pause" between each iteration but I guess not... If someone could point me in the right direction for that too, it'd be much appreciated :)
std::stringinstead ofconst char*? You have the benefits of the standard C++ class, and you can retrieve the pointer needed by Allegro withstd::string::c_str(). \$\endgroup\$std::strings fromconst char*and thec_str()function is here to get access to the underlying null-terminated character string, for cases like yours. \$\endgroup\$