1 /* File: main-sdl2.h */ 2 /* Purpose: SDL2 frontend header file for mangband */ 3 /* Original SDL2 written by "kts of kettek (kettek1@kettek.net)". */ 4 5 #include <SDL.h> 6 7 /* forward declarations */ 8 typedef struct FontData FontData; 9 typedef struct PictData PictData; 10 typedef struct TermData TermData; 11 /* defines/constants */ 12 #define TERM_MAIN 0 13 #define TERM_MIRROR 1 14 #define TERM_RECALL 2 15 #define TERM_CHOICE 3 16 #define TERM_CHAT 4 17 #define TERM_MAX 8 18 19 // MMM BEGIN: our "special" SDL2 menu system, take note it can be removed fairly easily! 20 static int menu_mode, menu_term; 21 static int menu_x, menu_y; 22 #define MENU_HIDE 0 23 #define MENU_SHOW 1 24 static errr renderMenu(TermData *td); 25 static errr handleMenu(int i, int x, int y); 26 // MMM END 27 28 /* main hooks */ 29 extern errr init_sdl2(void); 30 extern void quit_sdl2(cptr s); 31 32 /* hook declarations */ 33 static errr xtraTermHook(int n, int v); 34 static errr cursTermHook(int x, int y); 35 static errr wipeTermHook(int x, int y, int n); 36 static errr textTermHook(int x, int y, int n, byte attr, cptr s); 37 static errr pictTermHook(int x, int y, int n, const byte *ap, const char *ch, const byte *tap, const char *tcp); 38 static void initTermHook(term *t); 39 static void nukeTermHook(term *t); 40 /* declarations */ 41 struct FontData { 42 cptr filename; // The filename of this font 43 SDL_Surface *surface; // surface of all glyphs 44 Uint8 w, h; // dimensions of character 45 int scalable; // if we can scale this font 46 }; 47 struct PictData { 48 cptr filename; // The filename of this pict 49 SDL_Surface *surface; // surface of sprites 50 Uint8 w, h; // dimensions of each sprite 51 }; 52 #define TERM_LOCK_CELLS (1 << 0) // Do not allow the terminal's rows/cols to be resized 53 #define TERM_LOCK_RATIO (1 << 1) // Force resizes to maintain original ratio of width/height 54 #define TERM_LOCK_SIZE (1 << 2) // Do not allow the window to be resized 55 #define TERM_DO_SCALE (1 << 3) // Scale the renderer 56 #define TERM_DO_STRETCH (1 << 4) // 57 #define TERM_IS_ONLINE (1 << 5) // Term is online 58 #define TERM_IS_VIRTUAL (1 << 6) // Term is virtual and uses term[TERM_MAIN]'s window/renderer 59 #define TERM_IS_HIDDEN (1 << 7) // Whether or not the term should be shown or not 60 #define TERM_FONT_SMOOTH (1 << 8) // whether to use font smoothing 61 // font/pict display modes 62 #define TERM_CELL_FONT 0 // Cell sizings are based on font sizes 63 #define TERM_CELL_PICT 1 // Cell sizings are based on pict sizes 64 #define TERM_CELL_CUST 2 // Cell sizings use orig_w&orig_h, derived from INI file 65 #define TERM_PICT_STRETCH 0 // Pict rendering stretches to fit the cell 66 #define TERM_PICT_SCALE 1 // Pict rendering scales to fit the cell 67 #define TERM_PICT_STATIC 2 // Pict rendering uses the static size 68 #define TERM_CHAR_SCALE 0 // Text rendering scales to fit the cell 69 #define TERM_CHAR_STATIC 1 // Text rendering uses the static size 70 #define TERM_CHAR_STRETCH 2 // Text rendering stretch to fit the cell 71 struct TermData { 72 SDL_Window *window; // The term's actual window 73 SDL_Renderer *renderer; // The renderer for above window 74 SDL_Texture *framebuffer; // Our framebuffer 75 SDL_Texture *alt_framebuffer; // Bad, but if using virtual terminals, TERM_MAIN must have 2 framebuffers, one for the vterm and one for the main render 76 77 unsigned int config; // configuration bit field, see TERM_* 78 Uint8 cell_mode; // See TERM_CELL_* 79 Uint8 pict_mode; // See TERM_PICT_* 80 Uint8 char_mode; // See TERM_CHAR_* 81 82 SDL_Rect ren_rect; // The term's x/y positions and w/h dimensions for rendering 83 SDL_Rect win_rect; // Store window size (UNUSED!!!) 84 int x, y; // The term's x/y position 85 int width, height; // The term's width and height 86 int fb_w, fb_h; // Framebuffer w/h 87 SDL_Rect dng_rect; // Dungeon rect (ALT MODE) 88 int alt_fb_w, alt_fb_h; // Alt.Framebuffer w/h (ALT MODE) 89 u16b dng_cols, dng_rows; // Dungeon size (ALT MODE) 90 91 int zoom; // Amount of zooming / 100 92 93 SDL_Rect menu_rect; 94 SDL_Rect grip_rect; 95 SDL_Rect resize_rect; // Resize rect 96 int need_render; // If this term needs re-render (GUI) 97 int win_need_render; // If this window needs re-render (and all VIRTUAL terms) 98 int need_redraw; // Framebuffer was lost, need to ask Term_draw() to do it all over 99 int need_cutout; // Need to perform special dungeon cutout (ALT MODE) 100 101 Uint32 window_id; // SDL window id acquired from SDL_GetWindowID(window) 102 103 size_t id; // id corresponding to TERM_* defines 104 105 term t; // The actual "z-term" object 106 107 char title[128]; // The title of the z-term 108 109 Uint8 rows, cols; // The term's rows and columns count 110 Uint8 cell_w, cell_h; // The term's cell width and height, in pixels, 111 Uint8 orig_w, orig_h; // The term's original cell width and height, in pixels, 112 // depending on user settings, this is set to font or pict size 113 // I would really prefer not to have these two file names needed, but I want loading/etc. logic to be handled separately from configuration loading 114 char font_file[128]; // Filename of the font 115 int font_size; // Size of the font 116 char pict_file[128]; // Filename of the pict 117 char mask_file[128]; // Filename of the pict 118 119 FontData *font_data; // The term's font data 120 PictData *pict_data; // The term's pict data 121 SDL_Texture *font_texture; // The term's font texture, in memory 122 SDL_Texture *pict_texture; // The term's pict texture 123 }; 124 /* functions */ 125 static errr initTermData(TermData *td, cptr name, int id, cptr font); 126 static errr applyTermConf(TermData *td); 127 static errr setTermCells(TermData *td, int w, int h); 128 static errr setTermTitle(TermData *td); 129 static errr refreshTerm(TermData *td); 130 static void refreshTermAlt(TermData *td); 131 static errr resizeTerm(TermData *td, int rows, int cols); 132 static errr loadConfig(void); 133 static errr saveConfig(void); 134 static errr loadFont(TermData *td, cptr filename, int fontsize, int smoothing); 135 static errr unloadFont(TermData *td); 136 static errr attachFont(FontData *fd, TermData *td); 137 static errr detachFont(TermData *td); 138 static errr loadPict(TermData *td, cptr filename); 139 static errr unloadPict(TermData *td); 140 static errr attachPict(PictData *pd, TermData *td); 141 static errr detachPict(TermData *td); 142 static errr fileToFont(FontData *fd, cptr filename, int fontsize, int smoothing); 143 static errr cleanFontData(FontData *fd); 144 145 static errr imgToPict(PictData *pd, cptr filename, cptr maskname); 146 static errr cleanPictData(PictData *pd); 147 148 /* GUI */ 149 static void renderTerm(TermData *td); 150 static errr renderGui(TermData *td); 151 static errr sysText(TermData *td, int x, int y, int n, byte attr, cptr s); 152 static void termStack(int i); 153 static void termConstrain(int i); 154 155 /* ALT.DUNGEON */ 156 static void wipeTermCell_UI(int x, int y, int cutout); 157 static errr textTermHook_ALT(int x, int y, int n, byte attr, cptr s); 158 static errr pictTermHook_ALT(int x, int y, int n, const byte *ap, const char *cp, const byte *tap, const char *tcp); 159 160 161 /* LISENGLAS Overlay */ 162 #define LISEN_W 96 163 #define LISEN_H 96 164 static errr loadUiCircle(void); 165 static void unloadUiCircle(void); 166 static void updateLisenglas(int wx, int wy); 167 168 /* MIcons Overlay */ 169 enum IconAction { 170 171 MICON_DO_NOTHING, 172 173 MICON_RUN_COMMAND, 174 MICON_LOCAL_COMMAND_HACK, 175 MICON_PICK_DIRECTION, 176 MICON_SINGLE_KEY, 177 178 MICON_TOGGLE_ROOT, 179 180 MICON_SELECT_INVEN, 181 MICON_SELECT_EQUIP, 182 MICON_SELECT_FLOOR, 183 MICON_SELECT_SPELL, 184 185 MICON_ESCAPE, 186 MICON_TOGGLE_KEYBOARD, 187 MICON_TOGGLE_OVERLAY, 188 189 MICON_LOCAL_QUICK_SLOT, 190 MICON_LOCAL_EXECUTE_MACRO, 191 192 }; 193 #define MICON_W 32 194 #define MICON_H 32 195 #define MICON_S 16 196 static errr loadCmdFont(cptr filename); 197 static void unloadCmdFont(void); 198 static errr loadTinyFont(cptr filename); 199 static void unloadTinyFont(void); 200 static void renderIconOverlay(TermData *td); 201 static void altCoord(int wx, int wy, int *x, int *t); 202 static void renderLisenGlas(TermData *td); 203 static void drawIconPanel_Commands(SDL_Rect *size, int filter); 204 static void drawIconPanel_Direction(SDL_Rect *size, bool allow_target, bool allow_friend); 205 static void drawIconPanel_Confirmation(SDL_Rect *size); 206 static void drawIconPanel_Selector(SDL_Rect *size, int filter); 207 static void drawIconPanel_Inventory(SDL_Rect *size, bool allow_inven, bool allow_equip, bool allow_floor); 208 static void drawIconPanel_Equipment(SDL_Rect *size, bool allow_inven, bool allow_equip, bool allow_floor); 209 static void drawIconPanel_Spells(SDL_Rect *size, int spell_realm, int spell_book); 210 static void drawIconPanel_Slots(SDL_Rect *size); 211 static void drawIconPanel_PassedMenu(SDL_Rect *size); 212 static errr iconPict(SDL_Rect *pos, byte a, char c, bool remember); 213 static void drawUiIcon(SDL_Rect *pos, int k); 214 static int matchIcon(int wx, int wy); 215 static void handleMIcon(int action, int sub_action); 216 static void renderMIcon(SDL_Rect *panel, SDL_Rect *pos, int action, int sub_action, int draw, int spacing); 217 static void convertIconToSlot(void *slot, void *icon, int slot_id); 218 219 /* Defines for 'additional' icons in the icon font. 220 * See graf/ui-cmd.txt for source list. */ 221 #define MICO_KBD 0xBA 222 #define MICO_SETTINGS 0xBB 223 #define MICO_YES 0xBC 224 #define MICO_NO 0xBD 225 #define MICO_DIRPAD 0xBE 226 #define MICO_SPEAKER_ON 0xBF 227 #define MICO_SPEAKER_OFF 0xBF 228 #define MICO_ONE_ATT 0xC1 229 #define MICO_OPEN_FOLDER 0xC2 230 #define MICO_SAVE_FLOPPY 0xC3 231 #define MICO_DUNGEON 0xC4 232 #define MICO_FINGER_CLICK 0xC5 233 #define MICO_SWORD_IN_STONE 0xC6 234 #define MICO_TREASURE_MAP 0xC7 235 #define MICO_TOMBSTONE 0xC8 236 #define MICO_FLOOR_ITEM 0xC9 237 #define MICO_QUIVER 0xCA 238 #define MICO_SPELL_BOOK 0xD0 239 #define MICO_SPELL_ITEM 0xD1 240 #define MICO_SPELL_HAND 0xD2 241 #define MICO_SPELL_BALL 0xD3 242 #define MICO_SPELL_BEAM 0xD4 243 #define MICO_SPELL_UNKNOWN 0xD5 244 #define MICO_SPELL_ILLEGIBLE 0xD6 245 #define MICO_SPELL_PROJECT 0xD7 246 #define MICO_SELL 0xEE 247 #define MICO_PURCHASE 0xE2 248