Allegro.cc - Online Community

Allegro.cc Forums » Installation, Setup & Configuration » I am looking for correct detailed instructions how to build Allegro....

This thread is locked; no one can reply to it. rss feed Print
 1   2   3 
I am looking for correct detailed instructions how to build Allegro....
Dizzy Egg
Member #10,824
March 2009
avatar

Meh (I've had a few cans)

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

takis76
Member #1,419
July 2001
avatar

Well I removed the <iostream.h>

I added "using namespace std;" to all modules except in the "Prototypes.h"

And there was an error in this line:

al_draw_text(font01,al_map_rgb(255,0,0),100,120,0, is_running.cstr() );

I should use is_running.c_str() instead of is_running.cstr();

If I want to do the same with an integer variable?
The "is_running" was a string variable, if for example I have a float or an integer, can I make a cast? or convert the float or the integer to string?

Trying this is not working

running.cstr();

error: request for member 'c_str' in 'running', which is of non-class type 'int'|

Trying this:

(string)running;

This doesn't seem to work too.

error: no matching function for call to 'std::__cxx11::basic_string<char>::basic_string(int&)'|

The running variable was defined as "int running;"
I have 2 variables 1 with name is_running which was string (we fixed this) and the running which is integer.

In the past I remember I used cast.

Chris Katko
Member #1,881
January 2002
avatar

cast says to the compiler "this variable is of a different type than you think so treat it as I say it is". It does not convert variables binary representation. So a string holding the character "1" is completely different in binary from the integer variable=1 for example.

int x = 1; 
string x = "1"; //completely different in memory! 

Semicolons are only used to end statements. A function call doesn't have any statements so it doesn't need any semicolons.

If you want to draw text, use al_draw_printf (for Allegro 5) or textprintf_ex (for allegro 4)

https://www.allegro.cc/manual/5/al_draw_textf
https://liballeg.org/stabledocs/en/alleg018.html#textprintf_ex

These are functions that take text, formatted like the normal printf function, and draw it to the screen.

You cannot send an integer variable to a printf function. It is expecting a string, that is formatted a special way that includes instructions on how to print variables.

see here:

https://www.cplusplus.com/reference/cstdio/printf/

So to send a numeric variable out, you send it inside of a string exactly the same way you'd use normal printf for printing to the console.

printf("my variable is equal to %d.\n", 10); 
// %d means print an integer here
// \n is a special code for add a newline

prints out

my variable is equal to 10.

It saw "%d" in the string and realized that was where a integer should be, so it then looked for the next argument which happened to equal 10, and then wrote "10" into the string.

Allegro printout functions work the same way as this is the standard way to write strings in C (and often times in C++). the "printf style" has been used in many programming languages.

What it looks like you were attempting to do, and the compiler was trying to help was this:

printf(10);
// stdio.h:332:43: note: expected ‘const char * restrict’ but argument is of type ‘int’

But printf doesn't know what 10 means. You could print the value 10 in many ways. As decimal. As binary. As hexidemical. As a ASCII character. It wants a string that specifies how to format the output.

more printf stuff:

    int X = 10;

    printf("my constant is equal to %d\n", 15); // a constant into a string
    printf("my variables are equal to %d, %d, %d\n", X, 20, 30); // multiple variables
    printf("my variables are equal to %d, %x, %p\n", X, X, X); //use same variable MULTIPLE TIMES but with different formatting

prints out:

my constant is equal to 10
my variables are equal to 10, 20, 30
my variables are equal to 10, a, 0xa

// A is hexidecimal for 10
// 0xa is because it's told to print the value 10 as if it was a pointer 

What you did when you called cstr on a string was create a c-style string and pass it to the Allegro function. That works. However, if you want a integer? You have to place it inside of a string. With a printf style function (or the C++ equivalent) for strings... called... sprintf ("string printf"). Or any Allegro function that mimics printf style like the ones I mentioned above, they do that for you. But the reason you cannot call cstr on an integer is because it's just that. An integer. It doesn't have any methods to call. cstr is a method only for the string class.

This post feels all over the place, but you've got a lot of missing fundamentals in your knowledge of C/C++ (which is okay, it's just they all need addressed to explain what's wrong).

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

takis76
Member #1,419
July 2001
avatar

Thank you very very much. :)

This worked excellent.

al_draw_text(font01,al_map_rgb(255,0,0),70,120,0, is_running.c_str() );
al_draw_textf(font01,al_map_rgb(0,255,255),70,140,0, "%d",running );
al_draw_textf(font01,al_map_rgb(0,255,255),70,160,0, "%s",is_running.c_str() );

Then I will try to load some music and sound in the next and if I will have any problem I will post here and after I will load some sound I will try this PhysFS addon.

Learning the Allegro, I will refresh my C++. (Yes I have many missing fundamentals in C++ and even some books do not cover everything) , but when I was using the C++ last time was with DJGPP and DOS (With Allegro 4.2.0 or may I will not using C++ at all may it was C no C++) these things worked (The example with cast I mentioned) and before the DJGPP I was programmed with C++ many years ago before DJGPP in 90s was with Borland C++, that's I have to say Borland C++ had the best manual, you pressed F1 on the command or function and you had an instant example, now F1 does nothing and even the code autocompletion presents a huge list of unrelated commands. Many things were changed.

And all C++ versions are not the same.

 1   2   3 


Go to: