浏览代码

fix core/image error.

SakiKawasaki 4 年之前
父节点
当前提交
649524fc12
共有 3 个文件被更改,包括 23 次插入16 次删除
  1. 9 11
      src/nodesnim/core/image.nim
  2. 13 3
      src/nodesnim/nodescontrol/texture_rect.nim
  3. 1 2
      src/nodesnim/window.nim

+ 9 - 11
src/nodesnim/core/image.nim

@@ -9,9 +9,8 @@ import
 
 when defined(debug):
   import logging
-  debug "result of `init()` is ", image.init()
-else:
-  discard image.init()
+
+discard image.init()
 
 
 type
@@ -20,7 +19,7 @@ type
     size*: Vector2Ref
 
 
-proc load*(file: cstring, size: var Vector2Ref, mode: Glenum = GL_RGB): Gluint =
+proc load*(file: cstring, x, y: var float, mode: Glenum = GL_RGB): Gluint =
   ## Loads image from file and returns texture ID.
   ##
   ## Arguments:
@@ -31,10 +30,8 @@ proc load*(file: cstring, size: var Vector2Ref, mode: Glenum = GL_RGB): Gluint =
   when defined(debug):
     if surface == nil:
       error("image \"", file, "\" not loaded!")
-    elif size == nil:
-      error("size is nil! maybe you mean: var size = Vector2()?")
-  size.x = surface.w.float
-  size.y = surface.h.float
+  x = surface.w.float
+  y = surface.h.float
 
 
   # OpenGL:
@@ -61,7 +58,8 @@ proc load*(file: cstring, mode: Glenum = GL_RGB): GlTextureObj =
   ## Arguments:
   ## - `file` - image path.
   var
-    size: Vector2Ref = Vector2Ref()
+    x: float = 0f
+    y: float = 0f
     textureid: Gluint
-  textureid = load(file, size, mode)
-  GlTextureObj(texture: textureid, size: size)
+  textureid = load(file, x, y, mode)
+  GlTextureObj(texture: textureid, size: Vector2(x, y))

+ 13 - 3
src/nodesnim/nodescontrol/texture_rect.nim

@@ -103,6 +103,14 @@ method draw*(self: TextureRectPtr, w, h: GLfloat) =
       glTexCoord2f(1, 0)
       glVertex2f(x2, y1)
     elif self.texture_mode == TEXTURE_CROP:
+      if self.texture_size.x < self.rect_size.x:
+        let q = self.rect_size.x / self.texture_size.x
+        self.texture_size.x *= q
+        self.texture_size.y *= q
+      if self.texture_size.y < self.rect_size.y:
+        let q = self.rect_size.y / self.texture_size.y
+        self.texture_size.x *= q
+        self.texture_size.y *= q
       let
         x1 = self.rect_size.x / self.texture_size.x
         y1 = self.rect_size.y / self.texture_size.y
@@ -135,9 +143,11 @@ method loadTexture*(self: TextureRectPtr, file: cstring) {.base.} =
   ##
   ## Arguments:
   ## - `file` is an image file path.
-  var size: Vector2Ref = Vector2Ref()
-  self.texture = load(file, size)
-  self.texture_size = size
+  var
+    x: float = 0f
+    y: float = 0f
+  self.texture = load(file, x, y)
+  self.texture_size = Vector2(x, y)
 
 method setTexture*(self: TextureRectPtr, gltexture: GlTextureObj) {.base.} =
   ## Changes texture.

+ 1 - 2
src/nodesnim/window.nim

@@ -11,8 +11,7 @@ import
   nodes/scene,
 
   environment,
-  os,
-  unicode
+  os
 
 when defined(debug):
   import logging