* Add "less" target to view make output through less.
[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 Graphics *gr;
26 float z_order = 1.0;
27
28
29 void Sprite::glEnter2DMode()
30 {
31         glPushAttrib(GL_ALL_ATTRIB_BITS);
32         
33         glDisable(GL_LIGHTING);
34         glDisable(GL_CULL_FACE);
35         glEnable(GL_DEPTH_TEST);
36         
37         glEnable(GL_TEXTURE_2D);
38
39         
40         /* This allows alpha blending of 2D textures with the scene */
41         glEnable(GL_BLEND);
42         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
43 //    glBlendFunc(GL_SRC_ALPHA, GL_ONE);
44
45         glViewport(0, 0, gr->getWidth(), gr->getHeight());
46
47         glMatrixMode(GL_PROJECTION);
48         glPushMatrix();
49         glLoadIdentity();
50
51         glOrtho(0.0, (GLdouble)gr->getWidth(), (GLdouble)gr->getHeight(), 0.0, 0.0, -2.0);
52 //      gluOrtho2D(0.0, (GLdouble)gr->getWidth(), (GLdouble)gr->getHeight(), 0.0);
53         
54         glMatrixMode(GL_MODELVIEW);
55         glPushMatrix();
56         glLoadIdentity();
57 //      glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
58    return;
59 }
60
61
62 void Sprite::glLeave2DMode()
63 {
64         glMatrixMode(GL_MODELVIEW);
65         glPopMatrix();
66
67         glMatrixMode(GL_PROJECTION);
68         glPopMatrix();
69
70         glPopAttrib();
71    return;
72 }
73
74 Sprite::Sprite( char * filename, colour key, int tw, int th, Graphics *g)
75 {
76         this->g = g;
77         tileWidth=tw;
78         tileHeight=th;
79         
80         glAlphaFunc(GL_NOTEQUAL, 0);
81                 glEnable(GL_ALPHA_TEST);
82                 tex = new Texture(filename, key, g);
83
84         texW = tex->getWidth();
85         texH = tex->getHeight();
86         texWFactor = (tileWidth / (float) texW);
87         texHFactor = (tileHeight / (float) texH);
88
89         return ;
90 }
91
92
93 Sprite::~Sprite(void)
94 {
95         delete tex;
96         return ;
97 }
98
99
100 void Sprite::blit(int x, int y, float scalex, float scaley, int dx, int dy, colour col)
101 {
102         z_order-=0.000001;
103         glEnter2DMode();
104 //      glColor4ub((colour & 0x00FF0000) >> 16, (colour  & 0x0000FF00) >> 8,
105 //                         (colour & 0x000000FF), (colour & 0xFF000000) >> 24);
106         glColor4f(col.r, col.g, col.b, col.a);
107
108         glBindTexture(GL_TEXTURE_2D, tex->gl_Tex);
109         glPushMatrix();
110         glTranslatef(x,y, 0);
111
112         glScalef(scalex, scaley, 1);
113         glBegin(GL_QUADS);
114                 glTexCoord2f(dx*texWFactor, 1 - dy*texHFactor);
115                 glVertex3f(0,0, z_order);
116
117                 glTexCoord2f(dx*texWFactor,  1 - (dy + 1)*texHFactor);
118                 glVertex3f(0,  tileHeight, z_order);
119
120                 glTexCoord2f((dx + 1)*texWFactor,  1 - (dy + 1)*texHFactor);
121                 glVertex3f( tileWidth, tileHeight, z_order);
122
123                 glTexCoord2f((dx + 1)*texWFactor, 1 - dy*texHFactor);
124                 glVertex3f( tileWidth, 0, z_order);
125         glEnd();
126         glPopMatrix();
127
128         glLeave2DMode();
129
130
131    return ;
132 }