Home | News | Downloads | Projects | Articles | Awards | Forums | Chat | Guestbook | About | Arcade
DealsFor.me - The best sales, coupons, and discounts for you



Forum Topics Replies Last Post Info
Older topics
Here you can access all topics posted from May 12th 2005 to Apr 9th 2006 (In read only mode).
1126 15694 9 Apr, 2006, 11:06
In: Last Post[GAME]Learn how to count to...
By: DJ Omnimaga
 

  post

 [68k C] TIGCC Help
Liazon
Posted: 19 Apr, 2006, 14:41


Staff


Group: Coders Of Tomorrow
Posts: 1491
Member No.: 24
Joined: 11 Apr, 2006



Once again, I have a question about TIGCC.

When is it good to use a global variable?

Also, I noticed in the shadowfalls source code, mirogfx.h looks like this:

#define GetTilesMiro(dir,pl,nr) &(mirotiles[(((dir)+(nr))*MHEIGHT*2)+(pl*MHEIGHT)])


unsigned short mirotiles[8*MHEIGHT*2] = {
// Tile 0
0x07DC,0x1BF4,0x2FE8,0x1FB4,0x2F7C,0x1FF8,0x0F50,0x0E50
,0x1110,0x0EE0,0x0BA0,0x0CA0,0x04E0,0x0720,0x03E0,0x03A0
,0x07DC,0x1C2C,0x3118,0x124C,0x34D4,0x12F8,0x0950,0x0F50
,0x1F90,0x0FE0,0x0D60,0x0AA0,0x07A0,0x07A0,0x03E0,0x03E0
// Tile 1
,0x07DC,0x1BF4,0x2FE8,0x1FB4,0x2F7C,0x1FF8,0x0F50,0x0E50
,0x1110,0x0EE0,0x1BA0,0x27E0,0x27E0,0x1990,0x1D28,0x0BF0
,0x07DC,0x1C2C,0x3118,0x124C,0x34D4,0x12F8,0x0950,0x0F50
,0x1F90,0x0DE0,0x1660,0x33A0,0x3C20,0x1E50,0x1DB8,0x0FF0
// Tile 2
,0x07E0,0x1BD8,0x2FF4,0x3FF8,0x1FFC,0x1FF8,0x3FFC,0x5DBA
,0x2FF4,0x2FF2,0x1FF2,0x0E7E,0x0FFA,0x08F4,0x0FA0,0x05F0.....
};[CODE]

Why are these tiles defined in a header file, and what exactly is the macro for? how do macros work?


--------------------
Projects: In need of sprites...
Top
saubue
Posted: 19 Apr, 2006, 15:22


member of boolsoft


Group: Members
Posts: 160
Member No.: 19
Joined: 11 Apr, 2006



Since your the only person who has got the source expect of me, I will answer biggrin.gif

Since diferent functions in different C-Files are needing the tiles for Miro very often, I decided to store them inside the game. I think it would be more effective if I would declare the array as an extern variable and store it inside a C-File, but I always do it that way.

Macros are just replacements. Every time I write GetTilesMiro(dir,pl,nr), the compiler replaces it with &(mirotiles[(((dir)+(nr))*MHEIGHT*2)+(pl*MHEIGHT)]), which is much more pain to write. It's the same as define HEIGHT 5 to use HEIGHT instead of the value 5 to make your code more readable. The nice thing about macros is that you can pass values. If you want to have more control (e.g. if you want to acceppt only certain types of arguments), you can also use an inline function:

CODE
inline unsigned short *GetTiles(short dir, short pl, short nr)
{
   return &(mirotiles[(((dir)+(nr))*MHEIGHT*2)+(pl*MHEIGHT)]);
}



EDIT: Oh, I nearly forgot your question about the global variable:
QUOTE (TIGCC Documentation FAQs)
Q: You quote very often in the documentation that we need to avoid global variables as much as possible. What is so wrong with them? 
A: Of course, globals are sometimes very necessary, but they are also easy to overuse. The good programming practice is to keep the scope of each identifier as small as possible. But, in addition to conventional programming theory, there are some extra reasons against globals which are related particularly to programming on TI calculators. First, every global variable is translated to the absolute addressing mode. As this mode is non-relocatable, the compiler adds a 4-byte long entry into the relocation table for each apperance of the global variable. If global variable "a" is mentioned say 100 times in the program, this is extra 400 bytes. When I ported some programs from PC to TI, I suceeded to reduce program space from 20K to 16K just by eliminating globals. And 4K of space is quite worth on TI... Second, globals are kept in the .89z (or .9xz) file itself. Sometimes it is good (to keep permanent data which will survive after the program exits etc.), but very often it is a wasting of file space (as the file length is limited). Read questions given further in this document to see how to reduce a file size if you have a strong reasons for using global arrays. To learn: avoid extensive usage of globals as much as possible! 


In fact, I try to put global arrays (which are not changed during the execution) like graphics in headers with their access macros and include the headers whenever I need them. If a program gets too large, I store them in seperate files (like in Shadow Falls alpha3: sfgfx.data and sfmaps.data)

Zeljko also says that every apperance of the variable reults in more size - I'm not sure if this is true when using macros...


--------------------
user posted image
Top
0 User(s) are reading this topic (0 Guests and 0 Anonymous Users)
0 Members:
InvisionFree - Free Forum Hosting
Free Forums with no limits on posts or members.
Learn More · Register for Free

Topic Options post





TI-Freakware's TI Ring
TI-Freakware's TI Ring
[ Join Now | Ring Hub | Random | << Prev | Next >> ]

Hosted for free by InvisionFree* (Terms of Use: Updated 2/10/2010) | Powered by Invision Power Board v1.3 Final © 2003 IPS, Inc.
Page creation time: 0.1364 seconds | Archive

Skin by xlibman and Jc
All programs and games are property of their respective owners.