English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Language > Coders. Blitz Basic

 
 
Thread Tools
Old 01 February 2020, 09:18   #1
Havie
Registered User
 
Join Date: Mar 2012
Location: UK
Posts: 512
Correct Way to End Program?

So if I compile and run my program a few times I get an out of memory error. I don't usually worry about it but really I feel I should be ending properly.

Can anyone suggest the correct way to end a program and free up all the memory?

How do you end your program?
Havie is offline  
Old 01 February 2020, 14:06   #2
Etze
A3000-Fan

Etze's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 193

End



I never had such problems - I thought BB takes care of all the garbage - at least if I end the programm correctly with "End".
Etze is offline  
Old 01 February 2020, 19:37   #3
Havie
Registered User
 
Join Date: Mar 2012
Location: UK
Posts: 512
It has an End but keeps coming up with a memory error after a few compiles which makes me think that perhaps BB2 isn't releasing all the memory...

Any helpful suggestions - not really a problem as doesn't affect the game but it's annoying nevertheless!

Last edited by Havie; 02 February 2020 at 17:41.
Havie is offline  
Old 02 February 2020, 16:29   #4
Daedalus
Registered User

Daedalus's Avatar
 
Join Date: Jun 2009
Location: Dublin, then Glasgow
Posts: 4,509
End takes care of most things, like standard Blitz objects etc., but if you have any routines or 3rd party commands that allocate memory, they might need resources to be manually freed at the end of the program. Alternatively, it could simply me memory fragmentation that's not being properly tidied up, causing you to run out if you don't have much spare.
Daedalus is offline  
Old 08 February 2020, 13:21   #5
Havie
Registered User
 
Join Date: Mar 2012
Location: UK
Posts: 512
Me again:

I am still having the memory issue and when I run my game on a real Amiga (1200 030 +16mb) it suspends when exiting. I have obviously done something wrong but have no idea what? SO I thought I'd post the code that is causing the problem for all your experts.

Code:
WBStartup


If OpenFile (0,"highscoret.dat")=-1
Fields 0,highold
Get 0,0
CloseFile 0
high=highold

End If


a$="Dragonfire Amiga conversion by H. Spring 2019. Press  SPACE to start.  Based on the Atari VCS "
a$=a$+"Version by Imagic.  Avoid the fireballs and get into the castle to steal the dragon's "
a$=a$+"treasure. (c) Copyright Havsoft 2019........"

BitMap 0,320+8,256,4
BitMap 1,320+8,256,4

LoadShapes 0,39,"graphics/images"

LoadShape 42,"graphics/door"
LoadShape 43,"graphics/opendoor"
loadshape 44,"graphics/dragon3c"
loadshape 45,"graphics/dragon4c"
LoadShape 99,"graphics/dragonscreen",2


;load castle background
LoadShape 40,"Graphics/castle.iff",0
LoadShape 41,"Graphics/castle2.iff"
Use BitMap 0: Blit 40,0,0
Use BitMap 1: Blit 40,0,0

LoadPalette 0,"graphics/palette1"
LoadPalette 2,"graphics/palette2"

LoadSound 1,"sounds/jump.iff"
LoadSound 2,"sounds/fireball.iff"
LoadSound 3,"sounds/splash.iff"
LoadSound 4,"sounds/die.iff"

LoadModule 0,"sounds/dragonfire.mod"

Dim t(10),tx(10),ty(10)
Dim fx1(10),fy1(10)

newgame:
;set up bitmap...
BLITZ

InitCopList 1,44,200,$00006+$40,8,32,0
DisplayPalette 1,0
CreateDisplay 1

DisplayBitMap 1,0


Use BitMap 0:Cls 1

;BLITZ
DisplayPalette 1,2
Block 99,0,0



PlayModule 0

;Title Screen with scrolling message

a=1:b= Len(a$):c=1

While Joyb(1)=0

If c=1
  BitMapOutput 0: Colour 15
  b$=Mid$(a$,a,1):Locate 39,23: Print b$
  a=a+1: If a>b Then a=1
End If

Scroll 1,184,319,8,0,184

c=c+1: If c=8 Then c=1

;PalRGB 0,1,Rnd(16),Rnd(16),Rnd(16)

VWait

Wend

InitCopList 0,44,200,$00004,8,16,0
DisplayPalette 0,0
CreateDisplay 0

DisplayBitMap 0,0

;display castle background
Use BitMap 0: Block 40,0,0
Use BitMap 1: Block 40,0,0



StopModule
;
;LoadModule 0,"sounds/tune2.mod" :PlayModule 0
;BLITZ

db=0

Use BitMap 0

Buffer 0,16384
Buffer 1,16384


;player speed
ps=2
lives=5: score=0 :ct=0: fireball=160

;fireball speeds
speed=10:fb1speed=2:fb2speed=4

restart:
x=300:y.f=127: jump=0:acc.f=3
an=0: dir=1
dr=1: pos=1:dragon=0
fx1=0:fx2=0
level=1

water=179:wdir=-1 :water2=179
inside=0 : drx=20:dry=150:drdir=1
count=0
c=1

While RawStatus(69)=0

UnBuffer db: Use BitMap db

duck=0

;make water move
ct=ct+1
If ct>2 Then water=water+wdir:ct=0 :water2=water2+(wdir*-1)

If wdir=-1 Then Line 0,water,145,water,11: Line 170,water2,319,water2,0
If wdir=1 Then Line 0,water,145,water,0:Line 170,water2,319,water2,11

If water+wdir=176 Then wdir=-wdir
If water+wdir=183 Then wdir=wdir*-1

;hit by fireball
If dead=1
  If jump=0 Then jump=1

  If jump=1 Then acc=acc-.25: y=y-acc: If acc<0 Then acc=0:  jump=2
  If jump=2 Then acc=acc+.25: y=y+acc: If y>127  Then jump=3: Sound 3,15

  If jump=3 y=y+acc: If y>180 Then Goto lifelost
  Goto jumping
End If

;control
 If Joyy(1)=1 Then duck=1: Goto ducked
 If Joyx(1)=1 Then x=x+ps: dir=-1:an=an+.2:If x>300 Then x=300
 If Joyx(1)=-1 Then x=x-ps:dir=1:an=an+.2:If x<0 Then x=0
 ducked:
 If Joyb(1)=1 AND jump=0 Then jump=1: Sound 1,15

 If an>6 Then an=0
 If jump=1 Then acc=acc-.2: y=y-acc: If acc<0 Then acc=0:  jump=2
 If jump=2 Then acc=acc+.2: y=y+acc: If y>127  Then jump=0: acc=3:y=127

jumping:

BitMapOutput db:Colour 15:Locate 0,0: Print "SCORE ":Print score
;Locate 10,10: Print speed:Print" "

If inside=0

If dead=1 AND dir=1 Then BBlit db,6,x,y: Goto loop
If dead=1 AND dir=-1 Then BBlit db,0,x,y: Goto loop

If duck=1 AND jump=0
   If dir=1 Then BBlit db,12,x,y
   If dir=-1 Then BBlit db,13,x,y
   Goto loop
End If


If jump>0
  If dir=1 Then BBlit db,20,x,y
  If dir=-1 Then BBlit db,21,x,y
  Goto loop
End If

If dir=1 Then BBlit db,0+6+an,x,y
If dir=-1 Then BBlit db,0+an,x,y

loop:

If dragon<>1000 Then dragon=dragon+1
If dragon>speed Then  dragon=1000


If dragon<>1000 Then Goto loopy

;draw dragon
If pos=1
  If dr<22 Then BBlit db,22,dr,130 Else If dr<40  BBlit db,22,20,130
  If dr>39 Then BBlit db,22,60-dr,130
  If fx1=0 AND dr=22 Then fx1=27 :Sound 2,6
End If

If pos=0
  If dr<22 Then BBlit db,22,dr,122 Else If dr<40  BBlit db,22,20,122
  If dr>39 Then BBlit db,22,60-dr,122
  If fx2=0 AND dr=22 Then fx2=27: Sound 2,6
End If

dr=dr+1
If dr>60 Then dr=0:pos=1-pos: dragon=0

loopy:

 If fx1>0 Then BBlit db,24,fx1,134: fx1=fx1+fb1speed: If fx1>290 Then fx1=0:score=score+10
 If fx2>0 Then BBlit db,24,fx2,124: fx2=fx2+fb2speed: If fx2>290 Then fx2=0:score=score+10

;check collision

  If duck=1
     If RectsHit (x,y+9,10,12,fx1,134,9,4)=-1 AND dead=0 Then dead=1: Sound 4,15
     If RectsHit (x,y+9,10,12,fx2,124,9,4)=-1 AND dead=0 Then dead=1:  Sound 4,15
     Goto looped
  End If

  If jump=1
     If RectsHit(x,y,11,15,fx1,134,9,4)=-1 AND dead=0 Then dead=1: Sound 4,15
     If RectsHit(x,y,11,15,fx2,124,9,4)=-1 AND dead=0 Then dead=1: Sound 4,15
     Goto looped
  End If

  If RectsHit(x,y,11,15,fx1,134,9,4)=-1 AND dead=0 Then dead=1:Sound 4,15
  If RectsHit(x,y,11,15,fx2,124,9,4)=-1 AND dead=0 Then dead=1: Sound 4,15

looped:

;draw walls at sides
Blit 30,0,79: Blit 31,299,79

End If

If x=10 Then inside=1:level=level+1
If inside=1 Then Gosub inside

;draw lives
For i= lives To 1 Step -1
  Blit 20,318-(i*16),5
Next i

 ;wait for vertical blank
  VWait

  db=1-db

Wend

End
Havie is offline  
Old 08 February 2020, 17:59   #6
Master484
Registered User
Master484's Avatar
 
Join Date: Nov 2015
Location: Vaasa, Finland
Posts: 424
I noticed at least one thing in the code.

Under the "draw dragon" section, and in some other places, you use the "Else If" command:

If dr<22 Then BBlit db,22,dr,130 Else If dr<40 BBlit db,22,20,130

I think Blitz doesn't support the "Else If" structure like some other basics do. So I think Blitz interpretes this an "Else" branch for the first "If", and after this it sees the second "If" coming after the "Else", which starts a new "IF-THEN" structure. And this second "If" structure is never closed with "End If", which can certainly cause problems, like eventually running out of memory when the main loop starts more and more IF structures but never ends them.

So try changing this, and all other "Else Ifs" to something like this:

If dr < 22
BBlit db,22,dr,130
Else
If dr < 40 Then BBlit db,22,20,130
End if


---
---

Also at the start of the program you make two arrays:
Dim fx1(10),fy1(10)

And then later, in the "restart" section you make a variable which has exactly the same name as the first array:
fx1=0:fx2=0

Although I think that Blitz allows this, but still it can be somewhat confusing.

---

Also if you have music playing when the program exits, then it might be best to do "StopModule" before the "End". Although Blitz should automatically stop the musics, but you never know.

---
---

Also one other thing that I noticed is that you use float variables (.f) in your code.

In Blitz all variables use the type "quick" (.q) by default, which supports fractional values like "0.25" , so you don't need to use floats (.f) for those.

For example right now in your code, under the "control" section, you have this line:
If Joyx(1)=1 Then x=x+ps: dir=-1:an=an+.2:If x>300 Then x=300

A fractional value is added to the "an" variable, although eariler in the code you have defined "an" as a normal variable, and not a float variable. But it works, because the default Blitz variable type "Quick" supports decimal values.

So, you don't have any need to actually use Floats for anything. The default Quick variables are much faster, and I think their accuracy is 4 fractional numbers after the decimal point. The higher precision floats are slow and only needed in mathematical applications.

But integer words ".w" are the fastest Blitz variable type, and usually I use them whenever possible. By putting "DEFTYPE .w" to the beginning of your program, all new variables become integers by default. This makes calculations, such as collision detection, a lot faster. But of course integers can't hold fractional decimal values, so when that's needed, then you must remember to manually define those variables as Quicks.
Master484 is offline  
Old 08 February 2020, 22:36   #7
Havie
Registered User
 
Join Date: Mar 2012
Location: UK
Posts: 512
Wow - that's a really helpful response - thanks!

I never realised (obviously) about the If then else thing.

Also, I will have a look at my .f variables - you are quite right.

Lots to think about...
Havie is offline  
Old 09 February 2020, 16:14   #8
Havie
Registered User
 
Join Date: Mar 2012
Location: UK
Posts: 512
Have reformatted the if statement - no difference; still getting the error.

Got rid of the .f as not needed (doh).

Found the problem - the music. Not sure why it's causing a problem but if I take it out, everything works well. Never really liked it in the first place - will try a different mod.
Havie is offline  
Old 09 February 2020, 18:40   #9
Master484
Registered User
Master484's Avatar
 
Join Date: Nov 2015
Location: Vaasa, Finland
Posts: 424
Quote:
Found the problem - the music. Not sure why it's causing a problem but if I take it out, everything works well.
Ok.

You can also test putting "Free Module" before the "End", and see if it still crashes when program closes.

But the standard Blitz "LoadModule" command set itself is not so good, and many people have had troubles with it. In my own projects it has never worked as it should, mods sounded like they played at 5x speed and so on.

The other default Blitz music command set, "LoadMedModule", is much better, but it only supports ".med" modules.

And also there is this new module command set that might be worth trying out if playing music continues to cause crashes:
https://eab.abime.net/showthread.php?t=87959
Master484 is offline  
Old 09 February 2020, 22:55   #10
Havie
Registered User
 
Join Date: Mar 2012
Location: UK
Posts: 512
Tried Free Module but it didn't work. Haven't had problems before so need a think!

Will have a look at Meds initially otherwise additional library seems like a good plan.

Thanks again.
Havie is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding start and end of my program's memory? Warty Coders. C/C++ 8 03 January 2019 11:24
External windows program communicating with program running inside WinUAE xxxxx support.WinUAE 10 19 February 2013 09:27
For a beginner what Program and Program language would you recommend? amigang New to Emulation or Amiga scene 5 27 March 2012 13:06
is this correct? CU_AMiGA MarketPlace 1 28 September 2007 20:05
Calling another program from a compiled Amos program...? andyr support.Apps 3 24 October 2004 23:47

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 10:15.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, vBulletin Solutions Inc.
Page generated in 0.07501 seconds with 15 queries