Page 1 of 1

wxDev-C++ and the game loop

Posted: Sun Jan 07, 2007 10:54 pm
by Mixael
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

Posted: Mon Jan 08, 2007 1:35 am
by buildere
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.

Posted: Mon Jan 08, 2007 1:55 am
by Mixael
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

Posted: Mon Jan 08, 2007 8:51 am
by lowjoel
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)

Posted: Mon Jan 08, 2007 6:26 pm
by Mixael
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

Posted: Mon Jan 08, 2007 10:46 pm
by Mixael
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

Posted: Mon Jan 08, 2007 11:50 pm
by tbreina
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

Posted: Tue Jan 09, 2007 12:24 am
by Mixael
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

Posted: Tue Jan 09, 2007 1:17 am
by Mixael
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

Posted: Tue Jan 09, 2007 9:37 am
by Sof_T
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

Posted: Tue Jan 09, 2007 6:14 pm
by Mixael
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

Posted: Tue Jan 09, 2007 9:40 pm
by Mixael
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

Posted: Tue Jan 09, 2007 10:25 pm
by Sof_T
I'm glad it was of use to you.