Introduction

Day 1
GBA Hardware

Day 2
"Hello, World!"

Day 3
Input

Day 4
Backgrounds -
  Bitmapped Modes

Day 5
Sprites

Day 6
Backgrounds -
  Tile Modes

Day 7
Project 1 -
  Tetris

Quiz - Week 1

Day 8
Sprites #2 -
  Animation

Day 9
Maps

Day 10
Sprites #3 -
  Animation #2

Day 11
Backgrounds -
  Rotation

Day 12
Sprites #4 -
  Mosaic

Version History

Downloads

Forums

Books

Links

Graphics FAQ

GFX2GBA Readme

Translations

Games

Projects

Credits

Support

Poll

HAM Tutorial :: Project 3 :: Slide Show

 

Someone emailed me asking for a way to do a simple slideshow. I came up with the following code. As usual, there are quite a few different ways this could be done. I chose what I hope will be the easiest to understand.

Don't know how to setup the graphics for this one?
gfx2gba -D -fsrc -pbg.pal -t1 bg.bmp

NOTE: I decided to make a separate palette for each image. You'll have to change bg.pal and bg.bmp to the other pictures and run this again for each one.

// The Main HAM Library
#include "mygba.h"

// Graphics Includes - for each image there is a
// separate palette

#include "gfx/bg.pal.c"
#include "gfx/bg.raw.c"
#include "gfx/bubbles.pal.c"
#include "gfx/bubbles.raw.c"
#include "gfx/paradise.pal.c"
#include "gfx/paradise.raw.c"
#include "gfx/water.pal.c"
#include "gfx/water.raw.c"

// Function Prototypes
void show_pic();

// Global Variables
u8 which_pic = 0; // 0 through 3
u8 update_screen = 0; // Does the screen need updating
u8 key_pressed = 0; // Was a button just pressed


// Main function
int main()
{
  // Initialize HAMlib
  ham_Init();

  // Setup the background mode
  ham_SetBgMode(4);

  // Display the first picture before we check for input

  // DMA the background picture
  TOOL_DMA1_SET(&bg_Bitmap,
                              MEM_BG_PTR,
                              SIZEOF_32BIT(bg_Bitmap),
                              DMA_TRANSFER_32BIT,
                              DMA_STARTAT_NOW)

  // Initialize the background palette
  ham_LoadBGPal(&bg_Palette,SIZEOF_16BIT(bg_Palette));

  // Infinite loop to keep the program running
  while(1)
  {
    // First, determine if left or right was pressed

    // if the user presses right, and the right key was not
    // just pressed, setup the next picture to be displayed

    if((!key_pressed) && (F_CTRLINPUT_RIGHT_PRESSED)) {
        update_screen = 1;
        key_pressed = 1;
        if (which_pic < 3) {
            which_pic++;
        } else {
            which_pic = 0;
        }
    // Same here, but left and decreasing
    } else if((!key_pressed) && (F_CTRLINPUT_LEFT_PRESSED)) {
        update_screen = 1;
        key_pressed = 1;
        if (which_pic > 0) {
            which_pic--;
        } else {
            which_pic = 3;
        }
    // If the buttons are released, update key_pressed
    } else if ((!F_CTRLINPUT_RIGHT_PRESSED) &&
                  (!F_CTRLINPUT_LEFT_PRESSED)) {
        key_pressed = 0;
    }

    // See if the screen needs to be updated
    if (update_screen) {
        show_pic();
        update_screen = 0;
    }
  } // End of while(1)

  return 0;
} // End of main()


// Draw the Start Screen
void show_pic()
{
  if (which_pic == 0) {
      // DMA the background picture
      TOOL_DMA1_SET(&bg_Bitmap,
                                  MEM_BG_PTR,
                                  SIZEOF_32BIT(bg_Bitmap),
                                  DMA_TRANSFER_32BIT,
                                  DMA_STARTAT_NOW)

      // Initialize the background palette
      ham_LoadBGPal(&bg_Palette,SIZEOF_16BIT(bg_Palette));
  } else if(which_pic == 1) {
      // DMA the background picture
      TOOL_DMA1_SET(&bubbles_Bitmap,
                                  MEM_BG_PTR,
                                  SIZEOF_32BIT(bubbles_Bitmap),
                                  DMA_TRANSFER_32BIT,
                                  DMA_STARTAT_NOW)

      // Initialize the background palette
      ham_LoadBGPal(&bubbles_Palette,SIZEOF_16BIT(bubbles_Palette));
  } else if(which_pic == 2) {
      // DMA the background picture
      TOOL_DMA1_SET(&paradise_Bitmap,
                                  MEM_BG_PTR,
                                  SIZEOF_32BIT(paradise_Bitmap),
                                  DMA_TRANSFER_32BIT,
                                  DMA_STARTAT_NOW)

      // Initialize the background palette
      ham_LoadBGPal(&paradise_Palette,SIZEOF_16BIT(paradise_Palette));
  } else if(which_pic == 3) {
      // DMA the background picture
      TOOL_DMA1_SET(&water_Bitmap,
                                  MEM_BG_PTR,
                                  SIZEOF_32BIT(water_Bitmap),
                                  DMA_TRANSFER_32BIT,
                                  DMA_STARTAT_NOW)

      // Initialize the background palette
      ham_LoadBGPal(&water_Palette,SIZEOF_16BIT(water_Palette));
  }

  return;
} // End of show_pic()

 

Code Explanation

// Global Variables
u8 which_pic = 0; // 0 through 3
This keeps track of the current image. For this demo there are only 4 pictures (numbered 0 - 3, of course.)
u8 update_screen = 0; // Does the screen need updating
This keeps track of whether the screen should be updated.
u8 key_pressed = 0; // Was a button just pressed
This is necessary so that when the user presses a direction, it only registers once. If you hold down a button, it will fly through the number of the picture to be displayed and skip them.

Now on to main()

Everything before the while(1) should make complete sense to you. If not, get back to the regular tutorials and come back later!

// if the user presses right, and the right key was not
// just pressed, setup the next picture to be displayed
if((!key_pressed) && (F_CTRLINPUT_RIGHT_PRESSED)) {
Make sure that the user isn't holding the button down (see above for more details) and that they pressed to the right.
update_screen = 1;
It's time to show the next picture.
key_pressed = 1;
This is how we'll make sure the d-pad gets released before we check again for input (see the final else below).
if (which_pic < 3) {
  which_pic++;
} else {
  which_pic = 0;
}
When the user presses to the right, we'll increment the picture. Since there are only 4 pictures in this example, if which_pic == 3, then we'll reset back to the first picture (number 0).


} else if((!key_pressed) && (F_CTRLINPUT_LEFT_PRESSED)) { ...
This is the same as the previous code except we are checking for left to be pressed, and if so, we decrement the picture to be displayed.

 // if the buttons are released, update key_pressed
} else if ((!F_CTRLINPUT_RIGHT_PRESSED) &&
              (!F_CTRLINPUT_LEFT_PRESSED)) {
    key_pressed = 0;
}

This is a very important step. We must make sure that all buttons have been released before we go on to the next set of checks. Otherwise, this program will be very choppy and skip through the pictures.

// See if the screen needs to be updated
if (update_screen) {
  show_pic();
  update_screen = 0;
}
Again, nothing difficult here. If the user pressed a button, then it's time to show a new picture. Once show_pic() has been called, reset update_screen to 0.

As for the show_pic() function, this is maybe not the cleanest ways to do it, but for now, it's one of the simplest.

That's it. Grab your own pics and put together a slide show for your family and friends!

 

Download Code

NOTE: You may need to Right-click and choose Save As.

HAM Version 2.40 And Higher
Necessary for all HAM programs: makefile
Main source file: main.c
Compiled binary: hello.gba

Download All Files In One Zip: Proj3_Slide_Show.zip

HAM Version 2.30 And Lower
Necessary for all HAM programs: makefile
Main source file: main.c
Compiled binary: hello.gba

Download All Files In One Zip: Proj3_Slide_Show.zip

View Demo Now

Discuss Project 3

 

<<

HOME

>>