Debian Codegraph @

src/main-sdl.c

raw symbols SCIP
1 /**
2 * \file main-sdl.c
3 * \brief Angband SDL port
4 *
5 * Copyright (c) 2007 Ben Harrison, Gregory Velichansky, Eric Stevens,
6 * Leon Marrick, Iain McFall, and others
7 *
8 * This work is free software; you can redistribute it and/or modify it
9 * under the terms of either:
10 *
11 * a) the GNU General Public License as published by the Free Software
12 * Foundation, version 2, or
13 *
14 * b) the "Angband licence":
15 * This software may be copied and distributed for educational, research,
16 * and not for profit purposes provided that this copyright and statement
17 * are included in all such copies. Other copyrights may also apply.
18 */
19 #include "angband.h"
20 #include "buildid.h"
21 #include "cmds.h"
22 #include "game-input.h"
23 #include "game-world.h"
24 #include "init.h"
25 #include "ui-command.h"
26 #include "ui-display.h"
27 #include "ui-game.h"
28 #include "ui-input.h"
29 #include "ui-map.h"
30 #include "ui-output.h"
31 #include "ui-prefs.h"
32
33 /**
34 * Comments and suggestions are welcome. The UI probably needs some
35 * adjustment, and I need comments from you.
36 * perhaps also something like "Angband 3.0.8 by Andi Sidwell and others;
37 * SDL port by Iain McFall an others, please see the accompanying documentation
38 * for credits" or something
39 */
40
41 /**
42 * This file helps Angband work with at least some computers capable of running
43 * SDL, a set of simple and quite popular game development libraries that work
44 * on many different operating systems, including Windows, most flavours of
45 * UNIX or similar, and Mac OS X. It requires a 32-bit (or higher) machine
46 * capable of displaying at least 640x480 in 256 colors. A Pentium or better
47 * is strongly recommended (for speed reasons treated more fully below).
48 *
49 * To use this file, use an appropriate "Makefile" or "Project File", install
50 * the required libraries (described below), make sure that "USE_SDL" is
51 * defined somewhere, and obtain various extra files (described below). If
52 * you are new to all this, read "makefile.sdl".
53 *
54 * This port uses the following libraries: SDL (v1.2+) and SDL_ttf.
55 * All are available as source code, pre-compiled libs for developers,
56 * and libs (or dlls) for players from www.libsdl.org
57 *
58 *
59 * Other files used by this port:
60 * - The game must have a collection of bitmap .fon files in /lib/fonts.
61 *
62 * - It also needs some .png graphics files in /lib/tiles,
63 * such as "16x16.png" and "16x16m.bmp".
64 *
65 * - The "lib/customize/pref-sdl.prf" file contains keymaps, macro definitions,
66 * and/or color redefinitions.
67 * - The "lib/customize/font-sdl.prf" contains attr/char mappings for use with
68 * the normal "*.fon" font files in the "lib/fonts/" directory.
69 *
70 *
71 *
72 * "Term" framework by Ben Harrison ([email protected]).
73 *
74 * Original Sangband SDL port and the "intrface" module by Leon Marrick
75 * (www.runegold.org/sangband).
76 *
77 * Additional helpful ideas by:
78 * 2001 Gregory Velichansky <[email protected]>, creator of the first Angband SDL
79 * port.
80 * 2006 Eric Stevens <[email protected]>, main author of the TOME SDL port.
81 */
82
83 /**
84 * Comments on using SDL with Angband:
85 *
86 * The good news:
87 * - SDL is cross-platform. Really. No joke. If this port doesn't work on
88 * your system, it probably isn't SDL's fault.
89 * - SDL is relatively easy to use, allowing you you to cobble up feature-rich
90 * apps that look half-way decent without too much fuss and bother. It's
91 * wonderful for prototyping.
92 * - SDL does most of what *band developers are likely to need, plus a whole
93 * lot more we haven't realized we want yet.
94 * - SDL is a cleanly written open-source API; it is much less painful to get
95 * the straight word then it is with most other libraries. Also, the SDL
96 * community offers active discussion boards, solid documentation, and
97 * numerous code examples relating to almost any question you might have
98 * occasion to ask.
99 *
100 * The bad news:
101 * - SDL can be tedious to install. Each individual library is straightforward
102 * enough, but *band development work requires several, the number growing as
103 * you get more sophisticated.
104 * - SDL (as a stand-alone lib, without the assistance of OpenGL) can be very
105 * sluggish if you aren't careful. It is poor at detecting, let alone making
106 * fullest use of, available video hardware, which cripples speed. So,
107 * getting half-way decent performance in a game like Angband takes some
108 * skill and vast amounts of effort. Speed - the lack thereof - is this
109 * port's biggest problem. More comments below.
110 * - SDL is not a complete game development library (although the add-ons help
111 * tremendously). Much-needed functionality - text output, blit stretching,
112 * and video refresh synching being three examples - were either missing
113 * altogether or covered by functions that proved too slow or delicate for
114 * production use. I ended up having to spend at least as much time, and
115 * write at least as much low-level code, as I did using the Win32 API.
116 * - SDL, like Allegro but to a lesser extent, is falling behind current tech-
117 * nology. Development progresses, but obsolescence looms, especially on
118 * Windows machines.
119 */
120
121 #ifdef USE_SDL
122
123 #include "main.h"
124 #include "SDL.h"
125 #include "SDL_ttf.h"
126 #include "SDL_image.h"
127
128 /* SDL flags used for the main window surface */
129 static Uint32 vflags = SDL_ANYFORMAT;
130
131 /* Current screen dimensions */
132 static int screen_w = 800;
133 static int screen_h = 600;
134
135 /* Fullscreen dimensions */
136 static int full_w;
137 static int full_h;
138
139 /* Want fullscreen? */
140 static bool fullscreen = false;
141
142 static int overdraw = 0;
143 static int overdraw_max = 0;
144
145 static char *sdl_settings_file;
146
147 /* Default point size for scalable fonts */
148 #define DEFAULT_POINT_SIZE (10)
149 /* Minimum allowed point size for scalable fonts */
150 #define MIN_POINT_SIZE (4)
151 /* Maximum allowed point size for scalable fonts */
152 #define MAX_POINT_SIZE (64)
153
154 #define MAX_FONTS 40
155 char *FontList[MAX_FONTS];
156 static int num_fonts = 0;
157
158 /* Holds details about requested properties for a terminal's font. */
159 typedef struct term_font term_font;
160 struct term_font
161 {
162 char *name; /* final component of path if one of the preset fonts;
163 full path if not a preset font */
164 int size; /* requested point size for the file; zero for
165 bitmapped fonts */
166 bool preset; /* true if this is a font included in the lib/fonts
167 directory for the game */
168 bool bitmapped; /* true if this is a bitmapped (.fon; case-insensitive)
169 font that can't be scaled */
170 };
171
172 /**
173 * Used as 'system' font.
174 */
175 static const term_font default_term_font = { "6x10x.fon", 0, true, true };
176
177 /**
178 * Used by the 'Point Size' and 'Font Browser' panels to accumulate
179 * information about a new requested font.
180 */
181 static term_font new_font = { NULL, 0, false, false };
182
183 /**
184 * A font structure
185 * Note that the data is only valid for a surface with matching
186 * values for pitch & bpp. If a surface is resized the data _must_ be
187 * recalculated.
188 */
189 typedef struct sdl_Font sdl_Font;
190 struct sdl_Font
191 {
192 int width; /* The dimensions of this font (in pixels)*/
193 int height;
194
195 char name[32]; /* Label in menu used to select the font */
196
197 Uint16 pitch; /* Pitch of the surface this font is made for */
198 Uint8 bpp; /* Bytes per pixel of the surface */
199 Uint8 something; /* Padding */
200
201 const term_font *req; /* Backreference to the request for this font */
202 int *data; /* The data */
203 TTF_Font *sdl_font; /* The native font */
204 };
205
206 static sdl_Font SystemFont;
207
208 /*
209 * Window information
210 * Each window has its own surface and coordinates
211 */
212 typedef struct term_window term_window;
213 struct term_window
214 {
215 term term_data;
216
217 SDL_Surface *surface; /* The surface for this window */
218 SDL_Surface *tiles; /* The appropriately sized tiles for this window */
219 SDL_Surface *onebyone; /* The appropriately sized tiles for this window */
220 uint8_t Term_idx; /* Index of term that relates to this */
221
222 int top; /* Window Coordinates on the main screen */
223 int left;
224
225 int keys; /* Size of keypress storage */
226
227 sdl_Font font; /* Font info */
228 term_font req_font; /* Requested font */
229 int rows; /* Dimension in tiles */
230 int cols;
231
232 int border; /* Border width */
233 int title_height; /* Height of title bar */
234
235 int width; /* Dimension in pixels ==
236 * tile_wid * cols + 2 x borde r*/
237 int height;
238
239 int tile_wid; /* Size in pixels of a char */
240 int tile_hgt;
241
242 bool visible; /* Can we see this window? */
243
244 SDL_Rect uRect; /* The part that needs to be updated */
245 };
246
247
248
249
250 typedef struct mouse_info mouse_info;
251 struct mouse_info
252 {
253 int left; /* Is it pressed? */
254 int right;
255
256 int leftx; /* _IF_ left button is pressed these */
257 int lefty; /* show where it was pressed */
258
259 int rightx;
260 int righty;
261
262 int x; /* Current position of mouse */
263 int y;
264
265 };
266
267 #define WINDOW_DRAW (SDL_USEREVENT + 1)
268
269 /**
270 * The basic angband text colours in an sdl friendly form
271 */
272 static SDL_Color text_colours[MAX_COLORS];
273
274 SDL_Color back_colour; /* Background colour */
275 Uint32 back_pixel_colour;
276 /* Default color for button captions */
277 SDL_Color DefaultCapColour = { 0, 0, 0 };
278
279 typedef struct sdl_ButtonBank sdl_ButtonBank;
280 typedef struct sdl_Button sdl_Button;
281 typedef struct sdl_Window sdl_Window;
282
283 typedef void (*button_press_func)(sdl_Button *sender);
284 struct sdl_Button
285 {
286 SDL_Rect pos; /* Position & Size */
287 bool selected; /* Selected? */
288 bool visible; /* Visible? */
289 button_press_func activate; /* A function to call when pressed */
290 sdl_ButtonBank *owner; /* Which bank is this in? */
291 char caption[50]; /* Text for this button */
292 SDL_Color unsel_colour; /* Button unselected colour */
293 SDL_Color sel_colour; /* Selected colour*/
294 SDL_Color cap_colour; /* Caption colour */
295 void *data; /* Something */
296 int tag; /* Something */
297
298 };
299
300 struct sdl_ButtonBank
301 {
302 sdl_Button *buttons; /* A collection of buttons */
303 bool *used; /* What buttons are available? */
304 sdl_Window *window; /* The window that these buttons are on */
305 bool need_update;
306 };
307
308 /**
309 * Other 'windows' (basically a surface with a position and buttons on it)
310 * Currently used for the top status bar and popup windows
311 */
312 typedef void (*sdl_WindowCustomDraw)(sdl_Window *window);
313 struct sdl_Window
314 {
315 int top; /* Position on main window */
316 int left;
317
318 int width; /* Dimensions */
319 int height;
320
321 bool visible; /* Visible? */
322
323 SDL_Surface *surface; /* SDL surface info */
324
325 sdl_ButtonBank buttons; /* Buttons */
326
327 sdl_Font font; /* Font */
328
329 SDL_Surface *owner; /* Who shall I display on */
330
331 sdl_WindowCustomDraw draw_extra; /* Stuff to draw on the surface */
332 bool need_update;
333 };
334
335
336
337
338 /**
339 * The main surface of the application
340 */
341 static SDL_Surface *AppWin;
342
343 /**
344 * The status bar
345 */
346 static sdl_Window StatusBar;
347
348 /**
349 * The Popup window
350 */
351 static sdl_Window PopUp;
352 static bool popped;
353
354 /**
355 * Term windows
356 */
357 static term_window windows[ANGBAND_TERM_MAX];
358 static int Zorder[ANGBAND_TERM_MAX];
359
360 /**
361 * Keep track of the mouse status
362 */
363 static mouse_info mouse;
364
365 /**
366 * The number pad consists of 10 keys, each with an SDL identifier
367 */
368 #define is_numpad(k) \
369 ((k == SDLK_KP0) || (k == SDLK_KP1) || (k == SDLK_KP2) || (k == SDLK_KP3) || \
370 (k == SDLK_KP4) || (k == SDLK_KP5) || (k == SDLK_KP6) || \
371 (k == SDLK_KP7) || (k == SDLK_KP8) || (k == SDLK_KP9) || (k == SDLK_KP_ENTER))
372
373 static int SnapRange = 5; /* Window snap range (pixels) */
374 static int StatusHeight; /* The height in pixels of the status bar */
375 static int SelectedTerm; /* Current selected Term */
376
377 static int AboutSelect; /* About button */
378 static int TermSelect; /* Term selector button */
379 static int FontSelect; /* Font selector button */
380 static int VisibleSelect; /* Hide/unhide window button*/
381 static int MoreSelect; /* Other options button */
382 static int QuitSelect; /* Quit button */
383
384 /* For saving the icon for the About Box */
385 static SDL_Surface *mratt = NULL;
386
387 /*
388 * Unselected colour used on the 'About', 'More', 'Point Size', and
389 * 'Font Browser' panels; also used to highlight the currently selected font
390 * in the font menu
391 */
392 static SDL_Color AltUnselColour = { 160, 60, 60 };
393 /*
394 * Selected colour used on the 'More', 'Point Size', and 'Font Browser' panels
395 */
396 SDL_Color AltSelColour = { 210, 110, 110 };
397 /*
398 * Used to highlight the currently selected font in the font menu and
399 * 'Font Browser' panel
400 */
401 SDL_Color AltCapColour = { 95, 95, 195 };
402
403 /* Buttons on the 'More' panel */
404 static int MoreOK; /* Accept changes */
405 static int MoreFullscreen; /* Fullscreen toggle button */
406 static int MoreSnapPlus; /* Increase snap range */
407 static int MoreSnapMinus; /* Decrease snap range */
408
409 /* Buttons on the 'Point Size' panel */
410 static int PointSizeBigDec; /* decrease point size by 10 */
411 static int PointSizeDec; /* decrease point size by 1 */
412 static int PointSizeInc; /* increase point size by 1 */
413 static int PointSizeBigInc; /* increase point size by 10 */
414 static int PointSizeOk; /* accept current point size */
415 static int PointSizeCancel; /* cancel point size selection */
416 /* Width of the border box about the 'Point Size' panel */
417 #define POINT_SIZE_BORDER (5)
418 /* Width of margin within the border box about the 'Point Size' panel */
419 #define POINT_SIZE_MARGIN (2)
420
421 /*
422 * Number of directories and files shown at a time in the 'Font Browser' panel;
423 * should be at least 3 so that there's vertical space for the scrolling
424 * controls; if making this larger than 15, you'll likely have to increase
425 * MAX_BUTTONS as well
426 */
427 #define FONT_BROWSER_PAGE_ENTRIES (15)
428 /*
429 * Number of characters to show for each directory in the 'Font Browser' panel
430 */
431 #define FONT_BROWSER_DIR_LENGTH (15)
432 /* Number of characters to show for each file in the 'Font Browser' panel */
433 #define FONT_BROWSER_FILE_LENGTH (25)
434 /* Buttons on the 'Font Browser' panel */
435 static int FontBrowserDirUp;
436 static int FontBrowserDirectories[FONT_BROWSER_PAGE_ENTRIES];
437 static int FontBrowserDirPageBefore;
438 static int FontBrowserDirPageAfter;
439 static int FontBrowserDirPageDummy;
440 static int FontBrowserFiles[FONT_BROWSER_PAGE_ENTRIES];
441 static int FontBrowserFilePageBefore;
442 static int FontBrowserFilePageAfter;
443 static int FontBrowserFilePageDummy;
444 static int FontBrowserPtSizeBigDec;
445 static int FontBrowserPtSizeDec;
446 static int FontBrowserPtSizeInc;
447 static int FontBrowserPtSizeBigInc;
448 static int FontBrowserOk;
449 static int FontBrowserRefresh;
450 static int FontBrowserCancel;
451 /* Height of the preview part of the 'Font Browser' panel */
452 #define FONT_BROWSER_PREVIEW_HEIGHT (80)
453 /* Width of the border around the 'Font Browser' panel */
454 #define FONT_BROWSER_BORDER (5)
455 /* Width of margin within the border box around the 'Font Browser' panel */
456 #define FONT_BROWSER_MARGIN (2)
457 /*
458 * Width of the border around the directory and file subpanels of the
459 * 'Font Browser' panel
460 */
461 #define FONT_BROWSER_SUB_BORDER (3)
462 /*
463 * Width of margin within the border box aroudn the directory and file
464 * subpanels of the 'Font Browser' panel
465 */
466 #define FONT_BROWSER_SUB_MARGIN (1)
467 /*
468 * Width of space to separate the scroll controls in the 'Font Browser' panel
469 * and the subpanel they affect.
470 */
471 #define FONT_BROWSER_HOR_SPACE (2)
472 /*
473 * In the 'Font Browser' panel, the height of space to separate the point size
474 * control from file and directory subpanels and the preview area from the
475 * point size control
476 */
477 #define FONT_BROWSER_VER_SPACE (4)
478
479 /*
480 * Current directory being browsed by the 'Font Browser' panel; if not NULL,
481 * should end with a path separator
482 */
483 static char *FontBrowserCurDir = NULL;
484 /*
485 * The length of the root portion (the part that shouldn't be backed over
486 * when going up the directory tree) of FontBrowerCurDir
487 */
488 static size_t FontBrowserRootSz = 0;
489 /*
490 * Last directory browsed by the previous instance of the 'Font Browser' panel
491 */
492 static char *FontBrowserLastDir = NULL;
493 /*
494 * Value for the length of the root portion of FontBrowserLastDir
495 */
496 static size_t FontBrowserLastRootSz = 0;
497 /*
498 * Array of the unabbreviated directory names in the current directory being
499 * browsed by the 'Font Browser' panel
500 */
501 static char **FontBrowserDirEntries = NULL;
502 /*
503 * Number of directories in the directory currently being browsed by the
504 * 'Font Browser'
505 */
506 static size_t FontBrowserDirCount = 0;
507 /* Number of entries allocated in the FontBrowserDirEntries array */
508 static size_t FontBrowserDirAlloc = 0;
509 /*
510 * Current page (each with FONT_BROWSER_PAGE_ENTRIES) of directories viewed
511 * by the 'Font Browser' panel
512 */
513 static size_t FontBrowserDirPage = 0;
514 /*
515 * Array of the unabbreviated fixed-width font files in the current directory
516 * being browsed by the 'Font Browser' panel
517 */
518 static char **FontBrowserFileEntries = NULL;
519 /*
520 * Number of usable files in the directory currently being browsed by the
521 * 'Font Browser'
522 */
523 static size_t FontBrowserFileCount = 0;
524 /* Number of entries allocated in the FontBrowserFileEntries array */
525 static size_t FontBrowserFileAlloc = 0;
526 /*
527 * Current page (each with FONT_BROWSER_PAGE_ENTRIES) of files viewed
528 * by the 'Font Browser' panel
529 */
530 static size_t FontBrowserFilePage = 0;
531 /*
532 * Currently selected file entry in the 'Font Browser' panel or (size_t) -1
533 * if there isn't a current selection
534 */
535 static size_t FontBrowserFileCur = (size_t) -1;
536 /* Font data used when rendering the preview in the 'Font Browser' panel */
537 static sdl_Font *FontBrowserPreviewFont = NULL;
538
539 static bool Moving; /* Moving a window */
540 static bool Sizing; /* Sizing a window */
541 static SDL_Rect SizingSpot; /* Rect to descibe the sizing area */
542 static bool Sizingshow = false; /* Is the resize thingy displayed? */
543 static SDL_Rect SizingRect; /* Rect to describe the current resize window */
544
545 #include "grafmode.h"
546
547 static SDL_Surface *GfxSurface = NULL; /* A surface for the graphics */
548
549 static int MoreWidthPlus; /* Increase tile width */
550 static int MoreWidthMinus; /* Decrease tile width */
551 static int MoreHeightPlus; /* Increase tile height */
552 static int MoreHeightMinus; /* Decrease tile height */
553 static int *GfxButtons; /* Graphics mode buttons */
554 static int SelectedGfx; /* Current selected gfx */
555
556 /**
557 * Verify if the given path refers to a font file that can be used.
558 */
559 static bool is_font_file(const char *path)
560 {
561 bool result = false;
562 TTF_Font *font = TTF_OpenFont(path, 1);
563
564 if (font) {
565 if (TTF_FontFaceIsFixedWidth(font)) {
566 result = true;
567 }
568 TTF_CloseFont(font);
569 }
570 return result;
571 }
572
573 /**
574 * Fill a buffer with the short name for a font.
575 */
576 static void get_font_short_name(char *buf, size_t bufsz, const term_font *font)
577 {
578 if (font->bitmapped) {
579 my_strcpy(buf, font->name + path_filename_index(font->name),
580 bufsz);
581 } else {
582 strnfmt(buf, bufsz, "%dpt %s", font->size, font->name +
583 path_filename_index(font->name));
584 }
585 }
586
587 /**
588 * Fill in an SDL_Rect structure.
589 * Note it also returns the value adjusted
590 */
591 static SDL_Rect *RECT(int x, int y, int w, int h, SDL_Rect *rect)
592 {
593 rect->x = x;
594 rect->y = y;
595 rect->w = w;
596 rect->h = h;
597
598 return rect;
599 }
600
601 /**
602 * Is a point(x, y) in a rectangle?
603 */
604 static bool point_in(SDL_Rect *rect, int x, int y)
605 {
606 if (x < rect->x) return (false);
607 if (y < rect->y) return (false);
608 if (x >= rect->x + rect->w) return (false);
609 if (y >= rect->y + rect->h) return (false);
610
611 /* Must be inside */
612 return (true);
613 }
614
615 /**
616 * Draw an outline box
617 * Given the top, left, width & height
618 */
619 static void sdl_DrawBox(SDL_Surface *surface, SDL_Rect *rect, SDL_Color colour, int width)
620 {
621 SDL_Rect rc;
622 int left = rect->x;
623 int right = rect->x + rect->w - width;
624 int top = rect->y;
625 int bottom = rect->y + rect->h - width;
626 Uint32 pixel_colour = SDL_MapRGB(surface->format, colour.r, colour.g, colour.b);
627
628 /* Top left -> Top Right */
629 RECT(left, top, rect->w, width, &rc);
630 SDL_FillRect(surface, &rc, pixel_colour);
631
632 /* Bottom left -> Bottom Right */
633 RECT(left, bottom, rect->w, width, &rc);
634 SDL_FillRect(surface, &rc, pixel_colour);
635
636 /* Top left -> Bottom left */
637 RECT(left, top, width, rect->h, &rc);
638 SDL_FillRect(surface, &rc, pixel_colour);
639
640 /* Top right -> Bottom right */
641 RECT(right, top, width, rect->h, &rc);
642 SDL_FillRect(surface, &rc, pixel_colour);
643 }
644
645 /**
646 * Get the width and height of a given font file
647 */
648 static errr sdl_CheckFont(const term_font *req_font, int *width, int *height)
649 {
650 TTF_Font *ttf_font;
651 errr result;
652
653 if (req_font->preset) {
654 char buf[1024];
655
656 /* Build the path */
657 path_build(buf, sizeof(buf), ANGBAND_DIR_FONTS, req_font->name);
658 /* Attempt to load it */
659 ttf_font = TTF_OpenFont(buf, req_font->size);
660 } else {
661 /* Attempt to load it */
662 ttf_font = TTF_OpenFont(req_font->name, req_font->size);
663 }
664
665 /* Bugger */
666 if (!ttf_font) return (-1);
667
668 /* Get the size */
669 if (!TTF_FontFaceIsFixedWidth(ttf_font)
670 || TTF_SizeText(ttf_font, "M", width, height)) {
671 result = -1;
672 } else {
673 result = 0;
674 }
675
676 /* Finished with the font */
677 TTF_CloseFont(ttf_font);
678
679 return result;
680 }
681
682 /**
683 * The sdl_Font routines
684 */
685
686 /**
687 * Free any memory assigned by Create()
688 */
689 static void sdl_FontFree(sdl_Font *font)
690 {
691 /* Finished with the font */
692 TTF_CloseFont(font->sdl_font);
693 font->sdl_font = NULL;
694 }
695
696
697 /**
698 * Create new font data with font fontname, optimizing the data
699 * for the surface given
700 */
701 static errr sdl_FontCreate(sdl_Font *font, const term_font *req_font,
702 SDL_Surface *surface)
703 {
704 TTF_Font *ttf_font;
705
706 if (req_font->preset) {
707 char buf[1024];
708
709 /* Build the path */
710 path_build(buf, sizeof(buf), ANGBAND_DIR_FONTS, req_font->name);
711 /* Attempt to load it */
712 ttf_font = TTF_OpenFont(buf, req_font->size);
713 } else {
714 ttf_font = TTF_OpenFont(req_font->name, req_font->size);
715 }
716
717 /* Bugger */
718 if (!ttf_font) return -1;
719
720 /* Get the size */
721 if (TTF_SizeText(ttf_font, "M", &font->width, &font->height)) {
722 TTF_CloseFont(ttf_font);
723 return -1;
724 }
725
726 /* Fill in some of the font struct */
727 get_font_short_name(font->name, sizeof(font->name), req_font);
728 font->req = req_font;
729 font->pitch = surface->pitch;
730 font->bpp = surface->format->BytesPerPixel;
731 font->sdl_font = ttf_font;
732
733 /* Success */
734 return 0;
735 }
736
737
738
739
740 /**
741 * Draw some text onto a surface, allowing shaded backgrounds
742 * The surface is first checked to see if it is compatible with
743 * this font, if it isn't the font will be 're-precalculated'
744 *
745 * You can, I suppose, use one font on many surfaces, but it is
746 * definitely not recommended. One font per surface is good enough.
747 */
748 static errr sdl_mapFontDraw(sdl_Font *font, SDL_Surface *surface,
749 SDL_Color colour, SDL_Color bg, int x, int y,
750 int n , const char *s)
751 {
752 Uint8 bpp = surface->format->BytesPerPixel;
753 Uint16 pitch = surface->pitch;
754
755 SDL_Rect rc;
756 SDL_Surface *text;
757
758 if ((bpp != font->bpp) || (pitch != font->pitch))
759 sdl_FontCreate(font, font->req, surface);
760
761 /* Lock the window surface (if necessary) */
762 if (SDL_MUSTLOCK(surface))
763 if (SDL_LockSurface(surface) < 0)
764 return (-1);
765
766 RECT(x, y, n * font->width, font->height, &rc);
767 text = TTF_RenderUTF8_Shaded(font->sdl_font, s, colour, bg);
768 if (text) {
769 SDL_BlitSurface(text, NULL, surface, &rc);
770 SDL_FreeSurface(text);
771 }
772
773 /* Unlock the surface */
774 if (SDL_MUSTLOCK(surface)) SDL_UnlockSurface(surface);
775
776 /* Success */
777 return (0);
778 }
779
780 /**
781 * Draw some text onto a surface
782 * The surface is first checked to see if it is compatible with
783 * this font, if it isn't the font will be 're-precalculated'
784 *
785 * You can, I suppose, use one font on many surfaces, but it is
786 * definitely not recommended. One font per surface is good enough.
787 */
788 static errr sdl_FontDraw(sdl_Font *font, SDL_Surface *surface, SDL_Color colour,
789 int x, int y, int n , const char *s)
790 {
791 Uint8 bpp = surface->format->BytesPerPixel;
792 Uint16 pitch = surface->pitch;
793
794 SDL_Rect rc;
795 SDL_Surface *text;
796
797 if ((bpp != font->bpp) || (pitch != font->pitch))
798 sdl_FontCreate(font, font->req, surface);
799
800 /* Lock the window surface (if necessary) */
801 if (SDL_MUSTLOCK(surface))
802 if (SDL_LockSurface(surface) < 0)
803 return (-1);
804
805 RECT(x, y, n * font->width, font->height, &rc);
806 text = TTF_RenderUTF8_Solid(font->sdl_font, s, colour);
807 if (text) {
808 SDL_BlitSurface(text, NULL, surface, &rc);
809 SDL_FreeSurface(text);
810 }
811
812 /* Unlock the surface */
813 if (SDL_MUSTLOCK(surface)) SDL_UnlockSurface(surface);
814
815 /* Success */
816 return (0);
817 }
818
819
820
821 /**
822 * Draw a button on the screen
823 */
824 static void sdl_ButtonDraw(sdl_Button *button)
825 {
826 SDL_Surface *surface = button->owner->window->surface;
827 sdl_Font *font = &button->owner->window->font;
828
829 SDL_Color colour = button->selected ?
830 button->sel_colour : button->unsel_colour;
831
832 if (!button->visible) return;
833
834 SDL_FillRect(surface, &button->pos, SDL_MapRGB(surface->format, colour.r,
835 colour.g, colour.b));
836
837 if (strlen(button->caption)) {
838 size_t len = strlen(button->caption);
839
840 unsigned max = button->pos.w / font->width;
841 int n = MIN(len, max);
842 int l = n * font->width / 2;
843 int x = button->pos.x + ((button->pos.w) / 2) - l;
844
845 sdl_FontDraw(font, surface, button->cap_colour,
846 x, button->pos.y + 1, n, button->caption);
847 }
848 }
849
850 /**
851 * Adjust the position of a button
852 */
853 static void sdl_ButtonMove(sdl_Button *button, int x, int y)
854 {
855 button->pos.x = x;
856 button->pos.y = y;
857 button->owner->need_update = true;
858 }
859
860 /**
861 * Adjust the size of a button
862 */
863 static void sdl_ButtonSize(sdl_Button *button, int w, int h)
864 {
865 button->pos.w = w;
866 button->pos.h = h;
867 button->owner->need_update = true;
868 }
869
870 /**
871 * Set the caption
872 */
873 static void sdl_ButtonCaption(sdl_Button *button, const char *s)
874 {
875 my_strcpy(button->caption, s, sizeof(button->caption));
876 button->owner->need_update = true;
877 }
878
879 /**
880 * Set the visibility of a button
881 */
882 static void sdl_ButtonVisible(sdl_Button *button, bool visible)
883 {
884 if (button->visible != visible) {
885 button->visible = visible;
886
887 button->owner->need_update = true;
888 }
889 }
890
891
892 /**
893 * Maximum amount of buttons in a bank
894 */
895 #define MAX_BUTTONS 50
896
897 /**
898 * ------------------------------------------------------------------------
899 * The button_bank package
900 * ------------------------------------------------------------------------ */
901
902 /**
903 * Initialize it
904 */
905 static void sdl_ButtonBankInit(sdl_ButtonBank *bank, sdl_Window *window)
906 {
907 bank->window = window;
908 bank->buttons = mem_zalloc(MAX_BUTTONS * sizeof(sdl_Button));
909 bank->used = mem_zalloc(MAX_BUTTONS * sizeof(bool));
910 bank->need_update = true;
911 }
912
913 /**
914 * Clear the bank
915 */
916 static void sdl_ButtonBankFree(sdl_ButtonBank *bank)
917 {
918 mem_free(bank->buttons);
919 mem_free(bank->used);
920 }
921
922 /**
923 * Draw all the buttons on the screen
924 */
925 static void sdl_ButtonBankDrawAll(sdl_ButtonBank *bank)
926 {
927 int i;
928
929 for (i = 0; i < MAX_BUTTONS; i++) {
930 sdl_Button *button = &bank->buttons[i];
931
932 if (!bank->used[i]) continue;
933 if (!button->visible) continue;
934
935 sdl_ButtonDraw(button);
936 }
937 bank->need_update = false;
938 }
939
940 /**
941 * Get a new button index
942 */
943 static int sdl_ButtonBankNew(sdl_ButtonBank *bank)
944 {
945 int i = 0;
946 sdl_Button *new_button;
947
948 while (bank->used[i] && (i < MAX_BUTTONS)) i++;
949
950 if (i == MAX_BUTTONS)
951 /* Bugger! */
952 return (-1);
953
954 /* Get the button */
955 new_button = &bank->buttons[i];
956
957 /* Mark the button as used */
958 bank->used[i] = true;
959
960 /* Clear it */
961 memset(new_button, 0, sizeof(sdl_Button));
962
963 /* Mark it as mine */
964 new_button->owner = bank;
965
966 /* Default colours */
967 new_button->unsel_colour.r = 160;
968 new_button->unsel_colour.g = 160;
969 new_button->unsel_colour.b = 60;
970 new_button->sel_colour.r = 210;
971 new_button->sel_colour.g = 210;
972 new_button->sel_colour.b = 110;
973 new_button->cap_colour = DefaultCapColour;
974
975 /* Success */
976 return (i);
977 }
978
979 /**
980 * Retrieve button 'idx' or NULL
981 */
982 static sdl_Button *sdl_ButtonBankGet(sdl_ButtonBank *bank, int idx)
983 {
984 /* Check the index */
985 if ((idx < 0) || (idx >= MAX_BUTTONS)) return (NULL);
986 if (!bank->used[idx]) return (NULL);
987
988 /* Return it */
989 return &bank->buttons[idx];
990 }
991
992 #if 0
993 /**
994 * Remove a Button by its index
995 */
996 static void sdl_ButtonBankRemove(sdl_ButtonBank *bank, int idx)
997 {
998 sdl_Button *button;
999
1000 /* Check the index */
1001 if ((idx < 0) || (idx >= MAX_BUTTONS)) return;
1002 if (!bank->used[idx]) return;
1003
1004 /* Grab it */
1005 button = &bank->buttons[idx];
1006
1007 /* Hide it */
1008 button->visible = false;
1009
1010 /* Draw it */
1011 bank->need_update = true;
1012
1013 /* Forget */
1014 bank->used[idx] = false;
1015 }
1016
1017 #endif
1018
1019 /**
1020 * Examine and respond to mouse presses
1021 * Return if we 'handled' the click
1022 */
1023 static bool sdl_ButtonBankMouseDown(sdl_ButtonBank *bank, int x, int y)
1024 {
1025 int i;
1026
1027 /* Check every button */
1028 for (i = 0; i < MAX_BUTTONS; i++) {
1029 sdl_Button *button = &bank->buttons[i];
1030
1031 /* Discard some */
1032 if (!bank->used[i]) continue;
1033 if (!button->visible) continue;
1034
1035 /* Check the coordinates */
1036 if (point_in(&button->pos, x, y)) {
1037 button->selected = true;
1038
1039 /* Draw it */
1040 bank->need_update = true;
1041
1042 return (true);
1043 }
1044 }
1045 return (false);
1046 }
1047
1048 /**
1049 * Respond to a mouse button release
1050 */
1051 static bool sdl_ButtonBankMouseUp(sdl_ButtonBank *bank, int x, int y)
1052 {
1053 int i;
1054
1055 /* Check every button */
1056 for (i = 0; i < MAX_BUTTONS; i++) {
1057 sdl_Button *button = &bank->buttons[i];
1058
1059 /* Discard some */
1060 if (!bank->used[i]) continue;
1061 if (!button->visible) continue;
1062
1063 /* Check the coordinates */
1064 if (point_in(&button->pos, x, y)) {
1065 /* Has this button been 'selected'? */
1066 if (button->selected) {
1067 /*
1068 * Do these before performing the callback (the
1069 * button is no longer selected and needs to
1070 * be redrawn) since the callback could remove
1071 * the button.
1072 */
1073 button->selected = false;
1074 bank->need_update = true;
1075
1076 /* Activate the button (usually) */
1077 if (button->activate) (*button->activate)(button);
1078
1079 return (true);
1080 }
1081 } else {
1082 /* This button was 'selected' but the release of the */
1083 /* mouse button was outside the area of this button */
1084 if (button->selected) {
1085 /* Now not selected */
1086 button->selected = false;
1087
1088 /* Draw it */
1089 bank->need_update = true;
1090 }
1091 }
1092 }
1093
1094 return (false);
1095 }
1096
1097 /**
1098 * sdl_Window functions
1099 */
1100 static void sdl_WindowFree(sdl_Window* window)
1101 {
1102 if (window->surface) {
1103 SDL_FreeSurface(window->surface);
1104 sdl_ButtonBankFree(&window->buttons);
1105 sdl_FontFree(&window->font);
1106 memset(window, 0, sizeof(sdl_Window));
1107 }
1108 }
1109
1110 /**
1111 * Initialize a window
1112 */
1113 static void sdl_WindowInit(sdl_Window* window, int w, int h, SDL_Surface *owner,
1114 const term_font *req_font)
1115 {
1116 sdl_WindowFree(window);
1117 window->owner = owner;
1118 window->width = w;
1119 window->height = h;
1120 window->surface = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h,
1121 owner->format->BitsPerPixel,
1122 owner->format->Rmask,
1123 owner->format->Gmask,
1124 owner->format->Bmask,
1125 owner->format->Amask);
1126 sdl_ButtonBankInit(&window->buttons, window);
1127 sdl_FontCreate(&window->font, req_font, window->surface);
1128 window->visible = true;
1129 window->need_update = true;
1130 }
1131
1132
1133 static void sdl_WindowBlit(sdl_Window* window)
1134 {
1135 SDL_Rect rc;
1136
1137 if (!window->visible) return;
1138
1139 RECT(window->left, window->top, window->width, window->height, &rc);
1140
1141 SDL_BlitSurface(window->surface, NULL, window->owner, &rc);
1142 SDL_UpdateRects(window->owner, 1, &rc);
1143 }
1144
1145 static void sdl_WindowText(sdl_Window* window, SDL_Color c, int x, int y,
1146 const char *s)
1147 {
1148 sdl_FontDraw(&window->font, window->surface, c, x, y, strlen(s), s);
1149 }
1150
1151 static void sdl_WindowUpdate(sdl_Window* window)
1152 {
1153 if ((window->need_update || window->buttons.need_update) &&
1154 (window->visible)) {
1155 SDL_Event Event;
1156
1157 SDL_FillRect(window->surface, NULL, back_pixel_colour);
1158
1159 if (window->draw_extra) (*window->draw_extra)(window);
1160
1161 sdl_ButtonBankDrawAll(&window->buttons);
1162
1163 window->need_update = false;
1164
1165 memset(&Event, 0, sizeof(SDL_Event));
1166
1167 Event.type = WINDOW_DRAW;
1168
1169 Event.user.data1 = (void*)window;
1170
1171 SDL_PushEvent(&Event);
1172 }
1173 }
1174
1175
1176
1177 static void term_windowFree(term_window* win)
1178 {
1179 if (win->surface) {
1180 SDL_FreeSurface(win->surface);
1181 win->surface = NULL;
1182
1183 /* Invalidate the gfx surface */
1184 if (win->tiles) {
1185 SDL_FreeSurface(win->tiles);
1186 win->tiles = NULL;
1187 }
1188 if (win->onebyone) {
1189 SDL_FreeSurface(win->onebyone);
1190 win->onebyone = NULL;
1191 }
1192 term_nuke(&win->term_data);
1193 }
1194
1195 sdl_FontFree(&win->font);
1196 }
1197
1198 static errr save_prefs(void);
1199 static void hook_quit(const char *str)
1200 {
1201 int i;
1202
1203 save_prefs();
1204
1205 string_free(sdl_settings_file);
1206
1207 /* Free the surfaces of the windows */
1208 for (i = 0; i < ANGBAND_TERM_MAX; i++) {
1209 term_windowFree(&windows[i]);
1210 string_free(windows[i].req_font.name);
1211 }
1212
1213 /* Free the graphics surface */
1214 if (GfxSurface) SDL_FreeSurface(GfxSurface);
1215
1216 close_graphics_modes();
1217 if (GfxButtons) mem_free(GfxButtons);
1218
1219 /* Free the 'System font' */
1220 sdl_FontFree(&SystemFont);
1221
1222 /* Free the statusbar window */
1223 sdl_WindowFree(&StatusBar);
1224
1225 /* free the popup window */
1226 sdl_WindowFree(&PopUp);
1227
1228 /* Free the main surface */
1229 SDL_FreeSurface(AppWin);
1230
1231 /* Shut down the font library */
1232 TTF_Quit();
1233
1234 /* Shut down SDL */
1235 SDL_Quit();
1236
1237 for (i = 0; i < MAX_FONTS; i++)
1238 string_free(FontList[i]);
1239 }
1240
1241 static void BringToTop(void)
1242 {
1243 int i, idx;
1244
1245 for (idx = 0; idx < ANGBAND_TERM_MAX; idx++)
1246 if (Zorder[idx] == SelectedTerm) break;
1247
1248 if (idx == ANGBAND_TERM_MAX) return;
1249
1250 for (i = idx; i < ANGBAND_TERM_MAX - 1; i++)
1251 Zorder[i] = Zorder[i + 1];
1252
1253 Zorder[ANGBAND_TERM_MAX - 1] = SelectedTerm;
1254 }
1255
1256
1257
1258 /**
1259 * Validate a file
1260 */
1261 static void validate_file(const char *s)
1262 {
1263 if (!file_exists(s))
1264 quit_fmt("cannot find required file:\n%s", s);
1265 }
1266
1267 /**
1268 * Find a window that is under the points x,y on
1269 * the main screen
1270 */
1271
1272 static int sdl_LocateWin(int x, int y)
1273 {
1274 int i;
1275
1276 for (i = ANGBAND_TERM_MAX - 1; i >= 0; i--) {
1277 term_window *win = &windows[Zorder[i]];
1278 SDL_Rect rc;
1279
1280 if (!win->visible) continue;
1281 if (!point_in(RECT(win->left, win->top, win->width, win->height, &rc),
1282 x, y))
1283 continue;
1284
1285 return (Zorder[i]);
1286 }
1287
1288 return (-1);
1289 }
1290
1291 static void draw_statusbar(sdl_Window *window)
1292 {
1293 char buf[128];
1294 term_window *win = &windows[SelectedTerm];
1295 int fw = window->font.width;
1296 int x = 1;
1297 sdl_Button *button;
1298
1299 SDL_Rect rc;
1300
1301 SDL_Color c = {160, 160, 60, 0};
1302
1303 RECT(0, StatusBar.height - 1, StatusBar.width, 1, &rc);
1304 SDL_FillRect(StatusBar.surface, &rc, SDL_MapRGB(StatusBar.surface->format,
1305 c.r, c.g, c.b));
1306
1307 button = sdl_ButtonBankGet(&StatusBar.buttons, AboutSelect);
1308 x += button->pos.w + 20;
1309
1310 sdl_WindowText(&StatusBar, c, x, 1, "Term:");
1311 x += 5 * fw;
1312
1313 button = sdl_ButtonBankGet(&StatusBar.buttons, TermSelect);
1314 button->pos.x = x;
1315 x += button->pos.w + 10;
1316
1317 strnfmt(buf, sizeof(buf), "(%dx%d)", win->cols, win->rows);
1318 sdl_WindowText(&StatusBar, c, x, 1, buf);
1319 x += strlen(buf) * fw + 20;
1320
1321 sdl_WindowText(&StatusBar, c, x, 1, "Visible:");
1322 x += 8 * fw;
1323
1324 button = sdl_ButtonBankGet(&StatusBar.buttons, VisibleSelect);
1325 button->pos.x = x;
1326 x += button->pos.w + 20;
1327
1328 button = sdl_ButtonBankGet(&StatusBar.buttons, FontSelect);
1329 if (button->visible) sdl_WindowText(&StatusBar, c, x, 1, "Font:");
1330 x += 5 * fw;
1331
1332
1333 button->pos.x = x;
1334 x += button->pos.w + 20;
1335
1336 button = sdl_ButtonBankGet(&StatusBar.buttons, MoreSelect);
1337 button->pos.x = x;
1338
1339 x += button->pos.w + 20;
1340
1341
1342 }
1343
1344
1345
1346 static void sdl_BlitWin(term_window *win)
1347 {
1348 SDL_Rect rc;
1349
1350 if (!win->surface) return;
1351 if (!win->visible) return;
1352 if (win->uRect.x == -1) return;
1353
1354 /* Select the area to be updated */
1355 RECT(win->left + win->uRect.x, win->top + win->uRect.y, win->uRect.w,
1356 win->uRect.h, &rc);
1357
1358 SDL_BlitSurface(win->surface, &win->uRect, AppWin, &rc);
1359 SDL_UpdateRects(AppWin, 1, &rc);
1360
1361 /* Mark the update as complete */
1362 win->uRect.x = -1;
1363 }
1364
1365 static void sdl_BlitAll(void)
1366 {
1367 SDL_Rect rc;
1368 sdl_Window *window = &StatusBar;
1369 int i;
1370 SDL_Color colour = {160, 160, 60, 0};
1371 /* int32 ccolour = SDL_MapRGB(AppWin->format, 160, 40, 40); */
1372 SDL_FillRect(AppWin, NULL, back_pixel_colour);
1373
1374 for (i = 0; i < ANGBAND_TERM_MAX; i++) {
1375 term_window *win = &windows[Zorder[i]];
1376
1377 if (!win->surface) continue;
1378 if (!win->visible) continue;
1379
1380 RECT(win->left, win->top, win->width, win->height, &rc);
1381 SDL_BlitSurface(win->surface, NULL, AppWin, &rc);
1382
1383 if (Zorder[i] == SelectedTerm) {
1384 SizingSpot.w = 10;
1385 SizingSpot.h = 10;
1386 SizingSpot.x = win->left + win->width - 10;
1387 SizingSpot.y = win->top + win->height - 10;
1388 /* SDL_FillRect(AppWin, &SizingSpot, ccolour); */
1389
1390 if (Sizing) {
1391 int width = 2;
1392 int grabsize = 10;
1393 rc = SizingRect;
1394 SizingSpot.w = grabsize;
1395 SizingSpot.h = grabsize;
1396 SizingSpot.x = SizingRect.x + SizingRect.w - grabsize;
1397 SizingSpot.y = SizingRect.y + SizingRect.h - grabsize;
1398 sdl_DrawBox(AppWin, &rc, colour, width);
1399 }
1400 }
1401 }
1402
1403 RECT(window->left, window->top, window->width, window->height, &rc);
1404
1405 SDL_BlitSurface(window->surface, NULL, AppWin, &rc);
1406
1407 SDL_UpdateRect(AppWin, 0, 0, AppWin->w, AppWin->h);
1408
1409
1410 }
1411
1412 static void RemovePopUp(void)
1413 {
1414 PopUp.visible = false;
1415 popped = false;
1416 sdl_BlitAll();
1417 }
1418
1419 static void QuitActivate(sdl_Button *sender)
1420 {
1421 SDL_Event Event;
1422
1423 Event.type = SDL_QUIT;
1424
1425 SDL_PushEvent(&Event);
1426 }
1427
1428 static void SetStatusButtons(void)
1429 {
1430 term_window *win = &windows[SelectedTerm];
1431 sdl_Button *button = sdl_ButtonBankGet(&StatusBar.buttons, TermSelect);
1432 sdl_Button *fontbutton = sdl_ButtonBankGet(&StatusBar.buttons, FontSelect);
1433 sdl_Button *visbutton = sdl_ButtonBankGet(&StatusBar.buttons,
1434 VisibleSelect);
1435
1436 sdl_ButtonCaption(button, angband_term_name[SelectedTerm]);
1437
1438 if (!win->visible) {
1439 sdl_ButtonVisible(fontbutton, false);
1440 sdl_ButtonCaption(visbutton, "No");
1441 } else {
1442 sdl_ButtonVisible(fontbutton, true);
1443 sdl_ButtonCaption(fontbutton, win->font.name);
1444 sdl_ButtonCaption(visbutton, "Yes");
1445 }
1446 }
1447
1448 static void TermFocus(int idx)
1449 {
1450 if (SelectedTerm == idx) return;
1451
1452 SelectedTerm = idx;
1453
1454 BringToTop();
1455
1456 SetStatusButtons();
1457
1458 sdl_BlitAll();
1459 }
1460
1461 static void AboutDraw(sdl_Window *win)
1462 {
1463 SDL_Rect rc;
1464 SDL_Rect icon;
1465
1466 RECT(0, 0, win->width, win->height, &rc);
1467
1468 /* Draw a nice box */
1469 SDL_FillRect(win->surface, &win->surface->clip_rect,
1470 SDL_MapRGB(win->surface->format, 255, 255, 255));
1471 sdl_DrawBox(win->surface, &win->surface->clip_rect, AltUnselColour, 5);
1472 if (mratt) {
1473 RECT((win->width - mratt->w) / 2, 5, mratt->w, mratt->h, &icon);
1474 SDL_BlitSurface(mratt, NULL, win->surface, &icon);
1475 }
1476 sdl_WindowText(win, AltUnselColour, 20, 150,
1477 format("You are playing %s", buildid));
1478 sdl_WindowText(win, AltUnselColour, 20, 160,
1479 "See http://www.rephial.org");
1480 }
1481
1482 static void AboutActivate(sdl_Button *sender)
1483 {
1484 int width = 350;
1485 int height = 200;
1486
1487 sdl_WindowInit(&PopUp, width, height, AppWin, StatusBar.font.req);
1488 PopUp.left = (AppWin->w / 2) - width / 2;
1489 PopUp.top = (AppWin->h / 2) - height / 2;
1490 PopUp.draw_extra = AboutDraw;
1491
1492 popped = true;
1493 }
1494
1495 static void SelectTerm(sdl_Button *sender)
1496 {
1497 RemovePopUp();
1498
1499 TermFocus(sender->tag);
1500 }
1501
1502 static void TermActivate(sdl_Button *sender)
1503 {
1504 int i, maxl = 0;
1505 int width, height = ANGBAND_TERM_MAX * (StatusBar.font.height + 1);
1506
1507 for (i = 0; i < ANGBAND_TERM_MAX; i++) {
1508 int l = strlen(angband_term_name[i]);
1509 if (l > maxl) maxl = l;
1510 }
1511
1512 width = maxl * StatusBar.font.width + 20;
1513
1514 sdl_WindowInit(&PopUp, width, height, AppWin, StatusBar.font.req);
1515 PopUp.left = sender->pos.x;
1516 PopUp.top = sender->pos.y;
1517
1518 for (i = 0; i < ANGBAND_TERM_MAX; i++) {
1519 int h = PopUp.font.height;
1520 int b = sdl_ButtonBankNew(&PopUp.buttons);
1521 sdl_Button *button = sdl_ButtonBankGet(&PopUp.buttons, b);
1522 sdl_ButtonSize(button, width - 2 , h);
1523 sdl_ButtonMove(button, 1, i * (h + 1));
1524 sdl_ButtonCaption(button, angband_term_name[i]);
1525 sdl_ButtonVisible(button, true);
1526 button->tag = i;
1527 button->activate = SelectTerm;
1528 }
1529 popped = true;
1530 }
1531
1532 static void ResizeWin(term_window* win, int w, int h);
1533 static void term_data_link_sdl(term_window *win);
1534
1535 static void VisibleActivate(sdl_Button *sender)
1536 {
1537 term_window *window = &windows[SelectedTerm];
1538
1539 if (SelectedTerm == 0) return;
1540
1541 if (window->visible) {
1542 window->visible = false;
1543 term_windowFree(window);
1544 angband_term[SelectedTerm] = NULL;
1545 } else {
1546 window->visible = true;
1547 ResizeWin(window, window->width, window->height);
1548 }
1549
1550 SetStatusButtons();
1551 sdl_BlitAll();
1552 }
1553
1554 static void HelpWindowFontChange(term_window *window)
1555 {
1556 int w, h;
1557
1558 if (sdl_CheckFont(&window->req_font, &w, &h)) {
1559 quit_fmt("could not use the requested font %s",
1560 window->req_font.name);
1561 }
1562
1563 /* Invalidate the gfx surface */
1564 if (window->tiles) {
1565 SDL_FreeSurface(window->tiles);
1566 window->tiles = NULL;
1567 }
1568
1569 ResizeWin(window, (w * window->cols) + (2 * window->border),
1570 (h * window->rows) + window->border + window->title_height);
1571
1572 SetStatusButtons();
1573 }
1574
1575 static void SelectPresetBitmappedFont(sdl_Button *sender)
1576 {
1577 term_window *window = &windows[SelectedTerm];
1578
1579 sdl_FontFree(&window->font);
1580 string_free(window->req_font.name);
1581 window->req_font.name = string_make(sender->caption);
1582 window->req_font.size = 0;
1583 window->req_font.preset = true;
1584 window->req_font.bitmapped = true;
1585 HelpWindowFontChange(window);
1586
1587 RemovePopUp();
1588 }
1589
1590 static void ChangePointSize(sdl_Button *sender)
1591 {
1592 sdl_Button *button;
1593
1594 if ((new_font.size == MIN_POINT_SIZE && sender->tag < 0)
1595 || (new_font.size == MAX_POINT_SIZE
1596 && sender->tag > 0)) {
1597 /* Size does not change. */
1598 return;
1599 }
1600
1601 new_font.size = MAX(MIN_POINT_SIZE, MIN(MAX_POINT_SIZE,
1602 new_font.size + sender->tag));
1603
1604 /* Change colors on the buttons. */
1605 if (new_font.size > MIN_POINT_SIZE) {
1606 button = sdl_ButtonBankGet(&PopUp.buttons, PointSizeBigDec);
1607 button->unsel_colour = AltUnselColour;
1608 button->sel_colour = AltSelColour;
1609 button->cap_colour = DefaultCapColour;
1610 button = sdl_ButtonBankGet(&PopUp.buttons, PointSizeDec);
1611 button->unsel_colour = AltUnselColour;
1612 button->sel_colour = AltSelColour;
1613 button->cap_colour = DefaultCapColour;
1614 } else {
1615 button = sdl_ButtonBankGet(&PopUp.buttons, PointSizeBigDec);
1616 button->unsel_colour = back_colour;
1617 button->sel_colour = back_colour;
1618 button->cap_colour = back_colour;
1619 button = sdl_ButtonBankGet(&PopUp.buttons, PointSizeDec);
1620 button->unsel_colour = back_colour;
1621 button->sel_colour = back_colour;
1622 button->cap_colour = back_colour;
1623 }
1624 if (new_font.size < MAX_POINT_SIZE) {
1625 button = sdl_ButtonBankGet(&PopUp.buttons, PointSizeInc);
1626 button->unsel_colour = AltUnselColour;
1627 button->sel_colour = AltSelColour;
1628 button->cap_colour = DefaultCapColour;
1629 button = sdl_ButtonBankGet(&PopUp.buttons, PointSizeBigInc);
1630 button->unsel_colour = AltUnselColour;
1631 button->sel_colour = AltSelColour;
1632 button->cap_colour = DefaultCapColour;
1633 } else {
1634 button = sdl_ButtonBankGet(&PopUp.buttons, PointSizeInc);
1635 button->unsel_colour = back_colour;
1636 button->sel_colour = back_colour;
1637 button->cap_colour = back_colour;
1638 button = sdl_ButtonBankGet(&PopUp.buttons, PointSizeBigInc);
1639 button->unsel_colour = back_colour;
1640 button->sel_colour = back_colour;
1641 button->cap_colour = back_colour;
1642 }
1643 PopUp.need_update = true;
1644 }
1645
1646 static void AcceptPointSize(sdl_Button *sender)
1647 {
1648 term_window *window = &windows[SelectedTerm];
1649
1650 sdl_FontFree(&window->font);
1651 string_free(window->req_font.name);
1652 window->req_font.name = string_make(new_font.name);
1653 window->req_font.size = new_font.size;
1654 window->req_font.preset = new_font.preset;
1655 assert(!new_font.bitmapped);
1656 window->req_font.bitmapped = false;
1657 HelpWindowFontChange(window);
1658
1659 RemovePopUp();
1660 }
1661
1662 static void CancelPointSize(sdl_Button *sender)
1663 {
1664 string_free(new_font.name);
1665 new_font.name = NULL;
1666 new_font.size = 0;
1667 new_font.preset = false;
1668 new_font.bitmapped = false;
1669 RemovePopUp();
1670 }
1671
1672 static void DrawPointSize(sdl_Window *win)
1673 {
1674 SDL_Rect rc;
1675
1676 RECT(0, 0, win->width, win->height, &rc);
1677 sdl_DrawBox(win->surface, &rc, AltUnselColour, POINT_SIZE_BORDER);
1678
1679 sdl_WindowText(win, AltUnselColour, win->width / 2
1680 - 5 * PopUp.font.width, POINT_SIZE_BORDER + POINT_SIZE_MARGIN,
1681 "Point Size");
1682 sdl_WindowText(win, AltUnselColour, 12 * PopUp.font.width
1683 + POINT_SIZE_BORDER + POINT_SIZE_MARGIN,
1684 POINT_SIZE_BORDER + POINT_SIZE_MARGIN + PopUp.font.height + 6,
1685 format("%d pt", new_font.size));
1686 }
1687
1688 /*
1689 * For this panel, buttons are enabled or disabled based on the state of the
1690 * point size selection. Don't use sdl_ButtonVisible() to do that because
1691 * clicks in invisible buttons aren't handled and end up dismissing the panel.
1692 * That seems likely to frustrate uses, so alter the coloring of the buttons
1693 * instead while leaving the "visible" field of the button true.
1694 */
1695 static void ActivatePointSize(sdl_Button *sender)
1696 {
1697 int left = sender->pos.x + sender->owner->window->left;
1698 int top = sender->pos.y + sender->owner->window->top;
1699 int height = 4 * (PopUp.font.height + 2) + 4 + 2 * (POINT_SIZE_BORDER
1700 + POINT_SIZE_MARGIN);
1701 int width = ((int) strlen(format("%d pt", MAX_POINT_SIZE)) + 24)
1702 * PopUp.font.width + 2 * (POINT_SIZE_BORDER
1703 + POINT_SIZE_MARGIN);
1704 sdl_Button *button;
1705
1706 sdl_WindowInit(&PopUp, width, height, AppWin, StatusBar.font.req);
1707 PopUp.left = left;
1708 PopUp.top = top;
1709 PopUp.draw_extra = DrawPointSize;
1710
1711 PointSizeBigDec = sdl_ButtonBankNew(&PopUp.buttons);
1712 button = sdl_ButtonBankGet(&PopUp.buttons, PointSizeBigDec);
1713 if (new_font.size > MIN_POINT_SIZE) {
1714 button->unsel_colour = AltUnselColour;
1715 button->sel_colour = AltSelColour;
1716 button->cap_colour = DefaultCapColour;
1717 } else {
1718 button->unsel_colour = back_colour;
1719 button->sel_colour = back_colour;
1720 button->cap_colour = back_colour;
1721 }
1722 sdl_ButtonSize(button, 4 * PopUp.font.width, PopUp.font.height + 2);
1723 sdl_ButtonVisible(button, true);
1724 sdl_ButtonCaption(button, "--");
1725 sdl_ButtonMove(button, POINT_SIZE_BORDER + POINT_SIZE_MARGIN,
1726 POINT_SIZE_BORDER + POINT_SIZE_MARGIN + PopUp.font.height + 6);
1727 button->tag = -10;
1728 button->activate = ChangePointSize;
1729
1730 PointSizeDec = sdl_ButtonBankNew(&PopUp.buttons);
1731 button = sdl_ButtonBankGet(&PopUp.buttons, PointSizeDec);
1732 if (new_font.size > MIN_POINT_SIZE) {
1733 button->unsel_colour = AltUnselColour;
1734 button->sel_colour = AltSelColour;
1735 button->cap_colour = DefaultCapColour;
1736 } else {
1737 button->unsel_colour = back_colour;
1738 button->sel_colour = back_colour;
1739 button->cap_colour = back_colour;
1740 }
1741 sdl_ButtonSize(button, 4 * PopUp.font.width, PopUp.font.height + 2);
1742 sdl_ButtonVisible(button, true);
1743 sdl_ButtonCaption(button, " -");
1744 sdl_ButtonMove(button, 6 * PopUp.font.width + POINT_SIZE_BORDER
1745 + POINT_SIZE_MARGIN, POINT_SIZE_BORDER + POINT_SIZE_MARGIN
1746 + PopUp.font.height + 6);
1747 button->tag = -1;
1748 button->activate = ChangePointSize;
1749
1750 PointSizeInc = sdl_ButtonBankNew(&PopUp.buttons);
1751 button = sdl_ButtonBankGet(&PopUp.buttons, PointSizeInc);
1752 if (new_font.size < MAX_POINT_SIZE) {
1753 button->unsel_colour = AltUnselColour;
1754 button->sel_colour = AltSelColour;
1755 button->cap_colour = DefaultCapColour;
1756 } else {
1757 button->unsel_colour = back_colour;
1758 button->sel_colour = back_colour;
1759 button->cap_colour = back_colour;
1760 }
1761 sdl_ButtonSize(button, 4 * PopUp.font.width, PopUp.font.height + 2);
1762 sdl_ButtonVisible(button, true);
1763 sdl_ButtonCaption(button, " +");
1764 sdl_ButtonMove(button, width - 10 * PopUp.font.width
1765 - POINT_SIZE_BORDER - POINT_SIZE_MARGIN, POINT_SIZE_BORDER
1766 + POINT_SIZE_MARGIN + PopUp.font.height + 6);
1767 button->tag = 1;
1768 button->activate = ChangePointSize;
1769
1770 PointSizeBigInc = sdl_ButtonBankNew(&PopUp.buttons);
1771 button = sdl_ButtonBankGet(&PopUp.buttons, PointSizeBigInc);
1772 if (new_font.size < MAX_POINT_SIZE) {
1773 button->unsel_colour = AltUnselColour;
1774 button->sel_colour = AltSelColour;
1775 button->cap_colour = DefaultCapColour;
1776 } else {
1777 button->unsel_colour = back_colour;
1778 button->sel_colour = back_colour;
1779 button->cap_colour = back_colour;
1780 }
1781 sdl_ButtonSize(button, 4 * PopUp.font.width, PopUp.font.height + 2);
1782 sdl_ButtonVisible(button, true);
1783 sdl_ButtonCaption(button, "++");
1784 sdl_ButtonMove(button, width - 4 * PopUp.font.width
1785 - POINT_SIZE_BORDER - POINT_SIZE_MARGIN, POINT_SIZE_BORDER
1786 + POINT_SIZE_MARGIN + PopUp.font.height + 6);
1787 button->tag = 10;
1788 button->activate = ChangePointSize;
1789
1790 PointSizeOk = sdl_ButtonBankNew(&PopUp.buttons);
1791 button = sdl_ButtonBankGet(&PopUp.buttons, PointSizeOk);
1792 button->unsel_colour = AltUnselColour;
1793 button->sel_colour = AltSelColour;
1794 sdl_ButtonSize(button, 8 * PopUp.font.width, PopUp.font.height + 2);
1795 sdl_ButtonVisible(button, true);
1796 sdl_ButtonCaption(button, "OK");
1797 sdl_ButtonMove(button, width / 2 - 10 * PopUp.font.width,
1798 height - PopUp.font.height - 2 - POINT_SIZE_BORDER
1799 - POINT_SIZE_MARGIN);
1800 button->activate = AcceptPointSize;
1801
1802 PointSizeCancel = sdl_ButtonBankNew(&PopUp.buttons);
1803 button = sdl_ButtonBankGet(&PopUp.buttons, PointSizeCancel);
1804 button->unsel_colour = AltUnselColour;
1805 button->sel_colour = AltSelColour;
1806 sdl_ButtonSize(button, 8 * PopUp.font.width, PopUp.font.height + 2);
1807 sdl_ButtonVisible(button, true);
1808 sdl_ButtonCaption(button, "Cancel");
1809 sdl_ButtonMove(button, width / 2 + 2 * PopUp.font.width,
1810 height - PopUp.font.height - 2 - POINT_SIZE_BORDER
1811 - POINT_SIZE_MARGIN);
1812 button->activate = CancelPointSize;
1813
1814 popped = true;
1815 }
1816
1817 static void SelectPresetScalableFont(sdl_Button *sender)
1818 {
1819 term_window *window = &windows[SelectedTerm];
1820
1821 RemovePopUp();
1822 string_free(new_font.name);
1823 new_font.name = string_make(sender->caption);
1824 new_font.size = (window->req_font.size > 0) ?
1825 window->req_font.size : DEFAULT_POINT_SIZE;
1826 new_font.preset = true;
1827 new_font.bitmapped = false;
1828 ActivatePointSize(sender);
1829 }
1830
1831 static void AlterNonPresetFontSize(sdl_Button *sender)
1832 {
1833 term_window *window = &windows[SelectedTerm];
1834
1835 assert(!window->req_font.preset);
1836 RemovePopUp();
1837 if (!window->req_font.bitmapped) {
1838 assert(window->req_font.size >= MIN_POINT_SIZE
1839 && window->req_font.size <= MAX_POINT_SIZE);
1840 string_free(new_font.name);
1841 new_font.name = string_make(window->req_font.name);
1842 new_font.size = window->req_font.size;
1843 new_font.preset = false;
1844 new_font.bitmapped = false;
1845 ActivatePointSize(sender);
1846 }
1847 }
1848
1849 static void HelpFontBrowserClose(void)
1850 {
1851 size_t i;
1852
1853 RemovePopUp();
1854
1855 /* Remember the directory where the browser was. */
1856 string_free(FontBrowserLastDir);
1857 FontBrowserLastDir = FontBrowserCurDir;
1858 FontBrowserLastRootSz = FontBrowserRootSz;
1859 FontBrowserCurDir = NULL;
1860 FontBrowserRootSz = 0;
1861
1862 /* Clear the other state that doesn't need to be retained. */
1863 for (i = 0; i < FontBrowserDirCount; ++i) {
1864 string_free(FontBrowserDirEntries[i]);
1865 }
1866 mem_free(FontBrowserDirEntries);
1867 FontBrowserDirEntries = NULL;
1868 FontBrowserDirCount = 0;
1869 FontBrowserDirAlloc = 0;
1870 FontBrowserDirPage = 0;
1871 for (i = 0; i < FontBrowserFileCount; ++i) {
1872 string_free(FontBrowserFileEntries[i]);
1873 }
1874 mem_free(FontBrowserFileEntries);
1875 FontBrowserFileEntries = NULL;
1876 FontBrowserFileCount = 0;
1877 FontBrowserFileAlloc = 0;
1878 FontBrowserFilePage = 0;
1879 FontBrowserFileCur = (size_t) -1;
1880 if (FontBrowserPreviewFont) {
1881 sdl_FontFree(FontBrowserPreviewFont);
1882 mem_free(FontBrowserPreviewFont);
1883 FontBrowserPreviewFont = NULL;
1884 }
1885 string_free(new_font.name);
1886 new_font.name = NULL;
1887 new_font.size = 0;
1888 new_font.preset = false;
1889 new_font.bitmapped = false;
1890 }
1891
1892 static void AcceptFontBrowser(sdl_Button *sender)
1893 {
1894 if (FontBrowserPreviewFont) {
1895 term_window *window = &windows[SelectedTerm];
1896
1897 sdl_FontFree(&window->font);
1898 string_free(window->req_font.name);
1899 assert(new_font.name);
1900 window->req_font.name = string_make(new_font.name);
1901 window->req_font.size = new_font.size;
1902 window->req_font.preset = new_font.preset;
1903 window->req_font.bitmapped = new_font.bitmapped;
1904 HelpWindowFontChange(window);
1905 }
1906 HelpFontBrowserClose();
1907 }
1908
1909 /**
1910 * Name sorting function
1911 */
1912 static int cmp_name(const void *f1, const void *f2)
1913 {
1914 const char *name1 = *(const char**)f1;
1915 const char *name2 = *(const char**)f2;
1916
1917 return strcmp(name1, name2);
1918 }
1919
1920 static void ChangeDirPageFontBrowser(sdl_Button *sender)
1921 {
1922 /*
1923 * Each page overlaps by one entry with the previous; thus the
1924 * use of FONT_BROWSER_PAGE_ENTRIES - 1 in the lines below.
1925 * If the count is greater than zero, max_page satisfies
1926 * max_page * (FONT_BROWSER_PAGE_ENTRIES - 1) < count and
1927 * max_page * (FONT_BROWSER_PAGE_ENTRIES - 1)
1928 * + FONT_BROWSER_PAGE_ENTRIES >= count.
1929 */
1930 size_t max_page = (FontBrowserDirCount > 0) ?
1931 (FontBrowserDirCount - 1) / (FONT_BROWSER_PAGE_ENTRIES - 1) : 0;
1932 sdl_Button *button;
1933 size_t page_start, i;
1934
1935 if ((FontBrowserDirPage == 0 && sender->tag < 0) ||
1936 (FontBrowserDirPage == max_page && sender->tag > 0)) {
1937 /* Scrolling that direction does nothing. */
1938 return;
1939 }
1940
1941 FontBrowserDirPage = MAX(0, MIN(max_page, FontBrowserDirPage
1942 + sender->tag));
1943
1944 /* Redo the captions and colours of the directory buttons. */
1945 page_start = FontBrowserDirPage * (FONT_BROWSER_PAGE_ENTRIES - 1);
1946 for (i = 0; i < FONT_BROWSER_PAGE_ENTRIES; ++i) {
1947 button = sdl_ButtonBankGet(&PopUp.buttons,
1948 FontBrowserDirectories[i]);
1949 if (i + page_start < FontBrowserDirCount) {
1950 /*
1951 * Truncate displayed name to at most
1952 * FONT_BROWSER_DIR_LENGTH Unicode code points.
1953 */
1954 char *caption;
1955
1956 assert(FontBrowserDirEntries
1957 && FontBrowserDirEntries[i + page_start]);
1958 caption = string_make(
1959 FontBrowserDirEntries[i + page_start]);
1960 utf8_clipto(caption, FONT_BROWSER_DIR_LENGTH);
1961 sdl_ButtonCaption(button, caption);
1962 string_free(caption);
1963 button->unsel_colour = AltUnselColour;
1964 button->sel_colour = AltSelColour;
1965 button->cap_colour = DefaultCapColour;
1966 } else {
1967 sdl_ButtonCaption(button, "");
1968 button->unsel_colour = back_colour;
1969 button->sel_colour = back_colour;
1970 button->cap_colour = back_colour;
1971 }
1972 }
1973
1974 /* Update the colors of the scroll controls if relevant. */
1975 button = sdl_ButtonBankGet(&PopUp.buttons, FontBrowserDirPageBefore);
1976 if (FontBrowserDirPage > 0) {
1977 button->unsel_colour = AltUnselColour;
1978 button->sel_colour = AltSelColour;
1979 button->cap_colour = DefaultCapColour;
1980 } else {
1981 button->unsel_colour = back_colour;
1982 button->sel_colour = back_colour;
1983 button->cap_colour = back_colour;
1984 }
1985 button = sdl_ButtonBankGet(&PopUp.buttons, FontBrowserDirPageAfter);
1986 if (FontBrowserDirPage < max_page) {
1987 button->unsel_colour = AltUnselColour;
1988 button->sel_colour = AltSelColour;
1989 button->cap_colour = DefaultCapColour;
1990 } else {
1991 button->unsel_colour = back_colour;
1992 button->sel_colour = back_colour;
1993 button->cap_colour = back_colour;
1994 }
1995
1996 PopUp.need_update = true;
1997 }
1998
1999 static void ChangeFilePageFontBrowser(sdl_Button *sender)
2000 {
2001 /*
2002 * Each page overlaps by one entry with the previous; thus the
2003 * use of FONT_BROWSER_PAGE_ENTRIES - 1 in the lines below.
2004 * If the count is greater than zero, max_page satisfies
2005 * max_page * (FONT_BROWSER_PAGE_ENTRIES - 1) < count and
2006 * max_page * (FONT_BROWSER_PAGE_ENTRIES - 1)
2007 * + FONT_BROWSER_PAGE_ENTRIES >= count.
2008 */
2009 size_t max_page = (FontBrowserFileCount > 0) ? (FontBrowserFileCount
2010 - 1) / (FONT_BROWSER_PAGE_ENTRIES - 1) : 0;
2011 sdl_Button *button;
2012 size_t page_start, i;
2013
2014 if ((FontBrowserFilePage == 0 && sender->tag < 0) ||
2015 (FontBrowserFilePage == max_page && sender->tag > 0)) {
2016 /* Scrolling that direction does nothing. */
2017 return;
2018 }
2019
2020 FontBrowserFilePage = MAX(0, MIN(max_page, FontBrowserFilePage
2021 + sender->tag));
2022
2023 /* Redo the captions and colours of the file buttons. */
2024 page_start = FontBrowserFilePage * (FONT_BROWSER_PAGE_ENTRIES - 1);
2025 for (i = 0; i < FONT_BROWSER_PAGE_ENTRIES; ++i) {
2026 button = sdl_ButtonBankGet(&PopUp.buttons,
2027 FontBrowserFiles[i]);
2028 if (i + page_start < FontBrowserFileCount) {
2029 /*
2030 * Truncate displayed name to at most
2031 * FONT_BROWSER_FILE_LENGTH characters.
2032 */
2033 char *caption;
2034
2035 assert(FontBrowserFileEntries &&
2036 FontBrowserFileEntries[i + page_start]);
2037 caption = string_make(
2038 FontBrowserFileEntries[i + page_start]);
2039 utf8_clipto(caption, FONT_BROWSER_FILE_LENGTH);
2040 sdl_ButtonCaption(button, caption);
2041 string_free(caption);
2042 button->unsel_colour = AltUnselColour;
2043 button->sel_colour = AltSelColour;
2044 if (i + page_start == FontBrowserFileCur) {
2045 button->cap_colour = AltCapColour;
2046 } else {
2047 button->cap_colour = DefaultCapColour;
2048 }
2049 } else {
2050 sdl_ButtonCaption(button, "");
2051 button->unsel_colour = back_colour;
2052 button->sel_colour = back_colour;
2053 button->cap_colour = back_colour;
2054 }
2055 }
2056
2057 /* Change the colors on the scroll controls if relevant. */
2058 button = sdl_ButtonBankGet(&PopUp.buttons, FontBrowserFilePageBefore);
2059 if (FontBrowserFilePage > 0) {
2060 button->unsel_colour = AltUnselColour;
2061 button->sel_colour = AltSelColour;
2062 button->cap_colour = DefaultCapColour;
2063 } else {
2064 button->unsel_colour = back_colour;
2065 button->sel_colour = back_colour;
2066 button->cap_colour = back_colour;
2067 }
2068 button = sdl_ButtonBankGet(&PopUp.buttons, FontBrowserFilePageAfter);
2069 if (FontBrowserFilePage < max_page) {
2070 button->unsel_colour = AltUnselColour;
2071 button->sel_colour = AltSelColour;
2072 button->cap_colour = DefaultCapColour;
2073 } else {
2074 button->unsel_colour = back_colour;
2075 button->sel_colour = back_colour;
2076 button->cap_colour = back_colour;
2077 }
2078
2079 PopUp.need_update = true;
2080 }
2081
2082 static void RefreshFontBrowser(sdl_Button *sender)
2083 {
2084 char *oldcur;
2085 ang_dir *dir;
2086 size_t i;
2087 char file_part[1024], full_path[1024];
2088
2089 /*
2090 * If there is a currently selected file, remember it so, if it
2091 * is still present, it can be used as the current selection.
2092 */
2093 if (FontBrowserFileCur != (size_t) -1) {
2094 assert(FontBrowserFileEntries
2095 && FontBrowserFileCur < FontBrowserFileCount
2096 && FontBrowserFileEntries[FontBrowserFileCur]);
2097 oldcur = FontBrowserFileEntries[FontBrowserFileCur];
2098 FontBrowserFileEntries[FontBrowserFileCur] = NULL;
2099 FontBrowserFileCur = (size_t) -1;
2100 } else {
2101 oldcur = NULL;
2102 }
2103
2104 /* Clear the old entries in the lists. */
2105 for (i = 0; i < FontBrowserDirCount; ++i) {
2106 string_free(FontBrowserDirEntries[i]);
2107 FontBrowserDirEntries[i] = NULL;
2108 }
2109 FontBrowserDirCount = 0;
2110 FontBrowserDirPage = 0;
2111 for (i = 0; i < FontBrowserFileCount; ++i) {
2112 string_free(FontBrowserFileEntries[i]);
2113 FontBrowserFileEntries[i] = NULL;
2114 }
2115 FontBrowserFileCount = 0;
2116 FontBrowserFilePage = 0;
2117
2118 /*
2119 * Build up the lists of directories and fixed-width font files
2120 * present.
2121 */
2122 assert(FontBrowserCurDir);
2123 dir = my_dopen(FontBrowserCurDir);
2124 if (!dir) {
2125 size_t sz1, sz2;
2126 int nresult;
2127
2128 /* Try ANGBAND_DIR_FONTS instead. */
2129 dir = my_dopen(ANGBAND_DIR_FONTS);
2130 if (dir == NULL) {
2131 quit_fmt("could not read the directories %s and %s",
2132 FontBrowserCurDir, ANGBAND_DIR_FONTS);
2133 }
2134 mem_free(FontBrowserCurDir);
2135 /*
2136 * Normalize the path (make it absolute with no relative
2137 * parts and no redundant path separators). That simplifies
2138 * how the font browser works up the directory tree.
2139 */
2140 sz1 = 1024;
2141 FontBrowserCurDir = mem_alloc(sz1);
2142 nresult = path_normalize(FontBrowserCurDir, sz1,
2143 ANGBAND_DIR_FONTS, true, &sz2, &FontBrowserRootSz);
2144 if (nresult == 1) {
2145 /* Try again with a buffer that should hold it. */
2146 assert(sz2 > sz1);
2147 FontBrowserCurDir = mem_realloc(FontBrowserCurDir, sz2);
2148 nresult = path_normalize(FontBrowserCurDir, sz2,
2149 ANGBAND_DIR_FONTS, true, NULL,
2150 &FontBrowserRootSz);
2151 }
2152 if (nresult != 0) {
2153 /* Could not normalize it. */
2154 quit_fmt("could not normalize %s", ANGBAND_DIR_FONTS);
2155 }
2156 dir = my_dopen(FontBrowserCurDir);
2157 if (!dir) {
2158 quit_fmt("could not open the directory, %s",
2159 FontBrowserCurDir);
2160 }
2161 }
2162 alter_ang_dir_only_files(dir, false);
2163 while (my_dread(dir, file_part, sizeof(file_part))) {
2164 path_build(full_path, sizeof(full_path), FontBrowserCurDir,
2165 file_part);
2166 if (dir_exists(full_path)) {
2167 /*
2168 * It's a directory. If it's readable and not "."
2169 * nor "..", put it in the directory list.
2170 */
2171 if (!streq(file_part, ".") && !streq(file_part, "..")) {
2172 ang_dir *dir2 = my_dopen(full_path);
2173
2174 if (dir2) {
2175 if (FontBrowserDirCount
2176 == FontBrowserDirAlloc) {
2177 if (FontBrowserDirAlloc
2178 > ((size_t) -1) / (2 * sizeof(char*))) {
2179 /*
2180 * There's too many
2181 * entries to represent
2182 * in memory.
2183 */
2184 continue;
2185 }
2186 FontBrowserDirAlloc =
2187 (FontBrowserDirAlloc == 0) ?
2188 16 : 2 * FontBrowserDirAlloc;
2189 FontBrowserDirEntries =
2190 mem_realloc(FontBrowserDirEntries,
2191 FontBrowserDirAlloc * sizeof(char*));
2192 }
2193 FontBrowserDirEntries[FontBrowserDirCount] =
2194 string_make(file_part);
2195 ++FontBrowserDirCount;
2196 my_dclose(dir2);
2197 }
2198 }
2199 } else if (is_font_file(full_path)) {
2200 /* Put it in the file list. */
2201 if (FontBrowserFileCount == FontBrowserFileAlloc) {
2202 if (FontBrowserFileAlloc
2203 > ((size_t) -1) / (2 * sizeof(char*))) {
2204 /*
2205 * There's too many entries to represent
2206 * in memory.
2207 */
2208 continue;
2209 }
2210 FontBrowserFileAlloc =
2211 (FontBrowserFileAlloc == 0) ?
2212 16 : 2 * FontBrowserFileAlloc;
2213 FontBrowserFileEntries = mem_realloc(
2214 FontBrowserFileEntries,
2215 FontBrowserFileAlloc * sizeof(char*));
2216 }
2217 FontBrowserFileEntries[FontBrowserFileCount] =
2218 string_make(file_part);
2219 ++FontBrowserFileCount;
2220 }
2221 }
2222 my_dclose(dir);
2223
2224 /* Sort the entries. */
2225 if (FontBrowserDirCount > 0) {
2226 sort(FontBrowserDirEntries, FontBrowserDirCount,
2227 sizeof(FontBrowserDirEntries[0]), cmp_name);
2228 }
2229 if (FontBrowserFileCount > 0) {
2230 sort(FontBrowserFileEntries, FontBrowserFileCount,
2231 sizeof(FontBrowserFileEntries[0]), cmp_name);
2232 }
2233
2234 /* Update what's shown. */
2235 if (oldcur) {
2236 i = 0;
2237 while (1) {
2238 if (i == FontBrowserFileCount) {
2239 sdl_Button *button;
2240
2241 /*
2242 * Didn't find the old selection. Clear
2243 * new_font and grey out the point size
2244 * controls.
2245 */
2246 string_free(new_font.name);
2247 new_font.name = NULL;
2248 new_font.size = 0;
2249 new_font.preset = false;
2250 new_font.bitmapped = false;
2251 if (FontBrowserPreviewFont) {
2252 sdl_FontFree(FontBrowserPreviewFont);
2253 mem_free(FontBrowserPreviewFont);
2254 FontBrowserPreviewFont = NULL;
2255 }
2256 button = sdl_ButtonBankGet(&PopUp.buttons,
2257 FontBrowserPtSizeBigDec);
2258 button->unsel_colour = back_colour;
2259 button->sel_colour = back_colour;
2260 button->cap_colour = back_colour;
2261 button = sdl_ButtonBankGet(&PopUp.buttons,
2262 FontBrowserPtSizeDec);
2263 button->unsel_colour = back_colour;
2264 button->sel_colour = back_colour;
2265 button->cap_colour = back_colour;
2266 button = sdl_ButtonBankGet(&PopUp.buttons,
2267 FontBrowserPtSizeInc);
2268 button->unsel_colour = back_colour;
2269 button->sel_colour = back_colour;
2270 button->cap_colour = back_colour;
2271 button = sdl_ButtonBankGet(&PopUp.buttons,
2272 FontBrowserPtSizeBigInc);
2273 button->unsel_colour = back_colour;
2274 button->sel_colour = back_colour;
2275 button->cap_colour = back_colour;
2276 PopUp.need_update = true;
2277 break;
2278 }
2279 if (streq(FontBrowserFileEntries[i], oldcur)) {
2280 /*
2281 * Found the old selection. Mark it and
2282 * make sure it's in the current page
2283 * displayed. The details in new_font,
2284 * FontBrowserPreviewFont, and the status
2285 * of the point size controls have not changed
2286 * so they're left as they are.
2287 */
2288 FontBrowserFileCur = i;
2289 FontBrowserFilePage =
2290 i / (FONT_BROWSER_PAGE_ENTRIES - 1);
2291 break;
2292 }
2293 ++i;
2294 }
2295 string_free(oldcur);
2296 }
2297 ChangeDirPageFontBrowser(sender);
2298 ChangeFilePageFontBrowser(sender);
2299 }
2300
2301 static void CancelFontBrowser(sdl_Button *sender)
2302 {
2303 HelpFontBrowserClose();
2304 }
2305
2306 static void GoUpFontBrowser(sdl_Button *sender)
2307 {
2308 size_t idx = strlen(FontBrowserCurDir);
2309 sdl_Button *button;
2310
2311 assert(idx >= 1 && FontBrowserCurDir[idx - 1] == PATH_SEPC);
2312 if (idx <= FontBrowserRootSz) {
2313 /* Can't go up any further. */
2314 return;
2315 }
2316
2317 /* Forget the font that was being previewed. */
2318 FontBrowserFileCur = (size_t) -1;
2319 if (FontBrowserPreviewFont) {
2320 sdl_FontFree(FontBrowserPreviewFont);
2321 mem_free(FontBrowserPreviewFont);
2322 FontBrowserPreviewFont = NULL;
2323 }
2324 string_free(new_font.name);
2325 new_font.name = NULL;
2326 new_font.size = 0;
2327 new_font.preset = false;
2328 new_font.bitmapped = false;
2329
2330 /*
2331 * Set up the path to the new directory. First strip off the trailing
2332 * path separator.
2333 */
2334 FontBrowserCurDir[idx - 1] = '\0';
2335 /*
2336 * Then drop the last path component, leaving a trailing path
2337 * separator.
2338 */
2339 idx = path_filename_index(FontBrowserCurDir);
2340 assert(idx >= FontBrowserRootSz);
2341 FontBrowserCurDir[idx] = '\0';
2342
2343 /* Can a path element be removed and still leave something? */
2344 button = sdl_ButtonBankGet(&PopUp.buttons, FontBrowserDirUp);
2345 if (idx > FontBrowserRootSz) {
2346 button->unsel_colour = AltUnselColour;
2347 button->sel_colour = AltSelColour;
2348 button->cap_colour = DefaultCapColour;
2349 } else {
2350 button->unsel_colour = back_colour;
2351 button->sel_colour = back_colour;
2352 button->cap_colour = back_colour;
2353 }
2354
2355 /*
2356 * Because there's no longer a selected font, grey out the point size
2357 * controls.
2358 */
2359 button = sdl_ButtonBankGet(&PopUp.buttons, FontBrowserPtSizeBigDec);
2360 button->unsel_colour = back_colour;
2361 button->sel_colour = back_colour;
2362 button->cap_colour = back_colour;
2363 button = sdl_ButtonBankGet(&PopUp.buttons, FontBrowserPtSizeDec);
2364 button->unsel_colour = back_colour;
2365 button->sel_colour = back_colour;
2366 button->cap_colour = back_colour;
2367 button = sdl_ButtonBankGet(&PopUp.buttons, FontBrowserPtSizeInc);
2368 button->unsel_colour = back_colour;
2369 button->sel_colour = back_colour;
2370 button->cap_colour = back_colour;
2371 button = sdl_ButtonBankGet(&PopUp.buttons, FontBrowserPtSizeBigInc);
2372 button->unsel_colour = back_colour;
2373 button->sel_colour = back_colour;
2374 button->cap_colour = back_colour;
2375
2376 /* Refresh the lists of files and directories. */
2377 button = sdl_ButtonBankGet(&PopUp.buttons, FontBrowserRefresh);
2378 RefreshFontBrowser(button);
2379
2380 PopUp.need_update = true;
2381 }
2382
2383 static void SelectDirFontBrowser(sdl_Button *sender)
2384 {
2385 sdl_Button *button;
2386 char *full_path;
2387 size_t sz, page_start = FontBrowserDirPage
2388 * (FONT_BROWSER_PAGE_ENTRIES - 1);
2389
2390 assert(sender->tag >= 0 && sender->tag < FONT_BROWSER_PAGE_ENTRIES);
2391 if (page_start + sender->tag >= FontBrowserDirCount) {
2392 /* Directory entry is past the end; do nothing. */
2393 return;
2394 }
2395
2396 /* Forget the font that was being previewed. */
2397 FontBrowserFileCur = (size_t) -1;
2398 if (FontBrowserPreviewFont) {
2399 sdl_FontFree(FontBrowserPreviewFont);
2400 mem_free(FontBrowserPreviewFont);
2401 FontBrowserPreviewFont = NULL;
2402 }
2403 string_free(new_font.name);
2404 new_font.name = NULL;
2405 new_font.size = 0;
2406 new_font.preset = false;
2407 new_font.bitmapped = false;
2408
2409 /* Set up the path to the new directory. */
2410 sz = strlen(FontBrowserCurDir);
2411 assert(FontBrowserCurDir && sz >= 1
2412 && FontBrowserCurDir[sz - 1] == PATH_SEPC);
2413 assert(FontBrowserDirEntries
2414 && FontBrowserDirEntries[page_start + sender->tag]);
2415 sz += strlen(FontBrowserDirEntries[page_start + sender->tag]) + 2;
2416 full_path = mem_alloc(sz);
2417 strnfmt(full_path, sz, "%s%s%c", FontBrowserCurDir,
2418 FontBrowserDirEntries[page_start + sender->tag], PATH_SEPC);
2419 mem_free(FontBrowserCurDir);
2420 FontBrowserCurDir = full_path;
2421
2422 /*
2423 * Having drilled down into a directory, it'll be possible to go back
2424 * up.
2425 */
2426 button = sdl_ButtonBankGet(&PopUp.buttons, FontBrowserDirUp);
2427 button->unsel_colour = AltUnselColour;
2428 button->sel_colour = AltSelColour;
2429 button->cap_colour = DefaultCapColour;
2430
2431 /*
2432 * Because there's no longer a selected font, grey out the point size
2433 * controls.
2434 */
2435 button = sdl_ButtonBankGet(&PopUp.buttons, FontBrowserPtSizeBigDec);
2436 button->unsel_colour = back_colour;
2437 button->sel_colour = back_colour;
2438 button->cap_colour = back_colour;
2439 button = sdl_ButtonBankGet(&PopUp.buttons, FontBrowserPtSizeDec);
2440 button->unsel_colour = back_colour;
2441 button->sel_colour = back_colour;
2442 button->cap_colour = back_colour;
2443 button = sdl_ButtonBankGet(&PopUp.buttons, FontBrowserPtSizeInc);
2444 button->unsel_colour = back_colour;
2445 button->sel_colour = back_colour;
2446 button->cap_colour = back_colour;
2447 button = sdl_ButtonBankGet(&PopUp.buttons, FontBrowserPtSizeBigInc);
2448 button->unsel_colour = back_colour;
2449 button->sel_colour = back_colour;
2450 button->cap_colour = back_colour;
2451
2452 /* Refresh the lists of files and directories. */
2453 button = sdl_ButtonBankGet(&PopUp.buttons, FontBrowserRefresh);
2454 RefreshFontBrowser(button);
2455
2456 PopUp.need_update = true;
2457 }
2458
2459 static void SelectFileFontBrowser(sdl_Button *sender)
2460 {
2461 sdl_Button *button;
2462 char *work;
2463 size_t sz1, sz2, page_start = FontBrowserFilePage
2464 * (FONT_BROWSER_PAGE_ENTRIES - 1);
2465 int nresult;
2466
2467 assert(sender->tag >= 0 && sender->tag < FONT_BROWSER_PAGE_ENTRIES);
2468 if (page_start + sender->tag >= FontBrowserFileCount) {
2469 /* File entry is past the end; do nothing. */
2470 return;
2471 }
2472
2473 if (page_start + sender->tag == FontBrowserFileCur) {
2474 /* It's the same selection as before, do nothing. */
2475 return;
2476 }
2477
2478 /* Fill in some details about the new font to preview. */
2479 assert(FontBrowserFileEntries
2480 && FontBrowserFileEntries[page_start + sender->tag]);
2481 string_free(new_font.name);
2482 /* If the font is in ANGBAND_DIR_FONTS, it is a preset font. */
2483 sz1 = strlen(FontBrowserCurDir)
2484 + strlen(FontBrowserFileEntries[page_start + sender->tag]) + 2;
2485 work = mem_alloc(sz1);
2486 nresult = path_normalize(work, sz1, ANGBAND_DIR_FONTS, true,
2487 &sz2, NULL);
2488 if (nresult == 1) {
2489 /* Try again with a buffer that should hold it. */
2490 assert(sz2 > sz1);
2491 work = mem_realloc(work, sz2);
2492 nresult = path_normalize(work, sz2, ANGBAND_DIR_FONTS, true,
2493 NULL, NULL);
2494 }
2495 if (nresult == 0 && streq(FontBrowserCurDir, work)) {
2496 new_font.name = string_make(
2497 FontBrowserFileEntries[page_start + sender->tag]);
2498 new_font.preset = true;
2499 mem_free(work);
2500 } else {
2501 path_build(work, sz1, FontBrowserCurDir,
2502 FontBrowserFileEntries[page_start + sender->tag]);
2503 new_font.name = work;
2504 new_font.preset = false;
2505 }
2506 if (suffix_i(new_font.name, ".fon")) {
2507 new_font.size = 0;
2508 new_font.bitmapped = true;
2509 } else {
2510 /*
2511 * If were not previewing a scalable font, then either use
2512 * the size of the currently selected font for the window or
2513 * the default size, as the size to start with the new font.
2514 * Otherwise, keep the size that was used for the previous
2515 * preview.
2516 */
2517 if (FontBrowserFileCur == (size_t) -1 || new_font.bitmapped) {
2518 term_window *window = &windows[SelectedTerm];
2519
2520 if (!window->req_font.bitmapped
2521 && window->req_font.size >= MIN_POINT_SIZE
2522 && window->req_font.size <= MAX_POINT_SIZE) {
2523 new_font.size = window->req_font.size;
2524 } else {
2525 new_font.size = DEFAULT_POINT_SIZE;
2526 }
2527 }
2528 new_font.bitmapped = false;
2529 }
2530
2531 /* Change the color on the button for the currently selected font. */
2532 sender->cap_colour = AltCapColour;
2533
2534 /*
2535 * Change the color on the button for the old file selection, if
2536 * visible.
2537 */
2538 if (FontBrowserFileCur >= page_start && FontBrowserFileCur < page_start
2539 + FONT_BROWSER_PAGE_ENTRIES) {
2540 button = sdl_ButtonBankGet(&PopUp.buttons,
2541 FontBrowserFiles[FontBrowserFileCur - page_start]);
2542 button->cap_colour = DefaultCapColour;
2543 }
2544
2545 /* Remember the selection. */
2546 FontBrowserFileCur = page_start + sender->tag;
2547
2548 /* Change the colors on the point size controls as appropriate. */
2549 if (!new_font.bitmapped && new_font.size > MIN_POINT_SIZE) {
2550 button = sdl_ButtonBankGet(&PopUp.buttons,
2551 FontBrowserPtSizeBigDec);
2552 button->unsel_colour = AltUnselColour;
2553 button->sel_colour = AltSelColour;
2554 button->cap_colour = DefaultCapColour;
2555 button = sdl_ButtonBankGet(&PopUp.buttons,
2556 FontBrowserPtSizeDec);
2557 button->unsel_colour = AltUnselColour;
2558 button->sel_colour = AltSelColour;
2559 button->cap_colour = DefaultCapColour;
2560 } else {
2561 button = sdl_ButtonBankGet(&PopUp.buttons,
2562 FontBrowserPtSizeBigDec);
2563 button->unsel_colour = back_colour;
2564 button->sel_colour = back_colour;
2565 button->cap_colour = back_colour;
2566 button = sdl_ButtonBankGet(&PopUp.buttons,
2567 FontBrowserPtSizeDec);
2568 button->unsel_colour = back_colour;
2569 button->sel_colour = back_colour;
2570 button->cap_colour = back_colour;
2571 }
2572 if (!new_font.bitmapped && new_font.size < MAX_POINT_SIZE) {
2573 button = sdl_ButtonBankGet(&PopUp.buttons,
2574 FontBrowserPtSizeInc);
2575 button->unsel_colour = AltUnselColour;
2576 button->sel_colour = AltSelColour;
2577 button->cap_colour = DefaultCapColour;
2578 button = sdl_ButtonBankGet(&PopUp.buttons,
2579 FontBrowserPtSizeBigInc);
2580 button->unsel_colour = AltUnselColour;
2581 button->sel_colour = AltSelColour;
2582 button->cap_colour = DefaultCapColour;
2583 } else {
2584 button = sdl_ButtonBankGet(&PopUp.buttons,
2585 FontBrowserPtSizeInc);
2586 button->unsel_colour = back_colour;
2587 button->sel_colour = back_colour;
2588 button->cap_colour = back_colour;
2589 button = sdl_ButtonBankGet(&PopUp.buttons,
2590 FontBrowserPtSizeBigInc);
2591 button->unsel_colour = back_colour;
2592 button->sel_colour = back_colour;
2593 button->cap_colour = back_colour;
2594 }
2595
2596 /* Try to load the newly selected font. */
2597 if (FontBrowserPreviewFont) {
2598 sdl_FontFree(FontBrowserPreviewFont);
2599 } else {
2600 FontBrowserPreviewFont =
2601 mem_alloc(sizeof(*FontBrowserPreviewFont));
2602 }
2603 if (sdl_FontCreate(FontBrowserPreviewFont, &new_font, PopUp.surface)) {
2604 /* Failed. */
2605 mem_free(FontBrowserPreviewFont);
2606 FontBrowserPreviewFont = NULL;
2607 }
2608
2609 PopUp.need_update = true;
2610 }
2611
2612 static void ChangePtSzFontBrowser(sdl_Button *sender)
2613 {
2614 sdl_Button *button;
2615
2616 if (FontBrowserFileCur == (size_t) -1 || new_font.bitmapped) {
2617 /* Size changes aren't relevant. */
2618 return;
2619 }
2620 if ((new_font.size == MIN_POINT_SIZE && sender->tag < 0)
2621 || (new_font.size == MAX_POINT_SIZE
2622 && sender->tag > 0)) {
2623 /* Size doesn't change. */
2624 return;
2625 }
2626
2627 new_font.size = MAX(MIN_POINT_SIZE, MIN(MAX_POINT_SIZE,
2628 new_font.size + sender->tag));
2629
2630 /* Change colors on the buttons. */
2631 if (new_font.size > MIN_POINT_SIZE) {
2632 button = sdl_ButtonBankGet(&PopUp.buttons,
2633 FontBrowserPtSizeBigDec);
2634 button->unsel_colour = AltUnselColour;
2635 button->sel_colour = AltSelColour;
2636 button->cap_colour = DefaultCapColour;
2637 button = sdl_ButtonBankGet(&PopUp.buttons,
2638 FontBrowserPtSizeDec);
2639 button->unsel_colour = AltUnselColour;
2640 button->sel_colour = AltSelColour;
2641 button->cap_colour = DefaultCapColour;
2642 } else {
2643 button = sdl_ButtonBankGet(&PopUp.buttons,
2644 FontBrowserPtSizeBigDec);
2645 button->unsel_colour = back_colour;
2646 button->sel_colour = back_colour;
2647 button->cap_colour = back_colour;
2648 button = sdl_ButtonBankGet(&PopUp.buttons,
2649 FontBrowserPtSizeDec);
2650 button->unsel_colour = back_colour;
2651 button->sel_colour = back_colour;
2652 button->cap_colour = back_colour;
2653 }
2654 if (new_font.size < MAX_POINT_SIZE) {
2655 button = sdl_ButtonBankGet(&PopUp.buttons,
2656 FontBrowserPtSizeInc);
2657 button->unsel_colour = AltUnselColour;
2658 button->sel_colour = AltSelColour;
2659 button->cap_colour = DefaultCapColour;
2660 button = sdl_ButtonBankGet(&PopUp.buttons,
2661 FontBrowserPtSizeBigInc);
2662 button->unsel_colour = AltUnselColour;
2663 button->sel_colour = AltSelColour;
2664 button->cap_colour = DefaultCapColour;
2665 } else {
2666 button = sdl_ButtonBankGet(&PopUp.buttons,
2667 FontBrowserPtSizeInc);
2668 button->unsel_colour = back_colour;
2669 button->sel_colour = back_colour;
2670 button->cap_colour = back_colour;
2671 button = sdl_ButtonBankGet(&PopUp.buttons,
2672 FontBrowserPtSizeBigInc);
2673 button->unsel_colour = back_colour;
2674 button->sel_colour = back_colour;
2675 button->cap_colour = back_colour;
2676 }
2677
2678 /* Try to create the preview font at the new size. */
2679 if (FontBrowserPreviewFont) {
2680 sdl_FontFree(FontBrowserPreviewFont);
2681 } else {
2682 FontBrowserPreviewFont =
2683 mem_alloc(sizeof(*FontBrowserPreviewFont));
2684 }
2685 if (sdl_FontCreate(FontBrowserPreviewFont, &new_font, PopUp.surface)) {
2686 /* Failed. */
2687 mem_free(FontBrowserPreviewFont);
2688 FontBrowserPreviewFont = NULL;
2689 }
2690
2691 PopUp.need_update = true;
2692 }
2693
2694 static void DrawFontBrowser(sdl_Window *win)
2695 {
2696 int filepanel_left = FONT_BROWSER_BORDER + FONT_BROWSER_MARGIN
2697 + 2 * (FONT_BROWSER_SUB_BORDER + FONT_BROWSER_SUB_MARGIN)
2698 + (FONT_BROWSER_DIR_LENGTH + 1) * PopUp.font.width
2699 + FONT_BROWSER_HOR_SPACE + 3 * PopUp.font.width
2700 + 2 * PopUp.font.width;
2701 int subpanel_bottom = FONT_BROWSER_BORDER + FONT_BROWSER_MARGIN
2702 + PopUp.font.height + 2 + 2 * (FONT_BROWSER_SUB_BORDER
2703 + FONT_BROWSER_SUB_MARGIN) + FONT_BROWSER_PAGE_ENTRIES
2704 * (PopUp.font.height + 2);
2705 sdl_Button *button;
2706 SDL_Rect rc;
2707
2708 /* Draw the panel border. */
2709 RECT(0, 0, win->width, win->height, &rc);
2710 sdl_DrawBox(win->surface, &rc, AltUnselColour, FONT_BROWSER_BORDER);
2711
2712 /* Draw the subpanel labels. */
2713 sdl_WindowText(win, AltUnselColour, FONT_BROWSER_BORDER +
2714 FONT_BROWSER_MARGIN, FONT_BROWSER_BORDER + FONT_BROWSER_MARGIN,
2715 "Directories");
2716 sdl_WindowText(win, AltUnselColour, filepanel_left, FONT_BROWSER_BORDER
2717 + FONT_BROWSER_MARGIN, "Fixed-width Fonts");
2718
2719 /* Draw the directory subpanel border. */
2720 RECT(FONT_BROWSER_BORDER + FONT_BROWSER_MARGIN, FONT_BROWSER_BORDER
2721 + FONT_BROWSER_MARGIN + PopUp.font.height + 2,
2722 filepanel_left - 2 * PopUp.font.width - FONT_BROWSER_BORDER
2723 - FONT_BROWSER_MARGIN,
2724 subpanel_bottom - PopUp.font.height - 2 - FONT_BROWSER_BORDER
2725 - FONT_BROWSER_MARGIN, &rc);
2726 sdl_DrawBox(win->surface, &rc, AltUnselColour, FONT_BROWSER_SUB_BORDER);
2727
2728 /* Draw the file subpanel border. */
2729 RECT(filepanel_left, FONT_BROWSER_BORDER + FONT_BROWSER_MARGIN
2730 + PopUp.font.height + 2, win->width - FONT_BROWSER_BORDER
2731 - FONT_BROWSER_MARGIN - filepanel_left, subpanel_bottom
2732 - PopUp.font.height - 2 - FONT_BROWSER_BORDER
2733 - FONT_BROWSER_MARGIN, &rc);
2734 sdl_DrawBox(win->surface, &rc, AltUnselColour, FONT_BROWSER_SUB_BORDER);
2735
2736 /* Draw the point size label as appropriate. */
2737 button = sdl_ButtonBankGet(&PopUp.buttons, FontBrowserPtSizeBigDec);
2738 if (FontBrowserFileCur != (size_t) -1 && !new_font.bitmapped) {
2739 assert(new_font.size >= MIN_POINT_SIZE
2740 && new_font.size <= MAX_POINT_SIZE);
2741 sdl_WindowText(win, AltUnselColour, button->pos.x
2742 + 12 * PopUp.font.width, button->pos.y + 1,
2743 format("%d pt", new_font.size));
2744 } else {
2745 int n = (int) strlen(format("%d pt", MAX_POINT_SIZE));
2746
2747 RECT(button->pos.x + 12 * PopUp.font.width, button->pos.y + 1,
2748 n * PopUp.font.width, PopUp.font.height, &rc);
2749 SDL_FillRect(win->surface, &rc, back_pixel_colour);
2750 }
2751
2752 if (FontBrowserPreviewFont) {
2753 /*
2754 * Redraw the preview area. Like sdl_FontDraw() but do not
2755 * use the font associated with the window. Include x07 in
2756 * the preview to see what glyph is there for the code page
2757 * 437 centered dot. Include the UTF-8 sequence xC2 xB7 to
2758 * see what glyph is there for U+00B7, the Unicode centered
2759 * dot.
2760 */
2761 const char *preview_contents[5] = {
2762 "abcdefghijklmnopqrst",
2763 "uvwxyz1234567890-=,.",
2764 "ABCDEFGHIJKLMNOPQRST",
2765 "UVWXYZ!@#$%^&*()_+<>",
2766 "/?;:'\"[{]}\\|`~\x07\xC2\xB7 "
2767 };
2768 int preview_bottom, i;
2769
2770 if (SDL_MUSTLOCK(win->surface)) {
2771 if (SDL_LockSurface(win->surface) < 0) {
2772 return;
2773 }
2774 }
2775
2776 if (20 * FontBrowserPreviewFont->width
2777 > win->width - 2 * (FONT_BROWSER_BORDER
2778 + FONT_BROWSER_MARGIN)) {
2779 /* Doesn't fit horizontally. Clip. */
2780 rc.x = FONT_BROWSER_BORDER + FONT_BROWSER_MARGIN;
2781 rc.w = win->width - 2 * (FONT_BROWSER_BORDER
2782 + FONT_BROWSER_MARGIN);
2783 } else {
2784 rc.x = (win->width - 20
2785 * FontBrowserPreviewFont->width) / 2;
2786 rc.w = 20 * FontBrowserPreviewFont->width;
2787 }
2788 rc.y = subpanel_bottom + FONT_BROWSER_VER_SPACE
2789 + PopUp.font.height + 2 + FONT_BROWSER_VER_SPACE;
2790 assert(rc.y < win->height - FONT_BROWSER_PREVIEW_HEIGHT);
2791 preview_bottom = rc.y + FONT_BROWSER_PREVIEW_HEIGHT;
2792 if (FontBrowserPreviewFont->height
2793 > FONT_BROWSER_PREVIEW_HEIGHT) {
2794 /* One row doesn't fit vertically. Clip. */
2795 rc.h = FONT_BROWSER_PREVIEW_HEIGHT;
2796 } else {
2797 rc.h = FontBrowserPreviewFont->height;
2798 }
2799 for (i = 0; i < 5; ++i) {
2800 SDL_Surface *text = TTF_RenderUTF8_Solid(
2801 FontBrowserPreviewFont->sdl_font,
2802 preview_contents[i], AltUnselColour);
2803
2804 if (text) {
2805 SDL_BlitSurface(text, NULL, PopUp.surface, &rc);
2806 SDL_FreeSurface(text);
2807 }
2808 rc.y += FontBrowserPreviewFont->height;
2809 if (rc.y >= preview_bottom) {
2810 break;
2811 }
2812 if (rc.y + FontBrowserPreviewFont->height
2813 > preview_bottom) {
2814 rc.h = preview_bottom - rc.y;
2815 }
2816 }
2817
2818 if (SDL_MUSTLOCK(win->surface)) {
2819 SDL_UnlockSurface(win->surface);
2820 }
2821 }
2822 }
2823
2824 /*
2825 * See the comment for ActivatePointSize() for why enabling and disabling
2826 * buttons is done by coloring them rather than using sdl_ButtonVisible().
2827 */
2828 static void ActivateFontBrowser(sdl_Button *sender)
2829 {
2830 term_window *window = &windows[SelectedTerm];
2831 int ptsz_width = ((int) strlen(format("%d pt", MAX_POINT_SIZE)))
2832 * PopUp.font.width;
2833 int height, width;
2834 int subpanel_top, subpanel_bottom;
2835 int dirpanel_right, filepanel_left, filepanel_right, ptsize_left;
2836 sdl_Button *button;
2837 int i;
2838
2839 RemovePopUp();
2840
2841 /*
2842 * Clear new_font, since a new font file hasn't been selected by this
2843 * use of the browser.
2844 */
2845 FontBrowserFileCur = (size_t) -1;
2846 if (FontBrowserPreviewFont) {
2847 sdl_FontFree(FontBrowserPreviewFont);
2848 mem_free(FontBrowserPreviewFont);
2849 FontBrowserPreviewFont = NULL;
2850 }
2851 string_free(new_font.name);
2852 new_font.name = NULL;
2853 new_font.size = 0;
2854 new_font.preset = false;
2855 new_font.bitmapped = false;
2856
2857 /*
2858 * If the current font is not preset, use its directory as the
2859 * starting point for the browser; otherwise, use the last directory
2860 * browsed by the browser or, if no browsing has been done so far,
2861 * ANGBAND_DIR_FONTS.
2862 */
2863 mem_free(FontBrowserCurDir);
2864 FontBrowserCurDir = NULL;
2865 if (!window->req_font.preset && window->req_font.name) {
2866 size_t sz1, sz2, file_index;
2867 int nresult;
2868
2869 /*
2870 * Normalize the path. It could have come from the preference
2871 * file and may not be already normalized.
2872 */
2873 sz1 = strlen(window->req_font.name) + 1;
2874 FontBrowserCurDir = mem_alloc(sz1);
2875 nresult = path_normalize(FontBrowserCurDir, sz1,
2876 window->req_font.name, false, &sz2, &FontBrowserRootSz);
2877 if (nresult == 1) {
2878 /* Try again with a buffer that should hold it. */
2879 assert(sz2 > sz1);
2880 FontBrowserCurDir = mem_realloc(FontBrowserCurDir, sz2);
2881 nresult = path_normalize(FontBrowserCurDir, sz2,
2882 window->req_font.name, false, NULL,
2883 &FontBrowserRootSz);
2884 }
2885 if (nresult == 0) {
2886 /*
2887 * Terminate just past the last directory separator in
2888 * the path name.
2889 */
2890 file_index = path_filename_index(FontBrowserCurDir);
2891 assert(file_index >= FontBrowserRootSz);
2892 FontBrowserCurDir[file_index] = '\0';
2893 } else {
2894 mem_free(FontBrowserCurDir);
2895 FontBrowserCurDir = NULL;
2896 }
2897 } else if (FontBrowserLastDir) {
2898 FontBrowserCurDir = string_make(FontBrowserLastDir);
2899 FontBrowserRootSz = FontBrowserLastRootSz;
2900 }
2901 if (!FontBrowserCurDir) {
2902 size_t sz1, sz2;
2903 int nresult;
2904
2905 sz1 = 1024;
2906 FontBrowserCurDir = mem_alloc(sz1);
2907 nresult = path_normalize(FontBrowserCurDir, sz1,
2908 ANGBAND_DIR_FONTS, true, &sz2, &FontBrowserRootSz);
2909 if (nresult == 1) {
2910 /* Try again with a buffer that should hold it. */
2911 assert(sz2 > sz1);
2912 FontBrowserCurDir = mem_realloc(FontBrowserCurDir, sz2);
2913 nresult = path_normalize(FontBrowserCurDir, sz2,
2914 ANGBAND_DIR_FONTS, true, NULL,
2915 &FontBrowserRootSz);
2916 }
2917 if (nresult != 0) {
2918 quit_fmt("could not normalize %s", ANGBAND_DIR_FONTS);
2919 }
2920 }
2921
2922 /* Set the window for the browser panel. */
2923 /* First account for the outside border. */
2924 height = 2 * (FONT_BROWSER_BORDER + FONT_BROWSER_MARGIN);
2925 width = 2 * (FONT_BROWSER_BORDER + FONT_BROWSER_MARGIN);
2926 /*
2927 * Then for the directory and file subpanels with their labels
2928 * and borders.
2929 */
2930 height += 2 * (FONT_BROWSER_SUB_BORDER + FONT_BROWSER_SUB_MARGIN)
2931 + PopUp.font.height + 2
2932 + FONT_BROWSER_PAGE_ENTRIES * (PopUp.font.height + 2);
2933 width += 4 * (FONT_BROWSER_SUB_BORDER + FONT_BROWSER_SUB_MARGIN)
2934 + (FONT_BROWSER_DIR_LENGTH + 1) * PopUp.font.width
2935 + (FONT_BROWSER_FILE_LENGTH + 1) * PopUp.font.width;
2936 /*
2937 * Now account for the scroll controls and a two character wide
2938 * space between the file and directory subpanels.
2939 */
2940 width += 2 * (FONT_BROWSER_HOR_SPACE + 3 * PopUp.font.width)
2941 + 2 * PopUp.font.width;
2942 /*
2943 * Account for the point size control and its separation from the
2944 * subpanels.
2945 */
2946 height += FONT_BROWSER_VER_SPACE + PopUp.font.height + 2;
2947 /*
2948 * Account for the preview and its separation from the point size
2949 * control.
2950 */
2951 height += FONT_BROWSER_VER_SPACE + FONT_BROWSER_PREVIEW_HEIGHT;
2952 /*
2953 * Account for the action buttons and their one character high
2954 * separation from the preview.
2955 */
2956 height += 2 * (PopUp.font.height + 2);
2957 sdl_WindowInit(&PopUp, width, height, AppWin, StatusBar.font.req);
2958 PopUp.left = AppWin->w / 2 - width / 2;
2959 PopUp.top = StatusBar.height + 2;
2960 PopUp.draw_extra = DrawFontBrowser;
2961
2962 /* Set buttons for the directory and file subpanels. */
2963 subpanel_top = FONT_BROWSER_BORDER + FONT_BROWSER_MARGIN
2964 + PopUp.font.height + 2;
2965 dirpanel_right = FONT_BROWSER_BORDER + FONT_BROWSER_MARGIN
2966 + 2 * (FONT_BROWSER_SUB_BORDER + FONT_BROWSER_SUB_MARGIN)
2967 + (FONT_BROWSER_DIR_LENGTH + 1) * PopUp.font.width
2968 + FONT_BROWSER_HOR_SPACE + 3 * PopUp.font.width;
2969 FontBrowserDirUp = sdl_ButtonBankNew(&PopUp.buttons);
2970 button = sdl_ButtonBankGet(&PopUp.buttons, FontBrowserDirUp);
2971 /*
2972 * Grey it out if there's not at least one element in the path that
2973 * can be stripped off while still leaving something in the path.
2974 */
2975 if (strlen(FontBrowserCurDir) <= FontBrowserRootSz) {
2976 button->unsel_colour = back_colour;
2977 button->sel_colour = back_colour;
2978 button->cap_colour = back_colour;
2979 } else {
2980 button->unsel_colour = AltUnselColour;
2981 button->sel_colour = AltSelColour;
2982 button->cap_colour = DefaultCapColour;
2983 }
2984 sdl_ButtonSize(button, 4 * PopUp.font.width, PopUp.font.height + 2);
2985 sdl_ButtonVisible(button, true);
2986 sdl_ButtonCaption(button, "Up");
2987 sdl_ButtonMove(button, dirpanel_right - 4 * PopUp.font.width,
2988 subpanel_top - PopUp.font.height - 2);
2989 button->activate = GoUpFontBrowser;
2990 for (i = 0; i < FONT_BROWSER_PAGE_ENTRIES; ++i) {
2991 FontBrowserDirectories[i] = sdl_ButtonBankNew(&PopUp.buttons);
2992 button = sdl_ButtonBankGet(&PopUp.buttons,
2993 FontBrowserDirectories[i]);
2994 /* Grey it out until needed. */
2995 button->unsel_colour = back_colour;
2996 button->sel_colour = back_colour;
2997 button->cap_colour = back_colour;
2998 sdl_ButtonSize(button, (FONT_BROWSER_DIR_LENGTH + 1)
2999 * PopUp.font.width, PopUp.font.height + 2);
3000 sdl_ButtonVisible(button, true);
3001 sdl_ButtonCaption(button, "");
3002 sdl_ButtonMove(button, FONT_BROWSER_BORDER
3003 + FONT_BROWSER_MARGIN + FONT_BROWSER_SUB_BORDER
3004 + FONT_BROWSER_SUB_MARGIN, subpanel_top
3005 + FONT_BROWSER_SUB_BORDER + FONT_BROWSER_SUB_MARGIN
3006 + i * (PopUp.font.height + 2));
3007 button->tag = i;
3008 button->activate = SelectDirFontBrowser;
3009 }
3010 dirpanel_right = FONT_BROWSER_BORDER + FONT_BROWSER_MARGIN
3011 + 2 * (FONT_BROWSER_SUB_BORDER + FONT_BROWSER_SUB_MARGIN)
3012 + (FONT_BROWSER_DIR_LENGTH + 1) * PopUp.font.width
3013 + FONT_BROWSER_HOR_SPACE + 3 * PopUp.font.width;
3014 FontBrowserDirPageBefore = sdl_ButtonBankNew(&PopUp.buttons);
3015 button = sdl_ButtonBankGet(&PopUp.buttons, FontBrowserDirPageBefore);
3016 /* Grey it out until needed. */
3017 button->unsel_colour = back_colour;
3018 button->sel_colour = back_colour;
3019 button->cap_colour = back_colour;
3020 sdl_ButtonSize(button, 3 * PopUp.font.width, PopUp.font.height + 2);
3021 sdl_ButtonVisible(button, true);
3022 sdl_ButtonCaption(button, "-");
3023 sdl_ButtonMove(button, dirpanel_right - 3 * PopUp.font.width
3024 - FONT_BROWSER_SUB_BORDER - FONT_BROWSER_SUB_MARGIN,
3025 subpanel_top + FONT_BROWSER_SUB_BORDER
3026 + FONT_BROWSER_SUB_MARGIN);
3027 button->tag = -1;
3028 button->activate = ChangeDirPageFontBrowser;
3029 FontBrowserDirPageAfter = sdl_ButtonBankNew(&PopUp.buttons);
3030 button = sdl_ButtonBankGet(&PopUp.buttons, FontBrowserDirPageAfter);
3031 /* Grey it out until needed. */
3032 button->unsel_colour = back_colour;
3033 button->sel_colour = back_colour;
3034 button->cap_colour = back_colour;
3035 sdl_ButtonSize(button, 3 * PopUp.font.width, PopUp.font.height + 2);
3036 sdl_ButtonVisible(button, true);
3037 sdl_ButtonCaption(button, "+");
3038 sdl_ButtonMove(button, dirpanel_right - FONT_BROWSER_SUB_BORDER
3039 - FONT_BROWSER_SUB_MARGIN - 3 * PopUp.font.width,
3040 subpanel_top + FONT_BROWSER_SUB_BORDER
3041 + FONT_BROWSER_SUB_MARGIN + (FONT_BROWSER_PAGE_ENTRIES - 1)
3042 * (PopUp.font.height + 2));
3043 button->tag = 1;
3044 button->activate = ChangeDirPageFontBrowser;
3045 /*
3046 * Have a button that does nothing between the page before and page
3047 * after buttons so that stray clicks in that area don't dismiss
3048 * the 'Font Browser' panel. If the button had access to the click
3049 * coordinates, it could implement jumping to a page.
3050 */
3051 FontBrowserDirPageDummy = sdl_ButtonBankNew(&PopUp.buttons);
3052 button = sdl_ButtonBankGet(&PopUp.buttons, FontBrowserDirPageDummy);
3053 button->unsel_colour = back_colour;
3054 button->sel_colour = back_colour;
3055 button->cap_colour = back_colour;
3056 sdl_ButtonSize(button, 3 * PopUp.font.width, (FONT_BROWSER_PAGE_ENTRIES
3057 - 2) * (PopUp.font.height + 2));
3058 sdl_ButtonVisible(button, true);
3059 sdl_ButtonCaption(button, "");
3060 sdl_ButtonMove(button, dirpanel_right - FONT_BROWSER_SUB_BORDER
3061 - FONT_BROWSER_SUB_MARGIN - 3 * PopUp.font.width,
3062 subpanel_top + FONT_BROWSER_SUB_BORDER
3063 + FONT_BROWSER_SUB_MARGIN + PopUp.font.height + 2);
3064
3065 filepanel_left = dirpanel_right + 2 * PopUp.font.width;
3066 for (i = 0; i < FONT_BROWSER_PAGE_ENTRIES; ++i) {
3067 FontBrowserFiles[i] = sdl_ButtonBankNew(&PopUp.buttons);
3068 button = sdl_ButtonBankGet(&PopUp.buttons,
3069 FontBrowserFiles[i]);
3070 /* Grey it out until needed. */
3071 button->unsel_colour = back_colour;
3072 button->sel_colour = back_colour;
3073 button->cap_colour = back_colour;
3074 sdl_ButtonSize(button, (FONT_BROWSER_FILE_LENGTH + 1)
3075 * PopUp.font.width, PopUp.font.height + 2);
3076 sdl_ButtonVisible(button, true);
3077 sdl_ButtonCaption(button, "");
3078 sdl_ButtonMove(button, filepanel_left + FONT_BROWSER_SUB_BORDER
3079 + FONT_BROWSER_SUB_MARGIN, subpanel_top
3080 + FONT_BROWSER_SUB_BORDER + FONT_BROWSER_SUB_MARGIN
3081 + i * (PopUp.font.height + 2));
3082 button->tag = i;
3083 button->activate = SelectFileFontBrowser;
3084 }
3085 filepanel_right = width - FONT_BROWSER_BORDER - FONT_BROWSER_MARGIN;
3086 FontBrowserFilePageBefore = sdl_ButtonBankNew(&PopUp.buttons);
3087 button = sdl_ButtonBankGet(&PopUp.buttons, FontBrowserFilePageBefore);
3088 /* Grey it out until needed. */
3089 button->unsel_colour = back_colour;
3090 button->sel_colour = back_colour;
3091 button->cap_colour = back_colour;
3092 sdl_ButtonSize(button, 3 * PopUp.font.width, PopUp.font.height + 2);
3093 sdl_ButtonVisible(button, true);
3094 sdl_ButtonCaption(button, "-");
3095 sdl_ButtonMove(button, filepanel_right - FONT_BROWSER_SUB_BORDER
3096 - FONT_BROWSER_SUB_MARGIN - 3 * PopUp.font.width, subpanel_top
3097 + FONT_BROWSER_SUB_BORDER + FONT_BROWSER_SUB_MARGIN);
3098 button->tag = -1;
3099 button->activate = ChangeFilePageFontBrowser;
3100 FontBrowserFilePageAfter = sdl_ButtonBankNew(&PopUp.buttons);
3101 button = sdl_ButtonBankGet(&PopUp.buttons, FontBrowserFilePageAfter);
3102 /* Grey it out until needed. */
3103 button->unsel_colour = back_colour;
3104 button->sel_colour = back_colour;
3105 button->cap_colour = back_colour;
3106 sdl_ButtonSize(button, 3 * PopUp.font.width, PopUp.font.height + 2);
3107 sdl_ButtonVisible(button, true);
3108 sdl_ButtonCaption(button, "+");
3109 sdl_ButtonMove(button, filepanel_right - FONT_BROWSER_SUB_BORDER
3110 - FONT_BROWSER_SUB_MARGIN - 3 * PopUp.font.width,
3111 subpanel_top + FONT_BROWSER_SUB_BORDER
3112 + FONT_BROWSER_SUB_MARGIN + (FONT_BROWSER_PAGE_ENTRIES - 1)
3113 * (PopUp.font.height + 2));
3114 button->tag = 1;
3115 button->activate = ChangeFilePageFontBrowser;
3116 /* As above, this is a dummy button to absorb frequent stray clicks. */
3117 FontBrowserFilePageDummy = sdl_ButtonBankNew(&PopUp.buttons);
3118 button = sdl_ButtonBankGet(&PopUp.buttons, FontBrowserFilePageDummy);
3119 button->unsel_colour = back_colour;
3120 button->sel_colour = back_colour;
3121 button->cap_colour = back_colour;
3122 sdl_ButtonSize(button, 3 * PopUp.font.width, (FONT_BROWSER_PAGE_ENTRIES
3123 - 2) * (PopUp.font.height + 2));
3124 sdl_ButtonVisible(button, true);
3125 sdl_ButtonCaption(button, "");
3126 sdl_ButtonMove(button, filepanel_right - FONT_BROWSER_SUB_BORDER
3127 - FONT_BROWSER_SUB_MARGIN - 3 * PopUp.font.width,
3128 subpanel_top + FONT_BROWSER_SUB_BORDER
3129 + FONT_BROWSER_SUB_MARGIN + PopUp.font.height + 2);
3130
3131 /*
3132 * Set up the point size controls, all are greyed out until needed.
3133 * Center them, if possible, below the file panel.
3134 */
3135 if (24 * PopUp.font.width + ptsz_width
3136 > filepanel_right - filepanel_left) {
3137 ptsize_left = filepanel_right - 24 * PopUp.font.width
3138 - ptsz_width;
3139 } else {
3140 ptsize_left = filepanel_left + (filepanel_right
3141 - filepanel_left - 24 * PopUp.font.width
3142 - ptsz_width) / 2;
3143 }
3144 subpanel_bottom = subpanel_top + 2 * (FONT_BROWSER_SUB_BORDER
3145 + FONT_BROWSER_SUB_MARGIN) + FONT_BROWSER_PAGE_ENTRIES
3146 * (PopUp.font.height + 2);
3147 FontBrowserPtSizeBigDec = sdl_ButtonBankNew(&PopUp.buttons);
3148 button = sdl_ButtonBankGet(&PopUp.buttons, FontBrowserPtSizeBigDec);
3149 button->unsel_colour = back_colour;
3150 button->sel_colour = back_colour;
3151 button->cap_colour = back_colour;
3152 sdl_ButtonSize(button, 4 * PopUp.font.width, PopUp.font.height + 2);
3153 sdl_ButtonVisible(button, true);
3154 sdl_ButtonCaption(button, "--");
3155 sdl_ButtonMove(button, ptsize_left, subpanel_bottom
3156 + FONT_BROWSER_VER_SPACE);
3157 button->tag = -10;
3158 button->activate = ChangePtSzFontBrowser;
3159 FontBrowserPtSizeDec = sdl_ButtonBankNew(&PopUp.buttons);
3160 button = sdl_ButtonBankGet(&PopUp.buttons, FontBrowserPtSizeDec);
3161 button->unsel_colour = back_colour;
3162 button->sel_colour = back_colour;
3163 button->cap_colour = back_colour;
3164 sdl_ButtonSize(button, 4 * PopUp.font.width, PopUp.font.height + 2);
3165 sdl_ButtonVisible(button, true);
3166 sdl_ButtonCaption(button, " -");
3167 sdl_ButtonMove(button, ptsize_left + 6 * PopUp.font.width,
3168 subpanel_bottom + FONT_BROWSER_VER_SPACE);
3169 button->tag = -1;
3170 button->activate = ChangePtSzFontBrowser;
3171 FontBrowserPtSizeInc = sdl_ButtonBankNew(&PopUp.buttons);
3172 button = sdl_ButtonBankGet(&PopUp.buttons, FontBrowserPtSizeInc);
3173 button->unsel_colour = back_colour;
3174 button->sel_colour = back_colour;
3175 button->cap_colour = back_colour;
3176 sdl_ButtonSize(button, 4 * PopUp.font.width, PopUp.font.height + 2);
3177 sdl_ButtonVisible(button, true);
3178 sdl_ButtonCaption(button, " +");
3179 sdl_ButtonMove(button, ptsize_left + 14 * PopUp.font.width
3180 + ptsz_width, subpanel_bottom + FONT_BROWSER_VER_SPACE);
3181 button->tag = 1;
3182 button->activate = ChangePtSzFontBrowser;
3183 FontBrowserPtSizeBigInc = sdl_ButtonBankNew(&PopUp.buttons);
3184 button = sdl_ButtonBankGet(&PopUp.buttons, FontBrowserPtSizeBigInc);
3185 button->unsel_colour = back_colour;
3186 button->sel_colour = back_colour;
3187 button->cap_colour = back_colour;
3188 sdl_ButtonSize(button, 4 * PopUp.font.width, PopUp.font.height + 2);
3189 sdl_ButtonVisible(button, true);
3190 sdl_ButtonCaption(button, "++");
3191 sdl_ButtonMove(button, ptsize_left + 20 * PopUp.font.width
3192 + ptsz_width, subpanel_bottom + FONT_BROWSER_VER_SPACE);
3193 button->tag = 10;
3194 button->activate = ChangePtSzFontBrowser;
3195
3196 FontBrowserOk = sdl_ButtonBankNew(&PopUp.buttons);
3197 button = sdl_ButtonBankGet(&PopUp.buttons, FontBrowserOk);
3198 button->unsel_colour = AltUnselColour;
3199 button->sel_colour = AltSelColour;
3200 sdl_ButtonSize(button, 10 * PopUp.font.width, PopUp.font.height + 2);
3201 sdl_ButtonVisible(button, true);
3202 sdl_ButtonCaption(button, "OK");
3203 sdl_ButtonMove(button, width / 2 - 19 * PopUp.font.width, height
3204 - FONT_BROWSER_BORDER - FONT_BROWSER_MARGIN
3205 - PopUp.font.height - 2);
3206 button->activate = AcceptFontBrowser;
3207
3208 /*
3209 * Have a refresh button since file system change notifications are
3210 * not monitored. That way the user has a way to resync with what's
3211 * in the directory without closing and reopening the browser panel
3212 * or, for some changes, going to another directory and then back to
3213 * the current one.
3214 */
3215 FontBrowserRefresh = sdl_ButtonBankNew(&PopUp.buttons);
3216 button = sdl_ButtonBankGet(&PopUp.buttons, FontBrowserRefresh);
3217 button->unsel_colour = AltUnselColour;
3218 button->sel_colour = AltSelColour;
3219 sdl_ButtonSize(button, 10 * PopUp.font.width, PopUp.font.height + 2);
3220 sdl_ButtonVisible(button, true);
3221 sdl_ButtonCaption(button, "Refresh");
3222 sdl_ButtonMove(button, width / 2 - 5 * PopUp.font.width, height
3223 - FONT_BROWSER_BORDER - FONT_BROWSER_MARGIN
3224 - PopUp.font.height - 2);
3225 /*
3226 * Set the tag to zero so this button can be passed to
3227 * ChangeDirPageFontBrowser() and ChangeFilePageFontBrowser().
3228 */
3229 button->tag = 0;
3230 button->activate = RefreshFontBrowser;
3231
3232 FontBrowserCancel = sdl_ButtonBankNew(&PopUp.buttons);
3233 button = sdl_ButtonBankGet(&PopUp.buttons, FontBrowserCancel);
3234 button->unsel_colour = AltUnselColour;
3235 button->sel_colour = AltSelColour;
3236 sdl_ButtonSize(button, 10 * PopUp.font.width, PopUp.font.height + 2);
3237 sdl_ButtonVisible(button, true);
3238 sdl_ButtonCaption(button, "Cancel");
3239 sdl_ButtonMove(button, width / 2 + 9 * PopUp.font.width, height
3240 - FONT_BROWSER_BORDER - FONT_BROWSER_MARGIN
3241 - PopUp.font.height - 2);
3242 button->activate = CancelFontBrowser;
3243
3244 /*
3245 * Update the browser for the directories and files in the directory
3246 * being browsed.
3247 */
3248 button = sdl_ButtonBankGet(&PopUp.buttons, FontBrowserRefresh);
3249 RefreshFontBrowser(button);
3250
3251 popped = true;
3252 }
3253
3254 static void FontActivate(sdl_Button *sender)
3255 {
3256 const char *browse_label = "Other ...";
3257 term_window *window = &windows[SelectedTerm];
3258 int i, maxl = (int) strlen(browse_label);
3259 int extra, width, height;
3260 int h, b;
3261 sdl_Button *button;
3262
3263 for (i = 0; i < num_fonts; i++) {
3264 size_t sl = strlen(FontList[i]);
3265
3266 /*
3267 * Use 49 because that is one less than the maximum size of
3268 * a button's caption.
3269 */
3270 maxl = (sl >= 49) ? 49 : MAX(maxl, (int) sl);
3271 }
3272
3273 /*
3274 * Always add an additional menu item to open a file browser to
3275 * select a font file. If not currently using a preset font, also
3276 * add an entry which corresponds to that font.
3277 */
3278 if (window->req_font.preset) {
3279 extra = 1;
3280 } else {
3281 extra = 2;
3282 }
3283 width = maxl * StatusBar.font.width + 20;
3284 height = (num_fonts + extra) * (StatusBar.font.height + 1);
3285
3286 sdl_WindowInit(&PopUp, width, height, AppWin, StatusBar.font.req);
3287 PopUp.left = sender->pos.x;
3288 PopUp.top = sender->pos.y;
3289
3290 h = PopUp.font.height;
3291 for (i = 0; i < num_fonts; i++) {
3292 b = sdl_ButtonBankNew(&PopUp.buttons);
3293 button = sdl_ButtonBankGet(&PopUp.buttons, b);
3294 sdl_ButtonSize(button, width - 2 , h);
3295 sdl_ButtonMove(button, 1, i * (h + 1));
3296 if (window->req_font.preset
3297 && streq(window->req_font.name, FontList[i])) {
3298 button->cap_colour = AltCapColour;
3299 }
3300 sdl_ButtonCaption(button, FontList[i]);
3301 sdl_ButtonVisible(button, true);
3302 button->activate = (suffix_i(FontList[i], ".fon")) ?
3303 SelectPresetBitmappedFont : SelectPresetScalableFont;
3304 }
3305
3306 if (extra == 2) {
3307 /*
3308 * Add a button corresponding to the current, but not preset
3309 * font.
3310 */
3311 char caption[50];
3312
3313 b = sdl_ButtonBankNew(&PopUp.buttons);
3314 button = sdl_ButtonBankGet(&PopUp.buttons, b);
3315 sdl_ButtonSize(button, width - 2 , h);
3316 sdl_ButtonMove(button, 1, num_fonts * (h + 1));
3317 button->cap_colour = AltCapColour;
3318 get_font_short_name(caption, sizeof(caption),
3319 &window->req_font);
3320 sdl_ButtonCaption(button, caption);
3321 sdl_ButtonVisible(button, true);
3322 button->activate = AlterNonPresetFontSize;
3323 }
3324
3325 /* Add the button for launching the font file browser dialog. */
3326 b = sdl_ButtonBankNew(&PopUp.buttons);
3327 button = sdl_ButtonBankGet(&PopUp.buttons, b);
3328 sdl_ButtonSize(button, width - 2 , h);
3329 sdl_ButtonMove(button, 1, (num_fonts + extra - 1) * (h + 1));
3330 sdl_ButtonCaption(button, browse_label);
3331 sdl_ButtonVisible(button, true);
3332 button->activate = ActivateFontBrowser;
3333
3334 popped = true;
3335 }
3336
3337 static errr load_gfx(void);
3338 static bool do_update = false;
3339
3340 static void SelectGfx(sdl_Button *sender)
3341 {
3342 SelectedGfx = sender->tag;
3343 }
3344
3345 static void AcceptChanges(sdl_Button *sender)
3346 {
3347 sdl_Button *button;
3348 bool do_video_reset = false;
3349
3350 if (use_graphics != SelectedGfx) {
3351 do_update = true;
3352 use_graphics = SelectedGfx;
3353 }
3354
3355 if (use_graphics) {
3356 arg_graphics = true;
3357 load_gfx();
3358 } else {
3359 current_graphics_mode = NULL;
3360 arg_graphics = false;
3361 tile_width = 1;
3362 tile_height = 1;
3363 reset_visuals(true);
3364 }
3365
3366 /* Invalidate all the gfx surfaces */
3367 if (do_update) {
3368 int i;
3369 for (i = 0; i < ANGBAND_TERM_MAX; i++) {
3370 term_window *win = &windows[i];
3371 if (win->tiles) {
3372 SDL_FreeSurface(win->tiles);
3373 win->tiles = NULL;
3374 }
3375 if (win->onebyone) {
3376 SDL_FreeSurface(win->onebyone);
3377 win->onebyone = NULL;
3378 }
3379 }
3380 }
3381
3382 button = sdl_ButtonBankGet(&PopUp.buttons, MoreFullscreen);
3383
3384 if (button->tag != fullscreen) {
3385 fullscreen = !fullscreen;
3386
3387 do_video_reset = true;
3388 }
3389
3390
3391 SetStatusButtons();
3392
3393 RemovePopUp();
3394
3395 if (do_update)
3396 if (character_dungeon) do_cmd_redraw();
3397
3398 if (do_video_reset) {
3399 SDL_Event Event;
3400
3401 memset(&Event, 0, sizeof(SDL_Event));
3402
3403 Event.type = SDL_VIDEORESIZE;
3404 Event.resize.w = screen_w;
3405 Event.resize.h = screen_h;
3406
3407 SDL_PushEvent(&Event);
3408 }
3409
3410 do_update = false;
3411
3412 }
3413
3414 static void FlipTag(sdl_Button *sender)
3415 {
3416 if (sender->tag) {
3417 sender->tag = 0;
3418 sdl_ButtonCaption(sender, "Off");
3419 } else {
3420 sender->tag = 1;
3421 sdl_ButtonCaption(sender, "On");
3422 }
3423 }
3424
3425 static void SnapChange(sdl_Button *sender)
3426 {
3427 SnapRange += sender->tag;
3428 if (SnapRange < 0) SnapRange = 0;
3429 if (SnapRange > 20) SnapRange = 20;
3430 PopUp.need_update = true;
3431 }
3432
3433 static void WidthChange(sdl_Button *sender)
3434 {
3435 tile_width += sender->tag;
3436 if (tile_width < 1) tile_width = 1;
3437 if (tile_width > 12) tile_width = 12;
3438 do_update = true;
3439 }
3440
3441 static void HeightChange(sdl_Button *sender)
3442 {
3443 tile_height += sender->tag;
3444 if (tile_height < 1) tile_height = 1;
3445 if (tile_height > 8) tile_height = 8;
3446 do_update = true;
3447 }
3448
3449 static void MoreDraw(sdl_Window *win)
3450 {
3451 SDL_Rect rc;
3452 sdl_Button *button;
3453 int y = 20;
3454 graphics_mode *mode;
3455
3456 RECT(0, 0, win->width, win->height, &rc);
3457
3458 /* Draw a nice box */
3459 sdl_DrawBox(win->surface, &rc, AltUnselColour, 5);
3460
3461 /*
3462 * Only allow tile multiplier changes when a tile set is used and
3463 * the game is at a command prompt. The latter avoids a multiplier
3464 * change causing blank screens for in-game menus or display artifacts
3465 * sometime later when the in-game menu is dismissed.
3466 */
3467 if (SelectedGfx) {
3468 sdl_WindowText(win, AltUnselColour, 20, y,
3469 format("Tile width is %d.", tile_width));
3470 }
3471 button = sdl_ButtonBankGet(&win->buttons, MoreWidthMinus);
3472 if (SelectedGfx && character_generated && inkey_flag) {
3473 sdl_ButtonMove(button, 200, y);
3474 sdl_ButtonVisible(button, true);
3475 } else {
3476 sdl_ButtonVisible(button, false);
3477 }
3478 button = sdl_ButtonBankGet(&win->buttons, MoreWidthPlus);
3479 if (SelectedGfx && character_generated && inkey_flag) {
3480 sdl_ButtonMove(button, 230, y);
3481 sdl_ButtonVisible(button, true);
3482 } else {
3483 sdl_ButtonVisible(button, false);
3484 }
3485 if (SelectedGfx) {
3486 y += 20;
3487 }
3488
3489 if (SelectedGfx) {
3490 sdl_WindowText(win, AltUnselColour, 20, y,
3491 format("Tile height is %d.", tile_height));
3492 }
3493 button = sdl_ButtonBankGet(&win->buttons, MoreHeightMinus);
3494 if (SelectedGfx && character_generated && inkey_flag) {
3495 sdl_ButtonMove(button, 200, y);
3496 sdl_ButtonVisible(button, true);
3497 } else {
3498 sdl_ButtonVisible(button, false);
3499 }
3500 button = sdl_ButtonBankGet(&win->buttons, MoreHeightPlus);
3501 if (SelectedGfx && character_generated && inkey_flag) {
3502 sdl_ButtonMove(button, 230, y);
3503 sdl_ButtonVisible(button, true);
3504 } else {
3505 sdl_ButtonVisible(button, false);
3506 }
3507 if (SelectedGfx) {
3508 y += 20;
3509 }
3510
3511 sdl_WindowText(win, AltUnselColour, 20, y, "Selected Graphics:");
3512 if (current_graphics_mode) {
3513 sdl_WindowText(win, AltSelColour, 200, y,
3514 current_graphics_mode->menuname);
3515 } else {
3516 sdl_WindowText(win, AltSelColour, 200, y, "None");
3517 }
3518 y += 20;
3519
3520 /*
3521 * Only allow changes to the graphics mode when at a command prompt
3522 * in game. Could also allow while at the splash screen, but that
3523 * isn't possible to test for with character_generated and
3524 * character_dungeon. In other situations, the saved screens for
3525 * overlayed menus could have tile references that become outdated
3526 * when the graphics mode is changed.
3527 */
3528 if (character_generated && inkey_flag) {
3529 sdl_WindowText(win, AltUnselColour, 20, y,
3530 "Available Graphics:");
3531 }
3532 mode = graphics_modes;
3533 while (mode) {
3534 if (!mode->menuname[0]) {
3535 mode = mode->pNext;
3536 continue;
3537 }
3538 button = sdl_ButtonBankGet(&win->buttons, GfxButtons[mode->grafID]);
3539 if (character_generated && inkey_flag) {
3540 sdl_ButtonMove(button, 200, y);
3541 sdl_ButtonVisible(button, true);
3542 y += 20;
3543 } else {
3544 sdl_ButtonVisible(button, false);
3545 }
3546 mode = mode->pNext;
3547 }
3548
3549 button = sdl_ButtonBankGet(&win->buttons, MoreFullscreen);
3550 sdl_WindowText(win, AltUnselColour, 20, y, "Fullscreen is:");
3551
3552 sdl_ButtonMove(button, 200, y);
3553 y+= 20;
3554
3555 sdl_WindowText(win, AltUnselColour, 20, y,
3556 format("Snap range is %d.", SnapRange));
3557 button = sdl_ButtonBankGet(&win->buttons, MoreSnapMinus);
3558 sdl_ButtonMove(button, 200, y);
3559
3560 button = sdl_ButtonBankGet(&win->buttons, MoreSnapPlus);
3561 sdl_ButtonMove(button, 230, y);
3562 }
3563
3564 static void MoreActivate(sdl_Button *sender)
3565 {
3566 int width = 300;
3567 int height = 300;
3568 sdl_Button *button;
3569 graphics_mode *mode;
3570
3571 sdl_WindowInit(&PopUp, width, height, AppWin, StatusBar.font.req);
3572 PopUp.left = (AppWin->w / 2) - width / 2;
3573 PopUp.top = (AppWin->h / 2) - height / 2;
3574 PopUp.draw_extra = MoreDraw;
3575
3576 MoreWidthPlus = sdl_ButtonBankNew(&PopUp.buttons);
3577 button = sdl_ButtonBankGet(&PopUp.buttons, MoreWidthPlus);
3578
3579 button->unsel_colour = AltUnselColour;
3580 button->sel_colour = AltSelColour;
3581 sdl_ButtonSize(button, 20, PopUp.font.height + 2);
3582 sdl_ButtonCaption(button, "+");
3583 button->tag = 1;
3584 sdl_ButtonVisible(button, true);
3585 button->activate = WidthChange;
3586
3587 MoreWidthMinus = sdl_ButtonBankNew(&PopUp.buttons);
3588 button = sdl_ButtonBankGet(&PopUp.buttons, MoreWidthMinus);
3589
3590 button->unsel_colour = AltUnselColour;
3591 button->sel_colour = AltSelColour;
3592 sdl_ButtonSize(button, 20, PopUp.font.height + 2);
3593 sdl_ButtonCaption(button, "-");
3594 button->tag = -1;
3595 sdl_ButtonVisible(button, true);
3596 button->activate = WidthChange;
3597
3598 MoreHeightPlus = sdl_ButtonBankNew(&PopUp.buttons);
3599 button = sdl_ButtonBankGet(&PopUp.buttons, MoreHeightPlus);
3600
3601 button->unsel_colour = AltUnselColour;
3602 button->sel_colour = AltSelColour;
3603 sdl_ButtonSize(button, 20, PopUp.font.height + 2);
3604 sdl_ButtonCaption(button, "+");
3605 button->tag = 1;
3606 sdl_ButtonVisible(button, true);
3607 button->activate = HeightChange;
3608
3609 MoreHeightMinus = sdl_ButtonBankNew(&PopUp.buttons);
3610 button = sdl_ButtonBankGet(&PopUp.buttons, MoreHeightMinus);
3611
3612 button->unsel_colour = AltUnselColour;
3613 button->sel_colour = AltSelColour;
3614 sdl_ButtonSize(button, 20, PopUp.font.height + 2);
3615 sdl_ButtonCaption(button, "-");
3616 button->tag = -1;
3617 sdl_ButtonVisible(button, true);
3618 button->activate = HeightChange;
3619
3620 SelectedGfx = use_graphics;
3621
3622 mode = graphics_modes;
3623 while (mode) {
3624 if (!mode->menuname[0]) {
3625 mode = mode->pNext;
3626 continue;
3627 }
3628 GfxButtons[mode->grafID] = sdl_ButtonBankNew(&PopUp.buttons);
3629 button = sdl_ButtonBankGet(&PopUp.buttons, GfxButtons[mode->grafID]);
3630
3631 button->unsel_colour = AltUnselColour;
3632 button->sel_colour = AltSelColour;
3633 sdl_ButtonSize(button, 50 , PopUp.font.height + 2);
3634 sdl_ButtonVisible(button, true);
3635 sdl_ButtonCaption(button, mode->menuname);
3636 button->tag = mode->grafID;
3637 button->activate = SelectGfx;
3638
3639 mode = mode->pNext;
3640 }
3641
3642 MoreFullscreen = sdl_ButtonBankNew(&PopUp.buttons);
3643 button = sdl_ButtonBankGet(&PopUp.buttons, MoreFullscreen);
3644
3645 button->unsel_colour = AltUnselColour;
3646 button->sel_colour = AltSelColour;
3647 sdl_ButtonSize(button, 50 , PopUp.font.height + 2);
3648 sdl_ButtonVisible(button, true);
3649 sdl_ButtonCaption(button, fullscreen ? "On" : "Off");
3650 button->tag = fullscreen;
3651 button->activate = FlipTag;
3652
3653 MoreSnapPlus = sdl_ButtonBankNew(&PopUp.buttons);
3654 button = sdl_ButtonBankGet(&PopUp.buttons, MoreSnapPlus);
3655
3656 button->unsel_colour = AltUnselColour;
3657 button->sel_colour = AltSelColour;
3658 sdl_ButtonSize(button, 20, PopUp.font.height + 2);
3659 sdl_ButtonCaption(button, "+");
3660 button->tag = 1;
3661 sdl_ButtonVisible(button, true);
3662 button->activate = SnapChange;
3663
3664 MoreSnapMinus = sdl_ButtonBankNew(&PopUp.buttons);
3665 button = sdl_ButtonBankGet(&PopUp.buttons, MoreSnapMinus);
3666
3667 button->unsel_colour = AltUnselColour;
3668 button->sel_colour = AltSelColour;
3669 sdl_ButtonSize(button, 20, PopUp.font.height + 2);
3670 sdl_ButtonCaption(button, "-");
3671 button->tag = -1;
3672 sdl_ButtonVisible(button, true);
3673 button->activate = SnapChange;
3674
3675 MoreOK = sdl_ButtonBankNew(&PopUp.buttons);
3676 button = sdl_ButtonBankGet(&PopUp.buttons, MoreOK);
3677
3678 button->unsel_colour = AltUnselColour;
3679 button->sel_colour = AltSelColour;
3680 sdl_ButtonSize(button, 50 , PopUp.font.height + 2);
3681 sdl_ButtonVisible(button, true);
3682 sdl_ButtonCaption(button, "OK");
3683 sdl_ButtonMove(button, width / 2 - 25, height - 40);
3684 button->activate = AcceptChanges;
3685
3686 popped = true;
3687 }
3688
3689 static errr Term_xtra_sdl_clear(void);
3690
3691 /**
3692 * Make a window with size (x,y) pixels
3693 * Note: The actual size of the window may end up smaller.
3694 * This may be called when a window wants resizing,
3695 * is made visible, or the font has changed.
3696 * This function doesn't go in for heavy optimization, and doesn't need it - it
3697 * may initialize a few too many redraws or whatnot, but everything gets done!
3698 */
3699 static void ResizeWin(term_window* win, int w, int h)
3700 {
3701 /* Don't bother */
3702 if (!win->visible) return;
3703
3704 win->border = 2;
3705 win->title_height = StatusHeight;
3706
3707 /* No font - a new font is needed -> get dimensions */
3708 if (!win->font.data) {
3709 /* Get font dimensions */
3710 if (sdl_CheckFont(&win->req_font, &win->tile_wid,
3711 &win->tile_hgt) || win->tile_wid <= 0
3712 || win->tile_hgt <= 0) {
3713 quit_fmt("unable to find font '%s';\n"
3714 "note that there are new extended font files "
3715 "ending in 'x' in %s;\n"
3716 "please check %s and edit if necessary",
3717 win->req_font.name, ANGBAND_DIR_FONTS,
3718 sdl_settings_file);
3719 }
3720 }
3721
3722 /* Get the amount of columns & rows */
3723 win->cols = (w - (win->border * 2)) / win->tile_wid;
3724 win->rows = (h - win->border - win->title_height) / win->tile_hgt;
3725
3726 /* Calculate the width & height */
3727 win->width = (win->cols * win->tile_wid) + (win->border * 2);
3728 win->height = (win->rows * win->tile_hgt) + win->border + win->title_height;
3729
3730 /* Delete the old surface */
3731 if (win->surface) SDL_FreeSurface(win->surface);
3732
3733 /* Create a new surface */
3734 win->surface = SDL_CreateRGBSurface(SDL_SWSURFACE, win->width, win->height,
3735 AppWin->format->BitsPerPixel,
3736 AppWin->format->Rmask,
3737 AppWin->format->Gmask,
3738 AppWin->format->Bmask,
3739 AppWin->format->Amask);
3740
3741 /* Fill it */
3742 SDL_FillRect(win->surface, NULL, SDL_MapRGB(AppWin->format, 160, 160, 60));
3743
3744 /* Label it */
3745 sdl_FontDraw(&SystemFont, win->surface, back_colour, 1, 1,
3746 strlen(angband_term_name[win->Term_idx]),
3747 angband_term_name[win->Term_idx]);
3748
3749 /* Mark the whole window for redraw */
3750 RECT(0, 0, win->width, win->height, &win->uRect);
3751
3752 /* Create the font if we need to */
3753 if (!win->font.data)
3754 sdl_FontCreate(&win->font, &win->req_font, win->surface);
3755
3756 /* This window was never visible before, or needs resizing */
3757 if (!angband_term[win->Term_idx]) {
3758 term *old = Term;
3759
3760 /* Initialize the term data */
3761 term_data_link_sdl(win);
3762
3763 /* Make it visible to angband */
3764 angband_term[win->Term_idx] = &win->term_data;
3765
3766 /* Activate it */
3767 Term_activate((term*)&win->term_data);
3768
3769 /* Redraw */
3770 Term_redraw();
3771
3772 /* Restore */
3773 Term_activate(old);
3774 } else {
3775 term *old = Term;
3776
3777 /* Activate it */
3778 Term_activate((term*)&win->term_data);
3779
3780 /* Resize */
3781 Term_resize(win->cols, win->rows);
3782
3783 /* Redraw */
3784 Term_redraw();
3785
3786 /* Restore */
3787 Term_activate(old);
3788 }
3789
3790 /* Calculate the hotspot */
3791 if (win->Term_idx == SelectedTerm) {
3792 SizingSpot.w = 10;
3793 SizingSpot.h = 10;
3794 SizingSpot.x = win->left + win->width - 10;
3795 SizingSpot.y = win->top + win->height - 10;
3796 }
3797
3798 StatusBar.need_update = true;
3799
3800 /* HACK - Redraw all windows */
3801 if (character_dungeon) do_cmd_redraw();
3802 }
3803
3804
3805 static errr load_prefs(void)
3806 {
3807 char buf[1024];
3808 ang_file *fff;
3809 term_window *win;
3810 int i;
3811
3812 /* Initialize the windows with crappy defaults! */
3813 for (i = 0; i < ANGBAND_TERM_MAX; i++) {
3814 win = &windows[i];
3815
3816 /* Clear the data */
3817 memset(win, 0, sizeof(term_window));
3818
3819 /* Who? */
3820 win->Term_idx = i;
3821
3822 win->req_font.name = string_make(default_term_font.name);
3823 win->req_font.size = default_term_font.size;
3824 win->req_font.preset = default_term_font.preset;
3825 win->req_font.bitmapped = default_term_font.bitmapped;
3826
3827 if (i == 0) {
3828 win->top = StatusHeight;
3829 win->width = 600;
3830 win->height = 380;
3831 win->keys = 1024;
3832 win->visible = true;
3833 } else {
3834 win->top = 400 + (i * 10);
3835 win->left = (i - 1) * 10;
3836 win->width = 400;
3837 win->height = 200;
3838 win->keys = 32;
3839
3840 win->visible = false;
3841 }
3842 }
3843
3844 /* Build the path */
3845 path_build(buf, sizeof(buf), ANGBAND_DIR_USER, "sdlinit.txt");
3846 sdl_settings_file = string_make(buf);
3847
3848 /* Open the file */
3849 fff = file_open(buf, MODE_READ, -1);
3850
3851 /* Check it */
3852 if (!fff) return (1);
3853
3854 /* Process the file */
3855 while (file_getl(fff, buf, sizeof(buf))) {
3856 char *s;
3857 if (!buf[0]) continue;
3858
3859 if (buf[0] == '#') continue;
3860
3861 s = strchr(buf, '=');
3862 s++;
3863 while (!isalnum(*s)) s++;
3864
3865 if (strstr(buf, "Resolution")) {
3866 screen_w = atoi(s);
3867 s = strchr(buf, 'x');
3868 screen_h = atoi(s + 1);
3869 } else if (strstr(buf, "Fullscreen")) {
3870 fullscreen = atoi(s);
3871 } else if (strstr(buf, "Graphics")) {
3872 use_graphics = atoi(s);
3873 if (use_graphics) arg_graphics = true;
3874 } else if (strstr(buf, "TileWidth")) {
3875 tile_width = atoi(s);
3876 } else if (strstr(buf, "TileHeight")) {
3877 tile_height = atoi(s);
3878 } else if (strstr(buf, "Bigtile")) {
3879 tile_width += atoi(s);
3880 } else if (strstr(buf, "Dbltile")) {
3881 tile_width += tile_width * atoi(s);
3882 tile_height += tile_height * atoi(s);
3883 } else if (strstr(buf, "Trptile")) {
3884 tile_width += 2 * tile_width * atoi(s);
3885 tile_height += 2 * tile_height * atoi(s);
3886 } else if (strstr(buf, "Window")) {
3887 win = &windows[atoi(s)];
3888 } else if (strstr(buf, "Visible")) {
3889 win->visible = atoi(s);
3890 } else if (strstr(buf, "Left")) {
3891 win->left = atoi(s);
3892 } else if (strstr(buf, "Top")) {
3893 win->top = atoi(s);
3894 } else if (strstr(buf, "Width")) {
3895 win->width = atoi(s);
3896 } else if (strstr(buf, "Height")) {
3897 win->height = atoi(s);
3898 } else if (strstr(buf, "Keys")) {
3899 win->keys = atoi(s);
3900 } else if (strstr(buf, "Font")) {
3901 const char *garbled_msg = "garbled font entry in pref "
3902 "file; use the default fault instead\n";
3903 long fsz;
3904 char *se;
3905 int w, h;
3906
3907 string_free(win->req_font.name);
3908 if (prefix(s, "NOTPRESET,")) {
3909 win->req_font.preset = false;
3910 fsz = strtol(s + 10, &se, 10);
3911 if (*se == ',') {
3912 if (!fsz && (fsz < MIN_POINT_SIZE
3913 || fsz > MAX_POINT_SIZE)) {
3914 (void) fprintf(stderr,
3915 "invalid point size, "
3916 "%ld, in pref file; "
3917 "use the default size "
3918 "instead\n", fsz);
3919 fsz = DEFAULT_POINT_SIZE;
3920 }
3921 win->req_font.name =
3922 string_make(se + 1);
3923 } else {
3924 (void) fprintf(stderr, "%s",
3925 garbled_msg);
3926 win->req_font.preset =
3927 default_term_font.preset;
3928 win->req_font.name = string_make(
3929 default_term_font.name);
3930 fsz = (default_term_font.bitmapped) ?
3931 0 : default_term_font.size;
3932 }
3933 } else {
3934 win->req_font.preset = true;
3935 if (prefix(s, "NOTBITMAP,")) {
3936 fsz = strtol(s + 10, &se, 10);
3937 if (*se == ',') {
3938 if (fsz < MIN_POINT_SIZE
3939 || fsz > MAX_POINT_SIZE) {
3940 fsz = DEFAULT_POINT_SIZE;
3941 }
3942 win->req_font.name =
3943 string_make(se + 1);
3944 } else {
3945 (void) fprintf(stderr, "%s",
3946 garbled_msg);
3947 win->req_font.preset =
3948 default_term_font.preset;
3949 win->req_font.name = string_make(
3950 default_term_font.name);
3951 fsz = (default_term_font.bitmapped) ?
3952 0 : default_term_font.size;
3953 }
3954 } else {
3955 win->req_font.name = string_make(s);
3956 fsz = 0;
3957 }
3958 }
3959 win->req_font.size = fsz;
3960 win->req_font.bitmapped = (fsz == 0);
3961 if (sdl_CheckFont(&win->req_font, &w, &h)) {
3962 if (streq(win->req_font.name,
3963 default_term_font.name)) {
3964 quit_fmt("could not load the default "
3965 "font, %s", win->req_font.name);
3966 }
3967 (void) fprintf(stderr, "unusable font "
3968 "file, %s, from pref file; using the "
3969 "default font\n", win->req_font.name);
3970 string_free(win->req_font.name);
3971 win->req_font.name = string_make(
3972 default_term_font.name);
3973 win->req_font.size = default_term_font.size;
3974 win->req_font.preset = default_term_font.preset;
3975 win->req_font.bitmapped =
3976 default_term_font.bitmapped;
3977 }
3978 }
3979 }
3980
3981 if (screen_w < 640) screen_w = 640;
3982 if (screen_h < 480) screen_h = 480;
3983
3984 file_close(fff);
3985
3986 return (0);
3987 }
3988
3989 static errr save_prefs(void)
3990 {
3991 ang_file *fff;
3992 int i;
3993
3994 /* Open the file */
3995 fff = file_open(sdl_settings_file, MODE_WRITE, FTYPE_TEXT);
3996
3997 /* Check it */
3998 if (!fff) return (1);
3999
4000 file_putf(fff, "Resolution = %dx%d\n", screen_w, screen_h);
4001 file_putf(fff, "Fullscreen = %d\n", fullscreen);
4002 file_putf(fff, "Graphics = %d\n", use_graphics);
4003 file_putf(fff, "TileWidth = %d\n\n", tile_width);
4004 file_putf(fff, "TileHeight = %d\n\n", tile_height);
4005
4006 for (i = 0; i < ANGBAND_TERM_MAX; i++) {
4007 term_window *win = &windows[i];
4008
4009 file_putf(fff, "Window = %d\n", i);
4010 file_putf(fff, "Visible = %d\n", (int)win->visible);
4011 file_putf(fff, "Left = %d\n", win->left);
4012 file_putf(fff, "Top = %d\n", win->top);
4013 file_putf(fff, "Width = %d\n", win->width);
4014 file_putf(fff, "Height = %d\n", win->height);
4015 file_putf(fff, "Keys = %d\n", win->keys);
4016 if (win->req_font.bitmapped) {
4017 file_putf(fff, "Font = %s%s\n\n",
4018 (win->req_font.preset) ? "" : "NOTPRESET,0,",
4019 win->req_font.name);
4020 } else {
4021 assert(win->req_font.size >= MIN_POINT_SIZE &&
4022 win->req_font.size <= MAX_POINT_SIZE);
4023 file_putf(fff, "Font = %s,%d,%s\n\n",
4024 (win->req_font.preset) ?
4025 "NOTBITMAP" : "NOTPRESET",
4026 win->req_font.size, win->req_font.name);
4027 }
4028 }
4029
4030 file_close(fff);
4031
4032 /* Done */
4033 return (0);
4034 }
4035
4036 static void set_update_rect(term_window *win, SDL_Rect *rc);
4037
4038 static void DrawSizeWidget(void)
4039 {
4040 Uint32 colour = SDL_MapRGB(AppWin->format, 30, 160, 70);
4041 SDL_FillRect(AppWin, &SizingSpot, colour);
4042 SDL_UpdateRects(AppWin, 1, &SizingSpot);
4043 }
4044
4045 static int Movingx;
4046 static int Movingy;
4047
4048 /**
4049 * Is What within Range units of Origin
4050 */
4051
4052 #define closeto(Origin, What, Range) \
4053 ((ABS((Origin) - (What))) < (Range))
4054
4055 /**
4056 * This function keeps the 'mouse' info up to date,
4057 * and reacts to mouse buttons appropriately.
4058 */
4059 static void sdl_HandleMouseEvent(SDL_Event *event)
4060 {
4061 term *old = Term;
4062 term_window *win;
4063 switch (event->type) {
4064 /* Mouse moved */
4065 case SDL_MOUSEMOTION:
4066 {
4067 mouse.x = event->motion.x;
4068 mouse.y = event->motion.y;
4069 win = &windows[SelectedTerm];
4070
4071 /* We are moving or resizing a window */
4072 if (Moving) {
4073 int i;
4074
4075 /* Move the window */
4076 win->left = (mouse.x - Movingx);
4077 win->top = (mouse.y - Movingy);
4078
4079 /* Left bounds check */
4080 if (win->left < 0) {
4081 win->left = 0;
4082 Movingx = mouse.x;
4083 }
4084
4085 /* Right bounds check */
4086 if ((win->left + win->width) > AppWin->w) {
4087 win->left = AppWin->w - win->width;
4088 Movingx = mouse.x - win->left;
4089 }
4090
4091 /* Top bounds check */
4092 if (win->top < StatusHeight) {
4093 win->top = StatusHeight;
4094 Movingy = mouse.y - win->top;
4095 }
4096
4097 /* Bottom bounds check */
4098 if ((win->top + win->height) > AppWin->h) {
4099 win->top = AppWin->h - win->height;
4100 Movingy = mouse.y - win->top;
4101 }
4102
4103 for (i = 0; i < ANGBAND_TERM_MAX; i++) {
4104 term_window *snapper = &windows[i];
4105
4106 /* Can't snap to self... */
4107 if (i == SelectedTerm) continue;
4108
4109 /* Can't snap to the invisible */
4110 if (!snapper->visible) continue;
4111
4112 /* Check the windows are across from each other */
4113 if ((snapper->top < win->top + win->height) &&
4114 (win->top < snapper->top + snapper->height)) {
4115 /* Lets try to the left... */
4116 if (closeto(win->left, snapper->left + snapper->width,
4117 SnapRange)) {
4118 win->left = snapper->left + snapper->width;
4119 Movingx = mouse.x - win->left;
4120 }
4121 /* Maybe to the right */
4122 if (closeto(win->left + win->width, snapper->left,
4123 SnapRange)) {
4124 win->left = snapper->left - win->width;
4125 Movingx = mouse.x - win->left;
4126 }
4127 }
4128
4129 /* Check the windows are above/below each other */
4130 if ((snapper->left < win->left + win->width) &&
4131 (win->left < snapper->left + snapper->width)) {
4132 /* Lets try to the top... */
4133 if (closeto(win->top, snapper->top + snapper->height,
4134 SnapRange)) {
4135 win->top = snapper->top + snapper->height;
4136 Movingy = mouse.y - win->top;
4137 }
4138 /* Maybe to the bottom */
4139 if (closeto(win->top + win->height, snapper->top,
4140 SnapRange)) {
4141 win->top = snapper->top - win->height;
4142 Movingy = mouse.y - win->top;
4143 }
4144 }
4145
4146 }
4147
4148 /* Show on the screen */
4149 sdl_BlitAll();
4150 } else if (Sizing) {
4151 /* Adjust the sizing rectangle */
4152 SizingRect.w = win->width - win->left + (mouse.x - Movingx);
4153 SizingRect.h = win->height - win->top + (mouse.y - Movingy);
4154
4155 /* XXX - The main window can't be too small */
4156 if (SelectedTerm == 0) {
4157 int minwidth = (win->tile_wid * 80) + 2 * win->border;
4158 int minheight = (win->tile_hgt * 24) +
4159 win->border + win->title_height;
4160 if (SizingRect.w < minwidth)
4161 SizingRect.w = minwidth;
4162 if (SizingRect.h < minheight)
4163 SizingRect.h = minheight;
4164 }
4165
4166 /* Show on the screen */
4167 sdl_BlitAll();
4168 } else if (!popped) {
4169 /* Have a look for the corner stuff */
4170 if (point_in(&SizingSpot, mouse.x, mouse.y)) {
4171 if (!Sizingshow) {
4172 /* Indicate the hotspot */
4173 Sizingshow = true;
4174 DrawSizeWidget();
4175 }
4176 } else if (Sizingshow) {
4177 SDL_Rect rc;
4178 Sizingshow = false;
4179 RECT(win->width - 10, win->height - 10, 10, 10, &rc);
4180 set_update_rect(win, &rc);
4181 sdl_BlitWin(win);
4182 }
4183 }
4184 break;
4185 }
4186
4187 /* A button has been pressed */
4188 case SDL_MOUSEBUTTONDOWN:
4189 {
4190
4191 sdl_Window *window;
4192 bool res;
4193 SDLMod mods;
4194 int button;
4195 int idx = sdl_LocateWin(mouse.x, mouse.y);
4196
4197 if (event->button.button == SDL_BUTTON_LEFT) {
4198 bool just_gained_focus = false;
4199 mouse.left = 1;
4200 mouse.leftx = event->button.x;
4201 mouse.lefty = event->button.y;
4202 button = 1;
4203
4204 /* Pop up window gets priority */
4205 if (popped) window = &PopUp; else window = &StatusBar;
4206
4207 /* React to a button press */
4208 res = sdl_ButtonBankMouseDown(&window->buttons,
4209 mouse.x - window->left,
4210 mouse.y - window->top);
4211
4212 /* If pop-up window active and no reaction, cancel the popup */
4213 if (popped && !res) {
4214 RemovePopUp();
4215 break;
4216 }
4217
4218 /* Has this mouse press been handled */
4219 if (res) break;
4220
4221 /* Is the mouse press in a term_window? */
4222 if (idx < 0) break;
4223
4224 /* The 'focused' window has changed */
4225 if (idx != SelectedTerm) {
4226 TermFocus(idx);
4227 just_gained_focus = true;
4228 }
4229
4230 /* A button press has happened on the focused term window */
4231 win = &windows[idx];
4232
4233 /* Check for mouse press in the title bar... */
4234 if (mouse.y < win->top + win->title_height) {
4235 /* Let's get moving */
4236 Moving = true;
4237
4238 /* BringToTop(idx); */
4239
4240 /* Remember where we started */
4241 Movingx = mouse.x - win->left;
4242 Movingy = mouse.y - win->top;
4243 } else if (point_in(&SizingSpot, mouse.x, mouse.y)) {
4244 /* ...or the little hotspot in the bottom right corner... */
4245 /* Let's get sizing */
4246 Sizing = true;
4247
4248 /* Create the sizing rectangle */
4249 RECT(win->left, win->top, win->width, win->height,
4250 &SizingRect);
4251
4252 /* Remember where we started */
4253 Movingx = mouse.x - win->left;
4254 Movingy = mouse.y - win->top;
4255
4256 } else if (!just_gained_focus) {
4257 /* ...or signal a mouse press to angband (only if the
4258 * window is already focused) */
4259 if (win->visible) {
4260 /* Calculate the 'cell' coords */
4261 int x = (mouse.x - win->left - win->border)
4262 / win->tile_wid;
4263 int y = (mouse.y - win->top - win->title_height)
4264 / win->tile_hgt;
4265 mods = SDL_GetModState();
4266 if (mods & KMOD_CTRL) {
4267 button |= 16;
4268 }
4269 if (mods & KMOD_SHIFT) {
4270 button |= 32;
4271 }
4272 if (mods & KMOD_ALT) {
4273 button |= 64;
4274 }
4275
4276 /* Send the mousepress to the appropriate term */
4277 Term_activate(angband_term[idx]);
4278 Term_mousepress(x, y, button);
4279 Term_activate(old);
4280 }
4281 }
4282 } else if (event->button.button == SDL_BUTTON_RIGHT) {
4283 mouse.right = 1;
4284 mouse.rightx = event->button.x;
4285 mouse.righty = event->button.y;
4286 button = 2;
4287
4288 /* Right-click always cancels the popup */
4289 if (popped) {
4290 popped = false;
4291 } else if (idx != -1) {
4292 /* Process right clicks inside a term_window */
4293 int x, y;
4294 win = &windows[idx];
4295
4296 /* Calculate the 'cell' coords */
4297 x = (mouse.x - win->left - win->border) /
4298 win->tile_wid;
4299 y = (mouse.y - win->top - win->title_height) /
4300 win->tile_hgt;
4301
4302 /* Bounds check */
4303 if ((x >= 0) && (y >= 0) && (x < win->cols) &&
4304 (y < win->rows)) {
4305 mods = SDL_GetModState();
4306 if (mods & KMOD_CTRL) {
4307 button |= 16;
4308 }
4309 if (mods & KMOD_SHIFT) {
4310 button |= 32;
4311 }
4312 if (mods & KMOD_ALT) {
4313 button |= 64;
4314 }
4315 /* Send the mousepress to the appropriate term */
4316 Term_activate(angband_term[idx]);
4317 Term_mousepress(x, y, button);
4318 Term_activate(old);
4319 }
4320 }
4321 }
4322
4323 break;
4324 }
4325 case SDL_MOUSEBUTTONUP:
4326 {
4327 /* Handle release of left button */
4328 if (event->button.button == SDL_BUTTON_LEFT) {
4329 sdl_Window *window;
4330 bool res;
4331 mouse.left = 0;
4332
4333 /* Pop up window gets priority */
4334 if (popped) window = &PopUp; else window = &StatusBar;
4335
4336 /* React to a button release */
4337 res = sdl_ButtonBankMouseUp(&window->buttons,
4338 mouse.x - window->left,
4339 mouse.y - window->top);
4340
4341 /* Cancel popup */
4342 if (popped && !res)
4343 RemovePopUp();
4344
4345 /* Finish moving */
4346 if (Moving) {
4347 Moving = false;
4348
4349 /* Update */
4350 sdl_BlitAll();
4351 }
4352
4353 /* Finish sizing */
4354 if (Sizing) {
4355 /* Sort out the window */
4356 ResizeWin(&windows[SelectedTerm], SizingRect.w, SizingRect.h);
4357 Sizing = false;
4358
4359 /* Update */
4360 sdl_BlitAll();
4361 }
4362 } else if (event->button.button == SDL_BUTTON_RIGHT) {
4363 mouse.right = 0;
4364 }
4365 break;
4366 }
4367
4368 }
4369 }
4370
4371 /**
4372 * Handle keypresses.
4373 *
4374 * We treat left and right modifier keys as equivalent.
4375 * We ignore any key without a valid SDLK index.
4376 */
4377 static void sdl_keypress(SDL_keysym keysym)
4378 {
4379 uint16_t key_code = keysym.unicode;
4380 SDLKey key_sym = keysym.sym;
4381
4382 int ch = 0;
4383
4384 /* Store the value of various modifier keys */
4385 bool mc = (keysym.mod & KMOD_CTRL) > 0;
4386 bool ms = (keysym.mod & KMOD_SHIFT) > 0;
4387 bool ma = (keysym.mod & KMOD_ALT) > 0;
4388 bool mm = (keysym.mod & KMOD_META) > 0;
4389 bool mg = (keysym.mod & KMOD_MODE) > 0;
4390 bool kp = false;
4391
4392 uint8_t mods = (ma ? KC_MOD_ALT : 0) | (mm ? KC_MOD_META : 0);
4393
4394 /* Ignore if main term is not initialized */
4395 if (!Term) return;
4396
4397 /* Handle all other valid SDL keys */
4398 switch (key_sym) {
4399 /* keypad */
4400 case SDLK_KP0: ch = '0'; kp = true; break;
4401 case SDLK_KP1: ch = '1'; kp = true; break;
4402 case SDLK_KP2: ch = '2'; kp = true; break;
4403 case SDLK_KP3: ch = '3'; kp = true; break;
4404 case SDLK_KP4: ch = '4'; kp = true; break;
4405 case SDLK_KP5: ch = '5'; kp = true; break;
4406 case SDLK_KP6: ch = '6'; kp = true; break;
4407 case SDLK_KP7: ch = '7'; kp = true; break;
4408 case SDLK_KP8: ch = '8'; kp = true; break;
4409 case SDLK_KP9: ch = '9'; kp = true; break;
4410 case SDLK_KP_PERIOD: ch = '.'; kp = true; break;
4411 case SDLK_KP_DIVIDE: ch = '/'; kp = true; break;
4412 case SDLK_KP_MULTIPLY: ch = '*'; kp = true; break;
4413 case SDLK_KP_MINUS: ch = '-'; kp = true; break;
4414 case SDLK_KP_PLUS: ch = '+'; kp = true; break;
4415 case SDLK_KP_ENTER: ch = KC_ENTER; kp = true; break;
4416 case SDLK_KP_EQUALS: ch = '='; kp = true; break;
4417
4418 /* have have these to get consistent ctrl-shift behaviour */
4419 case SDLK_0: if ((!ms || mc || ma) && !mg) ch = '0'; break;
4420 case SDLK_1: if ((!ms || mc || ma) && !mg) ch = '1'; break;
4421 case SDLK_2: if ((!ms || mc || ma) && !mg) ch = '2'; break;
4422 case SDLK_3: if ((!ms || mc || ma) && !mg) ch = '3'; break;
4423 case SDLK_4: if ((!ms || mc || ma) && !mg) ch = '4'; break;
4424 case SDLK_5: if ((!ms || mc || ma) && !mg) ch = '5'; break;
4425 case SDLK_6: if ((!ms || mc || ma) && !mg) ch = '6'; break;
4426 case SDLK_7: if ((!ms || mc || ma) && !mg) ch = '7'; break;
4427 case SDLK_8: if ((!ms || mc || ma) && !mg) ch = '8'; break;
4428 case SDLK_9: if ((!ms || mc || ma) && !mg) ch = '9'; break;
4429
4430 case SDLK_UP: ch = ARROW_UP; break;
4431 case SDLK_DOWN: ch = ARROW_DOWN; break;
4432 case SDLK_RIGHT: ch = ARROW_RIGHT; break;
4433 case SDLK_LEFT: ch = ARROW_LEFT; break;
4434
4435 case SDLK_INSERT: ch = KC_INSERT; break;
4436 case SDLK_HOME: ch = KC_HOME; break;
4437 case SDLK_PAGEUP: ch = KC_PGUP; break;
4438 case SDLK_DELETE: ch = KC_DELETE; break;
4439 case SDLK_END: ch = KC_END; break;
4440 case SDLK_PAGEDOWN: ch = KC_PGDOWN; break;
4441 case SDLK_ESCAPE: ch = ESCAPE; break;
4442 case SDLK_BACKSPACE: ch = KC_BACKSPACE; break;
4443 case SDLK_RETURN: ch = KC_ENTER; break;
4444 case SDLK_TAB: ch = KC_TAB; break;
4445
4446 case SDLK_F1: ch = KC_F1; break;
4447 case SDLK_F2: ch = KC_F2; break;
4448 case SDLK_F3: ch = KC_F3; break;
4449 case SDLK_F4: ch = KC_F4; break;
4450 case SDLK_F5: ch = KC_F5; break;
4451 case SDLK_F6: ch = KC_F6; break;
4452 case SDLK_F7: ch = KC_F7; break;
4453 case SDLK_F8: ch = KC_F8; break;
4454 case SDLK_F9: ch = KC_F9; break;
4455 case SDLK_F10: ch = KC_F10; break;
4456 case SDLK_F11: ch = KC_F11; break;
4457 case SDLK_F12: ch = KC_F12; break;
4458 case SDLK_F13: ch = KC_F13; break;
4459 case SDLK_F14: ch = KC_F14; break;
4460 case SDLK_F15: ch = KC_F15; break;
4461
4462 default: break;
4463 }
4464
4465 if (ch) {
4466 if (kp) mods |= KC_MOD_KEYPAD;
4467 if (mc) mods |= KC_MOD_CONTROL;
4468 if (ms) mods |= KC_MOD_SHIFT;
4469 Term_keypress(ch, mods);
4470 } else if (key_code) {
4471 /* If the keycode is 7-bit ASCII (except numberpad) send
4472 * directly to the game. */
4473 if (mc && (key_sym == SDLK_TAB || key_sym == SDLK_RETURN ||
4474 key_sym == SDLK_BACKSPACE ||
4475 MODS_INCLUDE_CONTROL(key_code)))
4476 mods |= KC_MOD_CONTROL;
4477 if (ms && MODS_INCLUDE_SHIFT(key_code)) mods |= KC_MOD_SHIFT;
4478
4479 Term_keypress(key_code, mods);
4480 }
4481 }
4482
4483
4484 static void init_windows(void);
4485 static void init_morewindows(void);
4486 /**
4487 * Handle a single message sent to the application.
4488 *
4489 * Functions that are either called from a separate thread or which need to
4490 * create a separate thread (such as sounds) need to pass messages to this
4491 * function in order to execute most operations. See the useage of
4492 * "SDL_USEREVENT".
4493 */
4494 static errr sdl_HandleEvent(SDL_Event *event)
4495 {
4496
4497 /* Handle message */
4498 switch (event->type)
4499 {
4500 /* Keypresses */
4501 case SDL_KEYDOWN:
4502 {
4503 /* Handle keypress */
4504 sdl_keypress(event->key.keysym);
4505
4506 break;
4507 }
4508 case SDL_MOUSEBUTTONDOWN:
4509 case SDL_MOUSEBUTTONUP:
4510 {
4511 /* Handle mouse stuff */
4512 sdl_HandleMouseEvent(event);
4513 break;
4514 }
4515
4516 case SDL_MOUSEMOTION:
4517 {
4518 int i;
4519 SDL_Event events[10];
4520
4521 /*
4522 * If there are a bundle of mouse movements pending,
4523 * we'll just take every tenth one - this makes a
4524 * simple approach to dragging practical, for instance.
4525 */
4526 i = SDL_PeepEvents(events, 10, SDL_GETEVENT,
4527 SDL_EVENTMASK(SDL_MOUSEMOTION));
4528 if (i > 0)
4529 *event = events[i - 1];
4530
4531 /* Handle mouse stuff */
4532 sdl_HandleMouseEvent(event);
4533 break;
4534 }
4535
4536 /* Shut down the game */
4537 /* XXX - check for stuck inside menu etc... */
4538 case SDL_QUIT:
4539 {
4540 bool really = true;
4541
4542 /* We are playing a game with an active character */
4543 if (character_generated && inkey_flag) {
4544 /* Hack -- Forget messages */
4545 msg_flag = false;
4546
4547 /* Save the game */
4548 if (!save_game_checked()
4549 && !get_check("Saving failed. Really quit? ")) {
4550 really = false;
4551 }
4552 }
4553
4554 if (really) {
4555 save_prefs();
4556 quit(NULL);
4557 }
4558 break;
4559 }
4560
4561 /* Resize the application */
4562 case SDL_VIDEORESIZE:
4563 {
4564 /* Free the surface */
4565 SDL_FreeSurface(AppWin);
4566
4567 if (!fullscreen) {
4568 /* Make sure */
4569 vflags &= ~(SDL_FULLSCREEN);
4570 vflags |= SDL_RESIZABLE;
4571
4572 screen_w = event->resize.w;
4573 screen_h = event->resize.h;
4574
4575 if (screen_w < 640) screen_w = 640;
4576 if (screen_h < 480) screen_h = 480;
4577
4578 /* Resize the application surface */
4579 AppWin = SDL_SetVideoMode(screen_w, screen_h, 0, vflags);
4580 } else {
4581 /* Make sure */
4582 vflags |= SDL_FULLSCREEN;
4583 vflags &= ~(SDL_RESIZABLE);
4584
4585 AppWin = SDL_SetVideoMode(full_w, full_h, 0, vflags);
4586 }
4587 init_morewindows();
4588 init_windows();
4589
4590 break;
4591 }
4592
4593 case WINDOW_DRAW:
4594 {
4595 /* Redraw window that have asked */
4596 sdl_Window *window = (sdl_Window*)event->user.data1;
4597 sdl_WindowBlit(window);
4598 break;
4599 }
4600 default:
4601 {
4602 /* Do nothing */
4603 break;
4604 }
4605 }
4606 sdl_WindowUpdate(&StatusBar);
4607 sdl_WindowUpdate(&PopUp);
4608 return (0);
4609 }
4610
4611 /**
4612 * Update the redraw rect
4613 * A simple but effective way to keep track of what
4614 * parts of a window need to updated.
4615 * Any new areas that are updated before a blit are simply combined
4616 * into a new larger rectangle to encompass all changes.
4617 */
4618 static void set_update_rect(term_window *win, SDL_Rect *rc)
4619 {
4620 /* No outstanding update areas yet? */
4621 if (win->uRect.x == -1) {
4622 /* Simple copy */
4623 win->uRect = *rc;
4624 } else {
4625 /* Combine the old update area with the new */
4626 int x = MIN(win->uRect.x, rc->x);
4627 int y = MIN(win->uRect.y, rc->y);
4628 int x2 = MAX(win->uRect.x + win->uRect.w, rc->x + rc->w);
4629 int y2 = MAX(win->uRect.y + win->uRect.h, rc->y + rc->h);
4630 RECT(x, y, x2 - x, y2 - y, &win->uRect);
4631 }
4632 }
4633
4634 /**
4635 * Clear a terminal window
4636 */
4637 static errr Term_xtra_sdl_clear(void)
4638 {
4639 term_window *win = (term_window*)(Term->data);
4640 SDL_Rect rc;
4641
4642 /* Oops */
4643 if (!win->surface) return (1);
4644
4645 /* Create the fill area */
4646 RECT(win->border, win->title_height, win->width - (2 * win->border),
4647 win->height - win->border - win->title_height, &rc);
4648
4649
4650 /* Fill the rectangle */
4651 SDL_FillRect(win->surface, &rc, back_pixel_colour);
4652
4653 /* Rectangle to update */
4654 set_update_rect(win, &rc);
4655
4656 /* Success */
4657 return (0);
4658 }
4659
4660 /**
4661 * Process at least one event
4662 */
4663 static errr Term_xtra_sdl_event(int v)
4664 {
4665 SDL_Event event;
4666 errr error = 0;
4667
4668 /* Wait or check for an event */
4669 if (v) {
4670 /* Wait in 0.02s increments while updating animations every 0.2s */
4671 int i = 0;
4672 while (!SDL_PollEvent(&event)) {
4673 if (i == 0) idle_update();
4674 usleep(20000);
4675 i = (i + 1) % 10;
4676 }
4677
4678 /* Handle it */
4679 error = sdl_HandleEvent(&event);
4680 } else {
4681 /* Get a single pending event */
4682 if (SDL_PollEvent(&event)) {
4683 /* Handle it */
4684 error = sdl_HandleEvent(&event);
4685 }
4686 }
4687
4688 /* Note success or failure */
4689 return (error);
4690 }
4691
4692 /**
4693 * Process all pending events
4694 */
4695 static errr Term_xtra_sdl_flush(void)
4696 {
4697 SDL_Event event;
4698
4699 /* Get all pending events */
4700 while (SDL_PollEvent(&event)) {
4701 /* Handle them (ignore errors) */
4702 (void)sdl_HandleEvent(&event);
4703 }
4704
4705 /* Done */
4706 return (0);
4707 }
4708
4709 /**
4710 * Delay for "x" milliseconds
4711 */
4712 static errr Term_xtra_sdl_delay(int v)
4713 {
4714 /* Sleep */
4715 if (v > 0) {
4716 Term_xtra_sdl_event(0);
4717 SDL_Delay(v);
4718 }
4719
4720 /* Success */
4721 return (0);
4722 }
4723
4724 static errr Term_bigcurs_sdl(int col, int row)
4725 {
4726 term_window *win = (term_window*)(Term->data);
4727
4728 SDL_Color colour = text_colours[COLOUR_YELLOW];
4729
4730 SDL_Rect rc;
4731
4732 /* Make a rectangle */
4733 RECT(col * win->tile_wid, row * win->tile_hgt, win->tile_wid * tile_width,
4734 win->tile_hgt * tile_height, &rc);
4735
4736 /* Translate it */
4737 rc.x += win->border;
4738 rc.y += win->title_height;
4739
4740 /* Draw it */
4741 sdl_DrawBox(win->surface, &rc, colour, 1);
4742
4743 /* Update area */
4744 set_update_rect(win, &rc);
4745
4746 /* Success */
4747 return (0);
4748 }
4749
4750 static errr Term_curs_sdl(int col, int row)
4751 {
4752 term_window *win = (term_window*)(Term->data);
4753
4754 SDL_Color colour = text_colours[COLOUR_YELLOW];
4755
4756 SDL_Rect rc;
4757
4758 /* Make a rectangle */
4759 RECT(col * win->tile_wid, row * win->tile_hgt, win->tile_wid,
4760 win->tile_hgt, &rc);
4761
4762 /* Translate it */
4763 rc.x += win->border;
4764 rc.y += win->title_height;
4765
4766 /* Draw it */
4767 sdl_DrawBox(win->surface, &rc, colour, 1);
4768
4769 /* Update area */
4770 set_update_rect(win, &rc);
4771
4772 /* Success */
4773 return (0);
4774 }
4775
4776 static errr Term_xtra_sdl(int n, int v)
4777 {
4778 switch (n)
4779 {
4780 /* Process an event */
4781 case TERM_XTRA_EVENT:
4782 {
4783 return (Term_xtra_sdl_event(v));
4784 }
4785 /* Flush all events */
4786 case TERM_XTRA_FLUSH:
4787 {
4788 return (Term_xtra_sdl_flush());
4789 }
4790 /* Clear the screen */
4791 case TERM_XTRA_CLEAR:
4792 {
4793 return (Term_xtra_sdl_clear());
4794 }
4795 /* Show or hide the cursor */
4796 case TERM_XTRA_SHAPE:
4797 {
4798 int x, y;
4799
4800 /* Obtain the cursor */
4801 (void)Term_locate(&x, &y);
4802
4803 /* Show or hide the cursor */
4804 Term_curs_sdl(x, y);
4805 return (0);
4806 }
4807 case TERM_XTRA_FRESH:
4808 {
4809 /* Get the current window data */
4810 term_window *win = (term_window*)(Term->data);
4811
4812 /* Blat it! */
4813 sdl_BlitWin(win);
4814
4815 /* Done */
4816 return (0);
4817 }
4818 case TERM_XTRA_DELAY:
4819 {
4820 return (Term_xtra_sdl_delay(v));
4821 }
4822 case TERM_XTRA_REACT:
4823 {
4824 int i;
4825 /* Re-initialize the colours */
4826 back_colour.r = angband_color_table[COLOUR_DARK][1];
4827 back_colour.g = angband_color_table[COLOUR_DARK][2];
4828 back_colour.b = angband_color_table[COLOUR_DARK][3];
4829 back_pixel_colour = SDL_MapRGB(AppWin->format,
4830 back_colour.r, back_colour.g, back_colour.b);
4831 for (i = 0; i < MAX_COLORS; i++)
4832 {
4833 text_colours[i].r = angband_color_table[i][1];
4834 text_colours[i].g = angband_color_table[i][2];
4835 text_colours[i].b = angband_color_table[i][3];
4836 }
4837 }
4838 }
4839
4840 return (1);
4841 }
4842
4843 /**
4844 * Erase a nc x nr block of characters whose upper left corner is at (col, row).
4845 */
4846 static errr Term_wipe_sdl_helper(int col, int row, int nc, int nr)
4847 {
4848 term_window *win = (term_window*)(Term->data);
4849
4850 SDL_Rect rc;
4851
4852 /* Build the area to black out */
4853 rc.x = col * win->tile_wid;
4854 rc.y = row * win->tile_hgt;
4855 rc.w = win->tile_wid * nc;
4856 rc.h = win->tile_hgt * nr;
4857
4858 /* Translate it */
4859 rc.x += win->border;
4860 rc.y += win->title_height;
4861
4862 /* Wipe it */
4863 SDL_FillRect(win->surface, &rc, back_pixel_colour);
4864
4865 /* Update */
4866 set_update_rect(win, &rc);
4867
4868 return (0);
4869 }
4870
4871 static errr Term_wipe_sdl(int col, int row, int n)
4872 {
4873 return Term_wipe_sdl_helper(col, row, n, 1);
4874 }
4875
4876 /**
4877 * Draw some text to a window
4878 */
4879 static errr Term_text_sdl(int col, int row, int n, int a, const wchar_t *s)
4880 {
4881 term_window *win = (term_window*)(Term->data);
4882 SDL_Color colour = text_colours[a % MAX_COLORS];
4883 SDL_Color bg = text_colours[COLOUR_DARK];
4884 int x = col * win->tile_wid;
4885 int y = row * win->tile_hgt;
4886 wchar_t src[255];
4887 char mbstr[MB_LEN_MAX * 255];
4888 size_t len;
4889
4890 /* Translate */
4891 x += win->border;
4892 y += win->title_height;
4893
4894 /* Not much point really... */
4895 if (!win->visible) return (0);
4896
4897 /* Clear the way */
4898 Term_wipe_sdl_helper(col, row, n, 1);
4899
4900 /* Take a copy of the incoming string, but truncate it at n chars */
4901 wcsncpy(src, s, n);
4902 src[n] = L'\0';
4903 /* Convert to UTF-8 for display */
4904 len = wcstombs(mbstr, src, n * MB_LEN_MAX);
4905 mbstr[len] = '\0';
4906
4907 /* Handle background */
4908 switch (a / MULT_BG)
4909 {
4910 case BG_BLACK:
4911 /* Default Background */
4912 break;
4913 case BG_SAME:
4914 /* Background same as foreground*/
4915 bg = colour;
4916 break;
4917 case BG_DARK:
4918 /* Highlight Background */
4919 bg = text_colours[COLOUR_SHADE];
4920 break;
4921 }
4922
4923 /* Draw it */
4924 return (sdl_mapFontDraw(&win->font, win->surface, colour, bg, x, y, n,
4925 mbstr));
4926 }
4927
4928 /**
4929 * Do a 'stretched blit'
4930 * SDL has no support for stretching... What a bastard!
4931 *
4932 */
4933 static void sdl_StretchBlit(SDL_Surface *src, SDL_Rect *srcRect, SDL_Surface *dest, SDL_Rect *destRect)
4934 {
4935 int x, y;
4936 int sx, sy, dx, dy;
4937 Uint8 *ps, *pd;
4938
4939 for (y = 0; y < destRect->h; y++) {
4940 for (x = 0; x < destRect->w; x++) {
4941 /* Actual source coords */
4942 sx = (srcRect->w * x / (destRect->w)) + srcRect->x;
4943 sy = (srcRect->h * y / (destRect->h)) + srcRect->y;
4944
4945 /* Find a source pixel */
4946 ps = (Uint8 *)src->pixels + (sx * src->format->BytesPerPixel) +
4947 (sy * src->pitch);
4948 #if 0
4949 /* Do we need to draw it? */
4950 switch (src->format->BytesPerPixel)
4951 {
4952 case 1:
4953 {
4954 if (*ps == src->format->colorkey) continue;
4955 break;
4956 }
4957 case 2:
4958 {
4959 Uint16 *ps16 = (Uint16*) ps;
4960 if (*ps16 == src->format->colorkey) continue;
4961 break;
4962 }
4963 case 3:
4964 case 4:
4965 {
4966 Uint32 *ps32 = (Uint32*) ps;
4967 if (*ps32 == src->format->colorkey) continue;
4968 break;
4969 }
4970 }
4971 #endif
4972
4973 /* Actual destination pixel coords */
4974 dx = x + destRect->x;
4975 dy = y + destRect->y;
4976
4977 /* Destination pixel */
4978 pd = (Uint8 *)dest->pixels + (dx * dest->format->BytesPerPixel) +
4979 (dy * dest->pitch);
4980
4981 switch (dest->format->BytesPerPixel)
4982 {
4983 case 1:
4984 {
4985 *pd = *ps;
4986 break;
4987 }
4988 case 2:
4989 {
4990 Uint16 *ps16 = (Uint16*) ps;
4991 Uint16 *pd16 = (Uint16*) pd;
4992 *pd16 = *ps16;
4993 break;
4994 }
4995 case 3:
4996 case 4:
4997 {
4998 Uint32 *ps32 = (Uint32*) ps;
4999 Uint32 *pd32 = (Uint32*) pd;
5000 *pd32 = *ps32;
5001 }
5002 }
5003
5004 }
5005 }
5006
5007 }
5008
5009 /**
5010 * Make the 'pre-stretched' tiles for this window
5011 * Assumes the tiles surface was freed elsewhere
5012 */
5013 static errr sdl_BuildTileset(term_window *win)
5014 {
5015 int x, y;
5016 int ta, td;
5017 int xx, yy;
5018 graphics_mode *info;
5019
5020 if (!GfxSurface) return (1);
5021
5022 info = get_graphics_mode(use_graphics);
5023 if (info->grafID == 0) return (1);
5024
5025 /* Calculate the number of tiles across & down*/
5026 ta = GfxSurface->w / info->cell_width;
5027 td = GfxSurface->h / info->cell_height;
5028
5029 /* Calculate the size of the new surface */
5030 x = ta * win->tile_wid * tile_width;
5031 y = td * win->tile_hgt * tile_height;
5032
5033 /* Make it */
5034 win->tiles = SDL_CreateRGBSurface(SDL_SWSURFACE, x, y,
5035 GfxSurface->format->BitsPerPixel,
5036 GfxSurface->format->Rmask,
5037 GfxSurface->format->Gmask,
5038 GfxSurface->format->Bmask,
5039 GfxSurface->format->Amask);
5040
5041 /* Bugger */
5042 if (!win->tiles) return (1);
5043
5044 /* For every tile... */
5045 for (xx = 0; xx < ta; xx++) {
5046 for (yy = 0; yy < td; yy++) {
5047 SDL_Rect src, dest;
5048 int dwid = win->tile_wid * tile_width;
5049 int dhgt = win->tile_hgt * tile_height;
5050
5051 /* Source rectangle (on GfxSurface) */
5052 RECT(xx * info->cell_width, yy * info->cell_height,
5053 info->cell_width, info->cell_height, &src);
5054
5055 /* Destination rectangle (win->tiles) */
5056 RECT(xx * dwid, yy * dhgt, dwid, dhgt, &dest);
5057
5058 /* Do the stretch thing */
5059 sdl_StretchBlit(GfxSurface, &src, win->tiles, &dest);
5060 }
5061 }
5062
5063 /* see if we need to make a separate surface for the map view */
5064 if (!((tile_width == 1) && (tile_height == 1))) {
5065 /* Calculate the size of the new surface */
5066 x = ta * win->tile_wid;
5067 y = td * win->tile_hgt;
5068
5069 /* Make it */
5070 win->onebyone = SDL_CreateRGBSurface(SDL_SWSURFACE, x, y,
5071 GfxSurface->format->BitsPerPixel,
5072 GfxSurface->format->Rmask, GfxSurface->format->Gmask,
5073 GfxSurface->format->Bmask, GfxSurface->format->Amask);
5074
5075 /* Bugger */
5076 if (!win->onebyone) return (1);
5077
5078 /* For every tile... */
5079 for (xx = 0; xx < ta; xx++) {
5080 for (yy = 0; yy < td; yy++) {
5081 SDL_Rect src, dest;
5082 int dwid = win->tile_wid;
5083 int dhgt = win->tile_hgt;
5084
5085 /* Source rectangle (on GfxSurface) */
5086 RECT(xx * info->cell_width, yy * info->cell_height,
5087 info->cell_width, info->cell_height, &src);
5088
5089 /* Destination rectangle (win->tiles) */
5090 RECT(xx * dwid, yy * dhgt, dwid, dhgt, &dest);
5091
5092 /* Do the stretch thing */
5093 sdl_StretchBlit(GfxSurface, &src, win->onebyone, &dest);
5094 }
5095 }
5096 }
5097
5098 return (0);
5099 }
5100
5101 /**
5102 * Put some gfx on the screen
5103 * XXX - This function _never_ seems to get called with n > 1 ?
5104 * This needs improvement...
5105 */
5106 static errr Term_pict_sdl(int col, int row, int n, const int *ap,
5107 const wchar_t *cp, const int *tap, const wchar_t *tcp)
5108 {
5109
5110 /* Get the right window */
5111 term_window *win = (term_window*)(Term->data);
5112
5113 SDL_Rect rc, src, ur;
5114 int i, j;
5115 bool haddbl = false;
5116 int dhrclip;
5117
5118 /* First time a pict is requested we load the tileset in */
5119 if (!win->tiles) {
5120 sdl_BuildTileset(win);
5121 if (!win->tiles) return (1);
5122 }
5123
5124 /*
5125 * Set exclusive lower bound in y for rendering upper halves of
5126 * double-height tiles.
5127 */
5128 if (overdraw) {
5129 dhrclip = Term_get_first_tile_row(Term) + tile_height - 1;
5130 } else {
5131 /*
5132 * There's no double-height tiles so the value does not
5133 * matter.
5134 */
5135 dhrclip = 0;
5136 }
5137
5138 /* Make the destination rectangle */
5139 RECT(col * win->tile_wid, row * win->tile_hgt, win->tile_wid,
5140 win->tile_hgt, &rc);
5141
5142 /* Translate it */
5143 rc.x += win->border;
5144 rc.y += win->title_height;
5145
5146 /* Stretch for bigtile mode */
5147 rc.w *= tile_width;
5148 rc.h *= tile_height;
5149
5150 /* Get the dimensions of the graphic surface */
5151 src.w = rc.w;
5152 src.h = rc.h;
5153
5154 /* Set up the bounds for what will be updated. */
5155 ur = rc;
5156 ur.w *= n;
5157
5158 /* Clear the way */
5159 Term_wipe_sdl_helper(col, row, n * tile_width, tile_height);
5160
5161 /* Blit 'em! (it) */
5162 for (i = 0; i < n; i++) {
5163 /* Get the terrain tile */
5164 j = (tap[i] & 0x7f);
5165 src.x = (tcp[i] & 0x7F) * src.w;
5166 src.y = j * src.h;
5167
5168 /* if we are using overdraw, draw the top rectangle */
5169 if (overdraw && row > dhrclip &&
5170 j >= overdraw && j <= overdraw_max) {
5171 src.y -= rc.h;
5172 rc.y -= rc.h;
5173 rc.h = (rc.h << 1); /* double the height */
5174 src.h = rc.h;
5175 SDL_BlitSurface(win->tiles, &src, win->surface, &rc);
5176 rc.h = (rc.h >> 1); /* halve the height */
5177 rc.y += rc.h;
5178 haddbl = true;
5179 } else
5180 SDL_BlitSurface(win->tiles, &src, win->surface, &rc);
5181
5182 /* If foreground is the same as background, we're done */
5183 if ((tap[i] == ap[i]) && (tcp[i] == cp[i])) continue;
5184
5185 /* Get the foreground tile */
5186 j = (ap[i] & 0x7f);
5187 src.x = (cp[i] & 0x7F) * src.w;
5188 src.y = j * src.h;
5189
5190 /* if we are using overdraw, draw the top rectangle */
5191 if (overdraw && row > dhrclip &&
5192 j >= overdraw && j <= overdraw_max) {
5193 src.y -= rc.h;
5194 rc.y -= rc.h;
5195 rc.h = (rc.h << 1); /* double the height */
5196 src.h = rc.h;
5197 SDL_BlitSurface(win->tiles, &src, win->surface, &rc);
5198 rc.h = (rc.h >> 1); /* halve the height */
5199 rc.y += rc.h;
5200 haddbl = true;
5201 } else
5202 SDL_BlitSurface(win->tiles, &src, win->surface, &rc);
5203 }
5204
5205 /* Update area */
5206 if (haddbl) {
5207 ur.y -= ur.h;
5208 ur.h *= 2;
5209 }
5210 set_update_rect(win, &ur);
5211
5212 return (0);
5213 }
5214
5215 static void Term_view_map_sdl(term *t)
5216 {
5217 SDL_Surface *fulltiles = NULL;
5218 /* Get the right window */
5219 term_window *win = (term_window*)(t->data);
5220
5221 /* First time a pict is requested we load the tileset in */
5222 if (!win->tiles)
5223 sdl_BuildTileset(win);
5224
5225 /* Override large tiles with small ones for the map view */
5226 if (win->onebyone) {
5227 /* Save screen so we can load it again after the tile image
5228 * is restored */
5229 screen_save();
5230
5231 fulltiles = win->tiles;
5232 win->tiles = win->onebyone;
5233 SDL_FreeSurface(fulltiles);
5234 win->onebyone = NULL;
5235 }
5236
5237 t->view_map_hook = NULL;
5238 do_cmd_view_map();
5239 t->view_map_hook = Term_view_map_sdl;
5240
5241 /* Swap back */
5242 if (fulltiles) {
5243 /* Free everything and rebuild the tileset */
5244 if (win->tiles) {
5245 SDL_FreeSurface(win->tiles);
5246 win->tiles = NULL;
5247 }
5248 sdl_BuildTileset(win);
5249
5250 /* Load screen with the correct tiles - the screen load in the
5251 * view map command was still using the image with small tiles */
5252 screen_load();
5253 }
5254 }
5255
5256
5257 /**
5258 * Create and initialize the Term contined within this window.
5259 */
5260 static void term_data_link_sdl(term_window *win)
5261 {
5262 term *t = &win->term_data;
5263
5264 /* Initialize the term */
5265 term_init(t, win->cols, win->rows, win->keys);
5266
5267 t->higher_pict = true;
5268
5269 /* Use a "software" cursor */
5270 t->soft_cursor = true;
5271
5272 /* Never refresh one row */
5273 t->never_frosh = true;
5274
5275 /* Differentiate between BS/^h, Tab/^i, etc. */
5276 t->complex_input = true;
5277
5278 /* Ignore the init/nuke hooks */
5279
5280 /* Prepare the template hooks */
5281 t->xtra_hook = Term_xtra_sdl;
5282 t->curs_hook = Term_curs_sdl;
5283 t->bigcurs_hook = Term_bigcurs_sdl;
5284 t->wipe_hook = Term_wipe_sdl;
5285 t->text_hook = Term_text_sdl;
5286 t->pict_hook = Term_pict_sdl;
5287 t->view_map_hook = Term_view_map_sdl;
5288 t->dblh_hook = (use_graphics && overdraw) ? is_dh_tile : NULL;
5289
5290 /* Remember where we came from */
5291 t->data = win;
5292 }
5293
5294 /**
5295 * Initialize the status bar:
5296 * Populate it with some buttons
5297 * Set the custom draw function for the bar
5298 */
5299 static void init_morewindows(void)
5300 {
5301 char buf[128];
5302 sdl_Button *button;
5303 int x;
5304
5305 popped = false;
5306
5307 /* Make sure */
5308 sdl_WindowFree(&PopUp);
5309
5310 /* Initialize the status bar */
5311 sdl_WindowInit(&StatusBar, AppWin->w, StatusHeight, AppWin,
5312 &default_term_font);
5313
5314 /* Cusom drawing function */
5315 StatusBar.draw_extra = draw_statusbar;
5316
5317 AboutSelect = sdl_ButtonBankNew(&StatusBar.buttons);
5318 button = sdl_ButtonBankGet(&StatusBar.buttons, AboutSelect);
5319
5320 my_strcpy(buf, buildid, sizeof(buf));
5321
5322 /* Initialize the 'about' button */
5323 sdl_ButtonSize(button, StatusBar.font.width * strlen(buf) + 5,
5324 StatusHeight - 2);
5325 sdl_ButtonMove(button, 1, 1);
5326 sdl_ButtonVisible(button, true);
5327 sdl_ButtonCaption(button, buf);
5328 button->activate = AboutActivate;
5329
5330
5331 /* New button */
5332 TermSelect = sdl_ButtonBankNew(&StatusBar.buttons);
5333 button = sdl_ButtonBankGet(&StatusBar.buttons, TermSelect);
5334
5335 /* Initialize the 'term' button */
5336 sdl_ButtonSize(button, 60, StatusHeight - 2);
5337 x = 100 + (StatusBar.font.width * 5);
5338 sdl_ButtonMove(button, x, 1);
5339 sdl_ButtonVisible(button, true);
5340 button->activate = TermActivate;
5341
5342 /* Another new button */
5343 VisibleSelect = sdl_ButtonBankNew(&StatusBar.buttons);
5344 button = sdl_ButtonBankGet(&StatusBar.buttons, VisibleSelect);
5345
5346 /* Initialize the 'visible' button */
5347 sdl_ButtonSize(button, 60, StatusHeight - 2);
5348 x = 200 + (StatusBar.font.width * 8);
5349 sdl_ButtonMove(button, x, 1);
5350 sdl_ButtonVisible(button, true);
5351 button->activate = VisibleActivate;
5352
5353 /* Another new button */
5354 FontSelect = sdl_ButtonBankNew(&StatusBar.buttons);
5355 button = sdl_ButtonBankGet(&StatusBar.buttons, FontSelect);
5356
5357 /* Initialize the 'font_select' button */
5358 sdl_ButtonSize(button, 60, StatusHeight - 2);
5359 sdl_ButtonMove(button, 400, 1);
5360 button->activate = FontActivate;
5361
5362 /* Another new button */
5363 MoreSelect = sdl_ButtonBankNew(&StatusBar.buttons);
5364 button = sdl_ButtonBankGet(&StatusBar.buttons, MoreSelect);
5365
5366 /* Initialize the 'more' button */
5367 sdl_ButtonSize(button, 60, StatusHeight - 2);
5368 sdl_ButtonMove(button, 400, 1);
5369 sdl_ButtonVisible(button, true);
5370 sdl_ButtonCaption(button, "Options");
5371 button->activate = MoreActivate;
5372
5373 /* Another new button */
5374 QuitSelect = sdl_ButtonBankNew(&StatusBar.buttons);
5375 button = sdl_ButtonBankGet(&StatusBar.buttons, QuitSelect);
5376
5377 /* Initialize the 'quit' button */
5378 sdl_ButtonSize(button, 60, StatusHeight - 2);
5379 sdl_ButtonMove(button, AppWin->w - 61, 1);
5380 sdl_ButtonCaption(button, "Quit");
5381 button->activate = QuitActivate;
5382 sdl_ButtonVisible(button, true);
5383
5384 SetStatusButtons();
5385
5386 TermFocus(0);
5387 }
5388
5389 /**
5390 * The new streamlined graphics loader.
5391 * Only uses colour keys.
5392 * Much more tolerant of different bit-planes
5393 */
5394 static errr load_gfx(void)
5395 {
5396 char buf[1024];
5397 const char *filename;
5398 SDL_Surface *temp;
5399 int i;
5400
5401 if (current_graphics_mode && GfxSurface
5402 && (use_graphics == current_graphics_mode->grafID)) {
5403 return (0);
5404 }
5405
5406 current_graphics_mode = get_graphics_mode(use_graphics);
5407 if (current_graphics_mode) {
5408 filename = current_graphics_mode->file;
5409 } else {
5410 filename = NULL;
5411 }
5412
5413 /* Free the old surface */
5414 if (GfxSurface) SDL_FreeSurface(GfxSurface);
5415
5416 /* This may be called when GRAPHICS_NONE is set */
5417 if (!filename) return (0);
5418
5419 /* Find and load the file into a temporary surface */
5420 path_build(buf, sizeof(buf), current_graphics_mode->path, filename);
5421 temp = IMG_Load(buf);
5422 if (!temp) return (1);
5423
5424 /* Change the surface type to the current video surface format */
5425 GfxSurface = SDL_DisplayFormatAlpha(temp);
5426
5427 /* Make sure we know what pref file to use */
5428 overdraw = current_graphics_mode->overdrawRow;
5429 overdraw_max = current_graphics_mode->overdrawMax;
5430
5431 /* Set double-height tile handling for terminals. */
5432 for (i = 0; i < ANGBAND_TERM_MAX; i++) {
5433 if (angband_term[i]) {
5434 angband_term[i]->dblh_hook = (overdraw) ?
5435 is_dh_tile : NULL;
5436 }
5437 }
5438
5439 /* Reset the graphics mapping for this tileset */
5440 if (character_dungeon) reset_visuals(true);
5441
5442 /* All good */
5443 return (0);
5444 }
5445
5446 /*
5447 * Initialize the graphics
5448 */
5449 static void init_gfx(void)
5450 {
5451 int i;
5452
5453 /* Check for existence of required files */
5454 i = 0;
5455 do {
5456 char path[1024];
5457
5458 /* Check the graphic file */
5459 if (graphics_modes[i].file[0]) {
5460 path_build(path, sizeof(path), graphics_modes[i].path,
5461 graphics_modes[i].file);
5462
5463 if (!file_exists(path)) {
5464 plog_fmt("Can't find file %s - graphics mode '%s' will be disabled.", path, graphics_modes[i].menuname);
5465 graphics_modes[i].file[0] = 0;
5466 }
5467
5468 if ((i + 1) == use_graphics) {
5469 current_graphics_mode = &(graphics_modes[i]);
5470 }
5471 }
5472 } while (graphics_modes[i++].grafID != 0);
5473
5474 /* Check availability (default to no graphics) */
5475 if (!current_graphics_mode->file[0]) {
5476 use_graphics = GRAPHICS_NONE;
5477 arg_graphics = false;
5478 tile_width = 1;
5479 tile_height = 1;
5480 }
5481
5482 /* Load the graphics stuff in */
5483 load_gfx();
5484 }
5485
5486 /**
5487 * Create the windows
5488 * Called sometime after load_prefs()
5489 */
5490 static void init_windows(void)
5491 {
5492 int i;
5493
5494 for (i = 0; i < ANGBAND_TERM_MAX; i++) {
5495 term_window *win = &windows[i];
5496
5497 /* Only bother with visible windows */
5498 if (win->visible) {
5499 /* Don't crowd out the status bar... */
5500 if (win->top < StatusHeight) win->top = StatusHeight;
5501
5502 /* Invalidate the gfx surface */
5503 if (win->tiles) {
5504 SDL_FreeSurface(win->tiles);
5505 win->tiles = NULL;
5506 }
5507 if (win->onebyone) {
5508 SDL_FreeSurface(win->onebyone);
5509 win->onebyone = NULL;
5510 }
5511
5512 /* This will set up the window correctly */
5513 ResizeWin(win, win->width, win->height);
5514 } else {
5515 /* Doesn't exist */
5516 angband_term[i] = NULL;
5517 }
5518
5519 /* Term 0 is at the top */
5520 Zorder[i] = ANGBAND_TERM_MAX - i - 1;
5521 }
5522
5523 /* Good to go... */
5524 Term_activate(term_screen);
5525 }
5526
5527 /**
5528 * Set up some SDL stuff
5529 */
5530 static void init_sdl_local(void)
5531 {
5532 const SDL_VideoInfo *VideoInfo;
5533
5534 int i;
5535 int h, w;
5536 char path[1024];
5537
5538 /* Get information about the video hardware */
5539 VideoInfo = SDL_GetVideoInfo();
5540
5541 /* Require at least 256 colors */
5542 if (VideoInfo->vfmt->BitsPerPixel < 8)
5543 quit_fmt("this %s port requires lots of colors", VERSION_NAME);
5544
5545 full_w = VideoInfo->current_w;
5546 full_h = VideoInfo->current_h;
5547
5548 /* Use a software surface - A tad inefficient, but stable... */
5549 vflags |= SDL_SWSURFACE;
5550
5551 /* Set fullscreen flag */
5552 if (fullscreen) vflags |= SDL_FULLSCREEN;
5553
5554 /* otherwise we make this surface resizable */
5555 else vflags |= SDL_RESIZABLE;
5556
5557 /* Create the main window */
5558 AppWin = SDL_SetVideoMode(fullscreen ? full_w : screen_w,
5559 fullscreen ? full_h : screen_h, 0, vflags);
5560
5561 /* Handle failure */
5562 if (!AppWin)
5563 quit_fmt("failed to create %dx%d window at %d bpp",
5564 screen_w, screen_h, VideoInfo->vfmt->BitsPerPixel);
5565
5566 /* Set the window caption */
5567 SDL_WM_SetCaption(VERSION_NAME, NULL);
5568
5569 /* Enable key repeating; use defaults */
5570 (void)SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,
5571 SDL_DEFAULT_REPEAT_INTERVAL);
5572
5573 /* Enable Unicode (so we can read key codes) */
5574 (void)SDL_EnableUNICODE(1);
5575
5576 /* Build a color for "black" that matches the pixel depth of this surface */
5577 back_colour.r = angband_color_table[COLOUR_DARK][1];
5578 back_colour.g = angband_color_table[COLOUR_DARK][2];
5579 back_colour.b = angband_color_table[COLOUR_DARK][3];
5580 back_pixel_colour = SDL_MapRGB(AppWin->format, back_colour.r,
5581 back_colour.g, back_colour.b);
5582
5583 /* Initialize the colours */
5584 for (i = 0; i < MAX_COLORS; i++) {
5585 text_colours[i].r = angband_color_table[i][1];
5586 text_colours[i].g = angband_color_table[i][2];
5587 text_colours[i].b = angband_color_table[i][3];
5588 }
5589
5590 /* Get the height of the status bar */
5591 if (sdl_CheckFont(&default_term_font, &w, &h)) {
5592 quit_fmt("could not load the default font, %s",
5593 default_term_font.name);
5594 }
5595 StatusHeight = h + 3;
5596
5597 /* Font used for window titles */
5598 sdl_FontCreate(&SystemFont, &default_term_font, AppWin);
5599
5600 /* Get the icon for display in the About box */
5601 path_build(path, sizeof(path), ANGBAND_DIR_ICONS, "att-128.png");
5602 if (file_exists(path))
5603 mratt = IMG_Load(path);
5604 }
5605
5606 /**
5607 * Font sorting function
5608 *
5609 * Orders by width, then height, then face
5610 */
5611 static int cmp_font(const void *f1, const void *f2)
5612 {
5613 const char *font1 = *(const char **)f1;
5614 const char *font2 = *(const char **)f2;
5615 int height1 = 0, height2 = 0;
5616 int width1 = 0, width2 = 0;
5617 char *ew, *face1 = NULL, *ext1 = NULL, *face2 = NULL, *ext2 = NULL;
5618 long lv;
5619
5620 lv = strtol(font1, &ew, 10);
5621 if (ew != font1 && *ew == 'x' && lv > INT_MIN && lv < INT_MAX) {
5622 width1 = (int)lv;
5623 lv = strtol(ew + 1, &face1, 10);
5624 if (face1 != ew + 1 && lv > INT_MIN && lv < INT_MAX) {
5625 height1 = (int)lv;
5626 ext1 = strchr(face1, '.');
5627 if (ext1 == face1) {
5628 ext1 = NULL;
5629 }
5630 }
5631 }
5632 lv = strtol(font2, &ew, 10);
5633 if (ew != font2 && *ew == 'x' && lv > INT_MIN && lv < INT_MAX) {
5634 width2 = (int)lv;
5635 lv = strtol(ew + 1, &face2, 10);
5636 if (face2 != ew + 1 && lv > INT_MIN && lv < INT_MAX) {
5637 height2 = (int)lv;
5638 ext2 = strchr(face2, '.');
5639 if (ext2 == face2) {
5640 ext2 = NULL;
5641 }
5642 }
5643 }
5644
5645 if (!ext1) {
5646 if (!ext2) {
5647 /*
5648 * Neither match the expected pattern. Sort
5649 * alphabetically.
5650 */
5651 return strcmp(font1, font2);
5652 }
5653 /* Put f2 first since it matches the expected pattern. */
5654 return 1;
5655 }
5656 if (!ext2) {
5657 /* Put f1 first since it matches the expected pattern. */
5658 return -1;
5659 }
5660 if (width1 < width2) {
5661 return -1;
5662 }
5663 if (width1 > width2) {
5664 return 1;
5665 }
5666 if (height1 < height2) {
5667 return -1;
5668 }
5669 if (height1 > height2) {
5670 return 1;
5671 }
5672 return strncmp(face1, face2, MAX(ext1 - face1, ext2 - face2));
5673 }
5674
5675 /**
5676 * This function is now mis-named as paths are set correctly by init_stuff()
5677 * in main.c before init_sdl calls this. But it still does some other stuff.
5678 */
5679 static void init_paths(void)
5680 {
5681 int i;
5682 char buf[1024], path[1024];
5683 ang_dir *dir;
5684
5685 /* Hack -- Validate the basic font */
5686 if (default_term_font.preset) {
5687 /* Build the filename */
5688 path_build(path, sizeof(path), ANGBAND_DIR_FONTS,
5689 default_term_font.name);
5690 validate_file(path);
5691 } else {
5692 validate_file(default_term_font.name);
5693 }
5694
5695 for (i = 0; i < MAX_FONTS; i++)
5696 FontList[i] = NULL;
5697
5698 /* Open the fonts directory */
5699 dir = my_dopen(ANGBAND_DIR_FONTS);
5700 if (!dir) return;
5701
5702 /* Read every font to the limit */
5703 while (my_dread(dir, buf, sizeof(buf))) {
5704 path_build(path, sizeof(path), ANGBAND_DIR_FONTS,
5705 buf);
5706 if (is_font_file(path)) {
5707 FontList[num_fonts++] = string_make(buf);
5708 }
5709 /* Don't grow to long */
5710 if (num_fonts == MAX_FONTS) break;
5711 }
5712
5713 sort(FontList, num_fonts, sizeof(FontList[0]), cmp_font);
5714
5715 /* Done */
5716 my_dclose(dir);
5717 }
5718
5719
5720 const char help_sdl[] = "SDL frontend";
5721
5722 /**
5723 * The SDL port's "main()" function.
5724 */
5725 int init_sdl(int argc, char *argv[])
5726 {
5727 /* Initialize SDL: Timer, video, and audio functions */
5728 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
5729 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
5730 return (2);
5731 }
5732
5733 /* Initialize the TTF library */
5734 if (TTF_Init() < 0) {
5735 fprintf(stderr, "Couldn't initialize TTF: %s\n",SDL_GetError());
5736 SDL_Quit();
5737 return (2);
5738 }
5739
5740 /* Init some extra paths */
5741 init_paths();
5742
5743 /* load possible graphics modes */
5744 init_graphics_modes();
5745 GfxButtons = mem_zalloc(sizeof(int) * (graphics_mode_high_id+1));
5746
5747 /* Load prefs */
5748 load_prefs();
5749
5750 /* Get sdl going */
5751 init_sdl_local();
5752
5753 /* Prepare the windows */
5754 init_windows();
5755
5756 /* Prepare the gfx */
5757 init_gfx();
5758
5759 /* Prepare some more windows(!) */
5760 init_morewindows();
5761
5762 /* Activate quit hook */
5763 quit_aux = hook_quit;
5764
5765 /* Paranoia */
5766 return (0);
5767 }
5768
5769 #endif /* USE_SDL */