1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
local class = require"common.class"
local chunk = require"common.chunk"
local Chunk = require"common.chunk".Chunk
local CHUNK_SIZE = require"common.constants".CHUNK_SIZE
local ChunkC = class.extend(chunk.Chunk)
function ChunkC.make(cls,...)
local self = chunk.Chunk.make(cls,...)
self.imesh = cls.imesh_from_chunk_data(self)
return self
end
local function tile_to_vattr(tile)
-- for now tiles are always numbers
-- so this just returns the input
-- this function might move to drawing.lua
-- once it does something less trivial
assert(type(tile) == "number",
"can't send non-numerical tile to gpu")
return tile
end
function ChunkC.imesh_from_chunk_data(the_chunk)
local mesh_data = {}
for q=0,CHUNK_SIZE-1 do
for r =0,CHUNK_SIZE-1 do
local t = the_chunk:_atqr(q,r)
table.insert(mesh_data,{tile_to_vattr(t)})
end
end
local imesh = love.graphics.newMesh({
{"tile_type","float",1}
}, mesh_data, nil, "dynamic")
return imesh
end
function ChunkC.set_at(self,hoffs,tile)
Chunk.set_at(self,hoffs,tile)
local idx = chunk.index(hoffs.q,hoffs.r)
self.imesh:setVertexAttribute(idx, 1, tile_to_vattr(tile))
end
return {ChunkC=ChunkC}
|