File: theme.c

package info (click to toggle)
neverball 1.6.0%2Bgit20180603-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 152,384 kB
  • sloc: ansic: 27,402; makefile: 454; cpp: 208; xml: 177; sh: 161
file content (155 lines) | stat: -rw-r--r-- 3,672 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (C) 2014 Neverball authors
 *
 * NEVERBALL is  free software; you can redistribute  it and/or modify
 * it under the  terms of the GNU General  Public License as published
 * by the Free  Software Foundation; either version 2  of the License,
 * or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
 * MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
 * General Public License for more details.
 */

#include "theme.h"
#include "config.h"
#include "image.h"
#include "video.h"

/*---------------------------------------------------------------------------*/

static const char theme_images[THEME_IMAGES_MAX][PATHMAX] = {
    "back-plain.png",       /* off and inactive    */
    "back-plain-focus.png", /* off and   active    */
    "back-hilite.png",      /* on  and inactive    */
    "back-hilite-focus.png" /* on  and   active    */
};

static GLuint theme_image(const char *path)
{
    const int W = video.device_w;
    const int H = video.device_h;

    int W2, H2;

    int w, h, b;
    void *p;

    GLuint o = 0;

    /*
     * Disable mipmapping and do a manual downscale.  Heuristic for
     * downscaling the texture: assume target size to be roughly 1/16
     * of a full screen texture, smallest size being 32x32.
     */

    image_near2(&W2, &H2, W, H);

    W2 = MAX(W2 / 16, 32);
    H2 = MAX(H2 / 16, 32);

    if ((p = image_load(path, &w, &h, &b)))
    {
        void *q;

        /* Prefer a small scale factor. */

        int s = MAX(w, h) / MAX(W2, H2);

        if (s > 1 && (q = image_scale(p, w, h, b, &w, &h, s)))
        {
            free(p);
            p = q;
        }

        o = make_texture(p, w, h, b, 0);

        free(p);
        p = NULL;
    }

    return o;
}

static const char *theme_path(const char *name, const char *file)
{
    static char path[MAXSTR];

    if ((name && *name) && (file && *file))
    {
        SAFECPY(path, "gui/");
        SAFECAT(path, name);
        SAFECAT(path, "/");
        SAFECAT(path, file);
        return path;
    }
    return "";
}

int theme_load(struct theme *theme, const char *name)
{
    char buff[MAXSTR];
    fs_file fp;

    float s[4] = { 0.25f, 0.25f, 0.25f, 0.25f };

    int i;

    if (theme && name && *name)
    {
        memset(theme, 0, sizeof (*theme));

        /* Load description. */

        if ((fp = fs_open_read(theme_path(name, "theme.txt"))))
        {
            while ((fs_gets(buff, sizeof (buff), fp)))
            {
                strip_newline(buff);

                if (strncmp(buff, "slice ", 6) == 0)
                    sscanf(buff + 6, "%f %f %f %f", &s[0], &s[1], &s[2], &s[3]);
            }

            fs_close(fp);
        }
        else
        {
            log_printf("Failure to open \"%s\" theme file\n", name);
        }

        theme->s[0] =  0.0f;
        theme->s[1] =  s[0];
        theme->s[2] = (1.0f - s[1]);
        theme->s[3] =  1.0f;

        theme->t[0] =  1.0f;
        theme->t[1] = (1.0f - s[2]);
        theme->t[2] =  s[3];
        theme->t[3] =  0.0f;

        /* Load textures. */

        for (i = 0; i < ARRAYSIZE(theme_images); i++)
            theme->tex[i] = theme_image(theme_path(name, theme_images[i]));

        return 1;
    }
    return 0;
}

void theme_free(struct theme *theme)
{
    int i;

    if (theme)
    {
        glDeleteTextures(THEME_IMAGES_MAX, theme->tex);

        for (i = 0; i < THEME_IMAGES_MAX; i++)
            theme->tex[i] = 0;
    }
}

/*---------------------------------------------------------------------------*/