Archived

This topic is now archived and is closed to further replies.

Laroche

OO in Windows

Recommended Posts

I''ve managed to complete a game using OO, and learned alot in the process, but i dont know how to code a GUI in windows using OO to make it simpler for me. I''m working on a map editor right now and it has tons of globals and its generally VERY messy, and i''ve come to the point that I want to change something, but can''t, because it would break all the other code. The only solution in my mind is a complete re-write. The only thing is, I have no idea how to arrange all the classes with the windows and controls. Where should i put the Message Callback function? What kind of inheritance heirarchy should I use? What design patterns work here? If anyone has any links to tutorials, or can help out in any way, it would be GREATLY appreciated. Thanks

Share this post


Link to post
Share on other sites
Ok, firstly, what is your ''message callback'' function? Sorry if this is a common term in Win32 or something, but I wrote my GUI without much reference to existing GUIs. Do you mean something like the WindowProc? If so, I don''t have anything like that in my design. Instead, I associate actions directly with events by using function pointers, or function objects in C++. I think the only time this is inadequate is when you need to destroy a GUI element, in which case I can mark it for deletion and perform a sort of garbage-collection once all other GUI activity is done.

The basic design pattern for most C++ GUIs is Composite, with each GUI element/window/widget/whatever having several child elements, each of which can be a different subclass and therefore implement different functionality. The number of subclasses you want will really vary depending on your needs and your philosophy. Some people prefer numerous lightweight components and others prefer a few heavyweight ones. (eg. Separate frame, panel, radio button group, and picture components on the one hand, or an all-purpose PicturePanel component on the other.) Another pattern you may find useful is the Factory Method, especially if you need to dynamically build up a GUI from a disk file rather than hard-code window positions etc. The Decorator pattern could theoretically be useful for GUIs too although I don''t use it myself. And the Chain Of Responsibility pattern is commonly used for handling events. If you have the Design Patterns book, look into Mediator, Observer, and Command too, as they all have some relevance.

There''s a good article here on GameDev.net about making a GUI although it''s a bit lacking on the specifics of some operations. It should give you a hint, though.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]

Share this post


Link to post
Share on other sites
If you are looking for an OO approach to UI design in Windows, take a look at the Microsoft Foundation Classes, or MFC. This library provides an OO framework for traditional Win32 UI programming. Each item in the UI is broken down into objects. Some examples are a Window Object, Frame Object, Document Object, View Object, CButton, CListView, etc... Each control is an object, with methods and event handler functions. This means there is no main WndProc function. Each object handles their own events. Take a look at the MSDN library as well as the Articles & Resources Section here. There are some simple examples of using MFC.

-----------------------------
kevin@mayday-anime.com
http://www.mayday-anime.com

Share this post


Link to post
Share on other sites
These tutorials approach Win32 in an OO fashion: http://www.relisoft.com/win32/



God puts an apple tree in the middle of the Garden of Eden and says, do what you like guys, oh, but don''t eat the apple. Surprise surprise, they eat it and he leaps out from behind a bush shouting "Gotcha." It wouldn''t have made any difference if they hadn''t eaten it... because if you''re dealing with somebody who has the sort of mentality which likes leaving hats on the pavement with bricks under them you know perfectly well they won''t give up. They''ll get you in the end. -- Douglas Adams

Share this post


Link to post
Share on other sites
I''m not trying to sound grim, but just realistic. Trying to wrap classes around the Windows API effectively is one of the most difficult tasks I''ve ever seen a programmer undertake.

It''s one of those scenarios you have to complete mostly on your own. Thirty people could give you thirty different approaches and not a single one of them would be perfect in any sense.

I guess what I''m saying is don''t take anybody''s advice verbatim. Pick and choose appropriately.


----
AIDS

Share this post


Link to post
Share on other sites
quote:
Original post by TheAIDSVirus
Trying to wrap classes around the Windows API effectively is one of the most difficult tasks I''ve ever seen a programmer undertake.


how so? the only interesting part of doing that is translating callback calls to member function calls. there are already many existing methods, some of which work better than others. aside from that, you''ll need a load of inlines and a ton of patience if you want to wrap more than a handful of functions.

now adding functionality on top of windows, such as splitter windows, printing, frames, and the such, is a whole different story, but it''s not really made significantly easier by wrapping or unwrapping winapi.

Share this post


Link to post
Share on other sites