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
89 bool UserSelectFile(char * path, char * filename)
91 extern int16 * backbuffer;
92 uint32 numFiles = CountROMFiles(path);
97 char * names = (char *)malloc(numFiles * 2048);
101 WriteLog("Could not allocate memory for %u files!\nAborting!\n", numFiles);
106 DIR * dp = opendir(path);
109 while ((de = readdir(dp)) != NULL)
111 char * ext = strrchr(de->d_name, '.');
113 if (strcmpi(ext, ".zip") == 0 || strcmpi(ext, ".jag") == 0)
114 strcpy(&names[i++ * 2048], de->d_name);
119 // Main GUI selection loop
121 uint32 cursor = 0, startFile = 0;
123 if (numFiles > 1) // Only go GUI if more than one possibility!
126 uint32 limit = (numFiles > 24 ? 24 : numFiles);
131 while (SDL_PollEvent(&event))
134 memset(backbuffer, 0x11, tom_getVideoModeWidth() * tom_getVideoModeHeight() * 2);
136 for(uint32 i=0; i<limit; i++)
138 bool invert = (cursor == i ? true : false);
140 // Guarantee that we clip our strings to fit in the screen...
141 memcpy(buf, &names[(startFile + i) * 2048], 38);
143 DrawText(backbuffer, 0, i*8, invert, " %s ", buf);
148 if (event.type == SDL_KEYDOWN)
150 SDLKey key = event.key.keysym.sym;
152 if (key == SDLK_DOWN)
154 if (cursor != limit - 1) // Cursor is within its window
156 else // Otherwise, scroll the window...
158 if (cursor + startFile != numFiles - 1)
172 if (key == SDLK_RETURN)
174 if (key == SDLK_ESCAPE)
176 WriteLog("GUI: Aborting VJ by user request.\n");
177 return false; // Bail out!
184 strcpy(filename, path);
185 // Potential GPF here: If length of dir is zero, then this will cause a page fault!
186 if (path[strlen(path) - 1] != '/')
187 strcat(filename, "/");
188 strcat(filename, &names[(cursor + startFile) * 2048]);
195 // Count # of possible ROM files in the current directory
197 uint32 CountROMFiles(char * path)
200 DIR * dp = opendir(path);
204 WriteLog("VJ: Could not open directory \"%s\"!\nAborting!\n", path);
211 while ((de = readdir(dp)) != NULL)
213 char * ext = strrchr(de->d_name, '.');
215 if (strcmpi(ext, ".zip") == 0 || strcmpi(ext, ".jag") == 0)