* Make Sprite::glEnter2DMode() use this->g instead of some undefined global gr variable.
[matthijs/ABM2.git] / ABM2 / Amaltheia / Sprite.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005 by Dimitris Saougos & Filippos Papadopoulos   *
3  *   <psybases@gmail.com>                                                             *
4  *                                                                                                       *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU Library General Public License as       *
7  *   published by the Free Software Foundation; either version 2 of the    *
8  *   License, or (at your option) any later version.                                    *
9  *                                                                                                           *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU Library General Public     *
16  *   License along with this program; if not, write to the                 *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include "Sprite.h"
22 #include <GL/gl.h>
23
24
25 float z_order = 1.0;
26
27
28 void Sprite::glEnter2DMode()
29 {
30         glPushAttrib(GL_ALL_ATTRIB_BITS);
31         
32         glDisable(GL_LIGHTING);
33         glDisable(GL_CULL_FACE);
34         glEnable(GL_DEPTH_TEST);
35         
36         glEnable(GL_TEXTURE_2D);
37
38         
39         /* This allows alpha blending of 2D textures with the scene */
40         glEnable(GL_BLEND);
41         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
42 //    glBlendFunc(GL_SRC_ALPHA, GL_ONE);
43
44         glViewport(0, 0, this->g->getWidth(), this->g->getHeight());
45
46         glMatrixMode(GL_PROJECTION);
47         glPushMatrix();
48         glLoadIdentity();
49
50         glOrtho(0.0, (GLdouble)this->g->getWidth(), (GLdouble)this->g->getHeight(), 0.0, 0.0, -2.0);
51 //      gluOrtho2D(0.0, (GLdouble)this->g->getWidth(), (GLdouble)this->g->getHeight(), 0.0);
52         
53         glMatrixMode(GL_MODELVIEW);
54         glPushMatrix();
55         glLoadIdentity();
56 //      glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
57    return;
58 }
59
60
61 void Sprite::glLeave2DMode()
62 {
63         glMatrixMode(GL_MODELVIEW);
64         glPopMatrix();
65
66         glMatrixMode(GL_PROJECTION);
67         glPopMatrix();
68
69         glPopAttrib();
70    return;
71 }
72
73 Sprite::Sprite( char * filename, colour key, int tw, int th, Graphics *g)
74 {
75         this->g = g;
76         tileWidth=tw;
77         tileHeight=th;
78         
79         glAlphaFunc(GL_NOTEQUAL, 0);
80                 glEnable(GL_ALPHA_TEST);
81                 tex = new Texture(filename, key, g);
82
83         texW = tex->getWidth();
84         texH = tex->getHeight();
85         texWFactor = (tileWidth / (float) texW);
86         texHFactor = (tileHeight / (float) texH);
87
88         return ;
89 }
90
91
92 Sprite::~Sprite(void)
93 {
94         delete tex;
95         return ;
96 }
97
98
99 void Sprite::blit(int x, int y, float scalex, float scaley, int dx, int dy, colour col)
100 {
101         z_order-=0.000001;
102         glEnter2DMode();
103 //      glColor4ub((colour & 0x00FF0000) >> 16, (colour  & 0x0000FF00) >> 8,
104 //                         (colour & 0x000000FF), (colour & 0xFF000000) >> 24);
105         glColor4f(col.r, col.g, col.b, col.a);
106
107         glBindTexture(GL_TEXTURE_2D, tex->gl_Tex);
108         glPushMatrix();
109         glTranslatef(x,y, 0);
110
111         glScalef(scalex, scaley, 1);
112         glBegin(GL_QUADS);
113                 glTexCoord2f(dx*texWFactor, 1 - dy*texHFactor);
114                 glVertex3f(0,0, z_order);
115
116                 glTexCoord2f(dx*texWFactor,  1 - (dy + 1)*texHFactor);
117                 glVertex3f(0,  tileHeight, z_order);
118
119                 glTexCoord2f((dx + 1)*texWFactor,  1 - (dy + 1)*texHFactor);
120                 glVertex3f( tileWidth, tileHeight, z_order);
121
122                 glTexCoord2f((dx + 1)*texWFactor, 1 - dy*texHFactor);
123                 glVertex3f( tileWidth, 0, z_order);
124         glEnd();
125         glPopMatrix();
126
127         glLeave2DMode();
128
129
130    return ;
131 }