Ver código fonte

add texture mode and texture anchor.

Ethosa 5 anos atrás
pai
commit
da9b39a497

+ 1 - 1
src/nodesnim/core/enums.nim

@@ -7,6 +7,6 @@ type
   PauseMode* {.size: sizeof(int8).} = enum
     PROCESS, PAUSE, INHERIT
   TextureMode* {.size: sizeof(int8).} = enum
-    TEXTURE_FULL_SIZE,
+    TEXTURE_FILL_XY,
     TEXTURE_KEEP_ASPECT_RATIO,
     TEXTURE_CROP

+ 16 - 4
src/nodesnim/core/image.nim

@@ -2,16 +2,24 @@
 import
   ../thirdparty/opengl,
   ../thirdparty/sdl2,
-  ../thirdparty/sdl2/image
+  ../thirdparty/sdl2/image,
+
+  ../core/vector2
 
 
 discard image.init()
 
 
-proc load*(file: cstring): Gluint =
+proc load*(file: cstring, size: var Vector2Ref): Gluint =
+  ## Loads image from file and returns texture ID.
+  ##
+  ## Arguments:
+  ## - `file` - image path.
   var
-    surface = image.load(file)
+    surface = image.load(file)  # load image from file
     textureid: Gluint
+  size.x = surface.w.float
+  size.y = surface.h.float
 
   # OpenGL:
   glGenTextures(1, textureid.addr)
@@ -23,5 +31,9 @@ proc load*(file: cstring): Gluint =
 
   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB.GLint, surface.w,  surface.h, 0, GL_RGB, GL_UNSIGNED_BYTE, surface.pixels)
   glBindTexture(GL_TEXTURE_2D, 0)
+
+  # free memory
   surface.freeSurface()
-  return textureid
+  surface = nil
+
+  textureid

+ 53 - 11
src/nodesnim/nodescontrol/texture_rect.nim

@@ -7,6 +7,7 @@ import
   ../core/anchor,
   ../core/input,
   ../core/image,
+  ../core/enums,
   ../core/color,
 
   ../nodes/node,
@@ -16,7 +17,9 @@ import
 type
   TextureRectObj* = object of ControlPtr
     texture*: Gluint
-    modulate*: ColorRef
+    texture_mode*: TextureMode
+    texture_size*: Vector2Ref
+    texture_anchor*: AnchorRef
   TextureRectPtr* = ptr TextureRectObj
 
 
@@ -26,12 +29,15 @@ proc TextureRect*(name: string, variable: var TextureRectObj): TextureRectPtr =
   variable.rect_size.x = 40
   variable.rect_size.y = 40
   variable.texture = 0
-  variable.modulate = Color(1, 1, 1)
+  variable.texture_mode = TEXTURE_FILL_XY
+  variable.texture_size = Vector2()
+  variable.texture_anchor = Anchor(0, 0, 0, 0)
 
 proc TextureRect*(obj: var TextureRectObj): TextureRectPtr {.inline.} =
   TextureRect("TextureRect", obj)
 
 
+
 method draw*(self: TextureRectPtr, w, h: GLfloat) =
   self.calcGlobalPosition()
   let
@@ -43,14 +49,48 @@ method draw*(self: TextureRectPtr, w, h: GLfloat) =
     glBindTexture(GL_TEXTURE_2D, self.texture)
 
     glBegin(GL_QUADS)
-    glTexCoord2f(0, 0)
-    glVertex2f(x, y)
-    glTexCoord2f(0, 1)
-    glVertex2f(x, y - self.rect_size.y)
-    glTexCoord2f(1, 1)
-    glVertex2f(x + self.rect_size.x, y - self.rect_size.y)
-    glTexCoord2f(1, 0)
-    glVertex2f(x + self.rect_size.x, y)
+    if self.texture_mode == TEXTURE_FILL_XY:
+      glTexCoord2f(0, 0)
+      glVertex2f(x, y)
+      glTexCoord2f(0, 1)
+      glVertex2f(x, y - self.rect_size.y)
+      glTexCoord2f(1, 1)
+      glVertex2f(x + self.rect_size.x, y - self.rect_size.y)
+      glTexCoord2f(1, 0)
+      glVertex2f(x + self.rect_size.x, y)
+    elif self.texture_mode == TEXTURE_KEEP_ASPECT_RATIO:
+      let
+        w = self.rect_size.x / self.texture_size.x
+        h = self.rect_size.y / self.texture_size.y
+        q = if w < h: w else: h
+        x1 = x + (self.rect_size.x*self.texture_anchor.x1) - (self.texture_size.x*q)*self.texture_anchor.x2
+        y1 = y - (self.rect_size.y*self.texture_anchor.y1) + (self.texture_size.y*q)*self.texture_anchor.y2
+        x2 = x1 + self.texture_size.x*q
+        y2 = y1 - self.texture_size.y*q
+      glTexCoord2f(0, 0)
+      glVertex2f(x1, y1)
+      glTexCoord2f(0, 1)
+      glVertex2f(x1, y2)
+      glTexCoord2f(1, 1)
+      glVertex2f(x2, y2)
+      glTexCoord2f(1, 0)
+      glVertex2f(x2, y1)
+    elif self.texture_mode == TEXTURE_CROP:
+      let
+        x1 = self.rect_size.x / self.texture_size.x
+        y1 = self.rect_size.y / self.texture_size.y
+        x2 = normalize(self.texture_anchor.x1 - x1*self.texture_anchor.x2, 0, 1)
+        y2 = normalize(self.texture_anchor.y1 - y1*self.texture_anchor.y2, 0, 1)
+        x3 = normalize(x2 + x1, 0, 1)
+        y3 = normalize(y2 + y1, 0, 1)
+      glTexCoord2f(x2, y2)
+      glVertex2f(x, y)
+      glTexCoord2f(x2, y3)
+      glVertex2f(x, y - self.rect_size.y)
+      glTexCoord2f(x3, y3)
+      glVertex2f(x + self.rect_size.x, y - self.rect_size.y)
+      glTexCoord2f(x3, y2)
+      glVertex2f(x + self.rect_size.x, y)
     glEnd()
     glDisable(GL_TEXTURE_2D)
 
@@ -59,4 +99,6 @@ method draw*(self: TextureRectPtr, w, h: GLfloat) =
     self.press(last_event.x, last_event.y)
 
 method loadTexture*(self: TextureRectPtr, file: cstring) {.base.} =
-  self.texture = load(file)
+  var size: Vector2Ref = Vector2Ref()
+  self.texture = load(file, size)
+  self.texture_size = size

+ 4 - 2
tests/test8.nim

@@ -13,8 +13,10 @@ var
 
 main.addChild(texturerect)
 
-texturerect.loadTexture("assets/sharp.jpg")
-texturerect.resize(640, 360)
+texturerect.loadTexture("assets/sharp.jpg")  # Load image from file.
+texturerect.resize(256, 256)
+texturerect.texture_mode = TEXTURE_CROP
+texturerect.texture_anchor = Anchor(0.5, 1, 0.5, 1)
 
 
 addScene(main)