Skip to content
This repository has been archived by the owner on Sep 6, 2023. It is now read-only.

Example code

gbl08ma edited this page May 9, 2016 · 1 revision

This page includes a few examples of PicoC scripts written with the Prizm in mind. It also serves as a guide on how to use some of the most important Utilities Framework functions.

Hello World

This example simply displays "Hello World" inside a message box, plus the typical "Press EXIT" message, and waits for the user to press EXIT.

We will use this simple example to demonstrate the different ways to achieve our goal: using libfxcg functions alone, using functions from the Utilities Framework in place of libfxcg functions and, finally, using higher-level Utilities Framework helpers that simplify the code significantly.

Using libfxcg functions

#include <fxcg/display.h>
#include <fxcg/keyboard.h>

MsgBoxPush(3);
PrintXY(3, 3, "  Hello World", TEXT_MODE_NORMAL, TEXT_COLOR_BLACK);
PrintXY_2(TEXT_MODE_NORMAL, 1, 5, 2, TEXT_COLOR_BLACK); // press exit message
int key = 0;
while(key != 30002) { // KEY_CTRL_EXIT
  GetKey(&key);
}
MsgBoxPop();

Using Utilities Framework replacements

The keyboard handling function of the Utilities Framework is used, so combinations like Shift+Menu are dealt with automatically. The message box functions are replaced with ones that keep track of the number of open dialogs, so that pressing Shift+Exit properly frees their resources. You should use these wrappers for native functions where possible.

#include <fxcg/display.h>
#include <utilities/graphicsProvider.h>
#include <utilities/keyboardProvider.h>

mMsgBoxPush(3);
mPrintXY(3, 3, "Hello World", TEXT_MODE_NORMAL, TEXT_COLOR_BLACK);
PrintXY_2(TEXT_MODE_NORMAL, 1, 5, 2, TEXT_COLOR_BLACK); // press exit message
int key = 0;
while(key != 30002) { // KEY_CTRL_EXIT
  mGetKey(&key, 0);
}
mMsgBoxPop();

Using higher-level helpers

In this version of the example, we will simplify the code by using framework functions to cut the amount of lines and function calls, while still providing the same functionality:

#include <utilities/graphicsProvider.h>
#include <utilities/menuGUI.h>

mMsgBoxPush(3);
mPrintXY(3, 3, "Hello World", TEXT_MODE_NORMAL, TEXT_COLOR_BLACK);
// Close a message box of type "press exit" (0) with the message on line 5:
closeMsgBox(0, 5);

Simple menu

This example displays a menu with four items and then displays a message showing which one was selected.

#include <stdio.h>
#include <utilities/graphicsProvider.h>
#include <utilities/menuGUI.h>
#include <utilities/structInit.h>

// See comments on the following structs, as well as default values, in the Utilities headers
typedef struct
{
  char* text;
  int type;
  int value;
  int color;
  int isfolder;
  int isselected;
  int icon;
} MenuItem;

typedef struct {
  char* statusText;
  char* title;
  char* subtitle;
  int titleColor;
  char* nodatamsg;
  int startX;
  int startY;
  int width;
  int height;
  int scrollout;
  int numitems;
  int type;
  int selection;
  int scroll;
  int fkeypage;
  int numselitems;
  int returnOnInfiniteScrolling;
  int returnOnLeft;
  int returnOnRight;
  int darken;
  int miniMiniTitle;
  int pBaRtR;
  MenuItem* items;
} Menu;

#define MENU_RETURN_EXIT 0
#define MENU_RETURN_SELECTION 1

Menu m;
initMenu(&m); // initialize the struct to default values
m.scrollout = 1;
m.title = "Test menu";
MenuItem items[4];
int i;
for(i = 0; i < 4; i++) {
    // intialize each item to default values
    initMenuItem(&items[i]);
}

items[0].text = "First item";
items[1].text = "Second item";
items[2].text = "3rd item";
items[3].text = "4th item";
m.items = items;
m.numitems = 4;
int res;
while(1) {
    res = doMenu(&m, NULL);
    if(res == MENU_RETURN_SELECTION) {
      unsigned char text[20];
      sprintf(text, "Option %d:", m.selection);
      mMsgBoxPush(4);
      mPrintXY(3, 2, text, TEXT_MODE_NORMAL, TEXT_COLOR_BLACK);
      mPrintXY(3, 3, items[m.selection-1].text, TEXT_MODE_NORMAL, TEXT_COLOR_BLACK);
      closeMsgBox(0, 5);
    } else break;
}