From: Alexis Laferrière Date: Mon, 1 Feb 2016 16:47:22 +0000 (-0500) Subject: lib/gamnit: intro `Sprite::invert_x` X-Git-Url: http://nitlanguage.org lib/gamnit: intro `Sprite::invert_x` Signed-off-by: Alexis Laferrière --- diff --git a/lib/gamnit/flat.nit b/lib/gamnit/flat.nit index 6f9dea4..06ac8df 100644 --- a/lib/gamnit/flat.nit +++ b/lib/gamnit/flat.nit @@ -55,6 +55,9 @@ class Sprite # Rotation on the Z axis var rotation = 0.0 is writable + # Mirror `texture` horizontally, inverting each pixel on the X axis + var invert_x = false is writable + # Scale applied to this sprite var scale = 1.0 is writable @@ -86,7 +89,10 @@ class Sprite simple_2d_program.use_texture.uniform true simple_2d_program.texture.uniform 0 - simple_2d_program.tex_coord.array(texture.texture_coords, 2) + simple_2d_program.tex_coord.array( + if invert_x then + texture.texture_coords_invert_x + else texture.texture_coords, 2) simple_2d_program.coord.array(texture.vertices, 3) simple_2d_program.rotation.uniform new Matrix.rotation(rotation, 0.0, 0.0, 1.0) @@ -295,6 +301,18 @@ redef class Texture for v in [c, d, a, b] do texture_coords.add_all v return texture_coords end + + # Coordinates of this texture on the `root` texture, with the X axis inverted + private var texture_coords_invert_x: Array[Float] is lazy do + var a = [offset_left, offset_bottom] + var b = [offset_right, offset_bottom] + var c = [offset_left, offset_top] + var d = [offset_right, offset_top] + + var texture_coords = new Array[Float] + for v in [d, c, b, a] do texture_coords.add_all v + return texture_coords + end end # Graphic program to display simple models with a texture, translation, rotation and scale