Developer tips: Difference between revisions
Jump to navigation
Jump to search
How to use the callback function for Power and Reset |
|||
Line 49: | Line 49: | ||
Or use SYS_RETURNTOMENU for a "soft" return to the system menu, SYS_POWEROFF to shut down the wii (automatically to the appropriate Idle or Standby mode, depending on the WC24 setting), or SYS_POWEROFF_STANDBY or _IDLE to specify the mode and override the system setting. | Or use SYS_RETURNTOMENU for a "soft" return to the system menu, SYS_POWEROFF to shut down the wii (automatically to the appropriate Idle or Standby mode, depending on the WC24 setting), or SYS_POWEROFF_STANDBY or _IDLE to specify the mode and override the system setting. | ||
===How to use the callback function for Power and Reset=== | |||
This is just a way of doing this stuff, feel free to change the code. | |||
<source lang="c"> | |||
u8 HWButton = 0; | |||
/** | |||
* Callback for the reset button on the Wii. | |||
*/ | |||
void WiiResetPressed() | |||
{ | |||
HWButton = SYS_RETURNTOMENU; | |||
} | |||
/** | |||
* Callback for the power button on the Wii. | |||
*/ | |||
void WiiPowerPressed() | |||
{ | |||
HWButton = SYS_POWEROFF_STANDBY; | |||
} | |||
/** | |||
* Callback for the power button on the Wiimote. | |||
* @param[in] chan The Wiimote that pressed the button | |||
*/ | |||
void WiimotePowerPressed(s32 chan) | |||
{ | |||
HWButton = SYS_POWEROFF_STANDBY; | |||
} | |||
/** | |||
* Entry point. | |||
* @param[in] argc The number of arguments invoked with the program | |||
* @param[in] argv The array containing the arguments | |||
* @return 0 on clean exit, an error code otherwise | |||
*/ | |||
int main(int argc, char **argv) | |||
{ | |||
// Initialization | |||
SYS_SetResetCallback(WiiResetPressed); | |||
SYS_SetPowerCallback(WiiPowerPressed); | |||
WPAD_SetPowerButtonCallback(WiimotePowerPressed); | |||
while(1) | |||
{ | |||
// Do Stuff Here | |||
if(HWButton) | |||
break; | |||
} | |||
if(HWButton) | |||
{ | |||
SYS_ResetSystem(HWButton, 0, 0); | |||
} | |||
return 0; | |||
} | |||
</source> | |||
[[Category:Development]] | [[Category:Development]] |
Revision as of 08:32, 21 November 2008
General Programming Tips
- Keep your code commented throughout; it helps others help you.
- Any unused code should be deleted out of the program, unless it is a program for teaching people.
- If someone does the same thing in a more efficient way (i.e. faster and/or in less code), accept it and learn from it.
- It is a good idea to release your app open source so others can learn from your code.
- To keep things tidy, make your application store files in the data folder on the root of the SD card (fat0:\data\XXXXX, where XXXXX is the name of your app).
Code Snippets
Video Auto-Detect Routine
The libogc included in DevkitPPC r15 does this for you with one function call. Here is the video detect code from the DevKitPro Wii example.
#include <gccore.h>
static GXRModeObj *rmode = NULL;
// ...
rmode = VIDEO_GetPreferredMode(NULL);
if( CONF_GetAspectRatio() )
{
rmode->viWidth = 678;
rmode->viXOrigin = (VI_MAX_WIDTH_PAL - 678)/2;
}
VIDEO_Configure(rmode);
Please see also: Display Issues
Exit to Loader
It's a good idea to add some way to return to the loader, otherwise you have to reboot your Wii to exit.
// Just call the exit() function to go back to the loader
// Returning from main also works, since that calls exit for you
#include <stdlib.h>
// ...
exit(0);
How to use the Wiimote
A separate article is available: How to use the Wiimote.
Reboot Wii
Use:
#include <gccore.h>
// ...
SYS_ResetSystem(SYS_RESTART,0,0);
Or use SYS_RETURNTOMENU for a "soft" return to the system menu, SYS_POWEROFF to shut down the wii (automatically to the appropriate Idle or Standby mode, depending on the WC24 setting), or SYS_POWEROFF_STANDBY or _IDLE to specify the mode and override the system setting.
How to use the callback function for Power and Reset
This is just a way of doing this stuff, feel free to change the code.
u8 HWButton = 0;
/**
* Callback for the reset button on the Wii.
*/
void WiiResetPressed()
{
HWButton = SYS_RETURNTOMENU;
}
/**
* Callback for the power button on the Wii.
*/
void WiiPowerPressed()
{
HWButton = SYS_POWEROFF_STANDBY;
}
/**
* Callback for the power button on the Wiimote.
* @param[in] chan The Wiimote that pressed the button
*/
void WiimotePowerPressed(s32 chan)
{
HWButton = SYS_POWEROFF_STANDBY;
}
/**
* Entry point.
* @param[in] argc The number of arguments invoked with the program
* @param[in] argv The array containing the arguments
* @return 0 on clean exit, an error code otherwise
*/
int main(int argc, char **argv)
{
// Initialization
SYS_SetResetCallback(WiiResetPressed);
SYS_SetPowerCallback(WiiPowerPressed);
WPAD_SetPowerButtonCallback(WiimotePowerPressed);
while(1)
{
// Do Stuff Here
if(HWButton)
break;
}
if(HWButton)
{
SYS_ResetSystem(HWButton, 0, 0);
}
return 0;
}