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/****c* PawnLibs/graphics/GFX_text_align33 * Summary34 * Enumeration of a text align variants.35 * Synopsis36 */37const GFX_text_align: {38 TEXT_ALIGN_CENTER = 0,39 TEXT_ALIGN_TOP_CENTER,40 TEXT_ALIGN_BOTTOM_CENTER,41 TEXT_ALIGN_LEFT_CORNER,42 TEXT_ALIGN_LEFT_TOP_CORNER,43 TEXT_ALIGN_LEFT_BOTTOM_CORNER,44 TEXT_ALIGN_RIGHT_CORNER,45 TEXT_ALIGN_RIGHT_TOP_CORNER,46 TEXT_ALIGN_RIGHT_BOTTOM_CORNER,47};48/******/4950/****c* PawnLibs/graphics/GFX_mirror51 * Summary52 * Enumeration of an image mirror variants.53 * Synopsis54 */55const GFX_mirror: {56 MIRROR_BLANK = 0,57 MIRROR_X,58 MIRROR_Y,59 MIRROR_XY,60};61/******/6263/****c* PawnLibs/graphics/GFX_format64 * Summary65 * Enumeration of a pixel color formats.66 * Synopsis67 */68const GFX_format: {69 FORMAT_RGB565 = 0,70 FORMAT_ARGB6666,71 FORMAT_ARGB8888,72};73/******/7475/****e* PawnLibs/graphics/ON_Render76 * Summary77 * Render handler.78 * Synopsis79 */80forward ON_Render();81/*82 * Description83 * Public function with such signature will be called at the end of an84 * application loop. Useful to logically divide the application and rendering85 * logic. There is no limitations to render entire scene in the ON_Tick()86 * handler.87 ******/8889/****n* PawnLibs/graphics/GFX_getAssetId90 * Summary91 * Get an image ID by the filename.92 * Synopsis93 */94native GFX_getAssetId(const name[]);95/*96 * Description97 * Usually an application uses image IDs to draw sprites and backgrounds. But98 * sometimes IDs are unknown, e.g. in common libraries, or they change rapidly99 * during development. This interface can be used to get an ID of the image by100 * its filename in the runtime. Maximum asset name length is 15 characters,101 * remaining symbols are truncated during lookup.102 * Inputs103 * * name - an image filename for which ID is requested104 * Return value105 * The ID of image with a given filename or -1 in case of error.106 * See also107 * * ON_Init()108 * History109 * * v5.0 - renamed from GFX_getId() to GFX_getAssetId()110 * * v6.0 - truncate asset name in WASM API111 ******/112113/****n* PawnLibs/graphics/GFX_clear114 * Summary115 * Clear a layer with a given color.116 * Synopsis117 */118native GFX_clear(const color);119/*120 * Inputs121 * * color - a color in RGB888 format to clear a layer with122 * History123 * * v6.0 - changed color format124 ******/125126/****n* PawnLibs/graphics/GFX_drawPoint127 * Summary128 * Draw a point specified by its coordinates with a given color.129 * Synopsis130 */131native GFX_drawPoint(const point[GFX_POINT], color);132native GFX_drawPointXY(x, y, color);133/*134 * Inputs135 * * point or (x, y) - coordinates of point to draw136 * * color - a color in ARGB8888 format to draw point with137 * See also138 * * GFX_POINT139 * History140 * * v5.0 - added GFX_drawPointXY() alias141 ******/142143/****n* PawnLibs/graphics/GFX_drawCircle144 * Summary145 * Draw a circle at the given point with given radius, color and line width.146 * Synopsis147 */148native GFX_drawCircle(const center[GFX_POINT], radius, width, color);149native GFX_drawCircleXY(x, y, radius, width, color);150/*151 * Inputs152 * * center or (x, y) - coordinates of the circle center153 * * radius - radius of the circle154 * * width - line width used to draw the circle155 * * color - a color in ARGB8888 format to draw circle with156 * See also157 * * GFX_POINT158 * History159 * * v5.0 - added GFX_drawCircleXY() alias160 ******/161162/****o* PawnLibs/graphics/GFX_drawSolidCircle163 * Summary164 * Draw a solid circle at the given point with given radius and color.165 * Synopsis166 */167native GFX_drawSolidCircle(const center[GFX_POINT], radius, color);168native GFX_drawSolidCircleXY(x, y, radius, color);169/*170 * Inputs171 * * center or (x, y) - coordinates of the circle center172 * * radius - radius of the circle173 * * color - a color in ARGB8888 format to draw circle with174 * See also175 * * GFX_POINT176 * * GFX_setFillShader()177 * * GFX_setLinearGradientShader()178 * * GFX_setRadialGradientShader()179 * History180 * * v5.0 - added GFX_drawSolidCircleXY() alias181 * * v6.0 - DEPRECATED, use GFX_setFillShader() instead182 ******/183184/****n* PawnLibs/graphics/GFX_drawArc185 * Summary186 * Draw an arc at the given point with given radius, line width, color and187 * angles.188 * Synopsis189 */190native GFX_drawArc(const center[GFX_POINT], radius, width, from, to, color);191native GFX_drawArcXY(x, y, radius, width, from, to, color);192/*193 * Inputs194 * * center or (x, y) - coordinates of the arc center195 * * radius - radius of the arc196 * * width - line width used to draw the arc197 * * from - starting angle of the arc TBD: in - ? from - ?198 * * to - ending angle of the arc TBD: in - ? from - ?199 * * color - a color in ARGB8888 format to draw arc with200 * See also201 * * GFX_POINT202 * History203 * * v5.0 - added GFX_drawArcXY() alias204 ******/205206/****n* PawnLibs/graphics/GFX_drawSector207 * Summary208 * Draw a filled circular sector, connecting the arc to the circle's center209 * like a piece of pie.210 * Synopsis211 */212native GFX_drawSector(const center[GFX_POINT], radius, from, to, color);213native GFX_drawSectorXY(x, y, radius, from, to, color);214/*215 * Inputs216 * * center or (x, y) - coordinates of the circle's center217 * * radius - radius of the circle218 * * from - starting angle of the sector TBD: in - ? from - ?219 * * to - ending angle of the sector TBD: in - ? from - ?220 * * color - a color in ARGB8888 format to draw arc with221 * See also222 * * GFX_POINT223 * History224 * * v5.1 - created225 ******/226227/****n* PawnLibs/graphics/GFX_drawLine228 * Summary229 * Draw a line with from a given point to another one and given width and230 * color.231 * Synopsis232 */233native GFX_drawLine(const start[GFX_POINT], const end[GFX_POINT], width, color);234native GFX_drawLineXY(xs, ys, xe, ye, width, color);235/*236 * Inputs237 * * start or (xs, ys) - coordinates of the line starting point238 * * end or (xe, ye) - coordinates of the line ending point239 * * width - line width to draw240 * * color - a color in ARGB8888 format to draw line with241 * See also242 * * GFX_POINT243 * History244 * * v5.0 - added GFX_drawLineXY() alias245 ******/246247/****n* PawnLibs/graphics/GFX_drawRectangle248 * Summary249 * Draw a solid rectangle at a given position with specified dimensions and a250 * color.251 * Synopsis252 */253native GFX_drawRectangle(const rectangle[GFX_RECTANGLE], color);254native GFX_drawRectangleXY(x, y, w, h, color);255/*256 * Inputs257 * * rectangle or (x, y, w, h) - top left corner coordinates and dimensions of258 * the rectangle to draw259 * * color - a color in ARGB8888 format to draw rectangle with260 * See also261 * * GFX_RECTANGLE262 * History263 * * v5.0 - added GFX_drawRectangleXY() alias264 ******/265266/****n* PawnLibs/graphics/GFX_drawText267 * Summary268 * Draw a formatted text by the specified coordinates with a given color,269 * scale, text align and a rotation angle.270 * Synopsis271 */272native GFX_drawText(const center[GFX_POINT], scale, angle, fontId, GFX_text_align: align, color, const format[], {Float,Fixed,_}:...);273native GFX_drawTextXY(x, y, scale, angle, fontId, GFX_text_align: align, color, const format[], {Float,Fixed,_}:...);274/*275 * Inputs276 * * center or (x, y) - coordinates of the text center277 * * scale - percentage scale, max font size is 200x200px (100%)278 * * angle - clockwise rotation angle in degrees279 * * fontId - not applicable, reserved for future use280 * * align - text align (TBD: double check)281 * * color - a color in ARGB8888 format to draw text with282 * * format - format string, see log.inc for specification283 * * ... - variable values to insert in the resulting string284 * See also285 * * GFX_POINT286 * * GFX_text_align287 * * PawnLibs/log288 * History289 * * v5.0 - clarified documentation, added GFX_drawTextXY() alias290 ******/291292/****n* PawnLibs/graphics/GFX_bakeImage293 * Summary294 * Set an ID of a memory buffer to save next rendering result.295 * Synopsis296 */297native GFX_bakeImage(const id, const width, const height, const GFX_format: format, bool: overwrite = true);298/*299 * Description300 * Specifies the image that will be created by combining the group of301 * graphical primitives used between GFX_bakeImage() and GFS_render()302 * according their order and taking into account alpha channel in images. HW303 * has a limitation and can combine no more than 4 layers simultaneously, so304 * if there are more primitives then they will be combined in a cascade.305 * Useful for creating complex images like backgrounds for later usage.306 * Inputs307 * * id - an ID of image to access later308 * * width, height - image dimensions309 * * format - color format of baked image310 * * overwrite - true to overwrite previously baked image, false to append on311 * top of it312 * See also313 * * GFX_format314 * * GFX_render()315 * * GFX_removeBakedImage()316 * History317 * * v6.0 - added overwrite parameter318 ******/319320/****n* PawnLibs/graphics/GFX_setRenderTarget321 * Summary322 * Set a screen which will be used to display next rendering result.323 * Synopsis324 */325native GFX_setRenderTarget(const screen);326/*327 * Description328 * Cube module is a special device which has 3 independent screens to display329 * graphics. GFX_setRenderTarget() specifies a screen number which will be330 * used to display a next rendering result. Graphics primitives used in331 * between GFX_setRenderTarget() and GFX_render() calls will be combined332 * together according their order and taking into account alpha channel in333 * images. Resulting image will be immediately flushed onto the given screen.334 * HW has a limitation and can combine no more than 4 layers simultaneously,335 * so if there are more primitives then they will be combined in a cascade. To336 * display anything on the screen GFX_setRenderTarget() has to be always337 * called.338 * Inputs339 * * screen - a screen number to render the resulting image on340 * See also341 * * GFX_render()342 * * GFX_bakeImage()343 * History344 * * v5.0 - renamed from GFX_updateDisplay() to GFX_setRenderTarget()345 ******/346347/****n* PawnLibs/graphics/GFX_drawImage348 * Summary349 * Draw an image from application assets at a specified position and with350 * given transformations.351 * Synopsis352 */353native GFX_drawImage(const center[GFX_POINT], opacity, colorKey, scaleX, scaleY, angle, GFX_mirror: mirror, id);354native GFX_drawImageXY(x, y, opacity, colorKey, scaleX, scaleY, angle, GFX_mirror: mirror, id);355/*356 * Inputs357 * * center or (x, y) - coordinates of the image center to draw358 * * opacity - opacity of the entire image, 0 is fully transparent359 * * colorKey - transparent background color, pixels of that color will not be360 * used for rendering, it is useful for image with color format without361 * alpha channel, e.g. RGB565362 * * (scaleX, scaleY) - scale in percent from 1 to 100 along the corresponding363 * axis364 * * angle - clockwise rotation angle in degrees365 * * mirror - mirroring variant366 * * id - an ID of assets image to draw367 * See also368 * * GFX_POINT369 * * GFX_mirror370 * * GFX_cacheImages()371 * History372 * * v5.0 - added GFX_drawImageXY() alias, color argument clarified373 * * v6.0 - added scale parameters374 ******/375376/****n* PawnLibs/graphics/GFX_drawSubImage377 * Summary378 * Draw a sub window of an image from application assets at a specified379 * position and with given transformations.380 * Synopsis381 */382native GFX_drawSubImage(const center[GFX_POINT], winX, winY, winW, winH, opacity, colorKey, scaleX, scaleY, angle, GFX_mirror: mirror, id);383native GFX_drawSubImageXY(x, y, winX, winY, winW, winH, opacity, colorKey, scaleX, scaleY, angle, GFX_mirror: mirror, id);384/*385 * Inputs386 * * center or (x, y) - coordinates of the sub image center to draw387 * * (winX, winY, winW, winH) - coordinates of image sub window relative to388 * its top level corner389 * * opacity - opacity of the entire image, 0 is fully transparent390 * * colorKey - transparent background color, pixels of that color will not be391 * used for rendering, it is useful for image with color format without392 * alpha channel, e.g. RGB565393 * * (scaleX, scaleY) - scale in percent from 1 to 100 along the corresponding394 * axis395 * * angle - clockwise rotation angle in degrees396 * * mirror - mirroring variant397 * * id - an ID of assets image to draw sub window398 * See also399 * * GFX_POINT400 * * GFX_mirror401 * * GFX_cacheImages()402 * History403 * * v6.2 - added404 ******/405406/****n* PawnLibs/graphics/GFX_drawBakedImage407 * Summary408 * Draw a previously baked image at a specified position and with given409 * transformations.410 * Synopsis411 */412native GFX_drawBakedImage(const center[GFX_POINT], opacity, colorKey, scaleX, scaleY, angle, GFX_mirror: mirror, id);413native GFX_drawBakedImageXY(x, y, opacity, colorKey, scaleX, scaleY, angle, GFX_mirror: mirror, id);414/*415 * Inputs416 * * center or (x, y) - coordinates of the image center to draw417 * * opacity - opacity of the entire image, 0 is fully transparent418 * * colorKey - transparent background color, pixels of that color will not be419 * used for rendering, it is useful for image with color format without420 * alpha channel, e.g. RGB565421 * * (scaleX, scaleY) - scale in percent from 1 to 100 along the corresponding422 * axis423 * * angle - clockwise rotation angle in degrees424 * * mirror - mirroring variants425 * * id - an ID of the baked image to draw426 * See also427 * * GFX_POINT428 * * GFX_mirror429 * History430 * * v5.0 - added GFX_drawBakedImageXY() alias, color argument clarified431 * * v6.0 - added scale parameters432 ******/433434/****n* PawnLibs/graphics/GFX_setFillShader435 * Summary436 * Fill next primitives with specified color.437 * Synopsis438 */439native GFX_setFillShader(color);440/*441 * Inputs442 * * color - a color in ARGB8888 format to fill with443 * History444 * * v6.0 - added primitives shading445 ******/446447 /****n* PawnLibs/graphics/GFX_setLinearGradientShader448 * Summary449 * Fill next primitives with linear gradient from a given point / color to450 * another one.451 * Synopsis452 */453native GFX_setLinearGradientShader(const start[GFX_POINT], const end[GFX_POINT], color0, color1);454native GFX_setLinearGradientShaderXY(x0, y0, x1, y1, color0, color1);455/*456 * Inputs457 * * start or (xs, ys) - coordinates of the fill starting point458 * * end or (xe, ye) - coordinates of the fill ending point459 * * color0 - a color in ARGB8888 format to start fill with460 * * color1 - a color in ARGB8888 format to end fill with461 * History462 * * v6.0 - added primitives shading463 ******/464465 /****n* PawnLibs/graphics/GFX_setRadialGradientShader466 * Summary467 * Fill next primitives with radial gradient from a given point with radius468 * and from a given color to another one.469 * Synopsis470 */471native GFX_setRadialGradientShader(const center[GFX_POINT], radius, color0, color1);472native GFX_setRadialGradientShaderXY(x, y, radius, color0, color1);473/*474 * Inputs475 * * center or (x, y) - coordinates of the fill center476 * * radius - radius of the fill477 * * color0 - a color in ARGB8888 format to start fill with478 * * color1 - a color in ARGB8888 format to end fill with479 * History480 * * v6.0 - added primitives shading481 ******/482483/****n* PawnLibs/graphics/GFX_removeShader484 * Summary485 * Do not use shading in next primitives.486 * Synopsis487 */488native GFX_removeShader();489/*490 * Inputs491 * History492 * * v6.0 - added primitives shading493 ******/494495/****n* PawnLibs/graphics/GFX_drawQrCode496 * Summary497 * Generate and draw QR-code for a given text string.498 * Synopsis499 */500native GFX_drawQrCode(const center[GFX_POINT], size, color0, color1, angle, id, const text[]);501native GFX_drawQrCodeXY(x, y, size, color0, color1, angle, id, const text[]);502/*503 * Description504 * Supported version 2 which is limited by 32 characters of input.505 * Inputs506 * * center or (x, y) - coordinates of the resulting image of the QR-code to507 * draw508 * * size - size of the individual square representing a single bit in the509 * encodeded text string510 * * color0 - color of squares representing zeroes in the ARGB8888 format511 * * color1 - color of squares representing ones in the ARGB8888 format512 * * angle - clockwise rotation angle in degrees513 * * id - an ID of resulting image to distinguish different codes on the same514 scene, starts from 0515 * * text - text string to encode and draw, maximum size is 256 characters516 * See also517 * * GFX_POINT518 * History519 * * v6.0 - added520 ******/521522/****n* PawnLibs/graphics/GFX_render523 * Summary524 * Start rendering process.525 * Synopsis526 */527native GFX_render();528/*529 * See also530 * * GFX_setRenderTarget()531 * * GFX_bakeImage()532 ******/533534/****n* PawnLibs/graphics/GFX_cacheImages535 * Summary536 * Force platform to cache application images.537 * Synopsis538 */539native GFX_cacheImages(imageList[], size = sizeof(imageList));540/*541 * Description542 * Platform automatically reads and caches images from flash when the543 * application draws an image. Flash operations are synchronous and may cause544 * delays especially when reading large portions of data. To prevent the545 * application from lags it is possible to cache image data in advance.546 * However if application requests to draw a non-cached image and there is not547 * enough memory then oldest accessed images will be removed from cache. Also548 * there is a limit of 256 for a total number of cached images.549 * Inputs550 * * imageList - array with IDs of images to cache551 * * size - size of imageList array552 * Return value553 * Number of cached images or negative value if an error happened. Error554 * codes:555 * * -1 invalid function arguments count556 * * -2 invalid imageList array557 * See also558 * * GFX_clearCache()559 * History560 * * v5.0 - moved from appCtrl module to graphics561 ******/562563/****n* PawnLibs/graphics/GFX_clearCache564 * Summary565 * Clear all cached images.566 * Synopsis567 */568native GFX_clearCache();569/*570 * Description571 * Oldest accessed image cache will be automatically freed if there is not572 * enough memory to cache a new image. This function forces this process, e.g.573 * when switch game levels.574 * See also575 * * GFX_cacheImages()576 * * GFX_removeBakedImage()577 ******/578579/****n* PawnLibs/graphics/GFX_removeBakedImage580 * Summary581 * Remove specified baked image from cache.582 * Synopsis583 */584native GFX_removeBakedImage(id);585/*586 * Description587 * Baked images do not automatically invalidate. This function allows to588 * remove any baked image from cache to save some space.589 * Inputs590 * * id - an ID of the baked image to remove from cache591 * See also592 * * GFX_clearCache()593 * * GFX_bakeImage()594 * History595 * * v6.0 - added596 ******/597598/****n* PawnLibs/graphics/GFX_getAssetsCount599 * Summary600 * Get the total count of graphic assets.601 * Synopsis602 */603native GFX_getAssetsCount();604/*605 * Return value606 * Number of graphic assets.607 * History608 * * v6.0 - added609 ******/610611/****f* PawnLibs/graphics/GFX_fromARGB8888612 * Summary613 * Returns a hexadecimal value of a color constructed from the channel614 * components.615 * Synopsis616 */617stock GFX_fromARGB8888(a, r, g, b)618/*619 * Inputs620 * * a - the alpha channel, valid values are 0 through 255621 * * r - the red channel, valid values are 0 through 255622 * * g - the green channel, valid values are 0 through 255623 * * b - the blue channel, valid values are 0 through 255624 * Return value625 * The hexadecimal value of a color constructed from the channel components.626 * See also627 * * GFX_toARGB8888()628 * History629 * * v5.0 - created630 * Source631 */632{633 return (a & 0xFF) << 24 | (r & 0xFF) << 16 | (g & 0xFF) << 8 | (b & 0xFF);634}635/******/636637/****f* PawnLibs/graphics/GFX_toARGB8888638 * Summary639 * Returns channel values of a color in the hexadecimal format.640 * Synopsis641 */642stock GFX_toARGB8888(value, &a, &r, &g, &b)643/*644 * Inputs645 * * value - the hexadecimal value of the color646 * Outputs647 * * a - the alpha channel648 * * r - the red channel649 * * g - the green channel650 * * b - the blue channel651 * See also652 * * GFX_fromARGB8888653 * History654 * * v5.0 - created655 * Source656 */657{658 a = value & 0xFF000000;659 r = value & 0x00FF0000;660 g = value & 0x0000FF00;661 b = value & 0x000000FF;662}663/******/664665
Wrapped for easier reading. Turn wrap off to inspect exact line lengths.