|
|
Mac OSX Nintendo DS Development
Day 1 Tutorial
Alright, even though there are many great
tutorials on starting DS development
this one is made to be Mac OSX specific.
So here's what you will need for basic development
For testing programs on the actual Nintendo DS in addition you will need
After that you open up devkitArm Release 12 which should be a disc image on your desktop.
Install the the devkitarm package and libnds package.
They are installed in /opt/local/ just for future reference that is how you will link them to your project
Devkitarm has all your compiling tools and tools for making the ds roms
libnds has all your defines and other setup files to include to your project
Now look at the mac version of doublec's tutorial
My xcode project file doesn't work to build our yet so we will be doing this the unix command line way.
Open terminal (in applications/utilities) and type cd and then the directory of where my MacTemplate folder is.
Now type make. It should say make[1]: `where the folder is/MacTemplate/Template.ds.gba' is up to date.
That means it's probably working. Now let's see why it works. First we will look at the code because it's the coolest part.:)
go to source and open arm9_main.cpp
see where it says consolePrintf("\n\n\tMac OSX DS Developing\n"); ?
You can put whatever you want in Place Of Mac OSX DS Developing and this will print your code on the DS.
Even though there are many other great and much better tutorials i thought if I'm going to do one I might as well go the whole 9 yards or whatever.
So lets take a look at our arm9 code shall we?
Lets look at the includes first
The first file (nds.h) is basically your nds library.
The second is the specific arm9 feature of console printing
In layman's terms (Because im a layman) these files define everything we say in our code
They define the commands we write such as consoleInitDefault is defined in console.h
Moving on to the code
Here we get set up. int main is what we are calling this command, uhh, thing. We turn everything on we set up the screen and tell which mode to use (videoSetMode)
The rest is pretty self explanatory
Now for the console printing
I was gonna write a tutorial on just console printing but here is all you need to know.
YAY! It took me a while but now the template has the working arm7 code so all this code works on mac. Including reading where the stylus is.
Now we look at the arm7 code.
|
//////////////////////////////////////////////////////////////////////
// Demo1 ARM7 Code - Based on an example shipped with NDSLIB.
// Chris Double (chris.double@double.co.nz)
//////////////////////////////////////////////////////////////////////
#include
//////////////////////////////////////////////////////////////////////
#define TOUCH_CAL_X1 (*(vs16*)0x027FFCD8)
#define TOUCH_CAL_Y1 (*(vs16*)0x027FFCDA)
#define TOUCH_CAL_X2 (*(vs16*)0x027FFCDE)
#define TOUCH_CAL_Y2 (*(vs16*)0x027FFCE0)
#define SCREEN_WIDTH 256
#define SCREEN_HEIGHT 192
s32 TOUCH_WIDTH = TOUCH_CAL_X2 - TOUCH_CAL_X1;
s32 TOUCH_HEIGHT = TOUCH_CAL_Y2 - TOUCH_CAL_Y1;
s32 TOUCH_OFFSET_X = ( ((SCREEN_WIDTH -60) * TOUCH_CAL_X1) / TOUCH_WIDTH ) - 28;
s32 TOUCH_OFFSET_Y = ( ((SCREEN_HEIGHT-60) * TOUCH_CAL_Y1) / TOUCH_HEIGHT ) - 28;
//////////////////////////////////////////////////////////////////////
void startSound(int sampleRate, const void* data, uint32 bytes, u8 channel=0, u8 vol=0x7F, u8 pan=63, u8 format=0) {
SCHANNEL_TIMER(channel) = SOUND_FREQ(sampleRate);
SCHANNEL_SOURCE(channel) = (uint32)data;
SCHANNEL_LENGTH(channel) = bytes;
SCHANNEL_CR(channel) = SOUND_ENABLE | SOUND_ONE_SHOT | SOUND_VOL(vol) | SOUND_PAN(pan) | (format==1?SOUND_8BIT:SOUND_16BIT);
}
s8 getFreeSoundChannel() {
for (int i=0; i<16; i++) {
if ( (SCHANNEL_CR(i) & SOUND_ENABLE) == 0 ) return i;
}
return -1;
}
//////////////////////////////////////////////////////////////////////
void InterruptHandler(void) {
static int heartbeat = 0;
if (IF & IRQ_VBLANK) {
uint16 but=0, x=0, y=0, xpx=0, ypx=0, z1=0, z2=0, batt=0, aux=0;
int t1=0, t2=0;
uint32 temp=0;
uint8 ct[sizeof(IPC->curtime)];
// Update the heartbeat
heartbeat++;
// Read the X/Y buttons and the /PENIRQ line
but = XKEYS;
if (!(but & 0x40)) {
// Read the touch screen
x = touchRead(TSC_MEASURE_X);
y = touchRead(TSC_MEASURE_Y);
xpx = ( ((SCREEN_WIDTH -60) * x) / TOUCH_WIDTH ) - TOUCH_OFFSET_X;
ypx = ( ((SCREEN_HEIGHT-60) * y) / TOUCH_HEIGHT ) - TOUCH_OFFSET_Y;
z1 = touchRead(TSC_MEASURE_Z1);
z2 = touchRead(TSC_MEASURE_Z2);
}
batt = touchRead(TSC_MEASURE_BATTERY);
aux = touchRead(TSC_MEASURE_AUX);
// Read the time
rtcGetTime((uint8 *)ct);
BCDToInteger((uint8 *)&(ct[1]), 7);
// Read the temperature
temp = touchReadTemperature(&t1, &t2);
// Update the IPC struct
IPC->heartbeat = heartbeat;
IPC->buttons = but;
IPC->touchX = x;
IPC->touchY = y;
IPC->touchXpx = xpx;
IPC->touchYpx = ypx;
IPC->touchZ1 = z1;
IPC->touchZ2 = z2;
IPC->battery = batt;
IPC->aux = aux;
for(u32 i=0; icurtime[i] = ct[i];
}
IPC->temperature = temp;
IPC->tdiode1 = t1;
IPC->tdiode2 = t2;
//sound code :)
TransferSound *snd = IPC->soundData;
IPC->soundData = 0;
if (snd) {
for (int i=0; icount; i++) {
s8 chan = getFreeSoundChannel();
if (chan >= 0) {
startSound(snd->data[i].rate, snd->data[i].data, snd->data[i].len, chan, snd->data[i].vol, snd->data[i].pan, snd->data[i].format);
}
}
}
}
// Acknowledge interrupts
IF = IF;
}
//////////////////////////////////////////////////////////////////////
int main(int argc, char ** argv) {
// Reset the clock if needed
rtcReset();
//enable sound
SOUND_CR = SCHANNEL_ENABLE | SOUND_VOL(0x7F);
IPC->soundData = 0;
// Set up the interrupt handler
IME = 0;
IRQ_HANDLER = &InterruptHandler;
IE = IRQ_VBLANK;
IF = ~0;
DISP_SR = DISP_VBLANK_IRQ;
IME = 1;
// Keep the ARM7 out of main RAM
while (1) swiWaitForVBlank();
return 0;
}
//////////////////////////////////////////////////////////////////////
|
Wooh... That's alotta code. Let's get to it
It first includes the nds library, then defines lot's of touch stuff
Touch_cal is the calibration of the touch screen. Below that it defines screen width and height.
Warning! I'm no amazing coder so don't take this as God-spoken truth.
I'm not sure about the s32 things
The next bit of code sets up sound. It sets the channel and makes sound possible
This file just basically sets up all possible arm7 functions that's why it is so long
It sets up touch screen, clock and interrupt handler
I will probably write more once i understand it myself but for now look through this a long but good read.
All in all this is a pretty simple little program.
Now to see how to convert demos and examples into mac format you can take a look at the makefile to see how it's all done.
There shouldn't ever be too much fiddling to do for mac as long as you are using that makefile and devkitarm
That might confuse newer coders so I will write more stuff later but for now look at Doublec's tutorials to learn how to do other features
|
|