the_goulin
31/05/2006, 21:48
Me alegro que te sirva de ayuda hombre ^_^ de eso se trata, pues nada aqui te dejo más codigo implementando el stick de la GP2X
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL.h>
#ifdef GP2X
#include <unistd.h>
#endif
#define TICK_INTERVAL 200
#define _snakeMaxTama 200 // tamaño maximo del array
#define mapa_w 30 // mapa ancho
#define mapa_h 20 // mapa alto
#define spriteTama 8 // tamaño de los sprites
// GP2X
// definiciones exclusivas Stick GP2X
#define GP2X_BUTTON_UP (0)
#define GP2X_BUTTON_DOWN (4)
#define GP2X_BUTTON_LEFT (2)
#define GP2X_BUTTON_RIGHT (6)
#define GP2X_BUTTON_UPLEFT (1)
#define GP2X_BUTTON_UPRIGHT (7)
#define GP2X_BUTTON_DOWNLEFT (3)
#define GP2X_BUTTON_DOWNRIGHT (5)
#define GP2X_BUTTON_CLICK (18)
#define GP2X_BUTTON_A (12)
#define GP2X_BUTTON_B (13)
#define GP2X_BUTTON_X (14)
#define GP2X_BUTTON_Y (15)
#define GP2X_BUTTON_L (10)
#define GP2X_BUTTON_R (11)
#define GP2X_BUTTON_START (8)
#define GP2X_BUTTON_SELECT (9)
#define GP2X_BUTTON_VOLUP (16)
#define GP2X_BUTTON_VOLDOWN (17)
// PC
#define UP 0
#define DOWN 4
#define LEFT 2
#define RIGHT 6
void pintaSnake(); // pinta la serpiente en la pantalla
void pintaImagen(int x,int y,SDL_Surface *img); // pinta img en la pantalla
void leeTeclado(); // lee el teclado
SDL_Surface * cargaImagen(char *ruta); // carga imagenes de disco y
Uint32 TimeLeft(void); // calculara el tiempo que ha pasado desde su ultima llamada
void iniciaSnake(); // inicia el array de la serpiente
void messageLoop();
typedef struct
{
int x; // pos x
int y; // pos y
int udir; // direccion
int activo; // activo?
int parte; // tipo parte ? cuerpo/cola
} poSnake;
int snakeTama=6; // tamaño que mide la serpiente
int nDir=2; // ultima direccion seleccionada
int dir=RIGHT; // direccion actual de snake
int iScore=0; // puntillos ^_^
int fx,fy; // posicion fruta
int snakeAumentos=0; // numero de largo q debe aumentar la cola
Uint8 *keys;
int dirJoy=0;
bool mouseButton;
int done;
poSnake Snake[_snakeMaxTama]; // declaracion del array
SDL_Surface *img_pantalla;
SDL_Surface *img_snake;
/* The screen surface */
SDL_Surface *screen = NULL;
// diseño nivel 1 (y unico de momento)
int level[]= {
1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1, 1,1,1,1,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,1,
1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,1,
0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,
0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,
0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,
0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,
0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,
0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,
1,1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0, 0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0, 0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0, 0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0, 0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0, 0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0, 0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0, 0,0,0,0,1,
1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1, 1,1,1,1,1};
/*
funcion encargada de dibujar las imagenes
Parametros: X , Y , Imagene a pintar
Retorna: Nada (void)
*/
void pintaImagen(int x,int y,SDL_Surface *img)
{
SDL_Rect r; // creamos un SDL_Rect que no es mas que una estructura 4 variable, X , Y , Alto, Ancho
r.x=x; // le especificamos la posicion X
r.y=y; // le especificamos la posicion Y
// no es necesario poner las variables W y H , este Rect solo lo utilizaremos para pintar una imagen en unas coordenadas asi que W y H no se utilizan
SDL_BlitSurface(img,NULL,screen,&r); // (imagen que queremos pintar, porcion de la imagen que queremos pintar(en nuestro caso toda asi que NULL), destino de la imagen (la pintaremos en la pantalla SCREEN), y la posicion donde la pintaremos
}
/*
funcion encargada de cargar las imagenes de disco
Parametros: ruta
Retorna: un SDL_Surface, o NULL si hay algun error
*/
SDL_Surface * cargaImagen(char *ruta)
{
SDL_Surface *img;
if (!(img=SDL_LoadBMP(ruta)))
{
printf("error no se pudo cargar la imagen\n");
return NULL;
}
SDL_SetColorKey(img,SDL_SRCCOLORKEY,SDL_MapRGB(img->format,255,0,255));
return img;
}
/*
inicializa las variables referentes a Snake
Parametros: Ninguno
Retorna: Nada (void)
*/
void iniciaSnake()
{
for (int n=0;n<_snakeMaxTama;n++)
{
Snake[n].x=9+(n*spriteTama+1);
Snake[n].y=9;
}
}
/*
como su nombre indica es la funcion que se encargara de dibujar la serpiente
Parametros: Ninguno
Retorna: Nada (void)
*/
void pintaSnake()
{
for (int n=0;n<snakeTama;n++)
{
pintaImagen(Snake[n].x,Snake[n].y,img_snake);
}
}
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[])
{
//pantalla=SDL_CreateRGBSurface(SDL_SWSURFACE, 320, 240, 16,img->format->Rmask,img->format->Gmask, img->format->Bmask,img->format->Amask);
/* Initialize SDL */
if (SDL_Init (SDL_INIT_VIDEO) < 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);
}
img_snake=cargaImagen("block.bmp");
iniciaSnake();
done = 0;
while (!done)
{
SDL_Event event;
SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
messageLoop();
leeTeclado();
pintaSnake();
SDL_Delay(TimeLeft());
SDL_Flip(screen);
}
return 0;
}
void leeTeclado()
{
for (int n=snakeTama;n>0;n--)
{
Snake[n].x=Snake[n-1].x;
Snake[n].y=Snake[n-1].y;
}
keys=SDL_GetKeyState(NULL);
if (keys[SDLK_RIGHT] || dirJoy==RIGHT)
{
dir=RIGHT;
}
if (keys[SDLK_LEFT] || dirJoy==LEFT)
{
dir=LEFT;
}
if (keys[SDLK_UP] || dirJoy==UP)
{
dir=UP;
}
if (keys[SDLK_DOWN] || dirJoy==DOWN)
{
dir=DOWN;
}
switch (dir)
{
case RIGHT:
Snake[0].x+=spriteTama+1;
break;
case LEFT:
Snake[0].x-=spriteTama+1;
break;
case UP:
Snake[0].y-=spriteTama+1;
break;
case DOWN:
Snake[0].y+=spriteTama+1;
break;
}
/*
controla que la serpiente no se salga de los limites de la pantalla
*/
if (Snake[0].x>320 && dir==RIGHT)
Snake[0].x=1;
if (Snake[0].x=1 && dir==LEFT)
Snake[0].x=320;
if (Snake[0].y>240 && dir==DOWN)
Snake[0].y=1;
if (Snake[0].y=1 && dir==UP)
Snake[0].y=240;
}
void pintaFondo()
{
int n,ny;
for (n=0;n< mapa_w;n++)
{
for (ny=0;ny<mapa_h;ny++)
{
if (level[n+(ny*mapa_w)]==1)
{
}
else
{
}
}
}
}
Uint32 TimeLeft(void)
{
static Uint32 next_time = 0;
Uint32 now;
now = SDL_GetTicks();
if ( next_time <= now ) {
next_time = now+TICK_INTERVAL;
return(0);
}
return(next_time-now);
}
void messageLoop()
{
// message processing loop
SDL_Event event;
while (SDL_PollEvent(&event))
{
// check for messages
dirJoy=-1;
mouseButton=-1;
switch (event.type)
{
case SDL_JOYBUTTONDOWN:
switch(event.jbutton.button)
{
case GP2X_BUTTON_X:
// X pressed
break;
case GP2X_BUTTON_L:
// L pressed
break;
case GP2X_BUTTON_LEFT:
dirJoy=LEFT;
break;
case GP2X_BUTTON_RIGHT:
dirJoy=RIGHT;
break;
case GP2X_BUTTON_UP:
dirJoy=UP;
break;
case GP2X_BUTTON_DOWN:
dirJoy=DOWN;
break;
default:
dirJoy=-1;
break;
}
break;
case SDL_JOYBUTTONUP:
switch(event.jbutton.button)
{
case GP2X_BUTTON_X:
// X released
break;
case GP2X_BUTTON_L:
// L released
break;
}
break;
// exit if the window is closed
case SDL_QUIT:
done = true;
break;
// check for keypresses
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE)
done = true;
break;
} // end switch
} // end of message processing
}
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.