4 // Floppy disk selector GUI
6 // © 2014 Underground Software
8 // JLH = James Hammons <jlhamm@acm.org>
11 // --- ---------- ------------------------------------------------------------
12 // JLH 10/13/2013 Created this file
18 #include "diskselector.h"
29 // Case insensitve string comparison voodoo
31 struct ci_char_traits : public std::char_traits<char>
33 static bool eq(char c1, char c2) { return toupper(c1) == toupper(c2); }
34 static bool ne(char c1, char c2) { return toupper(c1) != toupper(c2); }
35 static bool lt(char c1, char c2) { return toupper(c1) < toupper(c2); }
36 static int compare(const char * s1, const char * s2, size_t n)
40 if (toupper(*s1) < toupper(*s2)) return -1;
41 if (toupper(*s1) > toupper(*s2)) return 1;
46 static const char * find(const char * s, int n, char a)
48 while (n-- > 0 && toupper(*s) != toupper(a))
56 typedef std::basic_string<char, ci_char_traits> ci_string;
58 // END Case insensitve string comparison voodoo
62 static SDL_Texture * window = NULL;
63 static SDL_Texture * charStamp = NULL;
64 static uint32_t windowPixels[400 * 300];
65 static uint32_t stamp[FONT_WIDTH * FONT_HEIGHT];
66 bool DiskSelector::showWindow = false;
67 std::vector<ci_string> imageList;
70 void DiskSelector::Init(SDL_Renderer * renderer)
72 window = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ABGR8888,
73 SDL_TEXTUREACCESS_TARGET, 400, 300);
74 charStamp = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888,
75 SDL_TEXTUREACCESS_TARGET, FONT_WIDTH, FONT_HEIGHT);
79 WriteLog("GUI (DiskSelector): Could not create window!\n");
83 if (SDL_SetTextureBlendMode(window, SDL_BLENDMODE_BLEND) == -1)
84 WriteLog("GUI (DiskSelector): Could not set blend mode for window.\n");
86 if (SDL_SetTextureBlendMode(charStamp, SDL_BLENDMODE_BLEND) == -1)
87 WriteLog("GUI (DiskSelector): Could not set blend mode for charStamp.\n");
89 for(uint32_t i=0; i<400*300; i++)
90 windowPixels[i] = 0xEF00FF00;
92 SDL_UpdateTexture(window, NULL, windowPixels, 128 * sizeof(Uint32));
94 DrawFilenames(renderer);
98 void DiskSelector::FindDisks(const char * path)
100 DIR * dir = opendir(settings.disksPath);
104 WriteLog("GUI (DiskSelector)::FindDisks: Could not open directory \"%s\%!\n", settings.disksPath);
111 while ((ent = readdir(dir)) != NULL)
113 if (HasLegalExtension(ent->d_name))
114 imageList.push_back(ci_string(ent->d_name));
118 std::sort(imageList.begin(), imageList.end());
121 std::vector<ci_string>::iterator i;
122 for(i=imageList.begin(); i!=imageList.end(); i++)
123 printf("GUI::DS::Found \"%s\"\n", (*i).c_str());
129 bool DiskSelector::HasLegalExtension(const char * name)
131 const char * ext = strrchr(name, '.');
133 if ((strcasecmp(ext, ".dsk") == 0) || (strcasecmp(ext, ".do") == 0)
134 || (strcasecmp(ext, ".po") == 0) || (strcasecmp(ext, ".nib") == 0))
141 void DiskSelector::DrawFilenames(SDL_Renderer * renderer)
143 if (SDL_SetRenderTarget(renderer, window) < 0)
145 WriteLog("GUI: Could not set Render Target to overlay... (%s)\n", SDL_GetError());
149 // 3 columns of 16 chars apiece (with 8X16 font), 18 rows
150 // 3 columns of 18 chars apiece (with 7X12 font), 24 rows
151 // 3 columns of 21 chars apiece (with 6X11 font), 27 rows
155 while (count < imageList.size())
157 // int currentX = (count / 18) * 17;
158 // int currentY = (count % 18);
159 // int currentX = (count / 24) * 19;
160 // int currentY = (count % 24);
161 int currentX = (count / 27) * 22;
162 int currentY = (count % 27);
164 // for(unsigned int i=0; i<16; i++)
165 // for(unsigned int i=0; i<18; i++)
166 for(unsigned int i=0; i<21; i++)
168 if (i >= imageList[count].length())
171 DrawCharacter(renderer, currentX + i, currentY, imageList[count][i]);
176 // if (count >= (18 * 3))
177 // if (count >= (24 * 3))
178 if (count >= (27 * 3))
182 // Set render target back to default
183 SDL_SetRenderTarget(renderer, NULL);
187 void DiskSelector::DrawCharacter(SDL_Renderer * renderer, int x, int y, uint8_t c)
190 // uint32_t pixel = 0xFF7F0000;
191 uint8_t * ptr = (uint8_t *)&font2[(c - 0x20) * FONT_WIDTH * FONT_HEIGHT];
193 for(int j=0; j<FONT_HEIGHT; j++)
195 for(int i=0; i<FONT_WIDTH; i++)
197 SDL_SetRenderDrawColor(renderer, 0xFF, 0x7F, 0x00, ptr[(j * FONT_WIDTH) + i]);
198 SDL_RenderDrawPoint(renderer, (x * FONT_WIDTH) + i, (y * FONT_HEIGHT) + j);
202 SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00);
204 uint32_t pixel = 0xFFFFA000;
205 uint8_t * ptr = (uint8_t *)&font10pt[(c - 0x20) * FONT_WIDTH * FONT_HEIGHT];
207 dst.x = x * FONT_WIDTH, dst.y = y * FONT_HEIGHT, dst.w = FONT_WIDTH, dst.h = FONT_HEIGHT;
209 for(int i=0; i<FONT_WIDTH*FONT_HEIGHT; i++)
210 stamp[i] = pixel | ptr[i];
212 SDL_UpdateTexture(charStamp, NULL, stamp, FONT_WIDTH * sizeof(Uint32));
213 SDL_RenderCopy(renderer, charStamp, NULL, &dst);
218 void DiskSelector::()
223 void DiskSelector::MouseDown(int32_t x, int32_t y, uint32_t buttons)
228 void DiskSelector::MouseUp(int32_t x, int32_t y, uint32_t buttons)
233 void DiskSelector::MouseMove(int32_t x, int32_t y, uint32_t buttons)
238 void DiskSelector::Render(SDL_Renderer * renderer)
240 if (!(window || showWindow))
244 dst.x = (VIRTUAL_SCREEN_WIDTH - 400) / 2, dst.y = (VIRTUAL_SCREEN_HEIGHT - 300) / 2, dst.w = 400, dst.h = 300;
246 SDL_RenderCopy(renderer, window, NULL, &dst);