Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » textprintf help

This thread is locked; no one can reply to it. rss feed Print
textprintf help
red_4900
Member #9,776
May 2008

I'm a newbie here and also a newbie to the allegro library.this is the code that I have problem with :

#include"allegro.h"
#include<conio.h>
#include<stdlib.h>
int main(){
allegro_init();

install_keyboard();

int ret=set_gfx_mode(GFX_AUTODETECT_WINDOWED,640,480,0,0);
if(ret!=0){
allegro_message(allegro_error);
return 0;
}

textprintf(screen, font, 0, 0, makecol(100,255,125),"%dx%d", SCREEN_W, SCREEN_H);

while(! key[KEY_ESC]);

allegro_exit();
}
END_OF_MAIN();

I copied it straight from the book yet it did not compiled perfectly.the compiler said there's an error in line 15, which is the 'textprintf line'.any help would be greatly appreciated.thank you.

And could someone show me how to distinguish the code from the text?something like

Thomas Harte
Member #33
April 2000
avatar

Distinguish code from text using the [ code ] tags (but with no spaces). E.g.

[ code ]
code
[ /code ]

As to the real problem — are you sure your compiler gave an error, not a warning? Can you supply the text of the error?

In general textprintf is deprecated in favour of textprintf_ex — it's recommended that you use the latter instead. For you that should be as simple as:

textprintf(screen, font, 0, 0, makecol(100,255,125), -1, "%dx%d", SCREEN_W, SCREEN_H);

torhu
Member #2,727
September 2002
avatar

What error messages do you get? textprintf is deprecated, textprintf_ex replaces it, but it should still work.

And you can enclose you code in code tags, like this:

<code>Your code here
</code>

EDIT: Well, I guess two replies are better than one, eh?

LennyLen
Member #5,313
December 2004
avatar

Thomas Harte said:

In general textprintf is deprecated in favour of textprintf_ex — it's recommended that you use the latter instead. For you that should be as simple as:

textprintf(screen, font, 0, 0, makecol(100,255,125), -1, "%dx%d", SCREEN_W, SCREEN_H);

Pssst.... You forgot to rename the function:

textprintf_ex(screen, font, 0, 0, makecol(100,255,125), -1, "%dx%d", SCREEN_W, SCREEN_H);

edit:

red_4900 said:

I copied it straight from the book

Let me guess, the book was Game Programming All in One? Unfortunately, that book is full of errors and bad coding practices (such as including conio.h when none of the functions in that library are used).

Thomas Harte
Member #33
April 2000
avatar

Oh, yes, listen to LennyLen's correction to my post — don't listen to my original post.

red_4900
Member #9,776
May 2008

I did as what u guys told me but it showed this error:
Compiler: Default compiler
Building Makefile: "C:\Dev-Cpp\Programming\Greet\Makefile.win"
Executing make...
mingw32-make -f "C:\Dev-Cpp\Programming\Greet\Makefile.win" all
g++.exe -c test2.c -o test2.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"

g++.exe test1.o test2.o Project1_private.res -o "Project1.exe" -L"C:/Dev-Cpp/lib" -mwindows -lalleg

test2.o(.text+0x0):test2.c: multiple definition of `_mangled_main()'
test1.o(.text+0x0):test1.cpp: first defined here
test2.o(.text+0x146):test2.c: multiple definition of `WinMain@16'
test1.o(.text+0x132):test1.cpp: first defined here
collect2: ld returned 1 exit status

mingw32-make: *** [Project1.exe] Error 1

Execution terminated

this is my code:

1#include"allegro.h"
2#include<stdlib.h>
3int main(){
4 allegro_init();
5
6 install_keyboard();
7
8 int ret=set_gfx_mode(GFX_AUTODETECT_WINDOWED,640,480,0,0);
9 if(ret!=0){
10 allegro_message(allegro_error);
11 return 1;
12 }
13
14 textprintf_ex(screen, font, 0, 0, makecol(255,255,255),-1,"%dx%d", SCREEN_W, SCREEN_H);
15
16 while(! key[KEY_ESC]);
17
18 allegro_exit();
19 }
20 END_OF_MAIN();

yes,I'm using the "Game Programming All In One".I cant find any other books that's noob-friendly.

LennyLen
Member #5,313
December 2004
avatar

Quote:

g++.exe test1.o test2.o Project1_private.res -o "Project1.exe" -L"C:/Dev-Cpp/lib" -mwindows -lalleg

There are two object files in your project, even though you appear to only have one source file. Did you originally have a file in your project called test1.c that you removed and replaced with a new file called test2.c?

Try creating a new project, as the code you just posted compiles as expected (no errors, just a warning).

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Ryan Stover
Member #9,755
May 2008
avatar

Hmmm that is weird... I have always put a semi-colon after END_OF_MAIN and it works fine.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

END_OF_MAIN() manual entry - It may work but it still doesn't belong there. It's a define , not a statement that needs to be ended with a semi-colon.

#define SOME_CODE \
for (int i = 0 ; i < 10 ; i++) {do_stuff();} 

// Which of the next two statements would be correct?
SOME_CODE

SOME_CODE;

As you can see , the semi-colon would be inappropriate there.

END_OF_MAIN() is a platform specific #define so there are several possible definitions.
Here is the one from alwin.h :

1 
2#if (!defined ALLEGRO_NO_MAGIC_MAIN) && (!defined ALLEGRO_SRC)
3 
4 #define ALLEGRO_MAGIC_MAIN
5 #define main _mangled_main
6 #undef END_OF_MAIN
7 
8 /* disable strict pointer typing because of the vague prototype below */
9 #define NO_STRICT
10 
11 #ifdef __cplusplus
12 extern "C" int __stdcall WinMain(void *hInst, void *hPrev, char *Cmd, int nShow);
13 #endif
14 
15 #define END_OF_MAIN() \
16 \
17 int __stdcall WinMain(void *hInst, void *hPrev, char *Cmd, int nShow) \
18 { \
19 return _WinMain((void *)_mangled_main, hInst, hPrev, Cmd, nShow); \
20 }
21 
22#endif

So you can see that it doesn't need a semi-colon there.

Go to: