/// <summary> /// Attempts to look-up the surface closest to the given size. /// </summary> /// <param name="size">The ideal size of the surface requrested.</param> /// <returns>A surface that closesly resembles the ideal surface in size.</returns> internal DirectDrawSpriteSurface GetClosestSurface(int size) { DirectDrawSpriteSurface close = null; if (!sizedSurfaces) { return(surfaces[0]); } for (var i = 1; i <= 48; i++) { if (surfaces[i] == null) { continue; } close = surfaces[i]; if (GameConfig.UseLargeGraphics) { continue; } if (i >= size) { break; } } return(close); }
/// <summary> /// Adds a new sized surface to the sprite manager. A sized surface /// attempts to load multiple surfaces for the various sizes of a keyed /// sprite. /// </summary> /// <param name="key">The name of the sprite surface.</param> /// <param name="xFrames">The number of frames of animation.</param> /// <param name="yFrames">The number of frame types.</param> internal void AddSizedSurface(string key, int xFrames, int yFrames) { if (string.IsNullOrEmpty(key)) { return; } Sprites[key] = null; try { Int32.Parse(key); return; } catch { } var tss = new TerrariumSpriteSurface(); var bmps = Directory.GetFiles(GameConfig.MediaDirectory, key + "*.bmp"); for (var i = 0; i < bmps.Length; i++) { if (!IsSourceValid(bmps[i], key)) { continue; } DirectDrawSpriteSurface dds; try { dds = new DirectDrawSpriteSurface( Path.GetFileNameWithoutExtension(bmps[i]), bmps[i], xFrames, yFrames ); } catch { dds = null; } if (dds != null) { // Only if frames are between 8 and 48 pixels. if (dds.FrameWidth > 7 && dds.FrameWidth < 49) { tss.AttachSurface(dds, dds.FrameWidth); Sprites[key] = tss; } } } }
/// <summary> /// Attaches a sized surface to this instance. No bounds /// checking is performed. The size should be between 0 and 48. /// The sizedSurfaces field is set to true. /// </summary> /// <param name="ddss">The sprite surface to attach to this instance.</param> /// <param name="size">The size of this sprite surface.</param> internal void AttachSurface(DirectDrawSpriteSurface ddss, int size) { sizedSurfaces = true; surfaces[size] = ddss; }
/// <summary> /// Attaches a single, unsized surface to this instance. /// If sizedSurface was previously set, it is now unset. /// </summary> /// <param name="ddss">The surface to attach.</param> internal void AttachSurface(DirectDrawSpriteSurface ddss) { sizedSurfaces = false; surfaces[0] = ddss; }