Blitz Sources - Tag Implementation


; Here there is our tagobj definition.
; You'll need to allocate an object like this
; to use these commands.

NEWTYPE .tagobj   ; Tag Object
  maxtags.w       ; Max Number of Tags you can add
  nexttag.w       ; Counter of tags
  *mem.l          ; Pointer to memory containing tags
End NEWTYPE

; This function Initialize a tag object
; Please, note you can Init the same object as many times as you want.
;
; SYNTAX:
;   result.b = InitTagObj{&tagobject, max_tags}
;
; RETURNS:
;   TRUE - Init successful. FALSE otherwise.
Function.b InitTagObj{*tobj.tagobj, maxtags.w}
  If *tobj\mem Then FreeVec_ *tobj\mem

  *m.l = AllocVec_ ( (maxtags+1)*8, #MEMF_ANY | #MEMF_CLEAR)

  If *m=0 Then Function Return False

  *tobj\mem     = *m
  *tobj\nexttag = 0
  *tobj\maxtags = maxtags

  Function Return True
End Function

; This command just clear the TagObject contents.
; It is usefull in case you don't want to allocate
; a new object, but just want to reuse the same
; memory previously allocated (avoiding memory fragmentation).
;
; SYNTAX:
;   ClearTagObj{&tagobject}
; RETURNS:
;   NONE.
Statement ClearTagObj{*tobj.tagobj}
  *m.l = *tobj\mem
  For t.w = 0 To *tobj\nexttag
    Poke.l *m, 0    ; Here we clear ti_Tag field
    *m + SizeOf .l
    Poke.l *m, 0    ; And here we clear ti_Data field
  Next
End Statement

; With this command you can add a tag into the object.
;
; SYNTAX:
;  result.b = SetTag{&tagobj, tag_to_set, value}
;
; RETURNS:
;  TRUE - adding OK. FALSE otherwise.
Function.b SetTag{*tobj.tagobj, tag.l, v.l}
  result.b = False

  If *tobj\nexttag<*tobj\maxtags
    If *tobj\mem
      *m.l = *tobj\mem
      *m + (*tobj\nexttag * (SizeOf .l)*2)
      Poke.l *m, tag  ; Here we set the ti_Tag field
      *m + SizeOf .l
      Poke.l *m, v    ; Here we set the ti_Data field
      *tobj\nexttag +1
      result = True
    EndIf
  EndIf

  Function Return result
End Function

; This command MUST be called before exiting to free memory
;
; SYNTAX:
;   DisposeTagObj{&tagobj}
;
; RETURNS:
;   NONE.
Statement DisposeTagObj{*tobj.tagobj}
  If *tobj\mem
    FreeVec_ (*tobj\mem)
    *tobj\mem = 0
  EndIf
End Statement

; This command returns the memory where tags were set.
;
; SYNTAX:
;   addr.l = TagObjAddr{&tagobj}
; RETURNS:
;   memory location where tags are stored.
Function.l TagObjAddr{*tobj.tagobj}
  Function Return *tobj\mem
End Function

#COMPILE_EXAMPLE = 1

CNIF #COMPILE_EXAMPLE = 1

; First of all, we have to create an object:
tobj.tagobj\maxtags = 0,0,0 ; By setting everything to 0, we are sure it will not
                            ; get any strange value inside it during allocation.

If InitTagObj{&tobj, 10}  ; Give us maximum 10 tags, please.

  err.b = SetTag{&tobj, #SA_Left,   0}
  err   = SetTag{&tobj, #SA_Top,    0}
  err   = SetTag{&tobj, #SA_Width,  320}
  err   = SetTag{&tobj, #SA_Height, 256}
  err   = SetTag{&tobj, #SA_Depth,  1}

  a$ = "This is just a little test"

  err   = SetTag{&tobj, #SA_Title, &a$}

  *scr.Screen = OpenScreenTagList_ (0, TagObjAddr{&tobj})



  DisposeTagObj{&tobj}
Else
  NPrint "Allocation Error."
EndIf

MouseWait

If *scr Then CloseScreen_ *scr

End

CEND