Jump to content

  • Log In with Google      Sign In   
  • Create Account

Journal of Lee Stripp

vortxGE : Threads

Posted by , 11 March 2013 - - - - - - · 878 views
Threads, C++11, vortxGE
After reading lots of tech papers/blogs on C++11 threads I redesigned the way the engine handles threading.

Developers can now change threads manually per Scene, each pipeline : Move, Optimise are now batch threaded for better performance. after lots of testing this approch seems to work better under heavy load and being scalable its my new method of choice.

Here is some code from the engine that creates threads based on nodes.

void lsNode::MoveThread( lsScene* scn, lsOpenGL* opengl, double delta )
{
#ifdef VORTX_DEBUG_NODES
    cout << "lsNode::MoveThred() ********************************" << endl;
#endif
    
    vector<thread> th;
    lsNode *ptr;
    int threads = 1;
    int tc = 0;
    
    // Threads
    if( scn )
    {
        threads = scn->getThreads();
    } else
    {
        threads = thread::hardware_concurrency() * 2;
    }
    
    // cycle through child and its next nodes
    ptr = getNode_list();
    while( ptr )
    {
        // Add thread
        th.push_back( thread( &lsNode::Move, ptr, scn, opengl, delta ) );
        tc++;
        
        // check our thread count
        if(( tc >= threads )||( ptr->getNext_node() == NULL ))
        {
#ifdef VORTX_DEBUG_NODETHREADS
            // debug
            cout << "lsNode::MoveThread - Thread count : " << tc << endl;
#endif
            // Join and wait
            for( auto &t : th )
            {
                t.join();
            }
            // clean up
            th.clear();
            tc = 0;
        }
        ptr = ptr->getNext_node();
    }
    
    // pass it on
    if( getNode_list() ) getNode_list()->MoveThread( scn, opengl, delta );
    if( getNext_node() ) getNext_node()->MoveThread( scn, opengl, delta );
}




C++11 Threads

Posted by , 07 March 2013 - - - - - - · 1,210 views

I was just brushing up on some C++11 concepts the last few nights (looking for parts of the engine to improve) and noticed quite a few people struggling with threads inside classes, as in using a class object as a thread. Some of the responses were so confusing to say the least.

Also they were struggling with std::cout while threads were running. So here's some example code of these problems all in one. I hope it points someone in the right direction.

Files:
lsThread2.h / .cpp
lsThread.h / .cpp
core.h / .cpp
main.cpp
// file : core.h

#ifndef cppThreads_core_h
#define cppThreads_core_h

#include <iostream>
#include <vector>
#include <thread>
#include <mutex>

using namespace std;

/*! Base class
 Used to start some lsThread objects.
 */
class core
{
public:
    core();
    virtual ~core();
    
    void run();
    void logText( const string& val, int tid  );
    
private:
    int nr_threads;
    mutex m;
};


#endif

// file : core.cpp
​
#include "core.h"
#include "lsThread.h"

core::core()
{
    nr_threads = 10;
}

core::~core()
{
}

void core::run()
{
    cout << "########## Start test ##########" << endl;
    
    vector<thread> th;
    lsThread *mt = new lsThread[nr_threads];
    
    //Launch a group of threads
    for( int i = 0; i < nr_threads; ++i )
    {
        mt[i].setTid( i );
        // Shows how to pass args to the start function in lsThread
        th.push_back( thread( &lsThread::start, mt[i], this, 5 ) );
    }
    
    //Join the threads with the main thread which core is running
    for( auto &t : th )
    {
        t.join();
    }
    
    // delete our array of lsThread objects
    delete [] mt;
    cout << "########## End test ##########" << endl;
}

/*! Logging
 This function is here so its in a common thread for all other threads to call.
 Remember in a real world App you would make a better logger.
 */
void core::logText( const string& val, int tid )
{
    // Lock for output
    m.lock();
    cout << "Thread : " << tid << " - " << val << endl;
    m.unlock();
}




Above: a base class that runs in the main thread.
// file : lsThread.h
​
#ifndef cppThreads_lsThread_h
#define cppThreads_lsThread_h

#include <iostream>
#include <thread>
#include <vector>

class core;

using namespace std;

/*! Thread class
 Nothing special about this class, you could thread any class.
 */
class lsThread
{
public:
    lsThread();
    virtual ~lsThread();
    
    virtual void start( core *c, int val );
    
    void setTid( int val )
    {
        tid = val;
    }
    
private:
    int tid;
    
};


#endif

// File : lsThread.cpp

#include "core.h"
#include "lsThread.h"
#include "lsThread2.h"

lsThread::lsThread()
{
    tid = 0;
}

lsThread::~lsThread()
{
}

void lsThread::start( core *c, int val )
{
    c->logText( "lsThread start", tid );
    
    // Here we create some more threads with a new class, based on the passed value.
    vector<thread> th;
    int nr_threads = val;
    lsThread2 *mt = new lsThread2[nr_threads];
    
    //Launch a group of threads
    for( int i = 0; i < nr_threads; ++i )
    {
        mt[i].setTid( i );
        th.push_back( thread( &lsThread2::start, mt[i], c ) );
    }
    
    // Join the threads with the current thread
    // This could be any of the threads created by the core class.
    for( auto &t : th )
    {
        t.join();
    }
    
    // delete our array of lsThread2 objects.
    delete [] mt;
}




Above: This class show how to launch more threads inside the current thread.
// File : lsThread2.h

#ifndef cppThreads_lsThread2_h
#define cppThreads_lsThread2_h

#include <iostream>
#include <thread>
#include <vector>

class core;

using namespace std;

class lsThread2
{
public:
    lsThread2();
    virtual ~lsThread2();
    
    virtual void start( core *c );
    
    void setTid( int val )
    {
        tid = val;
    }
    
private:
    int tid;
};


#endif

// File : lsThread2.cpp

#include "core.h"
#include "lsThread2.h"


lsThread2::lsThread2()
{
    tid = 0;
}

lsThread2::~lsThread2()
{
}

void lsThread2::start( core *c )
{
    // In this thread we just call the core class logging function
    c->logText( "lsThread2 start", tid );
}




Above: Simple thread that will just output a message to stdout.
// File : main.cpp

#include "core.h"


int main(int argc, const char * argv[])
{
    // Thread demo
    core *c = new core();
    c->run();
    delete c;
    
    return 0;
}


Above: And last but not least the main()

I've kept this example as simple as possible but still trying to show as much as possible. Hope it helps someone.

Cheers
Lee


My Business

Posted by , 06 March 2013 - - - - - - · 855 views

Well I've been away from GameDev.net for what seems like forever. It has been a hard road but I'm up and running with the new business. First App is on the AppStore and I created a Online Shop for my site too.

The plan is to start making some money to fund future Games using my engine, once the engine is done I'll release it free. With any luck this means jobs for others and I get back to coding full time. All this hard work is taking me away from what I love! CODE

www.leestripp.com

Cheers
Lee


Still Alive

Posted by , 22 December 2012 - - - - - - · 731 views
Ill be back
I have been very busy setting up the new Business, sadly this meant I couldn't work on vortxGE. I have created 3 other Apps for iPhone/iPad and Mac OS X. This has been taking up all my time. I'm also in the middle of creating a new website for the Business : http://www.leestripp.com still under construction.

I'm just adding this entry to say I will be back soon with some vortxGE updates.

Cheers
Lee


vortxEDIT : video

Posted by , 30 October 2012 - - - - - - · 783 views
video, vortxGE
My first attempt at a video with audio. wow shocking :-)




vortxGE : Update

Posted by , 23 October 2012 - - - - - - · 4,873 views
Update
Started work on the Game level system, for this I needed to add hierarchy support to the Controls and also get that working in vortxEDIT. A recap on Controls in vortxGE, a control is a building block inside the engine for anything from a Viewer, Composite, Game Levels and so on. I wanted to be able to stack Levels on top of a LevelLoader control for example. This is all done and tested, as in the linking etc...

vortxEDIT
Now has support for loading Mesh assets and also simple Object adding. In the image you can see I was playing around with adding existing cubes but this could be any object I export from Blender. I also did even more code cleanups to do with Memory management. No ARC used anymore.

Attached Image


vortxGE : Level Loading

Posted by , 22 October 2012 - - - - - - · 696 views
Game Levels
Here is a little insight into how I work, I always plan out major systems on paper before I ever start coding them, I don't go overboard with tones of detail because this is just off the top of my head. Things may change as I write the code and I make changes to this document as I go. If I don't do this stage I find it takes 10x longer to write the code. This also gives a great reference for later Product documentation.

Cheers
Lee


Level Progression Games
Attached Image
How level loading works
In vortxEDIT, create a LevelLoader control and as many Level controls you need for your game. The loader should hold an lsViewer that displays lsLevel loading progress or whatever you like, as long as the user knows something is loading. A lsLevel control holds a filename of the vortxEDIT Level project to be loaded and a pointer to the lsGame instance needed for the level data. GamePlay controls are created by the game developer and trigger game end, restart and set scores in the lsLevel.

An lsGame control is used to hold the current lsLevel loaded data and gameplay pointer. You only need one lsGame control, the lsLevelLoader should create this control and pass it to lslevel classes.

Create your Level files using vortxEDIT as single projects containing everything you need for that levels game play, other in-game objects can always be added at runtime.

Sandbox Games
Attached Image
How Sandbox works
lsSandBox class manages many lsSandBoxZone classes. Because a sandbox is really one massive level it should control lsSandBoxZone loading and releasing data into the lsGame class. An lsSandBoxZone controls all its asset loading and releasing in turn. Using zones should give greater flexibility to the developer and his/her target platform. Smaller zones can leave a smaller memory footprint. LsSandBoxZone's will be triggered by distance from the player/camera and must dynamically add/remove data from the lsGame class depending on the distance.

This structure looks like the lsLevelLoader structure but it in no way works the same way. Keep this in mind when you create your lsGamePlay subclass.


vortxGE : single code base

Posted by , 18 October 2012 - - - - - - · 951 views

After playing with the IOS engine code base for a while now I was confident I could recombine the 2 engine code bases together.... 4 hours later its all done and working very well, even sorted a small bug when a mesh file was missing and the engine still tried to access its bounding sphere for culling

vortxEDIT and iPhone/iPad projects are all under the same workspace now, very nice.

Attached Image


vortxEDIT : Save

Posted by , 17 October 2012 - - - - - - · 633 views

Been working hard at getting a save project feature working in vortxEDIT. This output will also be used by the Engine to load games
Small changes to the asset manager were done.

Here is the project file output so far. Just getting the basic elements down.
# vortxEDIT v1.0 by Lee A. Stripp
# Shaders
add_shader light_tex_dn
add_shader quad
add_shader debug
# Assets
add_asset texture data/textures/tech_2_2_d.tga
add_asset texture data/textures/tech_2_2_n.tga
add_asset mesh data/meshes/Cube.vbm
# Controls
add_composite Composite
add_scene Scene
add_object Quad Scene
add_camera Camera Scene
add_pass DR Scene
add_viewer Viewer
add_scene Scene
add_object Scene#Grid Scene
add_camera Camera Scene
add_light point Light Scene
add_object Point#Light#Empty Light
add_object Test#Cube Scene
add_pass FBO Scene
# EOF

I have also been working on a saveGameState() function that will save the current state the game is in, a save game if you will.


vortxEDIT : Control View

Posted by , 12 October 2012 - - - - - - · 582 views
vortxGE
I have now changed the Scene TreeView into a TreeView for the engine, showing all controls and sub-systems. This took a bit of work :-)

Started adding more Properties, here's an exsample of the Test cube with some changed material values.

Attached Image






December 2016 »

S M T W T F S
    123
45678 9 10
11121314151617
18192021222324
25262728293031

Recent Comments