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, int w, int h)
33 png_structp writep = NULL;
34 png_infop infop = NULL;
35 png_bytep *bytep = NULL;
39 unsigned char *p = NULL;
41 /* Initialize all PNG export data structures. */
43 if (!(filep = fopen(filename, FMODE_WB)))
45 if (!(writep = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0)))
47 if (!(infop = png_create_info_struct(writep)))
50 /* Enable the default PNG error handler. */
52 if (setjmp(png_jmpbuf(writep)) == 0)
54 /* Initialize the PNG header. */
56 png_init_io (writep, filep);
57 png_set_IHDR(writep, infop, w, h, 8,
60 PNG_COMPRESSION_TYPE_DEFAULT,
61 PNG_FILTER_TYPE_DEFAULT);
63 /* Allocate the pixel buffer and copy pixels there. */
65 if ((p = (unsigned char *) malloc(w * h * 4)))
67 glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, p);
69 /* Allocate and initialize the row pointers. */
71 if ((bytep = png_malloc(writep, h * sizeof (png_bytep))))
73 for (i = 0; i < h; ++i)
74 bytep[h - i - 1] = (png_bytep) (p + i * w * 3);
76 png_set_rows (writep, infop, bytep);
78 /* Write the PNG image file. */
80 png_write_info(writep, infop);
81 png_write_png (writep, infop, 0, NULL);
89 /* Release all resources. */
91 png_destroy_write_struct(&writep, &infop);
95 void image_size(int *W, int *H, int w, int h)
100 while (*W < w) *W *= 2;
101 while (*H < h) *H *= 2;
104 /*---------------------------------------------------------------------------*/
107 * Create on OpenGL texture object using the given SDL surface and
108 * format, scaled using the current scale factor. When scaling,
109 * assume dimensions are used only for layout and lie about the size.
111 GLuint make_image_from_surf(int *w, int *h, SDL_Surface *s)
113 int t = config_get_d(CONFIG_TEXTURES);
116 glGenTextures(1, &o);
117 glBindTexture(GL_TEXTURE_2D, o);
121 SDL_Surface *d = image_scale(s, t);
123 /* Load the scaled image. */
125 if (d->format->BitsPerPixel == 32)
126 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, d->w, d->h, 0,
127 GL_RGBA, GL_UNSIGNED_BYTE, d->pixels);
129 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, d->w, d->h, 0,
130 GL_RGB, GL_UNSIGNED_BYTE, d->pixels);
136 /* Load the unscaled image. */
138 if (s->format->BitsPerPixel == 32)
139 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, s->w, s->h, 0,
140 GL_RGBA, GL_UNSIGNED_BYTE, s->pixels);
142 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, s->w, s->h, 0,
143 GL_RGB, GL_UNSIGNED_BYTE, s->pixels);
146 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
147 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
149 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
150 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
158 /*---------------------------------------------------------------------------*/
161 * Load an image from the named file. If the image is not RGBA,
162 * convert it to RGBA. Return an OpenGL texture object.
164 GLuint make_image_from_file(int *W, int *H,
165 int *w, int *h, const char *name)
175 if ((src = IMG_Load(config_data(name))))
180 image_size(&w2, &h2, src->w, src->h);
185 /* Create a new destination surface. */
187 if ((dst = SDL_CreateRGBSurface(SDL_SWSURFACE, w2, h2, 32,
188 RMASK, GMASK, BMASK, AMASK)))
190 /* Copy source pixels to the center of the destination. */
192 rect.x = (Sint16) (w2 - src->w) / 2;
193 rect.y = (Sint16) (h2 - src->h) / 2;
195 SDL_SetAlpha(src, 0, 0);
196 SDL_BlitSurface(src, NULL, dst, &rect);
198 o = make_image_from_surf(W, H, dst);
200 SDL_FreeSurface(dst);
202 SDL_FreeSurface(src);
207 /*---------------------------------------------------------------------------*/
210 * Render the given string using the given font. Transfer the image
211 * to a surface of power-of-2 size large enough to fit the string.
212 * Return an OpenGL texture object.
214 GLuint make_image_from_font(int *W, int *H,
215 int *w, int *h, const char *text, TTF_Font *font, int k)
217 SDL_Color fg = { 0xFF, 0xFF, 0xFF, 0xFF };
225 /* Render the text. */
227 if (text && strlen(text) > 0)
229 if ((src = TTF_RenderUTF8_Blended(font, text, fg)))
234 image_size(&w2, &h2, src->w, src->h);
239 /* Create a new destination surface. */
241 if ((dst = SDL_CreateRGBSurface(SDL_SWSURFACE, w2, h2, 32,
242 RMASK, GMASK, BMASK, AMASK)))
244 /* Copy source pixels to the center of the destination. */
246 rect.x = (Sint16) (w2 - src->w) / 2;
247 rect.y = (Sint16) (h2 - src->h) / 2;
249 SDL_SetAlpha(src, 0, 0);
250 SDL_BlitSurface(src, NULL, dst, &rect);
254 o = make_image_from_surf(W, H, dst);
256 SDL_FreeSurface(dst);
258 SDL_FreeSurface(src);
277 /*---------------------------------------------------------------------------*/