Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma target fte
- // ========================================================
- // 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 = ftoi(hash_get(srctab, key));
- return sources + idx;
- }
- }SourcePics;
- // --------------------------------------------------------
- // Graphics - Accessor for graphic
- // --------------------------------------------------------
- accessor gfx:Graphic*
- {
- get SourcePic* [string key] =
- {
- int idx = ftoi(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_FLOAT);
- sourcecnt++;
- SourcePic *temp = memalloc(sizeof(SourcePic) * sourcecnt);
- memcpy(temp, sources, sizeof(SourcePic) * (sourcecnt - 1));
- memfree(sources);
- sources = temp;
- hash_add(srctab, alias, itof(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 - Remove a source
- // --------------------------------------------------------
- void(SourcePic *source) SourcePic_Remove =
- {
- int index = SourcePic_IndexOf(source);
- memcpy(&sources[index], &sources[index + 1], sizeof(SourcePic) * (sourcecnt - (index + 1)));
- };
- // --------------------------------------------------------
- // 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 *source) Graphic_AddNew =
- {
- if(graphiccnt <= 0) gfxtab = hash_createtab(0, EV_FLOAT);
- graphiccnt++;
- Graphic *temp = memalloc(sizeof(Graphic) * graphiccnt);
- memcpy(temp, graphics, sizeof(Graphic) * (graphiccnt - 1));
- hash_add(gfxtab, alias, itof(graphiccnt - 1));
- Graphics[alias]->source = source->alias;
- Graphics[alias]->origin = origin;
- Graphics[alias]->size = size;
- };
- int(Graphic *gf) Graphic_IndexOf =
- {
- return ftoi(hash_get(gfxtab, gf->alias));
- };
- void(Graphic *gf) Graphic_Remove =
- {
- int index = Graphic_IndexOf(gf);
- memcpy(&graphics[index], &graphics[index + 1], sizeof(Graphic) * (graphiccnt - (index + 1)));
- };
- 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 hotspot = anchor + location;
- // Distances
- float uld = GetDistance(hotspot, ul);
- float urd = GetDistance(hotspot, ur);
- float lrd = GetDistance(hotspot, lr);
- float lld = GetDistance(hotspot, ll);
- // Rotate with trig
- ul = OrbitPoint(hotspot, uld, angle);
- rl = OrbitPoint(hotspot, urd, angle);
- lr = OrbitPoint(hotspot, lrd, angle);
- ll = OrbitPoint(hotspot, lld, 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();
- };
- 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();
- };
- void() Graphic_Clear =
- {
- memfree(graphics);
- graphiccnt = 0;
- hash_destroytab(gfxtab);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement