Adding letters to a mesh

Hey guys. I’ve been working on a sort of tile game of late and I’ve come to a roadblock…I need to draw numbers on a square tile (45x45), centered. Currently I’m drawing a mesh for the square, then just a text(“#”, x,x)… You get the idea.

Is there a way that I can commit letters onto a mesh? A real simple way? Thanks a bunch guys.

Sprite the letters to an image and use that image as a texture for the mesh

self.xx = mesh()
    self.xx.vertices = { vec2(1,1), vec2(self.tileLength/2, self.tileLength/2), vec2(1, self.tileLength-1), vec2(self.tileLength-1,1), vec2(self.tileLength/2, self.tileLength/2), vec2(self.tileLength-1, self.tileLength-1)}
    self.xx:setColors(60,0,0)
    self.xx.texture = "1"

That doesn’t seem to work… How would i properly do it?

untested code

--in setup
letters={}
table.insert(letters, AddLetter("a"))
table.insert(letters, AddLetter("b"))
--etc

--in draw
translate(200,200)
char="b"
letters[char]:draw() --draws letter b

--put your own values where I've put @ below
function AddLetter(a) 
  s=20 --size of tile
  local img=image(s,s) --try 20?
  setContext(img) --draw to image
  background(@) --background tile colour
  fill(@) --text colour
  fontSize(@) --choose to fit tile size
  text(a,s/2,s/2)
  setContext()
  m=mesh()
  m:addRect(0,0,s,s) --set width,height
  m.texture=img
  return m
end

@invad3rZIM Here’s @Ignatz code, modified to show a larger range of the character set. Slide your finger up or down the screen to change the view.


displayMode(FULLSCREEN)

function setup()
    dy=0
    letters={}
    for z=33,126 do
        table.insert(letters, AddLetter(string.char(z)))
    end
end

function draw()  
    background(0)  
    for z=1,#letters do
        pushMatrix()
        translate(WIDTH/2,HEIGHT-z*50+dy)
        letters[z]:draw()
        popMatrix()
    end
end

function AddLetter(a) 
  s=45 --size of tile
  local img=image(s,s)
  setContext(img) --draw to image
  background(0, 185, 255, 255) --background tile colour
  fill(234, 32, 32, 255) --text colour
  fontSize(40) --choose to fit tile size
  text(a,s/2,s/2)
  setContext()
  m=mesh()
  m:addRect(0,0,s,s) --set width,height
  m.texture=img
  return m
end

function touched(t)
    if t.state==MOVING then
        dy=dy+t.deltaY
    end
end