4 // Graphical User Interface support
16 // Private function prototypes
18 uint32 CountROMFiles(char * path);
30 // Render the backbuffer to the primary screen surface
32 void BlitBackbuffer(void)
34 extern SDL_Surface * surface, * mainSurface;
35 extern int16 * backbuffer;
37 if (SDL_MUSTLOCK(surface))
38 while (SDL_LockSurface(surface) < 0)
41 memcpy(surface->pixels, backbuffer, tom_getVideoModeWidth() * tom_getVideoModeHeight() * 2);
43 if (SDL_MUSTLOCK(surface))
44 SDL_UnlockSurface(surface);
46 SDL_Rect srcrect, dstrect;
47 srcrect.x = srcrect.y = 0, srcrect.w = surface->w, srcrect.h = surface->h;
48 dstrect.x = dstrect.y = 0, dstrect.w = surface->w, dstrect.h = surface->h;
49 SDL_BlitSurface(surface, &srcrect, mainSurface, &dstrect);
50 SDL_Flip(mainSurface);
54 // Draw text at the given x/y coordinates. Can invert text as well.
56 void DrawText(int16 * screen, uint32 x, uint32 y, bool invert, const char * text, ...)
62 vsprintf(string, text, arg);
65 uint32 pitch = TOMGetSDLScreenPitch() / 2; // Returns pitch in bytes but we need words...
66 uint32 length = strlen(string), address = x + (y * pitch);
68 for(uint32 i=0; i<length; i++)
70 uint32 fontAddr = (uint32)string[i] * 64;
72 for(uint32 yy=0; yy<8; yy++)
74 for(uint32 xx=0; xx<8; xx++)
76 if ((font1[fontAddr] && !invert) || (!font1[fontAddr] && invert))
77 *(screen + address + xx + (yy * pitch)) = 0xFE00;
87 // Very very crude GUI file selector
90 #define FILENAME_MAX 2048
92 bool UserSelectFile(char * path, char * filename)
94 extern int16 * backbuffer;
95 uint32 numFiles = CountROMFiles(path);
100 char * names = (char *)malloc(numFiles * FILENAME_MAX);
104 WriteLog("Could not allocate memory for %u files!\nAborting!\n", numFiles);
109 DIR * dp = opendir(path);
112 while ((de = readdir(dp)) != NULL)
114 char * ext = strrchr(de->d_name, '.');
117 if (stricmp(ext, ".zip") == 0 || stricmp(ext, ".jag") == 0)
119 // Do a QnD insertion sort...
120 // (Yeah, it's n^2/2 time, but there aren't that many items...)
123 for(int k=0; k<i; k++)
125 if (stricmp(&names[k * FILENAME_MAX], de->d_name) < 0)
131 uint32 blockSize = (i - pos) * FILENAME_MAX;
134 //This only works on Win32 for some reason...
135 // memcpy(&names[(pos + 1) * FILENAME_MAX], &names[pos * FILENAME_MAX], blockSize);
136 for(int k=blockSize-1; k>=0; k--)
137 names[((pos + 1) * FILENAME_MAX) + k] = names[(pos * FILENAME_MAX) + k];
139 strcpy(&names[pos * FILENAME_MAX], de->d_name);
147 // Main GUI selection loop
149 uint32 cursor = 0, startFile = 0;
151 if (numFiles > 1) // Only go GUI if more than one possibility!
154 uint32 limit = (numFiles > 24 ? 24 : numFiles);
159 while (SDL_PollEvent(&event))
162 memset(backbuffer, 0x11, tom_getVideoModeWidth() * tom_getVideoModeHeight() * 2);
164 for(uint32 i=0; i<limit; i++)
166 bool invert = (cursor == i ? true : false);
168 // Guarantee that we clip our strings to fit in the screen...
169 memcpy(buf, &names[(startFile + i) * FILENAME_MAX], 38);
171 DrawText(backbuffer, 0, i*8, invert, " %s ", buf);
176 if (event.type == SDL_KEYDOWN)
178 SDLKey key = event.key.keysym.sym;
180 if (key == SDLK_DOWN)
182 if (cursor != limit - 1) // Cursor is within its window
184 else // Otherwise, scroll the window...
186 if (cursor + startFile != numFiles - 1)
200 if (key == SDLK_PAGEDOWN)
203 if (key == SDLK_PAGEUP)
206 if (key == SDLK_RETURN)
208 if (key == SDLK_ESCAPE)
210 WriteLog("GUI: Aborting VJ by user request.\n");
211 return false; // Bail out!
213 if (key >= SDLK_a && key <= SDLK_z)
215 // Advance cursor to filename with first letter pressed...
216 uint8 which = (key - SDLK_a) + 65; // Convert key to A-Z char
217 for(uint32 i=0; i<numFiles; i++)
219 if ((names[i * FILENAME_MAX] & 0xDF) == which)
221 cursor = i - startFile;
222 if (i > startFile + limit - 1)
223 startFile = i - limit + 1,
237 strcpy(filename, path);
238 // Potential GPF here: If length of dir is zero, then this will cause a page fault!
239 if (path[strlen(path) - 1] != '/')
240 strcat(filename, "/");
241 strcat(filename, &names[(cursor + startFile) * FILENAME_MAX]);
248 // Count # of possible ROM files in the current directory
250 uint32 CountROMFiles(char * path)
253 DIR * dp = opendir(path);
257 WriteLog("VJ: Could not open directory \"%s\"!\nAborting!\n", path);
264 while ((de = readdir(dp)) != NULL)
266 char * ext = strrchr(de->d_name, '.');
268 if (stricmp(ext, ".zip") == 0 || stricmp(ext, ".jag") == 0)