Line 1,164:
Line 1,164:
- [[user:WiiPhlex|WiiPhlex]]
- [[user:WiiPhlex|WiiPhlex]]
+
+
== EXAMPLE PROGRAMS ==
+
+
'''Lesson 1: Setting up Libwiisprite for use.'''
+
+
Description: This is the same code encountered in chapter 1, to build, make sure to have to include the correct lib’s (as shown in chapter 1) in your make file. For a template make file, refer to lesson 1.
+
+
'''Lesson 2: Setting up the Video display. '''
+
+
Description: basic template for using Libwiisprite, in order to build make sure you have included the correct lib’s as stated above. The following program will set up the wii and render the background with the colour white.
+
+
main.cpp
+
+
<source lang = "cpp">
+
#include <wiiuse/wpad.h>
+
#include <wiisprite.h>
+
+
// libwiisprite uses wsp as it's namespace
+
using namespace wsp;
+
+
int main(int argc, char **argv)
+
{
+
// Create the game window and initalise the VIDEO subsystem
+
GameWindow gwd;
+
gwd.InitVideo();
+
+
gwd.SetBackground((GXColor){ 255, 255, 255, 255 });
+
+
// Initialise Wiimote
+
WPAD_Init();
+
+
for(;;)
+
{
+
WPAD_ScanPads();
+
if(WPAD_ButtonsDown(WPAD_CHAN_0)&WPAD_BUTTON_HOME)
+
break;
+
gwd.Flush();
+
}
+
return 0;
+
}
+
</source>
+
+
'''Lesson 3: Loading and printing images.'''
+
+
Description: a simple program that loads an image from SD card and prints it to the screen, see Lesson3 for more information.
+
+
<source lang = "cpp">
+
#include <stdio.h>
+
#include <stdlib.h>
+
#include <gccore.h>
+
#include <wiiuse/wpad.h>
+
#include <fat.h>
+
+
#include <wiisprite.h>
+
using namespace wsp;
+
+
GameWindow gwd; // Initializes and renders our scene.
+
Sprite sprite; // The drawable object we can modify.
+
Image image; // Holds our image/texture from the SD Card.
+
+
int main(int argc, char **argv) {
+
// Initialize filesystem to load from SD Card
+
fatInitDefault();
+
+
gwd.InitVideo();
+
+
LayerManager manager(1);
+
+
if(image.LoadImage("libwiisprite.png") != IMG_LOAD_ERROR_NONE)exit(0);
+
sprite.SetImage(&image);
+
sprite.SetPosition(0, 0);
+
+
manager.Append(&sprite); // Gets drawn the closest.
+
+
WPAD_Init();
+
WPAD_SetDataFormat(WPAD_CHAN_0, WPAD_FMT_BTNS_ACC_IR);
+
+
while(1) {
+
+
WPAD_ScanPads();
+
u32 pressed = WPAD_ButtonsHeld(WPAD_CHAN_0);
+
+
// Draw everything what's in the manager.
+
manager.Draw(0, 0);
+
gwd.Flush();
+
}
+
+
manager.Draw(0, 0);
+
gwd.Flush();
+
+
return 0;
+
}
+
</source>
+
+
'''Lesson 4: Basic Image Manipulation '''
+
+
Description: Shows some examples of manipulation images with some of the basic public functions in Libwiisprite, this was taken from sprite.cpp in the Libwiisprite examples release. This assumes you have two images on the SD card called libwiisprite.png and libwiisprite2.png..
+
+
<source lang = "cpp">
+
#include <stdio.h>
+
#include <stdlib.h>
+
#include <gccore.h>
+
#include <wiiuse/wpad.h>
+
#include <fat.h>
+
+
#include <wiisprite.h>
+
using namespace wsp;
+
+
GameWindow gwd; // Initializes and renders our scene.
+
Sprite sprite; // The drawable object we can modify.
+
Sprite sprite2; // Another drawable object we can modify.
+
Quad quad; // A drawable rectangle for fading in and out.
+
Image image; // Holds our image/texture from the SD Card.
+
Image image2; // Holds our buffer image/texture.
+
+
bool calc_fade(u8 fade);
+
u8 fadedata = 0xff;
+
+
int main(int argc, char **argv) {
+
fatInitDefault();
+
+
gwd.InitVideo();
+
+
LayerManager manager(3);
+
+
if(image.LoadImage("libwiisprite.png") != IMG_LOAD_ERROR_NONE)exit(0);
+
if(image2.LoadImage("libwiisprite2.png") != IMG_LOAD_ERROR_NONE)exit(0);
+
+
sprite.SetImage(&image);
+
sprite2.SetImage(&image2);
+
+
sprite.SetPosition(0, 0);
+
+
sprite2.SetRefPixelPositioning(REFPIXEL_POS_PIXEL);
+
sprite2.SetPosition(320, 240);
+
+
quad.SetPosition(-40, -40);
+
quad.SetWidth(800);
+
quad.SetHeight(600);
+
+
manager.Append(&quad);
+
manager.Append(&sprite);
+
manager.Append(&sprite2);
+
+
WPAD_Init();
+
WPAD_SetDataFormat(WPAD_CHAN_0, WPAD_FMT_BTNS_ACC_IR);
+
+
u8 fading = 1;
+
while(1) {
+
+
WPAD_ScanPads();
+
u32 pressed = WPAD_ButtonsHeld(WPAD_CHAN_0);
+
+
if(pressed & WPAD_BUTTON_HOME)fading = 2;
+
+
if(calc_fade(fading))break;
+
if(fadedata == 0x00)fading = 0;
+
+
if(pressed & WPAD_BUTTON_MINUS)
+
sprite.SetZoom(sprite.GetZoom()-0.1f);
+
if(pressed & WPAD_BUTTON_PLUS)
+
sprite.SetZoom(sprite.GetZoom()+0.1f);
+
+
if(pressed & WPAD_BUTTON_A && sprite.GetTransparency() < 0xff-4)
+
sprite.SetTransparency(sprite.GetTransparency()+5);
+
if(pressed & WPAD_BUTTON_B && sprite.GetTransparency() > 4){
+
sprite.SetTransparency(sprite.GetTransparency()-5);
+
}
+
+
if(pressed & WPAD_BUTTON_UP)
+
sprite2.SetStretchHeight(sprite2.GetStretchHeight()+0.1f);
+
if(pressed & WPAD_BUTTON_DOWN)
+
sprite2.SetStretchHeight(sprite2.GetStretchHeight()-0.1f);
+
if(pressed & WPAD_BUTTON_RIGHT)
+
sprite2.SetStretchWidth(sprite2.GetStretchWidth()+0.1f);
+
if(pressed & WPAD_BUTTON_LEFT)
+
sprite2.SetStretchWidth(sprite2.GetStretchWidth()-0.1f);
+
+
ir_t ir;
+
WPAD_IR(WPAD_CHAN_0, &ir);
+
sprite.SetPosition(ir.sx-WSP_POINTER_CORRECTION_X, ir.sy-WSP_POINTER_CORRECTION_Y);
+
sprite.Move(-((f32)sprite.GetWidth()/2), -((f32)sprite.GetHeight()/2));
+
sprite.SetRotation(ir.angle/2);
+
+
manager.Draw(0, 0);
+
+
gwd.Flush();
+
}
+
+
manager.Draw(0, 0);
+
gwd.Flush();
+
+
return 0;
+
}
+
+
bool calc_fade(u8 fade){
+
+
if(fade == 1){ // Fading in
+
if(fadedata < 0x10)fadedata = 0x10;
+
fadedata -= 0x10;
+
}else if(fade == 2){ //Fading out
+
if(0xef < fadedata)fadedata = 0xef;
+
fadedata += 0x10;
+
}
+
+
quad.SetFillColor((GXColor){0x00, 0x00, 0x00, fadedata});
+
+
if(fade == 2 && fadedata == 0xff)return true;
+
+
return false;
+
}
+
</source.
+
+
'''Lesson 5: Layer Management.'''
+
+
Description: A small program that shows how to use function in the LayerManager class. By then end of the series of calls, Quad is at location 0 and sprite is at 1.
+
+
<source lang = "cpp">
+
#include <stdio.h>
+
#include <stdlib.h>
+
#include <gccore.h>
+
#include <wiiuse/wpad.h>
+
#include <fat.h>
+
+
#include <wiisprite.h>
+
using namespace wsp;
+
+
GameWindow gwd; // Initializes and renders our scene.
+
Sprite sprite; // The drawable object we can modify.
+
Quad quad; // drawable quad object
+
Image image; // Holds our image/texture from the SD Card.
+
+
int main(int argc, char **argv) {
+
+
fatInitDefault();
+
+
gwd.InitVideo();
+
+
LayerManager manager(2);
+
+
if(image.LoadImage("libwiisprite.png") != IMG_LOAD_ERROR_NONE)exit(0);
+
sprite.SetImage(&image);
+
sprite.SetPosition(0, 0);
+
+
quad.SetPosition(-40, -40);
+
quad.SetWidth(800);
+
quad.SetHeight(600);
+
+
//action starts here
+
manager.Append(&sprite);
+
manager.Append(&quad);
+
+
u32 sizeOfList = manager.GetSize();
+
+
manager.RemoveAll();
+
+
manager.Append(&sprite);
+
manager.Insert(&quad, 0);
+
// and ends here
+
+
WPAD_Init();
+
WPAD_SetDataFormat(WPAD_CHAN_0, WPAD_FMT_BTNS_ACC_IR);
+
+
while(1) {
+
+
WPAD_ScanPads();
+
u32 pressed = WPAD_ButtonsHeld(WPAD_CHAN_0);
+
+
// Draw everything what's in the manager.
+
manager.Draw(0, 0);
+
gwd.Flush();
+
}
+
+
manager.Draw(0, 0);
+
gwd.Flush();
+
+
return 0;
+
}
+
</source>
+
+
'''Lesson 6: Quad.'''
+
+
Description: Shows examples of how you might call functions in the quad class as well as how to create one.
+
+
<source lang = "cpp">
+
#include <stdio.h>
+
#include <stdlib.h>
+
#include <gccore.h>
+
#include <wiiuse/wpad.h>
+
#include <fat.h>
+
+
#include <wiisprite.h>
+
using namespace wsp;
+
+
GameWindow gwd; // Initializes and renders our scene.
+
Quad quad; // drawable quad object
+
Quad quad2; // another quad object
+
+
int main(int argc, char **argv) {
+
+
gwd.InitVideo();
+
+
LayerManager manager(2);
+
+
quad.SetPosition(-40, -40);
+
quad.SetWidth(800);
+
quad.SetHeight(600);
+
+
quad2.SetPosition (200, 200);
+
quad2.Setwidth(50);
+
quad2.SetHeight(50);
+
+
quad2.SetBorder(true);
+
quad2.SetBorderWidth(10);
+
quad2.SetBorderColor((GXColor) {0xFF, 0xFF, 0xFF, 0xFF});
+
+
quad2.SetFillColor((GXColor) {0x00, 0x00, 0x00, 0x00});
+
+
manager.Append(&quad);
+
manager.Append(&quad2);
+
+
WPAD_Init();
+
WPAD_SetDataFormat(WPAD_CHAN_0, WPAD_FMT_BTNS_ACC_IR);
+
+
while(1) {
+
+
WPAD_ScanPads();
+
u32 pressed = WPAD_ButtonsHeld(WPAD_CHAN_0);
+
+
// Draw everything what's in the manager.
+
manager.Draw(0, 0);
+
gwd.Flush();
+
}
+
+
manager.Draw(0, 0);
+
gwd.Flush();
+
+
return 0;
+
}</source>
+
+
'''Lesson 7: Basic Animation'''
+
None Yet, if you have a sample to submit for this section email me at phlex.wii@gmail.com
+
+
'''Lesson 8: Collision Detection'''
+
+
Description: Shows how to test for collision between various objects, does not contain an example for node based collision detection (yet).
+
+
<source lang = "cpp">
+
#include <stdio.h>
+
#include <stdlib.h>
+
#include <gccore.h>
+
#include <wiiuse/wpad.h>
+
#include <fat.h>
+
+
#include <wiisprite.h>
+
using namespace wsp;
+
+
GameWindow gwd; // Initializes and renders our scene.
+
+
Sprite sprite; // The drawable object we can modify.
+
Sprite sprite2; // another sprite to check collision against.
+
+
Image image; // Holds our image/texture from the SD Card.
+
Image image2; // hold the other image/tecture from SD card.
+
+
Rectangle rect; // rectangle object as an example.
+
Quad quad; //quad object for rect.
+
+
struct player {
+
+
u32 xpos, ypos
+
+
} Player;
+
+
void checkCollision(u32 oldX, u32 oldY);
+
+
int main(int argc, char **argv) {
+
+
fatInitDefault();
+
+
gwd.InitVideo();
+
+
LayerManager manager(3);
+
+
if(image.LoadImage("libwiisprite.png") != IMG_LOAD_ERROR_NONE)exit(0);
+
if(image.LoadImage("libwiisprite2.png") != IMG_LOAD_ERROR_NONE)exit(0);
+
+
sprite.SetImage(&image);
+
sprite.SetPosition(Player.xpos, Player.ypos);
+
+
sprite2.SetImage(&image2);
+
sprite2.SetPosition(200,200);
+
+
manager.Append(&sprite);
+
manager.Append(&Rectangle);
+
+
rect.width = 20; rect.height = 20;
+
rect.x = 50; rect.y = 50;
+
+
quad.SetRectangle(&rect);
+
+
WPAD_Init();
+
WPAD_SetDataFormat(WPAD_CHAN_0, WPAD_FMT_BTNS_ACC_IR);
+
+
while(1) {
+
u32 oldX = Player.xpos, oldY = Player.ypos;
+
+
WPAD_ScanPads();
+
u32 pressed = WPAD_ButtonsHeld(WPAD_CHAN_0);
+
+
if(pressed & WPAD_BUTTON_UP)
+
Player.ypos -= 1;
+
+
if(pressed & WPAD_BUTTON_DOWN)
+
Player.ypos += 1;
+
+
if(pressed & WPAD_BUTTON_LEFT)
+
Player.xpos -= 1;
+
+
if(pressed & WPAD_BUTTON_RIGHT)
+
Player.xpos += 1;
+
+
sprite.SetPosition(Player.xpos, Player.ypos);
+
+
checkCollision();
+
+
sprite.SetPosition(Player.xpos, Player.ypos);
+
+
manager.Draw(0, 0);
+
gwd.Flush();
+
}
+
+
manager.Draw(0, 0);
+
gwd.Flush();
+
+
return 0;
+
}
+
+
void checkCollision(u32 oldX, u32 oldY)
+
{
+
if (sprite.CollidesWith(&sprite2)) {
+
Player.x = oldX;
+
Player.y = oldY;
+
}
+
+
if (sprite.CollidesWith(&quad)) {
+
Player.x = oldX;
+
Player.y = oldY;
+
}
+
+
}</source>
+
+
'''Lesson 9: Tiled Layers.'''
+
None Yet, if you have a sample to submit for this section email me at phlex.wii@gmail.com
+
+
'''Lesson 10: Pixel Referencing and Offset.'''
+
None Yet, if you have a sample to submit for this section email me at phlex.wii@gmail.com