GP32 Gp32 Tutorial 01 "hello World!"


synkro

0xdeadbeef
Joined
Aug 26, 2003
Messages
823
Location
Germany
Website
Visit site
Code:
/*
 * GP32 Tutorial 01 "Hello World!"
 *
 * Intro
 * -----
 * You want to learn coding for the GP32 but have no clue where to start?
 * Then you found the right tutorials! These are for you when:
 *
 * - you have basic programming knowledge (from BASIC,...)
 * - you have already installed a compiler and Mr. Mirko's SDK
 *   (http://home.t-online.de/home/mirkoroller/gp32/)
 * - you have a proper development enviroment
 * - you can't learn from the examples and manual directly
 * - you need more than knowlegde about the SDK
 * - you need someone explaining the stuff to you
 * - can understand my bad english
 *
 * These tutorials will:
 *
 * - introduce you to C programming for the GP32
 * - explain how the GP32 works
 * - show basic techniques to handle the GP32
 * - NOT help you installing a compiler on your PC
 * - NOT guide you to a complete game engine
 * - NOT help you on makefiles or editor issues
 * - NOT show any ARM ASM or Thumb
 *
 * Why not using the official SDK? There are already some beginner tutorials
 * from rico covering this. Also Mr. Mirko's SDK will become a serious
 * replacement, it is minimalistic and fast.
 *
 * If you have any questions ask the dudes and dudettes at:
 * www.gp32x.de or #gp32dev (EFNET) they know everything and are willing
 * to help you anytime.
 * If you have any c/c, ideas or suggestions mail me: synkro(at)gmx(dot)net
 *
 * Let's start!
 * ------------
 * 1. Open this file in your fav editor and make sure it compiles properly
 *    without any errors or warnings. Try tp execute the FXE in GeePee32 or
 *    on your GP32. I will now explain the things in detail, some in the
 *    following paragraphs or directly in the code.
 *    You won't understand everything on the first go, maybe after some weeks
 *    but that's ok! Rome wasn't build on a single day or with one tutorial.
 *    Just read everything carefuly...
 *
 * 2. Every C program starts its execution with the "main" function. Every
 *    programm has to have one, that's the place the GP32 starts the execution.
 *    The first "void" means that this function won't return any value after
 *    execution. The second "void" in braces means that the "main" function
 *    needs no arguments on execution.
 *
 * 3. The following commands are in "{,}" These define commands together into
 *    a block.
 *
 * 4. unsigned short *framebuffer;
 *    This declares a pointer to the primary FRAMEBUFFER. A pointer is a
 *    memory cell which contains an address of a data element. The variable
 *    name is preceded by a '*' character. A framebuffer is a memory area
 *    treated like a screen or any other data element.
 *
 * 5. Every function starting with "gp_" is part of Mr. Mirko's SDK. Keep in
 *    in mind that C is case sensitive.
 *
 * 6. char text[23] = "Hello World! What else?";
 *    We can allocate arrays of simple types by adding a pair of square
 *    brackets enclosing a number which is the size of the array after the
 *    variable's name. This is an array of chars we fill with a short
 *    message also called string. This string has a length of 23.
 *
 * 7. int x_pos = 50;
 *    int (integer) is a data type that stores whole numbers. We declare a
 *    variable named 'x_pos' as an integer with the value 50.
 *
 * 8. while (1){}
 *    This is an endless loop to prevent the program to end before we can
 *    see anything on the screen. While the condition is true, in this case
 *    the condition is '1' also always true.
 *
 * So, that's it so far for this time. Try to re-type (not copy and paste)
 * the following code and try to understand what each line does. Before you
 * go over to the next chapter fool around with the code:
 * A) Add new lines of text
 * B) in different colors and on different positions.
 * C) Clear the screen in any other color
 *
 * Disclaimer
 * ----------
 * This Tutorial is based on Mr. Mirko's SDK (mirko@mirkoroller.de)
 * download it at: http://home.t-online.de/home/mirkoroller/gp32/
 * THIS TUTORIAL IS NOT WRITTEN BY MR. MIRKO! Parts of this Tutorial
 * are from the SDK's examples or manual.
 *
 *                                 SDK by Mr. Mirko mirko(at)mirkoroller(dot)de
 *                            Tutorial by synkro    synkro(at)gmx(dot)net
 *                                                  26.04.2004 14:09:49
 */


#include <gp32.h>            // Includes the SDK header with all definitions

unsigned short *framebuffer; // A framebuffer pointer

void main(void)
{

  framebuffer = (unsigned short*) FRAMEBUFFER;
  // We point the framebuffer pointer to a memory area used
  // for the screen. We manipulate the screen by manipulating
  // this framebuffer.

  gp_SetScreen(framebuffer, 16);
  // We set the framebuffer into 16bit mode. We will always use
  // the 16bit mode because I am to lazy to write about palette
  // handling and it is just the best screen mode.

  int i;
  for (i=0; i<320*240; i++) framebuffer[i]=0xFFFF;
  // Though the screen is two dimensional, it is possible
  // to access the framebuffer linear. All data in memory
  // is aligned the same way. In this loop we set every
  // pixel white (0xFFFF), we clear the screen.

  gp_SetCpuSpeed(66);
  // We set the CPU clock to 66MHz. It compromises battery
  // life and performance.

  char text[23] = "Hello World! What else?";
  // An array of chars also known as string.

  int x_pos   = 50;     // This int stores the x-position of the text
  int y_pos   = 110;
  int length  = 23;     // length of the char[] to be drawn
  int color   = 0xF000; // RGB (RRRRRGGGGGBBBBB0 = 16 bit)

  gp_SetFont8(x_pos, y_pos, length, text, color, framebuffer);
  // We draw a string named text with lenght of 23
  // at (x_pos, y_pos) on the framebuffer. This function
  // is part of the SDK.


  while (1) // This while loop ist always true.
  {
  }

} // End of the "main" function
 
Last edited by a moderator:

Mark

Boris Johnson: Cuddly Conservative
Joined
Dec 27, 2003
Messages
2,806
Location
Stoke-On-Trent UK
Website
www.gp-magazine.org.uk
These look nice.....I want to start learning, and only have a basic knowledge of programming (as in basic BASIC ;) ) and should find this useful, along with the others. Thanks.
 

Tobriand

Very Active Member
Joined
Dec 27, 2002
Messages
4,071
Age
36
Location
Croydon (UK)
Website
Visit site
This rocks. By which I mean there's almost nothing in it which I didn't get after thinking about it, whereas with Rico's tutorials, I found the Hello World example fine once inside all the framebuffer setting, screen wiping, and surface definitions, which I didn't understand the point of at all...

Only bit, if you could clarify...

int color = 0xF000; // RGB (RRRRRGGGGGBBBBB0 = 16 bit)

I thought all int values had to be numerical integers - so how can colour have the value 0xF000? Or does 0x specify that the rest of the value is in hex or something? (I've only done very very basic C++ before).

Also - stupid as it may seem, I'm not entirely sure I understand the difference between a pointer (e.g. "unsigned short *framebuffer") and simply an unsigned short - why couldn't you just omit the *, or could you indeed, but it makes reading the stuff easier?


Thanks so much for writing this - the muddy waters have cleared a bit. I might actually give Mr Mirko's SDK a try and fiddle with once I find a decent windows dev environment (wasn't there a nice one called minigp32 or something?) to set it up in :)
 

Daz_Genetic

Certified Guru
Joined
Oct 26, 2003
Messages
424
Age
44
Location
Maine, USA
Website
www.dazos.com
Tobriand posted on Mar 9 2004 at 09:01 PM said:
This rocks. By which I mean there's almost nothing in it which I didn't get after thinking about it, whereas with Rico's tutorials, I found the Hello World example fine once inside all the framebuffer setting, screen wiping, and surface definitions, which I didn't understand the point of at all...

Only bit, if you could clarify...

int color   = 0xF000; // RGB (RRRRRGGGGGBBBBB0 = 16 bit)

I thought all int values had to be numerical integers - so how can colour have the value 0xF000? Or does 0x specify that the rest of the value is in hex or something? (I've only done very very basic C++ before).

Also - stupid as it may seem, I'm not entirely sure I understand the difference between a pointer (e.g. "unsigned short *framebuffer") and simply an unsigned short - why couldn't you just omit the *, or could you indeed, but it makes reading the stuff easier?


Thanks so much for writing this - the muddy waters have cleared a bit. I might actually give Mr Mirko's SDK a try and fiddle with once I find a decent windows dev environment (wasn't there a nice one called minigp32 or something?) to set it up in :)
You are exactly right 0x defines that the following number is in Hex.

The * is the symbol for a pointer. Instead of a variable that stores the whole frambuffer, it is a pointer that stores the address of the start of where the framebuffer exists in memory. Understanding pointers and structures are the absolute key to being able to write games in my opinion.
 
Last edited by a moderator:

synkro

0xdeadbeef
Joined
Aug 26, 2003
Messages
823
Location
Germany
Website
Visit site
* An int is 2bytes = 16 bit long. 4 bits can be understanded like one hex digit, 0 - F (HEX) is the same as 0000 - 1111 (BIN) ans 0 - 15 (DEC). In hex it is a bit easier to pick the right color.

* I know pointers are a bit weird, I am focusing this as topic next. It is the main "thing" in C
 

Tobriand

Very Active Member
Joined
Dec 27, 2002
Messages
4,071
Age
36
Location
Croydon (UK)
Website
Visit site
Right... after fiddling for an hour or so trying to work out how to set up devkitadv with Mr Mirko's SDK, I've given up for the moment. For not, I'll just read and try to understand, since these tutorials are still ideal from my point of view :)
 

Charge

Member
Joined
May 26, 2003
Messages
206
Age
43
Location
Croydon, England
Tobriand posted on Mar 9 2004 at 09:01 PM said:
Also - stupid as it may seem at I'm not entirely sure I understand the difference between a pointer (e.g. "unsigned short *framebuffer") and simply an unsigned short - why couldn't you just omit the *, or could you indeed, but it makes reading the stuff easier?
Excellent tutorial syncro, great idea.

As for the pointer issue, its a pretty complicated topic so its probably best you read it up in a book but basically you can think about it like this -

A normal variable (like unsigned short framebuffer) holds the a value itself.

A pointer variable (like unsigned short *framebuffer) holds a number corresponding to a place in memory. You can then access (as in read or write) to it the place in memory with the following syntax :

*framebuffer = 10;

you can change the place in memory that the pointer is pointing to by assigning a number without using the star (*) before the variable. But DON'T do this if you don't know what you are doing - your app will probably crash as soon as you acess the new memory position!

A big benefit of using pointers is that you can access places in memory with array style syntax. For example :

*framebuffer = 10;

is the same as

framebuffer[0] = 10;

as you are acessing the first area of memory that the number stored in the variable framebuffer is pointing to. Thus in this example (note I haven't looked at mr.mirko's sdk so I may be wrong from now on...) it seems as though framebuffer is pointing to a big array that holds all of the pixels on the screen and is probably (320 * 240=) 76800 shorts in size. So you may be able to go:

framebuffer[x] = y;

to set a pixel colour on the screen (where x is the pixel number from 0 to 76799 and y is the pixel colour).

I may be wrong about some of this (and I know I basically skipped over an normal ie non-pointer variable).
 
Last edited by a moderator:

coRp

Still Fresh
Joined
Apr 15, 2004
Messages
40
Age
31
Location
North East England
Website
www.corp-com.tk
hrmm... C isn't that different in Macromedia Flash's Actionscript...
but I have no idea what the pointer thing is all about. overall, I just have no idea where to start when it comes to coding for GP32. and btw, what is an SDK?? :unsure:

no, seriously, i'm not joking...
 

Axeman

Axemeister
Joined
Apr 5, 2003
Messages
4,220
Location
Newport, Wales, UK
Website
Visit site

coRp

Still Fresh
Joined
Apr 15, 2004
Messages
40
Age
31
Location
North East England
Website
www.corp-com.tk
okay, thanks axeman - i'll have a look at those sites :)

also, I have just downloaded the Mirko SDK file 'SDK063.gz2' from his site. I unzipped it with winRAR, but the only file inside - 'SDK063' does not even seem to have a file ending, so I have no idea what to do with it. :blink:
 

Tobriand

Very Active Member
Joined
Dec 27, 2002
Messages
4,071
Age
36
Location
Croydon (UK)
Website
Visit site
*Finally* got Mr Mirko's SDK set up and combined with Devkitadv... now I can actually take a look at some of these tutorials.

First... to try and get a Hello world with a scrolling ^ underneath it...
 
Top