Hey guys I've just been playing around with allegro 5, and playing with examples from codingmadeeasy, this is an example from him, and It works fine when he compiles it. He is however using vs and i'm using codeblocks. The code is
[code]// C++ ALLEGRO 5 MADE EASY TUTORIAL 5 - FONT & TEXT
// CODINGMADEEASY
#include <allegro5/allegro.h>
#include <allegro5/allegro_native_dialog.h>
#include <allegro5/allegro_ttf.h>
#include <allegro5/allegro_font.h>
#define ScreenWidth 800
#define ScreenHeight 600
int main()
{
ALLEGRO_DISPLAY *display;
if(!al_init())
{
al_show_native_message_box(NULL, NULL, "Error", "Could not initialize Allegro 5", NULL, ALLEGRO_MESSAGEBOX_ERROR);
return -1;
}
display = al_create_display(ScreenWidth, ScreenHeight);
if(!display)
{
al_show_native_message_box(NULL, NULL, "Error", "Could not create Allegro 5 display", NULL, ALLEGRO_MESSAGEBOX_ERROR);
return -1;
}
// You generally want to do this after you check to see if the display was created. If the display wasn't created then there's
// no point in calling this function
al_set_new_display_flags(ALLEGRO_NOFRAME);
al_set_window_position(display, 200, 100);
al_set_window_title(display, "CodingMadeEasy");
al_init_font_addon();
al_init_ttf_addon();
ALLEGRO_FONT *font = al_load_font("font.ttf", 36, NULL);
al_draw_text(font, al_map_rgb(44, 117, 255), ScreenWidth / 2, ScreenHeight / 2, ALLEGRO_ALIGN_CENTRE, "www.codingmadeeasy.ca");
al_flip_display();
al_rest(10.0);
al_destroy_font(font);
al_destroy_display(display);
return 0;
}
[/code]
The error given is ..
Assertion failed C:\ ...
File:allegro-5.0.x/addons/font/text.c
line 73
expression : Font
So my original though was maybe I needed to use my own font, So I downloaded it from urban fonts, placed it in project folder and set it up to use it, the font name is molten and was set accordingly and doesn't give the error when it pops up...but after a few seconds it crashes..
and advice/help appreciated.
thanks for reading.
That program will shut down after it has been open for 10 seconds.
Also this site uses SGML/HTML/XML style tags. Like: <code>
Use <code>, not [code] to show code in your post.
If I'm not wrong, al_load_font() (Actually, al_load_ttf_font(), since you're using a truetype font) returns NULL on error.
Check if variable font isn't null after loading. If it is, a problem happened while loading it. You can try changing the folder it is placed. Also, try checking if the font's name(and extension) is exactly the same of what you're trying to load.
Thanks for the quick response, I double checked to make sure I had the extension and name correct, both are
As far as checking the value of font, I tried using cout to print its value to the console, however nothing comes out
for that you can just test it against NULL.
ALLEGRO_FONT *font = al_load_font("font.ttf", 36, NULL); if(font == NULL) return ERR_CANT_LOAD_FONT // fictitious error constant. You might want to return 1 or any other non-zero value
alternatively,
ALLEGRO_FONT *font = al_load_font("font.ttf", 36, NULL); if(!font) return ERR_CANT_LOAD_FONT // fictitious error constant. You might want to return 1 or any other non-zero value
doesn't give the error when it pops up
You mean the display or the text? I mean, is the text showing, or there's only a black window?
Sorry, to clarify what I meant by nothing pops up, is that there is no text.
The window is white though, and I have it set so a console will appear for purposes of debugging, but more specifically I meant that it won't print anything to console, its entirely blank when the program crashes.
Thanks!
If you have a console, you should be able to see
That said, if it can't load the font it might be the compiler changing the directory, you can get around this with al_get_standard_path(ALLEGRO_EXENAME_PATH);
Thank you for the reply.
I added in this.
But no change is seen. It still doesn't print to console and crashes
Thanks again for reply
Two things:
1. You need to check the return result of al_load_font() IMMEDIATELY after calling it. Currently, you don't check the return until after your program has run its course.
2. al_set_new_display_flags() has to be called before calling al_create_display(). This is why the command is called "new display flags", since it only affects new displays created afterwards.
I can't believe I didn't catch that I even put it after it was destroyed, that was stupid of me. I have rectified this so my code is now
And what happened?
You won't see fprintf on Windows unless you build a special way. Try using al_show_native_message_box instead.
I used a messagebox instead of printf or cout, however it doesn't load. It comes right after font is declared and set.
Thanks!
You could try passing an absolute path to your font. If that fails, you probably have a faulty installation of Allegro.
Something else I noticed: al_get_standard_path() doesn't actually change or set path information. It creates and returns a pointer to an ALLEGRO_PATH object. This means you will have a memory leak if you don't put the results of the call into an ALLEGRO_PATH pointer, plus if you actually want to use an ALLEGRO_PATH object to properly obtain a file location, you need to use combinations of al_set_path_filename() and al_path_cstr(). Here's the function I wrote to simplify loading textures from a textures folder off of my main game folder:
ALLEGRO_BITMAP *VZ_LoadBitmap (const char *filepath, const char *filename, int bitmap_flags) { // This is merely a function to simplify bitmap loading. Return values are identical to al_load_bitmap() ALLEGRO_BITMAP *tempbmp; al_destroy_path(temppath); temppath = al_clone_path(gamepath); al_append_path_component(temppath,filepath); al_set_path_filename(temppath,filename); al_set_new_bitmap_flags(bitmap_flags); tempbmp = al_load_bitmap(al_path_cstr(temppath,ALLEGRO_NATIVE_PATH_SEP)); return tempbmp; }
Note that "temppath" and "gamepath" are defined as global pointers to ALLEGRO_PATH objects and that gamepath has been set at the start of the program using al_get_standard_path(). Also, the first al_destroy_path(temppath) call only works because I specifically set all pointers I use to NULL at the start of my programs, otherwise this would crash everything.
It also may look like my call to al_path_cstr() is a memory leak, but the pointer is actually tracked by the ALLEGRO_PATH object so I don't have to worry about it.
Thank you for the detailed post! Could I ask for a little more detail on how to use it though? I'm in a hurry so forgive my quick post, but you said linking to an allegro program file, which I don't understand, I installed it by drag and drop the bin include and lib folders.
I'm sorry for the hasty reply but College + work = pain in the ass >_<
Thanks again
Uhh... slow down and actually have time to read stuff before you respond again. I never said anything like that... *reads up* ...doesn't look like anyone else did either.
Sorry about the hasty reply. But thanks a lot for your excerpt.
I'm pretty new to allegro and I won't lie that looks pretty scary..But id be a horrible programmer if I let everything scare me away
So from what youve told me I'm wanting to do something like this..
string *path = al_path_cstr("c:\..."; //then use al_get_standard_path(*path);
Thanks again for reading
Uh... No.
The following is essentially what you need to do to work with filenames and paths with Allegro's built-in functions. Use the Allegro Manual to get information on the usage of these functions:
1. Create a pointer to an ALLEGRO_PATH object.
2. Use al_get_standard_path() to assign a path to your ALLEGRO_PATH object.
3. Use al_set_path_filename() to set the name of the file you want to load in your ALLEGRO_PATH object.
4. When you're ready to load your file, use al_path_cstr() to acquire a string pointer for use directly in a loading command, like al_load_font(). The string returned is stored and tracked by the ALLEGRO_PATH object that generated it, so you can treat the result like a constant instead of as an object that needs to be destroyed.
5. When you're done with your ALLEGRO_PATH object, call al_destroy_path() on it.
I'm having trouble with step 3 of that. so far I have this
<code>
ALLEGRO_PATH *myPath = al_get_standard_path(ALLEGRO_EXENAME_PATH);
al_set_path_filename(*myPath, const char *filename = "drakon.ttf");
</code.>
so in the manual it gives this
void al_set_path_filename(ALLEGRO_PATH *path, const char *filename)
I'm at a loss here, I've already created my allegro path object, and declared my al_set_path_filename, so why created a function to set them again?
Thanks
I think you might need to study up on C/C++ a bit more. Your code there shows a lack of basic C/C++ knowledge.
More-so a lack of pointer knowledge. *'s indicate that you either want to create a pointer to a variable, that you want to grab the information from a pointed variable, or that a function wants a pointer as one of its arguements.
Like:
int *PointerToInt; ThisFunctionWantsAPointer(PointerToInt); ThisFunctionWantsAnInt(*PointerToInt);
Or, you can grab the pointer of a non-pointer variable using an ampersand, like so:
int MyInt; ThisFunctionWantsAnInt(MyInt); ThisFunctionWantsAPointer(&MyInt);
I'm fine with pointers, but this explanation really confused me.
To make it simple, I'll just state it as is:
Pointer is an integer that contains an absolute address to some place in a memory, no more than that. Type of a pointer only helps to know how much data is supposed to be there where pointer points to.
More-so a lack of pointer knowledge.
You might want to check his code again:
ALLEGRO_PATH *myPath = al_get_standard_path(ALLEGRO_EXENAME_PATH); al_set_path_filename(*myPath, const char *filename = "drakon.ttf");
Particularly the second argument to al_set_path_filename.