Platform Independent Graphics Classes

Started by
15 comments, last by LordElectro 23 years, 1 month ago
quote:Original post by LordElectro

My approach, on paper at least, works beautifully and will result in a perfectly platform independent *game* assuming u write an implementation of the graphics library for each API u want the game to run on. So about a week or so of work. I intend to stay away from Win API functions, except in the map editor, which for now will have to be Windows only, but boo hoo. Now, functions and stuff work fine, but when i want to pass data to a function through a structure, or receive such data, i cannot use the DirectX (or other APIs'') structures. This kinda ticks me off, because first off, i have to re-invent the wheel and make my own structures that are more or less the same as the DirectDraw ones. Then i have to write and more importantly, call functions that convert my graphics library structures to API specific structures. I was hoping there is a better solution for this, but no one has really jumped up and slapped me about my approach, so im guessing im forced to do this. It''s not terribly bad, since stuff like blt doesnt need structures passed to it, but it IS an annoyance.



LordElectro, it''s been a while since I''ve used DirectDraw, but what kind of structure do you need to pass to your DirectDraw graphics API? Can you give an example?


- Houdini
- Houdini
Advertisement
DirectDraw has structures for a lot of things, but key concerns are:

Surface description strucuture
Device capabilities/description structure
Color key structure

There''s more, but u get the idea. My library, being based a lot on the DD archetecture (seeing as it is probably only ever going to be for DD, i just want to leave myself the ability to be able to port it in the future, or even upgrade it to DirectDraw9 if MS ever make it) uses similiar structures. it would be really nice if i didnt have to convert my structures to DD structures, but having thought over it for a week, i see no other way


Get Banner At: http://www.crosswinds.net/~druidgames/resist.jpg
BetaShare - Run Your Beta Right!
quote:Original post by LordElectro

i dont think u guys *quite* get what i mean. I''ll try one more shot at explaining, here goes

My approach, on paper at least, works beautifully and will result in a perfectly platform independent *game* assuming u write an implementation of the graphics library for each API u want the game to run on. So about a week or so of work. I intend to stay away from Win API functions, except in the map editor, which for now will have to be Windows only, but boo hoo. Now, functions and stuff work fine, but when i want to pass data to a function through a structure, or receive such data, i cannot use the DirectX (or other APIs'') structures. This kinda ticks me off, because first off, i have to re-invent the wheel and make my own structures that are more or less the same as the DirectDraw ones. Then i have to write and more importantly, call functions that convert my graphics library structures to API specific structures. I was hoping there is a better solution for this, but no one has really jumped up and slapped me about my approach, so im guessing im forced to do this. It''s not terribly bad, since stuff like blt doesnt need structures passed to it, but it IS an annoyance.

Anyway thanks, and i welcome (demand actually) any feedback on this appraoch.


Get Banner At: http://www.crosswinds.net/~druidgames/resist.jpg


save yourself some( a lot ) of work
http://www.libsdl.org/

quote:Original post by zedzeek

Original post by LordElectro

i dont think u guys *quite* get what i mean. I''ll try one more shot at explaining, here goes

My approach, on paper at least, works beautifully and will result in a perfectly platform independent *game* assuming u write an implementation of the graphics library for each API u want the game to run on. So about a week or so of work. I intend to stay away from Win API functions, except in the map editor, which for now will have to be Windows only, but boo hoo. Now, functions and stuff work fine, but when i want to pass data to a function through a structure, or receive such data, i cannot use the DirectX (or other APIs'') structures. This kinda ticks me off, because first off, i have to re-invent the wheel and make my own structures that are more or less the same as the DirectDraw ones. Then i have to write and more importantly, call functions that convert my graphics library structures to API specific structures. I was hoping there is a better solution for this, but no one has really jumped up and slapped me about my approach, so im guessing im forced to do this. It''s not terribly bad, since stuff like blt doesnt need structures passed to it, but it IS an annoyance.

Anyway thanks, and i welcome (demand actually) any feedback on this appraoch.


Get Banner At: http://www.crosswinds.net/~druidgames/resist.jpg


save yourself some( a lot ) of work
http://www.libsdl.org/



No for two reasons:

1) I want to write my own library. That''s the point of all this, to try my hand at writing code that isn''t rooted in Windows. To show myself that it is possible, and that Bill Gates doesn''t have to be sitting there on my neck, and jumping down on my back. I want the PRACTICE. If i just cared about the graphics, I would be done by now, and have written a simple but effective set of wrapper classes for DirectDraw.

2) The particular library you pointed out is C, i need a C++ one :p

Anyway, I am gonna go the route of making my own structures too. It will keep me from being lazy but oh well


Get Banner At: http://www.crosswinds.net/~druidgames/resist.jpg
BetaShare - Run Your Beta Right!
One other method to consider is quite different; Instead of using the base class/derive class thing, create a platform and API-neutral interface for the class in your header file. Then in your source files for implementing the class for a certain API/platform, use static global variables for all of the platform or API-specific stuff. For example, your LPDIRECTDRAW stuff or HGLRC stuff, or whatever would be in there that's not neutral. Now, you're probably tempted to jump on me for even mentioning global variables. But note that I said static global variables, which means that they'll never be accessable from anywhere else. And since these source files should contain the implementation of only that class, the effect is identical to having those variables as members of that class, except that those non-neutral variables aren't exposed in the interface.

These are the advantages of this method:
- the header file isn't littered with #ifdefs and exposes only one platform and API-neutral interface
- there's no need for virtual functions since there's no need for inheritance (this will give you a bit of speed), and you can go as far as to make all functions static so there's no need to thrash around a this pointer, assuming you'll only need one instance of the class
- you still can build the implementation into a DLL and use that to choose the implementation you want to use at runtime (this negates the static function case)


Edited by - merlin9x9 on March 26, 2001 4:06:20 AM
quote:Original post by LordElectro
upgrade it to DirectDraw9


Direct3D/DirectXGraphics 9.


Just because you''re outnumbered doesn''t mean you''re wrong.


www.directdeveloper.co.uk

quote:Original post by LordElectro

DirectDraw has structures for a lot of things, but key concerns are:

Surface description strucuture
Device capabilities/description structure
Color key structure

There''s more, but u get the idea. My library, being based a lot on the DD archetecture (seeing as it is probably only ever going to be for DD, i just want to leave myself the ability to be able to port it in the future, or even upgrade it to DirectDraw9 if MS ever make it) uses similiar structures. it would be really nice if i didnt have to convert my structures to DD structures, but having thought over it for a week, i see no other way



I think your problem is that you need to make your graphics API more "high level". Your main program shouldn''t have to deal with those structures you mentioned, except for the possible exception of the device capabilities.

As far as surface description goes, all you need to pass to your Surface class to create a new surface is the Width, Height, BPP, and Fullscreen. Your surface class would create an instance of the DDSURFACEDESC2 and fill in elements from the parameters you passed, and fill in the rest of the necessary elements by itself.

Same with color key. You could pass an RGB color variable to your surface class that just tells it the color to set the color key for your surface. The surface class would call DDSetColorKey, and pass that returned structure to SetColorKey.

For display modes, I would recommend querying all modes, and store those structures in a linked list, all done automatically by your graphics API. If your main program is just going to ask if a specific mode is available, it just calls IsDisplayModeAvailable from your graphics API, passing the width, height, bpp, etc. This function would return a bool true or false value.

If you want to retrieve all good display modes, then I would suggest creating a simple generic structure that holds the basic information needed, and you can pass the whole linked list back to the main app.

Overall, making your graphics API higher level makes it much easier to make it API independant, plus makes it easier to program with. There will be a slight performance decrease, but it''s a small price to pay for usability and maintainability.


- Houdini
- Houdini

This topic is closed to new replies.

Advertisement