gamnit: CheckerTexture with custom size and move up implementation
authorAlexis Laferrière <alexis.laf@xymus.net>
Fri, 9 Jun 2017 14:47:46 +0000 (10:47 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Mon, 12 Jun 2017 18:39:11 +0000 (14:39 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/gamnit/textures.nit

index 6b47788..ad176a5 100644 (file)
@@ -98,24 +98,18 @@ abstract class Texture
        fun offset_bottom: Float do return 1.0
 end
 
-# Colorful small texture of 2x2 pixels
+# Colorful small texture of 32x32 pixels by default
 class CheckerTexture
        super RootTexture
 
+       # Width and height in pixels, defaults to 32
+       var size = 32 is optional
+
        redef fun load(force)
        do
-               var pixels = [0xFFu8, 0x00u8, 0x00u8,
-                             0x00u8, 0xFFu8, 0x00u8,
-                             0x00u8, 0x00u8, 0xFFu8,
-                             0xFFu8, 0xFFu8, 0xFFu8]
-
-               var cpixels = new CByteArray.from(pixels)
-
-               width = 2.0
-               height = 2.0
-               load_from_pixels(cpixels.native_array, 2, 2, gl_RGB)
-
-               cpixels.destroy
+               if gl_texture != -1 then return
+               load_checker size
+               loaded = true
        end
 end
 
@@ -151,6 +145,31 @@ class RootTexture
                glGenerateMipmap(gl_TEXTURE_2D)
        end
 
+       private fun load_checker(size: Int)
+       do
+               var cpixels = new CByteArray(3*size*size)
+
+               var i = 0
+               for x in [0..size[ do
+                       var quadrant_x = if x < size/2 then 0 else 1
+                       for y in [0..size[ do
+                               var quadrant_y = if y < size/2 then 0 else 1
+                               var color = if quadrant_x == quadrant_y then
+                                       [0u8, 0u8, 0u8, 255u8]
+                               else [255u8, 255u8, 255u8, 255u8]
+
+                               for j in [0..3[ do cpixels[i+j] = color[j]
+                               i += 3
+                       end
+               end
+
+               width = size.to_f
+               height = size.to_f
+               load_from_pixels(cpixels.native_array, size, size, gl_RGB)
+
+               cpixels.destroy
+       end
+
        # Set whether this texture should be pixelated when drawn, otherwise it is interpolated
        fun pixelated=(pixelated: Bool)
        do