About buffers..

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

About buffers..

Berkant Atay
Hi Apeine,

First of all, I am very new to Lua, so if there is anything wrong or
foolish in the following please feel free to criticise me.

A buffer is actually a memory segment that can be accessed using a
name (a tag) and an (x,y) binary coordinate system, just as in the
regular output LCD screen. You can create a buffer using, say,
mybuffer=buffer.new(320,320), or mybuffer=buffer.get(0,0,320,320).
The first example in fact creates an empty buffer of 320*320 pixels;
the second statement copies what's on the screen at that moment pixel
by pixel, starting from (0,0) coords and extending 320 pixels both
horizontally and vertically. Of course, you can copy just a part of
the screen changing these parameters. In order to put the buffer on
the screen (show it) use the statement buffer.put(mybuffer,0,0). This
will put your 'mybuffer' on the screen starting from coords (0,0),
replacing the stuff already there. But there are other modes
of 'putting', such as inverted, masked, overlayed etc.. you have to
refer to Plua doc for this. You can place the contents of your buffer
anywhere on the screen by changing the (0,0) above. You can print or
draw anything in any color and any font on any buffer you create, and
unless you want it to show on the screen the view resides in the
buffer, hidden, until you get rid of the buffer using buffer.free
(mybuffer). 'Putting' a buffer doesn't kill it. I am not sure about
the exact boundaries of x and y, but it works well for negative
coordinates and out of the screen (more than 320) areas. That means
you can print or draw out of the screen! Yeah, this is the real power
of this. On the normal display, you can actually draw out of the
screen using normal graphics commands but you can't see them. But
they are there, believe me. Check it out using the buffers.. You can
write an extra line of text under the last line visible. You cannot
see it, but its there. For example, screen.moveto(330, 340) print
("Apeine") actually writes a text out of the screen. You can make it
visible by bringing it into view by mybuffer=buffer.get(0,0,400,360)
buffer.put(mybuffer,-100,-100). Now, this is important. You can print
or draw on the buffer itself. To assign a pre-created buffer to print
or draw on, you have to use buffer.use(mybuffer) command before you
print or draw anything on it. This will have effect only on
the 'mybuffer'. Everything you print or draw after buffer.use
(mybuffer) will be written on the buffer, not on the screen. It will
not be shown on the display. You can prepare a virtual screen full of
text and graphics and then you can show it instantly using the
buffer.put command. However, in order to put it on the screen, first
you have to change the displaying mode to normal using buffer.use().
This will make the LCD as the primary display. You can have multiple
buffers at one time. I dont know the limit on this but it works well
for 40 buffers. I guess the memory overflows and causes crashes after
about 40 buffers of full screen sizes. To kill a buffer you have to
remove it from the memory when you're done with it using buffer.free
command.

This has been a long narrative, but I hope it will be of help.
Following is an example piece of code to scroll text in continuous
fashion, depicting all of the above functions:

Regards,
Berkant.

function newline()            -- adds a new line under the last line
buffer.use(vbuf)              -- change drawing mode to virtual mode
screen.moveto(math.random(180),320)     -- move out of the screen
print(math.random())                    -- write a sample text
end

vbuf=buffer.new(320,340)    -- create a buffer larger than the screen
newline()                   -- write a last line there

while true do               -- loop endlessly
buffer.use()                -- now write on the real screen
 for i=1,22 do              -- scroll everything 22 pixels up 1 by 1
 buffer.put(vbuf,0,-i)      --show it
 if gui.event(0)==appStop then os.exit() end    -- to break the loop
 end
buffer.free(vbuf)     -- kill the buffer, not to overflow the memory
vbuf=buffer.get(0,0,320,340)          -- copy the new screen
newline()                             -- write another line
end

-- Nice, isn't it?  :)

Reply | Threaded
Open this post in threaded view
|

Re: About buffers..

apeine
Berkant,

Thanks a lot. Now I've a really good grasp at it, and now I can use it
for my evil plans..... :-)
And congratulations, as you made it really easy to understand (maybe a
regular programmer would think this is easy, but for me, this is
completely new).
Regards,

Alfredo

--- In [hidden email], "Berkant Atay" <berkant_atay@...> wrote:

>
> Hi Apeine,
>
> First of all, I am very new to Lua, so if there is anything wrong or
> foolish in the following please feel free to criticise me.
>
> A buffer is actually a memory segment that can be accessed using a
> name (a tag) and an (x,y) binary coordinate system, just as in the
> regular output LCD screen. You can create a buffer using, say,
> mybuffer=buffer.new(320,320), or mybuffer=buffer.get(0,0,320,320).
> The first example in fact creates an empty buffer of 320*320 pixels;
> the second statement copies what's on the screen at that moment pixel
> by pixel, starting from (0,0) coords and extending 320 pixels both
> horizontally and vertically. Of course, you can copy just a part of
> the screen changing these parameters. In order to put the buffer on
> the screen (show it) use the statement buffer.put(mybuffer,0,0). This
> will put your 'mybuffer' on the screen starting from coords (0,0),
> replacing the stuff already there. But there are other modes
> of 'putting', such as inverted, masked, overlayed etc.. you have to
> refer to Plua doc for this. You can place the contents of your buffer
> anywhere on the screen by changing the (0,0) above. You can print or
> draw anything in any color and any font on any buffer you create, and
> unless you want it to show on the screen the view resides in the
> buffer, hidden, until you get rid of the buffer using buffer.free
> (mybuffer). 'Putting' a buffer doesn't kill it. I am not sure about
> the exact boundaries of x and y, but it works well for negative
> coordinates and out of the screen (more than 320) areas. That means
> you can print or draw out of the screen! Yeah, this is the real power
> of this. On the normal display, you can actually draw out of the
> screen using normal graphics commands but you can't see them. But
> they are there, believe me. Check it out using the buffers.. You can
> write an extra line of text under the last line visible. You cannot
> see it, but its there. For example, screen.moveto(330, 340) print
> ("Apeine") actually writes a text out of the screen. You can make it
> visible by bringing it into view by mybuffer=buffer.get(0,0,400,360)
> buffer.put(mybuffer,-100,-100). Now, this is important. You can print
> or draw on the buffer itself. To assign a pre-created buffer to print
> or draw on, you have to use buffer.use(mybuffer) command before you
> print or draw anything on it. This will have effect only on
> the 'mybuffer'. Everything you print or draw after buffer.use
> (mybuffer) will be written on the buffer, not on the screen. It will
> not be shown on the display. You can prepare a virtual screen full of
> text and graphics and then you can show it instantly using the
> buffer.put command. However, in order to put it on the screen, first
> you have to change the displaying mode to normal using buffer.use().
> This will make the LCD as the primary display. You can have multiple
> buffers at one time. I dont know the limit on this but it works well
> for 40 buffers. I guess the memory overflows and causes crashes after
> about 40 buffers of full screen sizes. To kill a buffer you have to
> remove it from the memory when you're done with it using buffer.free
> command.
>
> This has been a long narrative, but I hope it will be of help.
> Following is an example piece of code to scroll text in continuous
> fashion, depicting all of the above functions:
>
> Regards,
> Berkant.
>
> function newline()            -- adds a new line under the last line
> buffer.use(vbuf)              -- change drawing mode to virtual mode
> screen.moveto(math.random(180),320)     -- move out of the screen
> print(math.random())                    -- write a sample text
> end
>
> vbuf=buffer.new(320,340)    -- create a buffer larger than the screen
> newline()                   -- write a last line there
>
> while true do               -- loop endlessly
> buffer.use()                -- now write on the real screen
>  for i=1,22 do              -- scroll everything 22 pixels up 1 by 1
>  buffer.put(vbuf,0,-i)      --show it
>  if gui.event(0)==appStop then os.exit() end    -- to break the loop
>  end
> buffer.free(vbuf)     -- kill the buffer, not to overflow the memory
> vbuf=buffer.get(0,0,320,340)          -- copy the new screen
> newline()                             -- write another line
> end
>
> -- Nice, isn't it?  :)
>


Reply | Threaded
Open this post in threaded view
|

Re: About buffers..

apeine
Berkant and other fellow programmers,

Berkant wrote:
> But there are other modes
> of 'putting', such as inverted, masked, overlayed etc.

I just read the Plua manual that came with 2.0b9, and I found no
reference on those "putting" methods.
Can anyone help me on that?
Regards,

Alfredo

--- In [hidden email], "apeine" <apeine@...> wrote:

>
> Berkant,
>
> Thanks a lot. Now I've a really good grasp at it, and now I can use it
> for my evil plans..... :-)
> And congratulations, as you made it really easy to understand (maybe a
> regular programmer would think this is easy, but for me, this is
> completely new).
> Regards,
>
> Alfredo
>
> --- In [hidden email], "Berkant Atay" <berkant_atay@> wrote:
> >
> > Hi Apeine,
> >
> > First of all, I am very new to Lua, so if there is anything wrong or
> > foolish in the following please feel free to criticise me.
> >
> > A buffer is actually a memory segment that can be accessed using a
> > name (a tag) and an (x,y) binary coordinate system, just as in the
> > regular output LCD screen. You can create a buffer using, say,
> > mybuffer=buffer.new(320,320), or mybuffer=buffer.get(0,0,320,320).
> > The first example in fact creates an empty buffer of 320*320 pixels;
> > the second statement copies what's on the screen at that moment pixel
> > by pixel, starting from (0,0) coords and extending 320 pixels both
> > horizontally and vertically. Of course, you can copy just a part of
> > the screen changing these parameters. In order to put the buffer on
> > the screen (show it) use the statement buffer.put(mybuffer,0,0). This
> > will put your 'mybuffer' on the screen starting from coords (0,0),
> > replacing the stuff already there. But there are other modes
> > of 'putting', such as inverted, masked, overlayed etc.. you have to
> > refer to Plua doc for this. You can place the contents of your buffer
> > anywhere on the screen by changing the (0,0) above. You can print or
> > draw anything in any color and any font on any buffer you create, and
> > unless you want it to show on the screen the view resides in the
> > buffer, hidden, until you get rid of the buffer using buffer.free
> > (mybuffer). 'Putting' a buffer doesn't kill it. I am not sure about
> > the exact boundaries of x and y, but it works well for negative
> > coordinates and out of the screen (more than 320) areas. That means
> > you can print or draw out of the screen! Yeah, this is the real power
> > of this. On the normal display, you can actually draw out of the
> > screen using normal graphics commands but you can't see them. But
> > they are there, believe me. Check it out using the buffers.. You can
> > write an extra line of text under the last line visible. You cannot
> > see it, but its there. For example, screen.moveto(330, 340) print
> > ("Apeine") actually writes a text out of the screen. You can make it
> > visible by bringing it into view by mybuffer=buffer.get(0,0,400,360)
> > buffer.put(mybuffer,-100,-100). Now, this is important. You can print
> > or draw on the buffer itself. To assign a pre-created buffer to print
> > or draw on, you have to use buffer.use(mybuffer) command before you
> > print or draw anything on it. This will have effect only on
> > the 'mybuffer'. Everything you print or draw after buffer.use
> > (mybuffer) will be written on the buffer, not on the screen. It will
> > not be shown on the display. You can prepare a virtual screen full of
> > text and graphics and then you can show it instantly using the
> > buffer.put command. However, in order to put it on the screen, first
> > you have to change the displaying mode to normal using buffer.use().
> > This will make the LCD as the primary display. You can have multiple
> > buffers at one time. I dont know the limit on this but it works well
> > for 40 buffers. I guess the memory overflows and causes crashes after
> > about 40 buffers of full screen sizes. To kill a buffer you have to
> > remove it from the memory when you're done with it using buffer.free
> > command.
> >
> > This has been a long narrative, but I hope it will be of help.
> > Following is an example piece of code to scroll text in continuous
> > fashion, depicting all of the above functions:
> >
> > Regards,
> > Berkant.
> >
> > function newline()            -- adds a new line under the last line
> > buffer.use(vbuf)              -- change drawing mode to virtual mode
> > screen.moveto(math.random(180),320)     -- move out of the screen
> > print(math.random())                    -- write a sample text
> > end
> >
> > vbuf=buffer.new(320,340)    -- create a buffer larger than the screen
> > newline()                   -- write a last line there
> >
> > while true do               -- loop endlessly
> > buffer.use()                -- now write on the real screen
> >  for i=1,22 do              -- scroll everything 22 pixels up 1 by 1
> >  buffer.put(vbuf,0,-i)      --show it
> >  if gui.event(0)==appStop then os.exit() end    -- to break the loop
> >  end
> > buffer.free(vbuf)     -- kill the buffer, not to overflow the memory
> > vbuf=buffer.get(0,0,320,340)          -- copy the new screen
> > newline()                             -- write another line
> > end
> >
> > -- Nice, isn't it?  :)
> >
>