Debriefing development 2D engine on WinForms

Introductory



A couple of years ago I came up with the idea of ​​writing a Visual Novel engine on WinForms. Why on WinForms? Because then I didn’t really know anything. Periodically, the engine has received and is receiving updates to the present day. During this time, a little useful code has accumulated that can be used everywhere.



Splitting text into lines



// string ActorText -  ,      (Split(' ')) // text_width -  ,     // StrSize -    // StrEndl -    string ActorText_str = ""; //     int old_y = 35, StrSize = 0, StrEndl = 0; MessBox_1.Image = (Image)new Bitmap(MessBox_1.Width, MessBox_1.Height); using (Graphics g = Graphics.FromImage(MessBox_1.Image)) { //   old_y -= 14; for (var i = 0; i <= ActorText.Length; i++) { if (StrSize < text_width & i != ActorText.Length) { StrSize += ActorText[i].Length; if (i != ActorText.Length - 1 & (StrSize + ActorText[i + 1].Length >= text_width)) StrSize = text_width + 12; } else { // String builder for (int CreatLineIter = StrEndl; CreatLineIter < i; CreatLineIter++) ActorText_str += ActorText[CreatLineIter] + " "; // Set endl pos StrEndl = i; if (i != ActorText.Length) StrSize = ActorText[i].Length; old_y += 14; // SetColor(lua.GetTextColor())) -    .   . g.DrawString(ActorText_str, new Font(lua.GetTextFont(), 10, FontStyle.Bold), new SolidBrush(SetColor(lua.GetTextColor())), new Point(10, old_y)); ActorText_str = ""; } } }
      
      





Sprites and PictureBox



As you know, PictureBox has two image layers. BackgroundImage & Image. In the first versions of the engine, I used about 5 boxes to draw sprites. Such a system had several big disadvantages:





Later, I made the algorithm through Graphics, which made it possible to draw sprites anytime, anywhere.



 PictureBox ALeft; Bitmap SpriteListPic; //       // FreeMovePicture - ,    // posX, posY -    // Scale -  private void SpriteBoxesHolder(Image FreeMovePicture, int posX, int posY, float Scale = 2) { using(Graphics SpGr = Graphics.FromImage(SpriteListPic)) { SpGr.DrawImage(FreeMovePicture, posX * 2, posY * 2, FreeMovePicture.Size.Width / Scale, FreeMovePicture.Size.Height / Scale); } ALeft.Image = SpriteListPic; }
      
      





LuaInterface and Try-Catch, as a feature



A little about the experience with LuaInterface:






All Articles