Author Topic: Preloading Objects  (Read 18782 times)

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
Re: Preloading Objects
« Reply #15 on: 2007-12-24, 07:07:32 PM »
The number of bytes per tile defines how many different tiles you can have on the layer.  One byte allows for 256 different tiles to be placed anywhere in the layer.  If you need more than that many kinds of tiles on a single layer, you'd need to use 2 bytes per tile, which allows for 65536 different tiles.  This should probably be enough for any mapper, but if you do in fact need more than 65536 different tiles on a single layer, SGDK2 has you covered.  You're crazy, and there's probably a better way to do things, but it's got you covered, because the final option is 4 bytes per tile, which lets you get up to 4294967296 different tiles per layer.

(When I say different tiles, I mean different kinds of tiles.  The size of the layer determines the actual number of tiles present on the map.)
Edward Dassmesser

Tanja

  • Clever
  • Fanatic
  • ***
  • Posts: 606
    • View Profile
Re: Preloading Objects
« Reply #16 on: 2008-01-17, 04:41:58 PM »
finally. i had the problem that my game would freeze for a little while when i came into a region, where a new (big) graphic sheet was introduced. (eg. wind streams)
it doesn't matters if this sheet is used in the same framework as the sheet for eg. rocks, which are already on the screen.

because i have a few layers, and one layer in the background with the same scrolling rate as the player layer, i can "hide" a wind tile behind a rock region in the foreground. now it takes the game longer to start, but there is no more lag while playing. condition to work for this: the hidden wind tile must be in the same start screen as the player.
i think the problem at the game start won't matter when i have build in a title screen.

Tanja

  • Clever
  • Fanatic
  • ***
  • Posts: 606
    • View Profile
Re: Preloading Objects
« Reply #17 on: 2008-02-21, 01:07:03 PM »
If you look at Display.cs you can see a property named "Texture" in a class named TextureRef, which calls a function named GetTexture only if the texture has not already been loaded and cached.  In order to pre-load the graphics, you would have to call GetTexture on the graphics that you want to load.

i looked and found it. i would like to write a custom code object, where i can define the graphic sheets that should be preloaded (because they are in other maps and are set for overlay at runtime). how would i do that?

Code: [Select]
public class TextureRef : IDisposable
   {
      private string m_Name;
      private Texture m_Texture = null;
      private Display m_Display;
     
      public TextureRef(Display Disp, string Name)
      {
         m_Display = Disp;
         m_Name = Name;
      }

      public string Name
      {
         get
         {
            return m_Name;
         }
      }

      public void Reset()
      {
         m_Texture = null;
      }

      public Texture Texture
      {
         get
         {
            if (m_Texture == null)
               m_Texture = m_Display.GetTexture(m_Name);
            return m_Texture;
         }
      }

      #region IDisposable Members
      public void Dispose()
      {
         if (m_Texture != null)
         {
            m_Texture.Dispose();
            m_Texture = null;
         }
      }
      #endregion
   }
   #endregion

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Preloading Objects
« Reply #18 on: 2008-02-21, 06:25:38 PM »
I think a line of code like this could go almost anywhere to pre-load a texture:
Display.TextureRef dummy = new Display.TextureRef(Project.GameWindow.GameDisplay, "MyGraphicSheet");

Tanja

  • Clever
  • Fanatic
  • ***
  • Posts: 606
    • View Profile
Re: Preloading Objects
« Reply #19 on: 2008-02-22, 04:38:42 AM »
almost everywhere?
meins30\Display.cs(12,1) : error CS0116: A namespace does not directly contain members such as fields or methods
in this case i've placed it directly under using System.Collections; in Display.cs.
i placed it into public class TextureRef : IDisposable also, and there comes an error message about the jit debugger.
what place would be right?

edit:
i tried to make a custom object for non-scripters. but it seems i fail to provide the right path to the graphic sheet.
is there a chance to provide a drop-down list with all graphic sheets in the users project?
Code: [Select]
namespace CustomObjects
{
   public class Preloader
   {
      [Description("bla")]
      public static void PreloadObjects(Texture GraphicSheet)
       {

Display.TextureRef dummy = new Display.TextureRef(Project.GameWindow.GameDisplay, GraphicSheet.ToString());
        }
   }
}

« Last Edit: 2008-02-22, 05:17:56 AM by Tanja »

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Preloading Objects
« Reply #20 on: 2008-02-22, 06:31:54 AM »
1) Almost anywhere, but not just any random place.  It has to be somewhere where you can put code (not in the root of a namespace) and you should put it in code that you know will run (not in TextureRef where nobody will call it).

2) I found a better line of code by looking at the generated Frameset.cs code:
Display.TextureRef dummy = Project.GameWindow.GameDisplay.GetTextureRef("MyGraphicSheet");

3) There is no possibility to display a dropdown list of graphic sheets.  SGDK 2.0 does not handle drop-down lists of graphic sheets (partly because there is no such thing as a "Graphic Sheet" at runtime).

4) There is no such thing a s a Graphic Sheet in generated code, so you can only refer to a graphic by name.  The parameter must be a string, and the caller must pass something like "SHFL32x32" (including quotes) as the value.

Tanja

  • Clever
  • Fanatic
  • ***
  • Posts: 606
    • View Profile
Re: Preloading Objects
« Reply #21 on: 2008-02-22, 07:50:47 AM »
is it not possible to provide a thing like i had in mind?

The parameter must be a string, and the caller must pass something like "SHFL32x32" (including quotes) as the value.

i understand this. i only want to seperate it from editing source code, so an user can put it into rules.

public static void PreloadObjects(MyGraphicSheet)  <-- i only want to provide this parameter, compiling doesn't work, it asks for an identifier
Display.TextureRef dummy = Project.GameWindow.GameDisplay.GetTextureRef(MyGraphicSheet.ToString());

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Preloading Objects
« Reply #22 on: 2008-02-22, 06:16:41 PM »
is it not possible to provide a thing like i had in mind?

Yes, just as I said by passing the parameter as a quoted string.

Code: [Select]
namespace CustomObjects
{
   public class Preloader
   {
      [Description("Cache the specified graphic sheet in video memory. GraphicSheet must be a quoted string.")]
      public static void PreloadObjects(string GraphicSheet)
      {
          Display.TextureRef dummy = Project.GameWindow.GameDisplay.GetTextureRef(GraphicSheet);
      }
   }
}

Then the parameter must be something like "SHFL32x32" (including quotes) when you define the rule.

Tanja

  • Clever
  • Fanatic
  • ***
  • Posts: 606
    • View Profile
Re: Preloading Objects
« Reply #23 on: 2008-02-23, 05:35:38 AM »
thanks! it compiles now.
but somehow it doesn't work. i tried it with the graphic sheet for my wind system, which is very big, so i can clearly see the pause when the file is loaded into memory. i made a plan rule at the title screen to load the wind sheet. but when the player moves from his start place to another place with wind, the pause occurs. so the preloader doesn't work.
do you know why this could be?

(the graphic sheet is named "windrad gross". i tried it with space, an underscore instead of the space, a minus instead of the space, and finally with a fantasy name. no error message, no nothing happened)
« Last Edit: 2008-02-23, 05:51:14 AM by Tanja »

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Preloading Objects
« Reply #24 on: 2008-02-23, 10:09:40 AM »
First of all, verify that it is in fact loading the graphic.  Call the function (for the first time) during the game and check if you notice the delay (you shouldn't notice the delay the second time, so make sure the first time is someplace where you could notice it).

Secondly, if it's loading the graphic, it might not have stayed loaded.  It's more likely to stay loaded if you put the result into a global variable instead of a dummy variable, so try this:

Code: [Select]
namespace CustomObjects
{
   public class Preloader
   {
      static Display.TextureRef dummy = null;
      [Description("Cache the specified graphic sheet in video memory. GraphicSheet must be a quoted string.")]
      public static void PreloadObjects(string GraphicSheet)
      {
          dummy = Project.GameWindow.GameDisplay.GetTextureRef(GraphicSheet);
      }
   }
}

If that works, and you want to be able to use it for more than one graphic, let me know and I can show you how to keep a global collection of "dummies".

Tanja

  • Clever
  • Fanatic
  • ***
  • Posts: 606
    • View Profile
Re: Preloading Objects
« Reply #25 on: 2008-02-23, 11:33:57 AM »
still doesn't work. i call it while the title screen is shown. because the screen fades out i could go to another place with wind before the overlay map was cleared, and there was still a pause. i think it simply doesn't load the graphic into memory.
what a role does the spelling play? if i have a space in the name of the sheet, does that matter?

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
Re: Preloading Objects
« Reply #26 on: 2008-02-23, 11:55:29 AM »
You have to replace all spaces with underscores... I think.  At least, that's the way it is with standard variables.

"Game Graphics" would become "Game_Graphics"
Edward Dassmesser

Tanja

  • Clever
  • Fanatic
  • ***
  • Posts: 606
    • View Profile
Re: Preloading Objects
« Reply #27 on: 2008-02-23, 01:01:49 PM »
i did that. how could i verify that the graphic is in the memory? you know, like
if flag 1 is false, do load graphic, set flag 1 true

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Preloading Objects
« Reply #28 on: 2008-02-23, 11:10:38 PM »
Did you skip step 1? First you have to verify that the graphic is loading by determining if there is a delay when you call the function.  Then you'll know if it's getting loaded.  If it runs very quickly, then you know it had an error and didn't load much.

Tanja

  • Clever
  • Fanatic
  • ***
  • Posts: 606
    • View Profile
Re: Preloading Objects
« Reply #29 on: 2008-02-24, 06:03:04 AM »
First of all, verify that it is in fact loading the graphic.  Call the function (for the first time) during the game and check if you notice the delay (you shouldn't notice the delay the second time, so make sure the first time is someplace where you could notice it).

i did that for many times and tried to say that i did that for many times...