Archived

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

Should I use main() or WinMain() ?

This topic is 6009 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m programming in Win32 MSVC++6.0 and used always WinMain as an entry point. But I can''t resist the idea of making multiplatform apps so I should use main() instead of WinMain(). What is the difference. If I use for example CreateWindow(...), I need the hInstance that is given in WinMain(...). Can I create a window using CreateWindow(...) in main(...) and get the hInstance using GetModuleHandle(NULL)? How does multiplatform apps (for example glut library) create window? Also if I make a glut program and add there DirectSound implementation. I think I need the HWND handle to create DirectSound, but how can I get it out from glut window?

Share this post


Link to post
Share on other sites
quote:
Original post by stefu
I''m programming in Win32 MSVC++6.0 and used always WinMain as an entry point. But I can''t resist the idea of making multiplatform apps so I should use main() instead of WinMain(). What is the difference.

If I use for example CreateWindow(...), I need the hInstance that is given in WinMain(...). Can I create a window using CreateWindow(...) in main(...) and get the hInstance using GetModuleHandle(NULL)?
How does multiplatform apps (for example glut library) create window?

Also if I make a glut program and add there DirectSound implementation. I think I need the HWND handle to create DirectSound, but how can I get it out from glut window?


When you create Win32 apps, you have to use WinMain. If you don''t, you get a compiler error. Main() is for Console apps. Basicly, WinMain is sort of similar to when you create your Game Loop, it''s part of a the Windows program. It needs to be defined to run. ( I know, I''m not that clear when I try to explain but hey. )...


"And that''s the bottom line cause I said so!"

Cyberdrek
Headhunter Soft
A division of DLC Multimedia

Resist Windows XP''s Invasive Production Activation Technology!

Share this post


Link to post
Share on other sites
So if I make a Console app, I can''t use any Win32 functions? I can''t use DirectX? How can I create a window in console app?

Share this post


Link to post
Share on other sites
If you want to make multiplatform apps, what are you doing then with win32 API functions? These only work on, surprise surprise, win32. Not much crossplatform, is it?

If you really want crossplatform apps, develop your progs with crossplatform libraries, like glut. To do the sounds, you''ll have to find another lib, glut and opengl only do gfx.

I use glut myself, not that much because I want multiplatform development, but because I don''t want to learn win32 api. In the dev phase, I use main(), because it allows me to easily print debug messages to the console. When I think it is ready to be tested by other people, I will change main() to WinMain(), just to get rid of the console.

Share this post


Link to post
Share on other sites
Well, my point was for example to support DirectX in Win32. I know there is OpenAL for sounds. What good they are? I heard tey are scrap? If you know a list of multiplatform libraries, give me the url. Thanks.

Share this post


Link to post
Share on other sites
You don''t need multiplatform libraries to create cross-platform applications. As long as you have enough abstraction, you should just be able to replace your Win32Sound class with your LinuxSound or MacSound class and compile under those other platforms. Of course, that means you''ll have to learn a different API for each platform, but the chances of a single-person project getting past the tech. demo stage and still being multiplatform are pretty slim anyway (simply because it''s very difficult to keep your code cross-platform, plus I personally don''t think it''s worth the trouble).

Note that I said "pretty slim" and "very difficult", not "zero" and "impossible". It is of course possible for a single person to write a complete multiplatform game, but I''ve never heard of anyone actually doing it to completion.

Share this post


Link to post
Share on other sites
Just want to point aout there is a Main() in the win32 Libs . Anyway the easiest way to make cross platform Apps including sound , input etc use something like SDL or ClanLib. SDL will let you use DX acceleration , and OGL app in windows , and OGL in Linux , Beos etc.

Share this post


Link to post
Share on other sites
I just want to reassert what Dean said. Abstraction! It is what makes C++ and OOP so great.

Using truly crossplatform libraries will cut down on development time a little bit, maybe. But at the cost of performance. You will never get the speed from cross-platform libraries that you will from dependent libraries. There is a reason the are platform dependent i.e.: They use specific features of the platform in the way they were meant to be used.

Normally APIs are not a whole lot different between platforms. And they should take up only a small portion of the code you write for your game. If you encapsulate sound, graphics and input in your own classes, and only call API methods from those classes, it is only a matter of rewriting the wrapper classes to port them to the next platform.

I will tell you from lots of experience that people who write cross-platform applications do not do so using cross-platform libraries. Atleast most don''t. There are a few oddballs who have the hair in the wrong place, but most will use abstraction and then port the platform dependent classes. It''s just a happier marriage that way.

Seeya
Krippy

Share this post


Link to post
Share on other sites
To get ontopic again:

You can also use WinMain AND main

  
int main(int argc, char** argv)
{
// Blah

// ...


return 0;
}

#if defined(_WIN32)
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)

return main( __argc, __argv );
}
#endif


You can also set the entry point symbol (menu Settings -> Link -> Output -> Entry-point symbol) of your program (windows version) to "mainCRTStartup" wich calls main instead of WinMain...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Until about a week ago it was pretty hard to write a cross-platform app that used a GUI. However, Trolltech (www.trolltech.com) have just released their QT cross-platform GUI library for free under windows (it had been free under linux for a while). It compiles under Win32, Linux, Mac OSX, and (I think) BeOS, with NO changes. As far as other cross-platform stuff. I''d go with the SDL (www.libsdl.org) to 2d and image surface stuff, and (of course) OpenGL and GLUT for the 3d stuff.

In summary:
1.) QT (www.trolltech.com) (This thing is Friggin'' Awesome! Check it out!)

2.) SDL (www.libsdl.org) (This also has some simple gross platform GUI libs written on top of it, so you might be able to use those too).

3.) OpenGL (www.opengl.org)

Share this post


Link to post
Share on other sites