Skip to content

Commit

Permalink
simpler text printing with 2x res support
Browse files Browse the repository at this point in the history
  • Loading branch information
parameterized committed Sep 5, 2018
1 parent f93323b commit 0474ff5
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 49 deletions.
6 changes: 4 additions & 2 deletions camera.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ camera.__index = camera

local function new(opts)
local obj = {
x=0, y=0, scale=1, rotation=0
x=0, y=0, scale=1, rotation=0,
isSet=false
}
-- screen size x, y
obj.ssx, obj.ssy = love.graphics.getDimensions()
Expand Down Expand Up @@ -43,7 +44,6 @@ function camera:move(dx, dy)
self.y = self.y + dy
end

-- todo: better name
function camera:scaleBy(x)
self.scale = self.scale*x
end
Expand All @@ -58,10 +58,12 @@ function camera:set()
love.graphics.scale(self.scale)
love.graphics.rotate(self.rotation)
love.graphics.translate(-self.x, -self.y)
self.isSet = true
end

function camera:reset()
love.graphics.pop()
self.isSet = false
end

function camera:draw(f)
Expand Down
14 changes: 4 additions & 10 deletions chat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,32 +38,26 @@ function chat.keypressed(k, scancode, isrepeat)
end

function chat.draw()
local _shader = love.graphics.getShader()

if gameTime - chat.lastMsgTime < 4 or chat.active then
local a = 1
if not chat.active and gameTime - chat.lastMsgTime > 3 then
a = 4 - (gameTime - chat.lastMsgTime)
end
love.graphics.setColor(1, 1, 1, a)
love.graphics.setFont(fonts.c17)
love.graphics.setShader(shaders.fontAlias)
local startIdx = math.max(#chat.log - (chat.active and 12 or 6) + 1, 1)
local numMsgs = #chat.log - startIdx + 1
for i=1, numMsgs do
local v = chat.log[startIdx + (i-1)]
local y = gsy - (numMsgs - (i-1) + 1)*20 - 6
love.graphics.print(v, 4, y)
local y = gsy - (numMsgs - (i-1) + 1)*10 - 3
text.printSmall(v, 3, y)
end
love.graphics.setShader(_shader)
end
if chat.active then
love.graphics.setColor(0, 0, 0, 0.8)
love.graphics.rectangle('fill', 2, gsy - 24, 200, 22)
love.graphics.rectangle('fill', 1, gsy - 12, 100, 11)
love.graphics.setColor(1, 1, 1)
love.graphics.setFont(fonts.c17)
love.graphics.setShader(shaders.fontAlias)
love.graphics.print(chat.val, 4, gsy - 22)
love.graphics.setShader(_shader)
text.printSmall(chat.val, 3, gsy - 11)
end
end
8 changes: 4 additions & 4 deletions client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function client.connect(ip, port)
client.currentState.players[v.id] = v
end
else
debugger.log('error decoding client rpc add')
print('error decoding client rpc add')
end
end,
remove = function(self, data)
Expand All @@ -55,11 +55,11 @@ function client.connect(ip, port)
end
client.currentState.players[id] = nil
else
debugger.log('server tried to remove local player')
print('server tried to remove local player')
end
end
else
debugger.log('error decoding client rpc remove')
print('error decoding client rpc remove')
end
end,
stateUpdate = function(self, data)
Expand All @@ -76,7 +76,7 @@ function client.connect(ip, port)
end
]]
else
debugger.log('error decoding client rpc stateUpdate')
print('error decoding client rpc stateUpdate')
end
end
}
Expand Down
5 changes: 1 addition & 4 deletions hud.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
hud = {}

function hud.draw()
local _shader = love.graphics.getShader()
love.graphics.setColor(1, 1, 1)
love.graphics.draw(gfx.hud.frame, 0, 0)
love.graphics.draw(gfx.hud.xpbar, math.floor(gsx/2), gsy - 5, 0, 1, 1,
Expand All @@ -19,8 +18,6 @@ function hud.draw()
love.graphics.draw(gfx.hud.dropDown, 143, 22)
love.graphics.draw(gfx.hud.pause, 453, 18)

love.graphics.setShader(shaders.fontAlias)
love.graphics.setFont(fonts.c17)
love.graphics.print(player.name, 44, 5)
love.graphics.setShader()
text.print(player.name, 44, 5)
end
3 changes: 3 additions & 0 deletions loadassets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ love.graphics.setDefaultFilter('nearest', 'nearest')
gsx, gsy = 480, 270
canvases = {
game = love.graphics.newCanvas(gsx, gsy),
game2x = love.graphics.newCanvas(gsx*2, gsy*2),
tempGame = love.graphics.newCanvas(gsx, gsy),
hpBar = love.graphics.newCanvas(18, 4)
}
Expand Down Expand Up @@ -91,6 +92,8 @@ fonts = {
c13 = love.graphics.newImageFont('gfx/fonts/small_font.png', ' abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,!?-+/():;%&`\'*#=[]"|~@$^_{}<>'),
c17 = love.graphics.newImageFont('gfx/fonts/big_font.png', ' abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,!?-+/():;%&`\'*#=[]"|~@$^_{}<>')
}
-- todo: test
-- love.graphics.newFont([filename, ] size, "mono")

shaders = {
fontAlias = love.graphics.newShader('shaders/fontAlias.glsl'),
Expand Down
23 changes: 20 additions & 3 deletions main.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

require 'utils'
require 'loadassets'
require 'text'
Camera = require 'camera'
camera = Camera{ssx=gsx, ssy=gsy}
nut = require 'love_nut'
Expand Down Expand Up @@ -38,7 +39,19 @@ function screen2game(x, y)
x = x / gameScale
y = y - (ssy-gameScale*gsy)/2
y = y / gameScale
return math.floor(x), math.floor(y)
return x, y
end

function setGameCanvas2x()
love.graphics.setCanvas(canvases.game2x)
love.graphics.setColor(1, 1, 1)
love.graphics.push()
love.graphics.origin()
love.graphics.draw(canvases.game, 0, 0, 0, 2, 2)
love.graphics.pop()
love.graphics.setCanvas(canvases.game)
love.graphics.clear()
love.graphics.setCanvas(canvases.game2x)
end

function love.update(dt)
Expand Down Expand Up @@ -111,6 +124,8 @@ end

function love.draw()
local mx, my = screen2game(love.mouse.getPosition())
love.graphics.setCanvas(canvases.game2x)
love.graphics.clear()
love.graphics.setCanvas(canvases.game)
love.graphics.clear(0.15, 0.15, 0.15)
if gameState == 'playing' then
Expand All @@ -126,15 +141,17 @@ function love.draw()
chat.draw()

love.graphics.setColor(1, 1, 1)
love.graphics.draw(gfx.cursors.main, mx, my, 0, 1, 1, 0, 0) -- hotspot 0, 0
love.graphics.draw(gfx.cursors.main, math.floor(mx), math.floor(my), 0, 1, 1, 0, 0) -- hotspot 0, 0
end

menu.draw()

-- draw game on game2x
setGameCanvas2x()
love.graphics.setCanvas()
love.graphics.clear(0, 0, 0)
love.graphics.setColor(1, 1, 1)
love.graphics.draw(canvases.game, ssx/2-gameScale*gsx/2, ssy/2-gameScale*gsy/2, 0, gameScale, gameScale)
love.graphics.draw(canvases.game2x, ssx/2-gameScale*gsx/2, ssy/2-gameScale*gsy/2, 0, gameScale/2, gameScale/2)
end

function love.quit()
Expand Down
41 changes: 15 additions & 26 deletions menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function menu.load()
name = 'Player',
ip = '127.0.0.1',
port = '1357',
resolution = 3,
resolution = 2,
fullscreen = 1,
vsync = true,
cursorLock = true
Expand Down Expand Up @@ -165,7 +165,7 @@ function menu.load()
end}

menu.resolutionBtn = menu.addBtn{state='options', text='Resolution', y=exitY - h*4,
type='cycle', items={'480x270', '960x540', '1440x810', '1920x1080'},
type='cycle', items={'960x540', '1440x810', '1920x1080'},
active=menuDefaults.resolution, action=function(v)
local fullscreen, fstype = love.window.getFullscreen()
if not (fullscreen and fstype == 'desktop') then
Expand All @@ -178,7 +178,6 @@ function menu.load()
love.resize(w, h)
end
end, draw=function(v, mx, my)
local _shader = love.graphics.getShader()
if mx > v.bx and mx < v.bx + v.bw and my > v.by and my < v.by + v.bh then
love.graphics.setColor(0.3, 0.3, 0.3)
else
Expand All @@ -191,16 +190,14 @@ function menu.load()
love.graphics.rectangle('fill', v.bx, v.by, v.bw, v.bh)
love.graphics.setColor(0.8, 0.8, 0.8)
love.graphics.setFont(v.font)
love.graphics.setShader(shaders.fontAlias)
love.graphics.print(v.text, math.floor(v.x - v.font:getWidth(v.text)/2), math.floor(v.by - v.font:getHeight()))
text.print(v.text, math.floor(v.x - v.font:getWidth(v.text)/2), math.floor(v.by - v.font:getHeight()))
love.graphics.setColor(1, 1, 1)
local text = v.items[v.active]
local txt = v.items[v.active]
if fullscreen and fstype == 'desktop' then
local w, h = love.graphics.getDimensions()
text = w .. 'x' .. h
txt = w .. 'x' .. h
end
love.graphics.print(text, math.floor(v.x - v.font:getWidth(text)/2), math.floor(v.y - v.font:getHeight()/2))
love.graphics.setShader(_shader)
text.print(txt, math.floor(v.x - v.font:getWidth(txt)/2), math.floor(v.y - v.font:getHeight()/2))
end}
menu.fullscreenBtn = menu.addBtn{state='options', text='Fullscreen', y=exitY - h*3,
type='cycle', items={'Windowed', 'Borderless Fullscreen Windowed', 'Fullscreen'},
Expand Down Expand Up @@ -350,8 +347,6 @@ function menu.keypressed(k, scancode, isrepeat)
end

function menu.draw()
local _shader = love.graphics.getShader()

if gameState == 'menu' then
local mx, my = screen2game(love.mouse.getPosition())

Expand All @@ -375,17 +370,15 @@ function menu.draw()
end
end
love.graphics.rectangle('fill', v.bx, v.by, v.bw, v.bh)
local text = v.text
local txt = v.text
love.graphics.setFont(v.font)
love.graphics.setShader(shaders.fontAlias)
if v.type == 'cycle' then
love.graphics.setColor(0.8, 0.8, 0.8)
love.graphics.print(v.text, math.floor(v.x - v.font:getWidth(v.text)/2), math.floor(v.by - v.font:getHeight()))
text = v.items[v.active]
text.print(v.text, math.floor(v.x - v.font:getWidth(v.text)/2), math.floor(v.by - v.font:getHeight()))
txt = v.items[v.active]
end
love.graphics.setColor(1, 1, 1)
love.graphics.print(text, math.floor(v.x - v.font:getWidth(text)/2), math.floor(v.y - v.font:getHeight()/2))
love.graphics.setShader(_shader)
text.print(txt, math.floor(v.x - v.font:getWidth(txt)/2), math.floor(v.y - v.font:getHeight()/2))
end
end
for _, v in pairs(menu.inputs[menu.state] or {}) do
Expand All @@ -401,14 +394,12 @@ function menu.draw()
love.graphics.rectangle('fill', v.bx, v.by, v.bw, v.bh)
love.graphics.setColor(0.8, 0.8, 0.8)
love.graphics.setFont(v.font)
love.graphics.setShader(shaders.fontAlias)
love.graphics.print(v.text, math.floor(v.x - v.font:getWidth(v.text)/2), math.floor(v.by - v.font:getHeight()))
text = v.value
if menu.activeInput == v then text = text .. (time % 1 < 0.5 and '' or '|') end
text.print(v.text, math.floor(v.x - v.font:getWidth(v.text)/2), math.floor(v.by - v.font:getHeight()))
txt = v.value
if menu.activeInput == v then txt = txt .. (time % 1 < 0.5 and '' or '|') end
love.graphics.setColor(1, 1, 1)
love.graphics.setFont(v.font)
love.graphics.print(text, math.floor(v.x - v.font:getWidth(text)/2), math.floor(v.y - v.font:getHeight()/2))
love.graphics.setShader(_shader)
text.print(txt, math.floor(v.x - v.font:getWidth(txt)/2), math.floor(v.y - v.font:getHeight()/2))
end
end
for _, v in pairs(menu.infos[menu.state] or {}) do
Expand All @@ -417,9 +408,7 @@ function menu.draw()
else
love.graphics.setColor(1, 1, 1)
love.graphics.setFont(v.font)
love.graphics.setShader(shaders.fontAlias)
love.graphics.print(v.text, math.floor(v.x - v.font:getWidth(v.text)/2), math.floor(v.y - v.font:getHeight()/2))
love.graphics.setShader(_shader)
text.print(v.text, math.floor(v.x - v.font:getWidth(v.text)/2), math.floor(v.y - v.font:getHeight()/2))
end
end
end
Expand Down
5 changes: 5 additions & 0 deletions player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@ function player.draw()
love.graphics.pop()
love.graphics.setShader(_shader)

-- names
local font = fonts.c17
love.graphics.setFont(font)
text.printSmall(player.name, math.floor(px) - font:getWidth(player.name)/4, math.floor(py) - 40)

if drawDebug then
love.graphics.setColor(1, 0, 0, 0.5)
love.graphics.circle('fill',
Expand Down
32 changes: 32 additions & 0 deletions text.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

text = {}

function text.print(txt, x, y)
local _shader = love.graphics.getShader()
love.graphics.setShader(shaders.fontAlias)
love.graphics.print(txt, math.floor(x), math.floor(y))
love.graphics.setShader(_shader)
end

function text.printSmall(txt, x, y)
local _canvas = love.graphics.getCanvas()
local _shader = love.graphics.getShader()
setGameCanvas2x()
love.graphics.setShader(shaders.fontAlias)

love.graphics.push()
-- set camera with 2x screen size
if camera.isSet then
love.graphics.origin()
love.graphics.translate(camera.ssx, camera.ssy)
love.graphics.scale(camera.scale)
love.graphics.rotate(camera.rotation)
love.graphics.translate(-camera.x*2, -camera.y*2)
end

love.graphics.print(txt, math.floor(x*2), math.floor(y*2))

love.graphics.pop()
love.graphics.setCanvas(_canvas)
love.graphics.setShader(_shader)
end

0 comments on commit 0474ff5

Please sign in to comment.