commit 8a84d0a0f57c8b5218106a23dfeee33aa9ba4f69 Author: Valeriano A.R Date: Tue Dec 13 17:00:00 2011 +0100 (20111213) diff --git a/Audio.c b/Audio.c new file mode 100644 index 0000000..a5de31d --- /dev/null +++ b/Audio.c @@ -0,0 +1,259 @@ +#ifdef WIN32 + #define _WIN32_WINNT 0x0501 + #include +#endif +#include + +#include "Audio.h" + +static void Audio_MixerCallback(void *ud,Uint8 *stream,int l); + + +//////////////////////////////////////////////// +// AudioWave // +/////////////// +// Reference to a sound. +typedef struct Tag_AudioWave { + SDL_AudioSpec spec; + Uint32 len; + Uint8 *buffer; + + struct Tag_AudioWave *next; +} AudioWave; +AudioWave *_waves=NULL; + + +//////////////////////////////////////////////// +// AudioChan // +/////////////// +// Reference to a sound. +typedef struct Tag_AudioChan { + AudioWave *wave; + Uint32 pos; + unsigned char rightvol; + unsigned char leftvol; + + struct Tag_AudioChan *next; +} AudioChan; +AudioChan *_channels=NULL; +AudioChan *_free_channels=NULL; + +///////////////////////////// +// Audio_Init +// +// Initializes the game audio. +int Audio_Init(){ + SDL_AudioSpec as; + SDL_AudioSpec as2; + + // Initialize audio subsistem +#ifdef WIN32 + // Force DSound Driver on win32 + putenv("SDL_AUDIODRIVER=dsound"); +#endif + if(SDL_InitSubSystem(SDL_INIT_AUDIO) < 0){ + printf("Audio_Init: Failure initializing SDL Audio.\n"); + printf("Audio_Init: SDL Error: %s\n",SDL_GetError()); + return(0); + } + + // Open the audio device using the desired parameters + as.freq = 44100; + as.format = AUDIO_S16SYS; + as.channels = 2; + as.samples = 1024; + as.callback = Audio_MixerCallback; + if(SDL_OpenAudio(&as, &as2) < 0){ + printf("Audio_Init: Failure opening audio.\n"); + printf("Audio_Init: SDL Error: %s\n",SDL_GetError()); + return(0); + } + + // Asert results + if( as2.format != AUDIO_S16SYS || + as2.freq != 44100 || + as2.channels != 2 ) + { + printf("Audio_Init: Failure opening audio. (44.1Khz/16b/2c).\n"); + SDL_CloseAudio(); + return(0); + } + + // Unpause and ready to go + SDL_PauseAudio(0); + + return(1); +} + + +///////////////////////////// +// Audio_MixerCallback +// +// Mixes the audio channels. +static void Audio_MixerCallback(void *ud,Uint8 *stream,int l){ + signed short *ptr_out,*ptr_wave; + AudioChan *prevchan; + AudioChan *chan; + AudioWave *wave; + int len=l/4; // Asume 16bpb and 2 output chan + int chan_remain; + int len_mix; + int i; + + // Clean + memset(stream,0,len); + + // Mix all the channels + prevchan=NULL; + chan=_channels; + while(chan){ + if(!chan->wave){ + // Remove finished channels + AudioChan *aux_chan=chan->next; + chan->next=_free_channels; + _free_channels=chan; + chan=aux_chan; + if(prevchan){ + prevchan->next=chan; + }else{ + _channels=chan; + } + continue; + } + + // Prepare the pointers + ptr_out=(signed short *)stream; + ptr_wave=((signed short *)chan->wave->buffer)+chan->pos; + wave=chan->wave; + + // Determine mixing lenght + chan_remain=wave->len-chan->pos; + if(chan_remain>len){ + len_mix=len; + }else{ + len_mix=chan_remain; + chan->wave=NULL; + } + + // Mix the buffer + for(i=0;ileftvol)>>8; + if(temp>(1<<14)){ + ptr_out[0]=1<<14; + }else if(temp<-(1<<14)){ + ptr_out[0]=-(1<<14); + }else{ + ptr_out[0]=temp; + } + + // Right Channel + temp=ptr_out[1]; + temp+=(ptr_wave[0]*chan->rightvol)>>8; + if(temp>(1<<14)){ + ptr_out[1]=1<<14; + }else if(temp<-(1<<14)){ + ptr_out[1]=-(1<<14); + }else{ + ptr_out[1]=temp; + } + + // Next sample + ptr_out+=2; + ptr_wave++; + } + chan->pos+=len_mix; + + // Next channel + prevchan=chan; + chan=chan->next; + } +} + + +///////////////////////////// +// Audio_Frame +// +// Notify a frame update to the audio subsystem. +void Audio_Frame(){ + +} + + +///////////////////////////// +// Audio_LoadSound +// +// Loads a sound, giving a reference. +AudioSnd Audio_LoadSound(char *filename){ + AudioWave *wave; + + // Allocate and load the sound + wave=malloc(sizeof(AudioWave)); + if( SDL_LoadWAV(filename, + &wave->spec, &wave->buffer, &wave->len) == NULL ) + { + printf("Audio_LoadSound: Failure Loading sound: %s\n",filename); + printf("Audio_LoadSound: SDL Error: %s\n",SDL_GetError()); + free(wave); + return(NULL); + } + + // Asert results + if( wave->spec.format != AUDIO_S16 || + wave->spec.freq != 44100 || + wave->spec.channels != 1 ) + { + printf("Audio_LoadSound: Failure opening sound. (44.1Khz/16b/1c).\n"); + SDL_FreeWAV(wave->buffer); + free(wave); + return(0); + } + + // Correct the lenght + wave->len/=2; + + // Take a reference + wave->next=_waves; + _waves=wave; + + return((AudioSnd)wave); +} + + +///////////////////////////// +// Audio_PlaySound +// +// Loads a sound, giving a reference. +void Audio_PlaySound(AudioSnd snd, + float leftvol, float rightvol) +{ + AudioChan *chan; + AudioWave *wave; + if(!snd) + return; + + // Cast AudioSnd to AudioWave + wave=snd; + + // Get a free channel + if(_free_channels){ + chan=_free_channels; + _free_channels=chan->next; + chan->next=NULL; + }else{ + chan=malloc(sizeof(AudioChan)); + chan->next=NULL; + } + + // Initialize the channel + chan->wave=wave; + chan->pos=0; + chan->rightvol=(rightvol*255); + chan->leftvol=(leftvol*255); + chan->next=_channels; + _channels=chan; +} + diff --git a/Audio.h b/Audio.h new file mode 100644 index 0000000..902033f --- /dev/null +++ b/Audio.h @@ -0,0 +1,41 @@ +#ifndef _AUDIO_H_ +#define _AUDIO_H_ + + +///////////////////////////// +// Audio_Init +// +// Initializes the game audio. +int Audio_Init(); + + +///////////////////////////// +// Audio_Frame +// +// Notify a frame update to the audio subsystem. +void Audio_Frame(); + + +//////////////////////////////////////////////// +// AudioSnd // +////////////// +// Reference to a sound. +typedef void *AudioSnd; + + +///////////////////////////// +// Audio_LoadSound +// +// Loads a sound, giving a reference. +AudioSnd Audio_LoadSound(char *filename); + + +///////////////////////////// +// Audio_PlaySound +// +// Loads a sound, giving a reference. +void Audio_PlaySound(AudioSnd snd, + float leftvol, float rightvol); + + +#endif diff --git a/Draw.c b/Draw.c new file mode 100644 index 0000000..edcfbf3 --- /dev/null +++ b/Draw.c @@ -0,0 +1,349 @@ +#ifdef WIN32 + #define _WIN32_WINNT 0x0501 + #include +#endif +#include + +#include "Time.h" +#include "Draw.h" +#include "Input.h" +#include "Audio.h" + + +// Globals +SDL_Surface *_screen=NULL; +long long _t_frame=17000; + +///////////////////////////// +// Draw_Init +// +// Initializes the game window. +int Draw_Init(int width,int height,char *title,int fps){ +#ifdef WIN32 + // Stdout on the parent console + AttachConsole(ATTACH_PARENT_PROCESS); + if(GetStdHandle(STD_OUTPUT_HANDLE)!=0){ + fclose(stdin); + fclose(stdout); + fclose(stderr); + freopen("CONIN$","r",stdin); + freopen("CONOUT$","w",stdout); + freopen("CONOUT$","w",stderr); + } +#endif + + // Initialize SDL + if(SDL_Init(SDL_INIT_VIDEO)<0){ + printf("Draw_Init: Failure initializing SDL.\n"); + printf("Draw_Init: SDL Error: %s\n",SDL_GetError()); + return(0); + } + + // Initialize video mode + _screen=SDL_SetVideoMode(width,height,32,SDL_HWSURFACE); + if( _screen == NULL){ + printf("Draw_Init: Failure initializing video mode.\n"); + printf("Draw_Init: SDL Error: %s\n",SDL_GetError()); + return(0); + } + SDL_WM_SetCaption(title, NULL); + _t_frame=1000000/fps; + + + return(1); +} + + +///////////////////////////// +// Draw_Loop +// +// Loops updating the game window. +void Draw_Loop(int (*proc)()){ + int done=0; + SDL_Event event; + Uint8* keys; + long long t_framestart; + long long t_frame; + long long t_swap; + long long t_proc; + int f_count; + + t_framestart=Time_GetTime(); + + while(!done){ + + // Update screen + t_swap=Time_GetTime(); + SDL_Flip(_screen); + t_swap=Time_GetTime()-t_swap; + + // Process Events + while(SDL_PollEvent(&event) ){ + if(event.type == SDL_QUIT ){ + done=1; + } + if(event.type == SDL_KEYDOWN ){ + if(event.key.keysym.sym == SDLK_ESCAPE ) { + done=1; + } + } + } + + // Process keys for Draw + keys=SDL_GetKeyState(NULL); + if(keys[SDLK_F12]){ + // Screenshot key + SDL_SaveBMP(_screen,"shot.bmp"); + } + + // Input and sound Frame + Input_Frame(); + Audio_Frame(); + + // Process loop, with frameskip for slow swapping systems + t_proc=Time_GetTime(); + if(!proc()){ + done=1; + } + f_count=(t_swap/_t_frame); + while(f_count>0 && !done){ + if(!proc()){ + done=1; + } + f_count--; + t_framestart+=_t_frame; + } + t_proc=Time_GetTime()-t_proc; + t_framestart+=_t_frame*(t_proc/_t_frame); + + + // Sleep to limit frames + t_frame=Time_GetTime()-t_framestart; + if(t_frame<_t_frame){ + Time_Pause(_t_frame-t_frame); + }else{ + Time_Pause(0); + } + t_framestart=Time_GetTime(); + } +} + + +///////////////////////////// +// Draw_Clean +// +// Cleans the game window. +void Draw_Clean( + unsigned char r, + unsigned char g, + unsigned char b) +{ + SDL_Rect rect; + + // Draw a full rectangle + rect.x=0; + rect.y=0; + rect.w=_screen->w; + rect.h=_screen->h; + SDL_FillRect(_screen, &rect, + SDL_MapRGB(_screen->format, r, g, b)); +} + + +///////////////////////////// +// Draw_LoadImage +// +// Loads a image, giving a reference. +DrawImg Draw_LoadImage(char *filename){ + SDL_Surface *img; + + // Load the BMP as a surface + img=SDL_LoadBMP(filename); + if(img == NULL){ + printf("Draw_LoadImage: Failure Loading image: %s\n",filename); + printf("Draw_LoadImage: SDL Error: %s\n",SDL_GetError()); + return(NULL); + } + + // FIX: Setting up the alpha channel. + if(img->format->BytesPerPixel==4){ + img->format->Amask=0xFF000000; + img->format->Ashift=24; + } + + return((DrawImg *)img); +} + + +///////////////////////////// +// Draw_ImgSetKeyCol +// +// Setting the image color key. +void Draw_ImgSetKeyCol(DrawImg img, + unsigned char r, + unsigned char g, + unsigned char b) +{ + SDL_Surface *surf; + if(!img) + return; + + // Set the color key for the surface + surf=(SDL_Surface *)img; + SDL_SetColorKey(surf, SDL_SRCCOLORKEY, + SDL_MapRGB(surf->format, r, g, b)); +} + + +///////////////////////////// +// Draw_ImgSetAlpha +// +// Setting the image alpha. +void Draw_ImgSetAlpha(DrawImg img, unsigned char a){ + SDL_Surface *surf; + if(!img) + return; + + // Set the alpha for the surface + surf=(SDL_Surface *)img; + SDL_SetAlpha(surf, SDL_SRCALPHA, a); +} + + +///////////////////////////// +// Draw_DrawImg +// Draw_DrawImgCenter +// +// Draws an image. And a centered variant +void Draw_DrawImg(DrawImg img,int x,int y){ + SDL_Surface *surf; + SDL_Rect orig; + SDL_Rect dest; + if(!img) + return; + + // Prepare the rects + surf=(SDL_Surface *)img; + orig.x=0; + orig.y=0; + dest.x=x; + dest.y=y; + orig.w=dest.w=surf->w; + orig.h=dest.h=surf->h; + + // Blit the surface on the screen + SDL_BlitSurface(surf,&orig,_screen,&dest); +} +void Draw_DrawImgCenter(DrawImg img,int x,int y){ + SDL_Surface *surf; + SDL_Rect orig; + SDL_Rect dest; + if(!img) + return; + + // Prepare the rects + surf=(SDL_Surface *)img; + orig.x=0; + orig.y=0; + dest.x=x-(surf->w/2); + dest.y=y-(surf->h/2); + orig.w=dest.w=surf->w; + orig.h=dest.h=surf->h; + + // Blit the surface on the screen + SDL_BlitSurface(surf,&orig,_screen,&dest); +} + + +//////////////////////////////////////////////// +// DrawFnt // +///////////// +// Reference to a Font. +typedef struct { + SDL_Surface *surf; + int w,h; +} DrawFont; + + +///////////////////////////// +// Draw_DefaultFont +// +// Creates a image with the default font. +#include "FontData.h" +DrawFnt Draw_DefaultFont( + unsigned char r, + unsigned char g, + unsigned char b, + unsigned char a) +{ + DrawFont *font; + int x,y,c; + Uint32 color,color2; + + // Create the default font + font=malloc(sizeof(DrawFont)); + font->surf = SDL_CreateRGBSurface(SDL_SWSURFACE, + 8*256, 8, 32,0,0,0,0); + font->w=8; + font->h=8; + font->surf->format->Amask=0xFF000000; + font->surf->format->Ashift=24; + SDL_SetAlpha(font->surf, SDL_SRCALPHA, 255); + + // Draw the font + SDL_LockSurface(font->surf); + color =SDL_MapRGBA(font->surf->format,r,g,b,a); + color2=SDL_MapRGBA(font->surf->format,r,g,b,0); + for(c=0;c<256;c++){ + for(y=0;y<8;y++){ + for(x=0;x<8;x++){ + if(((fontdata_8x8[c*8+y]>>(7-x)) & 0x01)==1){ + //Imagen_PutPixel(dest,c*8+x,y,color); + ((Uint32 *)font->surf->pixels)[(c*8+x)+(8*256*y)]= + color; + }else{ + //Imagen_PutPixel(dest,c*8+x,y,color2); + ((Uint32 *)font->surf->pixels)[(c*8+x)+(8*256*y)]= + color2; + } + } + } + } + SDL_UnlockSurface(font->surf); + + return((DrawFnt)font); +} + + +///////////////////////////// +// Draw_DrawText +// +// Draws text using a font +void Draw_DrawText(DrawFnt f,char *text,int x,int y){ + DrawFont *font=f; + SDL_Rect orig; + SDL_Rect dest; + char *ptr; + if(!f) + return; + + // Prepare the rects + orig.w=dest.w=font->w; + orig.h=dest.h=font->h; + orig.y=0; + dest.x=x; + dest.y=y; + + // Iterate the string + ptr=text; + while(*ptr){ + orig.x=(*ptr)*font->w; + dest.x=x; + dest.y=y; + // Blit every character + SDL_BlitSurface(font->surf,&orig,_screen,&dest); + x+=font->w; + ptr++; + } +} + diff --git a/Draw.h b/Draw.h new file mode 100644 index 0000000..22a52da --- /dev/null +++ b/Draw.h @@ -0,0 +1,93 @@ +#ifndef _DRAW_H_ +#define _DRAW_H_ + + +///////////////////////////// +// Draw_Init +// +// Initializes the game window. +int Draw_Init(int width,int height,char *title,int fps); + + +///////////////////////////// +// Draw_Loop +// +// Loops updating the game window. +void Draw_Loop(int (*proc)()); + + +///////////////////////////// +// Draw_Clean +// +// Cleans the game window. +void Draw_Clean( + unsigned char r, + unsigned char g, + unsigned char b); + + +//////////////////////////////////////////////// +// DrawImg // +///////////// +// Reference to a image. +typedef void *DrawImg; + + +///////////////////////////// +// Draw_LoadImage +// +// Loads a image, giving a reference. +DrawImg Draw_LoadImage(char *filename); + + +///////////////////////////// +// Draw_ImgSetKeyCol +// +// Setting the image color key. +void Draw_ImgSetKeyCol(DrawImg img, + unsigned char r, + unsigned char g, + unsigned char b); + + +///////////////////////////// +// Draw_ImgSetAlpha +// +// Setting the image alpha. +void Draw_ImgSetAlpha(DrawImg img, unsigned char a); + + +///////////////////////////// +// Draw_DrawImg +// Draw_DrawImgCenter +// +// Draws an image. And a centered variant +void Draw_DrawImg(DrawImg img,int x,int y); +void Draw_DrawImgCenter(DrawImg img,int x,int y); + + +//////////////////////////////////////////////// +// DrawFnt // +///////////// +// Reference to a Font. +typedef void *DrawFnt; + + +///////////////////////////// +// Draw_DefaultFont +// +// Loads a image, giving a reference. +DrawFnt Draw_DefaultFont( + unsigned char r, + unsigned char g, + unsigned char b, + unsigned char a); + + +///////////////////////////// +// Draw_DrawText +// +// Draws text using a font +void Draw_DrawText(DrawFnt f,char *text,int x,int y); + +#endif diff --git a/Entity.c b/Entity.c new file mode 100644 index 0000000..b519d82 --- /dev/null +++ b/Entity.c @@ -0,0 +1,37 @@ +#ifndef _ENTITY_H_ +#define _ENTITY_H_ + + +//////////////////////////////////////////////// +// Entity // +//////////// +// Reference to an entity. +typedef void *Entity; + + +///////////////////////////// +// Entity_Create +// +// Creates an entity. +Entity Entity_Create( + int x,int y, + void (*process)(Entity ent,void *data), + void *data); + + +///////////////////////////// +// Entity_Create +// +// Creates an entity. + +///////////////////////////// +// Entity_CreateClass +// +// Initializes the game. +void Entity_CreateClass(char *name, + void *(*createdata)(), + void (*process)(Entity ent,void *data)); + + + +#endif diff --git a/Entity.h b/Entity.h new file mode 100644 index 0000000..8d5c1ad --- /dev/null +++ b/Entity.h @@ -0,0 +1,6 @@ +#ifndef _ENTITY_H_ +#define _ENTITY_H_ + + + +#endif diff --git a/FontData.h b/FontData.h new file mode 100644 index 0000000..5953c63 --- /dev/null +++ b/FontData.h @@ -0,0 +1,258 @@ +static unsigned char fontdata_8x8[2048] = { + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0xfe,0xfe,0xfe,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x10,0x38,0x7c,0xfe,0x00,0x00, + 0x00,0x00,0xfe,0x7c,0x38,0x10,0x00,0x00, + 0x20,0x30,0x38,0x3c,0x38,0x30,0x20,0x00, + 0x04,0x0c,0x1c,0x3c,0x1c,0x0c,0x04,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x18,0x18,0x18,0x18,0x18,0x00,0x18,0x00, + 0x6c,0x6c,0x00,0x00,0x00,0x00,0x00,0x00, + 0x6c,0x6c,0xfe,0x6c,0xfe,0x6c,0x6c,0x00, + 0x18,0x3e,0x60,0x3c,0x06,0x7c,0x18,0x00, + 0x00,0xc6,0xcc,0x18,0x30,0x66,0xc6,0x00, + 0x38,0x6c,0x38,0x76,0xdc,0xcc,0x76,0x00, + 0x18,0x18,0x30,0x00,0x00,0x00,0x00,0x00, + 0x0c,0x18,0x30,0x30,0x30,0x18,0x0c,0x00, + 0x30,0x18,0x0c,0x0c,0x0c,0x18,0x30,0x00, + 0x00,0x6c,0x38,0xfe,0x38,0x6c,0x00,0x00, + 0x00,0x18,0x18,0x7e,0x18,0x18,0x00,0x00, + 0x00,0x00,0x00,0x00,0x18,0x18,0x30,0x00, + 0x00,0x00,0x00,0x7e,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x18,0x18,0x00,0x00, + 0x06,0x0c,0x18,0x30,0x60,0xc0,0x80,0x00, + 0x3c,0x66,0x6e,0x76,0x66,0x3c,0x00,0x00, + 0x18,0x38,0x78,0x18,0x18,0x18,0x00,0x00, + 0x3c,0x66,0x06,0x1c,0x30,0x7e,0x00,0x00, + 0x3c,0x66,0x0c,0x06,0x66,0x3c,0x00,0x00, + 0x1c,0x3c,0x6c,0xcc,0xfe,0x0c,0x00,0x00, + 0x7e,0x60,0x7c,0x06,0x66,0x3c,0x00,0x00, + 0x1c,0x30,0x60,0x7c,0x66,0x3c,0x00,0x00, + 0x7e,0x06,0x06,0x0c,0x18,0x18,0x00,0x00, + 0x3c,0x66,0x3c,0x66,0x66,0x3c,0x00,0x00, + 0x3c,0x66,0x3e,0x06,0x0c,0x38,0x00,0x00, + 0x00,0x18,0x18,0x00,0x18,0x18,0x00,0x00, + 0x00,0x18,0x18,0x00,0x18,0x18,0x30,0x00, + 0x00,0x06,0x18,0x60,0x18,0x06,0x00,0x00, + 0x00,0x00,0x7e,0x00,0x7e,0x00,0x00,0x00, + 0x00,0x60,0x18,0x06,0x18,0x60,0x00,0x00, + 0x3c,0x66,0x06,0x0c,0x18,0x00,0x18,0x00, + 0x3c,0x66,0x5a,0x5a,0x5e,0x60,0x3c,0x00, + 0x3c,0x66,0x66,0x7e,0x66,0x66,0x00,0x00, + 0x7c,0x66,0x7c,0x66,0x66,0x7c,0x00,0x00, + 0x3c,0x60,0x60,0x60,0x60,0x3c,0x00,0x00, + 0x78,0x6c,0x66,0x66,0x6c,0x78,0x00,0x00, + 0x7e,0x60,0x78,0x60,0x60,0x7e,0x00,0x00, + 0x7e,0x60,0x78,0x60,0x60,0x60,0x00,0x00, + 0x3c,0x66,0x60,0x6e,0x66,0x3e,0x00,0x00, + 0x66,0x66,0x7e,0x66,0x66,0x66,0x00,0x00, + 0x3c,0x18,0x18,0x18,0x18,0x3c,0x00,0x00, + 0x06,0x06,0x06,0x06,0x66,0x3c,0x00,0x00, + 0xc6,0xcc,0xd8,0xf8,0xcc,0xc6,0x00,0x00, + 0x60,0x60,0x60,0x60,0x60,0x7c,0x00,0x00, + 0xc6,0xee,0xfe,0xd6,0xc6,0xc6,0x00,0x00, + 0xc6,0xe6,0xf6,0xde,0xce,0xc6,0x00,0x00, + 0x3c,0x66,0x66,0x66,0x66,0x3c,0x00,0x00, + 0xfc,0xc6,0xc6,0xfc,0xc0,0xc0,0x00,0x00, + 0x78,0xcc,0xcc,0xcc,0xdc,0x7e,0x00,0x00, + 0x7c,0x66,0x66,0x7c,0x6c,0x66,0x00,0x00, + 0x7c,0xc6,0x70,0x1c,0xc6,0x7c,0x00,0x00, + 0x7e,0x18,0x18,0x18,0x18,0x18,0x00,0x00, + 0x66,0x66,0x66,0x66,0x66,0x3c,0x00,0x00, + 0x66,0x66,0x66,0x66,0x3c,0x18,0x00,0x00, + 0xc6,0xc6,0xd6,0xfe,0xee,0xc6,0x00,0x00, + 0xc6,0x6c,0x38,0x38,0x6c,0xc6,0x00,0x00, + 0xc6,0x6c,0x38,0x30,0x30,0x30,0x00,0x00, + 0xfe,0x0c,0x18,0x30,0x60,0xfe,0x00,0x00, + 0x3c,0x30,0x30,0x30,0x30,0x30,0x3c,0x00, + 0xc0,0x60,0x30,0x18,0x0c,0x06,0x02,0x00, + 0x3c,0x0c,0x0c,0x0c,0x0c,0x0c,0x3c,0x00, + 0x18,0x3c,0x66,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0xfe,0x00, + 0x18,0x18,0x0c,0x00,0x00,0x00,0x00,0x00, + 0x00,0x3c,0x06,0x3e,0x66,0x3e,0x00,0x00, + 0x60,0x60,0x7c,0x66,0x66,0x7c,0x00,0x00, + 0x00,0x3c,0x60,0x60,0x60,0x3c,0x00,0x00, + 0x06,0x06,0x3e,0x66,0x66,0x3e,0x00,0x00, + 0x00,0x3c,0x66,0x7e,0x60,0x3c,0x00,0x00, + 0x1c,0x30,0x7c,0x30,0x30,0x30,0x00,0x00, + 0x00,0x3e,0x66,0x66,0x3e,0x06,0x3c,0x00, + 0x60,0x60,0x7c,0x66,0x66,0x66,0x00,0x00, + 0x30,0x00,0x30,0x30,0x30,0x18,0x00,0x00, + 0x0c,0x00,0x0c,0x0c,0x0c,0x0c,0x78,0x00, + 0x60,0x66,0x6c,0x78,0x6c,0x66,0x00,0x00, + 0x18,0x18,0x18,0x18,0x18,0x0c,0x00,0x00, + 0x00,0xec,0xfe,0xd6,0xc6,0xc6,0x00,0x00, + 0x00,0x7c,0x66,0x66,0x66,0x66,0x00,0x00, + 0x00,0x3c,0x66,0x66,0x66,0x3c,0x00,0x00, + 0x00,0x7c,0x66,0x66,0x7c,0x60,0x60,0x00, + 0x00,0x3e,0x66,0x66,0x3e,0x06,0x06,0x00, + 0x00,0x7c,0x66,0x60,0x60,0x60,0x00,0x00, + 0x00,0x3c,0x60,0x3c,0x06,0x7c,0x00,0x00, + 0x30,0x30,0x7c,0x30,0x30,0x1c,0x00,0x00, + 0x00,0x66,0x66,0x66,0x66,0x3e,0x00,0x00, + 0x00,0x66,0x66,0x66,0x3c,0x18,0x00,0x00, + 0x00,0xc6,0xc6,0xd6,0xfe,0x6c,0x00,0x00, + 0x00,0xcc,0x78,0x30,0x78,0xcc,0x00,0x00, + 0x00,0x66,0x66,0x66,0x3c,0x18,0x30,0x00, + 0x00,0x7e,0x0c,0x18,0x30,0x7e,0x00,0x00, + 0x0c,0x18,0x18,0x30,0x18,0x18,0x0c,0x00, + 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x00, + 0x30,0x18,0x18,0x0c,0x18,0x18,0x30,0x00, + 0x76,0xdc,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x10,0x38,0x6c,0xc6,0xc6,0xfe,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + }; diff --git a/GameLib.c b/GameLib.c new file mode 100644 index 0000000..d9ef72c --- /dev/null +++ b/GameLib.c @@ -0,0 +1,26 @@ +#include + +#include "Draw.h" +#include "Input.h" +#include "Audio.h" +#include "Entity.h" +#include "GameLib.h" + + +///////////////////////////// +// GameLib_Init +// +// Initializes the game. +int GameLib_Init(int w,int h,char *title,int fps){ + if(!Draw_Init(w,h,title,fps)){ + return(0); + } + if(!Input_Init()){ + return(0); + } + Audio_Init(); + return(1); +} + + + diff --git a/GameLib.h b/GameLib.h new file mode 100644 index 0000000..f208ca0 --- /dev/null +++ b/GameLib.h @@ -0,0 +1,33 @@ +#ifndef _GAMELIB_H_ +#define _GAMELIB_H_ + +#include "Draw.h" +#include "Input.h" +#include "Audio.h" +#include "Entity.h" + + +///////////////////////////// +// GameLib_Init +// +// Initializes the game. +int GameLib_Init(int w,int h,char *title,int fps); + + +///////////////////////////// +// GameLib_Loop +// +// Loops the game. +void GameLib_Loop(); + + +///////////////////////////// +// GameLib_BreakLoop +// +// Breaks the game loop. +void GameLib_BreakLoop(); + + + + +#endif diff --git a/Input.c b/Input.c new file mode 100644 index 0000000..06a8eac --- /dev/null +++ b/Input.c @@ -0,0 +1,68 @@ +#include + +#include "Input.h" + + +// Globals +InputKeyStatus _keys[InputKey_Max]; + + +///////////////////////////// +// Input_Init +// +// Initializes the game input. +int Input_Init(){ + int i; + + // Mark released all the keys + for(i=0;i=InputKey_Pressed){ + _keys[key]=InputKey_Holded; + }else{ + _keys[key]=InputKey_Pressed; + } + } +} + + +///////////////////////////// +// Input_GetKey +// +// Reports a the status of a key. +InputKeyStatus Input_GetKey(InputKey key){ + return(_keys[key]); +} diff --git a/Input.h b/Input.h new file mode 100644 index 0000000..dfaf10f --- /dev/null +++ b/Input.h @@ -0,0 +1,61 @@ +#ifndef _INPUT_H_ +#define _INPUT_H_ + + +///////////////////////////// +// Input_Init +// +// Initializes the game input. +int Input_Init(); + + +///////////////////////////// +// Input_Frame +// +// Notify a frame update to the input subsystem. +void Input_Frame(); + + +//////////////////////////////////////////////// +// InputKey // +////////////// +// Key enumeration. +typedef enum { + InputKey_Action1, + InputKey_Action2, + InputKey_Up, + InputKey_Down, + InputKey_Left, + InputKey_Right, + InputKey_Jump, + InputKey_Max +} InputKey; + + +///////////////////////////// +// Input_SetKey +// +// Notify a key press to the input subsystem. +void Input_SetKey(InputKey key,int status); + + +//////////////////////////////////////////////// +// InputKeyStatus // +/////////////////// +// Key status enumeration. +typedef enum { + InputKey_Released, + InputKey_Pressed, + InputKey_Holded +} InputKeyStatus; + + +///////////////////////////// +// Input_GetKey +// +// Reports a the status of a key. +InputKeyStatus Input_GetKey(InputKey key); + + + +#endif diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..17e6917 --- /dev/null +++ b/Makefile @@ -0,0 +1,26 @@ +ifneq (,$(findstring MINGW,$(shell uname -s))) + TARGET_ARCH=mingw +else +ARCH:=$(shell uname) +ifeq ($(ARCH), Darwin) + TARGET_ARCH=macosx +else + TARGET_ARCH=linux +endif # Darwin +endif # windir + + + + + +ifeq ($(TARGET_ARCH),mingw) + include Makefile.mingw +else +ifeq ($(TARGET_ARCH),linux) + include Makefile.linux +else +ifeq ($(TARGET_ARCH),macosx) + include Makefile.macosx +endif # macosx +endif # linux +endif # mingw diff --git a/Makefile.common b/Makefile.common new file mode 100644 index 0000000..e73611d --- /dev/null +++ b/Makefile.common @@ -0,0 +1,56 @@ +HEADS= \ + Time.h \ + Draw.h \ + Input.h \ + Audio.h \ + Entity.h \ + GameLib.h +OBJS= \ + $(BUILDDIR)/Time.o \ + $(BUILDDIR)/Draw.o \ + $(BUILDDIR)/Input.o \ + $(BUILDDIR)/Audio.o \ + $(BUILDDIR)/Entity.o \ + $(BUILDDIR)/GameLib.o \ + $(BUILDDIR)/main.o + + +all: $(BUILDDIR) $(BUILDDIR)/$(RESULT) + +$(BUILDDIR): + mkdir $(BUILDDIR) + + + +$(BUILDDIR)/Time.o: Time.c $(HEADS) + $(CC) -c Time.c -o $(BUILDDIR)/Time.o $(CFLAGS) + +$(BUILDDIR)/Draw.o: Draw.c $(HEADS) + $(CC) -c Draw.c -o $(BUILDDIR)/Draw.o $(CFLAGS) + +$(BUILDDIR)/Input.o: Input.c $(HEADS) + $(CC) -c Input.c -o $(BUILDDIR)/Input.o $(CFLAGS) + +$(BUILDDIR)/Audio.o: Audio.c $(HEADS) + $(CC) -c Audio.c -o $(BUILDDIR)/Audio.o $(CFLAGS) + +$(BUILDDIR)/Entity.o: Entity.c $(HEADS) + $(CC) -c Entity.c -o $(BUILDDIR)/Entity.o $(CFLAGS) + +$(BUILDDIR)/GameLib.o: GameLib.c $(HEADS) + $(CC) -c GameLib.c -o $(BUILDDIR)/GameLib.o $(CFLAGS) + +$(BUILDDIR)/main.o: main.c $(HEADS) + $(CC) -c main.c -o $(BUILDDIR)/main.o $(CFLAGS) + +$(BUILDDIR)/$(RESULT): $(OBJS) + $(CC) -o $(BUILDDIR)/$(RESULT) $(OBJS) $(LIBS) $(CFLAGS) + + +clean: + rm -f $(OBJS) $(BUILDDIR)/$(RESULT) + +run: $(BUILDDIR)/$(RESULT) + ./$(BUILDDIR)/$(RESULT) + + diff --git a/Makefile.linux b/Makefile.linux new file mode 100644 index 0000000..4f167ec --- /dev/null +++ b/Makefile.linux @@ -0,0 +1,9 @@ +LIBS= -lSDL -lpthread -L/usr/X11R6/lib -L/usr/lib +CFLAGS= -Wall -g -I/usr/include/ -I/usr/include/SDL/ -I/usr/X11R6/include/ +CC=gcc +RM=rm -rf + +RESULT=game +BUILDDIR=build-linux + +include Makefile.common diff --git a/Makefile.macosx b/Makefile.macosx new file mode 100644 index 0000000..89f4429 --- /dev/null +++ b/Makefile.macosx @@ -0,0 +1,9 @@ +LIBS=-lm -ldl -framework Cocoa -framework SDL macosx/SDLMain.m +CFLAGS=-g -DDEBUG -Wall -DMACOSX -ObjC -Dmain=SDL_main +CC=gcc +RM=rm -rf + +RESULT=game +BUILDDIR=build-macosx + +include Makefile.common \ No newline at end of file diff --git a/Makefile.mingw b/Makefile.mingw new file mode 100644 index 0000000..88ece6e --- /dev/null +++ b/Makefile.mingw @@ -0,0 +1,11 @@ +LIBS=-I/mingw/include/SDL -D_GNU_SOURCE=1 -Dmain=SDL_main +CFLAGS= -L/mingw/lib -lmingw32 -lSDLmain -lSDL -mwindows +CC=gcc +RM=rm -rf + +RESULT=game.exe +BUILDDIR=build-mingw + +include Makefile.common + + diff --git a/SDL.dll b/SDL.dll new file mode 100644 index 0000000..a5da7bb Binary files /dev/null and b/SDL.dll differ diff --git a/Time.c b/Time.c new file mode 100644 index 0000000..78aa06b --- /dev/null +++ b/Time.c @@ -0,0 +1,83 @@ +#include +#include +#include +#include +#include +#include + +#include "Time.h" + + +///////////////////////////// +// Time_GetTime +// +// Gets the current time in usecs. +///////////////////////////// +// Time_Pause +// +// Pauses the execution for t usecs. +#if WIN32 +#include +// WIN32 +long long Time_GetTime(){ + LARGE_INTEGER freq; + LARGE_INTEGER tim; + long long int microt; + + QueryPerformanceFrequency(&freq); + QueryPerformanceCounter(&tim); + microt=(tim.QuadPart*1000000)/freq.QuadPart; + return(microt); +} +void Time_Pause(int pausa){ + long long tend,t,diff; + + tend=Time_GetTime()+pausa; + do{ + diff=tend-t; + if(diff>1000){ + SDL_Delay(diff/1000); + }else{ + Sleep(0); + } + t=Time_GetTime(); + }while(tend>=t); +} +#else +#ifdef MACOSX +#include +// MacOSX +long long Time_GetTime(){ + static mach_timebase_info_data_t info = {0,0}; + uint64_t t; + if(info.denom==0){ + mach_timebase_info(&info); + } + t=mach_absolute_time()*(info.numer / info.denom); + return(t/1000); +} +void Time_Pause(int pausa){ + struct timeval tv; + tv.tv_sec=(long long)pausa/1000000; + tv.tv_usec=(long long)pausa%1000000; + select(0, NULL, NULL, NULL, &tv); +} +#else +// UNIX +long long Time_GetTime(){ + struct timeval t; + long long usecs; + gettimeofday(&t,NULL); + usecs=(t.tv_sec*1000000ll)+(t.tv_usec); + return(usecs); +} +void Time_Pause(int pausa){ + struct timeval tv; + tv.tv_sec=(long long)pausa/1000000; + tv.tv_usec=(long long)pausa%1000000; + select(0, NULL, NULL, NULL, &tv); +} +#endif // if MACOSX +#endif // if WIN32 + + diff --git a/Time.h b/Time.h new file mode 100644 index 0000000..be19faf --- /dev/null +++ b/Time.h @@ -0,0 +1,17 @@ +#ifndef _TIME_H_ +#define _TIME_H_ + +///////////////////////////// +// Time_GetTime +// +// Gets the current time in usecs. +long long Time_GetTime(); + + +///////////////////////////// +// Time_Pause +// +// Pauses the execution for t usecs. +void Time_Pause(int pausa); + +#endif diff --git a/data/agua.bmp b/data/agua.bmp new file mode 100644 index 0000000..6960a10 Binary files /dev/null and b/data/agua.bmp differ diff --git a/data/bisho.bmp b/data/bisho.bmp new file mode 100644 index 0000000..6bc3171 Binary files /dev/null and b/data/bisho.bmp differ diff --git a/data/bisho_alpha.bmp b/data/bisho_alpha.bmp new file mode 100644 index 0000000..8782c7a Binary files /dev/null and b/data/bisho_alpha.bmp differ diff --git a/data/coin.wav b/data/coin.wav new file mode 100644 index 0000000..22d9ed0 Binary files /dev/null and b/data/coin.wav differ diff --git a/data/tierra.bmp b/data/tierra.bmp new file mode 100644 index 0000000..b0bbfae Binary files /dev/null and b/data/tierra.bmp differ diff --git a/data/tile0000.bmp b/data/tile0000.bmp new file mode 100644 index 0000000..8529e2f Binary files /dev/null and b/data/tile0000.bmp differ diff --git a/data/tile0001.bmp b/data/tile0001.bmp new file mode 100644 index 0000000..aca7cf0 Binary files /dev/null and b/data/tile0001.bmp differ diff --git a/data/tile0010.bmp b/data/tile0010.bmp new file mode 100644 index 0000000..9316c35 Binary files /dev/null and b/data/tile0010.bmp differ diff --git a/data/tile0011.bmp b/data/tile0011.bmp new file mode 100644 index 0000000..0af0a55 Binary files /dev/null and b/data/tile0011.bmp differ diff --git a/data/tile0100.bmp b/data/tile0100.bmp new file mode 100644 index 0000000..331b79f Binary files /dev/null and b/data/tile0100.bmp differ diff --git a/data/tile0101.bmp b/data/tile0101.bmp new file mode 100644 index 0000000..e1f7676 Binary files /dev/null and b/data/tile0101.bmp differ diff --git a/data/tile0110.bmp b/data/tile0110.bmp new file mode 100644 index 0000000..a8c1e02 Binary files /dev/null and b/data/tile0110.bmp differ diff --git a/data/tile0111.bmp b/data/tile0111.bmp new file mode 100644 index 0000000..6740d0c Binary files /dev/null and b/data/tile0111.bmp differ diff --git a/data/tile1000.bmp b/data/tile1000.bmp new file mode 100644 index 0000000..659475b Binary files /dev/null and b/data/tile1000.bmp differ diff --git a/data/tile1001.bmp b/data/tile1001.bmp new file mode 100644 index 0000000..bbf570f Binary files /dev/null and b/data/tile1001.bmp differ diff --git a/data/tile1010.bmp b/data/tile1010.bmp new file mode 100644 index 0000000..fbbd7e6 Binary files /dev/null and b/data/tile1010.bmp differ diff --git a/data/tile1011.bmp b/data/tile1011.bmp new file mode 100644 index 0000000..6cce6c0 Binary files /dev/null and b/data/tile1011.bmp differ diff --git a/data/tile1100.bmp b/data/tile1100.bmp new file mode 100644 index 0000000..3c883d2 Binary files /dev/null and b/data/tile1100.bmp differ diff --git a/data/tile1101.bmp b/data/tile1101.bmp new file mode 100644 index 0000000..20f2488 Binary files /dev/null and b/data/tile1101.bmp differ diff --git a/data/tile1110.bmp b/data/tile1110.bmp new file mode 100644 index 0000000..9df81d6 Binary files /dev/null and b/data/tile1110.bmp differ diff --git a/data/tile1111.bmp b/data/tile1111.bmp new file mode 100644 index 0000000..47e5bb5 Binary files /dev/null and b/data/tile1111.bmp differ diff --git a/macosx/.DS_Store b/macosx/.DS_Store new file mode 100644 index 0000000..6ac8ba5 Binary files /dev/null and b/macosx/.DS_Store differ diff --git a/macosx/SDLMain.h b/macosx/SDLMain.h new file mode 100644 index 0000000..c56d90c --- /dev/null +++ b/macosx/SDLMain.h @@ -0,0 +1,16 @@ +/* SDLMain.m - main entry point for our Cocoa-ized SDL app + Initial Version: Darrell Walisser + Non-NIB-Code & other changes: Max Horn + + Feel free to customize this file to suit your needs +*/ + +#ifndef _SDLMain_h_ +#define _SDLMain_h_ + +#import + +@interface SDLMain : NSObject +@end + +#endif /* _SDLMain_h_ */ diff --git a/macosx/SDLMain.m b/macosx/SDLMain.m new file mode 100644 index 0000000..150d552 --- /dev/null +++ b/macosx/SDLMain.m @@ -0,0 +1,381 @@ +/* SDLMain.m - main entry point for our Cocoa-ized SDL app + Initial Version: Darrell Walisser + Non-NIB-Code & other changes: Max Horn + + Feel free to customize this file to suit your needs +*/ + +#include +#include "SDLMain.h" +#include /* for MAXPATHLEN */ +#include + +/* For some reaon, Apple removed setAppleMenu from the headers in 10.4, + but the method still is there and works. To avoid warnings, we declare + it ourselves here. */ +@interface NSApplication(SDL_Missing_Methods) +- (void)setAppleMenu:(NSMenu *)menu; +@end + +/* Use this flag to determine whether we use SDLMain.nib or not */ +#define SDL_USE_NIB_FILE 0 + +/* Use this flag to determine whether we use CPS (docking) or not */ +#define SDL_USE_CPS 1 +#ifdef SDL_USE_CPS +/* Portions of CPS.h */ +typedef struct CPSProcessSerNum +{ + UInt32 lo; + UInt32 hi; +} CPSProcessSerNum; + +extern OSErr CPSGetCurrentProcess( CPSProcessSerNum *psn); +extern OSErr CPSEnableForegroundOperation( CPSProcessSerNum *psn, UInt32 _arg2, UInt32 _arg3, UInt32 _arg4, UInt32 _arg5); +extern OSErr CPSSetFrontProcess( CPSProcessSerNum *psn); + +#endif /* SDL_USE_CPS */ + +static int gArgc; +static char **gArgv; +static BOOL gFinderLaunch; +static BOOL gCalledAppMainline = FALSE; + +static NSString *getApplicationName(void) +{ + const NSDictionary *dict; + NSString *appName = 0; + + /* Determine the application name */ + dict = (const NSDictionary *)CFBundleGetInfoDictionary(CFBundleGetMainBundle()); + if (dict) + appName = [dict objectForKey: @"CFBundleName"]; + + if (![appName length]) + appName = [[NSProcessInfo processInfo] processName]; + + return appName; +} + +#if SDL_USE_NIB_FILE +/* A helper category for NSString */ +@interface NSString (ReplaceSubString) +- (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString; +@end +#endif + +@interface NSApplication (SDLApplication) +@end + +@implementation NSApplication (SDLApplication) +/* Invoked from the Quit menu item */ +- (void)terminate:(id)sender +{ + /* Post a SDL_QUIT event */ + SDL_Event event; + event.type = SDL_QUIT; + SDL_PushEvent(&event); +} +@end + +/* The main class of the application, the application's delegate */ +@implementation SDLMain + +/* Set the working directory to the .app's parent directory */ +- (void) setupWorkingDirectory:(BOOL)shouldChdir +{ + if (shouldChdir) + { + char parentdir[MAXPATHLEN]; + CFURLRef url = CFBundleCopyBundleURL(CFBundleGetMainBundle()); + CFURLRef url2 = CFURLCreateCopyDeletingLastPathComponent(0, url); + if (CFURLGetFileSystemRepresentation(url2, 1, (UInt8 *)parentdir, MAXPATHLEN)) { + chdir(parentdir); /* chdir to the binary app's parent */ + } + CFRelease(url); + CFRelease(url2); + } +} + +#if SDL_USE_NIB_FILE + +/* Fix menu to contain the real app name instead of "SDL App" */ +- (void)fixMenu:(NSMenu *)aMenu withAppName:(NSString *)appName +{ + NSRange aRange; + NSEnumerator *enumerator; + NSMenuItem *menuItem; + + aRange = [[aMenu title] rangeOfString:@"SDL App"]; + if (aRange.length != 0) + [aMenu setTitle: [[aMenu title] stringByReplacingRange:aRange with:appName]]; + + enumerator = [[aMenu itemArray] objectEnumerator]; + while ((menuItem = [enumerator nextObject])) + { + aRange = [[menuItem title] rangeOfString:@"SDL App"]; + if (aRange.length != 0) + [menuItem setTitle: [[menuItem title] stringByReplacingRange:aRange with:appName]]; + if ([menuItem hasSubmenu]) + [self fixMenu:[menuItem submenu] withAppName:appName]; + } +} + +#else + +static void setApplicationMenu(void) +{ + /* warning: this code is very odd */ + NSMenu *appleMenu; + NSMenuItem *menuItem; + NSString *title; + NSString *appName; + + appName = getApplicationName(); + appleMenu = [[NSMenu alloc] initWithTitle:@""]; + + /* Add menu items */ + title = [@"About " stringByAppendingString:appName]; + [appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""]; + + [appleMenu addItem:[NSMenuItem separatorItem]]; + + title = [@"Hide " stringByAppendingString:appName]; + [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"]; + + menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"]; + [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)]; + + [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""]; + + [appleMenu addItem:[NSMenuItem separatorItem]]; + + title = [@"Quit " stringByAppendingString:appName]; + [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"]; + + + /* Put menu into the menubar */ + menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""]; + [menuItem setSubmenu:appleMenu]; + [[NSApp mainMenu] addItem:menuItem]; + + /* Tell the application object that this is now the application menu */ + [NSApp setAppleMenu:appleMenu]; + + /* Finally give up our references to the objects */ + [appleMenu release]; + [menuItem release]; +} + +/* Create a window menu */ +static void setupWindowMenu(void) +{ + NSMenu *windowMenu; + NSMenuItem *windowMenuItem; + NSMenuItem *menuItem; + + windowMenu = [[NSMenu alloc] initWithTitle:@"Window"]; + + /* "Minimize" item */ + menuItem = [[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"]; + [windowMenu addItem:menuItem]; + [menuItem release]; + + /* Put menu into the menubar */ + windowMenuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""]; + [windowMenuItem setSubmenu:windowMenu]; + [[NSApp mainMenu] addItem:windowMenuItem]; + + /* Tell the application object that this is now the window menu */ + [NSApp setWindowsMenu:windowMenu]; + + /* Finally give up our references to the objects */ + [windowMenu release]; + [windowMenuItem release]; +} + +/* Replacement for NSApplicationMain */ +static void CustomApplicationMain (int argc, char **argv) +{ + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + SDLMain *sdlMain; + + /* Ensure the application object is initialised */ + [NSApplication sharedApplication]; + +#ifdef SDL_USE_CPS + { + CPSProcessSerNum PSN; + /* Tell the dock about us */ + if (!CPSGetCurrentProcess(&PSN)) + if (!CPSEnableForegroundOperation(&PSN,0x03,0x3C,0x2C,0x1103)) + if (!CPSSetFrontProcess(&PSN)) + [NSApplication sharedApplication]; + } +#endif /* SDL_USE_CPS */ + + /* Set up the menubar */ + [NSApp setMainMenu:[[NSMenu alloc] init]]; + setApplicationMenu(); + setupWindowMenu(); + + /* Create SDLMain and make it the app delegate */ + sdlMain = [[SDLMain alloc] init]; + [NSApp setDelegate:sdlMain]; + + /* Start the main event loop */ + [NSApp run]; + + [sdlMain release]; + [pool release]; +} + +#endif + + +/* + * Catch document open requests...this lets us notice files when the app + * was launched by double-clicking a document, or when a document was + * dragged/dropped on the app's icon. You need to have a + * CFBundleDocumentsType section in your Info.plist to get this message, + * apparently. + * + * Files are added to gArgv, so to the app, they'll look like command line + * arguments. Previously, apps launched from the finder had nothing but + * an argv[0]. + * + * This message may be received multiple times to open several docs on launch. + * + * This message is ignored once the app's mainline has been called. + */ +- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename +{ + const char *temparg; + size_t arglen; + char *arg; + char **newargv; + + if (!gFinderLaunch) /* MacOS is passing command line args. */ + return FALSE; + + if (gCalledAppMainline) /* app has started, ignore this document. */ + return FALSE; + + temparg = [filename UTF8String]; + arglen = SDL_strlen(temparg) + 1; + arg = (char *) SDL_malloc(arglen); + if (arg == NULL) + return FALSE; + + newargv = (char **) realloc(gArgv, sizeof (char *) * (gArgc + 2)); + if (newargv == NULL) + { + SDL_free(arg); + return FALSE; + } + gArgv = newargv; + + SDL_strlcpy(arg, temparg, arglen); + gArgv[gArgc++] = arg; + gArgv[gArgc] = NULL; + return TRUE; +} + + +/* Called when the internal event loop has just started running */ +- (void) applicationDidFinishLaunching: (NSNotification *) note +{ + int status; + + /* Set the working directory to the .app's parent directory */ + [self setupWorkingDirectory:gFinderLaunch]; + +#if SDL_USE_NIB_FILE + /* Set the main menu to contain the real app name instead of "SDL App" */ + [self fixMenu:[NSApp mainMenu] withAppName:getApplicationName()]; +#endif + + /* Hand off to main application code */ + gCalledAppMainline = TRUE; + status = SDL_main (gArgc, gArgv); + + /* We're done, thank you for playing */ + exit(status); +} +@end + + +@implementation NSString (ReplaceSubString) + +- (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString +{ + unsigned int bufferSize; + unsigned int selfLen = [self length]; + unsigned int aStringLen = [aString length]; + unichar *buffer; + NSRange localRange; + NSString *result; + + bufferSize = selfLen + aStringLen - aRange.length; + buffer = (unichar *)NSAllocateMemoryPages(bufferSize*sizeof(unichar)); + + /* Get first part into buffer */ + localRange.location = 0; + localRange.length = aRange.location; + [self getCharacters:buffer range:localRange]; + + /* Get middle part into buffer */ + localRange.location = 0; + localRange.length = aStringLen; + [aString getCharacters:(buffer+aRange.location) range:localRange]; + + /* Get last part into buffer */ + localRange.location = aRange.location + aRange.length; + localRange.length = selfLen - localRange.location; + [self getCharacters:(buffer+aRange.location+aStringLen) range:localRange]; + + /* Build output string */ + result = [NSString stringWithCharacters:buffer length:bufferSize]; + + NSDeallocateMemoryPages(buffer, bufferSize); + + return result; +} + +@end + + + +#ifdef main +# undef main +#endif + + +/* Main entry point to executable - should *not* be SDL_main! */ +int main (int argc, char **argv) +{ + /* Copy the arguments into a global variable */ + /* This is passed if we are launched by double-clicking */ + if ( argc >= 2 && strncmp (argv[1], "-psn", 4) == 0 ) { + gArgv = (char **) SDL_malloc(sizeof (char *) * 2); + gArgv[0] = argv[0]; + gArgv[1] = NULL; + gArgc = 1; + gFinderLaunch = YES; + } else { + int i; + gArgc = argc; + gArgv = (char **) SDL_malloc(sizeof (char *) * (argc+1)); + for (i = 0; i <= argc; i++) + gArgv[i] = argv[i]; + gFinderLaunch = NO; + } + +#if SDL_USE_NIB_FILE + NSApplicationMain (argc, argv); +#else + CustomApplicationMain (argc, argv); +#endif + return 0; +} + diff --git a/macosx/SDL_dev.zip b/macosx/SDL_dev.zip new file mode 100644 index 0000000..e860dfb Binary files /dev/null and b/macosx/SDL_dev.zip differ diff --git a/main.c b/main.c new file mode 100644 index 0000000..d5942a1 --- /dev/null +++ b/main.c @@ -0,0 +1,162 @@ +#include +#include +#include "GameLib.h" + + +#define MAP_ANCHO 11 +#define MAP_ALTO 9 + +DrawImg tiles[20]; +int mapa[MAP_ANCHO][MAP_ALTO]; + +// Dibujar el mapa +void DrawMapa(){ + int x; + int y; + int value; + + // dibujar el entorno + for(x=0;x0.7){ + mapa[x][y]=0; + }else{ + mapa[x][y]=1; + } + } + } + return 0; +} + +DrawFnt font; +DrawFnt fonts; +DrawImg bisho; +float xacel,yacel; +int xpos=0,ypos=0; +AudioSnd coin; +int t_start,t_end; +int frames=0; + +int ProcGame(){ + int i; + + //Draw_Clean(0,0,0); + DrawMapa(); + + if(Input_GetKey(InputKey_Up) && yacel>-10){ + yacel-=2; + } + if(Input_GetKey(InputKey_Down) && yacel<10){ + yacel+=2; + } + if(Input_GetKey(InputKey_Left) && xacel>-10){ + xacel-=2; + } + if(Input_GetKey(InputKey_Right) && xacel<10){ + xacel+=2; + } + if(xacel!=0){ + xpos+=xacel; + if(xacel>0){ + xacel-=1; + }else{ + xacel+=1; + } + } + if(yacel!=0){ + ypos+=yacel; + if(yacel>0){ + yacel-=1; + }else{ + yacel+=1; + } + } + Draw_DrawImgCenter(bisho,xpos,ypos); + Draw_DrawText(fonts,"BISHO!",xpos-15,ypos-23); + Draw_DrawText(font ,"BISHO!",xpos-16,ypos-24); + + + if(Input_GetKey(InputKey_Action1)==InputKey_Pressed){ + Audio_PlaySound(coin,1,1); + } + + /*for(i=0;i<1000;i++){ + Draw_DrawImgCenter(bisho,rand()%640,rand()%480); + }*/ + + Draw_DrawText(fonts,"Hola Mundo!",11,11); + Draw_DrawText(font ,"Hola Mundo!",10,10); + + frames++; + + return(1); +} + +int main(int argc,char *argv[]){ + + srand(time(NULL)); + + GameLib_Init(640,480,"Game",60); + + tiles[0] = Draw_LoadImage("data/tile0000.bmp"); + tiles[1] = Draw_LoadImage("data/tile0001.bmp"); + tiles[2] = Draw_LoadImage("data/tile0010.bmp"); + tiles[3] = Draw_LoadImage("data/tile0011.bmp"); + tiles[4] = Draw_LoadImage("data/tile0100.bmp"); + tiles[5] = Draw_LoadImage("data/tile0101.bmp"); + tiles[6] = Draw_LoadImage("data/tile0110.bmp"); + tiles[7] = Draw_LoadImage("data/tile0111.bmp"); + tiles[8] = Draw_LoadImage("data/tile1000.bmp"); + tiles[9] = Draw_LoadImage("data/tile1001.bmp"); + tiles[10] = Draw_LoadImage("data/tile1010.bmp"); + tiles[11] = Draw_LoadImage("data/tile1011.bmp"); + tiles[12] = Draw_LoadImage("data/tile1100.bmp"); + tiles[13] = Draw_LoadImage("data/tile1101.bmp"); + tiles[14] = Draw_LoadImage("data/tile1110.bmp"); + tiles[15] = Draw_LoadImage("data/tile1111.bmp"); + + font=Draw_DefaultFont(255,255,255,255); + fonts=Draw_DefaultFont(0,0,0,127); + + //bisho=Draw_LoadImage("data/bisho.bmp"); + //Draw_ImgSetKeyCol(bisho,0,255,0); + //Draw_ImgSetAlpha(bisho,127); + + bisho=Draw_LoadImage("data/bisho_alpha.bmp"); + Draw_ImgSetAlpha(bisho,255); + + xpos=320; + ypos=240; + + + coin=Audio_LoadSound("data/coin.wav"); + + + CrearMapa(); + + t_start=SDL_GetTicks(); + Draw_Loop(ProcGame); + t_end=SDL_GetTicks(); + printf("%d %d %.2f\n",t_end-t_start,frames,frames/((t_end-t_start)/1000.0f)); + + return(0); +}