tennis.map: Texture tweak
[neverball] / share / image.c
1 /*
2  * Copyright (C) 2003 Robert Kooima
3  *
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.
8  *
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.
13  */
14
15 #include <SDL.h>
16 #include <SDL_ttf.h>
17 #include <string.h>
18 #include <math.h>
19 #include <png.h>
20 #include <stdlib.h>
21
22 #include "glext.h"
23 #include "image.h"
24 #include "base_image.h"
25 #include "config.h"
26
27 #include "fs.h"
28 #include "fs_png.h"
29
30 /*---------------------------------------------------------------------------*/
31
32 void image_snap(const char *filename)
33 {
34     fs_file     filep  = NULL;
35     png_structp writep = NULL;
36     png_infop   infop  = NULL;
37     png_bytep  *bytep  = NULL;
38
39     int w = config_get_d(CONFIG_WIDTH);
40     int h = config_get_d(CONFIG_HEIGHT);
41     int i;
42
43     unsigned char *p = NULL;
44
45     /* Initialize all PNG export data structures. */
46
47     if (!(filep = fs_open(filename, "w")))
48         return;
49     if (!(writep = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0)))
50         return;
51     if (!(infop = png_create_info_struct(writep)))
52         return;
53
54     /* Enable the default PNG error handler. */
55
56     if (setjmp(png_jmpbuf(writep)) == 0)
57     {
58         /* Initialize the PNG header. */
59
60         png_set_write_fn(writep, filep, fs_png_write, fs_png_flush);
61         png_set_IHDR(writep, infop, w, h, 8,
62                      PNG_COLOR_TYPE_RGB,
63                      PNG_INTERLACE_NONE,
64                      PNG_COMPRESSION_TYPE_DEFAULT,
65                      PNG_FILTER_TYPE_DEFAULT);
66
67         /* Allocate the pixel buffer and copy pixels there. */
68
69         if ((p = (unsigned char *) malloc(w * h * 3)))
70         {
71             glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, p);
72
73             /* Allocate and initialize the row pointers. */
74
75             if ((bytep = (png_bytep *) png_malloc(writep, h * sizeof (png_bytep))))
76             {
77                 for (i = 0; i < h; ++i)
78                     bytep[h - i - 1] = (png_bytep) (p + i * w * 3);
79
80                 png_set_rows (writep, infop, bytep);
81
82                 /* Write the PNG image file. */
83
84                 png_write_info(writep, infop);
85                 png_write_png (writep, infop, 0, NULL);
86
87                 free(bytep);
88             }
89             free(p);
90         }
91     }
92
93     /* Release all resources. */
94
95     png_destroy_write_struct(&writep, &infop);
96     fs_close(filep);
97 }
98
99 /*---------------------------------------------------------------------------*/
100
101 /*
102  * Create an OpenGL texture object using the given image buffer.
103  */
104 static GLuint make_texture(const void *p, int w, int h, int b)
105 {
106     static const GLenum format[] =
107         { 0, GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_RGB, GL_RGBA };
108
109     GLuint o = 0;
110
111     /* Scale the image as configured, or to fit the OpenGL limitations. */
112
113 #ifdef GL_TEXTURE_MAX_ANISOTROPY_EXT
114     int a = config_get_d(CONFIG_ANISO);
115 #endif
116 #ifdef GL_GENERATE_MIPMAP_SGIS
117     int m = config_get_d(CONFIG_MIPMAP);
118 #endif
119     int k = config_get_d(CONFIG_TEXTURES);
120     int W = w;
121     int H = h;
122
123     GLint max = gli.max_texture_size;
124
125     void *q = NULL;
126
127     while (w / k > (int) max || h / k > (int) max)
128         k *= 2;
129
130     if (k > 1)
131         q = image_scale(p, w, h, b, &W, &H, k);
132
133     /* Generate and configure a new OpenGL texture. */
134
135     glGenTextures(1, &o);
136     glBindTexture(GL_TEXTURE_2D, o);
137
138     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
139     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
140
141     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
142     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
143
144 #ifdef GL_GENERATE_MIPMAP_SGIS
145     if (m)
146     {
147         glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
148         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
149                         GL_LINEAR_MIPMAP_LINEAR);
150     }
151 #endif
152 #ifdef GL_TEXTURE_MAX_ANISOTROPY_EXT
153     if (a) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, a);
154 #endif
155
156     /* Copy the image to an OpenGL texture. */
157
158     glTexImage2D(GL_TEXTURE_2D, 0,
159                  format[b], W, H, 0,
160                  format[b], GL_UNSIGNED_BYTE, q ? q : p);
161
162     if (q) free(q);
163
164
165     return o;
166 }
167
168 /*
169  * Load an image from the named file.  Return an OpenGL texture object.
170  */
171 GLuint make_image_from_file(const char *filename)
172 {
173     void  *p;
174     int    w;
175     int    h;
176     int    b;
177     GLuint o = 0;
178
179     /* Load the image. */
180
181     if ((p = image_load(filename, &w, &h, &b)))
182     {
183         o = make_texture(p, w, h, b);
184         free(p);
185     }
186
187     return o;
188 }
189
190 /*---------------------------------------------------------------------------*/
191
192 /*
193  * Render the given  string using the given font.   Transfer the image
194  * to a  surface of  power-of-2 size large  enough to fit  the string.
195  * Return an OpenGL texture object.
196  */
197 GLuint make_image_from_font(int *W, int *H,
198                             int *w, int *h,
199                             const char *text, TTF_Font *font)
200 {
201     GLuint o = 0;
202
203     /* Render the text. */
204
205     if (font && text && strlen(text) > 0)
206     {
207         SDL_Color    col = { 0xFF, 0xFF, 0xFF, 0xFF };
208         SDL_Surface *orig;
209
210         if ((orig = TTF_RenderUTF8_Blended(font, text, col)))
211         {
212             void *p;
213             int  w2;
214             int  h2;
215             int   b = orig->format->BitsPerPixel / 8;
216
217             SDL_Surface *src;
218             SDL_PixelFormat fmt;
219
220             fmt = *orig->format;
221
222             fmt.Rmask = RMASK;
223             fmt.Gmask = GMASK;
224             fmt.Bmask = BMASK;
225             fmt.Amask = AMASK;
226
227             if ((src = SDL_ConvertSurface(orig, &fmt, orig->flags)) == NULL)
228             {
229                 fprintf(stderr, _("Failed to convert SDL_ttf surface: %s\n"),
230                         SDL_GetError());
231
232                 /* Pretend everything's just fine. */
233
234                 src = orig;
235             }
236             else
237                 SDL_FreeSurface(orig);
238
239             /* Pad the text to power-of-two. */
240
241             p = image_next2(src->pixels, src->w, src->h, b, &w2, &h2);
242
243             if (w) *w = src->w;
244             if (h) *h = src->h;
245             if (W) *W = w2;
246             if (H) *H = h2;
247
248             /* Saturate the color channels.  Modulate ONLY in alpha. */
249
250             image_white(p, w2, h2, b);
251
252             /* Create the OpenGL texture object. */
253
254             o = make_texture(p, w2, h2, b);
255
256             free(p);
257             SDL_FreeSurface(src);
258         }
259     }
260     else
261     {
262         /* Empty string. */
263
264         if (w) *w = 0;
265         if (h) *h = 0;
266         if (W) *W = 0;
267         if (H) *H = 0;
268     }
269
270     return o;
271 }
272
273 /*---------------------------------------------------------------------------*/
274
275 /*
276  * Load an image from the named file.  Return an SDL surface.
277  */
278 SDL_Surface *load_surface(const char *filename)
279 {
280     void  *p;
281     int    w;
282     int    h;
283     int    b;
284
285     SDL_Surface *srf = NULL;
286
287     if ((p = image_load(filename, &w, &h, &b)))
288     {
289         void *q;
290
291         if ((q = image_flip(p, w, h, b, 0, 1)))
292             srf = SDL_CreateRGBSurfaceFrom(q, w, h, b * 8, w * b,
293                                            RMASK, GMASK, BMASK, AMASK);
294         free(p);
295     }
296     return srf;
297 }
298
299 /*---------------------------------------------------------------------------*/