Mixing C and C++

  • Thread starter Thread starter halkun
  • Start date Start date
Status
Not open for further replies.
Borland's VCL doesn't really use templates, because it's written in Delphi, which doesn't have templates. However, I could well believe the C++ interfaces to it needed to use templates in order to support sets - something Object Pascal has in the language.

While not templates, C# is at least getting generics in 2.0 ... which means Delphi will be getting it too, thank goodness ;)  In fact, it's possible D2005 has it already.
 
I have to say, there isn't much use for macros in C++. I mean, you can just use inline functions and templates, and they are type safe, not to mention they can be debugged easier.
 
I have to say, there isn't much use for macros in C++. I mean, you can just use inline functions and templates, and they are type safe, not to mention they can be debugged easier.
There is a definite use for macros in C++, even though the "pure" scholars try to deny it. For example, you might have a very common kind of loop with the same instructions done at the start and end every time. Would you rather write:Code: [Select]
Code:
// ...for ( ... ; ... ; ... ) {   // ...   actual_code_unique_to_this_particular_loop;   // ...}// ...
Or, with the use of macros, you could:Code: [Select]
Code:
#define BEGIN_LOOP(args) ...; for ( ... ; ... ; ...) {#define END_LOOP(args) ...; } ...;BEGIN_LOOP(args)    actual_code_unique_to_this_particular_loop;END_LOOP(args)

Macros are low-level and have some severe limitations, but since they're purely text-based and work entirely outside of C++, you can do some very useful things that would simply not be possible in C++. How would you declare a new scope and fill it with user-supplied code (see example above) in C++?

The whole reason people use them these days is for reducing code redundancy and improving code readability and maintainability. Personally, while making a program relying heavily on arrays and maps and being able to traverse the elements of these, I felt the need for a foreach statement in C++. Of course there isn't one, and there's no way to implement one inside of C++ (that looks pretty, anyway). But with macros, you can usually hide away all the nitty-gritty details inside a simple for statement, giving:Code: [Select]
Code:
#define foreach(type, item, container)   for ( ... ; ... ; ... )foreach( int, value, some_array ) {    printf("%d\n", value);}
Not perfect, and the foreach macro wouldn't behave exactly like C++ (no space allowed between foreach and the opening parenthesis, for example), but you have to admit that's pretty damn more handy to code than manually messing with iterators or Microsoft's POSITION values every time you just want to simply step through all items in a container. This simple solution would not be possible without preprocessor macros.

That having been said, there are plenty of ways to use macros badly. But I usually say that as long as each macro keeps a clear semantic meaning, they do not obstruct debugging (unlike macros that have you wondering "what the hell does this do?"). Special care must of course be taken too, so you don't clutter up the variable name space with "hidden" variables declared from inside macros; these are much harder to debug, of course. An ideal macro does what it appears to do, and nothing else.

As for type safety, well, I usually say that macros should never be used in situations complex enough for type safety to become an issue. That having been said, ironically templates are often used to get around type restrictions, i.e. have them act more like macros which accept anything, and let the compiler gawk if it doesn't work with the underlying code.
 
I didn't say they useless, now. You can use them for inclusion guards. :D
 
Qhimm: i think that too complex macros are hard-to-read, because it takes some time to figure out what are they doing. I would use inline functions instead, because they do similair thing as macros (replace their referece with code) and are easy to read and debug.

Well and guys lets keep it simple here, or Halkun will get scared of C++ :)
 
... You know... I've had the idea of building a new engine for FF7 since the start of the remake project. (I even suggested it a few times...) However, the extent of my programming abilities is PHP.

So all I'm going to say is Good Luck, and I'll be here when you need to play-test it.
 
So okay. Let's try to sum up what needs to be rewritten then.
I remeber someone (maybe halkun or SaiNt) posted how the FF7 engine is built up - that it first starts the kernel itself, and then, runs the field, battle or menu engine, depending on which one is needed.

So could someone maybe draw a little diagram for us, how the structure of the Engine should look like? (Means which "sub-programs" need to be written and what is called by what...) :-?

 - Alhexx
 
A remake of the engine might not need to use the module swapping, because PCs have a lot more memory than consoles.
 
Holy Macro's batman.

Macros are handy for creating automatic names and defining constants. These are things most people don't do because they use a normal IDE or are lazy. Either way :)

I've used macros a lot in embeded systems which use C or C++ (I prefer the later without the use of constant objects of course that is a huge problem with an embeded system).

For example if you are defining a GUI (Yes you have to do that often in an embeded system).  It's much easier to do this
[/code]
#define SHOWB(M) _show_##M
#define SHOW(M) void SHOWB(void)
SHOW(RootWindow)
{
}
[/code]
Than it is to type _show_RootWindow
void _show_SensorSettings(void)
void _show_...
And then to define the base name over and over again.  It makes for a LOT less typing.
Especially if you say #define ROOT_WIN RootWindow then if you decide to change your root windows name you just change it in one place.  This seems like a C thing but it works just fine in C++.  Especially when you only have 16K of RAM and 64K of FLASH to deal with.  Saves LOTS of memory it turns out (since you don't have extraineous definitions) and makes the code a LOT more readable (and easier to modify and extend).

Alhexx hmmmmm
I really don't know I believe Halkun has refered to this in gears but no specific details exist.

I believe he said it went something like
Load Kernel
Kernel Loads Game module (also known as Field module)
Game Module starts up basic game loop (IE wait for person to start new game or load old one). Then the game module loads in a FIELD location.  If a combat situation arises the GAME I think the game saves a few variables then dumps the field engine and loads the battle engine.  Mini games are done much the same way.  Essentially the current field module gets dumped and the minigame gets loaded.  The the field module gets reloaded and it's state restored as needed.  The Game is the field engine I believe is the best way to look at it.  The kernal has the game state information in it.  So if it's in the START state it grabs the information in the script about start up and begins from there (Shows the intro movie then loads the train station location and the game is a foot so to speak).  I believe some things in the kernel are understood though. (IE game state information for example).

I can't verify if this is what actually happens however :)

A remake of the engine might not need to use the module swapping, because PCs have a lot more memory than consoles.
Hmmmm no it might not need module swaping at all however the basic system intercommunication it has is needed.  So essentially you'll need all the modules, you just don't need to worry about loading and unloading them all the time perhaps?


How about a Dynamic Link Library approach?
The Kernal loads the basic game 'DLL' which uses call backs to the kernal for various support functions.  The kernal it'self can use in a plugin interface for handling graphics support etc.  This makes the whole thing more generic. Much like PCSX and ePSXe operate under Linux and windows almost identically.  The main GUI can be used to handle picking what support modules you wish to use. The ultimate in compatibility support so to speak :)


Cyb
 
I'm going to kick off a new tech thread on engine building, you macro boys have fun.
 
Status
Not open for further replies.
Back
Top