cross compile build script
[fishladder.git] / vg / vg_tex.h
1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
2
3 #define VG_TEXTURE_NO_MIP 0x1
4 #define VG_TEXTURE_REPEAT 0x2
5 #define VG_TEXTURE_CLAMP 0x4
6 #define VG_TEXTURE_NEAREST 0x8
7
8 struct vg_tex2d
9 {
10 const char *path;
11 u32 flags;
12 GLuint name;
13 };
14
15 struct vg_sprite
16 {
17 v4f uv_xywh;
18 };
19
20 static void vg_tex2d_bind( vg_tex2d *tex, u32 id )
21 {
22 glActiveTexture( GL_TEXTURE0 + id );
23 glBindTexture( GL_TEXTURE_2D, tex->name );
24 }
25
26 static inline void vg_tex2d_mipmap(void)
27 {
28 glGenerateMipmap( GL_TEXTURE_2D );
29 }
30
31 static inline void vg_tex2d_linear(void)
32 {
33 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
34 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
35 }
36
37 static inline void vg_tex2d_nearest(void)
38 {
39 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
40 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
41 }
42
43 static inline void vg_tex2d_linear_mipmap(void)
44 {
45 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
46 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
47 }
48
49 static inline void vg_tex2d_repeat(void)
50 {
51 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
52 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
53 }
54
55 static inline void vg_tex2d_clamp(void)
56 {
57 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
58 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
59 }
60
61 static GLuint vg_tex2d_rgba( const char *path )
62 {
63 i64 length;
64 u8 *src_data = vg_asset_read_s( path, &length );
65
66 GLuint texture_name;
67 glGenTextures( 1, &texture_name );
68 glBindTexture( GL_TEXTURE_2D, texture_name );
69
70 if( src_data )
71 {
72 qoi_desc info;
73 u8 *tex_buffer = qoi_decode( src_data, length, &info, 4 );
74
75 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, info.width, info.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_buffer );
76
77 free( tex_buffer );
78 free( src_data );
79 }
80 else
81 {
82 u32 tex_err[4] =
83 {
84 0xffff00ff,
85 0xff000000,
86 0xff000000,
87 0xffff00ff
88 };
89
90 vg_error( "Loading texture failed (%s)\n", path );
91 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_err );
92 }
93
94 return texture_name;
95 }
96
97 static void vg_tex2d_init( vg_tex2d *textures[], int num )
98 {
99 for( int i = 0; i < num; i ++ )
100 {
101 vg_tex2d *tex = textures[i];
102 tex->name = vg_tex2d_rgba( tex->path );
103 if( !(tex->flags & VG_TEXTURE_NO_MIP) )
104 vg_tex2d_mipmap();
105
106 if( tex->flags & VG_TEXTURE_NEAREST )
107 {
108 if( tex->flags & VG_TEXTURE_NO_MIP )
109 vg_error( "Invalid texture settings\n" );
110 else
111 vg_tex2d_nearest();
112 }
113 else
114 {
115 if( tex->flags & VG_TEXTURE_NO_MIP )
116 vg_tex2d_linear();
117 else
118 vg_tex2d_linear_mipmap();
119 }
120
121 if( tex->flags & VG_TEXTURE_CLAMP )
122 vg_tex2d_clamp();
123 else
124 vg_tex2d_repeat();
125 }
126 }
127
128 static void vg_tex2d_free( vg_tex2d *textures[], int num )
129 {
130 for( int i = 0; i < num; i ++ )
131 {
132 glDeleteTextures( 1, &textures[i]->name );
133 }
134 }