The Code Wizard's Musings

Diary of a husband, father and coder

WinROTT Port

clock November 11, 2008 20:58 by author gharris

I've been able to make quite a bit of progress converting C modules to C++ modules in WinROTT. Most of the problems I encounter at this stage are missing link symbols. It is time consuming rearranging code and inserting extern "C" blocks. As always, testing consumes the most amount of time. I hope to have a significant (to me) release this weekend. I am down to about 30 modules that need converting. This is down from roughly 60 when I started.

I've had a couple of inquiries regarding my porting strategy. I employ a concept of "least dependency" when selecting modules to move over to C++ from C. In practice, you simply choose the last C module in the compile process. In theory, the last C module to compile has the fewest dependencies and should require the smallest amount of fixes for missing symbols. The occasionally doesn't work, but it's simple to implement. I try to never port more than one module between testing. For me, testing entails playing several game levels to verify code execution. When I complete the C to CPP migration, I want to implement some unit testing, which should significantly speed up future modifications.

When the migration is complete, I intend to scrub the code of extern "C" blocks and create proper header files for the modules. Currently, the code has a number of places where I simply threw in the function prototypes to get the code to compile and execute. You'll often see multiple prototypes for the same function included in the source. This is a result of not having proper header files that can be included as needed in source modules.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Back To WinROTT

clock August 26, 2008 20:11 by author gharris

Summer is over. My son is off to college, my daughter has started her sophomore year in high school and the web site redesign is stable. It's time to crack open the WinROTT project and get started.

I am moving UserActor.c to a cpp file. There are some dependencies to resolve. This may take a couple of evenings while I get back into the swing of things. I am really feeling the need for some sort of .c and .cpp cross referencing tool. I still have somewhere around fifty .c files to convert. I'm looking forward to getting rid of all the extern "C" hacks I've had to use while the code is mixed between c++ and c. I was able to move a few functions outside of the extern "C" blocks. Whenever I am able to do this, it feels like progress is being made.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Release 4 of WinROTT Source Code

clock July 17, 2008 21:33 by author gharris

I managed to complete the port of rt_actor.c to rt_actor.cpp. There are some source code changes, and a lot of changes to header files so both C and C++ files will compile and link properly. The source code may be downloaded from my WinROTT page. Please let me know if you have any problems.

I will be taking a break from WinROTT for a couple of weeks to enjoy the summer with my children. I will be updating some porting notes in my wiki. I have some notes regarding function and enum declarations a developer needs to watch out for when porting from C to C++. Please have fun playing WinROTT! Enjoy the summer.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


WinROTT Port and the !^@%$# dirtype enum

clock July 16, 2008 21:21 by author gharris

The dirtype enum has been driving me crazy in the process of porting WinROTT C code to C++. The C compiler allows variables of type dirtype to be used interchangably with int types.  The C style enum is defined as:

typedef enum {
       east,
       northeast,
       north,
       northwest,
       west,
       southwest,
       south,
       southeast,
       nodir
} dirtype;

// So with C, you are allowed to perform the following:
dirtype x;
int y = north;
x = y >> 1; // (result is northeast)

// In C++, you cannot assign or cast an integer type to  a dirtype.
// I could have defined operator overloads for dirtype, but it was
// easier to work out a brute force translation until I get all the files
// moved over to C++.
// The fix is something like this:
            switch (y >> 1)
            {
            case 0:
                x = east;
                break;
            case 1:
                x = northeast;
                break;
            case 2:
                x = north;
                break;
            case 3:
                x = northest;
                break;
            case 4:
                x = west;
                break;
            case 5:
                x = southwest;
                break;
            case 6:
                x = south;
                break;
            case 7:
                x = southeast;
                break;
            default:
                x = east;
                break;
            }

Because of the mixed environment, it is difficult to modify enum so that it can interchange with an integer. What works for C doesn't work correctly for C++.  I ended up redefining the direction enum to use #define. It proved to be the simplest fix. No complex macros, no worrying about c++ decoration.

#define    east 0
#define    northeast 1
#define    north 2
#define    northwest 3
#define    west 4
#define    southwest 5
#define    south 6
#define    southeast 7
#define    nodir -99
typedef short dirtype;

This allows the same tokens to be used in both the C++ modules and the C modules. It also allows arithmetic on dirtype variables.

The code is compiling, but I have 86 unresolved symbol errors. These are caused by C++ decoration of names in most cases and should be easy to fix. Hopefully, I'll have something to release soon. 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Some notes on porting WinROTT to C++

clock July 15, 2008 20:12 by author gharris

I am in the process of porting the file rc_actor.c to rc_actor.cpp for the game Rise of the Triad (see my progress page). I had forgotten how flexible (or loose) C is compared to C++.

The difference in type checking between C and C++ can be maddening when porting simple function pointers:

In C, you can declare a function pointer as:

void (think *) (void *);

and

void gettarget(objtype *);

You can then assign:

think = gettarget;

In C++, you must declare gettarget as

void gettarget(void * tob)
{
    // Must assign ob to tob
    // tob is the void*, cast to objtype*
    objtype * ob = tob;
....
}

If you do not, then you will see the following error:

error C2440: '=' : cannot convert from 'void (__cdecl *)(void *)' to 'void (__cdecl *)(objtype *)'

Converting the function calls, and handling any code changes neccessary for casting void* to objtype* is very tedious. I'm down to about 75 errors in my work with rt_actor.cpp. I certainly hope it all works after I complete the port. Surprised

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


WinROTT Release

clock July 13, 2008 20:38 by author gharris

I managed to get another release of WinROTT posted today. I had settled on upgrading Bots.c to Bots.cpp. It turned out to be more work than I anticipated. Bots code is used by many other modules. I had to adapt a lot of header files to prevent the c++ compiler from decorating variables and functions. The following macros are very handy  when upgrading header files and function prototypes:

#ifdef  __cplusplus
extern "C" {
#endif

... Your declarations ...

#ifdef  __cplusplus
}
#endif

If you're a WinROTT fan, enjoy the code. 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5



 Subscribe to RSS



Disclaimer

This blog represents my personal hobby, observations and views. It does not represent the views of my employer, clients, especially my wife, children, in-laws, clergy, the dog, the cats or my daughter's horse. In fact, I am not even sure it represents my views when I take the time to reread postings.

© Copyright 2008

Sign in