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-2014 **/
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 <string.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <sys/stat.h>
18
19 #ifndef _MSC_VER
20 #include <unistd.h>
21 #endif
22
23 #if defined(WINDOWS) || defined(UNIX) || defined(MAEMO) || defined(MEEGO) || defined(ANDROID)
24 #define NewImage GenericNewImage
25 #endif
26
27 #if defined(WINDOWS) || defined(UNIX) || defined(MAEMO) || defined(MEEGO)
28 #define FreeImage GenericFreeImage
29 #define CropImage GenericCropImage
30 extern Image BigScreen;
31 #endif
32
33 #if defined(UNIX) || defined(MAEMO) || defined(MEEGO) || defined(ANDROID)
34 #define SetVideo GenericSetVideo
35 #endif
36
37 /** Current Video Image **************************************/
38 /** These parameters are set with SetVideo() and used by **/
39 /** ShowVideo() to show a WxH fragment from <X,Y> of Img. **/
40 /*************************************************************/
41 Image *VideoImg = 0; /* Current ShowVideo() image */
42 int VideoX; /* X for ShowVideo() */
43 int VideoY; /* Y for ShowVideo() */
44 int VideoW; /* Width for ShowVideo() */
45 int VideoH; /* Height for ShowVideo() */
46
47 /** KeyHandler ***********************************************/
48 /** This function receives key presses and releases. **/
49 /*************************************************************/
50 void (*KeyHandler)(unsigned int Key) = 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 return 0;
128 }
129
130 /** SetKeyHandler() ******************************************/
131 /** Attach keyboard handler that will be called when a key **/
132 /** is pressed or released. **/
133 /*************************************************************/
SetKeyHandler(void (* Handler)(unsigned int Key))134 void SetKeyHandler(void (*Handler)(unsigned int Key))
135 {
136 KeyHandler=Handler;
137 }
138
139 /** GetFilePath() ********************************************/
140 /** Extracts pathname from filename and returns a pointer **/
141 /** to the internal buffer containing just the path name **/
142 /** ending with "\". **/
143 /*************************************************************/
GetFilePath(const char * Name)144 const char *GetFilePath(const char *Name)
145 {
146 static char Path[256];
147 const char *P;
148 char *T;
149
150 P=strrchr(Name,'\\');
151
152 /* If path not found or too long, assume current */
153 if(!P||(P-Name>200)) { strcpy(Path,"");return(Path); }
154
155 /* Copy and return the pathname */
156 for(T=Path;Name<P;*T++=*Name++);
157 *T='\0';return(Path);
158 }
159
160 /** NewFile() ************************************************/
161 /** Given pattern NAME.EXT, generates a new filename in the **/
162 /** NAMEnnnn.EXT (nnnn = 0000..9999) format and returns a **/
163 /** pointer to the internal buffer containing new filename. **/
164 /*************************************************************/
NewFile(const char * Pattern)165 const char *NewFile(const char *Pattern)
166 {
167 static char Name[256];
168 struct stat FInfo;
169 const char *P;
170 char S[256],*T;
171 int J;
172
173 /* If too long name, fall out */
174 if(strlen(Pattern)>200) { strcpy(Name,"");return(Name); }
175
176 /* Make up the format string */
177 for(T=S,P=Pattern;*P&&(*P!='.');) *T++=*P++;
178 *T='\0';
179 strcat(S,"%04d");
180 strcat(S,P);
181
182 /* Scan through the filenames */
183 for(J=0;J<10000;J++)
184 {
185 sprintf(Name,S,J);
186 if(stat(Name,&FInfo)) break;
187 }
188
189 if(J==10000) strcpy(Name,"");
190 return(Name);
191 }
192