From 336f66e50677c95a802d321b61b3e282e016f668 Mon Sep 17 00:00:00 2001 From: brunzelchen Date: Wed, 6 Oct 2010 15:29:51 +0000 Subject: less cpu usage for threads git-svn-id: svn://svn.code.sf.net/p/ultrastardx/svn/branches/1.0.1 Challenge MOD@2638 b956fd51-792f-4845-bead-9b4dfca2ff2c --- Game/Code/Classes/UMain.pas | 32 ++++++++----- Game/Code/Classes/UVideo.pas | 87 ++++++++++++++++++++++-------------- Game/Code/lib/DSPack/UCaptureWDM.pas | 19 +++++--- 3 files changed, 87 insertions(+), 51 deletions(-) diff --git a/Game/Code/Classes/UMain.pas b/Game/Code/Classes/UMain.pas index c8b398b7..ecbc9c59 100644 --- a/Game/Code/Classes/UMain.pas +++ b/Game/Code/Classes/UMain.pas @@ -579,6 +579,7 @@ begin SMin := 0; SMax := Czesci[P].Akt; + SDet := SMin; // check if we can add new note Mozna := false; for S := SMin to SMax do @@ -721,12 +722,15 @@ begin if (Czesci[P].Czesc[S].Nuta[N].Start + tap < Czas.AktBeatD) then tap := Czas.AktBeatD - Czesci[P].Czesc[S].Nuta[N].Start - tap; - if (Ini.LineBonus = 0) then - // add points without LineBonus - SumN := SumN + 10000 / Czesci[P].Wartosc * Czesci[P].Czesc[S].Nuta[N].Wartosc * tap - else - // add points with Line Bonus - SumN := SumN + 9000 / Czesci[P].Wartosc * Czesci[P].Czesc[S].Nuta[N].Wartosc * tap; + if (tap<>0) then + begin + if (Ini.LineBonus = 0) then + // add points without LineBonus + SumN := SumN + 10000 / Czesci[P].Wartosc * Czesci[P].Czesc[S].Nuta[N].Wartosc * tap + else + // add points with Line Bonus + SumN := SumN + 9000 / Czesci[P].Wartosc * Czesci[P].Czesc[S].Nuta[N].Wartosc * tap; + end; end; end; @@ -754,6 +758,7 @@ begin SMin := 0; SMax := Czesci[P].Akt; + SDet := SMin; // check if we can add new note Mozna := false; for S := SMin to SMax do @@ -896,12 +901,15 @@ begin if (Czesci[P].Czesc[S].Nuta[N].Start + tap < Czas.AktBeatD) then tap := Czas.AktBeatD - Czesci[P].Czesc[S].Nuta[N].Start - tap; - if (Ini.LineBonus = 0) then - // add points without LineBonus - SumN := SumN + 10000 / Czesci[P].Wartosc * Czesci[P].Czesc[S].Nuta[N].Wartosc * tap - else - // add points with Line Bonus - SumN := SumN + 9000 / Czesci[P].Wartosc * Czesci[P].Czesc[S].Nuta[N].Wartosc * tap; + if (tap<>0) then + begin + if (Ini.LineBonus = 0) then + // add points without LineBonus + SumN := SumN + 10000 / Czesci[P].Wartosc * Czesci[P].Czesc[S].Nuta[N].Wartosc * tap + else + // add points with Line Bonus + SumN := SumN + 9000 / Czesci[P].Wartosc * Czesci[P].Czesc[S].Nuta[N].Wartosc * tap; + end; end; end; diff --git a/Game/Code/Classes/UVideo.pas b/Game/Code/Classes/UVideo.pas index 2eaa72bd..d54ca5c7 100644 --- a/Game/Code/Classes/UVideo.pas +++ b/Game/Code/Classes/UVideo.pas @@ -9,7 +9,6 @@ //{$define Info} - unit UVideo; interface @@ -29,6 +28,7 @@ uses SDL, glu, glext, SysUtils, + SyncObjs, {$ifdef DebugDisplay} {$ifdef win32} dialogs, @@ -65,6 +65,7 @@ type protected constructor Create; procedure Execute; override; + public procedure Update(); end; @@ -144,6 +145,7 @@ var FrameThread: TFrameThread; + EventDecode: TEvent; implementation @@ -196,6 +198,9 @@ begin if (FrameThread<>nil) then begin FrameThread.Terminate; + FrameThread.WaitFor; + FrameThread.Free; + FrameThread := nil; end; end; @@ -310,7 +315,14 @@ begin SetTime := 0; NewFrame := false; SetSkip := false; - FrameThread := TFrameThread.Create(); + + if (EventDecode = nil) then + EventDecode := TEvent.Create(nil, false, false, ''); + + if (FrameThread = nil) then + FrameThread := TFrameThread.Create(); + + FrameThread.Resume; end; procedure acClose; @@ -318,6 +330,12 @@ begin if VideoOpened then begin if (FrameThread<>nil) then + begin + FrameThread.Suspend; + //Framethread.WaitFor; + end; + + {if (FrameThread<>nil) then begin FrameThread.Terminate; FrameThread.WaitFor; @@ -325,6 +343,8 @@ begin FrameThread := nil; end; + FreeAndNil(EventDecode);} + NewFrame := false; SetSkip := false; @@ -344,8 +364,6 @@ begin if(FrameData<>nil) then FreeMem(FrameData); FrameData := nil; - - end; end; @@ -360,11 +378,13 @@ begin SetGap := Gap; SetStart := Start; SetSkip := true; + EventDecode.SetEvent; end; procedure acGetFrame(Time: Extended); begin SetTime := Time; + EventDecode.SetEvent; end; procedure TFrameThread.DoDecode; @@ -502,22 +522,27 @@ begin move(FrameDataPtr2[0], FrameDataPtr[0], numBytes*TexX*TexY); VideoTime := videodecoder^.timecode; + NewFrame := true; end; end; procedure TFrameThread.DoSkip; +var + SkipTime: Extended; + begin - VideoSkiptime:=Gap; - NegativeSkipTime:=Start+Gap; - if Start+Gap > 0 then + VideoSkiptime:=Gap; + NegativeSkipTime:=Start+Gap; + SkipTime := Start+Gap; + + if SkipTime > 0 then begin - VideoTime:=Start+Gap; + VideoTime:=SkipTime; try - ac_seek(videodecoder, -1, Floor((Start+Gap)*1000)); + ac_seek(videodecoder, -1, Floor((SkipTime)*1000)); except Log.LogError('Error seeking Video "acSkip2" on video ('+fName+')'); - //acClose; end; end else begin @@ -525,7 +550,6 @@ begin ac_seek(videodecoder, 0, 0); except Log.LogError('Error seeking Video "acSkip2" on video ('+fName+')'); - //acClose; end; VideoTime:=0; end; @@ -535,7 +559,7 @@ constructor TFrameThread.Create; begin inherited Create(true); - Self.Priority := tpLower; + //Self.Priority := tpLower; Self.FreeOnTerminate := false; Time := 0; @@ -549,30 +573,32 @@ procedure TFrameThread.Execute; begin while not terminated do begin - try - if iSkip then - DoSkip; + if (EventDecode.WaitFor(100) = wrSignaled) then + begin + try + if iSkip then + DoSkip; - if iDecode then - DoDecode(); - - if not terminated then - Update(); - except + if iDecode then + DoDecode(); + except + end; end; - Sleep(0); + if not terminated then + Synchronize(Update); end; end; procedure TFrameThread.Update; begin - Time := SetTime; + Time := SetTime; iDecode := VideoOpened and not VideoPaused and not NewFrame; iSkip := SetSkip; SetSkip := false; Gap := SetGap; - Start := SetStart; + Start := SetStart; + UploadNewFrame(); end; procedure ToggleAspectCorrection(); @@ -718,7 +744,7 @@ var begin if VideoOpened and NewFrame then begin - if(not pbo_supported) then + if (not pbo_supported) then begin FrameDataPtr:=FrameData; @@ -800,8 +826,8 @@ begin glBindTexture(GL_TEXTURE_2D, 0); glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0); end; - NewFrame := false; + EventDecode.SetEvent; end; end; @@ -810,14 +836,9 @@ var ScreenRect, TexRect: TRectCoords; begin - if DoDraw then - UploadNewFrame - else - begin - NewFrame := false; + if not DoDraw then Exit; - end; - + // have a nice black background to draw on (even if there were errors opening the vid) if Not Window.windowed then begin diff --git a/Game/Code/lib/DSPack/UCaptureWDM.pas b/Game/Code/lib/DSPack/UCaptureWDM.pas index 107d296a..156e160a 100644 --- a/Game/Code/lib/DSPack/UCaptureWDM.pas +++ b/Game/Code/lib/DSPack/UCaptureWDM.pas @@ -8,7 +8,7 @@ unit UCaptureWDM; interface uses - Classes, Windows, DSPack, DirectShow9, DSUtil, SDL, ExtCtrls; + Classes, Windows, DSPack, DirectShow9, DSUtil, SDL, ExtCtrls, SyncObjs; type TCaptureState = (csPlay, csStop, csDisbaled); @@ -50,7 +50,8 @@ type protected procedure Execute; override; public - FramePtr: PByteArray; + FramePtr: PByteArray; + EventDecode: TEvent; constructor Create(DeviceID, MediaTypeID: integer); destructor Destroy; override; @@ -71,7 +72,7 @@ type implementation uses - Graphics, SyncObjs, SysUtils; + Graphics, SysUtils; function GetCapDevices: TList; var @@ -138,7 +139,7 @@ constructor TSampleClass.Create(DeviceID, MediaTypeID: integer); begin inherited Create(true); - Self.Priority := tpLower; + //Self.Priority := tpLower; Self.FreeOnTerminate := false; Capture := TCapture.Create(DeviceID, MediaTypeID); @@ -156,6 +157,8 @@ begin ready := true; Error := false; + EventDecode := TEvent.Create(nil, false, false, ''); + Self.Resume; end; @@ -163,6 +166,8 @@ destructor TSampleClass.Destroy; begin inherited; + FreeAndNil(EventDecode); + if(frame<>nil) then FreeMem(frame); frame := nil; @@ -175,7 +180,7 @@ procedure TSampleClass.Execute; begin while not terminated do begin - if capturing then + if (EventDecode.WaitFor(100) = wrSignaled) and capturing then begin ready := false; GetImage; @@ -186,7 +191,6 @@ begin if Error then Self.Terminate; end; - Sleep(0); end; end; @@ -208,6 +212,9 @@ begin capturing := true; end else capturing := false; + + if capturing then + EventDecode.SetEvent; end; procedure TSampleClass.GetImage; -- cgit v1.2.3