Import from seb-0702.tar.gz
[neverball-archive.git] / src / image.c
blobe15c240281e2caecfb4a280e6cc673252ad90d5d
1 /*
2 * Copyright (C) 2003 Robert Kooima
4 * SUPER EMPTY BALL is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * 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.
15 #include <SDL/SDL_image.h>
17 #include "gl.h"
18 #include "image.h"
20 /*---------------------------------------------------------------------------*/
22 static const GLenum format[5] = {
24 GL_LUMINANCE,
25 GL_LUMINANCE_ALPHA,
26 GL_RGB,
27 GL_RGBA
30 int image_load(struct image *image, const char *filename)
32 if ((image->s = IMG_Load(filename)))
34 void *p = image->s->pixels;
35 int w = image->s->w;
36 int h = image->s->h;
37 int b = image->s->format->BytesPerPixel;
38 int f = format[b];
40 glGenTextures(1, &image->o);
41 glBindTexture(GL_TEXTURE_2D, image->o);
43 glTexImage2D(GL_TEXTURE_2D, 0, b, w, h, 0, f, GL_UNSIGNED_BYTE, p);
45 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
46 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
48 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
49 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
51 return 1;
53 return 0;
56 int image_test(struct image *image)
58 return (image->s && glIsTexture(image->o)) ? 1 : 0;
61 void image_free(struct image *image)
63 if (image->o) glDeleteTextures(1, &image->o);
64 if (image->s) SDL_FreeSurface(image->s);
66 image->s = NULL;
67 image->o = 0;
70 /*---------------------------------------------------------------------------*/
72 void image_bind(struct image *image)
74 glBindTexture(GL_TEXTURE_2D, image->o);
77 void image_rect(struct image *image,
78 double x0, double y0,
79 double x1, double y1, double a)
81 glMatrixMode(GL_PROJECTION);
83 glPushMatrix();
84 glLoadIdentity();
85 glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
87 glMatrixMode(GL_MODELVIEW);
89 glBindTexture(GL_TEXTURE_2D, image->o);
91 glPushAttrib(GL_ENABLE_BIT);
93 glDisable(GL_LIGHTING);
94 glDisable(GL_DEPTH_TEST);
95 glEnable(GL_COLOR_MATERIAL);
97 glBegin(GL_QUADS);
99 glColor4d(1.0, 1.0, 1.0, a);
101 glTexCoord2d(0.0, 1.0); glVertex2d(x0, y0);
102 glTexCoord2d(1.0, 1.0); glVertex2d(x1, y0);
103 glTexCoord2d(1.0, 0.0); glVertex2d(x1, y1);
104 glTexCoord2d(0.0, 0.0); glVertex2d(x0, y1);
106 glEnd();
108 glPopAttrib();
110 glMatrixMode(GL_PROJECTION);
112 glPopMatrix();
114 glMatrixMode(GL_MODELVIEW);
117 /*---------------------------------------------------------------------------*/