Embeddable shaders

This commit is contained in:
Debucquoy Anthony 2024-06-25 22:20:35 +02:00
parent 6025a5a614
commit 39dbf40cd3
Signed by: tonitch
GPG Key ID: A78D6421F083D42E
4 changed files with 35 additions and 17 deletions

2
.gitignore vendored
View File

@ -2,3 +2,5 @@
compile_commands.json
zoomer
zoomer.o
fshader.h
vshader.h

View File

@ -1,10 +1,10 @@
.PHONY: all clean run
VERSION = 0.0.1
.PHONY: all clean run bear build
VERSION = 0.0.2
PREFIX=/usr/local
LIBS = sdl2 glew
CMACRO = -DVERSION=\"$(VERSION)\"
CMACRO = -DVERSION=\"$(VERSION)\" $(if $(EMBED_SHADER),-DEMBED_SHADER,)
CC = gcc
CFLAGS = -g -Wall -Wextra -pedantic $(shell pkg-config $(LIBS) --cflags) $(CMACRO)
@ -12,21 +12,28 @@ LDFLAGS = $(shell pkg-config $(LIBS) --libs) -lm
all: zoomer
zoomer: zoomer.o
zoomer: $(if $(EMBED_SHADER),fshader.h vshader.h,) zoomer.o
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<
%.h: %.glsl
xxd -i $< $@
clean:
rm -f fshader.h vshader.h
rm -f *.o
rm -f zoomer
install: zoomer
install -Dm755 $< $(DESTDIR)$(PREFIX)/bin/$<
install: build
install -Dm755 zoomer $(DESTDIR)$(PREFIX)/bin/zoomer
bear: clean
bear -- make
build:
EMBED_SHADER=y $(MAKE) zoomer
run: zoomer
./$<

View File

@ -9,16 +9,16 @@ const char* pullArg(int *argc, char** argv[]){
return *argv[0]++;
}
const char *readFile(const char* filename){
const char* readFile(const char* filename, long *size){
FILE *fd = fopen(filename, "r");
fseek(fd, 0, SEEK_END);
long size = ftell(fd);
*size = ftell(fd);
rewind(fd);
char* ret = malloc(size+1);
fread(ret, sizeof(char), size, fd);
ret[size] = 0;
char* ret = malloc(*size+1);
fread(ret, sizeof(char), *size, fd);
ret[*size] = 0;
return ret;
}

View File

@ -68,11 +68,20 @@ int main(int argc, char *argv[])
GLuint vshader = glCreateShader(GL_VERTEX_SHADER),
fshader = glCreateShader(GL_FRAGMENT_SHADER);
const char *vshadersrc = readFile("vshader.glsl"),
*fshadersrc = readFile("fshader.glsl");
glShaderSource(vshader, 1, &vshadersrc, NULL);
glShaderSource(fshader, 1, &fshadersrc, NULL);
#ifndef EMBED_SHADER
long vshader_glsl_len, fshader_glsl_len;
const char *vshadersrc = readFile("vshader.glsl", &vshader_glsl_len),
*fshadersrc = readFile("fshader.glsl", &fshader_glsl_len);
#else
#include "vshader.h"
#include "fshader.h"
const char *vshadersrc = vshader_glsl,
*fshadersrc = fshader_glsl;
#endif
glShaderSource(vshader, 1, &vshadersrc, (int *)&vshader_glsl_len);
glShaderSource(fshader, 1, &fshadersrc, (int *)&fshader_glsl_len);
glCompileShader(vshader);
glCompileShader(fshader);