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