graphics.inc
graphics.inc
CPP1/****h* PawnLibs/graphics2 * Description3 * TBD: explain basic principles and underlying hardware limitations4 ******/56/****s* PawnLibs/graphics/GFX_POINT7 * Summary8 * Array with named fields which represents a point on a screen.9 * Synopsis10 */11#define GFX_POINT [ .x, .y ]12/*13 * Description14 * Fields:15 * * x, y - coordinates of the point16 ******/1718/****s* PawnLibs/graphics/GFX_RECTANGLE19 * Summary20 * Array with named fields which represents a rectangle.21 * Synopsis22 */23#define GFX_RECTANGLE [ .x, .y, .w, .h ]24/*25 * Description26 * Fields:27 * * x, y - top left coordinates of the rectangle28 * * w - width of the rectangle29 * * h - height of the rectangle30 ******/3132/****s* PawnLibs/graphics/GFX_PARTICLE33 * Summary34 * Array with named fields with a single particle description.35 * Synopsis36 */37#define GFX_PARTICLE [ .ttl, .vx, .vy, .ax, .ay, bool:.explosion, .velocity ]38/*39 * Description40 * Fields:41 * * ttl - a particles lifetime in ms42 * * vx, vy - an initial particles velocity in px/ms43 * * ax, ay - a particles acceleration in px/ms^244 * * explosion - an explosion emulation flag45 * * velocity - an explosion velocity46 * See also47 * * GFX_EMISSION48 * * GFX_drawParticles()49 * * GFX_drawParticlesXY()50 ******/5152/****s* PawnLibs/graphics/GFX_EMISSION53 * Summary54 * Array with named fields with particles emission description.55 * Synopsis56 */57#define GFX_EMISSION [ .duration, .frequency, .max, bool:.loop ]58/*59 * Description60 * Fields:61 * * duration - how long particles emit in ms62 * * frequency - how frequently new particles emit in Hz63 * * max - how many particles can emit64 * * loop - keep emission TBD: check?65 * See also66 * * GFX_PARTICLE67 * * GFX_drawParticles()68 * * GFX_drawParticlesXY()69 ******/7071/****c* PawnLibs/graphics/GFX_text_align72 * Summary73 * Enumeration of a text align variants.74 * Synopsis75 */76const GFX_text_align: {77 TEXT_ALIGN_CENTER = 0,78 TEXT_ALIGN_TOP_CENTER,79 TEXT_ALIGN_BOTTOM_CENTER,80 TEXT_ALIGN_LEFT_CORNER,81 TEXT_ALIGN_LEFT_TOP_CORNER,82 TEXT_ALIGN_LEFT_BOTTOM_CORNER,83 TEXT_ALIGN_RIGHT_CORNER,84 TEXT_ALIGN_RIGHT_TOP_CORNER,85 TEXT_ALIGN_RIGHT_BOTTOM_CORNER,86};87/******/8889/****c* PawnLibs/graphics/GFX_mirror90 * Summary91 * Enumeration of an image mirror variants.92 * Synopsis93 */94const GFX_mirror: {95 MIRROR_BLANK = 0,96 MIRROR_X,97 MIRROR_Y,98 MIRROR_XY,99};100/******/101102/****c* PawnLibs/graphics/GFX_format103 * Summary104 * Enumeration of a pixel color formats.105 * Synopsis106 */107const GFX_format: {108 FORMAT_RGB565 = 0,109 FORMAT_ARGB6666,110 FORMAT_ARGB8888,111};112/******/113114/****e* PawnLibs/graphics/ON_Render115 * Summary116 * Render handler.117 * Synopsis118 */119forward ON_Render();120/*121 * Description122 * Public function with such signature will be called at the end of an123 * application loop. Useful to logically divide the application and rendering124 * logic. There is no limitations to render entire scene in the ON_Tick()125 * handler.126 ******/127128/****n* PawnLibs/graphics/GFX_getAssetId129 * Summary130 * Get an image ID by the filename.131 * Synopsis132 */133native GFX_getAssetId(const name[]);134/*135 * Description136 * Usually an application uses image IDs to draw sprites and backgrounds. But137 * sometimes IDs are unknown, e.g. in common libraries, or they change rapidly138 * during development. This interface can be used to get an ID of the image by139 * its filename in the runtime. Maximum asset name length is 15 characters,140 * remaining symbols are truncated during lookup.141 * Inputs142 * * name - an image filename for which ID is requested143 * Return value144 * The ID of image with a given filename or -1 in case of error.145 * See also146 * * ON_Init()147 * History148 * * v5.0 - renamed from GFX_getId() to GFX_getAssetId()149 * * v6.0 - truncate asset name in WASM API150 ******/151152/****n* PawnLibs/graphics/GFX_clear153 * Summary154 * Clear a layer with a given color.155 * Synopsis156 */157native GFX_clear(const color);158/*159 * Inputs160 * * color - a color in RGB888 format to clear a layer with161 * History162 * * v6.0 - changed color format163 ******/164165/****n* PawnLibs/graphics/GFX_drawPoint166 * Summary167 * Draw a point specified by its coordinates with a given color.168 * Synopsis169 */170native GFX_drawPoint(const point[GFX_POINT], color);171native GFX_drawPointXY(x, y, color);172/*173 * Inputs174 * * point or (x, y) - coordinates of point to draw175 * * color - a color in ARGB8888 format to draw point with176 * See also177 * * GFX_POINT178 * History179 * * v5.0 - added GFX_drawPointXY() alias180 ******/181182/****n* PawnLibs/graphics/GFX_drawCircle183 * Summary184 * Draw a circle at the given point with given radius, color and line width.185 * Synopsis186 */187native GFX_drawCircle(const center[GFX_POINT], radius, width, color);188native GFX_drawCircleXY(x, y, radius, width, color);189/*190 * Inputs191 * * center or (x, y) - coordinates of the circle center192 * * radius - radius of the circle193 * * width - line width used to draw the circle194 * * color - a color in ARGB8888 format to draw circle with195 * See also196 * * GFX_POINT197 * History198 * * v5.0 - added GFX_drawCircleXY() alias199 ******/200201/****o* PawnLibs/graphics/GFX_drawSolidCircle202 * Summary203 * Draw a solid circle at the given point with given radius and color.204 * Synopsis205 */206native GFX_drawSolidCircle(const center[GFX_POINT], radius, color);207native GFX_drawSolidCircleXY(x, y, radius, color);208/*209 * Inputs210 * * center or (x, y) - coordinates of the circle center211 * * radius - radius of the circle212 * * color - a color in ARGB8888 format to draw circle with213 * See also214 * * GFX_POINT215 * * GFX_setFillShader()216 * * GFX_setLinearGradientShader()217 * * GFX_setRadialGradientShader()218 * History219 * * v5.0 - added GFX_drawSolidCircleXY() alias220 * * v6.0 - DEPRECATED, use GFX_setFillShader() instead221 ******/222223/****n* PawnLibs/graphics/GFX_drawArc224 * Summary225 * Draw an arc at the given point with given radius, line width, color and226 * angles.227 * Synopsis228 */229native GFX_drawArc(const center[GFX_POINT], radius, width, from, to, color);230native GFX_drawArcXY(x, y, radius, width, from, to, color);231/*232 * Inputs233 * * center or (x, y) - coordinates of the arc center234 * * radius - radius of the arc235 * * width - line width used to draw the arc236 * * from - starting angle of the arc TBD: in - ? from - ?237 * * to - ending angle of the arc TBD: in - ? from - ?238 * * color - a color in ARGB8888 format to draw arc with239 * See also240 * * GFX_POINT241 * History242 * * v5.0 - added GFX_drawArcXY() alias243 ******/244245/****n* PawnLibs/graphics/GFX_drawSector246 * Summary247 * Draw a filled circular sector, connecting the arc to the circle's center248 * like a piece of pie.249 * Synopsis250 */251native GFX_drawSector(const center[GFX_POINT], radius, from, to, color);252native GFX_drawSectorXY(x, y, radius, from, to, color);253/*254 * Inputs255 * * center or (x, y) - coordinates of the circle's center256 * * radius - radius of the circle257 * * from - starting angle of the sector TBD: in - ? from - ?258 * * to - ending angle of the sector TBD: in - ? from - ?259 * * color - a color in ARGB8888 format to draw arc with260 * See also261 * * GFX_POINT262 * History263 * * v5.1 - created264 ******/265266/****n* PawnLibs/graphics/GFX_drawLine267 * Summary268 * Draw a line with from a given point to another one and given width and269 * color.270 * Synopsis271 */272native GFX_drawLine(const start[GFX_POINT], const end[GFX_POINT], width, color);273native GFX_drawLineXY(xs, ys, xe, ye, width, color);274/*275 * Inputs276 * * start or (xs, ys) - coordinates of the line starting point277 * * end or (xe, ye) - coordinates of the line ending point278 * * width - line width to draw279 * * color - a color in ARGB8888 format to draw line with280 * See also281 * * GFX_POINT282 * History283 * * v5.0 - added GFX_drawLineXY() alias284 ******/285286/****n* PawnLibs/graphics/GFX_drawRectangle287 * Summary288 * Draw a solid rectangle at a given position with specified dimensions and a289 * color.290 * Synopsis291 */292native GFX_drawRectangle(const rectangle[GFX_RECTANGLE], color);293native GFX_drawRectangleXY(x, y, w, h, color);294/*295 * Inputs296 * * rectangle or (x, y, w, h) - top left corner coordinates and dimensions of297 * the rectangle to draw298 * * color - a color in ARGB8888 format to draw rectangle with299 * See also300 * * GFX_RECTANGLE301 * History302 * * v5.0 - added GFX_drawRectangleXY() alias303 ******/304305/****n* PawnLibs/graphics/GFX_drawText306 * Summary307 * Draw a formatted text by the specified coordinates with a given color,308 * scale, text align and a rotation angle.309 * Synopsis310 */311native GFX_drawText(const center[GFX_POINT], scale, angle, fontId, GFX_text_align: align, color, const format[], {Float,Fixed,_}:...);312native GFX_drawTextXY(x, y, scale, angle, fontId, GFX_text_align: align, color, const format[], {Float,Fixed,_}:...);313/*314 * Inputs315 * * center or (x, y) - coordinates of the text center316 * * scale - percentage scale, max font size is 200x200px (100%)317 * * angle - clockwise rotation angle in degrees318 * * fontId - not applicable, reserved for future use319 * * align - text align (TBD: double check)320 * * color - a color in ARGB8888 format to draw text with321 * * format - format string, see log.inc for specification322 * * ... - variable values to insert in the resulting string323 * See also324 * * GFX_POINT325 * * GFX_text_align326 * * PawnLibs/log327 * History328 * * v5.0 - clarified documentation, added GFX_drawTextXY() alias329 ******/330331/****n* PawnLibs/graphics/GFX_bakeImage332 * Summary333 * Set an ID of a memory buffer to save next rendering result.334 * Synopsis335 */336native GFX_bakeImage(const id, const width, const height, const GFX_format: format, bool: overwrite = true);337/*338 * Description339 * Specifies the image that will be created by combining the group of340 * graphical primitives used between GFX_bakeImage() and GFS_render()341 * according their order and taking into account alpha channel in images. HW342 * has a limitation and can combine no more than 4 layers simultaneously, so343 * if there are more primitives then they will be combined in a cascade.344 * Useful for creating complex images like backgrounds for later usage.345 * Inputs346 * * id - an ID of image to access later347 * * width, height - image dimensions348 * * format - color format of baked image349 * * overwrite - true to overwrite previously baked image, false to append on350 * top of it351 * See also352 * * GFX_format353 * * GFX_render()354 * * GFX_removeBakedImage()355 * History356 * * v6.0 - added overwrite parameter357 ******/358359/****n* PawnLibs/graphics/GFX_setRenderTarget360 * Summary361 * Set a screen which will be used to display next rendering result.362 * Synopsis363 */364native GFX_setRenderTarget(const screen);365/*366 * Description367 * Cube module is a special device which has 3 independent screens to display368 * graphics. GFX_setRenderTarget() specifies a screen number which will be369 * used to display a next rendering result. Graphics primitives used in370 * between GFX_setRenderTarget() and GFX_render() calls will be combined371 * together according their order and taking into account alpha channel in372 * images. Resulting image will be immediately flushed onto the given screen.373 * HW has a limitation and can combine no more than 4 layers simultaneously,374 * so if there are more primitives then they will be combined in a cascade. To375 * display anything on the screen GFX_setRenderTarget() has to be always376 * called.377 * Inputs378 * * screen - a screen number to render the resulting image on379 * See also380 * * GFX_render()381 * * GFX_bakeImage()382 * History383 * * v5.0 - renamed from GFX_updateDisplay() to GFX_setRenderTarget()384 ******/385386/****n* PawnLibs/graphics/GFX_drawImage387 * Summary388 * Draw an image from application assets at a specified position and with389 * given transformations.390 * Synopsis391 */392native GFX_drawImage(const center[GFX_POINT], opacity, colorKey, scaleX, scaleY, angle, GFX_mirror: mirror, id);393native GFX_drawImageXY(x, y, opacity, colorKey, scaleX, scaleY, angle, GFX_mirror: mirror, id);394/*395 * Inputs396 * * center or (x, y) - coordinates of the image center to draw397 * * opacity - opacity of the entire image, 0 is fully transparent398 * * colorKey - transparent background color, pixels of that color will not be399 * used for rendering, it is useful for image with color format without400 * alpha channel, e.g. RGB565401 * * (scaleX, scaleY) - scale in percent from 1 to 100 along the corresponding402 * axis403 * * angle - clockwise rotation angle in degrees404 * * mirror - mirroring variant405 * * id - an ID of assets image to draw406 * See also407 * * GFX_POINT408 * * GFX_mirror409 * * GFX_cacheImages()410 * History411 * * v5.0 - added GFX_drawImageXY() alias, color argument clarified412 * * v6.0 - added scale parameters413 ******/414415/****n* PawnLibs/graphics/GFX_drawSubImage416 * Summary417 * Draw a sub window of an image from application assets at a specified418 * position and with given transformations.419 * Synopsis420 */421native GFX_drawSubImage(const center[GFX_POINT], winX, winY, winW, winH, opacity, colorKey, scaleX, scaleY, angle, GFX_mirror: mirror, id);422native GFX_drawSubImageXY(x, y, winX, winY, winW, winH, opacity, colorKey, scaleX, scaleY, angle, GFX_mirror: mirror, id);423/*424 * Inputs425 * * center or (x, y) - coordinates of the sub image center to draw426 * * (winX, winY, winW, winH) - coordinates of image sub window relative to427 * its top level corner428 * * opacity - opacity of the entire image, 0 is fully transparent429 * * colorKey - transparent background color, pixels of that color will not be430 * used for rendering, it is useful for image with color format without431 * alpha channel, e.g. RGB565432 * * (scaleX, scaleY) - scale in percent from 1 to 100 along the corresponding433 * axis434 * * angle - clockwise rotation angle in degrees435 * * mirror - mirroring variant436 * * id - an ID of assets image to draw sub window437 * See also438 * * GFX_POINT439 * * GFX_mirror440 * * GFX_cacheImages()441 * History442 * * v6.2 - added443 ******/444445/****n* PawnLibs/graphics/GFX_drawBakedImage446 * Summary447 * Draw a previously baked image at a specified position and with given448 * transformations.449 * Synopsis450 */451native GFX_drawBakedImage(const center[GFX_POINT], opacity, colorKey, scaleX, scaleY, angle, GFX_mirror: mirror, id);452native GFX_drawBakedImageXY(x, y, opacity, colorKey, scaleX, scaleY, angle, GFX_mirror: mirror, id);453/*454 * Inputs455 * * center or (x, y) - coordinates of the image center to draw456 * * opacity - opacity of the entire image, 0 is fully transparent457 * * colorKey - transparent background color, pixels of that color will not be458 * used for rendering, it is useful for image with color format without459 * alpha channel, e.g. RGB565460 * * (scaleX, scaleY) - scale in percent from 1 to 100 along the corresponding461 * axis462 * * angle - clockwise rotation angle in degrees463 * * mirror - mirroring variants464 * * id - an ID of the baked image to draw465 * See also466 * * GFX_POINT467 * * GFX_mirror468 * History469 * * v5.0 - added GFX_drawBakedImageXY() alias, color argument clarified470 * * v6.0 - added scale parameters471 ******/472473/****n* PawnLibs/graphics/GFX_setFillShader474 * Summary475 * Fill next primitives with specified color.476 * Synopsis477 */478native GFX_setFillShader(color);479/*480 * Inputs481 * * color - a color in ARGB8888 format to fill with482 * History483 * * v6.0 - added primitives shading484 ******/485486 /****n* PawnLibs/graphics/GFX_setLinearGradientShader487 * Summary488 * Fill next primitives with linear gradient from a given point / color to489 * another one.490 * Synopsis491 */492native GFX_setLinearGradientShader(const start[GFX_POINT], const end[GFX_POINT], color0, color1);493native GFX_setLinearGradientShaderXY(x0, y0, x1, y1, color0, color1);494/*495 * Inputs496 * * start or (xs, ys) - coordinates of the fill starting point497 * * end or (xe, ye) - coordinates of the fill ending point498 * * color0 - a color in ARGB8888 format to start fill with499 * * color1 - a color in ARGB8888 format to end fill with500 * History501 * * v6.0 - added primitives shading502 ******/503504 /****n* PawnLibs/graphics/GFX_setRadialGradientShader505 * Summary506 * Fill next primitives with radial gradient from a given point with radius507 * and from a given color to another one.508 * Synopsis509 */510native GFX_setRadialGradientShader(const center[GFX_POINT], radius, color0, color1);511native GFX_setRadialGradientShaderXY(x, y, radius, color0, color1);512/*513 * Inputs514 * * center or (x, y) - coordinates of the fill center515 * * radius - radius of the fill516 * * color0 - a color in ARGB8888 format to start fill with517 * * color1 - a color in ARGB8888 format to end fill with518 * History519 * * v6.0 - added primitives shading520 ******/521522/****n* PawnLibs/graphics/GFX_removeShader523 * Summary524 * Do not use shading in next primitives.525 * Synopsis526 */527native GFX_removeShader();528/*529 * Inputs530 * History531 * * v6.0 - added primitives shading532 ******/533534/****n* PawnLibs/graphics/GFX_drawParticles535 * Summary536 * Draw particle system with given parameters.537 * Synopsis538 */539native GFX_drawParticles(const center[GFX_POINT], opacity, colorKey, angle, id, const particle[GFX_PARTICLE], const emission[GFX_EMISSION], time);540native GFX_drawParticlesXY(x, y, opacity, colorKey, angle, id, const particle[GFX_PARTICLE], const emission[GFX_EMISSION], time);541/*542 * Description543 * TBD: create extended description how to use a particle system.544 * Inputs545 * * center or (x, y) - coordinates of the (TBD: clarify which center) center546 * to draw547 * * opacity - opacity of the entire particle system, 0 is fully transparent548 * * colorKey - transparent background color, pixels of that color will not be549 * used for rendering, it is useful for image with color format without550 * alpha channel, e.g. RGB565551 * * angle - clockwise rotation angle in degrees552 * * id - an ID of the assets image to use as a particle553 * * particle - a particle parameters554 * * emission - a particle emission parameters555 * * time - TBD: check556 * See also557 * * GFX_POINT558 * * GFX_PARTICLE559 * * GFX_EMISSION560 * History561 * * v5.0 - added GFX_drawParticlesXY() alias, color argument clarified562 ******/563564/****n* PawnLibs/graphics/GFX_drawQrCode565 * Summary566 * Generate and draw QR-code for a given text string.567 * Synopsis568 */569native GFX_drawQrCode(const center[GFX_POINT], size, color0, color1, angle, id, const text[]);570native GFX_drawQrCodeXY(x, y, size, color0, color1, angle, id, const text[]);571/*572 * Description573 * Supported version 2 which is limited by 32 characters of input.574 * Inputs575 * * center or (x, y) - coordinates of the resulting image of the QR-code to576 * draw577 * * size - size of the individual square representing a single bit in the578 * encodeded text string579 * * color0 - color of squares representing zeroes in the ARGB8888 format580 * * color1 - color of squares representing ones in the ARGB8888 format581 * * angle - clockwise rotation angle in degrees582 * * id - an ID of resulting image to distinguish different codes on the same583 scene, starts from 0584 * * text - text string to encode and draw, maximum size is 256 characters585 * See also586 * * GFX_POINT587 * History588 * * v6.0 - added589 ******/590591/****n* PawnLibs/graphics/GFX_render592 * Summary593 * Start rendering process.594 * Synopsis595 */596native GFX_render();597/*598 * See also599 * * GFX_setRenderTarget()600 * * GFX_bakeImage()601 ******/602603/****n* PawnLibs/graphics/GFX_cacheImages604 * Summary605 * Force platform to cache application images.606 * Synopsis607 */608native GFX_cacheImages(imageList[], size = sizeof(imageList));609/*610 * Description611 * Platform automatically reads and caches images from flash when the612 * application draws an image. Flash operations are synchronous and may cause613 * delays especially when reading large portions of data. To prevent the614 * application from lags it is possible to cache image data in advance.615 * However if application requests to draw a non-cached image and there is not616 * enough memory then oldest accessed images will be removed from cache. Also617 * there is a limit of 256 for a total number of cached images.618 * Inputs619 * * imageList - array with IDs of images to cache620 * * size - size of imageList array621 * Return value622 * Number of cached images or negative value if an error happened. Error623 * codes:624 * * -1 invalid function arguments count625 * * -2 invalid imageList array626 * See also627 * * GFX_clearCache()628 * History629 * * v5.0 - moved from appCtrl module to graphics630 ******/631632/****n* PawnLibs/graphics/GFX_clearCache633 * Summary634 * Clear all cached images.635 * Synopsis636 */637native GFX_clearCache();638/*639 * Description640 * Oldest accessed image cache will be automatically freed if there is not641 * enough memory to cache a new image. This function forces this process, e.g.642 * when switch game levels.643 * See also644 * * GFX_cacheImages()645 * * GFX_removeBakedImage()646 ******/647648/****n* PawnLibs/graphics/GFX_removeBakedImage649 * Summary650 * Remove specified baked image from cache.651 * Synopsis652 */653native GFX_removeBakedImage(id);654/*655 * Description656 * Baked images do not automatically invalidate. This function allows to657 * remove any baked image from cache to save some space.658 * Inputs659 * * id - an ID of the baked image to remove from cache660 * See also661 * * GFX_clearCache()662 * * GFX_bakeImage()663 * History664 * * v6.0 - added665 ******/666667/****n* PawnLibs/graphics/GFX_getAssetsCount668 * Summary669 * Get the total count of graphic assets.670 * Synopsis671 */672native GFX_getAssetsCount();673/*674 * Return value675 * Number of graphic assets.676 * History677 * * v6.0 - added678 ******/679680/****f* PawnLibs/graphics/GFX_fromARGB8888681 * Summary682 * Returns a hexadecimal value of a color constructed from the channel683 * components.684 * Synopsis685 */686stock GFX_fromARGB8888(a, r, g, b)687/*688 * Inputs689 * * a - the alpha channel, valid values are 0 through 255690 * * r - the red channel, valid values are 0 through 255691 * * g - the green channel, valid values are 0 through 255692 * * b - the blue channel, valid values are 0 through 255693 * Return value694 * The hexadecimal value of a color constructed from the channel components.695 * See also696 * * GFX_toARGB8888()697 * History698 * * v5.0 - created699 * Source700 */701{702 return (a & 0xFF) << 24 | (r & 0xFF) << 16 | (g & 0xFF) << 8 | (b & 0xFF);703}704/******/705706/****f* PawnLibs/graphics/GFX_toARGB8888707 * Summary708 * Returns channel values of a color in the hexadecimal format.709 * Synopsis710 */711stock GFX_toARGB8888(value, &a, &r, &g, &b)712/*713 * Inputs714 * * value - the hexadecimal value of the color715 * Outputs716 * * a - the alpha channel717 * * r - the red channel718 * * g - the green channel719 * * b - the blue channel720 * See also721 * * GFX_fromARGB8888722 * History723 * * v5.0 - created724 * Source725 */726{727 a = value & 0xFF000000;728 r = value & 0x00FF0000;729 g = value & 0x0000FF00;730 b = value & 0x000000FF;731}732/******/733734
Wrapped for easier reading. Turn wrap off to inspect exact line lengths.