]> Shamusworld >> Repos - apple2/blob - src/gui/diskselector.cpp
Removed GUI cruft, added 10 & 12pt fonts.
[apple2] / src / gui / diskselector.cpp
1 //
2 // diskselector.cpp
3 //
4 // Floppy disk selector GUI
5 // by James Hammons
6 // © 2014 Underground Software
7 //
8 // JLH = James Hammons <jlhamm@acm.org>
9 //
10 // WHO  WHEN        WHAT
11 // ---  ----------  ------------------------------------------------------------
12 // JLH  10/13/2013  Created this file
13 //
14 // STILL TO DO:
15 //
16 //
17
18 #include "diskselector.h"
19 #include <dirent.h>
20 #include <algorithm>
21 #include <string>
22 #include <vector>
23 #include "font10pt.h"
24 #include "log.h"
25 #include "settings.h"
26 #include "video.h"
27
28 //
29 // Case insensitve string comparison voodoo
30 //
31 struct ci_char_traits : public std::char_traits<char>
32 {
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)
37         {
38                 while (n-- != 0)
39                 {
40                         if (toupper(*s1) < toupper(*s2)) return -1;
41                         if (toupper(*s1) > toupper(*s2)) return 1;
42                         ++s1; ++s2;
43                 }
44                 return 0;
45         }
46         static const char * find(const char * s, int n, char a)
47         {
48                 while (n-- > 0 && toupper(*s) != toupper(a))
49                 {
50                         ++s;
51                 }
52                 return s;
53         }
54 };
55
56 typedef std::basic_string<char, ci_char_traits> ci_string;
57 //
58 // END Case insensitve string comparison voodoo
59 //
60
61
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;
68
69
70 void DiskSelector::Init(SDL_Renderer * renderer)
71 {
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);
76
77         if (!window)
78         {
79                 WriteLog("GUI (DiskSelector): Could not create window!\n");
80                 return;
81         }
82
83         if (SDL_SetTextureBlendMode(window, SDL_BLENDMODE_BLEND) == -1)
84                 WriteLog("GUI (DiskSelector): Could not set blend mode for window.\n");
85
86         if (SDL_SetTextureBlendMode(charStamp, SDL_BLENDMODE_BLEND) == -1)
87                 WriteLog("GUI (DiskSelector): Could not set blend mode for charStamp.\n");
88
89         for(uint32_t i=0; i<400*300; i++)
90                 windowPixels[i] = 0xEF00FF00;
91
92         SDL_UpdateTexture(window, NULL, windowPixels, 128 * sizeof(Uint32));
93         FindDisks(NULL);
94         DrawFilenames(renderer);
95 }
96
97
98 void DiskSelector::FindDisks(const char * path)
99 {
100         DIR * dir = opendir(settings.disksPath);
101
102         if (!dir)
103         {
104                 WriteLog("GUI (DiskSelector)::FindDisks: Could not open directory \"%s\%!\n", settings.disksPath);
105                 return;
106         }
107
108         imageList.clear();
109         dirent * ent;
110
111         while ((ent = readdir(dir)) != NULL)
112         {
113                 if (HasLegalExtension(ent->d_name))
114                         imageList.push_back(ci_string(ent->d_name));
115         }
116
117         closedir(dir);
118         std::sort(imageList.begin(), imageList.end());
119 #if 0
120 {
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());
124 }
125 #endif
126 }
127
128
129 bool DiskSelector::HasLegalExtension(const char * name)
130 {
131         const char * ext = strrchr(name, '.');
132
133         if ((strcasecmp(ext, ".dsk") == 0) || (strcasecmp(ext, ".do") == 0)
134                 || (strcasecmp(ext, ".po") == 0) || (strcasecmp(ext, ".nib") == 0))
135                 return true;
136
137         return false;
138 }
139
140
141 void DiskSelector::DrawFilenames(SDL_Renderer * renderer)
142 {
143         if (SDL_SetRenderTarget(renderer, window) < 0)
144         {
145                 WriteLog("GUI: Could not set Render Target to overlay... (%s)\n", SDL_GetError());
146                 return;
147         }
148
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
152
153         int count = 0;
154
155         while (count < imageList.size())
156         {
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);
163
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++)
167                 {
168                         if (i >= imageList[count].length())
169                                 break;
170
171                         DrawCharacter(renderer, currentX + i, currentY, imageList[count][i]);
172                 }
173
174                 count++;
175
176 //              if (count >= (18 * 3))
177 //              if (count >= (24 * 3))
178                 if (count >= (27 * 3))
179                         break;
180         }
181
182         // Set render target back to default
183         SDL_SetRenderTarget(renderer, NULL);
184 }
185
186
187 void DiskSelector::DrawCharacter(SDL_Renderer * renderer, int x, int y, uint8_t c)
188 {
189 #if 0
190 //      uint32_t pixel = 0xFF7F0000;
191         uint8_t * ptr = (uint8_t *)&font2[(c - 0x20) * FONT_WIDTH * FONT_HEIGHT];
192
193         for(int j=0; j<FONT_HEIGHT; j++)
194         {
195                 for(int i=0; i<FONT_WIDTH; i++)
196                 {
197                         SDL_SetRenderDrawColor(renderer, 0xFF, 0x7F, 0x00, ptr[(j * FONT_WIDTH) + i]);
198                         SDL_RenderDrawPoint(renderer, (x * FONT_WIDTH) + i, (y * FONT_HEIGHT) + j);
199                 }
200         }
201
202         SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00);
203 #else
204         uint32_t pixel = 0xFFFFA000;
205         uint8_t * ptr = (uint8_t *)&font10pt[(c - 0x20) * FONT_WIDTH * FONT_HEIGHT];
206         SDL_Rect dst;
207         dst.x = x * FONT_WIDTH, dst.y = y * FONT_HEIGHT, dst.w = FONT_WIDTH, dst.h = FONT_HEIGHT;
208
209         for(int i=0; i<FONT_WIDTH*FONT_HEIGHT; i++)
210                 stamp[i] = pixel | ptr[i];
211
212         SDL_UpdateTexture(charStamp, NULL, stamp, FONT_WIDTH * sizeof(Uint32));
213         SDL_RenderCopy(renderer, charStamp, NULL, &dst);
214 #endif
215 }
216
217 /*
218 void DiskSelector::()
219 {
220 }
221 */
222
223 void DiskSelector::MouseDown(int32_t x, int32_t y, uint32_t buttons)
224 {
225 }
226
227
228 void DiskSelector::MouseUp(int32_t x, int32_t y, uint32_t buttons)
229 {
230 }
231
232
233 void DiskSelector::MouseMove(int32_t x, int32_t y, uint32_t buttons)
234 {
235 }
236
237
238 void DiskSelector::Render(SDL_Renderer * renderer)
239 {
240         if (!(window || showWindow))
241                 return;
242
243         SDL_Rect dst;
244         dst.x = (VIRTUAL_SCREEN_WIDTH - 400) / 2, dst.y = (VIRTUAL_SCREEN_HEIGHT - 300) / 2, dst.w = 400, dst.h = 300;
245
246         SDL_RenderCopy(renderer, window, NULL, &dst);
247 }
248