• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
schupf

Writing portable code

12 posts in this topic

Hi,

I have a program that needs to compile and run under Linux and Windows. I have a Dialog class that draws a very simple Dialog (on Windows it uses plain WinAPI, on Linux... I don't know. Haven't been so far yet;)

Should I know write two totally different classes (one for windows and one for linux) and just exclude the one that does not fit to the current platform or should I make one single class (one .cpp and one .h) and just code the platform specific parts inside the methods with #ifdefs?
1

Share this post


Link to post
Share on other sites
I would make a single class, seems like it would be easier to maintain and there would be a bit less redundant code.
0

Share this post


Link to post
Share on other sites
Yes, its certainly easier to maintain, but I fear the could would be a total mess. Each function is totally platform dependent (for example I have a method like Dialog::fillResolutionComboBox() - filling a combobox on Win32 is totally different compared to Linux). Moreover I am using a lot of DirectX types, which are also bound to Windows:/
0

Share this post


Link to post
Share on other sites
[quote name='phantom' timestamp='1308504543' post='4825148']
One way of solving this is with one class but with multiple files.

So, you have a single header which defines the class interface, then you have 3 .cpp files; one which has common code in it which works for both platforms, then in the platform specific .cpp files you only do the functions which need to be done for that platform specifically. Then when building on a platform you only build the .cpp files for that platform + common.

This might require some #ifdef stuff in the header to sort out platform specific header information but that is going to be cleaner than having inline #ifdef stuff in a single .cpp file.
[/quote]

I endorse this suggestion.


As someone who has been writing portable code for years, my general recommendation is to stay the hell away from absolutely any library or framework that is not well implemented and well supported on your target platforms (note: some libraries claim to be "cross-platform", but their implementation really sucks on certain systems or is not fully complete). Don't use DirectX, use OpenGL (unless you want to provide both DX and OGL implementations to your users, which some games do). Don't use WinAPI, use SDL for window and input management. And so on. Of course this is just a suggestion and you can choose to use platform-specific libraries if you want, but in the long run I think it will be more work than what it is worth. Cross-platform development is already a challenge so I don't see why you would want to make it even more difficult, unless you take pleasure in pain...:o
0

Share this post


Link to post
Share on other sites
Ok this basically means I have a Dialog.h, a Dialog.cpp (platform independent stuff), DialogWin.cpp and DialogLinux.cpp. And in my Windows project I just exclude DialogLinux.cpp from compilation. Is this correct?
Should I make this exclusion by project settings (makefile etc.) or with #ifdefs, so that DialogLinux.cpp gets to the compiler but isn't compiled since everything is "commented out by ifdef"?
0

Share this post


Link to post
Share on other sites
[quote name='ChaosEngine' timestamp='1308526644' post='4825266']
Why don't you just use a cross platform GUI library, like WxWidgets or QT?
[/quote]

+1.
There are already a lot of cross platform GUI libraries, and some are quite lightweight.
wxWidgets is lightweight, I believe some others are more lightweight than it.

So we don't need to reinvent the wheel.

I would spend my most time on app logic/features, etc, rather than cross platform issues when there are libraries can handle them.
0

Share this post


Link to post
Share on other sites
Well, because I really just need ONE Dialog and I hate dependencies. Even one DLL or .so are too much in my opinion for just one window.

If I picked a GUI toolkit I would definitely go for Qt. But I neither know if it is allowed to link Qt statically nor do I think the extra amount of work (compiling Qt statically will probably last hours) for just ONE Window...
0

Share this post


Link to post
Share on other sites
[quote name='phantom' timestamp='1308504543' post='4825148']
This might require some #ifdef stuff in the header to sort out platform specific header information but that is going to be cleaner than having inline #ifdef stuff in a single .cpp file.
[/quote]

If this gets to be messy, you can also use a pimpl-esque struct containing the member data for each platform:
[code]
// Header File

struct DialogData;

class Dialog
{
private:
DialogData* data;

...
};
[/code]

[code]
// CPP File for Windows

struct DialogData
{
HWND windowHandle;
...
};

Dialog::Dialog()
{
data = new DialogData();

// Create window, do other stuff
}
[/code]

It adds an extra pointer indirection whenever the class needs to access some of its data, but keeps your header files cleaner. It can also help prevent the platform-specific headers from leaking out into cross-platform code.
0

Share this post


Link to post
Share on other sites
[quote name='schupf' timestamp='1308556290' post='4825361']
Well, because I really just need ONE Dialog and I hate dependencies. Even one DLL or .so are too much in my opinion for just one window.

If I picked a GUI toolkit I would definitely go for Qt. But I neither know if it is allowed to link Qt statically nor do I think the extra amount of work (compiling Qt statically will probably last hours) for just ONE Window...
[/quote]
If you plan to port your code to Linux, you will have to use some GUI library anyways I think. Writing bare XWindows interface is possible, but nothing I'd suggest anyone to do - especially if you only need one dialog ;-)
And if you have to use some GUI library anyways, why not use something like wxWidgets for both Windows and Linux versions? You can link it statically, so only the needed parts of the library will be included in your program - probably about 100kb or less. Yes - it is slightly bigger than a dialog written by bare WinAPI - but in my opinion you save a lot more in source code complexity.

0

Share this post


Link to post
Share on other sites
[quote name='phantom' timestamp='1308504543' post='4825148']
Then when building on a platform you only build the .cpp files for that platform + common.
[/quote]

Hooray for link-time polymorphism! [img]http://public.gamedev.net/public/style_emoticons/default/biggrin.gif[/img]


0

Share this post


Link to post
Share on other sites
[quote name='schupf' timestamp='1308556290' post='4825361']
Well, because I really just need ONE Dialog and I hate dependencies. Even one DLL or .so are too much in my opinion for just one window.

If I picked a GUI toolkit I would definitely go for Qt. But I neither know if it is allowed to link Qt statically nor do I think the extra amount of work (compiling Qt statically will probably last hours) for just ONE Window...
[/quote]
I statically linking against it possible, but is not recommended. Qt is huge.

[url="http://doc.qt.nokia.com/latest/deployment.html#static-vs-shared-libraries"]http://doc.qt.nokia.com/latest/deployment.html#static-vs-shared-libraries[/url]
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0