A String as screenbuffer

General FreeBASIC programming questions.
Max_B
Posts: 17
Joined: May 04, 2007 19:36

A String as screenbuffer

Postby Max_B » Oct 06, 2008 19:30

Hello,

Is there a way to use a string as a screenbuffer like this:

Code: Select all

declare function rgb16(Byval r As Ubyte,Byval g As Ubyte,Byval b As Ubyte)  As Ushort
option explicit

screenres 1024,768,16,2
const length as longint =(4 + (1024 * 768)*2)+1

dim shared scherm as string * length
dim cc as integer,ccp as integer=1,a,c as ushort,z$

get(0,0)-(1023,767),@scherm

for a= 4 to length/2
    cc +=ccp
    if cc>=255 or cc<=0 then ccp=-ccp
   
    c=rgb16(cc,cc,0)
   
    poke ushort,sadd(scherm)+a*2,c
next a

do
    z$=mid$(scherm,6, 1024*4+1)
   
    mid$(scherm,7)=mid$(scherm,1024*4+7)+z$
   
    put(0,0),@scherm,pset

loop until inkey$=chr$(27)

end

Function RGB16(Byval r As Ubyte, Byval g As Ubyte, Byval b As Ubyte) As Ushort
    r Shr= 3
    g Shr= 2
    b Shr= 3
    Return (r Shl 11) Or (g Shl 5) Or b
End Function


This one is using PUT to show the buffer.
But is there a way to edit the screenbuffer with MID$ etc. ?
I think it would be fast for horizontal line plotting.
vdecampo
Posts: 2982
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Re: A String as screenbuffer

Postby vdecampo » Oct 06, 2008 19:35

Max_B wrote:I think it would be fast for horizontal line plotting..


If this is your reasoning for trying to do this, you would be wrong. The quickest way to plot pixels would be to directly access the memory. Using string functions carries enormous overhead per call.

-Vince
Max_B
Posts: 17
Joined: May 04, 2007 19:36

Postby Max_B » Oct 06, 2008 20:09

What do you mean by overhead?
I thought that a string as screenbuffer is easy because you can edit the values easily with MID$ LTRIM$ etc.
How do I point my own string buffer to the screenbuffer?
Last edited by Max_B on Oct 06, 2008 20:35, edited 1 time in total.
vdecampo
Posts: 2982
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Postby vdecampo » Oct 06, 2008 20:18

Max_B wrote:What do you mean by overhead?
I thought that a string as screenbuffer is easy because you can edit the values easily with MID$ LTRIM$ etc.
How do I point the screenbuffer to the screenbuffer?


Functions like MID$ and LTRIM$ don't just instantly do what you want, they have to push parameters to the stack, make the call to the function, move and load pointers around, then reverse the entire process. This would happen for each pixel you adjust.

Rather you can just get a pointer to the memory and access the pixel via an offset. It will be many orders of magnitute faster.

-Vince
www.iMakeGames.com
Max_B
Posts: 17
Joined: May 04, 2007 19:36

Postby Max_B » Oct 06, 2008 20:37

Hhm okay. That is slower.
But how can i point that stringbuffer to the screen so it will be displayed?
Do I have to use Tinyptc for that?
vdecampo
Posts: 2982
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Postby vdecampo » Oct 06, 2008 21:34

Max_B wrote:Hhm okay. That is slower.
But how can i point that stringbuffer to the screen so it will be displayed?
Do I have to use Tinyptc for that?


Are you trying to display the characters or pixel data?

You can copy the strings data to the screen with memcpy. You will need to include "crt.bi" for that function.

Code: Select all

ScreenLock
memcpy ScreenPtr,strptr(scherm),len(scherm)
ScreenUnlock


It would help to know what you are really trying to do. This is just an example to accomplish what you asked, not a proper solution for whatever you are trying to do.

-Vince
www.iMakeGames.com
counting_pine
Site Admin
Posts: 6190
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Postby counting_pine » Oct 06, 2008 22:36

Why are you trying to do this? I can't think of any situations where you'd want to LTRIM an image buffer.
If you're going to work on image buffers in a low-level way, you should be prepared to learn how to deal with pointers and memory manipulation.

If you do, you'll find you can do anything with pointers that you could do with strings, and that you'll only want basic operations such as setting and copying anyway.

In fact, if you want to point a string at screen memory, you'll have to use pointers anyway, so there's no real simplicity advantage.

But my overall point here is, strings really aren't suitable for this.
Eclipzer
Posts: 432
Joined: Oct 01, 2005 10:50
Location: Maryland
Contact:

Postby Eclipzer » Oct 07, 2008 3:50

Why are you trying to do this? I can't think of any situations where you'd want to LTRIM an image buffer.


Because clearly he has a very limited understanding of how accessing video memory works. Questions like this come from a clear lack of understanding. There really isn't a parallel to computer programming, which means when you first start out you really have no frame of reference. It takes time, experience, trial and error, study, practice, etc.
to reach a point where these concepts become much simpler to grasp.

My suggestion? Point him to the appropriate tutorials and reference materials and save yourself some typing.
kinem
Posts: 87
Joined: Sep 24, 2008 15:55

Postby kinem » Oct 08, 2008 2:12

Max_B, you might take a look at the screenbuffer code in the "jerktest.bas" program in the thread

http://www.freebasic.net/forum/viewtopic.php?t=12348

note the line

buf[1+x+640*y] = cc

The square brackets are an easy way to access a pointer with offset.

The 1 is due to the 'header' part of the buffer, and that can vary but is not hard to figure out.
counting_pine
Site Admin
Posts: 6190
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Postby counting_pine » Oct 08, 2008 2:49

kinem, your method is dependent on the image having a particular kind of format, which isn't produced by default in FB's default dialect. There, the header is more than four bytes, and there can be byte padding between rows.

The recommended way to work directly with image buffers is to use the method shown here:
http://www.freebasic.net/wiki/KeyPgimageinfo

ImageInfo retrieves all of the necessary information about the image buffer. It tells you where the image data starts, and how many bytes there are between the start of each row.
The only other thing you need to know is how individual color values are stored in the buffer, which can be found out here:
http://www.freebasic.net/wiki/GfxInternalFormats
kinem
Posts: 87
Joined: Sep 24, 2008 15:55

Postby kinem » Oct 09, 2008 15:08

OK, thanks.
Max_B
Posts: 17
Joined: May 04, 2007 19:36

Postby Max_B » Oct 10, 2008 18:53

Thanks for all the replies y'all.
A long time ago I made a game in QB4.5 (screen 13) that had a string as screenbuffer. And I draw all tiles as strings with MID$ to the buffer.
After that I memcopied the buffer to the screen.
This worked for me also in Freebasic. Only now I find out that isn't much faster the PUTting things to the screen. And you have to lock the screen also before memcopying (that slows it down).
Thanks...

Return to “General”

Who is online

Users browsing this forum: albert, bcohio2001, Majestic-12 [Bot] and 2 guests