![]() |
CRM64Pro GDK
v0.94
A free cross-platform game development kit built on top of SDL 2.0
|
Introduction
Nowadays, in the multicore CPU era, using multiple threads is, on some cases, a really needed scenario. The "easy" way is to run the different computational categories on different threads that will run on the CPU cores, I said "easy" because multithreading development is all but easy task and good skills and planification are needed.
In the following sections, we will focus on the Graphics/Logic categories, starting from simpliest to more advanced methods.
This is the simpliest method which can be used on small demos or applications, while you are learning the GDK or if you are implementing a custom graphics/logic/timing method. Code example: // Initialization stuff Main &mC64 = Main::Instance(); ... // Main loop SDL_Event myEvent; Uint8 bRunning = 1; while(bRunning) { // My logic stuff ... // My graphics stuff ... // GDK main governor while(mC64.update(&myEvent) { switch(myEvent.type) { case SDL_QUIT: bRunning = 0; break; } } } The "Main loop" will run at the maximum speed provided by the CPU (assuming ConfigMgr::iMTFriendly = 0), the "logic" will be executed, then the "graphics stuff" and then the "main governor loop" will do its tasks (Main::update()). Although you can use ConfigMgr::iMTFriendly with a minimum of milliseconds to wait while Main::update() is executed, this is useful for giving back the execution control to the operating system and not eat the whole CPU probably doing a lot of unnecesary work.
This method can be used on menu screens or when you dont need to have a perfect "smoothness". Code example: // Initialization stuff Main &mC64 = Main::Instance(); mC64.ITimer().init(); mC64.ITimer().setRate(0,20); // Set the Logic Frame Rate to 20 ... // Main loop SDL_Event myEvent; Uint8 bRunning = 1; while(bRunning) { // My logic stuff ... // GDK main governor while(mC64.update(&myEvent) { switch(myEvent.type) { case SDL_QUIT: bRunning = 0; break; case EVENT_C64: if(myEvent.user.code == EVENT_C64_RENDER) { // My graphics stuff ... } break; } } } The "Main loop" and "logic stuff" are running at the Logic Frame Rate of 20 executions per second (they are equally distributed across the timeline).
Along the GDK, this method is know as the fixed virtual logic frame rate with interpolation and can be used on the game itself as provides the most advanced "smoothness". Code example: // Initialization stuff Main &mC64 = Main::Instance(); mC64.ITimer().init(); mC64.ITimer().setRate(0,20); // Set the Logic Frame Rate to 20 ... // Set a render callback for our main screen ScreenObj *mScreen = mC64.IConfigMgr().get(); mScreen->setRenderCallback(myRenderFunc); // Main loop SDL_Event myEvent; Uint8 bRunning = 1; while(bRunning) { // My logic stuff ... // GDK main governor while(mC64.update(&myEvent) { switch(myEvent.type) { case SDL_QUIT: bRunning = 0; break; } } } Sint32 myRenderFunc(Sint32 iMode) { // My graphics stuff ... } The "Main loop" and "logic stuff" are running at the Logic Frame Rate of 20 executions per second (they are equally distributed across the timeline). |