Recent

Author Topic: What is buffer and how to use it?  (Read 6248 times)

ssliackus

  • Jr. Member
  • **
  • Posts: 65
What is buffer and how to use it?
« on: September 18, 2018, 09:18:42 pm »
Hi guys,

That is me again  :)

What is buffer and how to use it? I know there is frame buffer, key buffer. For example Crt module has got readkey that reads from buffer, but I cannot find any tutorial or good explanation on that.

Thank you for help.

S.

Handoko

  • Hero Member
  • *****
  • Posts: 5129
  • My goal: build my own game engine using Lazarus
Re: What is buffer and how to use it?
« Reply #1 on: September 18, 2018, 10:29:49 pm »
I'm good in codes not words so I'm not going to explain the definition of buffer. You can search the Internet for it if you want.

Here I have some explanations how to use buffer to reduce flickering effect when painting on the screen:
https://forum.lazarus.freepascal.org/index.php/topic,38136.msg263143.html#msg263143

And here has explanation about using memory buffer to load and store font for use in game programming:
https://forum.lazarus.freepascal.org/index.php/topic,33909.msg220914.html#msg220914

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: What is buffer and how to use it?
« Reply #2 on: September 18, 2018, 10:38:29 pm »
Simplified explanation

In the sense you mean, a buffer is nothing more than a zone of memory where one process deposits values and from which another process retrieves them.

For example, a frame buffer is a piece of memory where p.e. your program constructs a representation of the screen (as it wants it to be) and which is subsequently "transferred" to the actual video memory, by the same or other function, system call or actual hardware.

The same for the keyboard buffer: the system software responds to physical key presses/releases by reading the keyboard and storing a representation in a buffer which afterwards is read by other system processes or applications (in a Pascal program, with ReadKey, v.g.).

Note that buffer, in a extense sense, is also used to name almost any region of memory dedicated to this or other kinds of process communication: file buffers, string buffers, stream buffers, etc.

Just think of what the word buffer means IRW and apply it to computer programs' behaviour..
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: What is buffer and how to use it?
« Reply #3 on: September 18, 2018, 10:49:29 pm »
Normally a buffer is a chunk of memory set aside to accumulate data in the background until
at some point the main application reads from it there by emptying the buffer/memory chunk.

 There are the most common types:

    1. First In, First Out  (FIFO);
         
          The first data item in the buffer will be read first when the buffer is read.

    2. First In, Last out (FILO);

         The First data item in the buffer will be read last, meaning its like a stack of papers, you
      need to remove the top sheets before you get to the bottom sheets, the first one.


 etc.

  A buffer is just a storage space with indexes to that when written to or read from these indexes
are managed to so the system will know where to put the next data item and where to read to
get the next data item.

 In most systems, when both of these indexes are the same, it means the buffer is empty..

The only true wisdom is knowing you know nothing

Handoko

  • Hero Member
  • *****
  • Posts: 5129
  • My goal: build my own game engine using Lazarus
Re: What is buffer and how to use it?
« Reply #4 on: September 19, 2018, 07:54:15 am »
I've just remembered, here I ever wrote the explanation how to use buffer for keyboard input:
http://forum.lazarus.freepascal.org/index.php/topic,38136.msg261965.html#msg261965

That example uses First In, First Out buffer technique to solve the missing keypress problem, which is a common issue in game programming.
« Last Edit: September 19, 2018, 07:59:00 am by Handoko »

ssliackus

  • Jr. Member
  • **
  • Posts: 65
Re: What is buffer and how to use it?
« Reply #5 on: September 24, 2018, 01:39:51 pm »
Thank you, guys, for your time and answers. I will as soon as I have time for that :-)

Weiss

  • Full Member
  • ***
  • Posts: 127
Re: What is buffer and how to use it?
« Reply #6 on: November 11, 2022, 04:13:29 am »
keyboard must have some kind of buffer? If key is pressed and held, how to limit the number of executions then? Say, to 3 or 4.

Currently, in my code, if I press and hold for more than a split second, say, zoom in button, graphs keeps being re-plotted.  As my graphs are quite slow, when I release the key, I still have to wait while graphs continues being replotted number of times, whatever number of keystrokes is in the buffer I guess.

How to control that?

or is it controlled by OS?

Weiss

  • Full Member
  • ***
  • Posts: 127
Re: What is buffer and how to use it?
« Reply #7 on: November 11, 2022, 06:58:01 am »
while waiting for your suggestions I found windows keyboard setting that does the trick. Decreasing the "repeat character rate" makes graph response so much more manageable, some plots are built in exact rate as key repeat, feels like direct control. I can also increase the delay for repeat character, which feels like one click for each graph refresh.

Meanwhile I also played with "repeat until not keypressed", "While keypressed Do readkey"  etc, all kinds of logic constructs. Results are not good.

Zvoni

  • Hero Member
  • *****
  • Posts: 2315
Re: What is buffer and how to use it?
« Reply #8 on: November 11, 2022, 09:31:39 am »
Normally a buffer is a chunk of memory set aside to accumulate data in the background until
at some point the main application reads from it there by emptying the buffer/memory chunk.

 There are the most common types:

    1. First In, First Out  (FIFO);
         
          The first data item in the buffer will be read first when the buffer is read.

    2. First In, Last out (FILO);

         The First data item in the buffer will be read last, meaning its like a stack of papers, you
      need to remove the top sheets before you get to the bottom sheets, the first one.


 etc.

  A buffer is just a storage space with indexes to that when written to or read from these indexes
are managed to so the system will know where to put the next data item and where to read to
get the next data item.

 In most systems, when both of these indexes are the same, it means the buffer is empty..
I'll add LIFO here: Last in, First Out. a real life example would be loading a Truck. --> This is (in a way) the same as FILO, but it's more known as LIFO
a programmers example of LIFO would be a Stack (Push/Pop).

The way i explain Buffer: a real life example: WhatsApp.
When you start writing a message to someone, that TextField you're writing in is your "Buffer".
The other person doesn't see what you're writing while you're writing.
The moment you tap "send", your buffer gets read, sent to the receiver, and emptied out on your side.
Without buffer, your message would be sent character by character, and the receiver would see the message appearing as you're typing
« Last Edit: November 11, 2022, 09:37:23 am by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: What is buffer and how to use it?
« Reply #9 on: November 11, 2022, 11:13:08 am »
keyboard must have some kind of buffer? If key is pressed and held, how to limit the number of executions then? Say, to 3 or 4.

Currently, in my code, if I press and hold for more than a split second, say, zoom in button, graphs keeps being re-plotted.  As my graphs are quite slow, when I release the key, I still have to wait while graphs continues being replotted number of times, whatever number of keystrokes is in the buffer I guess.

How to control that?

or is it controlled by OS?
OS only has generic controls, like the repeat rate that you've found, but it might not be sufficient for your own needs. In this case, you can make your own buffer, a queue for instance, where instead of directly processing all ReadKey directly, you enqueue it instead. Then, somewhere else another code dequeues key from this queue one by one, at the rate you control (a simple SysUtils.Sleep before next iteration should do). As a means of preventing DDoS, you can limit the size of the queue that when it's reached, any ReadKey result is simply discarded / ignored.

Weiss

  • Full Member
  • ***
  • Posts: 127
Re: What is buffer and how to use it?
« Reply #10 on: November 16, 2022, 08:06:23 pm »

OS only has generic controls, like the repeat rate that you've found, but it might not be sufficient for your own needs. In this case, you can make your own buffer, a queue for instance, where instead of directly processing all ReadKey directly, you enqueue it instead. Then, somewhere else another code dequeues key from this queue one by one, at the rate you control (a simple SysUtils.Sleep before next iteration should do). As a means of preventing DDoS, you can limit the size of the queue that when it's reached, any ReadKey result is simply discarded / ignored.

you are right, slowing the repeat rate wasn't good idea. I forgot that same computer is used by many technicians for some other needs too, and everybody is used to a certain speed.

I tried to play with keyboard unit (TKeyEvent etc.) but this doesn't work with GRAPH unit. How else I can make my custom logical buffer?

The easiest solution turned out to be "while KeyPressed Do Key := ReadKey; "  where "Key" is declared as char type.  This way there is no stored repeat keys, direct control.

I don't understand how it works, but it has desirable effect.  I am getting used to not understanding things.



Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: What is buffer and how to use it?
« Reply #11 on: November 16, 2022, 10:43:27 pm »
you are right, slowing the repeat rate wasn't good idea. I forgot that same computer is used by many technicians for some other needs too, and everybody is used to a certain speed.

I tried to play with keyboard unit (TKeyEvent etc.) but this doesn't work with GRAPH unit. How else I can make my custom logical buffer?

The easiest solution turned out to be "while KeyPressed Do Key := ReadKey; "  where "Key" is declared as char type.  This way there is no stored repeat keys, direct control.

I don't understand how it works, but it has desirable effect.  I am getting used to not understanding things.
The downside of graph unit (alongside crt) is that they're not friendly with the KVM (keyboard, video, mouse) units. Both units manipulate everything in their own incompatible way. So indeed if you have to use graph and crt, stick to everything they have.

I don't think it's possible to escape threads, though. So this is approximately what you need to do:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2.  
  3. uses
  4.   {$ifdef unix}
  5.   cthreads,
  6.   {$endif}
  7.   SysUtils,
  8.   crt,
  9.   gqueue;
  10.  
  11. type
  12.   TCharQueue = specialize TQueue<Char>;
  13. var
  14.   cq: TCharQueue;
  15.  
  16. function PrintChar(AData: Pointer): PtrInt;
  17. begin
  18.   while true do begin
  19.     if cq.Size > 0 then begin
  20.       Write(cq.Front); // this should be your char handler, i.e. what to do with the char
  21.       cq.Pop; // remove char from the buffer
  22.       // this is your rate control, i.e. it won't handle the next char after 1 second has passed since the last handled char
  23.       // change it as you need
  24.       SysUtils.Sleep(1000);
  25.     end;
  26.   end;
  27. end;
  28.  
  29. var
  30.   c: Char;
  31. begin
  32.   cq := TCharQueue.Create;
  33.   BeginThread(@PrintChar, nil);
  34.   WriteLn('Press and hold any key then see how they''re written slowly');
  35.   repeat
  36.     c := ReadKey;
  37.     if UpCase(c) = #27 then Break; // ESC to stop the program, even before all the chars have been handled
  38.     if cq.Size < 5 then cq.Push(c); // maximum 5 chars in buffer
  39.   until false;
  40.   cq.Free;
  41. end.
  42.  
You can press as many keys as you want, but if the buffer has 5 chars already where they're not yet processed, the 6th and following chars will be ignored. Likewise, in the middle of processing, if you press ESC, the program will terminate anyway without waiting for the rest of the char in the buffer. Try pressing a-z sequentially one by one with normal speed (e.g.: 100ms difference), you will see not all characters will be printed.

Weiss

  • Full Member
  • ***
  • Posts: 127
Re: What is buffer and how to use it?
« Reply #12 on: November 19, 2022, 04:28:14 am »
thank you Leledumbo. It works. You are using some syntax FPC compiler doesn't accept though. Like SysUtils need to be declared, can't call SysUtils.Sleep() directly. And then  since it is declared, I can just call Sleep().

I remember C# is like that, no need to declare if you don't mind typing the whole thing every time. FPC apparently not. Maybe in Delphi it is possible?

A bit of mind-bending though, how it works. In your example, in my mind, as soon as you handle it to function PrintChar(), user is stuck in infinite loop. But, keyboard input is still handled, in parallel?

Weiss

  • Full Member
  • ***
  • Posts: 127
Re: What is buffer and how to use it?
« Reply #13 on: November 19, 2022, 04:37:17 am »
aha, found it, it is threads. Another thing I didn't know exist.

Bogen85

  • Hero Member
  • *****
  • Posts: 595
Re: What is buffer and how to use it?
« Reply #14 on: November 19, 2022, 09:45:04 am »
thank you Leledumbo. It works. You are using some syntax FPC compiler doesn't accept though. Like SysUtils need to be declared, can't call SysUtils.Sleep() directly. And then  since it is declared, I can just call Sleep().

I just tried Leledumbo's program with FPC 3.3.1 and 3.2.2. Both compiled it and the resulting executables ran as expected.
« Last Edit: November 19, 2022, 09:47:29 am by Bogen85 »

 

TinyPortal © 2005-2018