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