Changeset 959

Show
Ignore:
Timestamp:
06/09/08 21:59:55 (3 months ago)
Author:
takkaria
Message:

Add an efficient font caching system (never call AddFontResource?, etc. for fonts already in use), and fix some MingW32 warnings. (Kenneth Boyd)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main-win.c

    r958 r959  
    661661}; 
    662662 
     663#include "cmds.h" 
    663664#include "game-cmd.h" 
    664665 
    665 static game_command cmd = { CMD_NULL, 0 }; 
     666static game_command cmd = { CMD_NULL, 0, { NULL } }; 
    666667 
    667668#if 0 
     
    14111412} 
    14121413 
    1413  
    1414 /* 
     1414/** 
     1415 * Recover term with target font if possible 
     1416 */ 
     1417static errr term_find_font_in_use(const char *new_font_file) 
     1418
     1419        if (new_font_file) 
     1420        { 
     1421                int i; 
     1422 
     1423                /* Scan windows */ 
     1424                for (i = 0; i < MAX_TERM_DATA; i++) 
     1425                { 
     1426                        /* Check "screen" */ 
     1427                        if ((data[i].font_file) && 
     1428                        (streq(data[i].font_file, new_font_file))) 
     1429                                return i; 
     1430                } 
     1431        } 
     1432        return -1; 
     1433
     1434 
     1435/** 
     1436 * Coordinate forgetting all relevant font information. 
     1437 */ 
     1438static void term_forget_font(term_data *td) 
     1439
     1440        /* Forget old font */ 
     1441        if (td->font_file) 
     1442        { 
     1443                char* tmp_font_file = td->font_file;    /* back up font name */ 
     1444                td->font_file = NULL;                                           /* forget it */ 
     1445 
     1446                { 
     1447                errr term_using = term_find_font_in_use(tmp_font_file); 
     1448 
     1449                if (-1 == term_using) 
     1450                { 
     1451                        /* forget the old font if needed */ 
     1452                        if (td->font_id) DeleteObject(td->font_id); 
     1453 
     1454                        /* Remove unused font resources */ 
     1455                        term_remove_font(tmp_font_file); 
     1456                } 
     1457 
     1458                /* Free the old name */ 
     1459                string_free(tmp_font_file); 
     1460 
     1461                /* going out of scope, so commented out */ 
     1462                /* tmp_font_file = NULL */ 
     1463                } 
     1464        } 
     1465 
     1466        td->font_id = 0; 
     1467
     1468 
     1469/** 
    14151470 * Force the use of a new "font file" for a term_data 
    14161471 * 
     
    14231478static errr term_force_font(term_data *td, cptr path) 
    14241479{ 
    1425         int i; 
    1426  
    14271480        int wid, hgt; 
    1428  
     1481        errr term_using; 
     1482        HFONT tmp_font_id; 
    14291483        char *base; 
    1430  
    14311484        char buf[1024]; 
    1432  
    14331485 
    14341486        /* Check we have a path */ 
    14351487        if (!path) return (1); 
    14361488 
    1437  
    1438         /* Forget the old font (if needed) */ 
    1439         if (td->font_id) DeleteObject(td->font_id); 
    1440  
    1441         /* Forget old font */ 
    1442         if (td->font_file) 
    1443         { 
    1444                 bool used = FALSE; 
    1445  
    1446                 /* Scan windows */ 
    1447                 for (i = 0; i < MAX_TERM_DATA; i++) 
    1448                 { 
    1449                         /* Check "screen" */ 
    1450                         if ((td != &data[i]) && 
    1451                             (data[i].font_file) && 
    1452                             (streq(data[i].font_file, td->font_file))) 
    1453                         { 
    1454                                 used = TRUE; 
    1455                         } 
    1456                 } 
    1457  
    1458                 /* Remove unused font resources */ 
    1459                 if (!used) term_remove_font(td->font_file); 
    1460  
     1489        /* Local copy */ 
     1490        my_strcpy(buf, path, sizeof(buf)); 
     1491 
     1492        /* Analyze font path */ 
     1493        base = analyze_font(buf, &wid, &hgt); 
     1494 
     1495        /* Verify suffix */ 
     1496        if (!suffix(base, ".FON")) return 1; 
     1497 
     1498        /* Verify file */ 
     1499        if (!file_exists(buf)) return 1; 
     1500 
     1501        /* see if we're already using this font */ 
     1502        if (td->font_file && streq(td->font_file, base)) return 0;      /* same font as before */ 
     1503        term_using = term_find_font_in_use(base); 
     1504        if (-1 != term_using) 
     1505        {       /* some other term is using this font already */ 
    14611506                /* Free the old name */ 
    14621507                string_free(td->font_file); 
    14631508 
    1464                 /* Forget it */ 
    1465                 td->font_file = NULL; 
    1466         } 
    1467  
    1468  
    1469  
    1470         /* Local copy */ 
    1471         my_strcpy(buf, path, sizeof(buf)); 
    1472  
    1473         /* Analyze font path */ 
    1474         base = analyze_font(buf, &wid, &hgt); 
    1475  
    1476         /* Verify suffix */ 
    1477         if (!suffix(base, ".FON")) return (1); 
    1478  
    1479         /* Verify file */ 
    1480         if (!file_exists(buf)) return (1); 
     1509                /* Save new font name */ 
     1510                td->font_file = string_make(base); 
     1511 
     1512                /* recover font information from the other terminal */ 
     1513                td->font_id = data[term_using].font_id;         /* font id */ 
     1514                td->font_wid = data[term_using].font_wid;       /* size */ 
     1515                td->font_hgt = data[term_using].font_hgt; 
     1516 
     1517                return 0;       /* success */ 
     1518        }        
    14811519 
    14821520        /* Load the new font */ 
     
    14931531 
    14941532        /* Create the font (using the 'base' of the font file name!) */ 
    1495         td->font_id = CreateFont(hgt, wid, 0, 0, FW_DONTCARE, 0, 0, 0, 
     1533        tmp_font_id = CreateFont(hgt, wid, 0, 0, FW_DONTCARE, 0, 0, 0, 
    14961534                                 ANSI_CHARSET, OUT_DEFAULT_PRECIS, 
    14971535                                 CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, 
    14981536                                 FIXED_PITCH | FF_DONTCARE, base); 
     1537 
     1538        /* Restore the "suffix" */ 
     1539        base[strlen(base)] = '.';        
     1540 
     1541        if (!tmp_font_id) 
     1542        {       /* oops, CreateFont failed */ 
     1543                RemoveFontResource(buf); 
     1544                return 1;        
     1545        } 
     1546 
     1547        /* Forget font information */ 
     1548        term_forget_font(td); 
     1549 
     1550        td->font_id = tmp_font_id; 
     1551 
     1552        /* Save new font name */ 
     1553        td->font_file = string_make(base); 
    14991554 
    15001555        /* Hack -- Unknown size */ 
     
    44854540        { 
    44864541                /* Remove all fonts from the system, free resources */ 
    4487                 if (data[i].font_file) term_remove_font(data[i].font_file); 
    4488                 if (data[i].font_id) DeleteObject(data[i].font_id); 
     4542                term_forget_font(data+i); 
    44894543                if (data[i].font_want) string_free(data[i].font_want); 
    44904544