Código:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "SDL\SDL.h"
#include "SDL\SDL_ttf.h"
#include "SDL\SDL_image.h"
#include "SDL\SDL_rotozoom.h"
#include "SDL\SDL_mixer.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
};
TTF_Font * font;
SDL_Color color={255,255,200};
/* The screen surface, joystick device */
SDL_Surface *screen = NULL;
SDL_Joystick *joy = NULL;
void cls()
{
SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0)); // dibujamos un rectangulo negro para borrar el frame anterior
}
void DrawText(char * texto,int x,int y, TTF_Font *font)
{
SDL_Color color={0,0,0};
SDL_Rect rect;
rect.x=x;
rect.y=y;
SDL_BlitSurface(TTF_RenderText_Solid(font,texto,color) ,NULL,screen,&rect); // FPS
}
void DrawText(char * texto,int x,int y, TTF_Font *font,SDL_Color color)
{
SDL_Rect rect;
rect.x=x;
rect.y=y;
SDL_BlitSurface(TTF_RenderText_Solid(font,texto,color) ,NULL,screen,&rect); // FPS
}
void Terminate(void)
{
SDL_Quit();
#ifdef GP2X
chdir("/usr/gp2x");
execl("/usr/gp2x/gp2xmenu", "/usr/gp2x/gp2xmenu", NULL);
#endif
}
Mix_Music * musica;
Mix_Chunk *sonido;
int canal;
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 ());
}
}
if (TTF_Init() == -1)
{
fprintf(stderr,"Unable to initialize SDL_ttf: %s \n", TTF_GetError());
//Put here however you want to do about the error.
//you could say:
//return true;
//Or:
//exit(1);
}
if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY,AUDIO_S16,MIX_DEFAULT_CHANNELS,128))
{
fprintf(stderr,"no se puede inicializar SDL_Mixer %s\n", Mix_GetError());
printf("no se puede inicializar SDL_Mixer %s\n",Mix_GetError());
exit(1);
}
#ifdef GP2X
/* Only use GP2X code here */
#endif
#ifdef WIN32
/* Only use Windows code here */
#endif
font=TTF_OpenFont("img/fuente.ttf", 8);
//fprintf (stderr, "cargando musica");
//musica=Mix_LoadMUS ("DARK.S3M");
if (font==NULL)
fprintf (stderr, "error en la carga");
sonido=Mix_LoadWAV("PASO10.WAV");
//canal=Mix_PlayMusic(musica,-1);
done = 0;
Mix_PlayChannel(-1,sonido,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;
}
}
if (!Mix_Playing(-1))
Mix_PlayChannel(-1,sonido,0);
// cls();
DrawText("TEST SONIDO",10,50,font,color);
if (sonido==NULL)
DrawText("NO CARGA SONIDO",10,100,font,color);
/* Processing */
SDL_Flip(screen);
}
return0;
}
Marcadores