From 4ad3bfa3e2e6e12a04a0b6272d0ed65394161efb Mon Sep 17 00:00:00 2001 From: Marrub Date: Thu, 21 Sep 2017 21:55:03 -0400 Subject: [PATCH] Add ResDecl parser --- objdefs/player.dod | 2 +- resdecl.rd | 9 +++ shaders/base.vp | 15 +++++ shaders/plasma.fp | 55 +++++++++++++++++ src/g_objdef.c | 7 +-- src/m_tokbuf.c | 25 +++++++- src/main.c | 6 +- src/r_resdec.c | 148 +++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 256 insertions(+), 11 deletions(-) create mode 100644 resdecl.rd create mode 100644 shaders/base.vp create mode 100644 shaders/plasma.fp create mode 100644 src/r_resdec.c diff --git a/objdefs/player.dod b/objdefs/player.dod index 39e4a74..665f536 100644 --- a/objdefs/player.dod +++ b/objdefs/player.dod @@ -9,7 +9,7 @@ entity Player < Shootable task G_Player_Think size 3 drawsize 16 - sprite "@sprites/particle3.png" + sprite "ent/player" } // EOF diff --git a/resdecl.rd b/resdecl.rd new file mode 100644 index 0000000..77d4eda --- /dev/null +++ b/resdecl.rd @@ -0,0 +1,9 @@ +// Copyright © 2017 Project Golan, all rights reserved. +font "base" = "fonts/base.ttf", 24pt + +texture "gui/border" = "textures/Border.png" +texture "ent/player" = "sprites/Particle3.png" + +shader "plasma" = "shaders/plasma.fp", "shaders/base.vp" + +// EOF diff --git a/shaders/base.vp b/shaders/base.vp new file mode 100644 index 0000000..06bcd62 --- /dev/null +++ b/shaders/base.vp @@ -0,0 +1,15 @@ +// Copyright © 2017 Project Golan, all rights reserved. +#version 120 + +varying vec4 color; +varying vec4 texcoord; + +void main(void) +{ + gl_Position = ftransform(); + + texcoord = gl_MultiTexCoord0; + color = gl_Color; +} + +// EOF diff --git a/shaders/plasma.fp b/shaders/plasma.fp new file mode 100644 index 0000000..0c126d1 --- /dev/null +++ b/shaders/plasma.fp @@ -0,0 +1,55 @@ +// Copyright © 2017 Project Golan, all rights reserved. +#version 120 + +uniform sampler2D DGE_Texture; +uniform int DGE_Ticks; +uniform float DGE_Seconds; + +varying vec4 color; +varying vec4 texcoord; + +vec4 HSVToRGB(float h, float s, float v, float a) +{ + h *= 360.0; + + if(h < 0) h = 0; + else if(h > 359.5) h = 359.5; + + float chroma = v * s; + float hp = h / 60.0; + float x = chroma * (1 - abs(mod(hp, 2) - 1)); + float r1 = 0, g1 = 0, b1 = 0; + + if(0 <= hp && hp < 1) r1 = chroma, g1 = x, b1 = 0; + else if(1 <= hp && hp < 2) r1 = x, g1 = chroma, b1 = 0; + else if(2 <= hp && hp < 3) r1 = 0, g1 = chroma, b1 = x; + else if(3 <= hp && hp < 4) r1 = 0, g1 = x, b1 = chroma; + else if(4 <= hp && hp < 5) r1 = x, g1 = 0, b1 = chroma; + else if(5 <= hp && hp < 6) r1 = chroma, g1 = 0, b1 = x; + + float m = v - chroma; + return vec4(r1 + m, g1 + m, b1 + m, a); +} + +void main(void) +{ + const float pi = 3.14159265; + + float t = (DGE_Seconds * 1000.0) / 32.0; + vec2 uv = texcoord.xy * vec2(256.0, 224.0); + float bx = uv.x + 0.5 * sin(t / 8.0); + float by = uv.y + 0.5 * cos(t / 8.0); + float v; + + v = sin((-bx + t) / 32.0); + v += cos((by + t) / 32.0); + v += sin((bx + by + t) / 32.0); + v += sin((sqrt(pow(bx + sin(t / 3.0), 2.0) + pow(by + cos(t / 2.0), 2.0) + 1.0) + t) / 128.0); + + gl_FragColor = + texture2D(DGE_Texture, texcoord.xy) * + HSVToRGB(sin(v * pi) * 0.5 + 0.5, 1.0, 0.2, 1.0) * + color; +} + +// EOF diff --git a/src/g_objdef.c b/src/g_objdef.c index 0b16e19..0d74afa 100644 --- a/src/g_objdef.c +++ b/src/g_objdef.c @@ -11,9 +11,6 @@ #include #include -#define G_dodTokBeg (4) -#define G_dodTokEnd (10) - #define G_ofmap_GetKey(o) ( (o)->name) #define G_ofmap_GetNext(o) (&(o)->next) #define G_ofmap_GetPrev(o) (&(o)->prev) @@ -491,8 +488,8 @@ void G_ObjDef_Init(void) void G_ObjDef_Load(char const *fname) { G_dodst st = {.fname = fname, .lines = 1, .tbPtr = &st.tb, - .tb = {.bbeg = G_dodTokBeg, .bend = G_dodTokEnd, - .tokProcess = G_ObjDef_tokProcess, .udata = &st}}; + .tb = {.bbeg = 4, .bend = 10, .tokProcess = G_ObjDef_tokProcess, + .udata = &st}}; st.fp = fopen(fname, "r"); st.tb.fp = st.fp; diff --git a/src/m_tokbuf.c b/src/m_tokbuf.c index ca24e65..edc3575 100644 --- a/src/m_tokbuf.c +++ b/src/m_tokbuf.c @@ -4,6 +4,21 @@ #include #include +// Static Functions ----------------------------------------------------------| + +// +// M_TokBuf_tokProcess +// +static enum M_tkprc M_TokBuf_tokProcess(M_token *tok, void *udata) +{ + switch(tok->type) { + default: return tokproc_next; + case tok_eof: return tokproc_done; + case tok_lnend: + case tok_cmtlin: return tokproc_skip; + } +} + // Extern Functions ----------------------------------------------------------| // @@ -12,6 +27,7 @@ void M_TokBuf_Ctor(M_tkbuf *tb) { tb->toks = calloc(tb->bend, sizeof(M_token)); + if(!tb->tokProcess) tb->tokProcess = M_TokBuf_tokProcess; } // @@ -19,10 +35,13 @@ void M_TokBuf_Ctor(M_tkbuf *tb) // void M_TokBuf_Dtor(M_tkbuf *tb) { - for(int i = 0; i < tb->bend; i++) - free(tb->toks[i].textV); + /* TODO + if(tb->toks) + for(int i = 0; i < tb->bend; i++) + free(tb->toks[i].textV); free(tb->toks); + */ } // @@ -34,8 +53,10 @@ M_token *M_TokBuf_Get(M_tkbuf *tb) if(++tb->tpos < tb->tend) return &tb->toks[tb->tpos]; + /* TODO for(int i = 0; i < tb->tend - tb->bbeg; i++) free(tb->toks[i].textV); + */ memmove(&tb->toks[0], &tb->toks[tb->tend - tb->bbeg], sizeof(M_token) * tb->bbeg); diff --git a/src/main.c b/src/main.c index 462e747..7a80ca4 100644 --- a/src/main.c +++ b/src/main.c @@ -49,6 +49,9 @@ void main(void) DGE_Renderer_SetVirtualRes(960, 720); + printf("Loading resource declarations...\n"); + R_ResDec_Load("resdecl.rd"); + printf("Loading object function table...\n"); G_ObjDef_Init(); G_ObjDef_loadFunc(G_Player_Think); @@ -56,9 +59,6 @@ void main(void) printf("Loading object definitions...\n"); G_ObjDef_Load("objdefs.dod"); - printf("Creating fonts...\n"); - DGE_Font_Create(s"myfont", "fonts/base.ttf", 24); - printf("Registering callbacks...\n"); DGE_Callback_Register(DGE_CB_DrawPost, (DGE_CallbackType)R_DrawPost); diff --git a/src/r_resdec.c b/src/r_resdec.c new file mode 100644 index 0000000..7ca2013 --- /dev/null +++ b/src/r_resdec.c @@ -0,0 +1,148 @@ +// Copyright © 2017 Project Golan, all rights reserved. +#include "m_tokbuf.h" +#include "m_str.h" + +#include + +#include +#include +#include + +// Static Functions ----------------------------------------------------------| + +_Noreturn static void R_ResDec_throw (struct R_rdcst *st, char const *emsg); +static M_token *R_ResDec_expect(struct R_rdcst *st, M_tokty t, char const *exp); + +// Types ---------------------------------------------------------------------| + +typedef struct R_rdcst +{ + __prop throw {call: R_ResDec_throw (this)} + __prop expect {call: R_ResDec_expect(this)} + + M_tkbuf tb; + jmp_buf ejmp; +} R_rdcst; + +// Static Functions ----------------------------------------------------------| + +// +// R_ResDec_throw +// +_Noreturn static void R_ResDec_throw(R_rdcst *st, char const *emsg) +{ + fprintf(stderr, "ResDec: %s\n", emsg); + longjmp(st->ejmp, 1); +} + +// +// R_ResDec_expect +// +static M_token *R_ResDec_expect(R_rdcst *st, M_tokty t, char const *exp) +{ + M_token *tok; + if((tok = st->tb.get())->type != t) + st->throw(M_StrFmt("expected %s", exp)); + return tok; +} + +// +// R_ResDec_getFont +// +static void R_ResDec_getFont(R_rdcst *st) +{ + char const *fname; + __str name; + int pt; + + name = M_StrCreate(st->expect(tok_string, "alias name")->textV); + st->expect(tok_eq, "'='"); + + fname = st->expect(tok_string, "file name")->textV; + st->expect(tok_comma, "','"); + + pt = strtoi(st->expect(tok_number, "pt size")->textV, NULL, 0); + DGE_Font_Create(name, fname, pt); +} + +// +// R_ResDec_getTexture +// +static void R_ResDec_getTexture(R_rdcst *st) +{ + char const *fname; + __str name; + + name = M_StrCreate(st->expect(tok_string, "alias name")->textV); + st->expect(tok_eq, "'='"); + + fname = st->expect(tok_string, "file name")->textV; + DGE_Texture_Create(name, fname); +} + +// +// R_ResDec_getShader +// +static void R_ResDec_getShader(R_rdcst *st) +{ + char const *fp, *vp; + __str name; + bool data = false; + + name = M_StrCreate(st->expect(tok_string, "alias name")->textV); + st->expect(tok_eq, "'='"); + + if(st->tb.drop(tok_identi) && strcmp(st->tb.reget()->textV, "data") == 0) + data = true; + + fp = st->expect(tok_string, "fragment shader")->textV; + st->expect(tok_comma, "','"); + + vp = st->expect(tok_string, "vertex shader")->textV; + if(data) DGE_Shader_CreateData(name, fp, vp); + else DGE_Shader_CreateFile(name, fp, vp); +} + +// Extern Functions ----------------------------------------------------------| + +// +// R_ResDec_Load +// +void R_ResDec_Load(char const *fname) +{ + R_rdcst st = {{.bbeg = 4, .bend = 10, .fp = fopen(fname, "r")}}; + + if(!st.tb.fp) { + fprintf(stderr, "ResDec: couldn't open file '%s'\n", fname); + goto done; + } + + st.tb.ctor(); + + if(setjmp(st.ejmp) == 1) + goto done; + + for(M_token *tok; (tok = st.tb.get())->type != tok_eof;) + { + if(tok->type != tok_identi) + st.throw("expected identifier"); + else if(strcmp(tok->textV, "font") == 0) + R_ResDec_getFont(&st); + else if(strcmp(tok->textV, "texture") == 0) + R_ResDec_getTexture(&st); + else if(strcmp(tok->textV, "shader") == 0) + R_ResDec_getShader(&st); + else if(strcmp(tok->textV, "include") == 0) + R_ResDec_Load(st.expect(tok_string, "file name")->textV); + else + st.throw(M_StrFmt("invalid resource type '%s'", tok->textV)); + } + + printf("ResDec: '%s' loaded.\n", fname); + +done: + st.tb.dtor(); + fclose(st.tb.fp); +} + +// EOF