简体   繁体   中英

Infinite loop -> Need to show the main screen with Qt

here I am again ! So, I am developing an GBC emulator in C++ but I'm having some issues. First of all, I'm using Qt in VS10, which is working nice so far. But well, I have my GUI(main window) with a few Objects (QListWidget, Buttons etc). So, in my CPU class, I have a loop that emulates all the GBC instructions. It works in a simple way. Get code, decode, fetch and call the operation in a switch, do it all over again. So, my problem is, on each interation, I'd like to have this main screen showed up with the List updated.

Image below, I can't post cause I dont have 10 rep points. http://i.stack.imgur.com/BdaHo.png

So, a chunk of code so you can (try?) to understand whats going on: (cpu.cpp)

next:
op = FETCH;
setSelection((UINT32)op);
ciclos = cycles_table[op];

switch(op)
{
       do the magic
       emit onEndProcess((UINT32)op);
       goto next;
}

cpu.h

signals:
void onEndProcess(UINT32);

which is received by ratagbc (ratagbc.h)

public slots:
        void receivedEndProcess(UINT32);

And implemented:

void RataGBC::receivedEndProcess(UINT32 i){
    this->ui.listWidget->item(i+1)->setSelected(true);
    this->show();
 }

And in the rata constructor, after ui.setupUI(...) I have:

cpp = new cpu();
    connect(cpp,SIGNAL(onEndProcess(UINT32)),this,SLOT(receivedEndProcess(UINT32)));

}

Where cpp is an instance of cpu class. Here there is a problem, this connect is returning false !!

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
RataGBC w;
dasm dsm;
FILE *file = fopen("Tetris DX.gbc","r");
int c = 0;
while(dsm.DAsm(file,w.ui.listWidget,c));
fclose(file);
w.cpp->start();
w.show();
return a.exec();
}

One thing that I noticed is that I need to implement (a blank) onEndProcess in my cpu.cpp, or it gives me linking error. Is it really necessary ?

Hope you guys can understand what I need ! Thanks !

a.exec() starts an event loop, which includes updating the GUI. Put the relevant steps into a function and use Qt's event system to execute the code.

And don't use goto.

Use QTimer with 0 timeout to perform idle processing.
Make QObject (store your "emulated cpu" within it) with a slot, connect it to timer signal, and in that slot process one step of a program (RagaGBC) at a time. Ie perform one step of your emulated program. Or a few steps, just not all of them. Start timer to start the emulated program.
Once you're done, call QCoreApplication::exit(); .

If you don't understand how to make QObject/use Timers, read Qt tutorials.

You are trying to run two “infinite loops” in the same thread. One is your CPU loop, which obviously blocks. The other is the Qt event loop, which will block until you quit the application.

I might be wrong here, since I never implemented a CPU emulator before, but — although it is possible — I think you should not run the CPU loop in the same thread as the GUI, since the CPU is tight and perhaps sensitive to timing fluctuations.

Instead, you should run the CPU in a separate thread and communicate with the GUI thread using queued slots and other multithreading constructs such as QMutex , QAtomicInt and QAtomicPointer .

The following is how I would implement it (you should obviously adapt it):

GUI Thread

This is where you run QApplication::exec() .

  • Construct the main window (with the list and drawing surface)
  • Start the CPU thread
  • Whenever an input event occurs, signal the change to the CPU thread
  • Whenever the CPU thread updates the video buffer, update the drawing surface
  • When the user quits, stop the CPU thread

CPU Thread

Implement a new QThread to do the following.

  • Fetch, decode, execute
  • Read input whenever it is updated
  • Write the video memory and notify the GUI thread to update the drawing surface
  • Quit when told to.

Remember, only the GUI thread can manipulate UI widgets such as lists and menus. The CPU thread should use mechanisms such as queued slots to tell the GUI thread to update the widgets.

Another advice: look at how other people solved the same issue .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM