wxDev-C++ and the game loop Topic is solved

If you are using wxDev-C++ for your wxWidgets design, please ask your questions here instead of in IDE Related.
Post Reply
Mixael
Earned a small fee
Earned a small fee
Posts: 13
Joined: Thu Apr 27, 2006 6:24 pm

wxDev-C++ and the game loop

Post by Mixael » Sun Jan 07, 2007 10:54 pm

I've been working with (meaning trying to learn) Dev-C++ to try to make a game (or at least something that looks like a game :) ). A day or two ago I decided to move it over to wxDev-C++. The part of it that is giving me problems is the game loop. here's the code that I have in the older project, and I need to find a similar thing in wxDev-C++:

Code: Select all

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    //Registering the Window Class
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(0);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // Creating the Window
    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        "Programmed by Mixael",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 600, 480,
        NULL, NULL, hInstance, NULL);

    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

  frmHWND = hwnd;

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

   //Initialize the engine
   InitTV();


   while (DoLoop)
   {
      MSG msg;
      HACCEL hAccelTable;
      hAccelTable = LoadAccelerators(hInstance, (LPCSTR)IDC_TEMPLATE);
            // get and handle messages
      if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
      {
         if (msg.message == WM_QUIT)
            break;
         if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
         {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
         }
      }
      else
      {

      if(GetFocus()==frmHWND)
      {
        MainFunc();

        if(pNput->IsKeyPressed(cTV_KEY_ESCAPE))
        {
          //KillEngine();
          SendMessage(frmHWND,WM_DESTROY,0,0);
        }
      }
      else
      {
        Sleep(100);
      }

   }
  }

    return Msg.wParam;
}
Any takers on helping me on this? I need the ability to do somethuing like VB6's 'DoEvents' thing.

Thanks for your concideration,
Michael

buildere
Super wx Problem Solver
Super wx Problem Solver
Posts: 358
Joined: Thu Oct 28, 2004 3:45 pm
Location: Costa Rica

Post by buildere » Mon Jan 08, 2007 1:35 am

Since wxDev-Cpp is just an extension to Dev-Cpp, it should run the code you posted exactly as Dev-Cpp did for you.
However, if you are trying to convert to wxWidgets (wich I would recommend :) ) then you should create a default wxProject so you see how does the code for a form window initialization looks like, since the code you posted appears to do just that, with the exception of the hardwareAccelerators table thing, that I don't really have experience with, but looks like something for keyboard key-press detection or something anyway... if that's true you'll be surprised to see how easy it is to do that in wxDevCpp designer, where you just add event detection (similar to what the loop does in the code you posted; in wxWidgets world the equivalent to that loop is "encapsulated" in the project's dialog or form classes) by entering a new event in the properties inspector. There are even event handlers for joystick events.
Last edited by buildere on Mon Jan 08, 2007 5:35 pm, edited 1 time in total.

Mixael
Earned a small fee
Earned a small fee
Posts: 13
Joined: Thu Apr 27, 2006 6:24 pm

Post by Mixael » Mon Jan 08, 2007 1:55 am

Thank you for the prompt answer. I actually came back to this post to check something about the code, as I had an epiphany (is that right?) about it - basicly what you said :)

And, yess, I AM trying to convert to wxWidgets. The code I posted was an excercise in getting a TrueVision3D app to work in Dev-C++, so I used an existing VC++ source file and cut it to peices until I got it working. And I don't kow what the hardwareAccelerators table does,either :)

So, it seems that I will be experimenting and try to get this going.

Michael

lowjoel
Moderator
Moderator
Posts: 1511
Joined: Sun Jun 19, 2005 11:37 am
Location: Singapore
Contact:

Post by lowjoel » Mon Jan 08, 2007 8:51 am

I'm not sure how you will get the main stuff to work, but I know that if you wanted to use openGL to do your 3D stuff wxWidgets has bult in support for it. Just a thought (because I was trained in OpenGL last year for my computer science course)

Mixael
Earned a small fee
Earned a small fee
Posts: 13
Joined: Thu Apr 27, 2006 6:24 pm

Post by Mixael » Mon Jan 08, 2007 6:26 pm

Well, lowjoel, getting "the main stuff to work" isn't really the problem...provided I can get a "game loop" that actually works properly. I haven't done the tests I need, as I was busy with family and real life, but If I were to have more questions/problems, I shall post here :)

Thanks for the offer, though!

Michael

Mixael
Earned a small fee
Earned a small fee
Posts: 13
Joined: Thu Apr 27, 2006 6:24 pm

Post by Mixael » Mon Jan 08, 2007 10:46 pm

buildere wrote: if that's true you'll be surprised to see how easy it is to do that in wxDevCpp designer, where you just add event detection (similar to what the loop does in the code you posted; in wxWidgets world the equivalent to that loop is "encapsulated" in the project's dialog or form classes) by entering a new event in the properties inspector.
I've been doing a few tests, and the portion of your reply that I quoted is the pertinant portion now. For the "game loop", I need the tightest loop I can get..no problem. HOWEVER, I need to detect OTHER events, so that they can be responded to. THAT is the problem I now face.

If I/we get this one solved, the rest is no problem, as it would be the same as any other setup (in other words, writing the actuall code for the game :) ). As a test, I'm trying to have to command buttons and a text control on a form. One button sends it to a loop, the next stops it. Here is some psuedo code for what I mean:

Code: Select all


button1.click
  loop=true
  while (loop)
    <do the stuff for my project>
    <event handler to see if there be something needed>
    //end of loop
//end button1.click

button2.click
  loop=false
//end button2.click
So that button one initiates the loop, during which we can check for other events (say, button two being clicked), and goes forever, until button two is clicked. Is that confusing to you, or just me?

In any event, THAT is what I'm after now. It is essentially what the visual basic "DoEvents" does.

Michael

tbreina
Moderator
Moderator
Posts: 1289
Joined: Thu Nov 25, 2004 7:38 pm
Location: Coronado, CA

Post by tbreina » Mon Jan 08, 2007 11:50 pm

If I/we get this one solved, the rest is no problem, as it would be the same as any other setup (in other words, writing the actuall code for the game :) ). As a test, I'm trying to have to command buttons and a text control on a form. One button sends it to a loop, the next stops it. Here is some psuedo code for what I mean:
Take a look at wxYield or wxSafeYield (http://www.wxwidgets.org/manuals/2.8.0/ ... xsafeyield). They should allow you to temporarily interupt your for loop to look for other events.

Also, instead of 2 buttons, you can use a single wxToggleButton for your 'on'/'off' events.

-Tony
Everybody's got something to hide except for me and my monkey.

Mixael
Earned a small fee
Earned a small fee
Posts: 13
Joined: Thu Apr 27, 2006 6:24 pm

Post by Mixael » Tue Jan 09, 2007 12:24 am

tbreina wrote: Take a look at wxYield or wxSafeYield (http://www.wxwidgets.org/manuals/2.8.0/ ... xsafeyield). They should allow you to temporarily interupt your for loop to look for other events.

Also, instead of 2 buttons, you can use a single wxToggleButton for your 'on'/'off' events.

-Tony
I had noticed wxYield and wxSafeYield, but for some reason didn't think they were appropriate..re-reading the descriptions makes me think it may be what I'm looking for.

The two buttons were a quick and dirty test, and I didn't even LOOK for toggles :)

I'll test this, and see if it's what I'm looking for. Thanks Tony!

Michael

Mixael
Earned a small fee
Earned a small fee
Posts: 13
Joined: Thu Apr 27, 2006 6:24 pm

Post by Mixael » Tue Jan 09, 2007 1:17 am

Okay, I give :? Can I have an example of using the wxSafeYield/wxYield?

I'm a little frustrated, and can't seem to think straight on this :(

Michael

Sof_T
Can't get richer than this
Can't get richer than this
Posts: 864
Joined: Thu Jul 28, 2005 9:48 pm
Location: New Forest, United Kingdom
Contact:

Post by Sof_T » Tue Jan 09, 2007 9:37 am

Have you tried putting your code into the OnIdle event in your main form? This is a function that gets called repeatedly when the form idles. To try it drop a Edit control onto a new frame. Select the frames OnIdle event and create a new function. Add this code into the new function

Code: Select all


	// insert your code here
	static int Update = 0;
	wxString TempValue;
	TempValue << wxT("My Value is: ");
	TempValue << Update;
	WxEdit1->SetValue(TempValue);
	Update++;
As you can see the OnIdle event forms a loop that is updated pretty quickly. The beauty is that it doesnt freeze the GUI either. You can add a toggle button like Tony said and check if it is toggled on at the start of the function if so run the code, if not skip the code.

Sof.T
The home of Sof.T http://www.sof-t.site88.net/
Author of Programming with wxDevC++
http://sourceforge.net/projects/wxdevcpp-book/

Mixael
Earned a small fee
Earned a small fee
Posts: 13
Joined: Thu Apr 27, 2006 6:24 pm

Post by Mixael » Tue Jan 09, 2007 6:14 pm

Sof_T wrote:Have you tried putting your code into the OnIdle event in your main form? This is a function that gets called repeatedly when the form idles. To try it drop a Edit control onto a new frame. Select the frames OnIdle event and create a new function. Add this code into the new function

Code: Select all


	// insert your code here
	static int Update = 0;
	wxString TempValue;
	TempValue << wxT("My Value is: ");
	TempValue << Update;
	WxEdit1->SetValue(TempValue);
	Update++;
As you can see the OnIdle event forms a loop that is updated pretty quickly. The beauty is that it doesnt freeze the GUI either. You can add a toggle button like Tony said and check if it is toggled on at the start of the function if so run the code, if not skip the code.

Sof.T
No, I hadn't thought of OnIdle. I'll do some tests later (when I get off work) using this. Since I don't think I'll be using wxDev-C++ for the actual GAME project, I think I can use it for the utilities, etc that I need. I think that, given your description, OnIdle is what I'm looking for :)

I'll let you know if my test(s) work well enough for utility purposes. Thanks for the help.

Michael

Mixael
Earned a small fee
Earned a small fee
Posts: 13
Joined: Thu Apr 27, 2006 6:24 pm

Post by Mixael » Tue Jan 09, 2007 9:40 pm

BINGO!!! I had a few minutes at work that I could use to make a test, and it looks like OnIdle is my ticket! Thanks Sof_T for pushing me over the hurdle.

Michael

Sof_T
Can't get richer than this
Can't get richer than this
Posts: 864
Joined: Thu Jul 28, 2005 9:48 pm
Location: New Forest, United Kingdom
Contact:

Post by Sof_T » Tue Jan 09, 2007 10:25 pm

I'm glad it was of use to you.
The home of Sof.T http://www.sof-t.site88.net/
Author of Programming with wxDevC++
http://sourceforge.net/projects/wxdevcpp-book/

Post Reply