DEADSOFTWARE

get rid of "kastet" and "pulemet" in symbols
[d2df-editor.git] / src / editor / f_main.pas
index 4d62e2ef6a0c04dfe7d373b0ac7b515dc997f174..d40610abd027e358c38bd3916a6ba3f53c979e57 100644 (file)
@@ -271,6 +271,7 @@ type
     procedure FormKeyUp(Sender: TObject; var Key: Word;
       Shift: TShiftState);
   private
+    LastDrawTime: UInt64;
     procedure Draw();
     procedure OnIdle(Sender: TObject; var Done: Boolean);
     procedure RefillRecentMenu (menu: TMenuItem; start: Integer; fmt: AnsiString);
@@ -1825,16 +1826,28 @@ begin
   end;
 
   UndoBuffer[i, ii].AddID := ID;
-
   MainForm.miUndo.Enabled := UndoBuffer <> nil;
 end;
 
+procedure DiscardUndoBuffer();
+var
+  i, k: Integer;
+begin
+  for i := 0 to High(UndoBuffer) do
+    for k := 0 to High(UndoBuffer[i]) do
+      with UndoBuffer[i][k] do
+        if UndoType = UNDO_DELETE_PANEL then
+          Dispose(Panel);
+
+  UndoBuffer := nil;
+end;
+
 procedure FullClear();
 begin
   RemoveSelectFromObjects();
   ClearMap();
   LoadSky(gMapInfo.SkyName);
-  UndoBuffer := nil;
+  DiscardUndoBuffer();
   slInvalidTextures.Clear();
   MapCheckForm.lbErrorList.Clear();
   MapCheckForm.mErrorDescription.Clear();
@@ -2541,13 +2554,15 @@ end;
 procedure TMainForm.miRecentFileExecute (Sender: TObject);
 var
   s, fn: AnsiString;
+  n: LongInt;
 begin
-  s := RecentFiles[(Sender as TMenuItem).Tag];
+  n := (Sender as TMenuItem).Tag;
+  s := RecentFiles[n];
   fn := g_ExtractWadName(s);
   if FileExists(fn) then
     OpenMap(fn, g_ExtractFilePathName(s))
   else
-    Application.MessageBox('', 'File not available anymore', MB_OK);
+    Application.MessageBox('File not available anymore', '', MB_OK);
 //  if Application.MessageBox(PChar(MsgMsgDelRecentPrompt), PChar(MsgMsgDelRecent), MB_ICONQUESTION or MB_YESNO) = idYes then
 //  begin
 //    RecentFiles.Delete(n);
@@ -2572,7 +2587,7 @@ begin
     else
     begin
       menu.Delete(i);
-      MI.Destroy();
+      Application.ReleaseComponent(MI);
     end;
   end;
 
@@ -2671,6 +2686,7 @@ var
   s: String;
 begin
   Randomize();
+  LastDrawTime := 0;
 
   {$IFDEF DARWIN}
     miApple.Enabled := True;
@@ -2756,6 +2772,8 @@ begin
 
   config := TConfig.CreateFile(CfgFileName);
 
+  gWADEditorLogLevel := config.ReadInt('WADEditor', 'LogLevel', DFWAD_LOG_DEFAULT);
+
   if config.ReadInt('Editor', 'XPos', -1) = -1 then
     Position := poDesktopCenter
   else begin
@@ -2862,6 +2880,23 @@ begin
   e_TextureFontPrintEx(X, Y, Text, FontID, 0, 0, 0, 1.0);
 end;
 
+procedure InitGraphics;
+begin
+  // FIXME: this is a shitty hack
+  if not gDataLoaded then
+  begin
+    e_WriteLog('Init OpenGL', MSG_NOTIFY);
+    e_InitGL();
+    e_WriteLog('Loading data', MSG_NOTIFY);
+    LoadStdFont('STDTXT', 'STDFONT', gEditorFont);
+    e_WriteLog('Loading more data', MSG_NOTIFY);
+    LoadData();
+    e_WriteLog('Loading even more data', MSG_NOTIFY);
+    gDataLoaded := True;
+    MainForm.FormResize(nil);
+  end;
+end;
+
 procedure TMainForm.Draw();
 var
   x, y: Integer;
@@ -2872,11 +2907,14 @@ var
   ObjCount: Word;
   aX, aY, aX2, aY2, XX, ScaleSz: Integer;
 begin
+  LastDrawTime := GetTickCount64();
   ID := 0;
   PID := 0;
   Width := 0;
   Height := 0;
 
+  InitGraphics();
+
   e_BeginRender();
 
   e_Clear(GL_COLOR_BUFFER_BIT,
@@ -2928,22 +2966,23 @@ begin
     else
       a := 0;
 
+    glDisable(GL_TEXTURE_2D);
+    glColor3ub(GetRValue(DotColor), GetGValue(DotColor), GetBValue(DotColor));
+    glPointSize(DotSize);
+    glBegin(GL_POINTS);
     x := MapOffset.X mod DotStep;
-    y := MapOffset.Y mod DotStep;
-
     while x < RenderPanel.Width do
     begin
+      y := MapOffset.Y mod DotStep;
       while y < RenderPanel.Height do
       begin
-        e_DrawPoint(DotSize, x + a, y + a,
-                    GetRValue(DotColor),
-                    GetGValue(DotColor),
-                    GetBValue(DotColor));
+        glVertex2i(x + a, y + a);
         y += DotStep;
       end;
       x += DotStep;
-      y := MapOffset.Y mod DotStep;
     end;
+    glEnd();
+    glColor4ub(e_Colors.R, e_Colors.G, e_Colors.B, 255);
   end;
 
 // Превью текстуры:
@@ -3381,7 +3420,7 @@ begin
               else
                 begin
                   item.ItemType := lbItemList.ItemIndex + ITEM_MEDKIT_SMALL;
-                  if item.ItemType >= ITEM_WEAPON_KASTET then
+                  if item.ItemType >= ITEM_WEAPON_IRONFIST then
                     item.ItemType := item.ItemType + 2;
                   item.X := MousePos.X-MapOffset.X;
                   item.Y := MousePos.Y-MapOffset.Y;
@@ -3759,7 +3798,12 @@ begin
   if Button = mbMiddle then
     MouseMDown := False;
 
-  DrawRect := nil;
+  if DrawRect <> nil then
+  begin
+    Dispose(DrawRect);
+    DrawRect := nil;
+  end;
+
   ResizeType := RESIZETYPE_NONE;
   TextureID := 0;
 
@@ -4324,8 +4368,11 @@ begin
   end;
 
 // Клавиши мыши не зажаты:
-  if (not MouseRDown) and (not MouseLDown) then
+  if (not MouseRDown) and (not MouseLDown) and (DrawRect <> nil) then
+  begin
+    Dispose(DrawRect);
     DrawRect := nil;
+  end;
 
 // Строка состояния - координаты мыши:
   StatusBar.Panels[1].Text := Format('(%d:%d)',
@@ -4355,6 +4402,8 @@ var
 begin
   config := TConfig.CreateFile(CfgFileName);
 
+  config.WriteInt('WADEditor', 'LogLevel', gWADEditorLogLevel);
+
   if WindowState <> wsMaximized then
   begin
     config.WriteInt('Editor', 'XPos', Left);
@@ -4400,7 +4449,8 @@ begin
   config.SaveFile(CfgFileName);
   config.Free();
 
-  slInvalidTextures.Free;
+  slInvalidTextures.Free();
+  DiscardUndoBuffer();
 end;
 
 procedure TMainForm.FormDropFiles(Sender: TObject;
@@ -4651,8 +4701,7 @@ begin
   end;
 
 // Удалить выделенные объекты:
-  if (Key = VK_DELETE) and (SelectedObjects <> nil) and
-     RenderPanel.Focused() then
+  if (Key = VK_DELETE) and (SelectedObjects <> nil) and RenderPanel.Focused() then
     DeleteSelectedObjects();
 
 // Снять выделение:
@@ -5551,7 +5600,7 @@ begin
         UNDO_DELETE_PANEL:
           begin
             AddPanel(Panel^);
-            Panel := nil;
+            Dispose(Panel);
           end;
         UNDO_DELETE_ITEM: AddItem(Item);
         UNDO_DELETE_AREA: AddArea(Area);
@@ -5566,9 +5615,7 @@ begin
     end;
 
   SetLength(UndoBuffer, Length(UndoBuffer)-1);
-
   RemoveSelectFromObjects();
-
   miUndo.Enabled := UndoBuffer <> nil;
 end;
 
@@ -6192,7 +6239,7 @@ begin
       if ShowModal() = mrOK then
       begin
         b := lbTypeSelect.ItemIndex + ITEM_MEDKIT_SMALL;
-        if b >= ITEM_WEAPON_KASTET then
+        if b >= ITEM_WEAPON_IRONFIST then
           b := b + 2;
         Values[Key] := ItemToStr(b);
         vleObjectPropertyApply(Sender);
@@ -6687,22 +6734,15 @@ begin
 end;
 
 procedure TMainForm.OnIdle(Sender: TObject; var Done: Boolean);
+  const MaxFPS = 60;
   var f: AnsiString;
 begin
-  // FIXME: this is a shitty hack
-  if not gDataLoaded then
+  // TODO: move refresh to user actions (ask to repaint only when something changed)
+  if GetTickCount64() - LastDrawTime >= 1000 div MaxFPS then
   begin
-    e_WriteLog('Init OpenGL', MSG_NOTIFY);
-    e_InitGL();
-    e_WriteLog('Loading data', MSG_NOTIFY);
-    LoadStdFont('STDTXT', 'STDFONT', gEditorFont);
-    e_WriteLog('Loading more data', MSG_NOTIFY);
-    LoadData();
-    e_WriteLog('Loading even more data', MSG_NOTIFY);
-    gDataLoaded := True;
-    MainForm.FormResize(nil);
+    PanelMap.Refresh;
   end;
-  Draw();
+
   if StartMap <> '' then
   begin
     f := StartMap;