;Example code for MethodBBLib (C)opyRight 1997 by Erwan Fouret

;##### IMPORTANT #####
;Include the resident 'blitzlibs:amigalibs.res'
;in 'Compiler->Compiler Options...'


;***** utility/tagitem.h *****
#TAG_USER=(1 LSL 31)

;***** datatypes/datatypesclass.h *****
#DTA_Dummy=(#TAG_USER+$1000)

#DTA_LayoutProc=(#DTA_Dummy+27)
#DTA_GroupID=(#DTA_Dummy+31)
#DTA_SourceType=(#DTA_Dummy+101)

#DTST_FILE=2

;***** datatypes/datatypes.h *****
#GID_PICTURE=$70696374

;***** datatupes/pictureclass.h ****
NEWTYPE.BitMapHeader
 bmh_Width.w
 bmh_Height.w
 bmh_Left.w
 bmh_Top.w
 bmh_Depth.b
 bmh_Masking.b
 bmh_Compression.b
 bmh_Pad.b
 bmh_Transparent.w
 bmh_XAspect.b
 bmh_YAspect.b
 bmh_PageWidth.w
 bmh_PageHeight.w
End NEWTYPE

#PDTA_ModeID=(#DTA_Dummy+200)
#PDTA_BitMapHeader=(#DTA_Dummy+201)
#PDTA_ColorRegisters=(#DTA_Dummy+203)


;A little statement to transform bytes greater than 128 into words
Statement adjust{*a.b}
 v.w=Peek.w(*a)
 If v<0
  vv.w=v+256
  Poke.w *a,vv
 EndIf
End Statement

NEWTYPE.tags a.l:b:c:d:e:f:g:h:i:j:k:l:m:n:o:p:q:r:s:t:u:v:w:x:y:z:End NEWTYPE

DEFTYPE.tags attrs
DEFTYPE.BitMapHeader *bmhd
DEFTYPE.l *vm

Dim *args.b(1)

rdargs.l=ReadArgs_("PIC/A",&*args(0),0) ;We read the args

If rdargs=0
 PrintFault_ IoErr_(),"ShowDTPic" ;We display an error if no args
 End
EndIf

name$=Peek$(*args(0)) ;We get the name from the args

Print name$,":"

FreeArgs_ rdargs

attrs.tags\a=#DTA_SourceType,#DTST_FILE,#DTA_GroupID,#GID_PICTURE,0

*obj.b=NewDTObjectA_(&name$,attrs) ;We create the DT Object

If *obj=0
 NPrint "Error creating DT Object !!!"
 End
EndIf

GetAttr_ #PDTA_BitMapHeader,*obj,&*bmhd
GetAttr_ #PDTA_ModeID,*obj,&*vm

;We get some informations on the picture
;And we print them

NPrint "(",*bmhd\bmh_Width,"x",*bmhd\bmh_Height,"x",*bmhd\bmh_Depth,")"

NPrint "ViewMode = $",Hex$(*vm)

w=*bmhd\bmh_Width
h=*bmhd\bmh_Height
d=*bmhd\bmh_Depth

If w<320 Then wi=320 Else wi=w

Screen 0,*bmhd\bmh_Left,*bmhd\bmh_Top,wi,h,d,*vm,"",1,2

*sc.Screen=Peek.l(Addr Screen(0))
;We get the address of our screen

*rp.RastPort=&*sc\_RastPort
;We get the address of our RastPort

ginfo.GadgetInfo\gi_Screen=*sc,0,0,*rp,*rp\Layer
ginfo\gi_Domain\Left=*bmhd\bmh_Left,*bmhd\bmh_Top,w,h
ginfo\gi_Pens\DetailPen=0,0
ginfo\gi_DrInfo=GetScreenDrawInfo_(*sc)

;We create a GadgetInfo structure

*gadinfo.GadgetInfo=&ginfo

attrs\a=#GA_Left,*bmhd\bmh_Left
attrs\c=#GA_Top,*bmhd\bmh_Top
attrs\e=#GA_Width,w
attrs\g=#GA_Height,h
attrs\i=#ICA_TARGET,#ICTARGET_IDCMP,0

SetDTAttrsA_ *obj,0,0,attrs
;We set some attrs to the Object

DoMethod *obj,#GM_LAYOUT,*gadinfo,0
;We draw the Picture on our screen

*isdrawn.l=0

Repeat                                     ;This loop
 GetAttr_ #DTA_LayoutProc,*obj,&*isdrawn   ;wait for the end
Until *isdrawn=0                           ;of the drawing of the pic

AGAPalRGB 0,((2^d)-1),0,0,0 ;init of the palette

*creg.b=0

GetAttr_ #PDTA_ColorRegisters,*obj,&*creg ;We get the color registers

For i=0 To ((2^d)-1)
 r.b=Peek.b(*creg):g.b=Peek.b(*creg+1):b.b=Peek.b(*creg+2)
 rr.w=r:gg.w=g:bb.w=b
 adjust{&rr}:adjust{&gg}:adjust{&bb}
 AGAPalRGB 0,i,rr,gg,bb
 *creg=*creg+3
Next
;This routine create the palette

ShowPalette 0

DoMethod *obj,#GM_RENDER,*gadinfo,*rp,1 ;We refresh the pic

Repeat
Until Joyb(0)=2

;We quit the prog

Free Palette 0

Free Screen 0

DisposeDTObject_ *obj

End
