Complete Noob - Drawing text in layers

General discussion about LÖVE, Lua, game development, puns, and unicorns.
RogerH
Prole
Posts: 13
Joined: Sun Sep 03, 2017 4:06 pm

Complete Noob - Drawing text in layers

Post by RogerH »

Hi all,

I am a noob at this. I have done some programming in Basic, SIOC and other languages so I have a rough idea of what I'm doing, but new to lua and LOVE.

I have a file with two lines in it - eg:

Hello
----------world!

(where "world!" has 10 spaces in front of it, not dashes, but when I previewed the post it deleted the spaces in my example))

I want to see

Hello world!

I have tried reading the lines one at a time and drawing them in the same place


local file = assert(io.open("path",helloWorld.txt"))
for x=1,2 ,1
do
local line = file:read()

function love.draw()
love.graphics.setNewFont(36)
love.graphics.print (line, 50, 50)
end
end

but the second print overwrites the first.

I have played with the alpha channel of the text and the background but i still just get

----------world!

(again, the dashes in the line abover are not in the actual file - they are spaces in the file, but the system here deleted the spaces from my example)

Any suggestions? I guess I'm doing something wrong or trying to do something not possible but hey that's the fun of learning!

Thanks in advance :-)

Roger.
User avatar
Bunnybacon
Prole
Posts: 20
Joined: Fri Mar 25, 2016 8:42 am

Re: Complete Noob - Drawing text in layers

Post by Bunnybacon »

love clears the main canvas on every draw call. I don't know how to disable this, but you can circumvent it by making a new canvas, like this:

Code: Select all

local file = assert(io.open("text.txt"))
local canvas = love.graphics.newCanvas(300,300)

for x=1,2 ,1 do
	local line = file:read()
	canvas:renderTo(function()
		love.graphics.setNewFont(36)
		love.graphics.print(line, 50, 50)
	end)
	function love.draw()
		love.graphics.draw(canvas,0,0)
	end
end
RogerH
Prole
Posts: 13
Joined: Sun Sep 03, 2017 4:06 pm

Re: Complete Noob - Drawing text in layers

Post by RogerH »

Hi,

Thanks so much for the help!

It made a difference in that the two lines now both show, but they are superimposed.

The second line has spaces at the beginning, which should mean (if they were printed) that it would show 'Hello world!'

But what I'm getting is 'Hello' and 'world!' printed on top of each other.

progress, though, and I'm grateful for the help.

I'm guessing they are on totally separate canvasses which are then superimposed on each other?

Thanks,

Roger.
RogerH
Prole
Posts: 13
Joined: Sun Sep 03, 2017 4:06 pm

Re: Complete Noob - Drawing text in layers

Post by RogerH »

So I changed it to:

local canvas = love.graphics.newCanvas()

local line = file:read()
canvas:renderTo(function()
love.graphics.setNewFont(36)
love.graphics.print(line, 50, 50)
end)
local line = file:read()
canvas:renderTo(function()
love.graphics.setNewFont(36)
love.graphics.print(line, 150, 50)
end)
function love.draw()
love.graphics.draw(canvas,0,0)
end

And with a bit of tinkering I can get it to work (I only have about 12 lines to deal with)

As an aside, I'm getting a square character at the end of each line - any idea why?

many thanks,

Roger.
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Complete Noob - Drawing text in layers

Post by grump »

Please use code tags.

You're getting square characters at the end of each line because your file has Windows style line endings (<CR><LF>); file:read() looks for Unix style line endings (<LF>) and ignores <CR>.

This results in lines that end in <CR>. The active font displays that (unprintable) character as a box.

You can either change the line endings in a text editor, or remove the <CR> character from the strings:

Code: Select all

local line = file:read():gsub("\r", "")
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Complete Noob - Drawing text in layers

Post by zorg »

RogerH wrote: Sun Sep 03, 2017 6:03 pm
The second line has spaces at the beginning, which should mean (if they were printed) that it would show 'Hello world!'
But what I'm getting is 'Hello' and 'world!' printed on top of each other.

I'm guessing they are on totally separate canvasses which are then superimposed on each other?
If i recall correctly, Löve removes those extra spaces from the beginning of lines to be printed, maybe that's why; you need to offset it yourself.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
RogerH
Prole
Posts: 13
Joined: Sun Sep 03, 2017 4:06 pm

Re: Complete Noob - Drawing text in layers

Post by RogerH »

Thanks so much for the help both of you - off to bed now as I have an early start but will get back to you with results tomorrow. Cgheers,

Roger.
RogerH
Prole
Posts: 13
Joined: Sun Sep 03, 2017 4:06 pm

Re: Complete Noob - Drawing text in layers

Post by RogerH »

All three of you, sorry!
RogerH
Prole
Posts: 13
Joined: Sun Sep 03, 2017 4:06 pm

Re: Complete Noob - Drawing text in layers

Post by RogerH »

Hi Guys,

Getting some success now. The code to get rid of the squares worked, and the code to draw separate canvasses worked too!

I figured a workaround for the superimposed graphics by using 'printf':

Code: Select all

--Read first 12 chars
	local line = file:read(12):gsub("\r", "")
	canvas:renderTo(function()
		love.graphics.setNewFont(32)
		love.graphics.printf(line, 40, 38, 800,"left")
	end)
	
--Read second 12 chars (plus line feeds = 14)
	local line = file:read(14):gsub("\r", "")
	canvas:renderTo(function()
		love.graphics.setNewFont(32)
		love.graphics.printf(line, 20, 38, 780,"right")
	end)
I was then able to justify left and right and get "Hello--------------world!" (again, dashes represent spaces in the example)

What I'm finding in this is that spaces are an issue - they seem to get stripped from the file as it's printed - at least, more than one space is - if there's a space between two words like " Hello lovely--------world!" then that space gets printed. But at the moment it sems multiple spaces get stripped. This is a bit of an problem as they are part of the formatting of the final presentation.

Is there a way of reading the lines of the file by character, rather than lines?

Would this be a way of reading the file character by character?

Code: Select all

local line = file:read(1):gsub("\r", "")
If the "file:read" comes to a space, how would I force it to print a space to the final screen?

many thanks again,

Roger.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Complete Noob - Drawing text in layers

Post by zorg »

As i said before, reading it all it one go is not the issue; your issue is that yes, love.graphics.print and printf does strip extra spaces.
Can't do anything about that, but you can use the font functions to get the width of the previous n characters, and calculate the offsets that way... although i'm sure there's an easier way to what you want to accomplish... probably.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest