source: trunk/src/forge.cpp @ 111

Last change on this file since 111 was 111, checked in by sam, 9 years ago

The frame counter is now an asset object.

  • Property svn:keywords set to Id
File size: 1.8 KB
Line 
1//
2// Deus Hax (working title)
3// Copyright (c) 2010 Sam Hocevar <sam@hocevar.net>
4//
5
6#if defined HAVE_CONFIG_H
7#   include "config.h"
8#endif
9
10#include <cstring>
11#include <cstdio>
12#include <cstdlib>
13
14#include "forge.h"
15
16#if defined WIN32
17#   define strcasecmp _stricmp
18#endif
19
20/*
21 * Forge implementation class
22 */
23
24static class ForgeData
25{
26    friend class Forge;
27
28public:
29    ForgeData() :
30        fonts(0),
31        nfonts(0)
32    {
33        /* Nothing to do */
34    }
35
36    ~ForgeData()
37    {
38        if (nfonts)
39            fprintf(stderr, "ERROR: still %i fonts in forge\n", nfonts);
40        free(fonts);
41    }
42
43private:
44    Font **fonts;
45    int nfonts;
46}
47forgedata;
48
49static ForgeData * const data = &forgedata;
50
51/*
52 * Public Forge class
53 */
54
55Font *Forge::GetFont(char const *path)
56{
57    int id, empty = -1;
58
59    /* If the font is already registered, remember its ID. Look for an
60     * empty slot at the same time. */
61    for (id = 0; id < data->nfonts; id++)
62    {
63        Font *t = data->fonts[id];
64        if (!t)
65            empty = id;
66        else if (!strcasecmp(path, t->GetName()))
67            break;
68    }
69
70    /* If this is a new font, create a new one. */
71    if (id == data->nfonts)
72    {
73        if (empty == -1)
74        {
75            empty = data->nfonts++;
76            data->fonts = (Font **)realloc(data->fonts,
77                                           data->nfonts * sizeof(Font *));
78        }
79
80        data->fonts[empty] = new Font(path);
81        id = empty;
82    }
83
84    data->fonts[id]->Ref();
85    return data->fonts[id];
86}
87
88void Forge::ReleaseFont(Font *font)
89{
90    if (font->Unref() == 0)
91        for (int id = 0; id < data->nfonts; id++)
92            if (font == data->fonts[id])
93            {
94                data->fonts[id] = NULL;
95                break;
96            }
97}
98
Note: See TracBrowser for help on using the repository browser.