Loading image in Orca/Pascal

8 views
Skip to first unread message

limtc

unread,
May 17, 2009, 9:35:23 PM5/17/09
to
Another question for Orca/Pascal expert. I am trying to load an image
in Orca/Pascal, but I am not sure how to do this - I know how to do in
Complete Pascal, but I am not sure about the equivalent of CTIUtils?

I need to do this to help Ding Wen do the Invader War title screen
(unless someone can help us in "how to draw big font in Orca/Pascal"
so that he can do a big "INVADER WAR" title).
--
Here's my Complete Pascal code segment from my demo software (http://
virtualgs.larwe.com/Virtual_GS/Slideshow.html):

uses Types, QuickDraw, Events, GSOS, CTIUtils, MiscTool;

{ Load Image }

procedure loadImage(path: string);
var
openRec: OpenRecGS;
ioRec: IORecGS;
closeRec: RefNumRecGS;
pathNameGS: GSstring255;

begin

{ Open image }

p2GSstring(path, pathNameGS);
openRec.pCount := 2;
openRec.pathName := @pathNameGS;
openGS(openRec);

{ Read into memory }

if _ToolErr = 0 then begin
ioRec.pCount := 4;
ioRec.refNum := openRec.refNum;
ioRec.dataBuffer := ptr($E12000);
ioRec.requestCount := 32768;
readGS(ioRec);

closeRec.pCount := 1;
closeRec.refNum := openRec.refNum;
closeGS(closeRec);
end;
end;

Polymorph

unread,
May 17, 2009, 11:00:24 PM5/17/09
to
On May 18, 11:35 am, limtc <thyech...@gmail.com> wrote:
> Another question for Orca/Pascal expert. I am trying to load an image
> in Orca/Pascal, but I am not sure how to do this - I know how to do in
> Complete Pascal, but I am not sure about the equivalent of CTIUtils?
>

<snip>

My "Colouring for the IIgs" program is all coded in Orca/Pascal now
(and has been for some time) so I have already made the changes
necessary to load images using Orca - again, they're fairly minimal. I
have a LoadImage procedure that can load packed (compressed) or
regular SHR images. I always used packed images as they take up a
fraction of the disk space. Note that the parameter to the procedure
is the pointer to the memory used to hold the image. In "Colouring for
the IIgs" I preload all images, but you could pass in a pointer to the
address of the SHR screen (or modify my code to suit).

Also required is the P2GSString to convert the Pascal string into a GS/
OS string (I have included that below as well as a function to convert
from a GS/OS string back to a Pascal string).

Here are the procedures lifted directly from Colouring for the IIgs:

{ Load an image from disk into memory }
PROCEDURE LoadImage(path: pString; ptrToPixImage: Ptr);
VAR
openRec : openOSDCB;
ioRec : readwriteOSDCB;
closeRec : closeOSDCB;
pathGS : gsosInString;
indexPack : Boolean;
srcHdl : Handle;
srcSize : LongInt;
destHdl : Handle;
destSize : Integer;
destPtr : Ptr;
result : Integer;
error : Integer;
BEGIN

indexPack := false;

{ Open the file }
openRec.pcount := 12;
p2GSstring(path, pathGS);
openRec.pathName := @pathGS;
openRec.requestAccess := 1;
openRec.resourceNumber := 0;
openRec.optionList := nil;
OpenGS(openRec);

if ToolError = 0 then begin

{ Check the file type to see if it is a packed file }
if openRec.fileType = $C0 then
indexPack := true;

{ If the file is packed, then load and unpack it! }
if indexPack then begin
srcSize := openRec.dataEOF;
srcHdl := NewHandle(srcSize,
UserID,
attrLocked,
0);
if ToolError = 0 then begin
ioRec.pcount := 4;
ioRec.refNum := openRec.refNum;
ioRec.dataBuffer := srcHdl^;
ioRec.requestCount := openRec.dataEOF;
ReadGS(ioRec);
if ToolError <> 0 then begin
{ handle error }


closeRec.pCount := 1;
closeRec.refNum := openRec.refNum;
closeGS(closeRec);

DisposeHandle(srcHdl);
srcHdl := nil;
end
else begin
{ Close the file }


closeRec.pCount := 1;
closeRec.refNum := openRec.refNum;
closeGS(closeRec);

{ Decompact the image }
destSize := $7FFF;
destPtr := @ptrToPixImage;
result := UnPackBytes(srcHdl^,
Integer(srcSize),
destPtr,
destSize);
DisposeHandle(srcHdl);
end;
end;
end
else begin

{ Read the image into memory }


ioRec.pCount := 4;
ioRec.refNum := openRec.refNum;

ioRec.dataBuffer := ptrToPixImage;
ioRec.requestCount := $08000;
readGS(ioRec);

{ Close the file }


closeRec.pCount := 1;
closeRec.refNum := openRec.refNum;
closeGS(closeRec);
end;
end

else begin
error := ToolError;
DisplayMessage(Concat('Tool error - ',Cnvis(error)),' ');
end;
END;

{ Convert a Pascal string into a GS/OS string }
PROCEDURE P2GSString(theString : pString; var GSString :
gsosInString);
VAR
i : Integer;
BEGIN
GSString.size := length(theString);
i := 0;
repeat
i := i + 1;
GSString.theString[i] := theString[i];
until i >= GSString.size
END;


{ Convert a GS/OS string into a Pascal string }
PROCEDURE GS2PString(GSString : gsosInString; var theString :
pString);
VAR
i : Integer;
BEGIN
theString[0] := chr(GSString.size);
i := 0;
repeat
i := i + 1;
theString[i] := GSString.theString[i];
until i >= GSString.size
END;


Hope this helps!

Cheers,
Mike

limtc

unread,
May 17, 2009, 11:42:30 PM5/17/09
to
Thanks!

That's a lot of codes to read and digest... will try. Do you need any
extra libraries?

Polymorph

unread,
May 18, 2009, 1:32:15 AM5/18/09
to

In addition to any other libraries you are using, make sure you have
Common, QuickDrawII, GSOS, and maybe MscToolset (not sure if you need
this) in your programs USES statement.

Cheers,
Mike

Polymorph

unread,
May 18, 2009, 1:39:23 AM5/18/09
to

One other thing I forgot to mention, you will have to remove any
references to DisplayMessage to get this working as-is...
DisplayMessage is just a simple procedure I use for displaying error
messages and debug info.

Cheers,
Mike

limtc

unread,
May 18, 2009, 3:45:03 AM5/18/09
to
Hi,

I got stuck in the "NewHandle" statement - identified not declared.
Any library to be used?

Polymorph

unread,
May 18, 2009, 7:50:06 AM5/18/09
to

NewHandle is in the Memory Manager tool set, you need to add
"MemoryMgr" to the USES line.

Cheers,
Mike

Bill Buckels

unread,
May 18, 2009, 8:49:12 AM5/18/09
to

"Polymorph" <polym...@hotmail.com> wrote:
>Hope this helps!

Hi Mike,

This would help me more if it was in Orca C. I'll unwirth it a little later
in the year when I get to my "learning GS coding in Orca C" projects.

Despite that it is good to see some source code being posted in here. I
thought I was almost the only lonely soul that still did that, and I was
beginning to worry these apple2 groups would sink into the quack-mire of
figuring-out how to use Macintoshes with IBM Monitors and NULL modem cables
and how to use other newer Apple Computer products like iPhones... rather
than programming CP/M 80 for Applicards or adapting flat-screen monitors
using Carte-Blanche and other topics actually relevant to apple2 computers
that interest me.

Bill


limtc

unread,
May 18, 2009, 9:02:45 AM5/18/09
to
On 5月18日, 下午8时49分, "Bill Buckels" <bbuck...@mts.net> wrote:

> Despite that it is good to see some source code being posted in here. I
> thought I was almost the only lonely soul that still did that, and I was
> beginning to worry these apple2 groups would sink into the quack-mire of
> figuring-out how to use Macintoshes with IBM Monitors and NULL modem cables
> and how to use other newer Apple Computer products like iPhones... rather
> than programming CP/M 80 for Applicards or adapting flat-screen monitors
> using Carte-Blanche and other topics actually relevant to apple2 computers
> that interest me.
>
> Bill

Bill, if you don't mind Pascal and BASIC programming, there are quite
a number of source code here:

http://virtualgs.larwe.com/Virtual_GS/Fun_and_Games.html

Note that many programs have more than 1 examples in various
languages. The 3 languages I am using are Complete Pascal, Orca/Pascal
and GSoft BASIC.

And if you don't mind if the source codes are written by a kid, there
are a few more here:

http://virtualgs.larwe.com/Virtual_GS/Lim_Ding_Wen.html

For Apple IIGS programming, you are not alone!

Polymorph

unread,
May 18, 2009, 6:36:44 PM5/18/09
to
To: Polymorph
Polymorph wrote:
> Bill,
>
> I'm more of a C guy myself, but I did enjoy my venture back into Pascal
> which, until recently, I hadn't used since the early 90's.
>
> I just converted the LoadImage method into C (fairly trivial). Note
> however, I have not tested this, but it should be pretty close if it
> isn't 100% correct:

<snip my untested code!>

Whoops! I forgot the parameters to the function! I believe it should read:

LoadImage(Str255 path, Pointer ptrToPixImage)

...

That should do it! :-)

Cheers,
Mike

Polymorph

unread,
May 18, 2009, 6:30:18 PM5/18/09
to
To: Bill Buckels

Bill,

I'm more of a C guy myself, but I did enjoy my venture back into Pascal
which, until recently, I hadn't used since the early 90's.

I just converted the LoadImage method into C (fairly trivial). Note
however, I have not tested this, but it should be pretty close if it
isn't 100% correct:


#define TRUE 1
#define FALSE 0
#define NULL 0

{ Load an image from disk into memory }

void LoadImage()
{
OpenRecGS openRec;
IORecGS ioRec ;
IORecGS closeRec;
GSString255 pathGS;
int indexPack;
Handle srcHdl;
long srcSize;
Handle destHdl;
int destSize;
Pointer destPtr;
int result;
int error;

indexPack = FALSE;

/* Open the file */


openRec.pcount = 12;
p2GSstring(path, pathGS);

openRec.pathName = &pathGS;


openRec.requestAccess = 1;
openRec.resourceNumber = 0;

openRec.optionList = NULL;
OpenGS(openRec);

if ( ToolError == 0 )
{
/* If the file is packed, then load and unpack it! */
if ( openRec.fileType == 0xC0 )
{


srcSize = openRec.dataEOF;
srcHdl = NewHandle(srcSize,
UserID,
attrLocked,
0);

if ( ToolError == 0 )
{


ioRec.pcount = 4;
ioRec.refNum = openRec.refNum;

ioRec.dataBuffer = *srcHdl;
ioRec.requestCount = openRec.dataEOF;
ReadGS(ioRec);
if ( ToolError != 0 )
{
/* handle error */


closeRec.pCount = 1;
closeRec.refNum = openRec.refNum;
closeGS(closeRec);
DisposeHandle(srcHdl);

srcHdl = NULL;
}
else
{
/* Close the file */


closeRec.pCount = 1;
closeRec.refNum = openRec.refNum;
closeGS(closeRec);

/* Decompact the image */
destSize = 0x7FFF;
destPtr = &ptrToPixImage;
result = UnPackBytes(*srcHdl,
(int)srcSize,
destPtr,
destSize);
DisposeHandle(srcHdl);
}
}
else
{
/* Read the image into memory */


ioRec.pCount = 4;
ioRec.refNum = openRec.refNum;
ioRec.dataBuffer = ptrToPixImage;

ioRec.requestCount = 0x08000;
readGS(ioRec);

/* Close the file */


closeRec.pCount = 1;
closeRec.refNum = openRec.refNum;
closeGS(closeRec);
}
}
}

else
{
error = ToolError;
// Do something here to handle the error. Display an error msg?

limtc

unread,
May 19, 2009, 9:00:58 AM5/19/09
to
On 5月18日, 上午11时00分, Polymorph <polymorp...@hotmail.com> wrote:

> PROCEDURE LoadImage(path: pString; ptrToPixImage: Ptr);

What is the Ptr points to? Are we able to simplify it to just pass it
a path and display the image?

Thanks!

Bill Buckels

unread,
May 20, 2009, 5:04:15 AM5/20/09
to

"Polymorph" <poly...@a2central.com.remove-x7e-this> wrote:
>I just converted the LoadImage method into C (fairly trivial). Note
>however, I have not tested this, but it should be pretty close if it isn't
>100% correct:

Hey thanks. When I get to it this will be fun. I will need to sit with Mike
W.'s manual, your code, and my GS when the summer comes and It gets too hot
to work:)

Bill


Polymorph

unread,
May 20, 2009, 6:19:15 AM5/20/09
to
To: Bill Buckels
Bill Buckels wrote:

Bill,

I also *highly* recommend getting your hands on the 3 Toolbox References
+ the GS/OS Reference. They are very large and somewhat cryptic at
times, but for really getting down dirty with desktop/toolbox
programming, you can't really live without them. You can get the
original copies (hard cover books) of these things 2nd hand off eBay, or
Syndicomm sells printed copies of them in 3 ring folders.

Just my 2c worth...

Cheers,
Mike

Polymorph

unread,
May 20, 2009, 6:13:03 AM5/20/09
to
To: limtc
limtc wrote:

As per my original email (I already pre-empted this question!):

"Note that the parameter to the procedure is the pointer to the memory
used to hold the image. In "Colouring for the IIgs" I preload all
images, but you could pass in a pointer to the address of the SHR screen
(or modify my code to suit)."

So expanding on this, by your original example, you are using address
$E12000 as the pointer to screen memory. So you should be able to call
my LoadImage procedure with the second parameter set as a pointer to
$E12000.

BTW, it is usually bad practice to assume the screen location. Ideally
you would use the Quickdraw tool box call:

{ Determine the current Grafport location }
GetPortLoc(portLoc);

where "portLoc" is a locInfo structure defined in Common.

to get the current grafport location. The actual address of the current
port is then determined by interrogating:

portLoc.ptrToPixelImage

You can also create locInfo structures and use SetPortLoc for offscreen
drawing, but that is something I haven't delved into much...

cheers,
Mike

PZ

unread,
May 20, 2009, 10:04:19 AM5/20/09
to
On May 20, 4:13 am, "Polymorph" <polymo...@a2central.com.remove-dl5-

Advanced Programming Techniques for the Apple IIGS Toolbox:
Part A: http://www.apple-iigs.info/doc/fichiers/adviigsa.pdf
Part B: http://www.apple-iigs.info/doc/fichiers/adviigsb.pdf

are is a great toolbox resources while you're waiting for the Toolbox
Reference volumes to arrive. The book uses TML Pascal and C code
examples.

- Paul

Jerry

unread,
May 21, 2009, 12:10:49 AM5/21/09
to
"Polymorph" <poly...@a2central.com.remove-dl5-this> writes:

Another source of good used books is http://www.getcheapbooks.com/.
I've bought more books from their indexed sites than from eBay, and
the prices and shipping costs are usually much more sane than the eBay
scene demands.

Slightly related topic: does anyone here have any Don Fudge books, or
any Bob Bishop books?


--
--
Jerry awanderin at yahoo dot ca

Reply all
Reply to author
Forward
0 new messages