summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/tilesdl.cc
blob: fdb888cf17074a0968c6d9299fed4ff66ccd031e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
#include "cio.h"
#include "itemname.h"
#include "items.h"
#include "itemprop.h"
#include "mon-util.h"
#include "player.h"
#include "stuff.h"
#include "tiles.h"
#include "tilesdl.h"
#include "travel.h"
#include "version.h"
#include "view.h"

#include "tiledef-dngn.h"
#include "tilefont.h"

#include <SDL.h>
#include <SDL_opengl.h>
#include <SDL_image.h>

// Note: these defaults should match the OpenGL defaults
GLState::GLState() :
    array_vertex(false),
    array_texcoord(false),
    array_colour(false),
    blend(false),
    texture(false)
{
}

void GLStateManager::init()
{
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glClearColor(0.0, 0.0, 0.0, 1.0f);
}

void GLStateManager::set(const GLState& state)
{
    if (state.array_vertex)
        glEnableClientState(GL_VERTEX_ARRAY);
    else
        glDisableClientState(GL_VERTEX_ARRAY);

    if (state.array_texcoord)
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    else
        glDisableClientState(GL_TEXTURE_COORD_ARRAY);

    if (state.array_colour)
    {
        glEnableClientState(GL_COLOR_ARRAY);
    }
    else
    {
        glDisableClientState(GL_COLOR_ARRAY);

        // [enne] This should *not* be necessary, but the Linux OpenGL
        // driver that I'm using sets this to the last colour of the
        // colour array.  So, we need to unset it here.
        glColor3f(1.0f, 1.0f, 1.0f);
    }

    if (state.texture)
        glEnable(GL_TEXTURE_2D);
    else
        glDisable(GL_TEXTURE_2D);

    if (state.blend)
        glEnable(GL_BLEND);
    else
        glDisable(GL_BLEND);
}

TilesFramework tiles;

TilesFramework::TilesFramework() :
    m_windowsz(1024, 768),
    m_viewsc(0, 0),
    m_context(NULL),
    m_fullscreen(false),
    m_active_layer(LAYER_CRT),
    m_buttons_held(0),
    m_key_mod(0),
    m_mouse(-1, -1),
    m_last_tick_moved(0)
{
}

TilesFramework::~TilesFramework()
{
}

void TilesFramework::shutdown()
{
    delete m_region_tile;
    delete m_region_stat;
    delete m_region_msg;
    delete m_region_map;
    delete m_region_self_inv;
    delete m_region_crt;
    delete m_region_menu_inv;

    m_region_tile = NULL;
    m_region_stat = NULL;
    m_region_msg = NULL;
    m_region_map = NULL;
    m_region_self_inv = NULL;
    m_region_crt = NULL;
    m_region_menu_inv = NULL;

    for (unsigned int i = 0; i < LAYER_MAX; i++)
    {
        m_layers[i].m_regions.clear();
    }

    for (unsigned int i = 0; i < m_fonts.size(); i++)
    {
        delete m_fonts[i].font;
        m_fonts[i].font = NULL;
    }

    SDL_Quit();
}

bool TilesFramework::initialise()
{
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0)
    {
        printf ("Failed to initialise SDL: %s\n", SDL_GetError());
        return false;
    }

    SDL_EnableUNICODE(true);

    SDL_WM_SetCaption(CRAWL " " VERSION, CRAWL);
    SDL_Surface *icon = IMG_Load("dat/tiles/stone_soup_icon-32x32.png");
    if (!icon)
    {
        printf ("Failed to load icon: %s\n", SDL_GetError());
        return false;
    }
    SDL_WM_SetIcon(icon, NULL);

    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
  
    unsigned int flags = SDL_OPENGL;
    if (Options.tile_full_screen)
        flags |= SDL_FULLSCREEN;

    m_windowsz.x = Options.tile_window_width;
    m_windowsz.y = Options.tile_window_height;

    m_context = SDL_SetVideoMode(m_windowsz.x, m_windowsz.y, 0, flags);
    if (!m_context)
    {
        printf ("Failed to set video mode: %s\n", SDL_GetError());
        return false;
    }

    if (!m_image.load_textures())
        return false;

    int crt_font = load_font(Options.tile_font_crt_file.c_str(),
                             Options.tile_font_crt_size);
    int msg_font = load_font(Options.tile_font_msg_file.c_str(),
                             Options.tile_font_msg_size);
    int stat_font = load_font(Options.tile_font_stat_file.c_str(),
                              Options.tile_font_stat_size);
    m_tip_font = load_font(Options.tile_font_tip_file.c_str(),
                           Options.tile_font_tip_size);
    int lbl_font = load_font(Options.tile_font_lbl_file.c_str(),
                             Options.tile_font_lbl_size);

    if (crt_font == -1 || msg_font == -1 || stat_font == -1
        || m_tip_font == -1 || lbl_font == -1)
    {
        return false;
    }

    m_region_tile = new DungeonRegion(&m_image, m_fonts[lbl_font].font, 
                                      TILE_X, TILE_Y);
    m_region_map = new MapRegion(Options.tile_map_pixels);
    m_region_self_inv = new InventoryRegion(&m_image, m_fonts[lbl_font].font,
                                            TILE_X, TILE_Y);

    m_region_msg = new MessageRegion(m_fonts[msg_font].font);
    m_region_stat = new StatRegion(m_fonts[stat_font].font);
    m_region_crt = new CRTRegion(m_fonts[crt_font].font);
    m_region_menu_inv = new InventoryRegion(&m_image, m_fonts[lbl_font].font,
                                            TILE_X, TILE_Y);

    m_layers[LAYER_NORMAL].m_regions.push_back(m_region_tile);
    m_layers[LAYER_NORMAL].m_regions.push_back(m_region_map);
    m_layers[LAYER_NORMAL].m_regions.push_back(m_region_self_inv);
    m_layers[LAYER_NORMAL].m_regions.push_back(m_region_msg);
    m_layers[LAYER_NORMAL].m_regions.push_back(m_region_stat);

    m_layers[LAYER_CRT].m_regions.push_back(m_region_crt);
    m_layers[LAYER_CRT].m_regions.push_back(m_region_menu_inv);

    cgotoxy(1, 1, GOTO_CRT);

    GLStateManager::init();

    resize();

    return true;
}

int TilesFramework::load_font(const char *font_file, int font_size,
                              bool default_on_fail)
{
    FTFont *font = new FTFont();

    for (unsigned int i = 0; i < m_fonts.size(); i++)
    {
        font_info &finfo = m_fonts[i];
        if (finfo.name == font_file && finfo.size == font_size)
            return i;
    }
    
    if (!font->load_font(font_file, font_size))
    {
        delete font;
        if (default_on_fail)
            return (load_font("VeraMono.ttf", 12, false));
        else
            return -1;
    }

    font_info finfo;
    finfo.name = font_file;
    finfo.size = font_size;
    finfo.font = font;
    m_fonts.push_back(finfo);

    return (m_fonts.size() - 1);
}

void TilesFramework::load_dungeon(unsigned int *tileb, int gx, int gy)
{
    unsigned int ox = m_region_tile->mx/2;
    unsigned int oy = m_region_tile->my/2;
    m_region_tile->load_dungeon(tileb, gx - ox, gy - oy);

    coord_def win_start(gx - ox, gy - oy);
    coord_def win_end(gx + ox + 1, gy + oy + 1);

    m_region_map->set_window(win_start, win_end);
}

void TilesFramework::load_dungeon(int cx, int cy)
{
    int wx = m_region_tile->mx;
    int wy = m_region_tile->my;
    unsigned int *tb = (unsigned int*)alloca(sizeof(unsigned int) *
                                             wy * wx * 2);

    int count = 0;
    for (int y = 0; y < wy; y++)
    {
        for (int x = 0; x < wx; x++)
        {
            unsigned int fg;
            unsigned int bg;

            const coord_def gc(cx + x - wx/2,
                               cy + y - wy/2);
            const coord_def ep = view2show(grid2view(gc));

            // mini "viewwindow" routine
            if (!map_bounds(gc))
            {
                fg = 0;
                bg = TILE_DNGN_UNSEEN;
            }
            else if (!crawl_view.in_grid_los(gc) || !env.show(ep))
            {
                fg = env.tile_bk_fg[gc.x][gc.y];
                bg = env.tile_bk_bg[gc.x][gc.y];
                if (!fg && !bg)
                    tileidx_unseen(fg, bg, get_envmap_char(gc.x, gc.y), gc);
                bg |=  tile_unseen_flag(gc);
            }
            else
            {
                fg = env.tile_fg[ep.x-1][ep.y-1];
                bg = env.tile_bg[ep.x-1][ep.y-1];
            }

            if (gc.x == cx && gc.y == cy)
                bg |= TILE_FLAG_CURSOR1;

            tb[count++] = fg;
            tb[count++] = bg;
        }
    }

    load_dungeon(tb, cx, cy);
    tiles.redraw();
}

void TilesFramework::resize()
{
    do_layout();

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    // For ease, vertex positions are pixel positions.
    glOrtho(0, m_windowsz.x, m_windowsz.y, 0, 0, 100); 
}

static unsigned char _get_modifiers(SDL_keysym &keysym)
{
    // keysym.mod can't be used to keep track of the modifier state.
    // If shift is hit by itself, this will not include KMOD_SHIFT.
    // Instead, look for the key itself as a separate event. 
    switch (keysym.sym)
    {
    case SDLK_LSHIFT:
    case SDLK_RSHIFT:
        return MOD_SHIFT;
    case SDLK_LCTRL:
    case SDLK_RCTRL:
        return MOD_CTRL;
    case SDLK_LALT:
    case SDLK_RALT:
        return MOD_ALT;
    default:
        return 0;
    }
}

static int _translate_keysym(SDL_keysym &keysym)
{
    // This function returns the key that was hit.  Returning zero implies that
    // the keypress (e.g. hitting shift on its own) should be eaten and not
    // handled.

    const int shift_offset = CK_SHIFT_UP - CK_UP;
    const int ctrl_offset = CK_CTRL_UP - CK_UP;

    int offset = 0;
    if (keysym.mod & KMOD_CTRL)
        offset = ctrl_offset;
    else if (keysym.mod & KMOD_SHIFT)
        offset = shift_offset;

    switch (keysym.sym)
    {
    case SDLK_RETURN:
        return CK_ENTER;
    case SDLK_BACKSPACE:
        return CK_BKSP;
    case SDLK_ESCAPE:
        return CK_ESCAPE;
    case SDLK_DELETE:
        return CK_DELETE;

    // Hack.  libw32c overloads clear with '5' too.
    case SDLK_KP5:
        return CK_CLEAR + offset;

    case SDLK_KP8:
    case SDLK_UP:
        return CK_UP + offset;
    case SDLK_KP2:
    case SDLK_DOWN:
        return CK_DOWN + offset;
    case SDLK_KP4:
    case SDLK_LEFT:
        return CK_LEFT + offset;
    case SDLK_KP6:
    case SDLK_RIGHT:
        return CK_RIGHT + offset;
    case SDLK_KP0:
    case SDLK_INSERT:
        return CK_INSERT + offset;
    case SDLK_KP7:
    case SDLK_HOME:
        return CK_HOME + offset;
    case SDLK_KP1:
    case SDLK_END:
        return CK_END + offset;
    case SDLK_CLEAR:
        return CK_CLEAR + offset;
    case SDLK_KP9:
    case SDLK_PAGEUP:
        return CK_PGUP + offset;
    case SDLK_KP3:
    case SDLK_PAGEDOWN:
        return CK_PGDN + offset;
    default:
        break;
    }

    bool is_ascii = ((keysym.unicode & 0xFF80) == 0);
    return is_ascii ? keysym.unicode & 0x7F : 0;
}


int TilesFramework::handle_mouse(MouseEvent &event)
{
    m_region_tile->place_cursor(CURSOR_MOUSE, Region::NO_CURSOR);

    // Note: the mouse event goes to all regions in the active layer because
    // we want to be able to start some GUI event (e.g. far viewing) and
    // stop if it moves to another region.
    int return_key = 0;
    for (unsigned int i = 0; i < m_layers[m_active_layer].m_regions.size(); i++)
    {
        // TODO enne - what if two regions give a key?
        int key = 0;
        key = m_layers[m_active_layer].m_regions[i]->handle_mouse(event);
        if (key)
            return_key = key;
    }

    // Let regions take priority in any mouse mode.
    if (return_key)
        return return_key;

    // Handle "more" mode globally here, rather than duplicate across regions.
    if (mouse_control::current_mode() == MOUSE_MODE_MORE
        && event.button == MouseEvent::LEFT
        && event.event == MouseEvent::PRESS)
    {
        return '\r';
    }

    // TODO enne - in what cases should the buttons be returned?
#if 0
    // If nothing else, return the mouse button that was pressed.
    switch (event.button)
    {
    case MouseEvent::LEFT:
        return CK_MOUSE_B1;
    case MouseEvent::RIGHT:
        return CK_MOUSE_B2;
    case MouseEvent::MIDDLE:
        return CK_MOUSE_B3;
    case MouseEvent::SCROLL_UP:
        return CK_MOUSE_B4;
    case MouseEvent::SCROLL_DOWN:
        return CK_MOUSE_B5;
    default:
    case MouseEvent::NONE:
        return 0;
    }
#endif

    return 0;
}

static void _translate_event(const SDL_MouseMotionEvent &sdl_event,
    MouseEvent &tile_event)
{
    tile_event.held = MouseEvent::NONE;
    tile_event.event = MouseEvent::MOVE;
    tile_event.button = MouseEvent::NONE;
    tile_event.px = sdl_event.x;
    tile_event.py = sdl_event.y;

    // TODO enne - do we want the relative motion?
}

static void _translate_event(const SDL_MouseButtonEvent &sdl_event,
    MouseEvent &tile_event)
{
    tile_event.held = MouseEvent::NONE;
    tile_event.event = (sdl_event.type == SDL_MOUSEBUTTONDOWN) ?
                       MouseEvent::PRESS : MouseEvent::RELEASE;
    switch (sdl_event.button)
    {
    case SDL_BUTTON_LEFT:
        tile_event.button = MouseEvent::LEFT;
        break;
    case SDL_BUTTON_RIGHT:
        tile_event.button = MouseEvent::RIGHT;
        break;
    case SDL_BUTTON_MIDDLE:
        tile_event.button = MouseEvent::MIDDLE;
        break;
    case SDL_BUTTON_WHEELUP:
        tile_event.button = MouseEvent::SCROLL_UP;
        break;
    case SDL_BUTTON_WHEELDOWN:
        tile_event.button = MouseEvent::SCROLL_DOWN;
        break;
    default:
        ASSERT(!"Unhandled button");
        tile_event.button = MouseEvent::NONE;
        break;
    }
    tile_event.px = sdl_event.x;
    tile_event.py = sdl_event.y;
}

static unsigned int _timer_callback(unsigned int ticks)
{
    // force the event loop to break
    SDL_Event event;
    event.type = SDL_USEREVENT;
    SDL_PushEvent(&event);

    unsigned int res = std::max(30, Options.tile_tooltip_ms);
    return res;
}

int TilesFramework::getch_ck()
{
    SDL_Event event;

    int key = 0;

    const unsigned int ticks_per_redraw = 16; // 60 FPS = 16.6 ms/frame
    unsigned int last_redraw_tick = 0;

    unsigned int res = std::max(30, Options.tile_tooltip_ms);
    SDL_SetTimer(res, &_timer_callback);

    while (!key)
    {
        unsigned int ticks;

        if (SDL_WaitEvent(&event))
        {
            ticks = SDL_GetTicks();
          
            if (event.type != SDL_USEREVENT)
                tiles.clear_text_tags(TAG_CELL_DESC);

            switch (event.type)
            {
            case SDL_KEYDOWN:
                m_key_mod |= _get_modifiers(event.key.keysym);
                key = _translate_keysym(event.key.keysym);

                // If you hit a key, disable tooltips until the mouse
                // is moved again.
                m_last_tick_moved = ~0; 
                break;

            case SDL_KEYUP:
                m_key_mod &= ~_get_modifiers(event.key.keysym);
                m_last_tick_moved = ~0; 
                break;

            case SDL_MOUSEMOTION:
                {
                    // Record mouse pos for tooltip timer
                    if (m_mouse.x != event.motion.x
                        || m_mouse.y != event.motion.y)
                    {
                        m_last_tick_moved = ticks;
                    }
                    m_mouse.x = event.motion.x;
                    m_mouse.y = event.motion.y;

                    MouseEvent mouse_event;
                    _translate_event(event.motion, mouse_event);
                    mouse_event.held = m_buttons_held;
                    mouse_event.mod = m_key_mod;
                    key = handle_mouse(mouse_event);
                }
                break;

            case SDL_MOUSEBUTTONUP:
                {
                    MouseEvent mouse_event;
                    _translate_event(event.button, mouse_event);
                    m_buttons_held &= ~(mouse_event.button);
                    mouse_event.held = m_buttons_held;
                    mouse_event.mod = m_key_mod;
                    key = handle_mouse(mouse_event);
                    m_last_tick_moved = ticks; 
                }
                break;

            case SDL_MOUSEBUTTONDOWN:
                {
                    MouseEvent mouse_event;
                    _translate_event(event.button, mouse_event);
                    m_buttons_held |= mouse_event.button;
                    mouse_event.held = m_buttons_held;
                    mouse_event.mod = m_key_mod;
                    key = handle_mouse(mouse_event);
                    m_last_tick_moved = ticks; 
                }
                break;

            case SDL_QUIT:
                // TODO enne
                exit(0);
                
            case SDL_USEREVENT:
            default:
                // This is only used to refresh the tooltip.
                break;
            }
        }

        bool show_tooltip = ((ticks - m_last_tick_moved 
                                > (unsigned int)Options.tile_tooltip_ms)
                                && ticks > m_last_tick_moved);

        if (show_tooltip)
        {
            tiles.clear_text_tags(TAG_CELL_DESC);
            if (m_tooltip.empty())
            {
                for (unsigned int i = 0; 
                    i < m_layers[m_active_layer].m_regions.size(); i++)
                {
                    Region *reg = m_layers[m_active_layer].m_regions[i];
                    if (!reg->inside(m_mouse.x, m_mouse.y))
                        continue;
                    if (reg->update_tip_text(m_tooltip))
                        break;
                }
            }
        }
        else
        {
            m_tooltip.clear();
        }

        if (ticks - last_redraw_tick > ticks_per_redraw)
        {
            redraw();
            last_redraw_tick = ticks;
        }
    }

    SDL_SetTimer(0, NULL);

    return key;
}

void TilesFramework::do_layout()
{
    // View size in pixels is (m_viewsc * crawl_view.viewsz)
    m_viewsc.x = 32;
    m_viewsc.y = 32;

    const int map_stat_buffer = 4;
    const int crt_width = 80;
    const int crt_height = 30;
    const int map_margin = 2;

    crawl_view.viewsz.x = Options.view_max_width;
    crawl_view.viewsz.y = Options.view_max_height;
    crawl_view.msgsz.x = crt_width;

    // Initial sizes.
    m_region_tile->dx = m_viewsc.x;
    m_region_tile->dy = m_viewsc.y;
    m_region_tile->resize(crawl_view.viewsz.x, crawl_view.viewsz.y);
    m_region_crt->resize(crt_width, crt_height);
    m_region_stat->resize(crawl_view.hudsz.x, crawl_view.hudsz.y);
    m_region_msg->resize(crawl_view.msgsz.x, crawl_view.msgsz.y);
    m_region_map->resize(GXM, GYM);
    m_region_menu_inv->resize(24, 1);

    // Place regions for normal layer
    const int margin = 4;

    m_region_tile->place(0, 0, margin);
    m_region_msg->place(0, m_region_tile->ey - margin, margin);

    bool message_overlay = false;

    if (m_windowsz.y < m_region_tile->dy * VIEW_MIN_HEIGHT)
    {
        crawl_view.viewsz.x = VIEW_MIN_WIDTH;
        crawl_view.viewsz.y = VIEW_MIN_HEIGHT;
        m_region_tile->place(0, 0, 0);
        int factor = m_windowsz.y / crawl_view.viewsz.y;
        m_viewsc.x = m_viewsc.y = std::min(32, factor);
        m_region_tile->dx = m_viewsc.x;
        m_region_tile->dy = m_viewsc.y;
        m_region_tile->resize(crawl_view.viewsz.x, crawl_view.viewsz.y);
        m_region_msg->place(0, 0, 0);
        message_overlay = true;
    }
    else
    {
        // Shrink viewsz if too wide:
        while (m_region_tile->wx + m_region_stat->wx > m_windowsz.x
               && crawl_view.viewsz.x > VIEW_MIN_WIDTH)
        {
            crawl_view.viewsz.x -= 2;
            m_region_tile->mx = crawl_view.viewsz.x;
            m_region_tile->place(0, 0, margin);
            m_region_msg->place(0, m_region_tile->ex, margin);
        }

        // Shrink viewsz if too tall:
        while (m_region_tile->wy + m_region_msg->wy > m_windowsz.y
               && crawl_view.viewsz.y > VIEW_MIN_HEIGHT)
        {
            crawl_view.viewsz.y -= 2;
            m_region_tile->my = crawl_view.viewsz.y;
            m_region_tile->place(0, 0, margin);
            m_region_msg->place(0, m_region_tile->ey, margin);
        }

        // Shrink msgsz if too tall:
        while (m_region_tile->wy + m_region_msg->wy > m_windowsz.y
               && crawl_view.msgsz.y > MSG_MIN_HEIGHT)
        {
            m_region_msg->resize(m_region_msg->mx, --crawl_view.msgsz.y);
        }

        if (m_region_tile->wy + m_region_msg->wy > m_windowsz.y)
        {
            m_region_tile->place(0, 0, 0);
            m_region_msg->place(0, 0, 0);
            message_overlay = true;
        }
    }

    if (message_overlay)
    {
        m_region_msg->resize_to_fit(m_region_tile->ex, m_region_msg->ey);
        crawl_view.msgsz.x = m_region_msg->mx;
        crawl_view.msgsz.y = m_region_msg->my;
    }
    else
    {
        m_region_msg->resize_to_fit(m_region_msg->wx,
                                    m_windowsz.y - m_region_msg->sx);
        int msg_y = std::min(Options.msg_max_height, (int)m_region_msg->my);
        m_region_msg->resize(m_region_msg->mx, msg_y);
    }
    m_region_msg->set_overlay(message_overlay);

    // Shrink view width if stat window can't fit...
    int stat_col;
    crawl_view.viewsz.x += 2;
    do
    {
        crawl_view.viewsz.x -= 2;
        m_region_tile->mx = crawl_view.viewsz.x;
        m_region_tile->place(0, 0, margin);

        stat_col = m_region_tile->ex + map_stat_buffer;
        m_region_stat->place(stat_col, 0, 0);
        m_region_stat->resize_to_fit(m_windowsz.x - m_region_stat->sx,
                                     m_region_stat->wy);
    }
    while (m_region_stat->ex > m_windowsz.x
           && crawl_view.viewsz.x > VIEW_MIN_WIDTH);

    int hud_width = std::min(50, (int)m_region_stat->mx);
    m_region_stat->resize(hud_width, m_region_stat->my);
    crawl_view.hudsz.x = m_region_stat->mx;
    crawl_view.hudsz.y = m_region_stat->my;

    // Resize map to fit the screen
    m_region_map->dx = m_region_map->dy = Options.tile_map_pixels;
    m_region_map->place(stat_col, m_region_stat->ey, map_margin);
    while (m_region_map->ex > m_windowsz.x)
    {
        m_region_map->dx--;
        m_region_map->dy--;
        m_region_map->resize(GXM, GYM);
    }

    int inv_col = std::max(m_region_tile->ex, m_region_msg->ex);
    if (message_overlay)
        inv_col = stat_col;

    m_region_self_inv->place(inv_col, m_region_map->ey, 0);
    m_region_self_inv->resize_to_fit(m_windowsz.x - 
                                     m_region_self_inv->sx,
                                     m_windowsz.y -
                                     m_region_self_inv->sy);
    m_region_self_inv->resize(std::min(13, (int)m_region_self_inv->mx),
                              std::min(6, (int)m_region_self_inv->my));

    int self_inv_y = m_windowsz.y - m_region_self_inv->wy - margin;
    m_region_self_inv->place(inv_col, self_inv_y, 0);

    // recenter map above inventory
    int map_cen_x = m_region_self_inv->sx + (m_region_self_inv->wx) / 2;
    m_region_map->place(map_cen_x - m_region_map->wy / 2, m_region_map->sy,
                        map_margin);

    // Place regions for crt layer
    m_region_crt->place(0, 0, margin);

    while (m_region_crt->ey + m_region_menu_inv->wy > m_windowsz.y)
    {
        m_region_crt->resize(m_region_crt->mx, m_region_crt->my - 1);
    }
    while (m_region_crt->ex > m_windowsz.x)
    {
        m_region_crt->resize(m_region_crt->mx - 1, m_region_crt->my);
    }

    m_region_menu_inv->place(0, m_region_crt->ey, margin);
    m_region_menu_inv->resize_to_fit(m_windowsz.x, m_windowsz.y -
                                     m_region_menu_inv->sy);
 
    // Depending on the font, the menu inventory may hold fewer items
    // than the crt menu can display.  Decrease the lines if necessary.
    const int ex = 3;
    if (m_region_crt->my - ex > m_region_menu_inv->mx)
        m_region_crt->resize(m_region_crt->mx, m_region_menu_inv->mx + ex);

    crawl_view.init_view();
}

void TilesFramework::clrscr()
{
    TextRegion::cursor_region = NULL;

    if (m_region_stat)
        m_region_stat->clear();
    if (m_region_msg)
        m_region_msg->clear();
    if (m_region_crt)
        m_region_crt->clear();
    if (m_region_menu_inv)
        m_region_menu_inv->clear();

    cgotoxy(1,1);
}

void TilesFramework::message_out(int which_line, int colour, const char *s, int firstcol, bool newline)
{
    if (!firstcol)
        firstcol = Options.delay_message_clear ? 2 : 1;

    cgotoxy(firstcol, which_line + 1, GOTO_MSG);
    textcolor(colour);

    cprintf("%s", s);

    if (newline && which_line == crawl_view.msgsz.y - 1)
        m_region_msg->scroll();
}

void TilesFramework::clear_message_window()
{
    m_region_msg->clear();
    m_active_layer = LAYER_NORMAL;
}

int TilesFramework::get_number_of_lines()
{
    return m_region_crt->my;
}

int TilesFramework::get_number_of_cols()
{
    // TODO enne - do we need to differentiate the number of columns
    // in the message window and the number of columns on the CRT?
    return m_region_crt->mx;
}

void TilesFramework::cgotoxy(int x, int y, int region)
{
    if (region == GOTO_LAST)
    {
        // nothing
    }
    else if (region == GOTO_CRT)
    {
        m_active_layer = LAYER_CRT;
        TextRegion::text_mode = m_region_crt;
    }
    else if (region == GOTO_MSG)
    {
        m_active_layer = LAYER_NORMAL;
        TextRegion::text_mode = m_region_msg;
    }
    else if (region == GOTO_STAT)
    {
        m_active_layer = LAYER_NORMAL;
        TextRegion::text_mode = m_region_stat;
    }

    TextRegion::cgotoxy(x, y);
}

void TilesFramework::redraw()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glScalef(m_viewsc.x, m_viewsc.y, 1.0f);

    for (unsigned int i = 0; i < m_layers[m_active_layer].m_regions.size(); i++)
    {
        m_layers[m_active_layer].m_regions[i]->render();
    }

    // Draw tooltip
    if (!m_tooltip.empty())
    {
        const coord_def min_pos(0, 0);
        FTFont *font = m_fonts[m_tip_font].font;
        font->render_string(m_mouse.x, m_mouse.y - 2, m_tooltip.c_str(),
                            min_pos, m_windowsz, WHITE, false, 150,
                            BLUE, 5);
    }
            
    SDL_GL_SwapBuffers();
}

void TilesFramework::update_minimap(int gx, int gy)
{
    if (!player_in_mappable_area())
        return;

    int object = env.map[gx][gy].object;
    map_feature f = (object >= DNGN_START_OF_MONSTERS) ? MF_MONS_HOSTILE :
        get_feature_def((dungeon_feature_type)object).minimap;

    if (f == MF_SKIP)
        f = get_feature_def(grd[gx][gy]).minimap;
    ASSERT(f < MF_MAX);

    tiles.update_minimap(gx, gy, f);
}

void TilesFramework::update_minimap(int gx, int gy, map_feature f)
{
    if (!player_in_mappable_area())
        return;

    coord_def gc(gx, gy);

    if (you.pos() == gc)
    {
        f = MF_PLAYER;
    }
    else if (f == MF_MONS_HOSTILE && mgrd[gx][gy] != NON_MONSTER)
    {
        const int grid = mgrd[gx][gy];
        if (mons_friendly(&menv[grid]))
            f = MF_MONS_FRIENDLY;
        else if (mons_neutral(&menv[grid]))
            f = MF_MONS_NEUTRAL;
        else if (mons_class_flag(menv[grid].type, M_NO_EXP_GAIN))
            f = MF_MONS_NO_EXP;
    }
    else if (f == MF_FLOOR || f == MF_MAP_FLOOR)
    {
        if (is_exclude_root(gc))
            f = MF_EXCL_ROOT;
        else if (is_excluded(gc))
            f = MF_EXCL;
    }

    if (f == MF_WALL || f == MF_FLOOR)
    {
        if (is_terrain_known(gx, gy) && !is_terrain_seen(gx, gy)
            || is_envmap_detected_item(gx, gy)
            || is_envmap_detected_mons(gx, gy))
        {
            f = (f == MF_WALL) ? MF_MAP_WALL : MF_MAP_FLOOR;
        }
    }

    m_region_map->set(gx, gy, f);
}

void TilesFramework::clear_minimap()
{
    m_region_map->clear();
}

static void _fill_item_info(InventoryTile &desc, const item_def &item)
{
    desc.tile = tileidx_item(item);

    int type = item.base_type;
    if (type == OBJ_FOOD || type == OBJ_SCROLLS 
        || type == OBJ_POTIONS || type == OBJ_MISSILES)
    {
        // -1 specifies don't display anything
        desc.quantity = item.quantity == 1 ? -1 : item.quantity;
    }
    else if (type == OBJ_WANDS
             && ((item.flags & ISFLAG_KNOW_PLUSES)
                 || item.plus2 == ZAPCOUNT_EMPTY))
    {
        desc.quantity = item.plus;
    }
    else
    {
        desc.quantity = -1;
    }

    desc.flag = 0;
    if (item_cursed(item) && item_ident(item, ISFLAG_KNOW_CURSE))
        desc.flag |= TILEI_FLAG_CURSE;
    if (item_type_tried(item))
        desc.flag |= TILEI_FLAG_TRIED;
    if (item.pos.x != -1)
        desc.flag |= TILEI_FLAG_FLOOR;
}

void TilesFramework::update_inventory()
{
    std::vector<InventoryTile> inv;

    if (!Options.tile_show_items)
        return;

    // item.base_type <-> char conversion table
    const static char *obj_syms = ")([/%#?=!#+\\0}x";

    const unsigned int mx = m_region_self_inv->mx;
    const unsigned int my = m_region_self_inv->my;

    unsigned int max_pack_row = (ENDOFPACK-1) / mx + 1;
    unsigned int max_pack_items = max_pack_row * mx;

    unsigned int num_ground = 0;
    for (int i = igrd(you.pos()); i != NON_ITEM; i = mitm[i].link)
        num_ground++;

    // If the inventory is full, show at least one row of the ground.
    unsigned int min_ground = std::min(num_ground, mx);
    max_pack_items = std::min(max_pack_items, mx * my - min_ground);

    for (unsigned int c = 0; c < strlen(Options.tile_show_items); c++)
    {
        if (inv.size() >= max_pack_items)
            break;

        const char *find = strchr(obj_syms, Options.tile_show_items[c]);
        if (!find)
            continue;
        object_class_type type = (object_class_type)(find - obj_syms);

        // First, normal inventory
        for (int i = 0; i < ENDOFPACK; i++)
        {
            if (inv.size() >= max_pack_items)
                break;

            if (!is_valid_item(you.inv[i]) || you.inv[i].quantity == 0)
                continue;

            if (you.inv[i].base_type != type)
                continue;

            InventoryTile desc;
            _fill_item_info(desc, you.inv[i]);
            desc.idx = i;

            for (int eq = 0; eq < NUM_EQUIP; eq++)
            {
                if (you.equip[eq] == i)
                {
                    desc.flag |= TILEI_FLAG_EQUIP;
                    break;
                }
            }

            inv.push_back(desc);
        }
    }

    int remaining = mx*my- inv.size();
    int empty_on_this_row = mx - inv.size() % mx;

    // If we're not on the last row...
    if (inv.size() < mx * (my-1))
    {
        if ((int)num_ground > remaining - empty_on_this_row)
        {
            // Fill out part of this row.
            int fill = remaining - num_ground;
            for (int i = 0; i < fill; i++)
            {
                InventoryTile desc;
                inv.push_back(desc);
            }
        }
        else
        {
            // Fill out the rest of this row.
            while (inv.size() % mx != 0)
            {
                InventoryTile desc;
                inv.push_back(desc);
            }

            // Add extra rows, if needed.
            unsigned int ground_rows = 
                std::max(((int)num_ground-1) / (int)mx + 1, 1);

            while (inv.size() / mx + ground_rows < my)
            {
                for (unsigned int i = 0; i < mx; i++)
                {
                    InventoryTile desc;
                    inv.push_back(desc);
                }
            }
        }
    }

    // Then, as many ground items as we can fit.
    for (unsigned int c = 0; c < strlen(Options.tile_show_items); c++)
    {
        if (inv.size() >= mx * my)
            break;

        const char *find = strchr(obj_syms, Options.tile_show_items[c]);
        if (!find)
            continue;
        object_class_type type = (object_class_type)(find - obj_syms);

        for (int i = igrd(you.pos()); i != NON_ITEM; i = mitm[i].link)
        {
            if (inv.size() >= mx * my)
                break;

            if (mitm[i].base_type != type)
                continue;

            InventoryTile desc;
            _fill_item_info(desc, mitm[i]);
            desc.idx = i;

            inv.push_back(desc);
        }
    }

    while (inv.size() < mx * my)
    {
        InventoryTile desc;
        desc.flag = TILEI_FLAG_FLOOR;
        inv.push_back(desc);
    }

    m_region_self_inv->update(inv.size(), &inv[0]);
}

void TilesFramework::update_menu_inventory(unsigned int slot, 
                                           const item_def &item, 
                                           bool selected, char key)
{
    InventoryTile desc;
    _fill_item_info(desc, item);
    desc.key = key;
    desc.idx = (desc.flag & TILEI_FLAG_FLOOR) ? item.index() : 
                                                letter_to_index(key);
    if (selected)
        desc.flag |= TILEI_FLAG_SELECT;

    m_region_menu_inv->update_slot(slot, desc);
}

void TilesFramework::place_cursor(cursor_type type, const coord_def &gc)
{
    m_region_tile->place_cursor(type, gc);
}

void TilesFramework::clear_text_tags(text_tag_type type)
{
    m_region_tile->clear_text_tags(type);
}

void TilesFramework::add_text_tag(text_tag_type type, const std::string &tag, 
                                  const coord_def &gc)
{
    m_region_tile->add_text_tag(type, tag, gc);
}

bool TilesFramework::initialise_items()
{
    return (m_image.load_item_texture());
}

const coord_def &TilesFramework::get_cursor() const
{
    return (m_region_tile->get_cursor());
}