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:
Problems with transparency due to multi-level inheritance
Lay out forms when updating
Later, I made the algorithm through Graphics, which made it possible to draw sprites anytime, anywhere.
Lua will return a value, but not the fact that it will be a number. Therefore Catch (Exception) . The option with double.TryParse is not suitable here, because lua.GetTable does not return a string, but a certain type, which is LuaTable and can be converted to a string if it was assigned such a value, also with a number.