Browse Source

add new example, bug fixs.

Ethosa 4 years ago
parent
commit
86aa95a341

BIN
examples/novel/assets/night.jpg


BIN
examples/novel/assets/sharp.jpg


+ 102 - 0
examples/novel/main.nim

@@ -0,0 +1,102 @@
+# main scene
+import nodesnim
+
+Window("Novel game", 1280, 720)
+
+var
+  main_obj: SceneObj
+  main = Scene("Main", main_obj)
+
+  button_obj: ButtonObj
+  button = Button("New game", button_obj)
+
+
+  # Game scene
+  game_scene_obj: SceneObj
+  game_scene = Scene("Game", game_scene_obj)
+
+  # Backgrounds:
+  night = load("assets/night.jpg")
+
+  # Charapters:
+  akiko_default = load("assets/sharp.jpg")
+
+  name_charapter_obj: LabelObj
+  name_charapter = Label(name_charapter_obj)
+
+  dialog_text_obj: RichLabelObj
+  dialog_text = RichLabel(dialog_text_obj)
+
+  background_image_obj: TextureRectObj
+  background_image = TextureRect(background_image_obj)
+
+  foreground_rect_obj: ColorRectObj
+  foreground_rect = ColorRect(foreground_rect_obj)
+
+  charapter_obj: TextureRectObj
+  charapter = TextureRect("Charapter", charapter_obj)
+
+  dialog = @[
+    ("Me", "H-Hey .. ?", false),
+    ("Eileen", "NANI??????", true)
+  ]
+  stage = -1
+
+
+game_scene.addChild(background_image)
+background_image.setSizeAnchor(1, 1)
+background_image.setTexture(night)
+background_image.setTextureAnchor(0.5, 0.5, 0.5, 0.5)
+background_image.texture_mode = TEXTURE_KEEP_ASPECT_RATIO
+
+game_scene.addChild(charapter)
+charapter.setSizeAnchor(1, 1)
+charapter.setTexture(akiko_default)
+charapter.setTextureAnchor(0.5, 0.5, 0.5, 0.5)
+charapter.texture_mode = TEXTURE_KEEP_ASPECT_RATIO
+charapter.visible = false
+
+game_scene.addChild(dialog_text)
+dialog_text.setSizeAnchor(0.8, 0.3)
+dialog_text.setAnchor(0.1, 0.6, 0, 0)
+dialog_text.setBackgroundColor(Color(0x0e131760))
+
+dialog_text.addChild(name_charapter)
+name_charapter.resize(128, 32)
+name_charapter.setAnchor(0, 0, 0, 1)
+name_charapter.setBackgroundColor(Color(0x0e131760))
+name_charapter.setTextAlign(0.1, 0.5, 0.1, 0.5)
+
+game_scene.addChild(foreground_rect)
+foreground_rect.setSizeAnchor(1, 1)
+foreground_rect.ready =
+  proc() =
+    foreground_rect.color = Color(0x0e1317ff)
+foreground_rect.process =
+  proc() =
+    if foreground_rect.color.a > 0f:
+      foreground_rect.color.a -= 0.001
+foreground_rect.input =
+  proc(event: InputEvent) =
+    if event.isInputEventMouseButton() and not event.pressed:
+      inc stage
+      if stage < dialog.len():
+        name_charapter.setText(dialog[stage][0])
+        dialog_text.setText(clrtext(dialog[stage][1]))
+        charapter.visible = dialog[stage][2]
+
+
+
+main.addChild(button)
+button.text = "New game"
+button.resize(128, 32)
+button.setAnchor(0.5, 0.5, 0.5, 0.5)
+button.on_click =
+  proc(x, y: float) =
+    changeScene("Game")
+
+
+addScene(main)
+addScene(game_scene)
+setMainScene("Main")
+windowLaunch()

+ 1 - 0
examples/novel/nim.cfg

@@ -0,0 +1 @@
+--path:"../../src"

BIN
examples/novel/one.gif


+ 5 - 0
examples/novel/readme.md

@@ -0,0 +1,5 @@
+# Novel game!
+
+See [`main.nim`](https://github.com/Ethosa/nodesnim/blob/master/examples/novel/main.nim) file.
+
+![gif here](https://github.com/Ethosa/nodesnim/blob/master/examples/novel/one.gif)

+ 1 - 0
examples/readme.md

@@ -2,3 +2,4 @@
 
 1. [Hello world](https://github.com/Ethosa/nodesnim/blob/master/examples/hello_world)
 2. [Calculator](https://github.com/Ethosa/nodesnim/blob/master/examples/calculator)
+3. [Novel](https://github.com/Ethosa/nodesnim/blob/master/examples/novel)

+ 5 - 1
src/nodesnim/core/image.nim

@@ -46,8 +46,12 @@ proc load*(file: cstring, size: var Vector2Ref): Gluint =
 
 
 proc load*(file: cstring): GlTexture =
+  ## Loads GL texture.
+  ##
+  ## Arguments:
+  ## - `file` - image path.
   var
-    size: Vector2Ref
+    size: Vector2Ref = Vector2Ref()
     textureid: Gluint
   textureid = load(file, size)
   GlTexture(texture: textureid, size: size)

+ 1 - 2
src/nodesnim/nodes/scene.nim

@@ -67,5 +67,4 @@ method reAnchorScene*(scene: ScenePtr, w, h: GLfloat, paused: bool) {.base.} =
   for child in scene.getChildIter():
     if paused and child.getPauseMode() != PROCESS:
       continue
-    if child.visible:
-      child.calcPositionAnchor()
+    child.calcPositionAnchor()

+ 3 - 0
src/nodesnim/nodescontrol/edittext.nim

@@ -165,3 +165,6 @@ method setTextAlign*(self: EditTextPtr, align: AnchorRef) {.base.} =
 method setTextAlign*(self: EditTextPtr, x1, y1, x2, y2: float) {.base.} =
   ## Changes text align.
   self.text_align = Anchor(x1, y1, x2, y2)
+
+method setText*(self: EditTextPtr, value: string) {.base.} =
+  self.text = value

+ 3 - 0
src/nodesnim/nodescontrol/label.nim

@@ -97,3 +97,6 @@ method setTextAlign*(self: LabelPtr, align: AnchorRef) {.base.} =
 
 method setTextAlign*(self: LabelPtr, x1, y1, x2, y2: float) {.base.} =
   self.text_align = Anchor(x1, y1, x2, y2)
+
+method setText*(self: LabelPtr, value: string) {.base.} =
+  self.text = value

+ 3 - 0
src/nodesnim/nodescontrol/rich_edit_text.nim

@@ -156,3 +156,6 @@ method setTextAlign*(self: RichEditTextPtr, align: AnchorRef) {.base.} =
 method setTextAlign*(self: RichEditTextPtr, x1, y1, x2, y2: float) {.base.} =
   ## Changes text align.
   self.text_align = Anchor(x1, y1, x2, y2)
+
+method setText*(self: RichEditTextPtr, value: ColorTextRef) {.base.} =
+  self.text = value

+ 3 - 0
src/nodesnim/nodescontrol/rich_label.nim

@@ -95,3 +95,6 @@ method setTextAlign*(self: RichLabelPtr, align: AnchorRef) {.base.} =
 
 method setTextAlign*(self: RichLabelPtr, x1, y1, x2, y2: float) {.base.} =
   self.text_align = Anchor(x1, y1, x2, y2)
+
+method setText*(self: RichLabelPtr, value: ColorTextRef) {.base.} =
+  self.text = value

+ 12 - 0
src/nodesnim/nodescontrol/texture_rect.nim

@@ -20,6 +20,7 @@ type
     texture_mode*: TextureMode
     texture_size*: Vector2Ref
     texture_anchor*: AnchorRef
+    texture_filter*: ColorRef
   TextureRectPtr* = ptr TextureRectObj
 
 
@@ -32,6 +33,7 @@ proc TextureRect*(name: string, variable: var TextureRectObj): TextureRectPtr =
   variable.texture_mode = TEXTURE_FILL_XY
   variable.texture_size = Vector2()
   variable.texture_anchor = Anchor(0, 0, 0, 0)
+  variable.texture_filter = Color(1f, 1f, 1f)
 
 proc TextureRect*(obj: var TextureRectObj): TextureRectPtr {.inline.} =
   TextureRect("TextureRect", obj)
@@ -46,6 +48,7 @@ method draw*(self: TextureRectPtr, w, h: GLfloat) =
 
   glColor4f(self.background_color.r, self.background_color.g, self.background_color.b, self.background_color.a)
   glRectf(x, y, x+self.rect_size.x, y-self.rect_size.y)
+  glColor4f(self.texture_filter.r, self.texture_filter.g, self.texture_filter.b, self.texture_filter.a)
 
 
   if self.texture > 0:
@@ -114,3 +117,12 @@ method loadTexture*(self: TextureRectPtr, file: cstring) {.base.} =
 method setTexture*(self: TextureRectPtr, gltexture: GlTexture) {.base.} =
   self.texture = gltexture.texture
   self.texture_size = gltexture.size
+
+method setTextureFilter*(self: TextureRectPtr, color: ColorRef) {.base.} =
+  self.texture_filter = color
+
+method setTextureAnchor*(self: TextureRectPtr, anchor: AnchorRef) {.base.} =
+  self.texture_anchor = anchor
+
+method setTextureAnchor*(self: TextureRectPtr, x1, y1, x2, y2: float) {.base.} =
+  self.texture_anchor = Anchor(x1, y1, x2, y2)

+ 4 - 1
tests/test8.nim

@@ -13,8 +13,11 @@ var
 
 main.addChild(texturerect)
 
-texturerect.loadTexture("assets/sharp.jpg")  # Load image from file.
+var texture = load("assets/sharp.jpg")  # Load image from file.
+texturerect.setTexture(texture)
 texturerect.resize(256, 256)
+texturerect.setBackgroundColor(Color(1, 0.6, 1, 0.6))
+texturerect.setTextureFilter(Color(1f, 1f, 1f))
 texturerect.texture_mode = TEXTURE_KEEP_ASPECT_RATIO  # Can be also TEXTURE_CROP or TEXTURE_FILL_XY
 texturerect.texture_anchor = Anchor(0.5, 1, 0.5, 1)