Main Menu

Problem With images!!

Started by LemonWizard, October 28, 2009, 05:09:25 PM

Previous topic - Next topic

LemonWizard

Playbasic Compiler Version: 1.63v
IDE Version: 1.1.7


UPDATE:
OKAY the problem appears to only occur some of the time that I run this program.
SOMETIMES it works without a problem.
OTHER Times, the compiler bugs out and tells me that Image #0 is illegal in array blits() or something...
KEVIN HELP!!!

Code:

; PROJECT : TheCode
; AUTHOR  : LemonWizard
; CREATED : 10/28/2009
; EDITED  : 10/28/2009
; ---------------------------------------------------------------------
amount=120
type blitter
xsize, ysize
x
y
speed
direction
endtype
dim blits(amount)
dim blitters(amount) as blitter
for temp=1 to amount
blitters(temp).xsize=rnd(32)
blitters(temp).ysize=rnd(32)
blitters(temp).x=rnd(getscreenwidth() )
blitters(temp).y=rnd(getscreenheight() )
blitters(temp).speed=rnd(20)
blitters(temp).direction=rnd(4)
if blitters(temp).direction <1 then blitters(temp).direction=1
if blitters(temp).direction >4 then blitters(temp).direction=4
tempx=blitters(temp).xsize
tempy=blitters(temp).ysize
blits(temp)=newfximage(tempx, tempy)
rendertoimage blits(temp)
cls rgb(0,0,0)
next temp
wait 5
rendertoscreen

main:
gosub updateblitters
gosub drawblitters
sync
cls Rgb(0,0,0)
goto main:

updateblitters:
for temp=1 to amount
rendertoimage blits(temp)
for tempy=1 to blitters(temp).ysize
for tempx=1 to blitters(temp).xsize
ink RndRGB()
dot tempx, tempy
next tempx
next tempy


;blitters movement

;reset if collides with right wall
if blitters(temp).x >getscreenwidth()
blitters(temp).direction=rnd(4)
if blitters(temp).direction <1 then blitters(temp).direction=1
if blitters(temp).direction >4 then blitters(temp).direction=4
endif

;reset if collides with left wall
if blitters(temp).x <1
blitters(temp).direction=rnd(4)
if blitters(temp).direction <1 then blitters(temp).direction=1
if blitters(temp).direction >4 then blitters(temp).direction=4
endif

;reset if collides with bottom wall
if blitters(temp).y >getscreenheight()
blitters(temp).direction=rnd(4)
if blitters(temp).direction <1 then blitters(temp).direction=1
if blitters(temp).direction >4 then blitters(temp).direction=4
endif

;reset if collides with top wall
if blitters(temp).y <1
blitters(temp).direction=rnd(4)
if blitters(temp).direction <1 then blitters(temp).direction=1
if blitters(temp).direction >4 then blitters(temp).direction=4
endif

;if moving left
if blitters(temp).direction=1
blitters(temp).x=blitters(temp).x-blitters(temp).speed
endif

;if moving right
if blitters(temp).direction=2
blitters(temp).x=blitters(temp).x+blitters(temp).speed
endif

;if moving up
if blitters(temp).direction=3
blitters(temp).y=blitters(temp).y-blitters(temp).speed
endif

;if moving down
if blitters(temp).direction=4
blitters(temp).y=blitters(temp).y+blitters(temp).speed
endif



next temp
rendertoscreen
return

drawblitters:
for temp=1 to amount
myx=blitters(temp).x
myy=blitters(temp).y
drawimage blits(temp), myx, myy, 0
next temp
return


The error:
Image# 0 out of legal range.

The bug:

Whenever I change the values in the type at the top:


for temp=1 to amount
blitters(temp).xsize=rnd(32)
blitters(temp).ysize=rnd(32)

It will work, the first time.. until I change a value.
Then I'll run it again and suddenly my image is out of range.
Or, if I change the amount..


amount=120


how many images can playbasic even handle? They are Fx images.. I see nothing wrong with my code because the minute I reduce a number of images it runs or something... is this a memory related problem not related to playbasic? Or did I actually code something wrong *gasp*

Somone help!

kevin

#1
Quotehow many images can playbasic even handle?

 As many as you need.  You can address a range of about 2^28 (from memory),  but you'll run out of memory long before you even get half way there.

QuoteI see nothing wrong with my code because the minute I reduce a number of images it runs or something... is this a memory related problem not related to playbasic? Or did I actually code something wrong *gasp*

 Sorry, but your example is faulty.    When you generate the random width/height for the image size, these values will be zero inclusive.  So the program is stopping later on, because it couldn't create zero sized imaged (in either the X or Y axis) previously.

   So this,  

   blitters(temp).xsize=rnd(32)
   blitters(temp).ysize=rnd(32)

   Should be something more like this, to stop a possible zero width or height

   blitters(temp).xsize=rndrange(1,32)
   blitters(temp).ysize=rndrange(1,32)

also, see Posting PlayBasic Bug Reports FAQ (login required)

 

LemonWizard

Thanks tons Kevin. Sorry I didn't follow the bug posting format I will try from now on if I ever have another issue. why would that throw an image allocation error? Can playbasic not allocate an image of 0 pixels in width or height? I suppose it makes sense.. but it's still somewhat confusing. I have never had that issue before I guess I never used rnd on an image size enough to get a zero...I need to use rndrange from now on =p.