Menu

Show posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

Show posts Menu

Topics - phaelax

#1
Code Snippets / xml parser
2011-Jan-28
I originally made this with DarkBasic and decided to port it over. I was able to make a few changes since GLB lets me use arrays in Types. There is a bug or two. It's adding the closing tag of a root node to the array when it shouldn't.  And the function which can return a tag's inner content doesn't work properly when it includes the inner content of all it's children. I don't have these issues in the DB version so I'm guessing it's something to do with GLB indices being zero-based while DB starts with 1. Or maybe I just copied something over wrong.  There's a text xml file here: http://zimnox.com/quiz.xml

Any time you call xmlReadFile() you should call xmlClear() first.

xmlReadFile(string)
xmlGetElementCount()
xmlGetTagName(int)
xmlGetAttirbuteValue$(int, string)
xmlAttributeExists(int, string)
xmlGetAttributeKey$(int, int)
xmlGetAttributeCount(int)
xmlGetTagContent$(int, bool)
xmlClear()

Code (glbasic) Select

// --------------------------------- //
// Project: XMLParser
// Author: Phaelax
// Start: Wednesday, January 26, 2011
// IDE Version: 8.078



TYPE AttributeSet
key$
value$
ENDTYPE

TYPE ElementObject
tagName$
parentElementId
content$
pos
parentPos
attributes[0] AS AttributeSet
ENDTYPE

GLOBAL escapes$[]
DIM escapes$[5][2]
escapes$[0][0] = "&lt;"  ; escapes$[0][1] = "<"
escapes$[1][0] = "&gt;"  ; escapes$[1][1] = ">"
escapes$[2][0] = "&amp;" ; escapes$[2][1] = "&"
escapes$[3][0] = "&apos;"; escapes$[3][1] = "'"
escapes$[4][0] = "&quot;"; escapes$[4][1] = CHR$(34)


GLOBAL xmlTags[] AS ElementObject
GLOBAL parseStack[]
DIM xmlTags[0]


//xmlReadFile("c:/quiz.xml")
xmlReadFile("C:/Documents AND Settings/Phaelax.NEWTON64/My Documents/GLBasic/zelda/zelda.gbap")
LOCAL key$
LOCAL y = 0
FOR i = 0 TO xmlGetElementCount()-1
PRINT i+": "+xmlTags[i].tagName$+" -> "+xmlGetTagContent$(i, FALSE), 50, y;INC y, 10
FOR j = 0 TO xmlGetAttributeCount(i)-1
key$ = xmlGetAttributeKey$(i, j)
PRINT key$ + " -> " + xmlGetAttributeValue$(i, key$), 100, y;INC y, 10
NEXT
NEXT


SHOWSCREEN
KEYWAIT
END









FUNCTION xmlReadFile:filename$
LOCAL xmlFileNo = 1
LOCAL L$, tagName$, c$, oldChar$, temp$, unparsedAttributes$
LOCAL matchOpenBracket, tagType, strLength, currentTag
OPENFILE(xmlFileNo, filename$, TRUE)

WHILE ENDOFFILE(xmlFileNo) = FALSE
READLINE xmlFileNo, L$
tagName$ = ""
matchOpenBracket = -1
tagType = 0
strLength = LEN(L$)

FOR i = 0 TO strLength-1
c$ = MID$(L$, i, 1)

//////////////////////////////////////////////
// open bracket found for new tag
//////////////////////////////////////////////
IF c$ = "<"
matchOpenBracket = i
tagType = 0
ENDIF

//////////////////////////////////////////////
// forward slash can either be part of a
// closing container tag, or closing an empty
//////////////////////////////////////////////
IF c$ = "/"
//////////////////////////////////////////////
// If part of a closing tag, the slash will be
// prefixed by the bracket (less-than sign)
//////////////////////////////////////////////
IF oldChar$ = "<" THEN tagType = 1
ENDIF

//////////////////////////////////////////////
// Closing bracket for a tag
//////////////////////////////////////////////
IF c$ = ">"

//////////////////////////////////////////////
// if character before closing bracket was
// a slash, then this bracket closed off an
// empty tag
//////////////////////////////////////////////
IF oldChar$ = "/"
tagType = 2
ELSE
//////////////////////////////////////////////
// "<? ?>" is part of the XML declaration
//////////////////////////////////////////////
IF oldChar$ = "?"
tagType = 2
ELSE
//////////////////////////////////////////////
// Normal close bracket, standard container
//////////////////////////////////////////////
ENDIF
ENDIF
//////////////////////////////////////////////
// If we closed off (completed) the opening
// tag's bracket, then it's open as the current
// container. Add this tag to the container stack
// for tracking the hierarchy and store a new
// tag element in the array
//////////////////////////////////////////////
IF tagType = 0

LOCAL e AS ElementObject
e.pos = matchOpenBracket
temp$ = MID$(L$, matchOpenBracket+1, i-matchOpenBracket-1)
e.tagName$ = TRIM$(UCASE$(LEFT$(temp$, pFindTagNameEndIndex(temp$))))
pParseXmlAttributes(e, TRIM$(RIGHT$(temp$, LEN(temp$)-LEN(e.tagName$))))
e.content$ = ""
//////////////////////////////////////////////
// A parent ID of -1 means it is the root node
//////////////////////////////////////////////
IF LEN(parseStack[]) <= 0
e.parentElementId = -1
ELSE
e.parentElementId = LEN(parseStack[])-1
//////////////////////////////////////////////
// The position within the parent tag's content
// where this tag's data is present
//////////////////////////////////////////////
e.parentPos = LEN(xmlTags[e.parentElementId].content$)
ENDIF
DIMPUSH xmlTags[], e

//////////////////////////////////////////////
// Add the index of the last tag element added
// to the xmlTags array to the stack. This keeps
// track of what container we're in
//////////////////////////////////////////////
DIMPUSH parseStack[], LEN(xmlTags[])-1
ENDIF
//////////////////////////////////////////////
// Closing tag was found, remove last container
// from stack
//////////////////////////////////////////////
IF tagType = 1
DIMDEL parseStack[], -1
ENDIF

//////////////////////////////////////////////
// This was an empty tag element. As they are
// not containers, nothing is added to the stack
// and nothing needs removed. Create a new
// element and add it to the xmlTags array.
//////////////////////////////////////////////
IF tagType = 2
LOCAL e AS ElementObject

//////////////////////////////////////////////
// Checks for special case with XML declaration
//////////////////////////////////////////////
IF oldChar$ <> "?"
temp$ = MID$(L$, matchOpenBracket+1, i-matchOpenBracket-2)
ELSE
temp$ = MID$(L$, matchOpenBracket+2, i-matchOpenBracket-3)
ENDIF

e.tagName$ = TRIM$(UCASE$(LEFT$(temp$, pFindTagNameEndIndex(temp$))))
pParseXmlAttributes(e, TRIM$(RIGHT$(temp$, LEN(temp$)-LEN(e.tagName$))))
e.content$ = ""
IF LEN(parseStack[]) <= 0
e.parentElementId = -1
ELSE
e.parentElementId = LEN(parseStack[])-1
//////////////////////////////////////////////
// The position within the parent tag's content
// where this tag's data begins
//////////////////////////////////////////////
e.parentPos = LEN(xmlTags[e.parentElementId].content$)
ENDIF
DIMPUSH xmlTags[], e
ENDIF

//////////////////////////////////////////////
// Start the whole process over again, the
// container has been closed.
//////////////////////////////////////////////
matchOpenBracket = -1

ELSE
IF matchOpenBracket = -1
LOCAL j = LEN(parseStack[])-1
currentTag = 0
IF j >= 0 THEN currentTag = parseStack[j]
IF currentTag > 0 AND currentTag <= LEN(xmlTags[])
IF LEN(xmlTags[currentTag].content$) > 0
xmlTags[currentTag].content$ = xmlTags[currentTag].content$ + c$
ELSE
IF ASC(c$) <> 32 AND ASC(c$) <> 9 THEN xmlTags[currentTag].content$ = xmlTags[currentTag].content$ + c$
ENDIF
ENDIF

ENDIF
ENDIF
//////////////////////////////////////////////
// Helps keep track of previous characters when
// checking for forward slashes, which are used
// to determine the type of tag
//////////////////////////////////////////////
oldChar$ = c$
NEXT
WEND

CLOSEFILE xmlFileNo
ENDFUNCTION



FUNCTION xmlClear:
REDIM xmlTags[0]
ENDFUNCTION



FUNCTION xmlGetElementCount:
RETURN LEN(xmlTags[])
ENDFUNCTION



FUNCTION xmlGetTagName$:elementId
RETURN xmlTags[elementId].tagName$
ENDFUNCTION



FUNCTION xmlGetAttributeValue$:elementId, key$
FOR j = 0 TO xmlGetAttributeCount(elementId)-1
IF xmlTags[elementId].attributes[j].key$ = key$ THEN RETURN xmlTags[elementId].attributes[j].value$
NEXT
ENDFUNCTION



FUNCTION xmlAttributeExists:elementId, key$
FOR j = 0 TO LEN(xmlTags[elementId].attributes[])-1
IF xmlTags[elementId].attributes[j].key$ = key$ THEN RETURN TRUE
NEXT
RETURN FALSE
ENDFUNCTION



FUNCTION xmlGetAttributeKey$:elementId, index
RETURN xmlTags[elementId].attributes[index].key$
ENDFUNCTION



FUNCTION xmlGetAttributeCount:elementId
RETURN LEN(xmlTags[elementId].attributes[])
ENDFUNCTION



FUNCTION xmlGetTagContent$:elementId, includeChildren
LOCAL content$ = xmlTags[elementId].content$
IF includeChildren = TRUE
LOCAL extendedLength = 0
FOR i = 0 TO LEN(xmlTags[])-1
IF xmlTags[i].parentElementId = elementId
content$ = pInsertString$(content$, xmlTags[i].content$, xmlTags[i].parentPos + extendedLength)
extendedLength = extendedLength + LEN(xmlTags[i].content$)
ENDIF
NEXT
ENDIF
RETURN content$
ENDFUNCTION


FUNCTION pParseXmlAttributes:element AS ElementObject, txt$
LOCAL s=0, x=0, s1=0, quote=34
LOCAL key$, value$

FOR j = 0 TO LEN(txt$)-1
x = INSTR(txt$, "=", s)
key$ = UCASE$(TRIM$(MID$(txt$, s, x-s)))
s = INSTR(txt$, CHR$(34), x)+1
s1 = INSTR(txt$, CHR$(39), x)+1

quote = 34
IF s1 > 0
IF s1 < s OR s < 1
s = s1
quote = 39
ENDIF
ENDIF
x = INSTR(txt$, CHR$(quote), s)

value$ = MID$(txt$, s, x-s)
FOR k = 0 TO BOUNDS(escapes$[], 0)-1
value$ = REPLACE$(value$, escapes$[k][0], escapes$[k][1])
NEXT

LOCAL a AS AttributeSet
a.key$ = key$
a.value$ = value$
DIMPUSH element.attributes[], a

s = x+1
j = x
NEXT
ENDFUNCTION



FUNCTION pFindTagNameEndIndex:tagLine$
LOCAL L = LEN(tagLine$)
FOR i = 0 TO L-1
IF MID$(tagLine$, i, 1) = " " THEN RETURN i
NEXT
RETURN L
ENDFUNCTION



FUNCTION pInsertString$:source$, seg$, pos
LOCAL t$ = LEFT$(source$, pos)
source$ = t$ + seg$ + RIGHT$(source$, LEN(source$)-LEN(t$))
RETURN source$
ENDFUNCTION

#2
Beta Tests / Legend of Zelda
2010-Oct-07
This was suppose to be for the retro competition, which I think was over at the end of September.  I actually started writing in DarkBasic first because I'm more familiar with the syntax (should be after 10 years of using it), and then I would port it over to GLB.  So it's currently behind where I'm actually at with the DB version, but the more I use GLB the quicker I'm able to copy stuff over.  The only major difference between the two codes is how I manage animated sprites.

Haven't had any real trouble writing the game until a few nights ago when I attempted to handle the map secrets, the ones where the screen loops until you go in the correct direction (up,left,down,left  to get to the graveyard).  After a few hours of just thinking, finally got a clean working system for it. Last night I had a thought about a more efficient way to code it merely due to GLB having certain features DB couldn't present me with at the time.  You can walk into some of the caves but can't interact with them yet. Another day or two and should have basic animated enemies, drop items, bombs, whistle, and a sword that actually shoots.

I'm open to suggestions on how to approach the AI.  I know it's pretty basic, but it's where my experience starts to fall behind a little.

Controls are arrow keys, and X, Z for the A, B buttons respectively.  Enter is the start button for bringing up the HUD.

Download is about 1MB.
http://zimnox.com/zelda/zeldagl.zip
#3
2D / diffuse sprite
2010-Sep-22
Unless there's already a way to change to the change the overall tint of a sprite.
#4
I've tried loading an mp3 for music and wavs for sound but can't get either one to play.  No errors, just no sound.
#5
I have a function for grabbing individual tiles from a larger tileset image, function looks something like this:

loadsprite path$, 0
drawsprite 0,0,0
//routine for grabbing individual sprites

The problem I've noticed is that it can only grab what fits within the screen's resolution.  I suppose a work around could be offsetting the tileset sprite as I grab each tile.  Is there a way to read the image data directly from memory and rebuild each tile that way?
#6
Code Snippets / fire routine
2010-Sep-08
My first GL program, a cool yet slow fire routine using a simple method I read about long ago.

Code (glbasic) Select
// --------------------------------- //
// Project: untitled
// Start: Wednesday, December 31, 1969
// IDE Version: 8.078


// c = (a*16777216)+(b*65536)+(g*256)+r

SETSCREEN 640, 480, 0

GLOBAL flame = 4.14
GLOBAL fire[]

DIM fire[320][240]

LOCAL r


REPEAT

FOR y = 0 TO 239
FOR x = 0 TO 319
fire[x][y] = newValue(x, y)
SETPIXEL x, y, fire[x][y]
NEXT
NEXT

FOR x = 0 TO 319
r = RND(255)
fire[x][238] = RGB(r,0,r)
SETPIXEL x, 238, fire[x][238]
NEXT


SHOWSCREEN

UNTIL KEY(28) = 1
END




FUNCTION newValue:x, y
LOCAL r,g,b,c,c1,c2,c3,c4,r1,g1,b1,r2,g2,b2,r3,g3,b3,r4,g4,b4

c1 = getColor(x, y+1)
r1 = GetRValue(c1)
g1 = GetGValue(c1)
b1 = GetBValue(c1)
c2 = getColor(x-1, y+1)
r2 = GetRValue(c2)
g2 = GetGValue(c2)
b2 = GetBValue(c2)
c3 = getColor(x+1, y+1)
r3 = GetRValue(c3)
g3 = GetGValue(c3)
b3 = GetBValue(c3)
c4 = getColor(x, y+2)
r4 = GetRValue(c4)
g4 = GetGValue(c4)
b4 = GetBValue(c4)

r = (r1+r2+r3+r4)/4.14
g = (g1+g2+g3+g4)/4.14
b = (b1+b2+b3+b4)/4.14


c = RGB(r, g, b)

RETURN c
ENDFUNCTION


FUNCTION getColor:x, y
IF x >= 0 AND x < 320 AND y >= 0 AND y < 239 THEN RETURN fire[x][y]
RETURN 0
ENDFUNCTION



FUNCTION GetRValue: Color
RETURN bAND(Color,255)
ENDFUNCTION

FUNCTION GetGValue: Color
RETURN (Color - (INTEGER(Color / 65536)*65536+bAND(Color,255))) / 256
ENDFUNCTION

FUNCTION GetBValue: Color
RETURN INTEGER(Color / 65536)
ENDFUNCTION


I did notice the functions supplied with the GLBasic examples for retrieving color components were incorrect. It appeared they assumed RGB but I guess it's store as BGR.
#7
I just downloaded GLBasic yesterday and thought I'd test it's speed by porting a fire routine over. However, I can't seem to define any arrays. Even copying code from the samples gives me the same error:

"untitled.gbas"(21) error : variable is not explicitly defined : fire

DIM fire[320][240]

So what's wrong? Isn't that statement defining the array variable?