MCPcopy
hub / github.com/KilledByAPixel/LittleJS / drawTile

Function drawTile

src/engineDraw.js:277–351  ·  view source on GitHub ↗

Draw textured tile centered in world space * @param {Vector2} pos - Center of the tile in world space * @param {Vector2} [size=vec2(1)] - Size of the tile in world space * @param {TileInfo} [tileInfo] - Tile info to use, untextured if undefined * @param {Color} [color=WHITE] - Color to

(pos, size=vec2(1), tileInfo, color=WHITE,
    angle=0, mirror, additiveColor, useWebGL=glEnable, screenSpace=false, context)

Source from the content-addressed store, hash-verified

275 * @param {CanvasRenderingContext2D|OffscreenCanvasRenderingContext2D} [context] - Canvas 2D context to draw to
276 * @memberof Draw */
277function drawTile(pos, size=vec2(1), tileInfo, color=WHITE,
278 angle=0, mirror, additiveColor, useWebGL=glEnable, screenSpace=false, context)
279{
280 ASSERT(isVector2(pos), 'pos must be a vec2');
281 ASSERT(isVector2(size), 'size must be a vec2');
282 ASSERT(isColor(color), 'color is invalid');
283 ASSERT(isNumber(angle), 'angle must be a number');
284 ASSERT(!additiveColor || isColor(additiveColor), 'additiveColor must be a color');
285 ASSERT(!context || !useWebGL, 'context only supported in canvas 2D mode');
286
287 const textureInfo = tileInfo?.textureInfo;
288 const bleed = tileInfo?.bleed ?? 0;
289 if (useWebGL && glEnable)
290 {
291 ASSERT(!!glContext, 'WebGL is not enabled!');
292 if (screenSpace)
293 [pos, size, angle] = screenToWorldTransform(pos, size, angle);
294 if (textureInfo)
295 {
296 // calculate uvs and render
297 const sizeInverse = textureInfo.sizeInverse;
298 const x = tileInfo.pos.x * sizeInverse.x;
299 const y = tileInfo.pos.y * sizeInverse.y;
300 const w = tileInfo.size.x * sizeInverse.x;
301 const h = tileInfo.size.y * sizeInverse.y;
302 glSetTexture(textureInfo.glTexture);
303 if (bleed)
304 {
305 const bleedX = sizeInverse.x*bleed;
306 const bleedY = sizeInverse.y*bleed;
307 glDraw(pos.x, pos.y, mirror ? -size.x : size.x, size.y, angle,
308 x + bleedX, y + bleedY,
309 x - bleedX + w, y - bleedY + h,
310 color.rgbaInt(), additiveColor && additiveColor.rgbaInt());
311 }
312 else
313 {
314 glDraw(pos.x, pos.y, mirror ? -size.x : size.x, size.y, angle,
315 x, y, x + w, y + h,
316 color.rgbaInt(), additiveColor && additiveColor.rgbaInt());
317 }
318 }
319 else
320 {
321 // untextured: fold color+additive to match the Canvas2D path's
322 // color.add(additiveColor) on line ~337.
323 const combined = additiveColor ? color.add(additiveColor) : color;
324 glDrawUntextured(pos.x, pos.y, size.x, size.y, angle, combined.rgbaInt());
325 }
326 }
327 else
328 {
329 // normal canvas 2D rendering method (slower)
330 ++drawCount;
331 ++primitiveCount;
332 size = new Vector2(size.x, -size.y); // flip upside down sprites
333 drawCanvas2D(pos, size, angle, mirror, (context)=>
334 {

Callers 15

drawRectFunction · 0.85
drawTextScreenMethod · 0.85
drawMethod · 0.85
drawLayerTileMethod · 0.85
drawTileMethod · 0.85
renderMethod · 0.85
renderMethod · 0.85
drawNineSliceFunction · 0.85
drawThreeSliceFunction · 0.85
drawTileMethod · 0.85
gameRenderFunction · 0.85
gameRenderFunction · 0.85

Calls 14

vec2Function · 0.85
isVector2Function · 0.85
isColorFunction · 0.85
isNumberFunction · 0.85
screenToWorldTransformFunction · 0.85
glSetTextureFunction · 0.85
glDrawFunction · 0.85
glDrawUntexturedFunction · 0.85
drawCanvas2DFunction · 0.85
drawImageColorFunction · 0.85
rgbaIntMethod · 0.80
ASSERTFunction · 0.70

Tested by

no test coverage detected