Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Image Loading
#1
Ok, I'm a real noob at this, but I'm trying to make a game (don't laugh). I've made some basic tiles, and the program runs. But the only tile it seems to load is the blank tile. Here is my code.

game.bas
Code:
#include "game.bi"

InitGame
GameMain

'*******************************************************************************
'input: name of bmp
'output: pointer to loaded bmp in memory
'functional: returns null if bmp is not legit
'Thanks deleter
Function LoadSpr(file as string) as ubyte ptr
    dim as integer ff,x,y
    dim as integer ptr img
    ff=freefile
  
    open file for binary as #ff
        if lof(ff)=0 then
            kill file
            return null
        end if
      
        get #ff,19,x
        get #ff,23,y
    close #ff
  
    img=imagecreate(x,y,&h0)
    bload file,img
    return img  
End Function

'*******************************************************************************
'Function: Loads sprites into the sprites array
Sub LoadSprites()
    'Blank tile
    spr(t_blank) = LoadSpr("img\blank.bmp")
    'circle tile
    spr(t_circle) = LoadSpr("img\circle.bmp")
    'triangle tile
    spr(t_triangle) = LoadSpr("img\triangle.bmp")
    'square tile
    spr(t_square) = LoadSpr("img\square.bmp")
    'x tile
    spr(t_x) = LoadSpr("img\x.bmp")
End Sub

'*******************************************************************************
'Function: Initializes game settings, loads sprites, etc.
Sub InitGame()
    '640x480, 8 bit, 2 pages, Windowed
    ScreenRes 640, 480, 8, 2, 0
    
    'Load the sprites, duh
    LoadSprites
    
    'Random numbers
    Randomize Timer
    
    quit = false
End Sub

'*******************************************************************************
'Function: Gets Input from user, and acts accordingly
Sub GetInput()
    If MultiKey(KEY_ESC) Then
        quit = true
    End If
End Sub

'*******************************************************************************
'Function: Updates variables
Sub Update()
End Sub

'*******************************************************************************
'Function: Draws screen
Sub DrawScreen()
    ScreenSync
    
    Put(1,1), spr(tile_x), trans
End Sub

'*******************************************************************************
'Function: Main game loop
Sub GameMain()
    Do
        GetInput
        Update
        DrawScreen
    Loop While quit = false
End Sub

'*******************************************************************************
'Function: Deletes sprites from memory
Sub DeleteSprites()
    'Blank tile
    ImageDestroy(spr(t_blank))
    'circle tile
    ImageDestroy(spr(t_circle))
    'triangle tile
    ImageDestroy(spr(t_triangle))
    'square tile
    ImageDestroy(spr(t_square))
    'x tile
    ImageDestroy(spr(t_x))
End Sub

game.bi
Code:
Declare Function LoadSpr(file As String) as ubyte ptr

Declare Sub InitGame()
Declare Sub LoadSprites()
Declare Sub GetInput()
Declare Sub Update()
Declare Sub DrawScreen()
Declare Sub GameMain()
Declare Sub DeleteSprites()

#define null
#define true -1
#define false NOT true

'Keys
#define KEY_ESC &h01

Enum TileEnum
    t_blank
    t_circle
    t_triangle
    t_square
    t_x
    t_end
End Enum

Dim Shared spr(t_end-1) As uByte Ptr
Dim Shared quit As Byte

Edit: It appears that the program is not loading an image at all. But it is. Maybe the load function is loading the first pixel only?
quote="Deleter"]judging gameplay, you can adaquately compare quake 4 with pong[/quote]
Reply
#2
I can't spot anything obvious. In the sprite loader add a line to check that the image dimensions are being read in ok.

Code:
get #ff,19,x
        get #ff,23,y
        Print x, y

Also consider that the current path may be wrong. try adding

Code:
CHDIR EXEPATH

to force it to the exes path.

maybe this will help?

EDIT : also

Code:
Put(1,1), spr(tile_x), trans

should be

Code:
Put(1,1), spr(t_x), trans

Adding Option Explicit will help catch these mistakes.
EVEN MEN OF STEEL RUST.
[Image: chav.gif]
Reply
#3
I'm getting a warning while running the program. Suspicous Pointer Assignment on line 28.

Edit: Also, remember that I tried with a pink background, and it is indeed drawing the tile, only it's always blank.

Edit: And another note. The text from the print statement is red. Why is this?
quote="Deleter"]judging gameplay, you can adaquately compare quake 4 with pong[/quote]
Reply
#4
Your warning occurs when LoadSpr tries to convert img - which is an integer ptr - to a ubyte ptr, it's return type.
stylin:
Reply
#5
Thanks stylin. Anyway, that's out of the way. I tried turning translucency off, and nothing is being drawn over the text. I don't get it.
quote="Deleter"]judging gameplay, you can adaquately compare quake 4 with pong[/quote]
Reply
#6
Don't have your sprites so I can't reproduce your problem, but a few suggestions:

1. Be sure to always check and if ImageCreate is successful:
Code:
Function LoadSpr(file as string) as ubyte ptr
    '/ ...
    
    dim as ubyte ptr img = imagecreate(x, y)
    if (null = img) then return null

    bload file, img
    return img  
End Function

2. Be sure to always check if LoadSpr is successful:
Quote:game.bi
Code:
Enum TileEnum
    t_start
    '/ ---
    t_blank
    t_circle
    t_triangle
    t_square
    t_x
    '/ ---
    t_end
End Enum

Dim Shared spr(t_end-2) As uByte Ptr
Quote:game.bas
Code:
Sub LoadSprites()
    spr(t_blank) = LoadSpr("img\blank.bmp")
    if (null = spr(t_blank)) then
        '/ sprite failed to load; handle this ...
    end if

    '/ ...
    
    '/ OR, check them all at once:
    dim as TileEnum tile
    for tile = t_start + 1 to t_end - 1
        if (null = spr(tile)) then
            '/ sprite failed to load; handle this ...
        end if
    next
End Sub

3. Avoid #defines:
Quote:game.bi
Code:
const null as integer = 0

enum boolean : false = 0 : true = not false : end enum

edit: after a brief look, you may be reading corrupt x and y vars from your image files. That would be the first place I'd look. (there are free-ware bitmap loaders that make reading the header very easy)

edit2: if your images seem to be loading (correct dimensions), I'd check the bitmap format next. I'm not 100% sure, but I believe BLoad doesn't err if the image is RLE compressed (which the wiki says it doesn't support).

edit3: (this should have been #1) Check the return value of BLoad. Even though you have established that the file exists, it's still good practice. (FB doesn't have exceptions, so we must be diligent with our error-checking) Tongue
stylin:
Reply
#7
thats a very good point stylin, i think from memory that the width and height are stored as short, not integer, so this would be likely to cause a problem.

the fact that the text is printing in red, means that it is likely that images are loading, and as you are in 8-bit mode, the pallete is being changed, and that is affecting the print colour.
EVEN MEN OF STEEL RUST.
[Image: chav.gif]
Reply
#8
http://fileanchor.com/25425-d
quote="Deleter"]judging gameplay, you can adaquately compare quake 4 with pong[/quote]
Reply
#9
In the code you uploaded you're not puting any images to the screen. Make sure you do this before your ScreenSync call. (your images display fine for me, by the way; latest fbc v.16b).
stylin:
Reply
#10
I found that they were 24 bit. Thanks anyway Wink
quote="Deleter"]judging gameplay, you can adaquately compare quake 4 with pong[/quote]
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)