Analisando o mecanismo 2D de desenvolvimento no WinForms

Introdutório


Há alguns anos, tive a idéia de escrever um mecanismo Visual Novel no WinForms. Por que no WinForms? Porque então eu realmente não sabia como. Periodicamente, o mecanismo recebeu e está recebendo atualizações até os dias atuais. Durante esse período, um pequeno código útil acumulou-se e pode ser usado em qualquer lugar.

Dividindo o texto em linhas


// 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 e PictureBox


Como você sabe, o PictureBox possui duas camadas de imagem. BackgroundImage & Image. Nas primeiras versões do mecanismo, usei cerca de 5 caixas para desenhar sprites. Esse sistema tinha várias grandes desvantagens:

  • Problemas com transparência devido a herança em vários níveis
  • Dispor formulários ao atualizar

Mais tarde, criei o algoritmo através do Graphics, para que fosse possível desenhar sprites o quanto você quisesse em qualquer lugar.

 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 e recurso try-catch like


Um pouco sobre a experiência com LuaInterface:

  • Suplemento a um artigo sobre LuaInterface sobre LuaTable: Para a maioria das coisas, você pode ficar sem funções usando apenas tabelas.

     lua.NewTable("Scene"); //   //     TextColor public string GetTextColor() { return (string)lua.GetTable("Font")["TextColor"]; } 
  • Problemas com vazamentos de memória ao trabalhar com tabelas
     string TableReaderS(string Table, string Key) { string Ret = ""; using (LuaTable tabx = lua.GetTable(Table)) { Ret = (string)tabx[Key]; } return Ret; } int TableReaderI(string Table, string Key) { int Ret = -1; using (LuaTable tabx = lua.GetTable(Table)) { Ret = (int)(double)tabx[Key]; //   int ... } return Ret; } 

    Cada vez que você chama GetTable , você obtém um novo objeto CLR que faz referência à tabela Lua referenciada por essa variável global Lua.
  • Se você não tiver certeza de qual é o valor da tabela:

     try { Size = TableReaderI("Scene", "Image" + Convert.ToString(num) + "Scale"); } catch (Exception ex) { Size = 2; } 

    Lua retornará um valor, mas não o fato de que será um número. Portanto, captura (exceção) . A opção com double.TryParse não é adequada aqui, porque lua.GetTable não retorna uma string, mas um certo tipo, que é LuaTable e pode ser convertido em uma string se tiver sido atribuído esse valor, também com um número.

Source: https://habr.com/ru/post/pt469599/


All Articles