Path: chuka.playstation.co.uk!news From: James Russell Newsgroups: scee.yaroze.beginners,scee.yaroze.programming.codewarrior,scee.yaroze.programming.2d_graphics Subject: Re: Still strugglin' with the basics: broken sprite code (long) Date: Thu, 16 Jul 1998 14:53:10 +0100 Organization: Sony Computer Entertainment Europe Lines: 71 Message-ID: <35AE05C6.489D09F9@scee.sony.co.uk> References: <35AC71D4.AA79EB4A@easynet.co.uk> <35AD10B6.153EE9C5@compuserve.com> <35AD2E8E.DEF2C169@nospam.easynet.co.uk> <35ADFF08.A310D245@easynet.co.uk> NNTP-Posting-Host: camfw01.millennium.co.uk Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Mailer: Mozilla 4.05 [en] (Win95; I) Xref: chuka.playstation.co.uk scee.yaroze.beginners:420 scee.yaroze.programming.codewarrior:305 scee.yaroze.programming.2d_graphics:446 Philip Gooch wrote: > > Does the GsOT_TAG structure count as an enormous data structure? Generally, yes. > Is that why in this case it needed to be global (or static)? No. The reason it must be defined outside a function (or static) is because the GPU will be reading it (so it can draw the screen) while the CPU is off Doing Other Things. If GsOT_TAG was defined locally (and not static), its contents would be valid only for the 'life' of the function (the time spent inside the function) it is contained in. This means that if you create a valid GsOT_TAG structure (which is done by the system when you call GsSortSprite() or GsSortObject4() ) and then set the GPU off reading it (using GsDrawOT), and then exit your function to go and do other things, then the space used by the GsOT_TAG structure will be reclaimed and probably wiped over, while the GPU is reading it! This is a very Bad Thing. So defining this structure globally will solve the problem. If you want to keep it as a local variable, defining it as 'static' will tell the compiler not to reclaim the space when the function exits. A variable that is defined 'static' in a function only has the scope of that function, but it is not destroyed when the function returns, unlike other local variables which are not defined 'static'. Example: ============ 8< snip ================ void nonStatic(void) { int ns = 0; printf("ns = %d\n",ns); ns++; } void usesStatic(void) { static int s = 0; // s is defined static here printf("s = %d\n",s); s++; } void main(void) { nonStatic(); nonStatic(); nonStatic(); usesStatic(); usesStatic(); usesStatic(); } ============ 8< snip ================ This program gives the following output: ns = 0; ns = 0; ns = 0; s = 0; s = 1; s = 2; Just to make life more confusing, a 'static' global variable has the scope of the object file it is defined in, so only functions in that C source file can use it. Cheers, James -- == James_Russell@scee.sony.co.uk +44 (171) 447-1626 == Developer Support Engineer - Sony Computer Entertainment Europe A sufficiently advanced god would use evolution.