Splitting Assets from Code: Difference between revisions

Almost finished writing this section
m (Fixed capitalization)
(Almost finished writing this section)
Line 99:
 
== Having a buffer somewhere in RAM ==
Our game has about 4MB of RAM to work with (8 if the Expansion Pak is available), and roughly 1.5 to 2MB would be occupied by the framebuffers, Z-Buffer, and the code itself. So why don't we instead make use of those 2 to 2.5MB we have free to store our assets there?
 
All we have to do is create a new C file, and in it we place a global buffer just like we did in the previous section. Except this time, we're not going to link this data to our codesegment, rather we'll tell the N64 to reserve this part of RAM for our buffer. We'll need to modify the makefile somewhat, as we'll need to compile our C file into an object file. Lets assume our buffer is in a C file called <code>texbuf.c</code>.
 
Your makefile should already have a dedicated <code>CODEFILES</code> taarget where you put all your C files, and then it gets compiled into one big <code>codesegment.o</code> object file via <code>CODEOBJECTS = $(CODEFILES:.c=.o)</code> and then <code>gcc -o $(CODESEGMENT) -r $(CODEOBJECTS)</code>. The idea now is that you create a new taarget, such as <code>DATAFILES</code>, and you place your buffer files here.
 
<syntaxhighlight lang="makefile">
DATAFILES = buffer.c
DATAOBJECTS = $(DATAFILES:.c=.o)
</syntaxhighlight>
 
<tabber>
Spec file =
TODO
|-|
Linker script =
TODO
</tabber>
 
A useful trick, you can <code>extern</code> the codesegment as well:
<syntaxhighlight lang="c">
extern u32 _codeSegmentRomStart[];
extern u32 _codeSegmentRomEnd[];
</syntaxhighlight>
 
You can use this, for instance, to load your assets right after the code segment by loading them into the address at <code>_codeSegmentRomEnd</code>. You don't ''need'' to create an object file with your buffer as you can just write to any RAM address you seem fit (for instance, you can just define <code>u8* buffer = 0x80000400</code> globally, and then treat it as a buffer) but it sure helps as the compiler/linker can potentially catch buffer overflows and/or segment overlapping.
 
You can see an implementation of this buffer method here (TODO: ADD A LINK).
 
== Finalizing the code ==