Wrap around for Win32

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • Wrap around for Win32

      Hi guys,

      I am writting a wrap-around class for win32 for my game project and need some help/advice. Heres the problem; I have a class called "CGameApp" which the functions responsable for creating a OpenGL/win32 window is located along with other functions.

      Brainfuck Source Code

      1. BOOL CGameApp::CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag)
      2. {
      3. .
      4. .
      5. .
      6. m_bFullScreen=fullscreenflag;
      7. m_hInstance = GetModuleHandle(NULL);
      8. m_WndCls.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
      9. m_WndCls.lpfnWndProc = (WNDPROC) WndProc; // <--------- PROBLEM!!
      10. m_WndCls.cbClsExtra = 0;
      11. m_WndCls.cbWndExtra = 0;
      12. .
      13. .
      14. .
      15. more code.
      Display All



      Source Code

      1. //LRESULT CALLBACK CGameApp::WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
      2. int CGameApp::WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
      3. {
      4. switch (uMsg) // Check For Windows Messages
      5. {
      6. case WM_ACTIVATE: // Watch For Window Activate Message
      7. {
      8. if (!HIWORD(wParam)) // Check Minimization State
      9. {
      10. m_bIsRunning = true; // Program Is Active
      11. .
      12. .
      13. .
      14. more code.
      Display All



      Source Code

      1. bool CGameApp::InitInstance( void )
      2. {
      3. if ( !IsOnlyInstance( GetGameTitle() ) )
      4. return ( false );
      5. CreateGLWindow(GetGameTitle(), 50, 50, 16, false);
      6. return ( true );
      7. }


      Source Code

      1. INT WINAPI WinMain( HINSTANCE hInstance, HINSTANCE, LPSTR, INT )
      2. {
      3. int tmpDbgFlag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
      4. _CrtSetDbgFlag(tmpDbgFlag);
      5. g_pApp->InitInstance(); // This will initilize the windows part of the game.
      6. // g_pGame->InitGame();
      7. // g_pGame->GameLoop();
      8. // g_pGame->GameShutdown();
      9. _CrtDumpMemoryLeaks();
      10. return 1;
      11. }
      Display All


      When I build the project I get the following message;
      "error C2440: 'type cast' : cannot convert from 'overloaded-function' to 'WNDPROC'"

      How can I get around this probem?

      Thanks again for the help :)

      Sabrina

      The post was edited 1 time, last by Sabrina ().

    • RE: Wrap around for Win32

      Opps, Here is where the problem is;

      I section of code is located in the CreateGLWindow(...)" method.

      Source Code

      1. m_WndCls.lpfnWndProc = (WNDPROC) WndProc;


      I moved "LRESULT CALLBACK WndProc(...)" method outside the class and attatched "static" just before LRESULT..., but still did not have much luck clearing up the problem.

      I tryed a few type casting, but again I was not able to clear up the problem.


      Sabrina.
    • RE: Wrap around for Win32

      Hmm....if WndProc is a function, try putting a & in front of it on that line you just posted. Also, make sure that the rerturn value and parameter list are exactly the same as they need to be.

      Moving the function outside the class makes the "static" redundant. You only need to use one of those solutions. I actually can't think of offhand what putting "static" in front of an already inherently static function will do, but it may be something radically different (in both Java and C++, the word static is heavily overloaded, meaning radically different things in radically different places. More so in Java, since it also doubles as const).
      -Larrik Jaerico

      www.LarrikJ.com
    • RE: Wrap around for Win32

      In order to the WndProc in a class it has to be static to avoid having the hidden this pointer passed in

      do you define STRICT before including the windows header.

      #define STRICT
      #include <windows.h>

      in your original post your WindowProc function returns an int and WindowProc's return LRESULT

      gb
    • RE: Wrap around for Win32

      Hi,

      Nope, none of the suggestions have been working. I tryed those before I made the post.

      The "#define STRICT" does nothing to help solve the problem and in fact has nothing to do with the problem at hand. The only purpose of STRICT that I am aware of is to enforece the highest possible level of type checking. When I attempt to built the project I already know there is a problem, so I know how/why STRICT will provide any other information.


      Sabrina.
    • You have to add the 'static' keyword to the method declaration in your CGameApp.h file. As garbob said, it avoids the implicit this pointer that gets passed to objects when an object method is called. This is the only way to get a Win32 WndProc function working correctly if it's to be a class method.

      This will give you some other problems though, mainly the access to the class variable m_bIsRunning. To reason this causes the compiler to error out is because you can't access "instance level" class members in a "static context". Basically that means that if you're in a static class method, you can't access any class member that DOESN'T have a static qualifier attached to it; you need to have an object of that class to act upon.

      There's a relatively clean and "correct" way to handle this though. I don't remember the syntax for this, so you'll have to look it up more in detail, with the first place to look being the WM_CREATE message on MSDN. The gist of it is that when you call CreateWindow/CreateWindowEx, one of the last parameters allows you to pass a pointer to a structure that allows you to send specific data to the WM_CREATE message, which will get handled by WndProc. As you can imagine, that's the message that's passed right when the window is being created. The pointer to the structure that you passed in is found in either the lParam or the wParam parameter, but I can't remember to be honest. So basically all you have to do is right before you make your call to CreateWindowEx, you create one of these structures, you package the 'this' pointer into it, then ship a pointer to that structure off in your call window creation call. Then inside your WndProc you'll add a handler for the WM_CREATE message, pull out the structure pointer, pull out your 'this' pointer from that structure (which is really just a pointer to an object of your CGameApp class), then do something like object_pointer->m_bIsRunning instead of just m_bIsRunning.

      I know that was a hasty reply, but I'm a rush to get to work on time. If you have any questions about it, please post them and I'll try to clarify that mess I just wrote up. Hope that helps.
    • RE: Wrap around for Win32

      I've solved you basic compiling problem.

      I've also noticed that you single instance code is a little shaky. FindWindow can't be trusted for this kind of thing. it be fouled by multi-threading. It may work for what you're doing here. i'm not sure. It's best to do it the right way. You can get solutions for this a codeproject.com

      You have this project setup to use .NET is that what
      you want? Are you using the free compiler toolkit?

      You're CreateGLWindow code is crashing.

      I wasn't sure how much help you wanted it

      I've attached my changes

      note that i created a global variable theGameApp
      This is the easiest way to access class instance variables in a static function of a singleton class

      gotta go, got a day job. Good luck!

      gb
    • RE: Wrap around for Win32

      A lot of my contracts are VERY concerned about messenger based viruses and intruder hacks
      Had to give up my MSN music too, lucky for me i've got a deep CD collection

      As for as the code goes, I get alot of practice working the puzzles in the Gimple software's PC-Lint ads in mags like Dr. Dobbs, C++ User's Journal
    • RE: Wrap around for Win32

      Hi,

      Thank you garbob for taking the time to look at my code
      and providing fixes! :D

      First, I'm frustrated and I'm about to throw this project
      to the wind, but I'm think I will give this one more
      chance. Hopefully, I will get more responses and
      help with this project. Anyway, I kinda thought my single
      instance code might have been incorrect or "shaky" as
      you put it. I took it from the authors book and other
      information on the Internet regarding singletions and
      their applications. Second, I built the project you posted,
      and it works for me. The window pops up and then
      suddently quits, at least we got that far! :] I added, a
      message pump and the windows stays up, BUT I when I
      click on the close button, nothing happens. I think its not
      sending any messages to tell it to shutdown. Any
      thoughts? Also, since you've seen how I am codding the
      "main" part, any ideas on how to intergate the message
      pumping method? I think my game logic has to reside
      inside that pumping loop, right? If thats the case, I think
      I might be heading down the wrong way as far as
      design.

      I'm using Visual Studio 2003 .NET so hopfully I'm on the
      "same page" as other developers.

      I'm still thinking/interpreting about what you said MRom. :rolleyes:
      I will need sometime to "digest" its contents.

      If you guys have the time and energy, I'd like some more
      feed back on this warp around project.

      Anyway, thanks again,

      Sabrina.
    • RE: Wrap around for Win32

      Come on, don't give in yet! You can make it if you stick to it. games aren't the easiest things in the world to write and C++ isn't the easiest language in the world. Dang its cool when you getting goin' though!

      make sure that the window is getting a WM_CLOSE message and that it is doing a PostQuitMessage(0) and returning 0 from the Window Proc

      then does the message pumping loop exit?
      Did you remember to shutdown the OpenGL?

      Post an update, I'll be glad to look it over again
      If you stay in there swingin' and clawin' for it I'll be with ya

      gb
    • RE: Wrap around for Win32

      It wouldn't be a terrible idea to pick up LaMothe's "Tips of the Windows Game Programming Gurus" It's DirectX rather than OpenGL, but that's hardly a reason to ignore it. There may be better books out there for the subject, but I don't know of any off the top of my head.

      (It's gigantic, though, and LaMothe isn't as good a read as a McShaffry :P)
      -Larrik Jaerico

      www.LarrikJ.com