51c8d7696356 — Leonard Ritter 3 months ago
spritepainter uses registered size
2 files changed, 30 insertions(+), 26 deletions(-)

M testing/test_spritepainter.sc
M tukan/SpritePainter.sc
M testing/test_spritepainter.sc +10 -20
@@ 28,8 28,8 @@ va-map
     inline (rc)
         'register painter rc sz
     ivec4 0 0 5 8
-    ivec4 6 0 (6 + 4) 8
-    ivec4 10 0 (10 + 5) 8
+    ivec4 6 0 10 8
+    ivec4 10 0 15 8
 
 @@ 'on GLMain.on-draw
 fn draw (time size glmain)

          
@@ 44,24 44,14 @@ fn draw (time size glmain)
 
     t += (/ 60.0)
 
-    let invsz = (8 * (/ 2 (vec2 size)))
-    painter.transform =
-        mat4
-            \ invsz.x 0 0 0
-            \ 0 invsz.y 0 0
-            \ 0 0 1 0
-            \ -1 -1 0 1
-
-    from (methodsof painter) let sprite
-    sprite 0 (vec2 0 0)
-        vec2 5 8
-        #angle = t
-    sprite 1 (vec2 6 0)
-        vec2 4 8
-        #angle = t
-    sprite 2 (vec2 11 0)
-        vec2 5 8
-        #angle = t
+    from (methodsof painter) let sprite screen
+    screen size 8
+    sprite 0 (vec2 5 40) (vec4 -0.5 -0.5 0.5 0.5)
+        angle = t
+    sprite 1 (vec2 11 40) (vec4 -0.5 -0.5 0.5 0.5)
+        angle = t
+    sprite 2 (vec2 17 40) (vec4 -0.5 -0.5 0.5 0.5)
+        angle = t
     GL.BindTextureUnit 0 texture_spritesheet
     'draw painter
 

          
M tukan/SpritePainter.sc +20 -6
@@ 14,6 14,8 @@ using import .stage
 struct Sprite plain
     # uv coordinates within texture (0..1)
     rect : vec4
+    # size in screen units
+    size : vec2
 
 # 4 kilobytes
 #let ColorCount = 256

          
@@ 67,8 69,8 @@ let shaders =
             angle := (deref cmd.angle)
             sprite := b_sprites.elements @ cmd.sprite
             uv := (deref sprite.rect)
+            size := (deref sprite.size)
             let vxid = (deref gl_VertexID)
-
             let rotmtx =
                 do
                     let ca sa = (cos angle) (sin angle)

          
@@ 88,7 90,7 @@ let shaders =
             io_uv.out = uv
             gl_Position =
                 u_transform *
-                    vec4 (origin + rotmtx * pos) 0 1
+                    vec4 (origin + rotmtx * (pos * size)) 0 1
 
         uniform u_smp : sampler2D
             location = SamplerLocation

          
@@ 147,19 149,22 @@ struct SpritePainter
     inline __typecall (cls)
         create;
 
-    fn... register (self, rect : vec4)
+    fn... register (self, rect : vec4, size : vec2)
         'append self.sprites
             Sprite
                 rect = rect
-    case (self, rect : ivec4, size : ivec2)
+                size = size
+    case (self, rect : ivec4, texturesize : ivec2)
+        rect := (vec4 rect)
         this-function self
-            (vec4 rect) / (vec2 size) . xyxy
+            rect / ((vec2 texturesize) . xyxy)
+            rect.zw - rect.xy
 
     fn invalidate (self)
         self.sprites_dirty = true
 
     fn... sprite
-    case (self, id : u32, origin : vec2, size : f32, angle : f32 = 0.0)
+    case (self, id : u32, origin : vec2, size : f32 = 1.0, angle : f32 = 0.0)
         this-function self id origin (vec4 0 0 size size) angle
     case (self, id : u32, origin : vec2, size : vec2, angle : f32 = 0.0)
         this-function self id origin (vec4 0 0 size) angle

          
@@ 171,6 176,15 @@ struct SpritePainter
                 angle = angle
                 sprite = id
 
+    fn... screen (self, size : ivec2, scale : f32 = 1.0)
+        let invsz = (scale * (/ 2 (vec2 size)))
+        self.transform =
+            mat4
+                \ invsz.x 0 0 0
+                \ 0 invsz.y 0 0
+                \ 0 0 1 0
+                \ -1 -1 0 1
+
     fn draw (self)
         if (empty? self.commands)
             return;