Hello World In C++ For Gp2x Xp Xp


Joined
Nov 21, 2008
Messages
259
Could sb post an example ( easy example of course ) of code in C++ for this platform ( gp2x ).
 
console example:

CODE

#include <stdio.h>

int main(void)
{
printf ("Hello World\n");
}



or a graphical example using minimal lib (displays "Hello World" on the LCD)

CODE

#include "minimal.h"

int main(void)
{
gp2x_init (1000, 16, 44100, 16, 1, 60, 1);
gp2x_printf(NULL, 0, 0, "Hello World");
gp2x_video_RGB_flip(0);
return 0;
}
 
CODE

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <SDL.h>

/* GP2X button mapping */
enum MAP_KEY
{
VK_UP , // 0
VK_UP_LEFT , // 1
VK_LEFT , // 2
VK_DOWN_LEFT , // 3
VK_DOWN , // 4
VK_DOWN_RIGHT , // 5
VK_RIGHT , // 6
VK_UP_RIGHT , // 7
VK_START , // 8
VK_SELECT , // 9
VK_FL , // 10
VK_FR , // 11
VK_FA , // 12
VK_FB , // 13
VK_FX , // 14
VK_FY , // 15
VK_VOL_UP , // 16
VK_VOL_DOWN , // 17
VK_TAT // 18
};

/* The screen surface, joystick device */
SDL_Surface *screen = NULL;
SDL_Joystick *joy = NULL;

void Terminate(void)
{
SDL_Quit();
#ifdef GP2X
chdir("/usr/gp2x");
execl("/usr/gp2x/gp2xmenu", "/usr/gp2x/gp2xmenu", NULL);
#endif
}

int main (int argc, char *argv[])
{
int done;

/* Initialize SDL */
if (SDL_Init (SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) < 0) {
fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ());
exit (1);
}
atexit (Terminate);

SDL_ShowCursor(SDL_DISABLE);

/* Set 320x240 16-bits video mode */
screen = SDL_SetVideoMode (320, 240, 16, SDL_SWSURFACE);
if (screen == NULL) {
fprintf (stderr, "Couldn't set 320x240x16 video mode: %s\n", SDL_GetError ());
exit (2);
}

/* Check and open joystick device */
if (SDL_NumJoysticks() > 0) {
joy = SDL_JoystickOpen(0);
if(!joy) {
fprintf (stderr, "Couldn't open joystick 0: %s\n", SDL_GetError ());
}
}

#ifdef GP2X
/* Only use GP2X code here */
#endif

#ifdef WIN32
/* Only use Windows code here */
#endif

done = 0;
while (!done)
{
SDL_Event event;

/* Check for events */
while (SDL_PollEvent (&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
/* if press Ctrl + C, terminate program */
if ( (event.key.keysym.sym == SDLK_c) && (event.key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) )
done = 1;
break;
case SDL_KEYUP:
break;
case SDL_JOYBUTTONDOWN:
/* if press Start button, terminate program */
if ( event.jbutton.button == VK_START )
done = 1;
break;
case SDL_JOYBUTTONUP:
break;
case SDL_QUIT:
done = 1;
break;
default:
break;
}
}

/* Processing */
}

return 0;
}
 
cout is object orientated. For example, you can give any variable type to cout (It's upto the class to specify to format the text appropriately), where as printf is specific in what it supports. Therefore cout is much more flexible.

In practice however, it's rare for people to use cout, as typically people are happy with printf's limited range.

eg:
TDateTime td = Now();

cout <<"Time is " << td << endl;
printf ("Time is %s\n", DateTimeToAscii(td));
 
I rather cout I was making a experiment and the results were that the cout is 2 x slower

Fot dev C++

ups sory it's in Polish ( mb I translate it sometime )

CODE

#include <iostream>
#include <time.h>
#include <conio.h>

using namespace std;

int main()
{
int i=0,timefirst,timesecond,timefree;
bool Mil=true;
time_t start,end,start2,end2,start3,end3;

time (&start);

while(Mil)
{
i++;
printf("%d\n" , i);
if(i==1000000)
{
Mil=false;
}
}

time(&end);
timefirst=difftime(end,start);

Mil=true;
i=0;

time (&start2);

while(Mil)
{
i++;
cout<<i<<"\n";
if(i==1000000)
{
Mil=false;
}
}

time(&end2);
timesecond=difftime(end2,start2);

Mil=true;
i=0;

time (&start3);

while(Mil)
{
i++;
if(i==1000000)
{
Mil=false;
}
}

time(&end3);
timefree=difftime(end3,start3);

cout<<"\n\nTime in wich the loop is done to 1 m: "<<timefree<<"\nTime with printf: "<<timefirst<<"\nTime with cout: "<<timesecond<<endl;
getch();

}



Transleted

The whole test takes about 2 min so be patient

Results in seconds
 
Back
Top