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>
23 #include "base_image.h"
24 #include "base_config.h"
26 /*---------------------------------------------------------------------------*/
28 void image_snap(char *filename, int w, int h)
32 SDL_Surface *buf = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 24,
33 RMASK, GMASK, BMASK, 0);
34 SDL_Surface *img = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 24,
35 RMASK, GMASK, BMASK, 0);
37 glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, buf->pixels);
39 for (i = 0; i < h; i++)
40 memcpy((GLubyte *) img->pixels + 3 * w * i,
41 (GLubyte *) buf->pixels + 3 * w * (h - i), 3 * w);
43 SDL_SaveBMP(img, filename);
49 void image_size(int *W, int *H, int w, int h)
54 while (*W < w) *W *= 2;
55 while (*H < h) *H *= 2;
58 /*---------------------------------------------------------------------------*/
61 * Create on OpenGL texture object using the given SDL surface and
62 * format, scaled using the current scale factor. When scaling,
63 * assume dimensions are used only for layout and lie about the size.
65 GLuint make_image_from_surf(int *w, int *h, SDL_Surface *s)
67 /*int t = config_get_d(CONFIG_TEXTURES);*/
68 int t = 0; /* TODO: Fint a way to revert CONFIG_TEXTURES */
72 glBindTexture(GL_TEXTURE_2D, o);
76 SDL_Surface *d = image_scale(s, t);
78 /* Load the scaled image. */
80 if (d->format->BitsPerPixel == 32)
81 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, d->w, d->h, 0,
82 GL_RGBA, GL_UNSIGNED_BYTE, d->pixels);
84 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, d->w, d->h, 0,
85 GL_RGB, GL_UNSIGNED_BYTE, d->pixels);
91 /* Load the unscaled image. */
93 if (s->format->BitsPerPixel == 32)
94 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, s->w, s->h, 0,
95 GL_RGBA, GL_UNSIGNED_BYTE, s->pixels);
97 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, s->w, s->h, 0,
98 GL_RGB, GL_UNSIGNED_BYTE, s->pixels);
101 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
102 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
104 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
105 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
113 /*---------------------------------------------------------------------------*/
116 * Load an image from the named file. If the image is not RGBA,
117 * convert it to RGBA. Return an OpenGL texture object.
119 GLuint make_image_from_file(int *W, int *H,
120 int *w, int *h, const char *name)
130 if ((src = IMG_Load(config_data(name))))
135 image_size(&w2, &h2, src->w, src->h);
140 /* Create a new destination surface. */
142 if ((dst = SDL_CreateRGBSurface(SDL_SWSURFACE, w2, h2, 32,
143 RMASK, GMASK, BMASK, AMASK)))
145 /* Copy source pixels to the center of the destination. */
147 rect.x = (Sint16) (w2 - src->w) / 2;
148 rect.y = (Sint16) (h2 - src->h) / 2;
150 SDL_SetAlpha(src, 0, 0);
151 SDL_BlitSurface(src, NULL, dst, &rect);
153 o = make_image_from_surf(W, H, dst);
155 SDL_FreeSurface(dst);
157 SDL_FreeSurface(src);
162 /*---------------------------------------------------------------------------*/
165 * Render the given string using the given font. Transfer the image
166 * to a surface of power-of-2 size large enough to fit the string.
167 * Return an OpenGL texture object.
169 GLuint make_image_from_font(int *W, int *H,
170 int *w, int *h, const char *text, TTF_Font *font, int k)
172 SDL_Color fg = { 0xFF, 0xFF, 0xFF, 0xFF };
180 /* Render the text. */
182 if (text && strlen(text) > 0)
184 if ((src = TTF_RenderUTF8_Blended(font, text, fg)))
189 image_size(&w2, &h2, src->w, src->h);
194 /* Create a new destination surface. */
196 if ((dst = SDL_CreateRGBSurface(SDL_SWSURFACE, w2, h2, 32,
197 RMASK, GMASK, BMASK, AMASK)))
199 /* Copy source pixels to the center of the destination. */
201 rect.x = (Sint16) (w2 - src->w) / 2;
202 rect.y = (Sint16) (h2 - src->h) / 2;
204 SDL_SetAlpha(src, 0, 0);
205 SDL_BlitSurface(src, NULL, dst, &rect);
209 o = make_image_from_surf(W, H, dst);
211 SDL_FreeSurface(dst);
213 SDL_FreeSurface(src);
232 /*---------------------------------------------------------------------------*/