Page 1 of 1

Game issues... display sprite...

Posted: Wed Nov 29, 2006 12:42 am
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);

Posted: Wed Nov 29, 2006 12:48 am
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.

Posted: Wed Nov 29, 2006 12:58 am
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;

Posted: Wed Nov 29, 2006 9:51 am
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.

Posted: Wed Nov 29, 2006 12:43 pm
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.

Posted: Thu Nov 30, 2006 2:25 pm
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()

Posted: Fri Dec 01, 2006 7:37 am
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.

Posted: Fri Dec 01, 2006 7:50 am
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...

Posted: Fri Dec 01, 2006 8:03 am
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?

Posted: Fri Dec 01, 2006 8:31 am
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?

Posted: Fri Dec 01, 2006 8:36 am
by netmaestro
Nothing leaps to mind.. is it working now?

Sounds dumb...

Posted: Thu Dec 07, 2006 1:55 am
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.