/// <summary> /// Executes drawing instructions for all objects, background, and any expansions /// </summary> static private void Render() { long start = timer.ElapsedMilliseconds; while (paused) { Thread.Sleep(100); return; } SpinWait.SpinUntil(() => render); render = false; camera.CalculateCoords(); if (resize) { D3DInterop.Init(mainwindow.Handle, xres, yres, (int)targetfps); resize = false; return; } RenderTarget.BeginDraw(); //draw background RenderTarget.Clear(); if (bgPtr != null && bgPtr != null) { Point bottomleft = camera.pos - new Point(xres / 2, yres / 2); bottomleft = new Point((int)(bottomleft.x / bgW) * bgW, (int)(bottomleft.y / bgH) * bgH); for (int j = (int)bottomleft.x - 2 * (int)bgW; j < bottomleft.x + xres + 2 * (int)bgW; j += (int)bgW) { for (int k = (int)bottomleft.y - 2 * (int)bgH; k < bottomleft.y + yres + 2 * (int)bgH; k += (int)bgH) { var pos = new Point(j, k); var rectangle = camera.Global2LocalCoords(pos);//.Global2LocalRectangle(pos, (int)bg.Size.Width, (int)bg.Size.Height); RenderTarget.DrawBitmap(bgPtr, (int)rectangle.x, (int)rectangle.y, 0); } } } //render sprites for (int j = 0; j < 5; j++) { lock (objects) for (int i = 0; i < objects.Count(); i++) { var objecti = objects[i]; if (objecti == null || !objecti.active) { continue; } var rectangle = camera.Global2LocalCoords(objecti.pos); bool offscreen = (rectangle.x <-xres || rectangle.x> xres * 2 || rectangle.y <-yres || rectangle.y> yres * 2); if (!offscreen && objecti.layer == j && objecti != null && objecti.sprite != null) { var sprite = objecti.sprite; var rotation = objecti.θ; RenderTarget.DrawBitmap(sprite, (int)rectangle.x, (int)rectangle.y, (int)objecti.θ); var expansions = objecti.GetAllExpansions(); //run all expansions render, based on priority for (int h = 4; h >= 0; h--) { for (int k = 0; k < expansions.Count; k++) { if (expansions[k] != null && expansions[k].Priority == h) { expansions[k].onRender(); } } } } } } //render cursor if (mouse.cursorIcon != null) { RenderTarget.DrawBitmap(mouse.cursorIcon, (int)mouse.location.x, (int)mouse.location.y, 0); } RenderTarget.EndDraw(); RenderTarget.Present(); doLogic = true; //time control favg.Dequeue(); var delta = (timer.ElapsedMilliseconds - lastrender); if (delta > 0) { favg.Enqueue(1000 / delta); } else { favg.Enqueue((float)targetfps); } lastrender = timer.ElapsedMilliseconds; var sleeps = (1000 / targetfps) - ((timer.ElapsedMilliseconds - start)); }
/// <summary> /// Initializes the renderer /// </summary> /// <param name="handle">Window handle that the renderer will draw to</param> /// <param name="x">X resolution</param> /// <param name="y">Y resolution</param> /// <param name="fps">Target fps</param> public static void Init(IntPtr handle, int x, int y, int fps) { D3DInterop.Init(handle, x, y, fps); started = true; }