Terraria ModLoader  0.11.7.8
A mod to make and play Terraria mods
BackgroundStyleLoaders.cs
Go to the documentation of this file.
1 using Microsoft.Xna.Framework;
2 using Microsoft.Xna.Framework.Graphics;
3 using System;
4 using System.Collections.Generic;
5 using Terraria.Graphics.Effects;
6 
7 namespace Terraria.ModLoader
8 {
9  //todo: further documentation
13  public static class UgBgStyleLoader
14  {
15  public const int vanillaUgBgStyleCount = 18;
16  private static int nextUgBgStyle = vanillaUgBgStyleCount;
17  internal static readonly IList<ModUgBgStyle> ugBgStyles = new List<ModUgBgStyle>();
18 
19  internal static int ReserveBackgroundSlot() {
20  int reserve = nextUgBgStyle;
21  nextUgBgStyle++;
22  return reserve;
23  }
24 
28  public static ModUgBgStyle GetUgBgStyle(int style) {
29  return style >= vanillaUgBgStyleCount && style < nextUgBgStyle
30  ? ugBgStyles[style - vanillaUgBgStyleCount] : null;
31  }
32 
33  internal static void ResizeAndFillArrays() {
34  }
35 
36  internal static void Unload() {
37  nextUgBgStyle = vanillaUgBgStyleCount;
38  ugBgStyles.Clear();
39  }
40 
41  public static void ChooseStyle(ref int style) {
42  if (!GlobalBgStyleLoader.loaded) {
43  return;
44  }
45  foreach (var ugBgStyle in ugBgStyles) {
46  if (ugBgStyle.ChooseBgStyle()) {
47  style = ugBgStyle.Slot;
48  }
49  }
50  foreach (var hook in GlobalBgStyleLoader.HookChooseUgBgStyle) {
51  hook(ref style);
52  }
53  }
54 
55  public static void FillTextureArray(int style, int[] textureSlots) {
56  if (!GlobalBgStyleLoader.loaded) {
57  return;
58  }
59  var ugBgStyle = GetUgBgStyle(style);
60  if (ugBgStyle != null) {
61  ugBgStyle.FillTextureArray(textureSlots);
62  }
63  foreach (var hook in GlobalBgStyleLoader.HookFillUgTextureArray) {
64  hook(style, textureSlots);
65  }
66  }
67  }
68 
69  public static class SurfaceBgStyleLoader
70  {
71  public const int vanillaSurfaceBgStyleCount = 10;
72  private static int nextSurfaceBgStyle = vanillaSurfaceBgStyleCount;
73  internal static readonly IList<ModSurfaceBgStyle> surfaceBgStyles = new List<ModSurfaceBgStyle>();
74 
75  //public static int SurfaceStyleCount => nextSurfaceBackgroundStyle;
76 
77  internal static int ReserveBackgroundSlot() {
78  int reserve = nextSurfaceBgStyle;
79  nextSurfaceBgStyle++;
80  return reserve;
81  }
82 
86  public static ModSurfaceBgStyle GetSurfaceBgStyle(int style) {
87  return style >= vanillaSurfaceBgStyleCount && style < nextSurfaceBgStyle
88  ? surfaceBgStyles[style - vanillaSurfaceBgStyleCount] : null;
89  }
90 
91  internal static void ResizeAndFillArrays() {
92  Array.Resize(ref Main.bgAlpha, nextSurfaceBgStyle);
93  Array.Resize(ref Main.bgAlpha2, nextSurfaceBgStyle);
94  }
95 
96  internal static void Unload() {
97  nextSurfaceBgStyle = vanillaSurfaceBgStyleCount;
98  surfaceBgStyles.Clear();
99  }
100 
101  public static void ChooseStyle(ref int style) {
102  if (!GlobalBgStyleLoader.loaded) {
103  return;
104  }
105  foreach (var surfaceBgStyle in surfaceBgStyles) {
106  if (surfaceBgStyle.ChooseBgStyle()) {
107  style = surfaceBgStyle.Slot;
108  }
109  }
110  foreach (var hook in GlobalBgStyleLoader.HookChooseSurfaceBgStyle) {
111  hook(ref style);
112  }
113  }
114 
115  public static void ModifyFarFades(int style, float[] fades, float transitionSpeed) {
116  if (!GlobalBgStyleLoader.loaded) {
117  return;
118  }
119  var surfaceBgStyle = GetSurfaceBgStyle(style);
120  if (surfaceBgStyle != null) {
121  surfaceBgStyle.ModifyFarFades(fades, transitionSpeed);
122  }
123  foreach (var hook in GlobalBgStyleLoader.HookModifyFarSurfaceFades) {
124  hook(style, fades, transitionSpeed);
125  }
126  }
127 
128  public static void DrawFarTexture() {
129  if (!GlobalBgStyleLoader.loaded) {
130  return;
131  }
132  // TODO: Causes background to flicker during load because Main.bgAlpha2 is resized after surfaceBgStyles is added to in AutoLoad.
133  foreach (var style in surfaceBgStyles) {
134  int slot = style.Slot;
135  Main.backColor = Main.trueBackColor;
136  Main.backColor.R = (byte)(Main.backColor.R * Main.bgAlpha2[slot]);
137  Main.backColor.G = (byte)(Main.backColor.G * Main.bgAlpha2[slot]);
138  Main.backColor.B = (byte)(Main.backColor.B * Main.bgAlpha2[slot]);
139  Main.backColor.A = (byte)(Main.backColor.A * Main.bgAlpha2[slot]);
140  if (Main.bgAlpha2[slot] > 0f) {
141  int textureSlot = style.ChooseFarTexture();
142  if (textureSlot >= 0 && textureSlot < Main.backgroundTexture.Length) {
143  Main.instance.LoadBackground(textureSlot);
144  for (int k = 0; k < Main.instance.bgLoops; k++) {
145  Main.spriteBatch.Draw(Main.backgroundTexture[textureSlot],
146  new Vector2(Main.instance.bgStart + Main.bgW * k, Main.instance.bgTop),
147  new Rectangle(0, 0, Main.backgroundWidth[textureSlot], Main.backgroundHeight[textureSlot]),
148  Main.backColor, 0f, default(Vector2), Main.bgScale, SpriteEffects.None, 0f);
149  }
150  }
151  }
152  }
153  }
154 
155  public static void DrawMiddleTexture() {
156  if (!GlobalBgStyleLoader.loaded) {
157  return;
158  }
159  foreach (var style in surfaceBgStyles) {
160  int slot = style.Slot;
161  Main.backColor = Main.trueBackColor;
162  Main.backColor.R = (byte)(Main.backColor.R * Main.bgAlpha2[slot]);
163  Main.backColor.G = (byte)(Main.backColor.G * Main.bgAlpha2[slot]);
164  Main.backColor.B = (byte)(Main.backColor.B * Main.bgAlpha2[slot]);
165  Main.backColor.A = (byte)(Main.backColor.A * Main.bgAlpha2[slot]);
166  if (Main.bgAlpha2[slot] > 0f) {
167  int textureSlot = style.ChooseMiddleTexture();
168  if (textureSlot >= 0 && textureSlot < Main.backgroundTexture.Length) {
169  Main.instance.LoadBackground(textureSlot);
170  for (int k = 0; k < Main.instance.bgLoops; k++) {
171  Main.spriteBatch.Draw(Main.backgroundTexture[textureSlot],
172  new Vector2(Main.instance.bgStart + Main.bgW * k, Main.instance.bgTop),
173  new Rectangle(0, 0, Main.backgroundWidth[textureSlot], Main.backgroundHeight[textureSlot]),
174  Main.backColor, 0f, default(Vector2), Main.bgScale, SpriteEffects.None, 0f);
175  }
176  }
177  }
178  }
179  }
180 
181  public static void DrawCloseBackground(int style) {
182  if (!GlobalBgStyleLoader.loaded) {
183  return;
184  }
185  if (Main.bgAlpha[style] <= 0f) {
186  return;
187  }
188  var surfaceBgStyle = GetSurfaceBgStyle(style);
189  if (surfaceBgStyle != null && surfaceBgStyle.PreDrawCloseBackground(Main.spriteBatch)) {
190  Main.bgScale = 1.25f;
191  Main.instance.bgParallax = 0.37;
192  float a = 1800.0f;
193  float b = 1750.0f;
194  int textureSlot = surfaceBgStyle.ChooseCloseTexture(ref Main.bgScale, ref Main.instance.bgParallax, ref a, ref b);
195  if (textureSlot >= 0 && textureSlot < Main.backgroundTexture.Length) {
196  //Custom: bgScale, textureslot, patallaz, these 2 numbers...., Top and Start?
197  Main.instance.LoadBackground(textureSlot);
198  Main.bgScale *= 2f;
199  Main.bgW = (int)((float)Main.backgroundWidth[textureSlot] * Main.bgScale);
200  SkyManager.Instance.DrawToDepth(Main.spriteBatch, 1f / (float)Main.instance.bgParallax);
201  Main.instance.bgStart = (int)(-Math.IEEERemainder(Main.screenPosition.X * Main.instance.bgParallax, Main.bgW) - (Main.bgW / 2));
202  Main.instance.bgTop = (int)((-Main.screenPosition.Y + Main.instance.screenOff / 2f) / (Main.worldSurface * 16.0) * a + b) + (int)Main.instance.scAdj;
203  if (Main.gameMenu) {
204  Main.instance.bgTop = 320;
205  }
206  Main.instance.bgLoops = Main.screenWidth / Main.bgW + 2;
207  if ((double)Main.screenPosition.Y < Main.worldSurface * 16.0 + 16.0) {
208  for (int k = 0; k < Main.instance.bgLoops; k++) {
209  Main.spriteBatch.Draw(Main.backgroundTexture[textureSlot],
210  new Vector2((Main.instance.bgStart + Main.bgW * k), Main.instance.bgTop),
211  new Rectangle(0, 0, Main.backgroundWidth[textureSlot], Main.backgroundHeight[textureSlot]),
212  Main.backColor, 0f, default(Vector2), Main.bgScale, SpriteEffects.None, 0f);
213  }
214  }
215  }
216  }
217  }
218  }
219 
220  internal static class GlobalBgStyleLoader
221  {
222  internal static readonly IList<GlobalBgStyle> globalBgStyles = new List<GlobalBgStyle>();
223  internal static bool loaded = false;
224  internal delegate void DelegateChooseUgBgStyle(ref int style);
225  internal static DelegateChooseUgBgStyle[] HookChooseUgBgStyle;
226  internal delegate void DelegateChooseSurfaceBgStyle(ref int style);
227  internal static DelegateChooseSurfaceBgStyle[] HookChooseSurfaceBgStyle;
228  internal static Action<int, int[]>[] HookFillUgTextureArray;
229  internal static Action<int, float[], float>[] HookModifyFarSurfaceFades;
230 
231  internal static void ResizeAndFillArrays(bool unloading = false) {
232  ModLoader.BuildGlobalHook(ref HookChooseUgBgStyle, globalBgStyles, g => g.ChooseUgBgStyle);
233  ModLoader.BuildGlobalHook(ref HookChooseSurfaceBgStyle, globalBgStyles, g => g.ChooseSurfaceBgStyle);
234  ModLoader.BuildGlobalHook(ref HookFillUgTextureArray, globalBgStyles, g => g.FillUgTextureArray);
235  ModLoader.BuildGlobalHook(ref HookModifyFarSurfaceFades, globalBgStyles, g => g.ModifyFarSurfaceFades);
236 
237  if (!unloading) {
238  loaded = true;
239  }
240  }
241 
242  internal static void Unload() {
243  loaded = false;
244  globalBgStyles.Clear();
245  }
246  }
247 }
Each background style determines in its own way how exactly the background is drawn. This class serves as a collection of functions for above-ground backgrounds.
This serves as the central class from which ModUgBgStyle functions are supported and carried out...
This serves as the central class which loads mods. It contains many static fields and methods related...
Definition: ModLoader.cs:28
static void ChooseStyle(ref int style)
static ModUgBgStyle GetUgBgStyle(int style)
Returns the ModUgBgStyle object with the given ID.
static void FillTextureArray(int style, int[] textureSlots)
static ModSurfaceBgStyle GetSurfaceBgStyle(int style)
Returns the ModSurfaceBgStyle object with the given ID.
static void ModifyFarFades(int style, float[] fades, float transitionSpeed)
Each background style determines in its own way how exactly the background is drawn. This class serves as a collection of functions for underground backgrounds.