2 * Copyright (C) 2003 Robert Kooima
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.
17 #include <SDL_image.h>
25 #include "base_image.h"
28 /*---------------------------------------------------------------------------*/
30 void image_snap(char *filename)
33 png_structp writep = NULL;
34 png_infop infop = NULL;
35 png_bytep *bytep = NULL;
37 int w = config_get_d(CONFIG_WIDTH);
38 int h = config_get_d(CONFIG_HEIGHT);
41 unsigned char *p = NULL;
43 /* Initialize all PNG export data structures. */
45 if (!(filep = fopen(filename, FMODE_WB)))
47 if (!(writep = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0)))
49 if (!(infop = png_create_info_struct(writep)))
52 /* Enable the default PNG error handler. */
54 if (setjmp(png_jmpbuf(writep)) == 0)
56 /* Initialize the PNG header. */
58 png_init_io (writep, filep);
59 png_set_IHDR(writep, infop, w, h, 8,
62 PNG_COMPRESSION_TYPE_DEFAULT,
63 PNG_FILTER_TYPE_DEFAULT);
65 /* Allocate the pixel buffer and copy pixels there. */
67 if ((p = (unsigned char *) malloc(w * h * 4)))
69 glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, p);
71 /* Allocate and initialize the row pointers. */
73 if ((bytep = png_malloc(writep, h * sizeof (png_bytep))))
75 for (i = 0; i < h; ++i)
76 bytep[h - i - 1] = (png_bytep) (p + i * w * 3);
78 png_set_rows (writep, infop, bytep);
80 /* Write the PNG image file. */
82 png_write_info(writep, infop);
83 png_write_png (writep, infop, 0, NULL);
91 /* Release all resources. */
93 png_destroy_write_struct(&writep, &infop);
97 void image_size(int *W, int *H, int w, int h)
102 while (*W < w) *W *= 2;
103 while (*H < h) *H *= 2;
106 /*---------------------------------------------------------------------------*/
109 * Create on OpenGL texture object using the given SDL surface and
110 * format, scaled using the current scale factor. When scaling,
111 * assume dimensions are used only for layout and lie about the size.
113 GLuint make_image_from_surf(int *w, int *h, SDL_Surface *s)
115 int t = config_get_d(CONFIG_TEXTURES);
118 glGenTextures(1, &o);
119 glBindTexture(GL_TEXTURE_2D, o);
123 SDL_Surface *d = image_scale(s, t);
125 /* Load the scaled image. */
127 if (d->format->BitsPerPixel == 32)
128 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, d->w, d->h, 0,
129 GL_RGBA, GL_UNSIGNED_BYTE, d->pixels);
131 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, d->w, d->h, 0,
132 GL_RGB, GL_UNSIGNED_BYTE, d->pixels);
138 /* Load the unscaled image. */
140 if (s->format->BitsPerPixel == 32)
141 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, s->w, s->h, 0,
142 GL_RGBA, GL_UNSIGNED_BYTE, s->pixels);
144 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, s->w, s->h, 0,
145 GL_RGB, GL_UNSIGNED_BYTE, s->pixels);
148 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
149 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
151 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
152 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
160 /*---------------------------------------------------------------------------*/
163 * Load an image from the named file. If the image is not RGBA,
164 * convert it to RGBA. Return an OpenGL texture object.
166 GLuint make_image_from_file(int *W, int *H,
167 int *w, int *h, const char *name)
177 if ((src = IMG_Load(config_data(name))))
182 image_size(&w2, &h2, src->w, src->h);
187 /* Create a new destination surface. */
189 if ((dst = SDL_CreateRGBSurface(SDL_SWSURFACE, w2, h2, 32,
190 RMASK, GMASK, BMASK, AMASK)))
192 /* Copy source pixels to the center of the destination. */
194 rect.x = (Sint16) (w2 - src->w) / 2;
195 rect.y = (Sint16) (h2 - src->h) / 2;
197 SDL_SetAlpha(src, 0, 0);
198 SDL_BlitSurface(src, NULL, dst, &rect);
200 o = make_image_from_surf(W, H, dst);
202 SDL_FreeSurface(dst);
204 SDL_FreeSurface(src);
209 /*---------------------------------------------------------------------------*/
212 * Render the given string using the given font. Transfer the image
213 * to a surface of power-of-2 size large enough to fit the string.
214 * Return an OpenGL texture object.
216 GLuint make_image_from_font(int *W, int *H,
217 int *w, int *h, const char *text, TTF_Font *font, int k)
219 SDL_Color fg = { 0xFF, 0xFF, 0xFF, 0xFF };
227 /* Render the text. */
229 if (text && strlen(text) > 0)
231 if ((src = TTF_RenderUTF8_Blended(font, text, fg)))
236 image_size(&w2, &h2, src->w, src->h);
241 /* Create a new destination surface. */
243 if ((dst = SDL_CreateRGBSurface(SDL_SWSURFACE, w2, h2, 32,
244 RMASK, GMASK, BMASK, AMASK)))
246 /* Copy source pixels to the center of the destination. */
248 rect.x = (Sint16) (w2 - src->w) / 2;
249 rect.y = (Sint16) (h2 - src->h) / 2;
251 SDL_SetAlpha(src, 0, 0);
252 SDL_BlitSurface(src, NULL, dst, &rect);
256 o = make_image_from_surf(W, H, dst);
258 SDL_FreeSurface(dst);
260 SDL_FreeSurface(src);
279 /*---------------------------------------------------------------------------*/