2 * Copyright (C) 2014 Neverball authors
4 * NEVERBALL is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published
6 * by the Free Software Foundation; either version 2 of the License,
7 * or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
20 /*---------------------------------------------------------------------------*/
22 static const char theme_images
[THEME_IMAGES_MAX
][PATHMAX
] = {
23 "back-plain.png", /* off and inactive */
24 "back-plain-focus.png", /* off and active */
25 "back-hilite.png", /* on and inactive */
26 "back-hilite-focus.png" /* on and active */
29 static GLuint
theme_image(const char *path
)
31 const int W
= video
.device_w
;
32 const int H
= video
.device_h
;
42 * Disable mipmapping and do a manual downscale. Heuristic for
43 * downscaling the texture: assume target size to be roughly 1/16
44 * of a full screen texture, smallest size being 32x32.
47 image_near2(&W2
, &H2
, W
, H
);
49 W2
= MAX(W2
/ 16, 32);
50 H2
= MAX(H2
/ 16, 32);
52 if ((p
= image_load(path
, &w
, &h
, &b
)))
56 /* Prefer a small scale factor. */
58 int s
= MAX(w
, h
) / MAX(W2
, H2
);
60 if (s
> 1 && (q
= image_scale(p
, w
, h
, b
, &w
, &h
, s
)))
66 o
= make_texture(p
, w
, h
, b
, 0);
75 static const char *theme_path(const char *name
, const char *file
)
77 static char path
[MAXSTR
];
79 if ((name
&& *name
) && (file
&& *file
))
81 SAFECPY(path
, "gui/");
90 int theme_load(struct theme
*theme
, const char *name
)
95 float s
[4] = { 0.25f
, 0.25f
, 0.25f
, 0.25f
};
99 if (theme
&& name
&& *name
)
101 memset(theme
, 0, sizeof (*theme
));
103 /* Load description. */
105 if ((fp
= fs_open(theme_path(name
, "theme.txt"), "r")))
107 while ((fs_gets(buff
, sizeof (buff
), fp
)))
111 if (strncmp(buff
, "slice ", 6) == 0)
112 sscanf(buff
+ 6, "%f %f %f %f", &s
[0], &s
[1], &s
[2], &s
[3]);
119 log_printf("Failure to open \"%s\" theme file\n", name
);
124 theme
->s
[2] = (1.0f
- s
[1]);
128 theme
->t
[1] = (1.0f
- s
[2]);
134 for (i
= 0; i
< ARRAYSIZE(theme_images
); i
++)
135 theme
->tex
[i
] = theme_image(theme_path(name
, theme_images
[i
]));
142 void theme_free(struct theme
*theme
)
148 glDeleteTextures(THEME_IMAGES_MAX
, theme
->tex
);
150 for (i
= 0; i
< THEME_IMAGES_MAX
; i
++)
155 /*---------------------------------------------------------------------------*/