~ubuntu-branches/ubuntu/edgy/pouetchess/edgy-updates

« back to all changes in this revision

Viewing changes to src/texture.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Lionel Le Folgoc (mr_pouit)
  • Date: 2006-07-15 15:45:57 UTC
  • Revision ID: james.westby@ubuntu.com-20060715154557-3paxq02hx4od0wm4
Tags: upstream-0.2.0
ImportĀ upstreamĀ versionĀ 0.2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2006 by Frederic MARTIN                                 *
 
3
 *   martin-frederic@users.sourceforge.net                                 *
 
4
 *                                                                         *
 
5
 *   This program is free software; you can redistribute it and/or modify  *
 
6
 *   it under the terms of the GNU General Public License as published by  *
 
7
 *   the Free Software Foundation; either version 2 of the License, or     *
 
8
 *   (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 General Public License     *
 
16
 *   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 "texture.h"
 
22
#include <SDL/SDL_image.h>
 
23
#include <string.h>
 
24
 
 
25
 
 
26
CBasicTexture::CBasicTexture()
 
27
{
 
28
 
 
29
}
 
30
 
 
31
CBasicTexture::~CBasicTexture()
 
32
{
 
33
        unload();
 
34
}
 
35
 
 
36
 
 
37
 
 
38
// Formats : BMP, PNM, XPM, LBM, PCX, GIF, JPEG, PNG, TGA.
 
39
bool CBasicTexture::load(const char *file, bool mipmap )        
 
40
{
 
41
        SDL_Surface *surface;
 
42
        int pixel;
 
43
 
 
44
        surface = IMG_Load(file);
 
45
        if (surface == NULL)
 
46
                return false;
 
47
                
 
48
        pixel=surface->format->BitsPerPixel;
 
49
        width=surface->w;
 
50
        height=surface->h;
 
51
        mipmapping=mipmap;
 
52
 
 
53
        glGenTextures(1, &id);
 
54
        glBindTexture(GL_TEXTURE_2D, id);
 
55
 
 
56
 
 
57
        if (mipmap)
 
58
        {
 
59
                if ( pixel == 24)
 
60
                        gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width, height, GL_RGB, GL_UNSIGNED_BYTE, surface->pixels);
 
61
                else
 
62
                        gluBuild2DMipmaps(GL_TEXTURE_2D,4,width, height, GL_RGBA,GL_UNSIGNED_BYTE,surface->pixels);
 
63
 
 
64
                SetTexParameter(GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
 
65
                SetTexParameter(GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);
 
66
        }
 
67
        else
 
68
        {
 
69
                if ( pixel == 24)
 
70
                        glTexImage2D(GL_TEXTURE_2D,0,3,width, height,0, GL_RGB,GL_UNSIGNED_BYTE,surface->pixels);
 
71
                else
 
72
                        glTexImage2D(GL_TEXTURE_2D,0,4,width, height,0, GL_RGBA,GL_UNSIGNED_BYTE,surface->pixels);
 
73
 
 
74
                SetTexParameter(GL_TEXTURE_MIN_FILTER,GL_LINEAR);
 
75
                SetTexParameter(GL_TEXTURE_MAG_FILTER,GL_LINEAR);
 
76
        }
 
77
 
 
78
        SDL_FreeSurface(surface);
 
79
        surface = NULL;
 
80
        
 
81
        return true;
 
82
}
 
83
 
 
84
 
 
85
 
 
86
void CBasicTexture::bind()
 
87
{
 
88
        glBindTexture ( GL_TEXTURE_2D, id);
 
89
}
 
90
 
 
91
void CBasicTexture::SetTexParameter (GLenum type, GLint mode)
 
92
{
 
93
        bind();
 
94
 
 
95
        glTexParameteri(GL_TEXTURE_2D,
 
96
                                        type,
 
97
                                        mode);
 
98
}
 
99
 
 
100
 
 
101
void CBasicTexture::Set_Blending (GLint mode, GLfloat *env_color)
 
102
{
 
103
        bind();
 
104
        if (mode == GL_BLEND)
 
105
        {
 
106
                glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, (GLfloat) mode);
 
107
                glTexEnvfv(GL_TEXTURE_ENV,GL_TEXTURE_ENV_COLOR,(GLfloat*)env_color);
 
108
        }
 
109
        else
 
110
        {
 
111
                glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE, (GLfloat) mode);
 
112
        }
 
113
}
 
114
 
 
115
void CBasicTexture::Set_Blending (GLint mode)
 
116
{
 
117
        bind();
 
118
        glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE, (GLfloat) mode);
 
119
}
 
120
 
 
121
void CBasicTexture::unload()
 
122
{
 
123
        glDeleteTextures(1,&id);
 
124
}
 
125