Allegro.cc - Online Community

Allegro.cc Forums » Allegro Development » Runtime determination of graphic mode types

This thread is locked; no one can reply to it. rss feed Print
Runtime determination of graphic mode types
Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Edit
I traded out is_a_real_mode for is_a_magic_mode, to be more consistent in usage among the set of functions. Source code and demonstration program updated as well.

I've created some helper functions that I think would be useful to add to the Allegro library version 4.3.11 for future use in Allegro 4.4. They let you determine whether a certain graphics card driver is windowed, fullscreen, definitely windowed or fullscreen, or whether it is a magic driver like GFX_AUTODETECT*, GFX_SAFE, and GFX_TEXT. There is also a function to determine the current graphics driver, which is useful for when you set a magic mode that can set several different drivers.

Here are the function declarations that would be added to allegro\gfx.h :

//
AL_FUNC(int , is_a_windowed_mode   , (const int graphics_card));
AL_FUNC(int , is_a_fullscreen_mode , (const int graphics_card));
AL_FUNC(int , is_a_definite_mode   , (const int graphics_card));
AL_FUNC(int , is_a_magic_mode      , (const int graphics_card));
AL_FUNC(int , current_gfx_mode     , ());
//

The first four functions allow the programmer to determine which drivers and modes they want to allow the users of their program to select during run time. In Windows, they can use the is_a_windowed_mode function to filter out any window sizes that are not smaller than the desktop to prevent a failure to set a driver. The is_a_definite_mode function can be used to prevent a user from trying to set GFX_AUTODETECT or GFX_SAFE, neither of which guarantee to be windowed or fullscreen only.

The current_gfx_mode function will let a programmer determine which of several possible graphics mode drivers has been set, especially in the case of using GFX_AUTODETECT* and GFX_SAFE modes.

Here's the code for all 5 functions that would be added to src\graphics.c :

1//
2 
3 
4/* is_a_windowed_mode :
5 * Determines if graphics_card is a windowed driver.
6 * A return value of 0 does NOT indicate that it is
7 * a fullscreen driver, just that it is unknown whether
8 * it is a windowed driver. GFX_SAFE is not definitively
9 * a windowed mode, as with system drivers that don't
10 * provide a recommended safe mode, GFX_AUTODETECT is
11 * used, and that can set windowed or fullscreen modes.
12 */
13int is_a_windowed_mode(const int graphics_card) {
14
15 _DRIVER_INFO* gfx_driver_info = NULL;
16 GFX_DRIVER* gfx_driver_entry = NULL;
17
18 ASSERT(system_driver);
19
20 if (graphics_card == GFX_AUTODETECT_WINDOWED) {return 1;}
21
22 /* ask the system driver for a list of graphics hardware drivers */
23 if (system_driver->gfx_drivers) {
24 gfx_driver_info = system_driver->gfx_drivers();
25 } else {
26 gfx_driver_info = _gfx_driver_list;
27 }
28
29 ASSERT(gfx_driver_info);
30
31 while(gfx_driver_info->driver) {
32 if (gfx_driver_info->id == graphics_card) {
33 gfx_driver_entry = (GFX_DRIVER*)gfx_driver_info->driver;
34 return gfx_driver_entry->windowed;
35 }
36 ++gfx_driver_info;
37 }
38 return 0;// windowed status is unknown for graphics_card value
39}
40 
41 
42 
43/* is_a_fullscreen_mode :
44 * Determines if graphics_card is a fullscreen driver.
45 * A return value of 0 does NOT indicate that it is
46 * a windowed driver, just that it is unknown whether
47 * it is a fullscreen driver. GFX_AUTODETECT_FULLSCREEN
48 * will return non-zero, but GFX_AUTODETECT will not, as
49 * it can set either windowed or fullscreen modes.
50 */
51int is_a_fullscreen_mode(const int graphics_card) {
52
53 _DRIVER_INFO* gfx_driver_info = NULL;
54 GFX_DRIVER* gfx_driver_entry = NULL;
55
56 ASSERT(system_driver);
57
58 if (graphics_card == GFX_AUTODETECT_FULLSCREEN) {return 1;}
59
60 /* ask the system driver for a list of graphics hardware drivers */
61 if (system_driver->gfx_drivers) {
62 gfx_driver_info = system_driver->gfx_drivers();
63 } else {
64 gfx_driver_info = _gfx_driver_list;
65 }
66
67 ASSERT(gfx_driver_info);
68 while(gfx_driver_info->driver) {
69 if (gfx_driver_info->id == graphics_card) {
70 gfx_driver_entry = (GFX_DRIVER*)gfx_driver_info->driver;
71 return !gfx_driver_entry->windowed;
72 }
73 ++gfx_driver_info;
74 }
75 return 0;// fullscreen status is unknown for graphics_card value
76}
77 
78 
79 
80/* is_a_definite_mode :
81 * Determines if graphics_card is definitively
82 * a windowed or fullscreen driver. This means
83 * GFX_AUTODETECT and GFX_TEXT return false.
84 * See is_a_windowed_mode for details about GFX_SAFE.
85 */
86int is_a_definite_mode(const int graphics_card) {
87 if (is_a_windowed_mode(graphics_card)) {return 1;}
88 if (is_a_fullscreen_mode(graphics_card)) {return 1;}
89 return 0;// graphics_card is not a definitive mode
90}
91 
92 
93 
94/* is_a_magic_mode :
95 * Determines whether graphics_card is one
96 * of the magic drivers, or is unknown.
97 */
98int is_a_magic_mode(const int graphics_card) {
99 switch (graphics_card) {
100 case GFX_AUTODETECT : return 1;
101 case GFX_AUTODETECT_FULLSCREEN : return 1;
102 case GFX_AUTODETECT_WINDOWED : return 1;
103 case GFX_SAFE : return 1;
104 case GFX_TEXT : return 1;
105 }
106 return 0;
107}
108 
109 
110 
111/* current_gfx_mode :
112 * Tells you which graphics mode is currently
113 * set. Useful for determining which driver that
114 * GFX_AUTODETECT* or GFX_SAFE set successfully.
115 */
116int current_gfx_mode() {
117 if (!gfx_driver) {return GFX_NONE;}
118 return gfx_driver->id;
119}
120 
121//

Naming conventions -

So, I have a couple questions :

1. Is the is_a_*_mode naming convention acceptable, or would people like to see something else?

If the current is_windowed_mode() function is renamed to in_windowed_mode(), then these functions could use a is_*_mode() naming convention, and I think in_windowed_mode would be a more appropriate name for it's purpose, since it checks the current graphics mode, and not an arbitrary one.

2. Do you see any improvements that could be made to the functions, or do you see any problems with them as they are?

Here is a zip file of the source code, and a zip file of a statically linked win32 executable of an example program that demonstrates the use of the functions to filter graphics modes used by the gfx_mode_select_filter function.

The program depends on Allegro 4.3.11+ for some fixes made to the gfx_mode_select* functions since 4.2 came out. Any revision more recent than August '08 should suffice. The program itself contains directions how to use it on screen.

I'll write up accompanying documentation for the manual tomorrow, and if everything's acceptable, I'll submit a patch for everything to [AD] for review.

Edit
Anyone have any constructive comments to make, or any preferences on the names of the functions before I submit them to [AD]?

Alianix
Member #10,518
December 2008
avatar

Looks neat.

1. I would not put is_* ,but rather use in_* instead because it makes more sense.

like in_magic_mode(...)

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

That would actually be a different set of functions though, because for these functions you can pass any graphics card that is recognized on your platform as the parameter, as opposed to finding out whether the currently set mode is one of those types. The second behaviour could be easily implemented using these functions though :

//
   int mode_set = current_gfx_mode();
   if (is_a_windowed_mode(mode_set))   {/* .... */}
   if (is_a_fullscreen_mode(mode_set)) {/* .... */}
//

However, the first four functions are generally more useful before you set a new graphics mode, and for customizing the allowable types of graphics modes that you wish to allow your program user to set. Also, the is_windowed_mode function already exists to test the current mode's type. If it were renamed to in_windowed_mode, then I could use the is_*_mode naming convention instead of the is_a_*_mode naming convention, but it seems fine to me either way.

Edit
After some suggestions by Peter, I combined the is_a_*_mode functions into a single get_gfx_mode_type function, and renamed current_gfx_mode to get_gfx_mode.

Here's the updated header addition :

//

/// Bitfield for relaying graphics driver type information
#define GFX_TYPE_UNKNOWN     0
#define GFX_TYPE_WINDOWED    1
#define GFX_TYPE_FULLSCREEN  2
#define GFX_TYPE_DEFINITE    4
#define GFX_TYPE_MAGIC       8

AL_FUNC(int , get_gfx_mode_type , (const int graphics_card));
AL_FUNC(int , get_gfx_mode      , ());

//

Reworked source code :

1//
2 
3/* get_gfx_mode_type :
4 * Evaluates the type of the graphics driver card
5 * and tells you whether it is a windowed, fullscreen,
6 * definitely windowed or fullscreen, and/or a magic driver.
7 */
8int get_gfx_mode_type(const int graphics_card) {
9 int gfx_type = GFX_TYPE_UNKNOWN;// 0
10
11 _DRIVER_INFO* gfx_driver_info = NULL;
12 GFX_DRIVER* gfx_driver_entry = NULL;
13
14 ASSERT(system_driver);
15
16 /* ask the system driver for a list of graphics hardware drivers */
17 if (system_driver->gfx_drivers) {
18 gfx_driver_info = system_driver->gfx_drivers();
19 } else {
20 gfx_driver_info = _gfx_driver_list;
21 }
22
23 ASSERT(gfx_driver_info);
24
25 while(gfx_driver_info->driver) {
26 if (gfx_driver_info->id == graphics_card) {
27 gfx_driver_entry = (GFX_DRIVER*)gfx_driver_info->driver;
28 if (gfx_driver_entry->windowed) {
29 gfx_type |= (GFX_TYPE_WINDOWED | GFX_TYPE_DEFINITE);
30 } else {
31 gfx_type |= (GFX_TYPE_FULLSCREEN | GFX_TYPE_DEFINITE);
32 }
33 break;
34 }
35 ++gfx_driver_info;
36 }
37
38 switch (graphics_card) {
39 case GFX_AUTODETECT :
40 gfx_type |= GFX_TYPE_MAGIC;
41 break;
42 case GFX_AUTODETECT_FULLSCREEN :
43 gfx_type |= (GFX_TYPE_MAGIC | GFX_TYPE_FULLSCREEN | GFX_TYPE_DEFINITE);
44 break;
45 case GFX_AUTODETECT_WINDOWED :
46 gfx_type |= (GFX_TYPE_MAGIC | GFX_TYPE_WINDOWED | GFX_TYPE_DEFINITE);
47 break;
48 case GFX_SAFE :
49 gfx_type |= GFX_TYPE_MAGIC;
50 break;
51 case GFX_TEXT :
52 gfx_type |= GFX_TYPE_MAGIC;
53 break;
54 }
55 return gfx_type;
56}
57 
58 
59 
60/* get_gfx_mode :
61 * Tells you which graphics mode is currently set.
62 * Useful for determining the actual driver that
63 * GFX_AUTODETECT* or GFX_SAFE set successfully.
64 */
65int get_gfx_mode() {
66 if (!gfx_driver) {return GFX_NONE;}
67 return gfx_driver->id;
68}
69 
70//

Updated source code of example using the functions.
Static win32 4.3.11+ binary of the example program.

Don Freeman
Member #5,110
October 2004
avatar

Looks good. Way to go Edgar!:D

--
"Everyone tells me I should forget about you, you don’t deserve me. They’re right, you don’t deserve me, but I deserve you."
"It’s so simple to be wise. Just think of something stupid to say and then don’t say it."

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Go to: