1 /** EMULib Emulation Library *********************************/
2 /**                                                         **/
3 /**                        EMULib.c                         **/
4 /**                                                         **/
5 /** This file contains platform-independent implementation  **/
6 /** part of the emulation library.                          **/
7 /**                                                         **/
8 /** Copyright (C) Marat Fayzullin 1996-2009                 **/
9 /**     You are not allowed to distribute this software     **/
10 /**     commercially. Please, notify me, if you make any    **/
11 /**     changes to this file.                               **/
12 /*************************************************************/
13 #include "EMULib.h"
14 #include "Console.h"
15 #include <string.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <sys/stat.h>
20 
21 #if defined(WINDOWS) || defined(UNIX) || defined(MAEMO)
22 #define NewImage  GenericNewImage
23 #define FreeImage GenericFreeImage
24 #define CropImage GenericCropImage
25 extern Image BigScreen;
26 #endif
27 
28 #if defined(UNIX) || defined(MAEMO)
29 #define SetVideo  GenericSetVideo
30 #endif
31 
32 /** Current Video Image **************************************/
33 /** These parameters are set with SetVideo() and used by    **/
34 /** ShowVideo() to show a WxH fragment from <X,Y> of Img.   **/
35 /*************************************************************/
36 Image *VideoImg = 0;           /* Current ShowVideo() image  */
37 int VideoX;                    /* X for ShowVideo()          */
38 int VideoY;                    /* Y for ShowVideo()          */
39 int VideoW;                    /* Width for ShowVideo()      */
40 int VideoH;                    /* Height for ShowVideo()     */
41 
42 /** KeyHandler ***********************************************/
43 /** This function receives key presses and releases.        **/
44 /*************************************************************/
45 void (*KeyHandler)(unsigned int Key) = 0;
46 
47 /** MouseHandler *********************************************/
48 /** This function receives mouse clicks/unclicks.           **/
49 /*************************************************************/
50 void (*MouseHandler)(int X,int Y,int State) = 0;
51 
52 /** NewImage() ***********************************************/
53 /** Create a new image of the given size. Returns pointer   **/
54 /** to the image data on success, 0 on failure.             **/
55 /*************************************************************/
NewImage(Image * Img,int Width,int Height)56 pixel *NewImage(Image *Img,int Width,int Height)
57 {
58   Img->Data    = (pixel *)malloc(Width*Height*sizeof(pixel));
59   Img->Cropped = 0;
60 
61   if(!Img->Data) Img->W=Img->H=Img->L=Img->D=0;
62   else
63   {
64     memset(Img->Data,0,Width*Height*sizeof(pixel));
65     Img->D = sizeof(pixel)<<3;
66     Img->W = Width;
67     Img->H = Height;
68     Img->L = Width;
69   }
70 
71   return(Img->Data);
72 }
73 
74 /** FreeImage() **********************************************/
75 /** Free previously allocated image.                        **/
76 /*************************************************************/
FreeImage(Image * Img)77 void FreeImage(Image *Img)
78 {
79   /* If image is used for video, unselect it */
80   if(VideoImg==Img) VideoImg=0;
81   /* Image gone */
82   if(Img->Data&&!Img->Cropped) free(Img->Data);
83   Img->Data    = 0;
84   Img->Cropped = 0;
85   Img->W       = 0;
86   Img->H       = 0;
87   Img->L       = 0;
88   Img->D       = 0;
89 }
90 
91 /** CropImage() **********************************************/
92 /** Create a subimage Dst of the image Src. Returns Dst.    **/
93 /*************************************************************/
CropImage(Image * Dst,const Image * Src,int X,int Y,int W,int H)94 Image *CropImage(Image *Dst,const Image *Src,int X,int Y,int W,int H)
95 {
96   Dst->Data    = (void *)((char *)Src->Data+(Src->L*Y+X)*(Src->D>>3));
97   Dst->Cropped = 1;
98   Dst->W       = W;
99   Dst->H       = H;
100   Dst->L       = Src->L;
101   Dst->D       = Src->D;
102   return(Dst);
103 }
104 
105 /** SetVideo() ***********************************************/
106 /** Set part of the image as "active" for display.          **/
107 /*************************************************************/
SetVideo(Image * Img,int X,int Y,int W,int H)108 void SetVideo(Image *Img,int X,int Y,int W,int H)
109 {
110   VideoImg = Img;
111   VideoX   = X<0? 0:X>=Img->W? Img->W-1:X;
112   VideoY   = Y<0? 0:Y>=Img->H? Img->H-1:Y;
113   VideoW   = VideoX+W>Img->W? Img->W-VideoX:W;
114   VideoH   = VideoY+H>Img->H? Img->H-VideoY:H;
115 #ifdef WINDOWS
116   FreeImage(&BigScreen);
117 #endif
118 }
119 
120 /** WaitJoystick() *******************************************/
121 /** Wait until one or more of the given buttons have been   **/
122 /** pressed. Returns the bitmask of pressed buttons. Refer  **/
123 /** to BTN_* #defines for the button mappings.              **/
124 /*************************************************************/
WaitJoystick(unsigned int Mask)125 unsigned int WaitJoystick(unsigned int Mask)
126 {
127   unsigned int I;
128 
129 #if defined(UNIX) || defined(MAEMO) || defined(NXC2600) || defined(STMP3700)
130   /* Wait for all requested buttons to be released first */
131   while(GetJoystick()&Mask) usleep(100000);
132   /* Wait for any of the buttons to become pressed */
133   do { I=GetJoystick()&Mask;usleep(100000); } while(!I);
134 #else
135   /* Wait for all requested buttons to be released first */
136   while(GetJoystick()&Mask);
137   /* Wait for any of the buttons to become pressed */
138   do I=GetJoystick()&Mask; while(!I);
139 #endif
140 
141   /* Return pressed buttons */
142   return(I);
143 }
144 
145 /** SetKeyHandler() ******************************************/
146 /** Attach keyboard handler that will be called when a key  **/
147 /** is pressed or released.                                 **/
148 /*************************************************************/
SetKeyHandler(void (* Handler)(unsigned int Key))149 void SetKeyHandler(void (*Handler)(unsigned int Key))
150 {
151   KeyHandler=Handler;
152 }
153 
154 /** SetMouseHandler() ****************************************/
155 /** Attach mouse handler that will be called when the       **/
156 /** mouse button is clicked or released.                    **/
157 /*************************************************************/
SetMouseHandler(void (* Handler)(int X,int Y,int State))158 void SetMouseHandler(void (*Handler)(int X, int Y, int State))
159 {
160   MouseHandler=Handler;
161 }
162 
163 /** GetFilePath() ********************************************/
164 /** Extracts pathname from filename and returns a pointer   **/
165 /** to the internal buffer containing just the path name    **/
166 /** ending with "\".                                        **/
167 /*************************************************************/
GetFilePath(const char * Name)168 const char *GetFilePath(const char *Name)
169 {
170   static char Path[256];
171   const char *P;
172   char *T;
173 
174   P=strrchr(Name,'\\');
175 
176   /* If path not found or too long, assume current */
177   if(!P||(P-Name>200)) { strcpy(Path,"");return(Path); }
178 
179   /* Copy and return the pathname */
180   for(T=Path;Name<P;*T++=*Name++);
181   *T='\0';return(Path);
182 }
183 
184 /** NewFile() ************************************************/
185 /** Given pattern NAME.EXT, generates a new filename in the **/
186 /** NAMEnnnn.EXT (nnnn = 0000..9999) format and returns a   **/
187 /** pointer to the internal buffer containing new filename. **/
188 /*************************************************************/
NewFile(const char * Pattern)189 const char *NewFile(const char *Pattern)
190 {
191   static char Name[256];
192   struct stat FInfo;
193   const char *P;
194   char S[256],*T;
195   int J;
196 
197   /* If too long name, fall out */
198   if(strlen(Pattern)>200) { strcpy(Name,"");return(Name); }
199 
200   /* Make up the format string */
201   for(T=S,P=Pattern;*P&&(*P!='.');) *T++=*P++;
202   *T='\0';
203   strcat(S,"%04d");
204   strcat(S,P);
205 
206   /* Scan through the filenames */
207   for(J=0;J<10000;J++)
208   {
209     sprintf(Name,S,J);
210     if(stat(Name,&FInfo)) break;
211   }
212 
213   if(J==10000) strcpy(Name,"");
214   return(Name);
215 }
216