/// <summary> /// When the scene starts it creates all the needed objects that are required by default /// </summary> internal void OnLoad() { QPrefs.Load().Wait(); CreatorQueue = new Queue <QEntity>(); DestroyQueue = new Queue <QBehavior>(); Window = new QWindow(Engine); SpriteRenderer = new QSpriteRenderer(Engine); GuiRenderer = new QGuiRenderer(Engine); Content = new QContentManager(Engine); Entities = new QEntityManager(Engine); Physics = new QPhysics(); List <IQLoad> Loaders = new List <IQLoad>(); //Use this method to load textures before the scene starts to compile all of them //so that the megatexture only has to be created once per scene so that there //is no delay when objects spawn, but totally optional if you have a better system BehaviorScriptLoader(Loaders); foreach (var loader in Loaders) { ((QBehavior)loader).SetName(); ((QBehavior)loader).Parent = QEntity.GetEntity(); loader.OnLoad(new QLoadContent(Engine, Content)); QEntity.FreeEntity(((QBehavior)(loader)).Parent); } Accumulator = new QAccumulator(); Coroutine = new QCoroutine(); Instantiate(Console = new QConsole()); Instantiate(Debug = new QDebug()); Instantiate(Camera = new QCamera()); CheckQueue(); DebugView = new QDebugView(Physics.PhysicsWorld); DebugView.LoadContent(Engine.GraphicsDevice, Engine.Content); Load(); }
/// <summary> /// Renders all thge objects, and debug information /// </summary> /// <param name="renderer"></param> internal void OnDraw(QSpriteRenderer renderer) { renderer.Begin(); Entities.SpriteObjects.For(s => s.OnDrawSprite(renderer)); renderer.End(); //normally ends here, debug renders here, laggy af if (Debug.DebugLevel < 2) { return; } var c = Camera.Bounds; Matrix a = Matrix.CreateOrthographicOffCenter(c.Left.ToSim(), c.Right.ToSim(), c.Bottom.ToSim(), c.Top.ToSim(), 0, 1); DebugView.RenderDebugData(ref a); }
/// <summary> /// Takes the created target and draws to it using the space that it needed for all textures /// </summary> /// <param name="target"></param> /// <param name="biggetsHeight"></param> /// <param name="maxWidth"></param> void RenderAtlas(RenderTarget2D target, int biggetsHeight, int maxWidth) { //slows down code but useful for debugging const bool takePictureOfAtlas = false; target.GraphicsDevice.SetRenderTargets(target); var render = new QSpriteRenderer(Engine); Rectangles = new Dictionary <string, QRectangle>(); var pos = QVector2.Zero; render.ClearColor = QColor.Transparent; render.Begin(); //var textures = ContentManager.Textures.ToList(); var textures = Textures; int texturesOnThisSheet = 0; for (int i = 0; i < textures.Count; i++) { var t = textures[i]; if (pos.X + t.Width > maxWidth) { pos.Y += biggetsHeight; pos.X = 0; } render.Draw(t, pos, QColor.White); Rectangles.Add(t.Name.Split('/').Last(), new QRectangle(pos, t.Bounds.Size)); pos.X += t.Width + 1; } render.End(); render.gd.SetRenderTarget(null); if (Texture != null) { ((Texture2D)Texture).Dispose(); } Texture = new QTexture(ContentManager, target); if (takePictureOfAtlas) { Texture.SaveAsPng($"{DateTime.Now.Minute}-{DateTime.Now.Second}.png"); } }