IonicWind Software

Creative Basic => General Questions => Topic started by: Techno on June 12, 2015, 12:33:08 AM

Title: Runtime error on line 2 ????
Post by: Techno on June 12, 2015, 12:33:08 AM
Dear support Graham

What wrong here. When I run my Creative Basic application it give me an runtime error:

"An Error has occured
Line Number 2
Please contact the program vendor"


' A Christmas Card  - 2009
' Creative Basic Code - GWS

autodefine "off"

'===================================================================
'Globals
'===================================================================
DEF w : WINDOW /* hWnd */
DEF style : INT
DEF i : INT
DEF n : INT
DEF nf : INT
DEF run : INT
DEF t : INT
DEF tf : INT
DEF red : INT
DEF grn : INT
DEF blu : INT
DEF c[10] : INT
DEF can[10,4] : INT
DEF f[10] : INT
DEF fp : INT
DEF fx : FLOAT
DEF fy : FLOAT
DEF twopi : FLOAT
DEF th : FLOAT

DECLARE "kernel32",GetTickCount(),int

twopi = 8 * atan(1)
th  = 0.0
style = @MINBOX|@MAXBOX|@SIZE|@NOAUTODRAW|@SIZE

'=================================================================
'Main Program Start here
'=================================================================
WINDOW w,-1200,0,1024,768,style,0,"Creative Christmas - 2009", mainHandler
SETWINDOWCOLOR w,rgb(0,0,30)

' create a DirectX screen ...
CREATESCREEN(w,1024,768)
SETFONT w,"Times New Roman",54,600,@SFITALIC
DRAWMODE w, @TRANSPARENT

' load candle sprites ..
c[1] = DXSPRITE(w,GETSTARTPATH + "c11.bmp",60,100,1,0)
c[2] = DXSPRITE(w,GETSTARTPATH + "c12.bmp",60,100,1,0)
c[3] = DXSPRITE(w,GETSTARTPATH + "c13.bmp",60,100,1,0)
c[4] = DXSPRITE(w,GETSTARTPATH + "c14.bmp",60,100,1,0)
c[5] = DXSPRITE(w,GETSTARTPATH + "c15.bmp",60,100,1,0)
c[6] = DXSPRITE(w,GETSTARTPATH + "c16.bmp",60,100,1,0)
c[7] = DXSPRITE(w,GETSTARTPATH + "c17.bmp",60,100,1,0)
c[8] = DXSPRITE(w,GETSTARTPATH + "c18.bmp",60,100,1,0)
c[9] = DXSPRITE(w,GETSTARTPATH + "c19.bmp",60,100,1,0)

FOR i = 1 to 9
DXSETSPRITEDATA c[i],@SDBLTTYPE,@BLTTRANSSCALED
DXSETSPRITEDATA c[i],@SDSCALE,0.2
next i

' set candle x,y positions and starting sprite number ..
can[1,1] = 405
can[1,2] = 349
can[1,3] = 1

can[2,1] = 780
can[2,2] = 329
can[2,3] = 5

can[3,1] = 110
can[3,2] = 254
can[3,3] = 3

can[4,1] = 147
can[4,2] = 240
can[4,3] = 4

can[5,1] = 162
can[5,2] = 254
can[5,3] = 7

can[6,1] = 173
can[6,2] = 258
can[6,3] = 6

' load fire sprites ..
f[1]  = DXSPRITE(w,GETSTARTPATH + "fm1.bmp",230,180,1,0)
f[2]  = DXSPRITE(w,GETSTARTPATH + "fm2.bmp",230,180,1,0)
f[3]  = DXSPRITE(w,GETSTARTPATH + "fm3.bmp",230,180,1,0)
f[4]  = DXSPRITE(w,GETSTARTPATH + "fm4.bmp",230,180,1,0)
f[5]  = DXSPRITE(w,GETSTARTPATH + "fm5.bmp",230,180,1,0)
f[6]  = DXSPRITE(w,GETSTARTPATH + "fm6.bmp",230,180,1,0)
f[7]  = DXSPRITE(w,GETSTARTPATH + "fm7.bmp",230,180,1,0)
f[8]  = DXSPRITE(w,GETSTARTPATH + "fm8.bmp",230,180,1,0)
f[9]  = DXSPRITE(w,GETSTARTPATH + "fm9.bmp",230,180,1,0)
f[10] = DXSPRITE(w,GETSTARTPATH + "fm10.bmp",230,180,1,0)

FOR i = 1 TO 10
DXSETSPRITEDATA f[i],@SDBLTTYPE,@BLTTRANSSCALED
DXSETSPRITEDATA f[i],@SDSCALE,0.9
NEXT i

' load fireplace sprite ..
fp = DXSPRITE(w,GETSTARTPATH + "fireplace.bmp",265,195,1,0)
DXSETSPRITEDATA fp,@SDBLTTYPE,@BLTTRANS

' play background music ..
PLAYWAVE GETSTARTPATH + "xmas.wav", @sndasync|@sndloop
STARTTIMER w,100,1
run = 1

' load background image ...
IF DXNEWMAP(w,GETSTARTPATH + "pub6.jpg",1024,768,1) = 0
MESSAGEBOX w, "Could not load the background image","Error"
run = 0
ENDIF
CENTERWINDOW w

' create a blank map filled with tile#0 ...
DXCREATEMAP w,1,1,0
DXDRAWMAP w

n  = 1
t  = GetTickCount()
nf = 1
tf = t

WAITUNTIL run = 0
STOPTIMER w,1
STOPTIMER w,2
PLAYWAVE "",0
CLOSEWINDOW w
END

mainHandler:
SELECT @CLASS
CASE @IDDXUPDATE
update

CASE @IDCLOSEWINDOW
  run = 0

CASE @IDTIMER
' slowly change the text color ..
th = th + 0.04
red = 127.5 * (1 + cos(th))
grn = 127.5 * (1 + cos(th + twopi / 3))
blu = 127.5 * (1 + cos(th + 2 * twopi / 3))
ENDSELECT
RETURN

SUB update
'draw the background
DXDRAWMAP w

' update candles every 250 msec
IF GetTickCount() - t > 250
t = GetTickCount()
FOR i = 1 TO 6
n = can[i,3] + 1
IF n > 9 THEN n = 1
can[i,3] = n
NEXT i
ENDIF

' draw candle sprites ..
FOR i = 1 TO 6
n = can[i,3]
DXMOVESPRITE c[n],can[i,1],can[i,2]
DXDRAWSPRITE w, c[n]
NEXT i

' update fire every 120 msec
IF GetTickCount() - tf > 120
tf = GetTickCount()
nf = nf + 1
IF nf > 10 THEN nf = 1

' draw new fire sprite ..
DXMOVESPRITE f[nf],508,363
DXDRAWSPRITE w, f[nf]
ELSE
' redraw existing fire sprite ..
DXMOVESPRITE f[nf],508,363
DXDRAWSPRITE w, f[nf]
ENDIF

' draw the fireplace mask ..
DXMOVESPRITE fp,494,338
DXDRAWSPRITE w, fp

FRONTPEN w,RGB(red,grn,blu)
MOVE w,300,640
PRINT w,"Happy Christmas"

'show the changes
DXFLIP w
RETURN
Title: Re: Runtime error on line 2 ????
Post by: GWS on June 12, 2015, 05:17:01 AM
Techno,

You appear to be jumping from one project to another every few hours ..  ::)
I can't see how you can learn anything that way except how to generate lots of error messages ..

The Xmas 2009 project was uploaded in two parts because of the upload limits on .zip file sizes.

Make a new directory called Xmas2009.  Download both parts from here into it:
http://www.ionicwind.com/forums/index.php?topic=3776.0 (http://www.ionicwind.com/forums/index.php?topic=3776.0)  and here:
http://www.ionicwind.com/forums/index.php?topic=3777.0 (http://www.ionicwind.com/forums/index.php?topic=3777.0)

Unzip them to gain access to all the sprites, and it should then run.

You appear to have re-written the definitions at the top of the program - and on separate lines  :o
There's no need to do that.  See the definition lines in the original program.

Your 'line 2' error was because, for some reason, you'd inserted a 'C style' comment after the Window definition. (/* hWnd */)  This is not the correct format for a same-line comment in CB.
See the User Guide and various example programs.

Since you are in fact writing two statements on one line, they need to be separated by a colon :

Then you write the comment, preceded by a single quote ', thus :'this is a comment
No characters are required to finish the comment.

You also seem to have redefined the size of the array f[] to have only 10 elements - it needs 11 to hold all the sprites.

Maybe you are trying to re-write the program for IWB.  It can be done, but it serves no purpose.  You will see no difference when you run it.  You will just give yourself a headache trying to pin down all the C -based incongruities between the two systems.

Keep to the simplicity of CBasic as discussed in the User Guide, and all should be well ..

Best wishes, :)

Graham



Title: Re: Runtime error on line 2 ????
Post by: GWS on June 13, 2015, 03:16:41 AM
Hmm .. no reply ..

Well let's use this example as a learning experience.

The starting point is that the original program when downloaded and extracted, is working correctly.

Therefore any new problem is going to be due to any changes made to the code.

You have to behave like Sherlock Holmes to discover errors.

In this case, CBasic points you directly to line2 (ignoring any comment lines).
This is the starting point.

The line has been modified from the original by adding some text - '/* hWnd */ to the definition of Window w.

This appears to have been intended as a comment.  This gives another clue what the problem is.

Comments are syntax color coded - Comments are shown in Green - but as written /* hWnd */, this is not highlighted in Green.  This shows that the text has not been recognised as a comment.

Now we are back to the User Guide to show how Comments are entered in CBasic.
Clearly the 'C style' format is not valid.
What is required is a simple leading single quote, followed by the desired text.
Thus:    :' hWnd
(I won't comment on why this comment is made since a handle to the window is not needed in CBasic).

Also to write a comment on the same line as a statement, means we are really writing two statements on the same line.  The User Guide again show that this requires a ':' to separate the two statements.

So, we need a ':' for sure, followed by the comments as shown above -

Changing the text to :' hwnd immediately changes colour to Green, showing that it is now a valid comment.

If you run the modified program, you will get another 'error' indicating array f[] is being addressed out of bounds.

The definitions have been modified from the original.  Examining array f[] shows that it is now defined as f[10] whereas before it was sized as f[11].

In other words, it is not now big enough to accept all the data being loaded.  This is the common problem of forgetting that arrays are 'Zero based'.  Which means that the first storage location is numbered '0'.
So in order to load 10 items, beginning at location 1, we need an extra location numbered 11.

Changing the definitions of array f[] back to it's original value '11' cures the problem, and the program now runs correctly.

As pointed out previously, there is no need to place each variable definition on a separate line.

I hope this helps with debugging methods .. ;D

Best wishes, :)

Graham