LoadSprite() not working, please help!

Advanced game related topics
tutiplain
User
User
Posts: 43
Joined: Wed Jun 30, 2010 3:00 am

LoadSprite() not working, please help!

Post by tutiplain »

Hi all,

I am trying to load and display a simple sprite, but the LoadSprite() function always returns 0. Can anyone tell me what I'm doing wrong?

Code: Select all

;init code
InitSprite()
InitKeyboard()
UseJPEGImageDecoder()
UseJPEGImageEncoder()
UseJPEG2000ImageDecoder()
UseJPEG2000ImageEncoder()
;data section, binary includes
;DataSection
;  IncludeBinary "stick_figure.JPG"
;EndDataSection


;globals
Global x
Global y

;additional setup, load sprites
OpenScreen(800,600,16,"",#PB_Screen_SmartSynchronization)

stick = LoadSprite(#PB_Any,"stick_figure.JPG")
If stick=0
  MessageRequester("Error","Unable to load sprite")
  End
EndIf

ExamineKeyboard()
;main loop
While KeyboardInkey()<>Chr(27)
  ExamineKeyboard()
  
  StartDrawing(ScreenOutput())
  
  DisplaySprite(stick,x,y)
  
  StopDrawing()
  FlipBuffers()
Wend
The file exists in the same directory as the source file. Note: specifying full file path does not work, either.

Thanks for any info.
User avatar
Demivec
Addict
Addict
Posts: 4282
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: LoadSprite() not working, please help!

Post by Demivec »

What value does InitSprite() return?
tutiplain
User
User
Posts: 43
Joined: Wed Jun 30, 2010 3:00 am

Re: LoadSprite() not working, please help!

Post by tutiplain »

It returns 1. Let me download/install DX, and see if it solves the problem.
User avatar
Comtois
Addict
Addict
Posts: 1432
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Re: LoadSprite() not working, please help!

Post by Comtois »

When you will be able to load your sprite, change your loop like this

Code: Select all

;main loop
While KeyboardInkey()<>Chr(27)
  ExamineKeyboard()

  DisplaySprite(stick,x,y)

  FlipBuffers()
Wend
Please correct my english
http://purebasic.developpez.com/
tutiplain
User
User
Posts: 43
Joined: Wed Jun 30, 2010 3:00 am

Re: LoadSprite() not working, please help!

Post by tutiplain »

Installing DirectX runtime didn't solve it. I also changed my code to use a hard-coded sprite number (0), instead of #PB_Any and letting PB assign one on its own. It still doesn't work. The error reported is "The specified #Sprite is not initialized", and it happens on the call to DrawSprite().
User avatar
Comtois
Addict
Addict
Posts: 1432
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Re: LoadSprite() not working, please help!

Post by Comtois »

i can test on my pc if you put your sprite on line.
Please correct my english
http://purebasic.developpez.com/
tutiplain
User
User
Posts: 43
Joined: Wed Jun 30, 2010 3:00 am

Re: LoadSprite() not working, please help!

Post by tutiplain »

Hi Comtois,

I made the changes you suggested. By removing the call to ExamineKeyboard() before the While, I got "ExamineKeyboard()" must be called before using this command" when the loop begins. After adding it again, I got "The specified #Sprite is not initialized", on the call to DrawSprite(). Any more suggestions?
User avatar
Comtois
Addict
Addict
Posts: 1432
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Re: LoadSprite() not working, please help!

Post by Comtois »

try another image.
Please correct my english
http://purebasic.developpez.com/
Nituvious
Addict
Addict
Posts: 1030
Joined: Sat Jul 11, 2009 4:57 am
Location: United States

Re: LoadSprite() not working, please help!

Post by Nituvious »

Try using and image format .jpeg instead of .JPG
▓▓▓▓▓▒▒▒▒▒░░░░░
User avatar
Comtois
Addict
Addict
Posts: 1432
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Re: LoadSprite() not working, please help!

Post by Comtois »

tutiplain wrote:Hi Comtois,

I made the changes you suggested. By removing the call to ExamineKeyboard() before the While, I got "ExamineKeyboard()" must be called before using this command" when the loop begins. After adding it again, I got "The specified #Sprite is not initialized", on the call to DrawSprite(). Any more suggestions?
i just suggest you to remove StartDrawing(ScreenOutput()) StopDrawing()
Please correct my english
http://purebasic.developpez.com/
tutiplain
User
User
Posts: 43
Joined: Wed Jun 30, 2010 3:00 am

Re: LoadSprite() not working, please help!

Post by tutiplain »

Tried another image. Didn't work either.

Could it have something to do with the PB version I'm using. I am using 4.51, under windows xp professional, SP3. The image is a stick figure drawn with Paint. I will try to upload to my site later, but I suppose any image you create in Paint should have the same problem. I also tried using a .bmp and had the same problem.

Comtois, I did remove the StartDrawing() and StopDrawing() calls. No effect, either.
User avatar
Comtois
Addict
Addict
Posts: 1432
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Re: LoadSprite() not working, please help!

Post by Comtois »

this code work on my pc , download PureBasic logo to test.

Code: Select all

;init code
InitSprite()
InitKeyboard()
UsePNGImageDecoder()

;globals
Global x
Global y

;additional setup, load sprites
OpenScreen(800,600,16,"",#PB_Screen_SmartSynchronization)

stick = LoadSprite(#PB_Any,"purebasic_logo.png")
If stick=0
  MessageRequester("Error","Unable to load sprite")
  End
EndIf


;main loop
Repeat 
  FlipBuffers()
  ClearScreen(0)
  ExamineKeyboard()
  DisplaySprite(stick,x,y)
Until KeyboardPushed(#PB_Key_Escape)
Please correct my english
http://purebasic.developpez.com/
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: LoadSprite() not working, please help!

Post by netmaestro »

I've had similar problems using a screen depth of 16. Try 32 for better success.
BERESHEIT
tutiplain
User
User
Posts: 43
Joined: Wed Jun 30, 2010 3:00 am

Re: LoadSprite() not working, please help!

Post by tutiplain »

Hi again, everyone.

Changing to 32-bit depth solved the issue. Also, downgrading to PB 4.41 seems to have made it work using 16-bit depth. Thanks to everyone who replied, and for all the advice.
Post Reply