4ddba83ce76c890b64ccaddfa93c276faba178cd
[sdk] / ecere / src / gfx / newFonts / textureManager.ec
1 import "Display"
2
3 #include <stdio.h>
4 #include <math.h>
5
6 #include "gl123es.h"
7
8 namespace gfx;
9
10 #include "cc.h"
11
12 struct IMGFormat
13 {
14    int width;
15    int height;
16    IMGFormatType type;
17    int bytesPerPixel;
18    int bytesPerLine;
19 };
20
21 enum IMGFormatType { any, rgb24, bfr24, rgba32, bgra32, grayScale };
22
23 struct IMGImage
24 {
25    IMGFormat format;
26    void *data;
27 };
28
29 class TextureFlags : uint32 { bool invalid:1; }
30
31 class Texture : struct
32 {
33    GLuint glTex;
34    int width;
35    int height;
36    float widthinv;
37    float heightinv;
38    TextureFlags flags;
39    int swizzle;
40    uint32 orderMask;
41
42    flags = { invalid = true };
43
44 public:
45
46    property uint32 orderMask { set { orderMask = value; } }
47
48    static bool setData( IMGImage image, int internalformat, int mipmapmode, float anisotropy, int maxresolution )
49    {
50       int width, height;
51       int glformat;
52       int swizzle = 0;
53
54       if( image.format.bytesPerPixel == 1 )
55       {
56 #if defined(_GLES) || defined(_GLES2)
57          glformat = GL_ALPHA, swizzle = 1;
58 #else
59
60    #if ENABLE_GL_LEGACY
61          if(glCaps_legacyFormats)
62             glformat = GL_ALPHA, swizzle = 1;
63          else
64    #endif
65            glformat = GL_RED, swizzle = 2;
66 #endif
67       }
68      else if( image.format.bytesPerPixel == 2 )
69      {
70 #if defined(_GLES) || defined(_GLES2)
71        glformat = GL_LUMINANCE_ALPHA;
72 #else
73    #if ENABLE_GL_LEGACY
74          if(glCaps_legacyFormats)
75             glformat = GL_LUMINANCE_ALPHA;
76          else
77    #endif
78             glformat = GL_RG;
79 #endif
80      }
81      else if( image.format.bytesPerPixel == 3 )
82        glformat = GL_RGB;
83      else if( image.format.bytesPerPixel == 4 )
84        glformat = GL_RGBA;
85      else
86      {
87        fprintf( stderr, "ERROR: Bad image format.\n" );
88        return false;
89      }
90      if( internalformat == -1 )
91        internalformat = glformat;
92
93      width = image.format.width;
94      height = image.format.height;
95      if(!glCaps_nonPow2Textures)
96      {
97        if( !( ccIsPow2Int32( width ) ) || !( ccIsPow2Int32( height ) ) )
98        {
99          fprintf( stderr, "ERROR: Non-power of two texture used and GL_ARB_texture_non_power_of_two not supported.\n" );
100          return false;
101        }
102      }
103
104      glGenTextures( 1, &this.glTex );
105      glBindTexture( GL_TEXTURE_2D, this.glTex );
106
107      glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
108      glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
109      if(image.data)
110        memset(image.data, 0, image.format.width * image.format.height);
111      glTexImage2D( GL_TEXTURE_2D, 0, internalformat, image.format.width, image.format.height, 0, glformat, GL_UNSIGNED_BYTE, image.data );
112
113 #if GL_TEXTURE_MAX_ANISOTROPY_EXT
114      if(anisotropy > 1.0)
115        glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, anisotropy );
116 #endif
117
118      this.width = width;
119      this.height = height;
120      this.swizzle = swizzle;
121      widthinv = 1.0f / (float)width;
122      heightinv = 1.0f / (float)height;
123
124      return true;
125    }
126
127    bool build( IMGImage image, int mipmapmode, float anisotropy, int maxresolution )
128    {
129      if(!flags.invalid)
130        glDeleteTextures( 1, &glTex );
131      if( !setData( image, -1, mipmapmode, anisotropy, maxresolution ) )
132      {
133        fprintf( stderr, "ERROR: Failed to create texture.\n" );
134        return false;
135      }
136      flags.invalid = false;
137
138      return true;
139    }
140
141    bool load( const String path, int mipmapmode, float anisotropy, int maxresolution )
142    {
143      bool result = false;
144      IMGImage image;
145
146    #if TEXMG_ENABLE_PNG_SUPPORT
147      if( !( imgReadPngFile( &image, path, 0 ) ) )
148      {
149        fprintf( stderr, "ERROR: Loading texture %s failed.\n", path );
150        return false;
151      }
152    #else
153      fprintf( stderr, "ERROR: File support disabled, %s:%d\n", __FILE__, __LINE__ );
154      return false;
155    #endif
156
157      result = build( image, mipmapmode, anisotropy, maxresolution );
158      if( !result )
159        fprintf( stderr, "ERROR: Bad format for texture %s.\n", path );
160      delete image.data;
161
162      return result;
163    }
164
165    ~Texture()
166    {
167       if( !flags.invalid )
168          glDeleteTextures( 1, &glTex );
169    }
170 }