I purchased a game programming book recently that uses Allegro as it's library. One of the first exercises after setting up Allegro is creating a program that displays some of Allegro's global variables, such as the library version, OS name and version, etc. However other than the library version, nothing is displayed correctly and anything beyond the OS name and version crashes the program. I've tried two different compilers and get the same results. This is the source code to the small program.
| 1 | #define ALLEGRO_USE_CONSOLE //Needed for MSVC8 for some reason |
| 2 | #include <stdio.h> |
| 3 | #include <conio.h> |
| 4 | #include "allegro.h" |
| 5 | |
| 6 | char *YesNo(int num) |
| 7 | { |
| 8 | if (num == 0) |
| 9 | return "No"; |
| 10 | else |
| 11 | return "Yes"; |
| 12 | } |
| 13 | |
| 14 | char *OSName(int number) |
| 15 | { |
| 16 | switch(number) |
| 17 | { |
| 18 | case OSTYPE_UNKNOWN:return "Unknown or MS-DOS"; |
| 19 | case OSTYPE_WIN3: return "Windows"; |
| 20 | case OSTYPE_WIN95: return "Windows 95"; |
| 21 | case OSTYPE_WIN98: return "Windows 98"; |
| 22 | case OSTYPE_WINME: return "Windows ME"; |
| 23 | case OSTYPE_WINNT: return "Windows NT"; |
| 24 | case OSTYPE_WIN2000: return "Windows 2000"; |
| 25 | case OSTYPE_WINXP: return "Windows XP"; |
| 26 | case OSTYPE_OS2: return "OS/2"; |
| 27 | case OSTYPE_WARP: return "OS/2 Warp 3"; |
| 28 | case OSTYPE_DOSEMU: return "Linux DOSEMU"; |
| 29 | case OSTYPE_OPENDOS: return "Caldera OpenDOS"; |
| 30 | case OSTYPE_LINUX: return "Linux"; |
| 31 | case OSTYPE_FREEBSD: return "FreeBSD"; |
| 32 | case OSTYPE_QNX: return "QNX"; |
| 33 | case OSTYPE_UNIX: return "Unix variant"; |
| 34 | case OSTYPE_BEOS: return "BeOS"; |
| 35 | case OSTYPE_MACOS: return "MacOS"; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | int main() |
| 40 | { |
| 41 | int width, height; |
| 42 | int caps = cpu_capabilities; |
| 43 | |
| 44 | printf("Allegro version: %s\n", allegro_id); |
| 45 | printf("Operating system: %s\n", OSName(os_type)); |
| 46 | printf("OS version: %d.%d\n", os_version, os_revision); |
| 47 | printf("Multitasking: %s\n", YesNo(os_multitasking)); |
| 48 | |
| 49 | //Anything past here is not displayed and the program crashes, even if I remove |
| 50 | //certain lines such as the following two or three |
| 51 | get_desktop_resolution(&width, &height); |
| 52 | printf("Desktop resolution:%d x %d\n", width, height); |
| 53 | printf("Color depth: %d bits\n", desktop_color_depth()); |
| 54 | |
| 55 | printf("CPU vendor: %s\n", cpu_vendor); |
| 56 | printf("CPU family: %d\n", cpu_family); |
| 57 | printf("CPU model: %d\n", cpu_model); |
| 58 | |
| 59 | printf("Processor ID: %s\n", YesNo((caps & CPU_ID) == CPU_ID)); |
| 60 | printf("x87 FPU: %s\n", YesNo((caps & CPU_FPU) == CPU_FPU)); |
| 61 | printf("MMX: %s\n", YesNo((caps & CPU_MMX) == CPU_MMX)); |
| 62 | printf("MMX+: %s\n", YesNo((caps & CPU_MMXPLUS) == CPU_MMXPLUS)); |
| 63 | printf("SSE: %s\n", YesNo((caps & CPU_SSE) == CPU_SSE)); |
| 64 | printf("SSE2: %s\n", YesNo((caps & CPU_SSE2) == CPU_SSE2)); |
| 65 | printf("3D Now: %s\n", YesNo((caps & CPU_3DNOW) == CPU_3DNOW)); |
| 66 | printf("Enhanced 3D Now: %s\n", YesNo((caps & CPU_ENH3DNOW) == CPU_ENH3DNOW)); |
| 67 | |
| 68 | |
| 69 | printf("Press any key to exit...\n"); |
| 70 | getch(); |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | END_OF_MAIN(); |
When the program runs, it displays my OS as Unknown (even though I'm running Win XP SP2), the version as -1.-1, and multithreading as No.
I'm running a Compaq Presario R3200 AMD Athlon XP Mobile 2800+ 1.6Ghz with 256mb RAM with Win XP SP2.
Is it a problem with my setup, my laptop hardware, or a bug in allegro?
EDIT: I just realized I'm retarded and forgot the most important thing - allegro_init(); Duh.
I'll leave this post up as a reminder not to forget certain important things
Run allegro_init() first.
allegro_init initializes those variables.
Yea, I just realized that after I posted it.. fast replies 
Maybe you guys could help me out with something else though. In MSVC8 when I compile any allegro program I always get 12 warnings, even with the warning level set at 1:
c:\program files\microsoft visual studio 8\vc\include\allegro\internal/alconfig.h(378) : warning C4312: 'type cast' : conversion from 'unsigned int' to 'unsigned char *' of greater size c:\program files\microsoft visual studio 8\vc\include\allegro\internal/alconfig.h(385) : warning C4312: 'type cast' : conversion from 'unsigned int' to 'unsigned char *' of greater size c:\program files\microsoft visual studio 8\vc\include\allegro\inline/draw.inl(421) : warning C4312: 'type cast' : conversion from 'unsigned int' to 'unsigned char *' of greater size c:\program files\microsoft visual studio 8\vc\include\allegro\inline/draw.inl(435) : warning C4312: 'type cast' : conversion from 'unsigned int' to 'unsigned char *' of greater size c:\program files\microsoft visual studio 8\vc\include\allegro\inline/draw.inl(446) : warning C4312: 'type cast' : conversion from 'unsigned int' to 'unsigned short *' of greater size c:\program files\microsoft visual studio 8\vc\include\allegro\inline/draw.inl(460) : warning C4312: 'type cast' : conversion from 'unsigned int' to 'unsigned short *' of greater size c:\program files\microsoft visual studio 8\vc\include\allegro\inline/draw.inl(471) : warning C4312: 'type cast' : conversion from 'unsigned int' to 'unsigned short *' of greater size c:\program files\microsoft visual studio 8\vc\include\allegro\inline/draw.inl(485) : warning C4312: 'type cast' : conversion from 'unsigned int' to 'unsigned short *' of greater size c:\program files\microsoft visual studio 8\vc\include\allegro\inline/draw.inl(521) : warning C4312: 'type cast' : conversion from 'unsigned int' to 'unsigned int *' of greater size c:\program files\microsoft visual studio 8\vc\include\allegro\inline/draw.inl(535) : warning C4312: 'type cast' : conversion from 'unsigned int' to 'unsigned int *' of greater size .\test.c(69) : warning C4996: 'getch' was declared deprecated
The last message is probably easy to remove: don't include conio.h, and don't use the function getch(). If you really want the program to wait for a keypress, you could use system("PAUSE") instead. Both ways are not very portable and will only work on the Windows platform.
When you're cleaning that up, get rid of conio.h. You neither need nor want it.
To disable the warnings:
#pragma warning( disable : 4312 );