| 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 25 * POSSIBILITY OF SUCH DAMAGE. |
25 * POSSIBILITY OF SUCH DAMAGE. |
| 26 */ |
26 */ |
| 27 |
27 |
| |
28 #include "ascension/context.h" |
| 28 #include "ascension/shader.h" |
29 #include "ascension/shader.h" |
| 29 #include "ascension/error.h" |
30 #include "ascension/error.h" |
| |
31 #include "ascension/filesystem.h" |
| 30 |
32 |
| 31 #include <GL/glew.h> |
33 #include <GL/glew.h> |
| 32 #include <string.h> |
34 #include <string.h> |
| 33 #include <cx/buffer.h> |
35 #include <cx/buffer.h> |
| 34 #include <cx/streams.h> |
36 #include <cx/streams.h> |
| 119 } |
121 } |
| 120 |
122 |
| 121 int asc_shader_sprite_init(AscShaderSprite *sprite) { |
123 int asc_shader_sprite_init(AscShaderSprite *sprite) { |
| 122 AscShaderCodes codes; |
124 AscShaderCodes codes; |
| 123 if (asc_shader_load_code_files((AscShaderCodeFiles){ |
125 if (asc_shader_load_code_files((AscShaderCodeFiles){ |
| 124 .vtx = "./shader/sprite_vtx.glsl", |
126 .vtx = "sprite_vtx.glsl", |
| 125 .frag = "./shader/sprite_frag.glsl" |
127 .frag = "sprite_frag.glsl" |
| 126 }, &codes)) { |
128 }, &codes)) { |
| 127 asc_error("Loading sprite shader failed."); |
129 asc_error("Loading sprite shader failed."); |
| 128 return 1; |
130 return 1; |
| 129 } |
131 } |
| 130 sprite->program = asc_shader_program_create(codes); |
132 sprite->program = asc_shader_program_create(codes); |
| 157 static int asc_shader_load_code_file(const char *filename, char **code) { |
159 static int asc_shader_load_code_file(const char *filename, char **code) { |
| 158 if (filename == NULL) { |
160 if (filename == NULL) { |
| 159 *code = NULL; |
161 *code = NULL; |
| 160 return 0; |
162 return 0; |
| 161 } |
163 } |
| 162 FILE *f = fopen(filename, "r"); |
164 cxmutstr fpath = asc_filesystem_combine_paths(cx_strcast(asc_context.shader_path), cx_str(filename)); |
| |
165 FILE *f = fopen(fpath.ptr, "r"); |
| |
166 cx_strfree(&fpath); |
| 163 if (f == NULL) return -1; |
167 if (f == NULL) return -1; |
| 164 CxBuffer buffer; |
168 CxBuffer buffer; |
| 165 cxBufferInit(&buffer, NULL, 1024, NULL, CX_BUFFER_AUTO_EXTEND); |
169 cxBufferInit(&buffer, NULL, 1024, NULL, CX_BUFFER_AUTO_EXTEND); |
| 166 cx_stream_copy(f, &buffer, (cx_read_func) fread, cxBufferWriteFunc); |
170 cx_stream_copy(f, &buffer, (cx_read_func) fread, cxBufferWriteFunc); |
| 167 cxBufferPut(&buffer, '\0'); |
171 cxBufferPut(&buffer, '\0'); |