How's my style?
Posted: Tue Feb 07, 2012 6:31 pm
I've been programming a SRPG for a few days now and am slowly building useful tables and functions to use during development.
I really don't have someone who can comment on my style in person, so I figured that some advice from the Love2D community would be nice.
This code stands on it's own, so you can throw it into a main.lua function if you need to test it. I'm not posting everything because most of it is too specific to my game to stand on it's own. I just want some comments, I can post more code if you want, but I'm not sure how easy the rest of it is to read.
Thanks!
Here's a snippet from my ui.lua file, lines are broken into a table so that I can create simple menus in the future. It's in early-development.
And here's an example call:
I really don't have someone who can comment on my style in person, so I figured that some advice from the Love2D community would be nice.
This code stands on it's own, so you can throw it into a main.lua function if you need to test it. I'm not posting everything because most of it is too specific to my game to stand on it's own. I just want some comments, I can post more code if you want, but I'm not sure how easy the rest of it is to read.
Thanks!
Here's a snippet from my ui.lua file, lines are broken into a table so that I can create simple menus in the future. It's in early-development.
Code: Select all
ui = {}
ui.windowstyles = {}
ui.windowstyles.default =
{
bgcolor = {0,0,0},
textcolor = {255,255,255},
textpad = 5,
font
}
function ui:load()
ui.windowstyles.default.font = love.graphics.newFont("fonts/AlegreyaSC-Regular.ttf")
end
function ui:drawTextWindow(texttable, x, y, stylekey)
local style = self.windowstyles[stylekey];
love.graphics.setFont(style.font)
--find max line length and height
local width = 0;
local height = 0;
for key,text in pairs(texttable) do
if style.font:getWidth(text) > width then
width = style.font:getWidth(text)
end
height = height+1;
end
height = style.font:getHeight()*height;
--draw the background
love.graphics.setColor(unpack(style.bgcolor))
love.graphics.rectangle("fill", x, y, width+(2*style.textpad), height+(2*style.textpad))
--draw the text
love.graphics.setColor(unpack(style.textcolor))
for key,text in pairs(texttable) do
love.graphics.print(text, x+style.textpad, y+(style.font:getHeight()*(key-1))+style.textpad)
end
end
Code: Select all
self:drawTextWindow(
{"Name: David",
"Level: 1",
"Class: Warrior"},
300, 300, "default")