switching from Timer driven loop to mainloop Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Louigi Verona
Earned some good credits
Earned some good credits
Posts: 127
Joined: Tue Mar 24, 2009 10:21 am
Contact:

switching from Timer driven loop to mainloop

Post by Louigi Verona »

Hey guys!

I have been making a cellular automata application and, being a novice programmer, have used wxTimer to drive my app. However, the Timer has a threshold of 10 ms which of course is very slow and I was advised to use mainloop instead as it is very fast.

Imagine an app where the main loop runs from an OnTimer event. How can that be changed? Is there like a MainLoop function where I can just put the calls to my functions or am I misunderstanding something? If there is such a function, how do I access it?

ps: read the docs, but need more info
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria »

See this article, especially the part about idle events :
http://wiki.wxwidgets.org/Making_a_render_loop

(it does a render loop, but you can do whatever you want in the loop of course)
"Keyboard not detected. Press F1 to continue"
-- Windows
Louigi Verona
Earned some good credits
Earned some good credits
Posts: 127
Joined: Tue Mar 24, 2009 10:21 am
Contact:

Post by Louigi Verona »

Auria, thanks for the article - basically this is how I tried doing this on my own, but couldn't do it and here's what happens:

the OnInit() function is declared in my app automatically by wxDev-C++, but in there I can't address any of the components because at that point they are not created yet. I tried putting the OnInit function further down the code or whatnot but it then says that wxApp class is not declared. (although it is in the MyApp.h file)

How can this be solved? Can I override the OnInit() function somewhere in the code when components are created?
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria »

You could do something like :

Code: Select all


// pseudo-code

class MyApp : wxApp
{

  MyData* data = NULL;
  MyFrame* frame = NULL;

  void OnInit()
  {
    data = new MyData();
    frame = new MyFrame(data);
    Connect( IDLE_EVENT ... );
  }

  void OnIdle()
  {
    data->foo();
    frame->bar();
  }

};
"Keyboard not detected. Press F1 to continue"
-- Windows
Louigi Verona
Earned some good credits
Earned some good credits
Posts: 127
Joined: Tue Mar 24, 2009 10:21 am
Contact:

Post by Louigi Verona »

Nope, unfortunately it doesn't work. Says: "expected primary expression before '->' token".

Here is the code:

Code: Select all

#include "TablesApp.h"
#include "TablesFrm.h"

IMPLEMENT_APP(TablesFrmApp)

bool TablesFrmApp::OnInit()
{
    TablesFrm* frame = new TablesFrm(NULL);
    SetTopWindow(frame);
    frame->Show();

    Connect( wxID_ANY, wxEVT_IDLE, wxIdleEventHandler (TablesFrmApp::OnIdle) );

    return true;
}

void OnIdle(wxIdleEvent& event){

    TablesFrm->GetNum();

}

int TablesFrmApp::OnExit()
{
	return 0;
}
Louigi Verona
Earned some good credits
Earned some good credits
Posts: 127
Joined: Tue Mar 24, 2009 10:21 am
Contact:

Post by Louigi Verona »

Yeah, I also tried frame->MyFunc(); but it then saud that 'frame' is undeclared.
Wintermute
Knows some wx things
Knows some wx things
Posts: 38
Joined: Tue May 26, 2009 8:39 am
Location: Moscow, Russia
Contact:

Post by Wintermute »

Hey!

As I can see, TablesFrm is a class, not an object (instance of this class). Try the following:

Code: Select all

class TablesFrmApp : public wxApp
{
private:
    TablesFrm *frame;
public:
    bool OnInit();
    void OnIdle(wxIdleEvent& event);
    int OnExit();
};

IMPLEMENT_APP(TablesFrmApp)

bool TablesFrmApp::OnInit()
{
    frame = new TablesFrm(NULL);
    SetTopWindow(frame);
    frame->Show();

    Connect( wxID_ANY, wxEVT_IDLE, wxIdleEventHandler (TablesFrmApp::OnIdle) );

    return true;
}

void TablesFrmApp::OnIdle(wxIdleEvent& event)
{
    frame->GetNum();
}

int TablesFrmApp::OnExit()
{
    return 0;
}
Kind regards
FAQ
1. What the ... ?
2. Again what the ... ?
Louigi Verona
Earned some good credits
Earned some good credits
Posts: 127
Joined: Tue Mar 24, 2009 10:21 am
Contact:

Post by Louigi Verona »

Tried it. It gave out a long-long-long list of errors. I am using wxDev-C++, so a lot of things are declared automatically.
Wintermute
Knows some wx things
Knows some wx things
Posts: 38
Joined: Tue May 26, 2009 8:39 am
Location: Moscow, Russia
Contact:

Post by Wintermute »

Yeah, well! What are the first three error messages?
FAQ
1. What the ... ?
2. Again what the ... ?
Louigi Verona
Earned some good credits
Earned some good credits
Posts: 127
Joined: Tue Mar 24, 2009 10:21 am
Contact:

Post by Louigi Verona »

ISO C++ forbids declaration of TablesFrm with no type

expected ';' before '*' token

'frame' undeclared
upCASE
Moderator
Moderator
Posts: 3176
Joined: Mon Aug 30, 2004 6:55 am
Location: Germany, Cologne

Post by upCASE »

Hi!
Most likely you're missing the

Code: Select all

#include "TablesFrm.h"
line. Basically the error is that TablesFrm is unknown as a type (the definition is missing, the compiler doesn't know what TablesFrm "looks like").
OS: OpenSuSE, Ubuntu, Win XP Pro
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4

"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda
Louigi Verona
Earned some good credits
Earned some good credits
Posts: 127
Joined: Tue Mar 24, 2009 10:21 am
Contact:

Post by Louigi Verona »

I guess the confusion may come out of the fact that when you create a project in wxDev-C++, many things are done automatically.

Let me show the main files I have and maybe you can help me sort it out.

TablesApp.cpp

Code: Select all

#include "TablesApp.h"
#include "TablesFrm.h"

IMPLEMENT_APP(TablesFrmApp)

bool TablesFrmApp::OnInit()
{
    frame = new TablesFrm(NULL);
    SetTopWindow(frame);
    frame->Show();

    Connect( wxID_ANY, wxEVT_IDLE, wxIdleEventHandler (TablesFrmApp::OnIdle) );

    return true;
}

void OnIdle(wxIdleEvent& event){

    frame->GetNum();

}

int TablesFrmApp::OnExit()
{
	return 0;
}
TablesApp.h

Code: Select all

#ifndef __TABLESFRMApp_h__
#define __TABLESFRMApp_h__

#ifdef __BORLANDC__
	#pragma hdrstop
#endif

#ifndef WX_PRECOMP
	#include <wx/wx.h>
#else
	#include <wx/wxprec.h>
#endif

class TablesFrmApp : public wxApp
{
    private:
        TablesFrm *frame;
	public:
        bool OnInit();
		void OnIdle(wxIdleEvent& event);
		int OnExit();
};

#endif

If I do it the old way and take out my code from the OnIdle prototype, just leave it empty, the compiler exits with an error that OnIdle(wxIdleEvent& event) has no reference or smth like that. To make everything work I did smth - I think I've commented out Connect() function call.
Louigi Verona
Earned some good credits
Earned some good credits
Posts: 127
Joined: Tue Mar 24, 2009 10:21 am
Contact:

Post by Louigi Verona »

Guys, bumping this up! Have been tackling this for days and still cannot understand how to make it work (((
upCASE
Moderator
Moderator
Posts: 3176
Joined: Mon Aug 30, 2004 6:55 am
Location: Germany, Cologne

Post by upCASE »

Louigi Verona wrote:Have been tackling this for days and still cannot understand how to make it work (((
Ok, so please post the errors you get and show us some code. It's very hard to guess what might cause the error.
OS: OpenSuSE, Ubuntu, Win XP Pro
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4

"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda
Louigi Verona
Earned some good credits
Earned some good credits
Posts: 127
Joined: Tue Mar 24, 2009 10:21 am
Contact:

Post by Louigi Verona »

Didn't I just post the code? What other code you need? And I listen the errors above too. Just tell me what more information do you need and I will deliver it.
Post Reply