chronologicaldot's junk

Discuss about anything related to the Irrlicht Engine, or read announcements about any significant features or usage changes.
Post Reply
chronologicaldot
Competition winner
Posts: 684
Joined: Mon Sep 10, 2012 8:51 am

chronologicaldot's junk

Post by chronologicaldot »

Hi guys,

I probably should have joined this forum a long time ago. It may have saved me alot of big headaches, but I've joined now.
Anyways, I do various expansion-feature projects for irrlicht from time to time. Simple things usually, the code for which you'll find on my website:
http://chronologicaldot.web44.net/proje ... nsion.html

My recent, unpublished work is a XML GUI-interface loader that utilizes IOAttributes. Instead of loading everything in an interface crammed into one xml file, I divide the job up into loading complementary files that are formatted like HTML and CSS (and it makes the job of making layouts WAY easier). The CSS files holds the key attributes that passed to deserializeAttributes() (inherited from IGUIElement), and by this method I can easily my code to include every GUI element in the irrlicht engine (and all custom GUI elements that inherit from IGUIElement and use deserializeAttributes() to establish their parameters).

*sigh* Whew! Lot to say for an introduction, but I'm quite proud of some of this work, so I felt like sharing.
CuteAlien
Admin
Posts: 9634
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: chronologicaldot's junk

Post by CuteAlien »

Thanks for sharing. You might consider adding some comments as to what to use each of those code-snippets for. It's hard to understand what exactly it can be used for without an example.

Also wondering - why freebsd and GPL 2 license when working with Irrlicht? The zlib license would be near identical to freebsd and it would make it easier for Irrlicht users to use.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
chronologicaldot
Competition winner
Posts: 684
Joined: Mon Sep 10, 2012 8:51 am

Re: chronologicaldot's junk

Post by chronologicaldot »

First - the license spheel: I couldn't decide what license to use, so I default to GPL 2 and freebsd. You have my permission to use it under zlib if you want. I'll change it myself eventually.

Second - as for the code snippets: I'll try to throw together some examples and post it next to the download links. Currently, the simplest examples I have are the debug files, and those wouldn't be very helpful unless you knew what I was testing. XD Thanks for bringing that up.
CuteAlien
Admin
Posts: 9634
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: chronologicaldot's junk

Post by CuteAlien »

Ah - I didn't mean writing examples - that's certainly good as well - I meant just describe an example what the snippet is for.
So far I'm just not sure what irrXMLStorage does for example by it's description. Is it creating a DOM tree using Irrlicht's xml? Or what kind of trees are you talking about?
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
chronologicaldot
Competition winner
Posts: 684
Joined: Mon Sep 10, 2012 8:51 am

Re: chronologicaldot's junk

Post by chronologicaldot »

Yes, irrXMLStorage creates a DOM tree from a given file.
I'm still not quite sure what you mean by "describe an example what the snippet is for".

irrTree is for making the DOM tree. irrXMLTree is a specific implementation of irrTree for holding XML data (e.g. node type, node name, node text, attributes, and a list of child nodes). irrXMLStorage is simply for acquiring the XML data from file and putting it in a DOM tree composed of irrXMLTreeNodes. Once irrXMLStorage loaded the information, you can use a pointer to point to the root node (irrXMLTreeNode) and use the data. Then you can delete the instance of irrXMLStorage since it's no longer needed.

Being that the tree structure is a DOM tree, I made it so you can access it similar to a multidimensional array.
For example:

Suppose we have a tree created by:

<root>
<n1>
<n2>
</n2>
</n1>
</root>

To access the name of the innermost node when the root is stored in "rootnode" (an instance of irrXMLTreeNode):

irrXMLTreeElement * element;

element = (irrXMLTreeElement*) rootnode[0][0];

element.getNodeName();
CuteAlien
Admin
Posts: 9634
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: chronologicaldot's junk

Post by CuteAlien »

Funny - I wrote something very similar just 2-3 weeks ago, also a DOM-tree around Irrlicht XML. Not posted yet thought, have to ask first if I can release those sources. Anyway, very nice, that's rather easier to use often than SAX-style XML.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
chronologicaldot
Competition winner
Posts: 684
Joined: Mon Sep 10, 2012 8:51 am

Incrementor

Post by chronologicaldot »

The following is a class I call "incrementor" ("Inc"). It's intended purpose is to keep track of an integer value, increment it, and possibly loop it over a range. It's basically a number with alot of neat and convenient features. Some of these features are as follows:

-> Any type of number
-> Adjustable range
-> Adjustable step size / increment size
-> 3 cycling types:
Default - Number just goes to infinity.
Boolean - Number cycles between 0 and 1.
Repeat - Number cycles in a set range (defaulted to 0 to 1).

I wrote another convenient class called "Range" which can be used by this one (so you'll see traces of it included but they are unnecessary for compiling the code), and I may upload that.

While tempting, it's probably not advisable to use this in place of an integer in a for-loop unless it makes your code easier to read.

Code: Select all

 
/*
(c) 2012-2013 Nicolaus Anderson
Generated from Incrementer.h on: Jan 1, 2012
License: zlib
 
Incrementor - A simple incrementing class for keeping track of changes in 
 
a value
with fewer keystrokes.
*/
 
#ifndef __INCREMENTOR__
#define __INCREMENTOR__
 
 
enum ECycleType
{
    /* This incrementor is to act like a boolean value, switching back 
 
and forth
    between 0 and 1. */
    CYC_BOOLEAN = 0,
    
    /* This incrementor must be set to the min value after reaching 
 
the max value
    or must be set to the max value if decremented from the min value. 
 
*/
    CYC_REPEAT,
    
    /* This incrementor counts up and down like an ordinary integer. 
 
*/
    CYC_DEFAULT
};
 
template<class Num>
class Inc
{
    Num value;
    Num step;
    Num min;
    Num max;
 
    ECycleType cycle;
 
public:
 
    //! Constructor
    Inc(
        ECycleType type = CYC_DEFAULT,
        Num start_val=0, Num start_step=1, Num start_min=0, Num 
 
start_max=1
        )
        : value( start_val ), step( start_step ), min(start_min), 
 
max( start_max ), cycle( type )
    {}
    
    
    //! Deconstructor
    ~Inc<Num>() {}
    
    
    //! Get type of cycle
    ECycleType getCycleType()
    {
        return cycle;
    }
    
    //! Set the cycle type directly
    void setCycleType( ECycleType type )
    {
        cycle = type;
    }
    
    //! Set the cycle type to the default
    void ctDef()
    {
        cycle = CYC_DEFAULT;
    }
    
    //! Set the cycle type to boolean
    void ctBool()
    {
        cycle = CYC_BOOLEAN;
    }
    
    //! Set the cycle type to repeat
    void ctRpt()
    {
        cycle = CYC_REPEAT;
    }
 
    //! Checks if out of bounds - fixes if necessary
    bool outOfBounds()
    {
        switch ( cycle )
        {
        case CYC_BOOLEAN:
            if ( value > 1 )
            {
                value = 0;
                return true;
            } else if ( value < 0 )
            {
                value = 1;
                return true;
            }
            break;
 
        case CYC_REPEAT:
            if ( value > max )
            {
                // Wrap around if greater than the max
                value = (value - max) + min;
                return true;
            } else if ( value < min )
            {
                // Wrap around if greater than the max
                value = (value - max) + min;
                return true;
            }
 
        default:
            if ( value < min || value > max )
                return true;
            break;
        }
 
        return false;
    }
 
 
//******* Getters and setters **********
 
    Num& Val()
    {
        return value;
    }
 
    Num& getStep()
    {
        return step;
    }
    
    Num& getMin()
    {
        return min;
    }
    
    Num& getMax()
    {
        return max;
    }
 
    void setVal( Num new_val )
    {
        value = new_val;
    }
    
    void setStep( Num new_step )
    {
        step = new_step;
    }
    
    void setMin( Num new_min )
    {
        min = new_min;
        clampVal();
    }
    
    void setMax( Num new_max )
    {
        max = new_max;
        clampVal();
    }
 
    void setMinShiftMax( Num new_min )
    {
        max += new_min - min;
        min = new_min;
        clampVal();
    }
 
    void setMaxShiftMin( Num new_max )
    {
        min += new_max - max;
        max = new_max;
        clampVal();
    }
 
    void setRange( Num new_min, Num new_max )
    {
        min = new_min;
        max = new_max;
        clampVal();
    }
 
    // Range modifiers
#ifdef __RANGE_CLASS__
    Range<Num> getRange()
    {
        return Range<Num>( min, max );
    }
 
    void setRange( Range<Num>& range )
    {
        min = range.start;
        max = range.end;
        clampVal();
    }
 
    Range<Num>& operator= ( Range<Num>& range )
    {
        min = range.start;
        max = range.end;
        clampVal();
        return *this;
    }
#endif
 
    void restart()
    {
        value = min;
    }
 
    void clampVal()
    {
        if ( value < min )
            value = min;
 
        else if ( max < value )
            value = max;
    }
 
 
// ********* Shortcut operators ***********
    
    bool operator++ ()
    {
        switch( cycle )
        {
        case CYC_BOOLEAN:
            value++;
            break;
        
        case CYC_REPEAT:
            if ( value == max )
            {
                value = min;
                return true;
            } else {
                value += step;
            }
            break;
        
        default:
            value += step;
        }
 
        return outOfBounds();
    }
    
    bool operator-- ()
    {
        switch( cycle )
        {
        case CYC_BOOLEAN:
            value--;
            break;
            
        case CYC_REPEAT:
            if ( value == min )
            {
                value = max;
                return true;
            } else {
                value -= step;
            }
            break;
            
        default:
            value -= step;
        }
 
        return outOfBounds();
    }
    
    bool operator==( Inc<Num>& other )
    {
        return value == other.value;
    }
    
    bool operator>( Inc<Num>& other )
    {
        return value > other.value;
    }
    
    bool operator>=( Inc<Num>& other )
    {
        return value >= other.value;
    }
    
    bool operator<( Inc<Num>& other )
    {
        return value < other.value;
    }
    
    bool operator<=( Inc<Num>& other )
    {
        return value <= other.value;
    }
    
    Inc<Num>& operator=( Inc<Num>& other )
    {       
        return copy(other);
    }
    
    Inc<Num>& copy( Inc<Num>& other )
    {
        value = other.value;
        step = other.step;
        min = other.min;
        max = other.max;
        cycle = other.cycle;
        
        return *this;
    }
 
    /* REMOVED
    Reason: Working with operator=( Num val ) it overrides
    the role of operator=( Inc<Num>& other )
 
    template<class T>
    operator T ()
    {
        return (T)value;
    }
    */
 
//****** assignment operators *******
 
    Inc<Num>& operator+=( Inc<Num>& other )
    {
        value = other.value;
 
        return *this;
    }
 
    Inc<Num>& operator-=( Inc<Num>& other )
    {
        value -= other.value;
 
        return *this;
    }
 
    Inc<Num>& operator=( Num val )
    {
        value = val;
 
        return *this;
    }
 
    Inc<Num>& operator+=( Num val )
    {
        value += val;
 
        return *this;
    }
    Inc<Num>& operator-=( Num val )
    {
        value -= val;
 
        return *this;
    }
 
    /*
    Inc<Num>& operator=( stdu32 val )
    {
        value = (Num)val;
 
        return *this;
    }
 
    Inc<Num>& operator+=( stdu32 val )
    {
        value += (Num)val;
 
        return *this;
    }
 
    Inc<Num>& operator-=( stdu32 val )
    {
        value -= (Num)val;
 
        return *this;
    }
    */
};
 
#endif // define __INCREMENTOR__
chronologicaldot
Competition winner
Posts: 684
Joined: Mon Sep 10, 2012 8:51 am

Range class

Post by chronologicaldot »

My range class, which is just a class for holding two integers and having a bunch of features. Warning: It isn't well tested, so I'll probably find something wrong with it in the future, even though it seems too simple to fail. (It hasn't failed in what I've used it in, fortunately.)

Code: Select all

/*
Range class
(c) Nicolaus Anderson
Created Jan 10, 2013
 
License: zlib
*/
 
#ifndef __RANGE_CLASS__
#define __RANGE_CLASS__
 
template<class T>
class Range
{
public:
    T start;
    T end;
 
    Range<T>()
    {
        start = 0;
        end = 0;
    }
 
    Range<T>( T new_start, T new_end )
    {
        start = new_start;
        end = new_end;
    }
 
    inline T Length()
    {
        return end - start;
    }
 
    operator T ()
    {
        return Length();
    }
 
    //------- division
 
    T operator/ ( Range<T>& other )
    {
        if ( other.Length() != 0 )
            return ( end - start ) / ( other.end - other.start );
        else
            return T(0);
    }
 
    T operator/ ( T& value )
    {
        return Length()/value;
    }
 
    Range<T>& operator/= ( Range<T>& other )
    {
        end = start + Length()/other.Length();
        return *this;
    }
 
    Range<T>& operator/= ( T& value )
    {
        end = start + Length()/value;
        return *this;
    }
 
    //------- multiplication
 
    T operator* ( Range<T>& other )
    {
        return Length()*other.Length();
    }
 
    T operator* ( T& value )
    {
        return Length()*value;
    }
 
    Range<T>& operator*= ( Range<T>& other )
    {
        end = start + Length()*other.Length();
        return *this;
    }
 
    Range<T>& operator*= ( T& value )
    {
        end = start + Length()*value;
        return *this;
    }
 
    //------- addition
 
        //! Add ranges
    /* Returns the union of two ranges */
    Range<T> operator+ ( Range<T>& other )
    {
        return Range<T>(
            (start < other.start)? start : other.start,
            (end > other.end)? end : other.end
            );
    }
 
        //! Add value
    /* Returns the length of the range plus the value */
    T operator+ ( T& value )
    {
        return Length() + value;
    }
 
    //------- other
 
        //! Extend range
    /* Extends the range by the given amount while
    returning the range for inline usage. */
    Range<T>& extend( T& value )
    {
        end += value;
        return *this;
    }
 
        //! In range
    /* Indicates if a given value is in the range.
    \param value - The number in question.
    \param bound_inclusive - Whether the bounds should be
    included in the range. */
    bool inRange( T& value, bool bound_inclusive=true )
    {
        if ( (bound_inclusive? min<=value : min<value) )
            if ( (bound_inclusive? value<=max : value<max) )
                return true;
 
        return false;
    }
 
 
    // ***************** With other types *******************
 
    template<class T2>
    inline T2 Length()
    {
        return (T2)(end - start);
    }
 
    template<class T2>
    operator T2 ()
    {
        return (T2)Length();
    }
 
    //-------- division
 
    template<class T2>
    T operator/ ( Range<T2>& other )
    {
        if ( other.Length() != 0 )
            return ( end - start ) / (T)( other.end - other.start );
        else
            return T(0);
    }
 
    template<class T2>
    T operator/ ( T2& value )
    {
        return Length()/(T)value;
    }
 
    template<class T2>
    Range<T>& operator/= ( Range<T2>& other )
    {
        end = start + Length()/(T)other.Length();
        return *this;
    }
 
    template<class T2>
    Range<T>& operator/= ( T2& value )
    {
        end = start + Length()/(T)value;
        return *this;
    }
 
    //------- multiplication
 
    template<class T2>
    T operator* ( Range<T2>& other )
    {
        return Length()*(T)other.Length();
    }
 
    template<class T2>
    T operator* ( T2& value )
    {
        return Length()*(T)value;
    }
 
    template<class T2>
    Range<T>& operator*= ( Range<T2>& other )
    {
        end = start + Length()*other.Length();
        return *this;
    }
 
    template<class T2>
    Range<T>& operator*= ( T2& value )
    {
        end = start + Length()*(T)value;
        return *this;
    }
 
    //------- addition
 
        //! Add ranges
    /* Returns the union of two ranges */
    template<class T2>
    Range<T> operator+ ( Range<T2>& other )
    {
        return Range<T>(
            (start < other.start)? start : (T)other.start,
            (end > other.end)? end : (T)other.end
            );
    }
 
        //! Add value
    /* Returns the length of the range plus the value */
    template<class T2>
    T operator+ ( T2& value )
    {
        return Length() + (T)value;
    }
 
    //-------
 
        //! Extend range
    /* Extends the range by the given amount while
    returning the range for inline usage. */
    template<class T2>
    Range<T>& extend( T2& value )
    {
        end += (T)value;
        return *this;
    }
 
        //! In range
    /* Indicates if a given value is in the range.
    \param value - The number in question.
    \param bound_inclusive - Whether the bounds should be
    included in the range. */
    template<class T2>
    bool inRange( T2& value, bool bound_inclusive=true )
    {
        if ( (bound_inclusive? min<=value : min<value) )
            if ( (bound_inclusive? value<=max : value<max) )
                return true;
 
        return false;
    }
 
};
 
#endif // define __RANGE_CLASS__
chronologicaldot
Competition winner
Posts: 684
Joined: Mon Sep 10, 2012 8:51 am

ComplexNumber

Post by chronologicaldot »

entity might be interested in this:
It's a simple complex number class, useful for making fractals (among other things).

Code: Select all

 
// (c) Nicolaus Anderson
// license: zlib
 
template<class T>
struct ComplexNumber
{
    T real;
    T imag; // imaginary component
 
 
    ComplexNumber()
        : real( (T)0 )
        , imag( (T)0 )
    {}
 
    ComplexNumber( const ComplexNumber& other )
        : real( other.real )
        , imag( other.imag )
    {}
 
    ComplexNumber( T realT, T imagT )
        : real( realT )
        , imag( imagT )
    {}
 
    void set( T realT, T imagT )
    {
        real = realT;
        imag = imagT;
    }
 
    ComplexNumber& operator = ( ComplexNumber& other )
    {
        real = other.real;
        imag = other.imag;
        return *this;
    }
 
    ComplexNumber& operator = ( T& value )
    {
        real = value;
        return *this;
    }
 
    bool operator == ( ComplexNumber& other )
    {
        return ( real == other.real && imag == other.imag );
    }
 
    bool operator == ( T& value )
    {
        return ( real == value && imag == (T)0 );
    }
 
    // Basic operators for other == ComplexNumber
 
    ComplexNumber& operator += ( ComplexNumber& other )
    {
        real += other.real;
        imag += other.imag;
        return *this;
    }
 
    ComplexNumber operator + ( ComplexNumber& other )
    {
        ComplexNumber temp(*this);
        return (temp += other);
    }
 
    ComplexNumber& operator -= ( ComplexNumber& other )
    {
        real -= other.real;
        imag -= other.imag;
        return *this;
    }
 
    ComplexNumber operator - ( ComplexNumber& other )
    {
        ComplexNumber temp(*this);
        return (temp -= other);
    }
 
    ComplexNumber& operator *= ( ComplexNumber& other )
    {
        T tempReal = real;
        real = tempReal * other.real - imag * other.imag;
        imag = tempReal * other.imag + imag * other.real;
        return *this;
    }
 
    ComplexNumber operator * ( ComplexNumber& other )
    {
        ComplexNumber temp(*this);
        return (temp *= other);
    }
 
    ComplexNumber& operator /= ( ComplexNumber& other )
    {
        real /= other.real;
        imag /= other.imag;
        return *this;
    }
 
    ComplexNumber operator / ( ComplexNumber& other )
    {
        ComplexNumber temp(*this);
        return (temp /= other);
    }
 
    // Basic operators for other == T
 
    ComplexNumber& operator += ( T& other )
    {
        real += other;
        return *this;
    }
 
    ComplexNumber operator + ( T& other )
    {
        ComplexNumber temp(*this);
        return (temp += other);
    }
 
    ComplexNumber& operator -= ( T& other )
    {
        real -= other;
        return *this;
    }
 
    ComplexNumber operator - ( T& other )
    {
        ComplexNumber temp(*this);
        return (temp -= other);
    }
 
    ComplexNumber& operator *= ( T& other )
    {
        real *= other;
        imag *= other;
        return *this;
    }
 
    ComplexNumber operator * ( T& other )
    {
        ComplexNumber temp(*this);
        return (temp *= other);
    }
 
    ComplexNumber& operator /= ( T& other )
    {
        real /= other;
        imag /= other;
        return *this;
    }
 
    ComplexNumber operator / ( T& other )
    {
        ComplexNumber temp(*this);
        return (temp /= other);
    }
};
 
Post Reply