4 // Floppy disk selector GUI
7 // JLH = James Hammons <jlhamm@acm.org>
10 // --- ---------- ------------------------------------------------------------
11 // JLH 10/13/2013 Created this file
17 #include "diskselector.h"
28 // Case insensitve string comparison voodoo
30 struct ci_char_traits : public std::char_traits<char>
32 static bool eq(char c1, char c2) { return toupper(c1) == toupper(c2); }
33 static bool ne(char c1, char c2) { return toupper(c1) != toupper(c2); }
34 static bool lt(char c1, char c2) { return toupper(c1) < toupper(c2); }
35 static int compare(const char * s1, const char * s2, size_t n)
39 if (toupper(*s1) < toupper(*s2)) return -1;
40 if (toupper(*s1) > toupper(*s2)) return 1;
45 static const char * find(const char * s, int n, char a)
47 while (n-- > 0 && toupper(*s) != toupper(a))
55 typedef std::basic_string<char, ci_char_traits> ci_string;
57 // END Case insensitve string comparison voodoo
61 static SDL_Texture * window = NULL;
62 static SDL_Texture * charStamp = NULL;
63 static uint32_t windowPixels[400 * 300];
64 static uint32_t stamp[FONT_WIDTH * FONT_HEIGHT];
65 bool DiskSelector::showWindow = false;
66 std::vector<ci_string> imageList;
69 void DiskSelector::Init(SDL_Renderer * renderer)
71 window = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ABGR8888,
72 SDL_TEXTUREACCESS_TARGET, 400, 300);
73 // charStamp = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ABGR8888,
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] = 0xB000FF00;
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
153 while (count < imageList.size())
155 int currentX = (count / 18) * 17;
156 int currentY = (count % 18);
158 for(unsigned int i=0; i<16; i++)
160 if (i >= imageList[count].length())
163 DrawCharacter(renderer, currentX + i, currentY, imageList[count][i]);
168 if (count >= (18 * 3))
172 // Set render target back to default
173 SDL_SetRenderTarget(renderer, NULL);
177 void DiskSelector::DrawCharacter(SDL_Renderer * renderer, int x, int y, uint8_t c)
180 // uint32_t pixel = 0xFF7F0000;
181 uint8_t * ptr = (uint8_t *)&font2[(c - 0x20) * FONT_WIDTH * FONT_HEIGHT];
183 for(int j=0; j<FONT_HEIGHT; j++)
185 for(int i=0; i<FONT_WIDTH; i++)
187 SDL_SetRenderDrawColor(renderer, 0xFF, 0x7F, 0x00, ptr[(j * FONT_WIDTH) + i]);
188 SDL_RenderDrawPoint(renderer, (x * FONT_WIDTH) + i, (y * FONT_HEIGHT) + j);
192 SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00);
194 uint32_t pixel = 0xFFCFA000;
195 uint8_t * ptr = (uint8_t *)&font2[(c - 0x20) * FONT_WIDTH * FONT_HEIGHT];
197 dst.x = x * FONT_WIDTH, dst.y = y * FONT_HEIGHT, dst.w = FONT_WIDTH, dst.h = FONT_HEIGHT;
199 for(int i=0; i<FONT_WIDTH*FONT_HEIGHT; i++)
200 stamp[i] = pixel | ptr[i];
202 SDL_UpdateTexture(charStamp, NULL, stamp, FONT_WIDTH * sizeof(Uint32));
203 SDL_RenderCopy(renderer, charStamp, NULL, &dst);
208 void DiskSelector::()
213 void DiskSelector::MouseDown(int32_t x, int32_t y, uint32_t buttons)
218 void DiskSelector::MouseUp(int32_t x, int32_t y, uint32_t buttons)
223 void DiskSelector::MouseMove(int32_t x, int32_t y, uint32_t buttons)
228 void DiskSelector::Render(SDL_Renderer * renderer)
230 if (!(window || showWindow))
234 dst.x = (VIRTUAL_SCREEN_WIDTH - 400) / 2, dst.y = (VIRTUAL_SCREEN_HEIGHT - 300) / 2, dst.w = 400, dst.h = 300;
236 SDL_RenderCopy(renderer, window, NULL, &dst);