Thread: Type conversion question.

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    7

    Question Type conversion question.

    Hi,
    I've been studying gamedev for 2 months now. We use RenderWare at school. RenderWare is written in C, but we use C++. I've now come to see how a mix like that don't always work out as one would like.

    RenderWare uses there own types for vectors, matrices and so on. I would like to use my own classes for that, cuz seriously, how can some one stand doing complex math formulas with functions like RwV3dAdd, RwV3dSub, RwV3dCrossProduct, and RwV3dDotProduct, where all functions take a RwV3d pointer as its first argument as the target.

    My classes uses operator overloads to do all of that in a clean fashion.

    My question is now. In the end, i will always have to use a RenderWare function to do certain things. I wonder if its possible to give a function which needs the type 'RWType', a completely different type, which would be converted when passed to this function i cant change ?

    I hope you understand what i meen =P

    Thx

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    You could just write wrappers for the renderware functions and overload those operators.

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    It is probably smart to write wrappers, but you but also write an operator RWType ().

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    You could have a member function (say get_raw()) return a reference to the innards of the class and pass this innard to the functions; whether you think this violates encapsulation is left for you to decide. GMP 4's C++ wrapper does it though.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    7
    I went for the 'operator RwV3d()' and 'operator RwV3d*()' way.
    Cuz my constructors can take both RwV3d and RwV3d* as a parameter (like RwV3d to CVector3 conversion).
    This gives me the freedom to convert back and forth the two types without me ever having to actually think about it.

    If i want, i could even make it go so far as making math operators work with both RwV3d and CVector3. (not gonna do that tho =P)

    CVector3 is my own vector class based on RW functions, and RwV3d is RWs vector (a struct with 3 floats).

    Anyways... thanks for all the feedback. Helped me alot.

  6. #6
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by ellis
    I went for the 'operator RwV3d()' and 'operator RwV3d*()' way.
    Cuz my constructors can take both RwV3d and RwV3d* as a parameter (like RwV3d to CVector3 conversion).
    This gives me the freedom to convert back and forth the two types without me ever having to actually think about it.
    be very wary of implicit conversions. They can bite you in the bum when you least expect it. IMO, it is good practice to use explicit constructors for conversions. I also like to write small inline casting template functions to convert from class types to others.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  5. Question type program for beginners
    By Kirdra in forum C++ Programming
    Replies: 7
    Last Post: 09-15-2002, 05:10 AM