1 /* bzflag
2  * Copyright (c) 1993-2021 Tim Riker
3  *
4  * This package is free software;  you can redistribute it and/or
5  * modify it under the terms of the license found in the file
6  * named COPYING that should have accompanied this file.
7  *
8  * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
9  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
10  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11  */
12 
13 /* OpenGLTexture:
14  *  Encapsulates an OpenGL texture.
15  *
16  * Data must be in GL_RGBA format with type GL_UNSIGNED_BYTE.
17  * If, for all pixels, the RGB components are equal, then
18  * the data will be transformed to GL_LUMINANCE_ALPHA.  If,
19  * for all pixels, the alpha component is opaque, then the
20  * data will be transformed to GL_LUMINANCE or GL_RGB.
21  * hasAlpha() will return true iff the alpha component was
22  * kept.
23  *
24  * OpenGLTexture reference counts so copying or assigning a
25  * texture keeps the same display list until the object is
26  * changed.
27  *
28  * A default constructed texture is invalid.  The only way to
29  * make such an object valid is to assign a valid texture to it.
30  * Drawing an invalid texture has no effect.
31  *
32  * operator==() returns true iff the two objects refer to the
33  * same display list.  operator!=() returns true iff the two
34  * objects do not refer to the same display list.  Textures
35  * that don't refer to the same display list but have exactly
36  * the same texture map compare not-equal.
37  *
38  * <, <=, >, >= define an arbitrary ordering of textures.
39  */
40 
41 #ifndef BZF_OPENGL_TEXTURE_H
42 #define BZF_OPENGL_TEXTURE_H
43 
44 #include "common.h"
45 #include <string>
46 #include "bzfgl.h"
47 
48 class OpenGLTexture
49 {
50 public:
51     enum Filter
52     {
53         Off,
54         Nearest,
55         Linear,
56         NearestMipmapNearest,
57         LinearMipmapNearest,
58         NearestMipmapLinear,
59         LinearMipmapLinear,
60         Max = LinearMipmapLinear,
61         Default = Max
62     };
63 
64     OpenGLTexture(int width, int height,
65                   const GLvoid* pixels,
66                   Filter maxFilter = Linear,
67                   bool repeat = true);
68     ~OpenGLTexture();
69     bool        hasAlpha() const;
70 
71     void        execute();
72 
73     float       getAspectRatio() const;
74     int         getWidth() const;
75     int         getHeight() const;
76 
77     Filter      getFilter();
78     void        setFilter(Filter);
79 
80     void        freeContext();
81     void        initContext();
82 
83 
84     static int      getFilterCount();
85     static const char*  getFilterName(Filter id);
86     static const char** getFilterNames();
87 
88     static Filter   getMaxFilter();
89     static void     setMaxFilter(Filter);
90 
91 private:
92     OpenGLTexture(const OpenGLTexture&);
93     OpenGLTexture&  operator=(const OpenGLTexture&);
94 
95     bool        operator==(const OpenGLTexture&) const;
96     bool        operator!=(const OpenGLTexture&) const;
97     bool        operator<(const OpenGLTexture&) const;
98     void        getBestFormat();
99     void        bind();
100     void        setupImage(const GLubyte* pixels);
101 
new(size_t s)102     void* operator new(size_t s)
103     {
104         return ::operator new(s);
105     }
delete(void * p)106     void  operator delete(void *p)
107     {
108         ::operator delete(p);
109     }
110 
111     bool        alpha;
112     const int       width;
113     const int       height;
114     GLint       scaledWidth;
115     GLint       scaledHeight;
116     GLubyte*        image;
117     GLubyte*        imageMemory;
118     bool        repeat;
119     int         internalFormat;
120     GLuint      list;
121     Filter      filter;
122 
123     static Filter   maxFilter;
124 
125     static const int    filterCount;
126     static const char*  configFilterNames[];
127 
128     static const GLenum minifyFilter[];
129     static const GLenum magnifyFilter[];
130 
131     static void     static_freeContext(void *that);
132     static void     static_initContext(void *that);
133 
134     friend class TextureManager;
135 };
136 
137 //
138 // OpenGLTexture
139 //
140 
hasAlpha()141 inline bool     OpenGLTexture::hasAlpha() const
142 {
143     return alpha;
144 }
145 
getWidth()146 inline int      OpenGLTexture::getWidth() const
147 {
148     return width;
149 }
getHeight()150 inline int      OpenGLTexture::getHeight() const
151 {
152     return height;
153 }
154 
155 #endif // BZF_OPENGL_TEXTURE_H
156 
157 // Local Variables: ***
158 // mode: C++ ***
159 // tab-width: 4 ***
160 // c-basic-offset: 4 ***
161 // indent-tabs-mode: nil ***
162 // End: ***
163 // ex: shiftwidth=4 tabstop=4
164