Pointers and Array of Screen 
Author Message
 Pointers and Array of Screen

I have been programming in Pascal for many years, but have never run
upon an occasion that I was forced to use pointers.  I have started to
program a simulation and found it necessary to make an array of every
pixel on the screen, something like:

        screen:array [1..640, 1..480] of byte;

where screen[x,y] contains a pixel color.  Of couse, this code will
not work because the structure is too large.  If anyone could suggest
a change in this declaration that would allow it to work, possibly
making use of pointers, please let me know.  

I know I should be able to create an array of the screen somehow,
since 640*480 bytes is certainly less than the availble memory, but I
have no clue about memory management...  anyway, please reply via
e-mail also, since my news server is not very reliable.

Thanks
Andy



Wed, 18 Jun 1902 08:00:00 GMT  
 Pointers and Array of Screen

Quote:

>I have been programming in Pascal for many years, but have never run
>upon an occasion that I was forced to use pointers.  I have started to
>program a simulation and found it necessary to make an array of every
>pixel on the screen, something like:

>    screen:array [1..640, 1..480] of byte;

I hope you wamtn array[1..480,1..640] of byte;

Quote:

>where screen[x,y] contains a pixel color.  Of couse, this code will
>not work because the structure is too large.  If anyone could suggest
>a change in this declaration that would allow it to work, possibly
>making use of pointers, please let me know.  

Is this a totally virtual screen and not some way to access the physical
screen? If it is virtual then why not just make:

type screenrow=array[1..640] of byte;

     screen:array[1..480] of ^screenrow;

then just allocate them:  for i:=1 to 640 do new(screen[i]);

to access the screen use screen[480]^[640] for right low corner.

This is 300 KB so protected mode is the way to go unless your program
otherwise needs little memory.

You may also make use record:

   screen:array[1..480] of record col:screenrow; end;

This allows you to use with:

 with screen[400] do begin
      col[300]:=1;
      col[600]:=2;
 End;

This speeds things up especially in protected mode.

Osmo



Wed, 18 Jun 1902 08:00:00 GMT  
 Pointers and Array of Screen



Quote:

>>I have been programming in Pascal for many years, but have never run
>>upon an occasion that I was forced to use pointers.  I have started to
>>program a simulation and found it necessary to make an array of every
>>pixel on the screen, something like:

>>        screen:array [1..640, 1..480] of byte;

>I hope you wamtn array[1..480,1..640] of byte;

I'm not really sure what you're getting at here...  I would use that
if I wanted to address the y coordinate first, but it otherwise
doesn't really matter... Does it?

This is a totally virtual screen, which will actually be displayed at
the end of the simulation.  I understand what you are doing just below
this paragraph, with the screenrow type, but I do not understand about
the records...  Anyway, thanks for the help, and I will try this.

Andy

Quote:
>Is this a totally virtual screen and not some way to access the physical
>screen? If it is virtual then why not just make:

>type screenrow=array[1..640] of byte;

>     screen:array[1..480] of ^screenrow;

>then just allocate them:  for i:=1 to 640 do new(screen[i]);

>to access the screen use screen[480]^[640] for right low corner.



Wed, 18 Jun 1902 08:00:00 GMT  
 Pointers and Array of Screen

Quote:

> I have been programming in Pascal for many years, but have never run
> upon an occasion that I was forced to use pointers.  I have started to
> program a simulation and found it necessary to make an array of every
> pixel on the screen, something like:

>         screen:array [1..640, 1..480] of byte;

> where screen[x,y] contains a pixel color.  Of couse, this code will
> not work because the structure is too large.  If anyone could suggest
> a change in this declaration that would allow it to work, possibly
> making use of pointers, please let me know.

> I know I should be able to create an array of the screen somehow,
> since 640*480 bytes is certainly less than the availble memory, but I
> have no clue about memory management...  anyway, please reply via
> e-mail also, since my news server is not very reliable.

> Thanks
> Andy

type
  ltype=array [0..639] of byte;
  ltypeptr=^ltype;

var
  pixl:array[0..479] of ltypeptr;
[...]

begin
  for i:=0 to 479 do new(pixl[i]);

Now you can acces each pixel at (x,y) through pixl[y]^[x].
The ltype stores a horizontal line.

Good luck,

Remco de Korte
Soft Machine



Wed, 18 Jun 1902 08:00:00 GMT  
 Pointers and Array of Screen


Quote:




>>>I have been programming in Pascal for many years, but have never run
>>>upon an occasion that I was forced to use pointers.  I have started to
>>>program a simulation and found it necessary to make an array of every
>>>pixel on the screen, something like:

>>>    screen:array [1..640, 1..480] of byte;

>>I hope you wamtn array[1..480,1..640] of byte;

>I'm not really sure what you're getting at here...  I would use that
>if I wanted to address the y coordinate first, but it otherwise
>doesn't really matter... Does it?

I think it is more natural to think that screen is formed from several
horizontal rows than several vertical lines. In two dimensional array
the first index is generally understood as row and the second as column.
It does matter in some cases, for example if you were to transfer the
screen into a physical one.

Quote:
>This is a totally virtual screen, which will actually be displayed at
>the end of the simulation.

In that case it is better to organize it as I said. One can then use
move() (several calls) to transfer the screen.

Quote:
>  I understand what you are doing just below
>this paragraph, with the screenrow type, but I do not understand about
>the records...  Anyway, thanks for the help, and I will try this.

The issue of records was how one can optimize the processing with
"with". There is no statement comparable to "with" for arrays.

Osmo



Wed, 18 Jun 1902 08:00:00 GMT  
 Pointers and Array of Screen

Quote:

>I have been programming in Pascal for many years, but have never run
>upon an occasion that I was forced to use pointers.  I have started to
>program a simulation and found it necessary to make an array of every
>pixel on the screen, something like:

>    screen:array [1..640, 1..480] of byte;

>where screen[x,y] contains a pixel color.  Of couse, this code will
>not work because the structure is too large.  If anyone could suggest
>a change in this declaration that would allow it to work, possibly
>making use of pointers, please let me know.  

>I know I should be able to create an array of the screen somehow,
>since 640*480 bytes is certainly less than the availble memory, but I
>have no clue about memory management...  anyway, please reply via
>e-mail also, since my news server is not very reliable.

     This is inherently doomed to failure.  As you notice, you can't
declare the array.  What you don't see is that it wouldn't do any good
if you could.  The array doesn't exist, other than with certain video
modes on certain cards.  (In which case you would need the docs on the
card to even find the array.  It would *NOT* be at $A000.)


Wed, 18 Jun 1902 08:00:00 GMT  
 Pointers and Array of Screen



Quote:
>>I know I should be able to create an array of the screen somehow,
>>since 640*480 bytes is certainly less than the availble memory, but I
>>have no clue about memory management...  anyway, please reply via
>>e-mail also, since my news server is not very reliable.

>     This is inherently doomed to failure.  As you notice, you can't
>declare the array.  What you don't see is that it wouldn't do any good
>if you could.  The array doesn't exist, other than with certain video
>modes on certain cards.  (In which case you would need the docs on the
>card to even find the array.  It would *NOT* be at $A000.)

I think you may have misunderstood my intentions.  This is to be a
fully virtual array, not a way to access display memory.  As others
have demonstrated, you can declare the array using the pointer
techniques described by others in this group.

Andy



Wed, 18 Jun 1902 08:00:00 GMT  
 Pointers and Array of Screen


Quote:

>>I have been programming in Pascal for many years, but have never run
>>upon an occasion that I was forced to use pointers.  I have started to
>>program a simulation and found it necessary to make an array of every
>>pixel on the screen, something like:

>> screen:array [1..640, 1..480] of byte;

>>where screen[x,y] contains a pixel color.  Of couse, this code will
>>not work because the structure is too large.  If anyone could suggest
>>a change in this declaration that would allow it to work, possibly
>>making use of pointers, please let me know.

>>I know I should be able to create an array of the screen somehow,
>>since 640*480 bytes is certainly less than the availble memory, but I
>>have no clue about memory management...  anyway, please reply via
>>e-mail also, since my news server is not very reliable.

>     This is inherently doomed to failure.  As you notice, you can't
>declare the array.  What you don't see is that it wouldn't do any good
>if you could.  The array doesn't exist, other than with certain video
>modes on certain cards.  (In which case you would need the docs on the
>card to even find the array.  It would *NOT* be at $A000.)

The problem you are having is that your array is bigger than 64K which is
the limit on a single structure.

Perhaps you could just create an array of 480 pointers to arrays of 640
bytes and then allocate memory in chunks of 640 bytes from the heap to them.

To clarify the comment made by Loren above, you cannot directly access video
memory for graphics modes in the same way as you can for text modes, as
there is no easy correspondence between bytes and pixels, in fact in most
video modes other than CGA modes, the video memory is not accessible at all
(as it resides on the graphics card and is not directly connected to the PC
bus) and must be manipulated through IO registers (ports) using IN and OUT
statements.



Wed, 18 Jun 1902 08:00:00 GMT  
 Pointers and Array of Screen

Quote:




> >>I have been programming in Pascal for many years, but have never run
> >>upon an occasion that I was forced to use pointers.  I have started to
> >>program a simulation and found it necessary to make an array of every
> >>pixel on the screen, something like:

> >> screen:array [1..640, 1..480] of byte;

> >>where screen[x,y] contains a pixel color.  Of couse, this code will
> >>not work because the structure is too large.  If anyone could suggest
> >>a change in this declaration that would allow it to work, possibly
> >>making use of pointers, please let me know.

> >>I know I should be able to create an array of the screen somehow,
> >>since 640*480 bytes is certainly less than the availble memory, but I
> >>have no clue about memory management...  anyway, please reply via
> >>e-mail also, since my news server is not very reliable.

> >     This is inherently doomed to failure.  As you notice, you can't
> >declare the array.  What you don't see is that it wouldn't do any good
> >if you could.  The array doesn't exist, other than with certain video
> >modes on certain cards.  (In which case you would need the docs on the
> >card to even find the array.  It would *NOT* be at $A000.)

> The problem you are having is that your array is bigger than 64K which is
> the limit on a single structure.

> Perhaps you could just create an array of 480 pointers to arrays of 640
> bytes and then allocate memory in chunks of 640 bytes from the heap to them.

> To clarify the comment made by Loren above, you cannot directly access video
> memory for graphics modes in the same way as you can for text modes, as
> there is no easy correspondence between bytes and pixels, in fact in most
> video modes other than CGA modes, the video memory is not accessible at all
> (as it resides on the graphics card and is not directly connected to the PC
> bus) and must be manipulated through IO registers (ports) using IN and OUT
> statements.

What you could also do is create an array, like you were going to do, do
all the changes in the array and after you are done copy the whole thing
to the video memory, which is probably $a000:0000. To create an array
more than WORD size you have to go to your COMPILER OPTIONS and turn off
RANGE CHECKING. After you do that you can allocate as much memory for
your array as needed. I also think that you should make you array
linear, becuase video memory is linear. This way you won't have any
trouble copying it to your array and backwards. Convert it to
coordinates only when you are actually writing in the array, using this
formula :
Array[x + ({horizontal resolution}*Y)] := -----; This is how you create
the array itself :

type
  Virt = array [1..3{or anything else}] of byte;

  VirtPtr = ^Virtual;                  

 var
  Virscr : VirtPtr;                      { This is the pointer to
your                                            array }

  Vaddr  : Word;                        { The segment of your array}

 Procedure SetUpVirtual;
   { This sets up the memory needed for the array }
 begin
  GetMem (VirScr,640*480);
  vaddr := seg (virscr^);
 end;

If you do it this way it doesn't matter how you declare your array,
since you specifiy its actual size in SetUpVirtual. Oh, and don't forget
to free the memory after you are done(FreeMem).

Hope I helped.



Wed, 18 Jun 1902 08:00:00 GMT  
 Pointers and Array of Screen


[...]

Quote:
>in fact in most
>video modes other than CGA modes, the video memory is not accessible at all
>(as it resides on the graphics card and is not directly connected to the PC
>bus) and must be manipulated through IO registers (ports) using IN and OUT
>statements.

Richard, please don't pronounce as fact, something that you clearly no nothing
about.

-- Jay

 --------------------------------------------------------------------------
| Jason Burgon - author of Graphic Vision, TV-Like GUI for 256 Colour SVGA |

| ***NEW VERSION OF GV AVAILABLE FROM*** -> http://www.jayman.demon.co.uk  |
 --------------------------------------------------------------------------



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Pointer to screen? HELP

2. array of pointers

3. FPC: Array of method pointers or procedures

4. Array with pointer variable access

5. printing an array to screen ?

6. array of pointers...

7. printing an array to screen ?

8. Array of pointers

9. pass a pointer to a array to a function

10. help with arrays of pointers?

11. Removing the Xth record from pointer array

12. Array/Pointer memory allocation?

 

 
Powered by phpBB® Forum Software