Gamestudio Links
Zorro Links
Newest Posts
Trading Journey
by howardR. 04/28/24 09:55
Zorro Trader GPT
by TipmyPip. 04/27/24 13:50
Help with plotting multiple ZigZag
by M_D. 04/26/24 20:03
Data from CSV not parsed correctly
by jcl. 04/26/24 11:18
M1 Oversampling
by jcl. 04/26/24 11:12
Why Zorro supports up to 72 cores?
by jcl. 04/26/24 11:09
Eigenwerbung
by jcl. 04/26/24 11:08
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (1 invisible), 735 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
wandaluciaia, Mega_Rod, EternallyCurious, howardR, 11honza11
19049 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Useful Macros #148667
08/17/07 14:34
08/17/07 14:34
Joined: Jul 2006
Posts: 783
London, UK
sheefo Offline OP
User
sheefo  Offline OP
User

Joined: Jul 2006
Posts: 783
London, UK
I was thinking, perhaps people can post some useful macros definitions here so we can have a collection for everyone to benefit from.

Here is one I came up with, since I found out about using tokens in macros.
It is so much better than using "malloc(sizeof(type))", and in VC++ it is highlighter
See below:

MACRO:
Code:

#define new(type) malloc(sizeof(type##))
#define delete(object) free(object)



USE:
Code:

// Create new object like this...
int *myint = new(int);
// ...Instead of like this.
int *myint = (int*)malloc(sizeof(int));
...
// Lookes better than 'free'
delete(myint);



Now it's your turn...

Re: Useful Macros [Re: sheefo] #148668
08/17/07 20:21
08/17/07 20:21
Joined: Jan 2006
Posts: 1,829
Neustadt, Germany
T
TWO Offline

Serious User
TWO  Offline

Serious User
T

Joined: Jan 2006
Posts: 1,829
Neustadt, Germany
Not bad mate, I had the same idea some time ago. I only defined a bigger, but in my eyes more useful, delete macro. (error should be replaced by some log function)

#define new(x) malloc(sizeof(x))
#define delete(x) if(x!=NULL) free(x); else error("Memory Access Error!")

Re: Useful Macros [Re: TWO] #148669
08/18/07 17:33
08/18/07 17:33
Joined: Jan 2002
Posts: 4,225
Germany / Essen
Uhrwerk Offline
Expert
Uhrwerk  Offline
Expert

Joined: Jan 2002
Posts: 4,225
Germany / Essen
Here is my assert macro:
Code:
#ifdef ASSERTIONS
#define assert(condition,message) if(!(condition)) printf("Assertion failed: %s",&message)
#else
#define assert(condition,message) ;
#endif



If you now write #define ASSERTIONS you can use the assert macro as you would use as any other function. If you delete the #define ASSERTIONS command from your main file all assertions in your code will be ignored.

For those who don't know what an assertion is: An assertion is a assumption you do while programming. Let's imaging you had a function that takes an int as a parameter that should never be 0. If you write as the very first instruction in your function assert(MyInt != 0,"Passed 0 to MyFunction"); you will be notified whenever your assumption is proven wrong. Assertions do not influence the runtime speed of your application as you can deactivate them when publishing your game.

Unfortunately the __line__ and __file__ macros are not yet implemented. They would greatly contribute to the assert function.


Always learn from history, to be sure you make the same mistakes again...
Re: Useful Macros [Re: Uhrwerk] #148670
08/19/07 16:32
08/19/07 16:32
Joined: Jul 2006
Posts: 783
London, UK
sheefo Offline OP
User
sheefo  Offline OP
User

Joined: Jul 2006
Posts: 783
London, UK
This isn't that useful, but it works.
If you need to initialze a variable in a for loop, you can call a custom loop, like this:

Code:

#define loop(i,j,k) i##;for(;j;k)
...
loop(int i = 0, i < n, i++) { ... }





Another one, this one is a really cool one. This one I use a lot of the time.
An easy way to loop through linked lists.

Code:

#define lListLoop(list, node) for(node = list.start; node != NULL; node = node->next)
...
lListLoop(MyLinkedList, node)
{
i = node->data;
...
}




PS: Thanks guys

Re: Useful Macros [Re: sheefo] #148671
11/19/07 16:48
11/19/07 16:48
Joined: Nov 2005
Posts: 94
Texas
3
3Dski Offline
Junior Member
3Dski  Offline
Junior Member
3

Joined: Nov 2005
Posts: 94
Texas
Quick macro related question. Excuse me if I missed something in the manual. Is there a way to view a preprocess code listing to view macro expansions for debugging them?

Editted: I also tried a similar syntax as the "assert" macro above, with ";" as being the only replacement text when "assertions" is not defined and this doesn't seem to work under Lite-C pure mode.

Last edited by 3Dski; 11/19/07 19:44.
Re: Useful Macros [Re: 3Dski] #148672
11/19/07 19:57
11/19/07 19:57
Joined: Nov 2005
Posts: 94
Texas
3
3Dski Offline
Junior Member
3Dski  Offline
Junior Member
3

Joined: Nov 2005
Posts: 94
Texas
Okay... I puzzled over the assertion macro posted above, and the replacement text of ";" for the #ifndef side doesn't seem to work under Lite-C pure mode. I've written some logging related macros that use a work around for the #ifndef side:

EDITTED: I think it best to add a reminder that logging is on, so I added some addtional code to show that logging is on and when the log file is closed.
Code:

#define LOGGING

#ifdef LOGGING
#define START_LOGGER(n) printf("Logging is on.");FILE* LOG_HANDLE = fopen( n, "w"); if (LOG_HANDLE == NULL) error("Couldn't open log file");
#define LOG(log_msg) fprintf( LOG_HANDLE, "%0.2d:%0.2d:%0.2d - %s\n", (int)sys_hours, (int)sys_minutes, (int)sys_seconds, (char*)#log_msg );
#define CLOSE_LOGGER if ( LOG_HANDLE != NULL) fclose(LOG_HANDLE);printf("Log closed.");
#else
#define COMMENT //
#define START_LOGGER(n) COMMENT
#define LOG(log_msg) COMMENT
#define CLOSE_LOGGER COMMENT
#endif

...
START_LOGGER("test.log")
...
LOG( "start_panel" )
...
CLOSE_LOGGER




Last edited by 3Dski; 11/19/07 20:09.

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1