TextureManager.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // SuperTux Editor
  2. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. using System;
  17. using System.Collections.Generic;
  18. using Resources;
  19. using Gtk;
  20. namespace Drawing
  21. {
  22. public static class TextureManager
  23. {
  24. private static Dictionary<string, ImageTexture> ImageTextures
  25. = new Dictionary<string, ImageTexture>();
  26. private static ImageTexture notFoundImageTexture = null;
  27. private static ImageTexture NotFoundImageTexture {
  28. get {
  29. if (notFoundImageTexture != null) {
  30. return notFoundImageTexture;
  31. } else {
  32. notFoundImageTexture = new ImageTexture(ResourceManager.Instance.Get("images/engine/missing.png"));
  33. return notFoundImageTexture;
  34. }
  35. }
  36. }
  37. public static ImageTexture Get(string Resourcepath)
  38. {
  39. ImageTexture Result;
  40. if(ImageTextures.ContainsKey(Resourcepath)) {
  41. Result = ImageTextures[Resourcepath];
  42. } else {
  43. Result = CreateImageTexture(Resourcepath);
  44. ImageTextures.Add(Resourcepath, Result);
  45. }
  46. return Result;
  47. }
  48. private static ImageTexture CreateImageTexture(string Resourcepath)
  49. {
  50. try {
  51. return new ImageTexture(ResourceManager.Instance.Get(Resourcepath));
  52. } catch {
  53. Console.WriteLine($"error: failed to load: {Resourcepath}");
  54. return NotFoundImageTexture;
  55. }
  56. }
  57. }
  58. }
  59. /* EOF */