diff options
author | ubq323 <ubq323@ubq323.website> | 2023-01-29 02:59:50 +0000 |
---|---|---|
committer | ubq323 <ubq323@ubq323.website> | 2023-01-29 02:59:50 +0000 |
commit | ebe12a65c07bccfbc704667c88d8125be33067cf (patch) | |
tree | 9c1ef457ce0da96b4d5dbe77cd56625f53b4ba89 | |
parent | 9b659149c7273a57a82c9be988a8a78c62bb23f4 (diff) |
camera stuff + vastly optimise hex drawing
-rw-r--r-- | client/camera.lua | 14 | ||||
-rw-r--r-- | client/drawing.lua | 58 | ||||
-rw-r--r-- | client/main.lua | 34 | ||||
-rw-r--r-- | client/profile.lua | 2 | ||||
-rw-r--r-- | common/chunk.lua | 5 |
5 files changed, 93 insertions, 20 deletions
diff --git a/client/camera.lua b/client/camera.lua index bc65044..6309e38 100644 --- a/client/camera.lua +++ b/client/camera.lua @@ -21,5 +21,19 @@ function Camera.apply_trans(self) love.graphics.translate(-self.pos.x,-self.pos.y) end +local screen_offset = coords.Pos.make(screen_width/2,screen_height/2) +function Camera.screen_to_world(self,pos) + return (pos-screen_offset)/self.zoom + self.pos +end +function Camera.world_to_screen(self,pos) + return (pos-self.pos) * self.zoom + screen_offset +end + +function Camera.extents(self) + -- returns top left and bottom right pos's in world coords + return self:screen_to_world(coords.Pos.make(0,0)), + self:screen_to_world(coords.Pos.make(screen_width,screen_height)) +end + return {Camera=Camera} diff --git a/client/drawing.lua b/client/drawing.lua index 29069e7..21513cb 100644 --- a/client/drawing.lua +++ b/client/drawing.lua @@ -13,29 +13,57 @@ for i=0,5 do table.insert(corners,y) end -local function draw_hex(cpos,color,thick) - love.graphics.push() - love.graphics.setLineWidth(thick or 0.1) - love.graphics.translate(cpos.x,cpos.y) - if color then love.graphics.setColor(color) else - love.graphics.setColor(love.math.colorFromBytes(0xe7,0x9e,0)) +-- local function draw_hex(cpos,color) +-- love.graphics.push() +-- love.graphics.setLineWidth(0.1) +-- love.graphics.translate(cpos.x,cpos.y) +-- if color then love.graphics.setColor(color) else +-- love.graphics.setColor(love.math.colorFromBytes(0xe7,0x9e,0)) +-- end +-- love.graphics.polygon("fill",corners) +-- love.graphics.setColor(0,0,0) +-- love.graphics.polygon("line",corners) +-- love.graphics.pop() +-- end + +local _corners = {} +local function draw_hex(cpos,color) + local cx,cy = cpos.x,cpos.y + for i=0,5 do + local angle = tau*(i+0.5)/6 + local x = cx + math.cos(angle) + local y = cy + math.sin(angle) + _corners[2*i+1] = x + _corners[2*i+2] = y end - love.graphics.polygon("fill",corners) + love.graphics.setLineWidth(0.1) + -- love.graphics.setColor(love.math.colorFromBytes(0xe7,0x9e,0)) + love.graphics.setColor(color or {0.91,0.62,0}) + love.graphics.polygon("fill",_corners) love.graphics.setColor(0,0,0) - love.graphics.polygon("line",corners) - love.graphics.pop() + love.graphics.polygon("line",_corners) end -local function draw_chunk(the_chunk) - for q = 0,31 do - for r = 0,31 do - local t = the_chunk.tiles[chunk.index(q,r)] +local function draw_chunk(camera,the_chunk) + local tl,br = camera:extents() + local tlh,brh = tl:to_hex():round(), br:to_hex():round() + local trh = coords.Pos.make(br.x,tl.y):to_hex():round() + + for r = tlh.r-1,brh.r+1 do + local rowidx = r-tlh.r + local minq = tlh.q - math.floor((rowidx+1)/2) + local maxq = minq+(trh.q-tlh.q)+1 + print(string.format("r=%d; q = %d to %d",r,minq,maxq)) + for q = minq,maxq do + local h = coords.Hex.make(q,r) + local t = the_chunk:tile_at_offset(h) if t then - local p = coords.Hex.make(q,r):to_pos() - draw_hex(p) + draw_hex(h:to_pos()) end end end + + end return {draw_hex=draw_hex,draw_chunk=draw_chunk} diff --git a/client/main.lua b/client/main.lua index fe2bf06..14392d1 100644 --- a/client/main.lua +++ b/client/main.lua @@ -1,14 +1,15 @@ local enet = require"enet" local words = require"common.words" -local SERVER_HOSTNAME = "ubq323.website" --- local SERVER_HOSTNAME = "localhost" +-- local SERVER_HOSTNAME = "ubq323.website" +local SERVER_HOSTNAME = "localhost" local PLAYER_SIZE = 0.7 local local_player = nil local drawing = require"drawing" local coords = require"common.coords" +local Pos = coords.Pos local camera = require"camera".Camera.make() -- local pprint=require"common.pprint" @@ -122,7 +123,7 @@ function love.draw() end camera:apply_trans() - drawing.draw_chunk(chunk) + drawing.draw_chunk(camera,chunk) if local_player then draw_player(local_player,true) @@ -131,15 +132,40 @@ function love.draw() draw_player(pl) end + love.graphics.setColor(1,0,0) + love.graphics.rectangle("fill",0,0,1,1) + + local sm = Pos.make(love.mouse.getPosition()) + local wm = camera:screen_to_world(sm) + local hm = wm:to_hex():round() + + local tl,br = camera:extents() + + love.graphics.origin() + love.graphics.scale(2) + love.graphics.setColor(1,1,1) + love.graphics.print(table.concat( + {"sm "..tostring(sm), + "wm "..tostring(wm), + "hm "..tostring(hm), + "", + "tl "..tostring(tl), + "br "..tostring(br)}, + "\n"),10,10) + + + + end function love.load() - -- require"profile".start(10,io.open("./trace","w")) + require"profile".start(10,io.open("./trace","w")) host = enet.host_create() peer = host:connect(SERVER_HOSTNAME..":8473") end function love.quit() + require"profile".stop() peer:disconnect() host:flush() end diff --git a/client/profile.lua b/client/profile.lua index e7eab02..47a9806 100644 --- a/client/profile.lua +++ b/client/profile.lua @@ -1,7 +1,7 @@ local profile = require"jit.profile" local function start(period,file) local function cb(thread,samples,vmstate) - file:write(profile.dumpstack(thread,"pF l;",-100), vmstate, " ", samples, "\n") + file:write(profile.dumpstack(thread,"pF;l;",-100), vmstate, " ", samples, "\n") end profile.start("vli"..tonumber(period), cb) end diff --git a/common/chunk.lua b/common/chunk.lua index 38cdaf0..432e37b 100644 --- a/common/chunk.lua +++ b/common/chunk.lua @@ -22,6 +22,11 @@ function Chunk.make() return setmetatable({tiles=tiles},Chunk) end +function Chunk.tile_at_offset(self,hoffs) + if not(0<=hoffs.q and 0<=hoffs.r and hoffs.q<CHUNK_SIZE and hoffs.r<CHUNK_SIZE) then return nil end + local idx = CHUNK_SIZE*hoffs.q + hoffs.r + return self.tiles[idx] +end return { Chunk=Chunk, |