Add instance extension member support

master
Marrub 2017-09-27 21:47:18 -04:00
parent 9191616182
commit 8ca4833b68
7 changed files with 125 additions and 71 deletions

View File

@ -1,9 +1,9 @@
// Copyright © 2017 Project Golan, all rights reserved. // Copyright © 2017 Project Golan, all rights reserved.
{0 Player -200 250 0} {0 Player -200 250 16}
{0 Sector -450 -330 510 660 -16 96} // center {0 Sector -450 -330 510 660 0 96} // center
{0 Sector -450 -355 510 25 64 96} // top {0 Sector -450 -355 510 25 64 96} // top
{0 Sector -450 330 510 25 64 96} // bottom {0 Sector -450 330 510 25 64 96} // bottom
{0 Sector -475 -355 25 710 64 96} // left {0 Sector -475 -355 25 710 64 96} // left
{0 Sector 60 -355 25 710 64 96} // right {0 Sector 60 -355 25 710 64 96} // right
// EOF // EOF

View File

@ -96,6 +96,7 @@ typedef struct G_odarg
typedef struct G_ofdcl typedef struct G_ofdcl
{ {
unsigned extm;
DGE_CallbackType fptr; DGE_CallbackType fptr;
char const *name; char const *name;
size_t keyhash; size_t keyhash;
@ -270,6 +271,7 @@ static void G_ObjDef_getEntProp(G_dodst *st, G_entty *type, char const *id)
{ {
G_ofdcl *fn = G_objfuncs.find(st->expect(tok_identi, "function")->textV); G_ofdcl *fn = G_objfuncs.find(st->expect(tok_identi, "function")->textV);
if(!fn) st->throw("invalid function name"); if(!fn) st->throw("invalid function name");
type->ext = fn->extm;
type->task = fn->fptr; type->task = fn->fptr;
} }
else if(strcmp(id, "size") == 0) else if(strcmp(id, "size") == 0)
@ -536,10 +538,10 @@ done:
// //
// G_ObjDef_LoadFunc // G_ObjDef_LoadFunc
// //
void G_ObjDef_LoadFunc(char const *name, DGE_CallbackType fptr) void G_ObjDef_LoadFunc(char const *name, unsigned ext, DGE_CallbackType fptr)
{ {
M_Vec_grow(G_ofvec, 1); M_Vec_grow(G_ofvec, 1);
G_ofvecV[G_ofvecC] = (G_ofdcl){fptr, name, M_StrHash(name)}; G_ofvecV[G_ofvecC] = (G_ofdcl){ext, fptr, name, M_StrHash(name)};
G_objfuncs.insert(&M_Vec_next(G_ofvec)); G_objfuncs.insert(&M_Vec_next(G_ofvec));
} }

67
src/g_objdef.h Normal file
View File

@ -0,0 +1,67 @@
// Copyright © 2017 Project Golan, all rights reserved.
#ifndef g_objdef_h
#define g_objdef_h
#include "m_types.h"
#include <Doominati.h>
#define G_ObjDef_setupEntity(type, th) \
do { \
(th).sprite = (type)->sprite; \
(th).health = (type)->health; \
(th).friction = (type)->friction; \
(th).mass = (type)->mass; \
(th).rsx = (type)->rsx; \
(th).rsy = (type)->rsy; \
(th).sx = (type)->sx; \
(th).sy = (type)->sy; \
(th).sz = (type)->sz; \
} while(0)
#define G_ObjDef_createTask(type, ...) \
(DGE_Task_Create(0, (type)->task, __VA_ARGS__))
#define G_ObjDef_loadFunc(ext, fn) \
G_ObjDef_LoadFunc(#fn, ext, (DGE_CallbackType)fn)
// Extern Functions ----------------------------------------------------------|
struct G_entty const *G_ObjDef_GetType(char const *name);
struct G_anima const *G_ObjDef_GetAnim(struct G_entty const *type, char const *name);
// Types ---------------------------------------------------------------------|
typedef struct G_frame // Frame
{
integ time;
M_texid sprite;
} G_frame;
typedef struct G_anima // Animation
{
G_frame *frame;
msize count;
} G_anima;
typedef struct G_entty // Entity Type
{
__prop getAnim {call: G_ObjDef_GetAnim(this)}
M_texid sprite;
integ health;
lfrac friction;
fixed mass;
fixed rsx, rsy;
fixed sx, sy, sz;
DGE_CallbackType task;
unsigned ext;
} G_entty;
// Extern Functions ----------------------------------------------------------|
void G_ObjDef_Init(void);
void G_ObjDef_Load(char const *fname);
void G_ObjDef_LoadFunc(char const *name, unsigned ext, DGE_CallbackType fptr);
#endif

View File

@ -14,7 +14,9 @@
// //
void G_Entity_Create(G_mfdat *info) void G_Entity_Create(G_mfdat *info)
{ {
DGE_Entity ent = {DGE_Entity_Create(0)}; G_entty const *type = G_ObjDef_GetType(info->ent.name);
DGE_Entity ent = {DGE_Entity_Create(type ? type->ext : 0)};
ent.x = info->ent.x; ent.x = info->ent.x;
ent.y = info->ent.y; ent.y = info->ent.y;
@ -24,10 +26,9 @@ void G_Entity_Create(G_mfdat *info)
ent.mass = 1; ent.mass = 1;
ent.friction = 0.875ulr; ent.friction = 0.875ulr;
G_entty const *type; if(type) {
if((type = G_ObjDef_GetType(info->ent.name))) {
G_ObjDef_setupEntity(type, ent); G_ObjDef_setupEntity(type, ent);
G_ObjDef_createTask (type, ent); G_ObjDef_createTask (type, ent.id);
} }
} }

View File

@ -2,70 +2,53 @@
#ifndef g_object_h #ifndef g_object_h
#define g_object_h #define g_object_h
#include "m_types.h" #include "g_objdef.h"
#include <Doominati.h> #include <Doominati.h>
#include <GDCC/HashMap.h>
#include <stddef.h> #define G_propMemOfs(ofs, t, name) \
__prop name {__get: DGE_Object_MemberGet(t, ->id, ofs), \
__set: DGE_Object_MemberSet(t, ->id, ofs)}
#define G_ObjDef_setupEntity(type, th) \ #define G_propMemExt(base, ofs, t, name) \
do { \ G_propMemOfs(DGE_OME_##base + (ofs), t, name)
(th).sprite = (type)->sprite; \
(th).health = (type)->health; \
(th).friction = (type)->friction; \
(th).mass = (type)->mass; \
(th).rsx = (type)->rsx; \
(th).rsy = (type)->rsy; \
(th).sx = (type)->sx; \
(th).sy = (type)->sy; \
(th).sz = (type)->sz; \
} while(0)
#define G_ObjDef_createTask(type, ...) \ #define G_Entity_propMem(ofs, t, name) \
(DGE_Task_Create(0, (type)->task, __VA_ARGS__)) G_propMemExt(Entity, ofs, t, name)
#define G_ObjDef_loadFunc(fn) G_ObjDef_LoadFunc(#fn, (DGE_CallbackType)fn) #define G_Entity_props() DGE_EntityProps()
// Extern Functions ----------------------------------------------------------|
struct G_entty const *G_ObjDef_GetType(char const *name);
struct G_anima const *G_ObjDef_GetAnim(struct G_entty const *type, char const *name);
// Types ---------------------------------------------------------------------| // Types ---------------------------------------------------------------------|
typedef struct G_frame // Frame enum
{ {
integ time; G_Player_ammo,
M_texid sprite; G_Player_propN
} G_frame; };
typedef struct G_anima // Animation typedef struct G_entit // Entity
{ {
G_frame *frame; int id;
msize count;
} G_anima;
typedef struct G_entty // Entity Type G_Entity_props()
} G_entit;
#define G_Player_props() G_Entity_props() \
G_Entity_propMem(G_Player_ammo, unsigned, ammo)
typedef struct G_playr // Player
{ {
__prop getAnim {call: G_ObjDef_GetAnim(this)} int id;
M_texid sprite; G_Player_props()
integ health; } G_playr;
lfrac friction;
fixed mass; // Extern Objects ------------------------------------------------------------|
fixed rsx, rsy;
fixed sx, sy, sz; extern int G_Player_Count;
DGE_CallbackType task;
} G_entty;
// Extern Functions ----------------------------------------------------------| // Extern Functions ----------------------------------------------------------|
void G_ObjDef_Init(void); DGE_Callback void G_Player_Think(unsigned id);
void G_ObjDef_Load(char const *fname);
void G_ObjDef_LoadFunc(char const *name, DGE_CallbackType fptr);
DGE_Callback void G_Player_Think(DGE_Entity ent);
void G_Entity_Create(union G_mfdat *info); void G_Entity_Create(union G_mfdat *info);
void G_Sector_Create(union G_mfdat *info); void G_Sector_Create(union G_mfdat *info);

View File

@ -7,14 +7,18 @@
#include <stdio.h> #include <stdio.h>
#include <math.h> #include <math.h>
// Extern Objects ------------------------------------------------------------|
int G_Player_Count = 1;
// Static Functions ----------------------------------------------------------| // Static Functions ----------------------------------------------------------|
// //
// G_Player_applyVelocity // G_Player_applyVelocity
// //
static void G_Player_applyVelocity(DGE_Entity ent) static void G_Player_applyVelocity(G_playr th)
{ {
DGE_Point2R ax = DGE_Input_GetAxis(1); DGE_Point3R ax = DGE_Input_GetAxis(1);
accum vx = (accum)ax.x, vy = (accum)ax.y; accum vx = (accum)ax.x, vy = (accum)ax.y;
float mag = sqrtf(vx * vx + vy * vy); float mag = sqrtf(vx * vx + vy * vy);
@ -23,8 +27,8 @@ static void G_Player_applyVelocity(DGE_Entity ent)
sincosf(ang, &s, &c); sincosf(ang, &s, &c);
ent.vx = ent.vx + c * mag * 5; th.vx = th.vx + c * mag * 5;
ent.vy = ent.vy - s * mag * 5; th.vy = th.vy - s * mag * 5;
// TODO: apply animation based on X velocity here // TODO: apply animation based on X velocity here
} }
@ -35,21 +39,18 @@ static void G_Player_applyVelocity(DGE_Entity ent)
// G_Player_Think // G_Player_Think
// //
DGE_Callback DGE_Callback
void G_Player_Think(DGE_Entity ent) void G_Player_Think(unsigned id)
{ {
DGE_Object_RefAdd(ent.id); DGE_Object_RefAdd(id);
for(;;) for(G_playr th = {id}; th.health > 0;)
{ {
if(ent.health <= 0) G_Player_applyVelocity(th);
break;
G_Player_applyVelocity(ent);
DGE_Task_Sleep(0, 1); DGE_Task_Sleep(0, 1);
} }
DGE_Object_RefSub(ent.id); DGE_Object_RefSub(id);
} }
// EOF // EOF

View File

@ -27,7 +27,7 @@ void GInit(char const *resdecl)
printf("Loading object function table...\n"); printf("Loading object function table...\n");
G_ObjDef_Init(); G_ObjDef_Init();
G_ObjDef_loadFunc(G_Player_Think); G_ObjDef_loadFunc(G_Player_propN, G_Player_Think);
printf("Loading object definitions...\n"); printf("Loading object definitions...\n");
G_ObjDef_Load("objdefs.dod"); G_ObjDef_Load("objdefs.dod");