Line 67:
Line 67:
== Your First Image ==
== Your First Image ==
+
+
Images are created using the GuiImage class. Images can either be loaded from the sd card, or from a buffer. There are quite a few things you will have to do, though, before you can display your image. Open demo.cpp from the libwiigui template.
+
+
First, we must initialize the video hardware, by using the function from video.cpp:
+
<source lang = "cpp">
+
InitVideo();
+
</source>
+
If you are going to be loading images from the sd card (or using the sd card for any other reason), you will need to initialize it:
+
<source lang="cpp">
+
fatInitDefault();
+
</source>
+
Next, you will need to start the Gui updating thread:
+
<source lang = "cpp">
+
InitGUIThreads();
+
</source>
+
This is what writes any changes you made to the screen. Last, you would call the function that starts your main menu. In the demo, it is:
+
<source lang="cpp">
+
MainMenu(MENU_SETTINGS);
+
</source>
+
but it can be whatever you named your menu function.
+
+
Now, open menu.cpp. Find the MainMenu function. To load an image, you declare an instance of the GuiImage class. Unless, in the main window, you will want to declare it this way:
+
<source lang="cpp">
+
GuiImage image(''ImageData'');
+
</source>
+
What is the ImageData inside the initializer function? Well before actually creating the image, you must load your image. This can be done by creating an instance of the GuiImageData class:
+
<source lang="cpp">
+
GuiImageData imagedata(''image_source'');
+
</source>
+
The image source is your image buffer, where the contents of the image are help. The image must be in the png format, and have dimensions that are a multiple of 4. To load an image from an sd card to an image buffer, do the following:
+
<source lang="cpp">
+
FILE *fp = fopen("''Path to image on sd card''","r");
+
''todo''
+
</source>
+
You may be wondering how an image gets loaded into a buffer. Whenever you want to add a new image, you must copy the image to the source/images folder, and add it to filelist.h:
+
<source lang = "cpp">
+
extern const u8 ''image_name_without_.png''_png[];
+
+
extern const u32 ''image_name_without_.png''_size;
+
</source>
+
Then your image will be put into the ''image_name_without_.png''_png buffer when it is compiled.
+
+
Last, you must append your new image to the GuiWindow you are dealing with. Before doing do, you need to suspend the gui thread, so that you are not making changes to variables while the thread is reading them, and resume the thread after appending:
+
<source lang = "cpp">
+
HaltGui();
+
''GuiWindow''.Append(&''image'');
+
ResumeGui();
+
</source>
+
Unless the image is declared as a pointer, you must have the & before it to pass the address of your GuiImage to the Append function.
+
+
So that's all it takes to load an image. Easy, right? Here's an example of the complete proccess:
+
+
''main.cpp:''
+
<source lang="cpp">
+
#include <gccore.h>
+
#include <stdio.h>
+
#include <stdlib.h>
+
#include <ogcsys.h>
+
#include <unistd.h>
+
+
+
+
+
#include "FreeTypeGX.h"
+
+
#include "video.h"
+
+
#include "audio.h"
+
+
#include "menu.h"
+
+
#include "input.h"
+
+
#include "filelist.h"
+
+
#include "demo.h"
+
+
+
+
+
+
+
int ExitRequested = 0;
+
+
+
+
+
+
void initall(){
+
+
InitVideo(); // Initialise video
+
+
+
+
+
InitGUIThreads();
+
+
+
+
+
fatInitDefault();
+
+
+
+
+
}
+
+
+
+
void ExitApp()
+
+
{
+
+
ShutoffRumble();
+
+
StopGX();
+
+
exit(0);
+
+
}
+
+
+
+
//---------------------------------------------------------------------------------
+
+
int main(int argc, char **argv) {
+
+
+
+
+
+
initall();
+
+
+
+
MainMenu(MAIN_SCREEN);
+
+
+
+
}
+
+
+
</source>
+
+
''menu.cpp:''
+
<source lang="cpp">
+
#include <gccore.h>
+
#include <ogcsys.h>
+
#include <stdio.h>
+
#include <unistd.h>
+
#include <stdlib.h>
+
#include <string.h>
+
#include <wiiuse/wpad.h>
+
+
#include "libwiigui/gui.h"
+
#include "menu.h"
+
#include "demo.h"
+
#include "input.h"
+
#include "filelist.h"
+
+
static GuiImageData * pointer[4];
+
static GuiImage * bgImg = NULL;
+
static GuiSound * bgMusic = NULL;
+
static GuiWindow * mainWindow = NULL;
+
static lwp_t guithread = LWP_THREAD_NULL;
+
static bool guiHalt = true;
+
+
+
int MenuDefault(){
+
+
int menu=MENU_DEFAULT;
+
GuiImageData image_data(image_png);
+
GuiImage image(&image_data);
+
HaltGui();
+
mainWindow->Append(&image);
+
ResumeGui();
+
while(menu==MENU_DEFAULT){
+
VIDEO_WaitVSync();
+
}
+
}
+
+
MainMenu(){
+
void MainMenu(int menu)
+
{
+
int currentMenu = menu;
+
+
#ifdef HW_RVL
+
pointer[0] = new GuiImageData(player1_point_png);
+
pointer[1] = new GuiImageData(player2_point_png);
+
pointer[2] = new GuiImageData(player3_point_png);
+
pointer[3] = new GuiImageData(player4_point_png);
+
#endif
+
+
mainWindow = new GuiWindow(screenwidth, screenheight);
+
+
bgImg = new GuiImage(screenwidth, screenheight, (GXColor){50, 50, 50, 255});
+
bgImg->ColorStripe(30);
+
mainWindow->Append(bgImg);
+
+
GuiTrigger trigA;
+
trigA.SetSimpleTrigger(-1, WPAD_BUTTON_A | WPAD_CLASSIC_BUTTON_A, PAD_BUTTON_A);
+
+
ResumeGui();
+
+
bgMusic = new GuiSound(bg_music_ogg, bg_music_ogg_size, SOUND_OGG);
+
bgMusic->SetVolume(50);
+
bgMusic->Play(); // startup music
+
+
while(currentMenu != MENU_EXIT)
+
{
+
switch (currentMenu)
+
{
+
case MENU_DEFAULT:
+
currentMenu = MenuDefault();
+
break;
+
default: // unrecognized menu
+
currentMenu = MenuDefault();
+
break;
+
}
+
}
+
+
ResumeGui();
+
ExitRequested = 1;
+
while(1) usleep(50);
+
+
HaltGui();
+
+
bgMusic->Stop();
+
delete bgMusic;
+
delete bgImg;
+
delete mainWindow;
+
+
delete pointer[0];
+
delete pointer[1];
+
delete pointer[2];
+
delete pointer[3];
+
+
mainWindow = NULL;
+
}
+
}
+
</source>
+
+
If you want any other menu names (MENU_DEFAULT), you can define them in menu.h:
+
+
''menu.h''
+
<source lang="cpp">
+
#ifndef _MENU_H_
+
#define _MENU_H_
+
+
#include <ogcsys.h>
+
+
void InitGUIThreads();
+
void MainMenu (int menuitem);
+
+
enum
+
{
+
MENU_EXIT = -1,
+
MENU_NONE,
+
MENU_SETTINGS,
+
MENU_SETTINGS_FILE,
+
'MENU_DEFAULT'
+
};
+
+
#endif
+
</source>