Game issues... display sprite...

Advanced game related topics
Hydrate
Enthusiast
Enthusiast
Posts: 436
Joined: Mon May 16, 2005 9:37 pm
Contact:

Game issues... display sprite...

Post by Hydrate »

I am simply trying to display a sprite, ive done it hundreds of times before with no problems. The codebelow returns the following error: "#Sprite object not initialised".

The only issue is that the file is exactly in the right place, and it returns no error loading the sprite itself, so it cant be that, am i making some huge obvious mistake?? I ahve used initsprite, initkeyboard, and usepngimagedecoder before you ask:

Code: Select all

OpenScreen(\lWidth,\lHeight,\lDepth,\sTitle);
  LoadSprite(0,"TitleScreen.png");
  
  FreeImage(MainWindow\Images\lBannerImage);
  FreeGadget(MainWindow\Gadgets\lImageBannerGadget);
  CloseWindow(MainWindow\lHwnd);
  Repeat;
    FlipBuffers();
    ClearScreen(RGB(0,0,0));
    DisplaySprite(0,0,0) ; Error is here
    
    ExamineKeyboard();
  Until KeyboardPushed(#PB_Key_Escape);
.::Image::.
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

You should always test the opening of a screen. I don't like the look of '\lWidth,\lHeight,\lDepth' in the OpenScreen command. Try removing the back slashes.
--Kale

Image
Hydrate
Enthusiast
Enthusiast
Posts: 436
Joined: Mon May 16, 2005 9:37 pm
Contact:

Post by Hydrate »

Kale wrote:You should always test the opening of a screen. I don't like the look of '\lWidth,\lHeight,\lDepth' in the OpenScreen command. Try removing the back slashes.
Perhaps i should post more code to show you why i used these:

Code: Select all

With MainScreen;
  \lWidth=640;
  \lHeight=480;
  \lDepth=16;
  \sTitle="";
  
  OpenScreen(\lWidth,\lHeight,\lDepth,\sTitle);
  
  \Sprites\lSpriteTitleScreen=0;
  LoadSprite(\Sprites\lSpriteTitleScreen,"Graphics\Loading\TitleScreen.jpg");
  
  FreeImage(MainWindow\Images\lBannerImage);
  FreeGadget(MainWindow\Gadgets\lImageBannerGadget);
  CloseWindow(MainWindow\lHwnd);
  Repeat;
    FlipBuffers();
    ClearScreen(RGB(0,0,0));
    DisplaySprite(\Sprites\lSpriteTitleScreen,0,0);

    ExamineKeyboard();
  Until KeyboardPushed(#PB_Key_Escape);
EndWith;
.::Image::.
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

That doesn't look like legal syntax to me. I'm not surprised it doesn't work. Try moving the screen commands out of the 'With' macro.
--Kale

Image
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

It should be legal syntax. But i you want us to help you must post code we can run. Include all the necessary code.
citystate
Enthusiast
Enthusiast
Posts: 638
Joined: Sun Feb 12, 2006 10:06 pm

Post by citystate »

I've encountered this problem when I use either JPG or PNG files and it's usually been that I've forgotten to use the appropriate image decoder.

Try including UsePNGDecoder() or UseJPEGDecoder() before the LoadSprite()
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

The code as written does work if I add the necessary structures, InitSprite(), etc. Structured variables and the With macro are an unusual choice for this task, but once everything's available to the code it runs without error. So, if you're still looking for assistance, a more complete version of the code will be necessary in order to expose a problem.
BERESHEIT
Hydrate
Enthusiast
Enthusiast
Posts: 436
Joined: Mon May 16, 2005 9:37 pm
Contact:

Post by Hydrate »

Code: Select all

; Loading Window Gadget Structure
Structure WindowMainGadget;
  lImageBannerGadget.l;
EndStructure;

; Loading Window Image Structure
Structure WindowImages;
  lBannerImage.l;
EndStructure;

; Loading Window Structure
Structure Windows;
  lx.l;
  ly.l;
  lWidth.l;
  lHeight.l
  sTitle.s;
  lParams.l;
  lWID.l;
  lHwnd.l;
  lGadgetList.l;
  Gadgets.WindowMainGadget;
  Images.WindowImages;
  lLoaded.l;
EndStructure;
Define MainWindow.Windows;

; Game Screen Sprites Structure
Structure ScreenSprites;
  lSpriteTitleScreen.l;
EndStructure;

; Game Screen Structure
Structure Screens;
  lWidth.l;
  lHeight.l;
  lDepth.l;
  sTitle.s;
  Sprites.ScreenSprites;
  sSection.s;
  
EndStructure;
Define MainScreen.Screens;

; Initialisations
If InitSprite()=0 Or InitKeyboard()=0
  MessageRequester("Error","Can't initialise DirectX 7 or later",0)
  End
EndIf

; Image Loading
UsePNGImageDecoder();
UseJPEGImageDecoder();

Macro LoadingWindow();
  \lx=10;
  \ly=10;
  \lWidth=304;
  \lHeight=151;
  \sTitle="Test";
  \lParams=#PB_Window_BorderLess|#PB_Window_ScreenCentered;
  \lHwnd=OpenWindow(#PB_Any,\lx,\ly,\lWidth,\lHeight,\sTitle,\lParams);
  \lWID=WindowID(\lHwnd);
  
  \Images\lBannerImage=LoadImage(#PB_Any,"Graphics/Banner.png");
  
  \lGadgetList=CreateGadgetList(\lWID);
  \Gadgets\lImageBannerGadget=0;
  \Gadgets\lImageBannerGadget=ImageGadget(#PB_Any,0,0,\lWidth,\lHeight,ImageID(\Images\lBannerImage));
  \lLoaded=#False;
  Repeat;
    lEvent=WindowEvent();
    Delay(1000);
    \lLoaded=#True;
  Until \lLoaded=#True;
EndMacro;

With MainWindow;
  LoadingWindow();
EndWith;

With MainScreen;
  \lWidth=640;
  \lHeight=480;
  \lDepth=16;
  \sTitle="Test";
  \sSection="Menu";
  
  OpenScreen(\lWidth,\lHeight,\lDepth,\sTitle);
  
  \Sprites\lSpriteTitleScreen=0;
  LoadSprite(\Sprites\lSpriteTitleScreen,"Graphics\Loading\TitleScreen.jpg");
  
  FreeImage(MainWindow\Images\lBannerImage);
  FreeGadget(MainWindow\Gadgets\lImageBannerGadget);
  CloseWindow(MainWindow\lHwnd);
  Repeat;
    FlipBuffers();
    ClearScreen(RGB(0,0,0));
    If \sSection="Menu";
      DisplaySprite(\Sprites\lSpriteTitleScreen,0,0);
    EndIf;

    ExamineKeyboard();
  Until KeyboardPushed(#PB_Key_Escape);
  FreeSprite(\Sprites\lSpriteTitleScreen);
EndWith;

End;
This is my entire code, far as i can see, no errors, no obvious ones anyway. Is there any reason this might not work? It simply doesnt display the sprite...
.::Image::.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

I executed your code unmodified except for the paths to the images and it ran without error. Let's look for a second at your paths:
Graphics/Banner.png
Graphics\Loading\TitleScreen.jpg
Your banner path has the wrong slash character, could you fix it and retry?
BERESHEIT
Hydrate
Enthusiast
Enthusiast
Posts: 436
Joined: Mon May 16, 2005 9:37 pm
Contact:

Post by Hydrate »

netmaestro wrote:I executed your code unmodified except for the paths to the images and it ran without error. Let's look for a second at your paths:
Graphics/Banner.png
Graphics\Loading\TitleScreen.jpg
Your banner path has the wrong slash character, could you fix it and retry?
Thank you very very much, but may i ask why the slash character on an image i loaded would effect loading a sprite with a different image and correct slash characters?
.::Image::.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Nothing leaps to mind.. is it working now?
BERESHEIT
GBeebe
Enthusiast
Enthusiast
Posts: 263
Joined: Sat Oct 09, 2004 6:52 pm
Location: Franklin, PA - USA
Contact:

Sounds dumb...

Post by GBeebe »

This may sound dumb, but I had the same problem where I could load some PNGs but not others... Try this:

Open your PNG file in your image editor, duplicate the image to a new layer. hide the origional layer. save the image.

I don't know why, but it works for me.
Post Reply