Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ========================================================
- // GRAPHICS **********************************************
- // ========================================================
- // This is the base graphic class. It includes sources and
- // subpic definitions used by both sprites and tiles.
- // --------------------------------------------------------
- // --------------------------------------------------------
- // Graphics - Struct - SourcePic
- // --------------------------------------------------------
- typedef struct
- {
- string alias;
- string path;
- vector size;
- }SourcePic;
- // --------------------------------------------------------
- // Graphics - Struct - Graphic (subpic)
- // --------------------------------------------------------
- typedef struct
- {
- string alias;
- string source;
- vector origin;
- vector size;
- }Graphic;
- // --------------------------------------------------------
- // Graphics - Pointers for storing sources and graphics
- // --------------------------------------------------------
- SourcePic *sources;
- Graphic *graphics;
- // --------------------------------------------------------
- // Graphics - Counters for pointer lengths
- // --------------------------------------------------------
- int sourcecnt;
- int graphiccnt;
- // --------------------------------------------------------
- // Graphics - Hashtables for quick lookup
- // --------------------------------------------------------
- hashtable srctab;
- hashtable gfxtab;
- // --------------------------------------------------------
- // Graphics - Accessor for sourcepic
- // --------------------------------------------------------
- accessor src:SourcePic*
- {
- get SourcePic* [string key] =
- {
- int idx = hash_get(srctab, key);
- return sources + idx;
- }
- }SourcePics;
- // --------------------------------------------------------
- // Graphics - Accessor for graphic
- // --------------------------------------------------------
- accessor gfx:Graphic*
- {
- get Graphic* [string key] =
- {
- int idx = hash_get(gfxtab, key);
- return graphics + idx;
- }
- }Graphics;
- // --------------------------------------------------------
- // Graphics - Add new source
- // --------------------------------------------------------
- void(string alias, string path) SourcePic_AddNew =
- {
- if(sourcecnt <= 0) srctab = hash_createtab(0, EV_INTEGER);
- sourcecnt++;
- SourcePic *temp = memalloc(sizeof(SourcePic) * sourcecnt);
- memcpy(temp, sources, sizeof(SourcePic) * (sourcecnt - 1));
- memfree(sources);
- sources = temp;
- hash_add(srctab, alias, sourcecnt - 1);
- SourcePics[alias]->path = path;
- SourcePics[alias]->alias = alias;
- SourcePics[alias]->size = drawgetimagesize(path);
- };
- // --------------------------------------------------------
- // Graphics - Source Index
- // --------------------------------------------------------
- int(SourcePic *source) SourcePic_IndexOf =
- {
- return ftoi(hash_get(srctab, source->alias));
- };
- // --------------------------------------------------------
- // Graphics - Draw source resized
- // --------------------------------------------------------
- void(SourcePic *source, vector location, vector size, vector rgb, float alpha) SourcePic_DrawScaled =
- {
- drawpic(location, source->path, size, rgb, alpha);
- };
- // --------------------------------------------------------
- // Graphics - Draw source at base size
- // --------------------------------------------------------
- void(SourcePic *source, vector location, vector rgb, float alpha) SourcePic_Draw =
- {
- SourcePic_DrawScaled(source, location, source->size, rgb, alpha);
- };
- // --------------------------------------------------------
- // Graphics - Purge all source data
- // --------------------------------------------------------
- void() SourcePic_Clear =
- {
- memfree(sources);
- hash_destroytab(srctab);
- sourcecnt = 0;
- };
- // --------------------------------------------------------
- // Graphics - Add new graphic
- // --------------------------------------------------------
- void(string alias, vector origin, vector size, SourcePic *src) Graphic_AddNew =
- {
- if(graphiccnt <= 0) gfxtab = hash_createtab(0, EV_INTEGER);
- graphiccnt++;
- Graphic *temp = memalloc(sizeof(Graphic) * graphiccnt);
- memcpy(temp, graphics, sizeof(Graphic) * (graphiccnt - 1));
- hash_add(gfxtab, alias, graphiccnt - 1);
- Graphics[alias]->source = src->alias;
- Graphics[alias]->origin = origin;
- Graphics[alias]->size = size;
- };
- // --------------------------------------------------------
- // Graphics - Get index
- // --------------------------------------------------------
- int(Graphic *gf) Graphic_IndexOf =
- {
- return ftoi(hash_get(gfxtab, gf->alias));
- };
- // --------------------------------------------------------
- // Graphics - Draw with rotation
- // --------------------------------------------------------
- void(Graphic *gf, vector location, vector anchor, float angle, vector rgb, float alpha) Graphic_DrawRotated =
- {
- // four corners of the rectangle.
- vector ul = location;
- vector ur = [location_x + gf->size[0], location_y];
- vector lr = [location_x + gf->size[0], location_y + gf->size[1]];
- vector ll = [location_x, location_y + gf->size[1]];
- // hotspot
- vector pivot = anchor + location;
- angle = angle * PI/180;
- ul = OrbitPoint2(pivot, ul, angle);
- ur = OrbitPoint2(pivot, ur, angle);
- lr = OrbitPoint2(pivot, lr, angle);
- ll = OrbitPoint2(pivot, ll, angle);
- // Draw
- R_BeginPolygon(SourcePics[gf->source]->path, 0, 1);
- R_PolygonVertex(ul, ToTexCoords(SourcePics[gf->source]->size, [gf->origin[0], gf->origin[1]]), rgb, alpha);
- R_PolygonVertex(ur, ToTexCoords(SourcePics[gf->source]->size, [gf->origin[0] + gf->size[0], gf->origin[1]]), rgb, alpha);
- R_PolygonVertex(lr, ToTexCoords(SourcePics[gf->source]->size, [gf->origin[0] + gf->size[0], gf->origin[1] + gf->size[1]]), rgb, alpha);
- R_PolygonVertex(ll, ToTexCoords(SourcePics[gf->source]->size, [gf->origin[0], gf->origin[1] + gf->size[1]]), rgb, alpha);
- R_EndPolygon();
- };
- // --------------------------------------------------------
- // Graphics - Draw without rotation
- // --------------------------------------------------------
- void(Graphic *gf, vector location, vector rgb, float alpha) Graphic_Draw =
- {
- // four corners of the rectangle.
- vector ul = location;
- vector ur = [location_x + gf->size[0], location_y];
- vector lr = [location_x + gf->size[0], location_y + gf->size[1]];
- vector ll = [location_x, location_y + gf->size[1]];
- // Draw
- R_BeginPolygon(SourcePics[gf->source]->path, 0, 1);
- R_PolygonVertex(ul, ToTexCoords(SourcePics[gf->source]->size, [gf->origin[0], gf->origin[1]]), rgb, alpha);
- R_PolygonVertex(ur, ToTexCoords(SourcePics[gf->source]->size, [gf->origin[0] + gf->size[0], gf->origin[1]]), rgb, alpha);
- R_PolygonVertex(lr, ToTexCoords(SourcePics[gf->source]->size, [gf->origin[0] + gf->size[0], gf->origin[1] + gf->size[1]]), rgb, alpha);
- R_PolygonVertex(ll, ToTexCoords(SourcePics[gf->source]->size, [gf->origin[0], gf->origin[1] + gf->size[1]]), rgb, alpha);
- R_EndPolygon();
- };
- // --------------------------------------------------------
- // Graphics - Clear graphics data
- // --------------------------------------------------------
- void() Graphic_Clear =
- {
- memfree(graphics);
- graphiccnt = 0;
- hash_destroytab(gfxtab);
- };
- // --------------------------------------------------------
- // Graphics - Get center
- // --------------------------------------------------------
- vector(Graphic *gf, vector pos) Graphic_GetCenter =
- {
- return pos + [gf->size[0] / 2, gf->size[1] / 2];
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement