Git Product home page Git Product logo

nim-tiled's Introduction

Nim Tiled

nimble

Introduction

A tiled map loader for the Nim programming language. The Tiled map editor can be found here. Documentation for the tiled file format can be found here.

Example

echo "Loaded the tiled map: ", loadTiledMap("tilemap.tmx").orDefault

Example with error handling

let res = loadTiledMap("tilemap.tmx")

if res.isOk:
  echo "Loaded the tiled map: ", res.tiledMap

Documentation

โ€‹ Generate documentation by running the nimble docs command

Example using Windy, Pixie and Boxy

Infinite Tilemap demo

Infinite Tilemap demo

import windy, boxy, pixie, opengl, options, os, nim_tiled

when isMainModule:
  var window = newWindow("Tiled Map Demo!", ivec2(640, 640))
  window.makeContextCurrent()
  loadExtensions()

  var bxy = newBoxy()

  let
    tiledMap = loadTiledMap("res/TestArea.tmx").orDefault
    tileset = tiledMap.tilesets[0]
    tilesetImage = readImage("res".joinPath tileset.image.get().source)

  proc renderTile(key: string, gid: Tile) =
    if not bxy.contains(key):
      let
        img = newImage(tileset.tilewidth, tileset.tileheight)
        ctx = newContext(img)
        tw = tileset.tilewidth.float
        th = tileset.tileheight.float
        rx = (gid mod tileset.columns).float * tw
        ry = (gid.float / tileset.columns.float).int.float * th

      ctx.drawImage(tilesetImage, rect(vec2(rx.float, ry.float), vec2(tw, th)),
          rect(vec2(), vec2(tw, th)))

      bxy.addImage(key, img)

  while not window.closeRequested:
    glClear(GL_COLOR_BUFFER_BIT)
    bxy.beginFrame(window.size)

    for layer in tiledMap.layers:
      if layer.kind == tiles:
        for chunk in layer.data.chunks:
          for x in 0..<chunk.width.int:
            for y in 0..<chunk.height.int:
              let
                tw = tileset.tilewidth.float
                th = tileset.tileheight.float
                gid = chunk.tiles[x + y * chunk.width.int]
                key = "tile-" & $gid

              if gid > 0:
                renderTile(key, gid - 1)
                bxy.drawImage(key, pos = vec2(200.0, 128.0) + vec2(((
                    chunk.x.int + x) * tw.int).float, ((chunk.y.int + y) *
                        th.int).float))

    bxy.endFrame()
    window.swapBuffers()
    pollEvents()

Preview

nim-tiled's People

Contributors

enthus1ast avatar exelotl avatar skyvault avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

nim-tiled's Issues

Tileset has no accessor for `.tiles`

Tileset.tiles is private and there is no proc to access its elements, leaving no way for users to access a TilesetTile object inside a TileSet

Infinite map support

This shouldn't be too hard, though it would require a change in how layers are used.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.