Image loading and UDT trouble

Game development specific discussions.
Post Reply
IvanisIvan
Posts: 44
Joined: Nov 07, 2019 21:57

Image loading and UDT trouble

Post by IvanisIvan »

G'day! I am very much a beginner in freeBASIC despite coding in it since 2019. My friend's birthday is coming and I thought it would be fun to code him a game. I am re-using the image loading code from about 2 years ago so it is old and may have received it from someone but I do not remember.

The issue
The issue I have run into is that the image fails to load when using user defined types. I am sure there is something simple I am glossing over. I have also tested it by copying the image to a folder with another program that I know can load other images, and pasting the code there to test if that was an issue, but it also gave me an error. Below I have attached the code, an image of both files in the folder, and the image I am trying to load.

Thank you for any help I may receive!

Code: Select all

type shipWeapon
    damage as integer
    health as integer
end type

type ship
    fileName as string
    imagePtr as any ptr
    health as integer
    weapons(any) as shipWeapon
end type

dim occult as ship

occult.fileName = "testbg.bmp"
occult.imagePtr = ImageCreate(256,256)

if BLoad(occult.fileName, occult.imagePtr) <> 0 then
   print "Error loading: " & occult.fileName
   sleep
   end
end if

sleep
https://www.flickr.com/photos/151891031 ... ed-public/

https://www.flickr.com/photos/151891031 ... ed-public/
Last edited by IvanisIvan on Dec 17, 2021 7:50, edited 1 time in total.
IvanisIvan
Posts: 44
Joined: Nov 07, 2019 21:57

Re: Image loading and UDT trouble

Post by IvanisIvan »

IvanisIvan wrote:
btw... I have updated my code down below and attached the error code:

D:\My Drive\FreeBASIC Projects\william's game\FBIDETEMP.bas(53) error 24: Invalid data types, before ',' in 'dim occult as ship = "testbg.bmp", 10, 1, {laser, bolter}'

Code: Select all

type shipWeapon
    identifier as string
    damage as integer
    AP as integer
    health as integer
    declare constructor ()
    declare constructor (byval i as string, byval d as integer, byval a as integer, byval h as integer)
end type

constructor shipWeapon()
    this.identifier = ""
    this.damage = 0
    this.AP = 0
    this.health = 0
end constructor

constructor shipWeapon(byval i as string, byval d as integer, byval a as integer, byval h as integer)
    
end constructor

type ship
    fileName as string
    imagePtr as any ptr
    health as integer
    armor as integer
    weapons(any) as shipWeapon
    declare constructor ()
    declare constructor (byval f as string, byval h as integer, byval a as integer, w (any) as shipWeapon)
end type

constructor ship()
    this.health = 0
    this.armor = 0
    redim this.weapons(0)
    
    this.fileName = ""
    this.imagePtr = ImageCreate(256,256)
    
    if BLoad(this.fileName, this.imagePtr) <> 0 then
        print "Error loading: " & this.fileName
        sleep
        end
    end if
end constructor

constructor ship(byval f as string, byval h as integer, byval a as integer, w(any) as shipWeapon)
    this.health = h
    this.armor = a
    redim this.weapons(len(w))
    for i as integer = 0 to len(w) step 1
        this.weapons(i) = w(i)
    next i
    
    this.fileName = f
    this.imagePtr = ImageCreate(256,256)
    
    if BLoad(this.fileName, this.imagePtr) <> 0 then
        print "Error loading: " & this.fileName
        sleep
        end
    end if
end constructor

dim laser as shipWeapon

laser.identifier = "laz"
laser.damage = 2
laser.AP = -1
laser.health = 2

dim bolter as shipWeapon

bolter.identifier = "blt"
bolter.damage = 3
bolter.AP = 0
bolter.health = 3

dim arr(1) as shipWeapon = {laser, bolter}

dim occult("testbg.bmp",10,1, arr(1)) as ship

sleep
paul doe
Moderator
Posts: 1730
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Image loading and UDT trouble

Post by paul doe »

IvanisIvan wrote:...
D:\My Drive\FreeBASIC Projects\william's game\FBIDETEMP.bas(53) error 24: Invalid data types, before ',' in 'dim occult as ship = "testbg.bmp", 10, 1, {laser, bolter}'
...
The compiler is complaining about the {laser, bolter} part which, AFAIK, is unsupported. It has nothing to do with the image loading itself:

Code: Select all

type shipWeapon
    identifier as string
    damage as integer
    AP as integer
    health as integer
end type

type ship
    fileName as string
    imagePtr as any ptr
    health as integer
    armor as integer
    weapons(any) as shipWeapon
    'declare constructor (byval fileName as string, byval imagePtr as any ptr, byval health as integer, byval armor as integer, weapons(any) as shipWeapon)'byval size as integer)
    declare constructor (f as string, byval h as integer, byval a as integer, w() as shipWeapon)
end type

'constructor ship(byval size as integer)
'    redim this.weapons(size)
'end constructor

constructor ship(f as string, byval h as integer, byval a as integer, w() as shipWeapon)
    this.health = h
    this.armor = a
    
    '' REDIM the weapons() array first! Otherwise you'll get a segmentation violation!
    redim this.weapons( 0 to ( ubound( w ) - lbound( w ) ) )
    
    for i as integer = 0 to ubound( this.weapons )
        this.weapons(i) = w(i)
    next i
   
    this.fileName = f
    'this.imagePtr = ImageCreate(256,256)
   
    'if BLoad(this.fileName, this.imagePtr) <> 0 then
    '    print "Error loading: " & this.fileName
    '    sleep
    '    end
    'end if
end constructor

dim laser as shipWeapon

laser.identifier = "laz"
laser.damage = 2
laser.AP = -1
laser.health = 2

dim bolter as shipWeapon

bolter.identifier = "blt"
bolter.damage = 3
bolter.AP = 0
bolter.health = 3

'' This syntax is not supported
''dim occult as ship = Ship( "testbg.bmp", 10, 1, { laser, bolter } )

dim as shipWeapon weapons( ... ) = { laser, bolter }
dim occult as ship = Ship( "testbg.bmp", 10, 1, weapons() )

screenRes( 800, 600, 32 ) '' Or whatever; just set a screen mode before using any gfx commands, including ImageCreate

occult.imagePtr = ImageCreate(256,256)

if BLoad(occult.fileName, occult.imagePtr) <> 0 then
    print "Error loading: " & occult.fileName
    sleep
    end
end if

'' Test to see if it shows
'put( 0, 0 ), occult.imagePtr, pset

sleep
That compiles as expected. See if you can make it work with your game.
paul doe
Moderator
Posts: 1730
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Image loading and UDT trouble

Post by paul doe »

Here is a little function that can ease your pains a little if you learn how to use the Fb.Image buffers:

Code: Select all

#include once "file.bi"
#include once "fbgfx.bi"

function loadBMP( byref path as const string ) as Fb.Image ptr
  #define __BM_WINDOWS__ &h4D42
  
  type __BITMAPFILEHEADER__ field = 1
    as ushort id
    as ulong size
    as ubyte reserved( 0 to 3 )
    as ulong offset
  end type
  
  type __BITMAPINFOHEADER__ field = 1
    as ulong size
    as long width
    as long height
    as ushort planes
    as ushort bpp
    as ulong compression_method
    as ulong image_size
    as ulong h_res
    as ulong v_res
    as ulong color_palette_num
    as ulong colors_used
  end type
  
  dim as any ptr img = 0
  
  if( fileExists( path ) ) then
    dim as __BITMAPFILEHEADER__ header 
    dim as __BITMAPINFOHEADER__ info
    
    dim as long f = freeFile()
    
    open path for binary as f
      get #f, , header
      get #f, sizeOf( header ) + 1, info
    close( f )
    
    '' Check if the file is indeed a Windows bitmap
    if( header.id = __BM_WINDOWS__ ) then
      img = imageCreate( info.width, info.height )
      bload( path, img )
    end if
  end if
  
  return( img )
end function
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Image loading and UDT trouble

Post by dodicat »

Before you bload or bsave or imagecreate you must have a graphics screen.
You must dimension (or re dimension) any array before trying to fill it.
You should pass arrays() as parameters, I use temp() to hold info.
optional use operator cast() to print the shipWeapon information.

Mess around:

Code: Select all

type shipWeapon
    identifier as string
    damage as integer
    AP as integer
    health as integer
    declare operator cast() as string 'to use print
end type

operator shipWeapon.cast() as string
return identifier + ","+str(damage)+","+str(AP)+","+str(health)
end operator

type ship
    fileName as string
    imagePtr as any ptr
    health as integer
    armor as integer 
    weapons(any) as shipWeapon
    'declare constructor (byval fileName as string, byval imagePtr as any ptr, byval health as integer, byval armor as integer, weapons(any) as shipWeapon)'byval size as integer)
    declare constructor (byval f as string, byval h as integer, byval a as integer, w() as shipWeapon)
end type

'constructor ship(byval size as integer)
'    redim this.weapons(size)
'end constructor

constructor ship(byval f as string, byval h as integer, byval a as integer, w() as shipWeapon)
    this.health = h
    this.armor = a
    redim this.weapons(ubound(w))
    for i as integer = 0 to ubound(w) step 1
        this.weapons(i) = w(i)
    next i
   
    this.fileName = f
    this.imagePtr = ImageCreate(256,256)
    for i as integer=0 to ubound(w)
          draw string imagePtr,(10,i*20),weapons(i)
          next i
    put(100,100),this.imagePtr,pset
   
    if BLoad(this.fileName, this.imagePtr) <> 0 then
        print "Error loading: " & this.fileName
        sleep
        end
    end if
end constructor

dim laser as shipWeapon

laser.identifier = "laz"
laser.damage = 2
laser.AP = -1
laser.health = 2

dim bolter as shipWeapon

bolter.identifier = "blt"
bolter.damage = 3
bolter.AP = 0
bolter.health = 3

screen 19,32

print "hello from bitmap, press any key to end . . ."
bsave("testbg.bmp",0)
dim as shipweapon temp(0 to 1)={laser,bolter}
dim occult as ship = ship("testbg.bmp", 10, 1,temp())

'dim occult as ship = 5
'occult.health = 10
'occult.armor = 1
'occult.weapons(0) = laser

'occult.fileName = "testbg.bmp"
'occult.imagePtr = ImageCreate(256,256)

'if BLoad(occult.fileName, occult.imagePtr) <> 0 then
'    print "Error loading: " & occult.fileName
'    sleep
'    end
'end if
sleep
kill "testbg.bmp" 
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Image loading and UDT trouble

Post by dodicat »

For fun:

Code: Select all

type shipWeapon
    identifier as string
    damage as integer
    AP as integer
    health as integer
    declare operator cast() as string 'to use print
end type

operator shipWeapon.cast() as string
return identifier + ","+str(damage)+","+str(AP)+","+str(health)
end operator

type ship
    fileName as string
    imagePtr as any ptr
    health as integer
    armor as integer
    weapons(any) as shipWeapon
    'declare constructor (byval fileName as string, byval imagePtr as any ptr, byval health as integer, byval armor as integer, weapons(any) as shipWeapon)'byval size as integer)
    declare constructor (byval f as string, byval h as integer, byval a as integer, w() as shipWeapon)
end type


sub create(im as any ptr)
      Dim  As String boat = _
"S1C0BM110,208M+4,42M+7,7M+1,15M+-6,2M+1,6"_
&"M+38,-1M+-1,-7M+-32,0"_
&"BM+14,4P4294932224,0"_
&"BM+-2,4M+6,7"_
&"M+-2,10M+2,5M+2,17M+2,20M+-1,2M+260,0"_
&"M+16,-10M+9,-13M+5,-16M+5,-11M+14,-7M+14,-6"_
&"M+10,-8M+-55,8M+-13,-6M+-52,3M+-41,3M+-42,1"_
&"M+-22,-2M+-45,-3M+-26,-5M+0,0M+-24,-5M+0,0"_
&"M+-31,-7M+-2,14M+30,1M+1,8M+2,0"_
&"M+0,0M+0,0M+-20,2"_
&"BM+154,32P4288822016,0"_
&"BM+-165,-29M+1,26"_
&"M+-3,18M+0,12M+0,0M+16,-1M+0,-16M+-1,-18"_
&"M+-1,-14M+-2,-6M+-8,-1"_
&"B"_
&"BM+2,-1M+-4,1"_
&""_
&"BM+7,28P4285822068,0"_
&"BM+5,-95M+22,9M+15,9M+4,1M+0,10"_
&"M+16,5M+24,3M+25,4M+8,14M+2,4"_
&"B"_
&"BM+-143,-70M+3,2M+25,7M+3,3M+0,-1"_
&"BM+-28,14M+9,0M+12,5M+9,0M+14,5M+18,7"_
&"M+5,0"_
&"BM+-51,-30M+0,14M+-4,-16M+-1,16M+1,8"_
&"M+-3,10M+8,-10M+-2,14M+8,1M+1,-12"_
&"BM+30,11"_
&"P4285412870,0"_
&"BM+-31,-30P4283708934,0"_
&"BM+195,48M+5,-7M+0,-4M+66,-3M+7,12"_
&""_
&"B"_
&"BM+-80,2M+2,4M+0,-2"_
&"BM+21,-6P4283708934,0"_
&"BM+59,2M+135,-83"_
&"M+-127,87"_
&"BM+6,-7"_
&"B"_
&"BM+-2,5M+1,5"_
&"B"_
&"BM+-6,-13M+-13,6"_
&"BM+16,-1P4279308561,0"_
&"BM+1,-2P4281940281,0"_
&"BM+-39,-10"_
&"BM+6,1M+-8,0"_
&"M+9,-99M+6,1M+-6,98M+-2,0"_
&"BM+0,-23P4281940281,0"_
&"B"_
&"BM+2,-76M+-11,-12M+2,-6M+1,1"_
&"M+25,0M+-3,13M+-8,4M+-2,-1"_
&"B"_
&"BM+-1,-12"_
&"P4294912000,0"_
&"BM+-3,-8M+0,3M+7,-103"_
&"BM+1,1M+3,-1"_
&"M+-2,102M+0,2"_
&"BM+-3,-10"_
&"P4283848278,0"_
&"BM+6,-96M+1,-22"_
&"BM+-136,265M+1,-126M+5,0M+4,125"_
&"BM+-4,-29"_
&"P4283848278,0"_
&"BM+1,-96M+8,-6M+0,-10M+-26,1M+0,8"_
&"M+4,6M+9,2"_
&"BM+-1,-9P4294919424,0"_
&"BM+-1,-8M+4,-123M+4,-1"_
&"M+1,124"_
&"BM+-5,-15P4283256141,0"_
&"BM+-107,106M+9,3M+0,-68M+-6,1"_
&"M+-1,64"_
&"BM+2,-13P4283256141,0"_
&"BM+4,-54M+8,-1M+-1,-8M+-19,0"_
&"M+-1,10M+10,3"_
&"M+11,-5"_
&"BM+-18,-3P4294919424,0"_
&"BM+3,-7M+0,1M+-1,-99M+6,1M+3,98"_
&"BP4283256141,0"_
&"BM+-4,-12"_
&"P4283256141,0"_
&"BM+-1,25M+-15,77M+7,0M+9,-76M+-2,77"_
&"BM+-11,-11"_
&"M+12,-2M+0,-8M+-10,0M+2,-4M+8,0M+0,-5"_
&"M+-10,0M+3,-7M+7,3M+0,-8M+-7,2"_
&"BM+112,-47"_
&"M+-64,136M+9,1M+52,-135M+-44,135M+11,0M+36,-136"_
&"M+-22,136M+6,2M+15,-136M+-58,111M+46,2M+3,-7"_
&"M+-41,-4M+4,-6M+37,1M+1,-8M+-34,-3M+5,-8"_
&"M+27,1M+4,-10M+-28,-2M+4,-8M+22,1M+4,-11"_
&"M+-23,1M+4,-10M+17,0M+3,-9M+-14,-2"_
&"B"_
&"BM+135,-29"_
&"M+-36,114M+8,1M+31,-112M+-25,114M+0,0M+7,-1"_
&"M+0,0M+15,-112M+-5,110M+7,-107M+-6,106M+10,0"_
&"M+-5,-107"_
&"BM+-192,131M+64,3M+0,-4M+-56,-4"_
&"M+-5,5"_
&"BM+24,-2"_
&"BM+0,0P4294955008,0"_
&"BM+0,-58M+-117,-153M+9,2"_
&"M+111,147M+-6,4"_
&"B"_
&"BM+-71,-104P4283848278,0"_
&"BM+71,95P4283848278,0"_
&"BM+-39,54"_
&"M+1,8M+7,0M+-1,-7M+-11,0"_
&""_
&"BM+8,3P4283848278,0"_
&"BM+89,-2M+2,8M+6,0M+1,-8M+-10,0"_
&""_
&"BM+5,5P4283848278,0"_
&"B"_
&"BM+22,-4M+12,-1"_
&"M+0,11M+-13,-1M+1,-9"_
&"BM+3,2P4283848278,0"_
&"BM+27,-4M+14,0"_
&"M+-2,10M+-10,-1M+0,-9"_
&"BM+3,3P4283848278,0"_
&"BM+90,7M+-19,10"_
&"M+-3,-5M+2,-4M+-3,8M+8,6M+0,5M+6,-1"_
&""_
&"B"
 dim as long d=700
        line im,(780+75-d,100)-(780+85-d,95)
        circle im,(780+85-d,95),2,,,,,f
        Line im,(780+75-d,100)-(780+10-d,170),0
        Line im,(780+75-d,100)-(930-10-d,170),0
        Line im,(780-d,150)-(930-d,250),Rgb(0,190,255),bf
        Line im,(780-d,220)-(930-d,250),Rgb(0,100,190),bf
        For k As Long=-5 To 5
            Line im,(780-k-d,150-k)-(930+k-d,250+k),Rgb(150+10*k,30,0),b
        Next k
        Draw im,boat
      end sub
'constructor ship(byval size as integer)
'    redim this.weapons(size)
'end constructor

constructor ship(byval f as string, byval h as integer, byval a as integer, w() as shipWeapon)
    this.health = h
    this.armor = a
    redim this.weapons(ubound(w))
    for i as integer = 0 to ubound(w) step 1
        this.weapons(i) = w(i)
    next i
   
    this.fileName = f
    this.imagePtr = ImageCreate(300,300,rgb(100,100,100))
    create(imageptr)
   for i as integer=0 to ubound(w)
          draw string imagePtr,(10,i*20+50),weapons(i)
          next i
    if Bsave(this.fileName, this.imagePtr) <> 0 then
        print "Error loading: " & this.fileName
        sleep
        end
    end if

end constructor

dim laser as shipWeapon

laser.identifier = "laz"
laser.damage = 2
laser.AP = -1
laser.health = 2

dim bolter as shipWeapon

bolter.identifier = "blt"
bolter.damage = 3
bolter.AP = 0
bolter.health = 3

screen 20,32

bsave("testbg.bmp",0)

dim as shipweapon temp(0 to 1)={laser,bolter}
dim occult as ship = ship("testbg.bmp", 10, 1,temp())

bload "testbg.bmp"

sleep
kill "testbg.bmp" 
IvanisIvan
Posts: 44
Joined: Nov 07, 2019 21:57

Re: Image loading and UDT trouble

Post by IvanisIvan »

paul doe wrote:
dodicat wrote:
Thank you both for your implementations! I couldn't work out how to properly dimension those arrays and thank you for teaching me! I also learnt how to cast UDT to strings and other helpful things such as that!

The reason I didn't have a graphics mode in there is because it is supposed to be called in a main file but it was quite silly of me!

Paul Doe, what does the second piece of code do? am I supposed to insert it above the previous post?

Dodicat, thank you for the fun image :D
IvanisIvan
Posts: 44
Joined: Nov 07, 2019 21:57

Re: Image loading and UDT trouble

Post by IvanisIvan »

New question: How should I go about creating a constructor for the shipWeapon type?
paul doe
Moderator
Posts: 1730
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Image loading and UDT trouble

Post by paul doe »

IvanisIvan wrote:...
Paul Doe, what does the second piece of code do? am I supposed to insert it above the previous post?
...
It's just a convenience function to load a BMP into a Fb.Image buffer, of the proper size. Use it at your leisure.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Image loading and UDT trouble

Post by dodicat »

IvanisIvan wrote:New question: How should I go about creating a constructor for the shipWeapon type?
Just the same way you did for ship.
But remember to include an empty constructor to handle lines 29 and 37

Code: Select all

type shipWeapon
    identifier as string
    damage as integer
    AP as integer
    health as integer
    declare constructor
      declare constructor(as string,as integer,as integer,as integer)
    declare operator cast() as string 'to use print
end type

operator shipWeapon.cast() as string
return identifier + ","+str(damage)+","+str(AP)+","+str(health)
end operator


constructor shipweapon
print __function__ + "  empty"
end constructor

 constructor shipweapon(id as string,dmg as integer,A as integer,hlth as integer)
 print __function__+ "  parameters"
 identifier=id
 damage=dmg
 AP=A
 health=hlth
end constructor
 
dim laser as shipWeapon '<-- need empty constructor

laser=shipweapon("laz",2,-1,2)
'laser.identifier = "laz"
'laser.damage = 2
'laser.AP = -1
'laser.health = 2

dim bolter as shipWeapon '<-- need empty constructor

bolter=shipweapon("blt",3,0,3)
'bolter.identifier = "blt"
'bolter.damage = 3
'bolter.AP = 0
'bolter.health = 3
print laser
print bolter
sleep
 
IvanisIvan
Posts: 44
Joined: Nov 07, 2019 21:57

Re: Image loading and UDT trouble

Post by IvanisIvan »

dodicat wrote:
right, thank you for teaching me about default constructors! It seems necessary to first dim the variable with the default constructor and then populate it with the secondary constructor, very interesting!
IvanisIvan
Posts: 44
Joined: Nov 07, 2019 21:57

Re: Image loading and UDT trouble

Post by IvanisIvan »

paul doe wrote:
IvanisIvan wrote:...
Paul Doe, what does the second piece of code do? am I supposed to insert it above the previous post?
...
It's just a convenience function to load a BMP into a Fb.Image buffer, of the proper size. Use it at your leisure.
I see, thank you very much!
badidea
Posts: 2586
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Image loading and UDT trouble

Post by badidea »

IvanisIvan wrote:It seems necessary to first dim the variable with the default constructor and then populate it with the secondary constructor, very interesting!
Why? You can also do:

Code: Select all

dim laser as shipWeapon = shipweapon("laz",2,-1,2)
Or shorter:

Code: Select all

var laser = shipweapon("laz",2,-1,2)
IvanisIvan
Posts: 44
Joined: Nov 07, 2019 21:57

Re: Image loading and UDT trouble

Post by IvanisIvan »

badidea wrote:
IvanisIvan wrote:It seems necessary to first dim the variable with the default constructor and then populate it with the secondary constructor, very interesting!
Why? You can also do:

Code: Select all

dim laser as shipWeapon = shipweapon("laz",2,-1,2)
Or shorter:

Code: Select all

var laser = shipweapon("laz",2,-1,2)
I was having issues before but I was probably just leaving out a comma or a bracket. Thank you for the tips!
Post Reply