图像移动中,CPU占有率问题 ( 积分: 200 )

  • 主题发起人 主题发起人 yanggh
  • 开始时间 开始时间
Y

yanggh

Unregistered / Unconfirmed
GUEST, unregistred user!
在Timer事件中,实现图像移动.当小窗口运行时,图像移动速度正常.但当大窗口或全屏运行时,移动速度极慢,此时发现CPU占有率为100%.
请各位大侠帮帮忙,这种问题怎么处理,如何降低CPU的占有率,而且还不影响图像移动速度.
 
在Timer事件中,实现图像移动.当小窗口运行时,图像移动速度正常.但当大窗口或全屏运行时,移动速度极慢,此时发现CPU占有率为100%.
请各位大侠帮帮忙,这种问题怎么处理,如何降低CPU的占有率,而且还不影响图像移动速度.
 
怎么没有办法?急切盼望各位大侠伸出援助之手
 
1.打开双缓冲 DoubleBuffered :=True;

2.用算法重画,如:bltbit;
 
to leosoft,这些代码程序里都有.
主要是如何降低CPU的占有率,无法解决
 
1。如果允许的话 用DelphiX中的TbackGroundSprite...那个没这个问题
2。好像有个fastblt 还是bltfast 的API吧。。
 
to Genl;
时间紧,任务重.对于DelphiX,我来不及学习和改动现在的代码;
试过bitfast,不行.
是否还有其他的办法吗??谢谢
 
不知道你是怎样移动的,如果是画的话,可以只画显示的.
 
还有一个方法用 PaintBox 画
以前我PanitBox画过声音的频率信号 不过找不到了

用法当时是抄的 所以忘了
好像是在 Timer中 调用 PaintBox1.repaint

具体的绘画在TPaintBox1.OnPaint中实现 和VC画屏幕的思路很像
查找一下这方面的资料 一般的频谱显示都是用PaintBox的。。
 
你在timer事件里加多一条sleep(100)试试看。
 
to Genl:
我改成PaintBox画了,情况好一些。但CPU占用率也挺大,从而对timer影响较大。造成图像不能按规定速度移动(这个程序要求规定时间内移动到图像的规定位置)。
经过多次尝试,发现在全屏单独运行此程序时,它的CPU占用率很小,约1%。若不断打开其他的软件,此程序的CPU占用率随之增加。然后,图像移动就极慢。
这是为什么?谢谢你。
to appfirst:在程序中,我采用了双缓冲技术。在后台Tbitmap中只画下一个timer中要更新的部分。
to jianguobo:在timer事件中,加Sleep(100),虽然会减少CPU占用率。但是牺牲掉了图像的实时移动。(这个程序需要实现图像实时移动)。
 
to Genl:
我改成PaintBox画了,情况好一些。但CPU占用率也挺大,从而对timer影响较大。造成图像不能按规定速度移动(这个程序要求规定时间内移动到图像的规定位置)。
经过多次尝试,发现在全屏单独运行此程序时,它的CPU占用率很小,约1%。若不断打开其他的软件,此程序的CPU占用率随之增加。然后,图像移动就极慢。
这是为什么?谢谢你。
to appfirst:在程序中,我采用了双缓冲技术。在后台Tbitmap中只画下一个timer中要更新的部分。
to jianguobo:在timer事件中,加Sleep(100),虽然会减少CPU占用率。但是牺牲掉了图像的实时移动。(这个程序需要实现图像实时移动)。
 
timer它会老占CPU的,加Sleep(100)来让它释放。你把那个100再改小的看看效果呢
且结合Genl的方法一起试试看
 
to jianguobo,我试了Sleep(),还是不行。打开的软件越多,此程序的CPU占用率越高。Timer中的图像移动越慢。
 
1、我临时编写了一个测试软件,没有出现楼主所说的现象,测试环境:win2k pro +d6+cpu/p4/2.0+内存/512M,楼主可以到以下地址下载该测试软件:
ftp.mlfjnp.com name: temp password: temp

2、建议使用API函数StretchBlt或BitBlt函数,在内存中显示完之后快速拷贝到屏幕,这是在编写游戏软件中使用的典型方法。

BOOL StretchBlt(

HDC hdcDest, // handle of destination device context
int nXOriginDest, // x-coordinate of upper-left corner of dest. rect.
int nYOriginDest, // y-coordinate of upper-left corner of dest. rect.
int nWidthDest, // width of destination rectangle
int nHeightDest, // height of destination rectangle
HDC hdcSrc, // handle of source device context
int nXOriginSrc, // x-coordinate of upper-left corner of source rectangle
int nYOriginSrc, // y-coordinate of upper-left corner of source rectangle
int nWidthSrc, // width of source rectangle
int nHeightSrc, // height of source rectangle
DWORD dwRop // raster operation code
);


Parameters

hdcDest

Identifies the destination device context.

nXOriginDest

Specifies the x-coordinate, in logical units, of the upper-left corner of the destination rectangle.

nYOriginDest

Specifies the y-coordinate, in logical units, of the upper-left corner of the destination rectangle.

nWidthDest

Specifies the width, in logical units, of the destination rectangle.

nHeightDest

Specifies the height, in logical units, of the destination rectangle.

hdcSrc

Identifies the source device context.

nXOriginSrc

Specifies the x-coordinate, in logical units, of the upper-left corner of the source rectangle.

nYOriginSrc

Specifies the y-coordinate, in logical units, of the upper-left corner of the source rectangle.

nWidthSrc

Specifies the width, in logical units, of the source rectangle.

nHeightSrc

Specifies the height, in logical units, of the source rectangle.

dwRop

Specifies the raster operation to be performed. Raster operation codes define how Windows combines colors in output operations that involve a brush, a source bitmap, and a destination bitmap.

See the BitBlt function for a list of common raster operation codes.



Return Values

If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

StretchBlt stretches or compresses the source bitmap in memory and then copies the result to the destination rectangle. The color data for pattern or destination pixels is merged after the stretching or compression occurs.
When an enhanced metafile is being recorded, an error occurs (and the function returns FALSE) if the source device context identifies an enhanced-metafile device context.
If the specified raster operation requires a brush, Windows uses the brush currently selected into the destination device context.

The destination coordinates are transformed by using the transformation currently specified for the destination device context; the source coordinates are transformed by using the transformation currently specified for the source device context.
If the source transformation has a rotation or shear, an error occurs.
If destination, source, and pattern bitmaps do not have the same color format, StretchBlt converts the source and pattern bitmaps to match the destination bitmap.

If StretchBlt must convert a monochrome bitmap to a color bitmap, it sets white bits (1) to the background color and black bits (0) to the foreground color. To convert a color bitmap to a monochrome bitmap, it sets pixels that match the background color to white (1) and sets all other pixels to black (0). The foreground and background colors of the device context with color are used.
StretchBlt creates a mirror image of a bitmap if the signs of the nWidthSrc and nWidthDest parameters or of the nHeightSrc and nHeightDest parameters differ. If nWidthSrc and nWidthDest have different signs, the function creates a mirror image of the bitmap along the x-axis. If nHeightSrc and nHeightDest have different signs, the function creates a mirror image of the bitmap along the y-axis.

Not all devices support the StretchBlt function. For more information, see the GetDeviceCaps function.

See Also

BitBlt, GetDeviceCaps, MaskBlt, PlgBlt, SetStretchBltMode

the BitBlt function performs a bit-block transfer of the color data corresponding to a rectangle of pixels from the specified source device context into a destination device context.

BOOL BitBlt(

HDC hdcDest, // handle to destination device context
int nXDest, // x-coordinate of destination rectangle's upper-left corner
int nYDest, // y-coordinate of destination rectangle's upper-left corner
int nWidth, // width of destination rectangle
int nHeight, // height of destination rectangle
HDC hdcSrc, // handle to source device context
int nXSrc, // x-coordinate of source rectangle's upper-left corner
int nYSrc, // y-coordinate of source rectangle's upper-left corner
DWORD dwRop // raster operation code
);


Parameters

hdcDest

Identifies the destination device context.

nXDest

Specifies the logical x-coordinate of the upper-left corner of the destination rectangle.

nYDest

Specifies the logical y-coordinate of the upper-left corner of the destination rectangle.

nWidth

Specifies the logical width of the source and destination rectangles.

nHeight

Specifies the logical height of the source and the destination rectangles.

hdcSrc

Identifies the source device context.

nXSrc

Specifies the logical x-coordinate of the upper-left corner of the source rectangle.

nYSrc

Specifies the logical y-coordinate of the upper-left corner of the source rectangle.

dwRop

Specifies a raster-operation code. These codes define how the color data for the source rectangle is to be combined with the color data for the destination rectangle to achieve the final color.
The following list shows some common raster operation codes:

Value Description
BLACKNESS Fills the destination rectangle using the color associated with index 0 in the physical palette. (This color is black for the default physical palette.)
DSTINVERT Inverts the destination rectangle.
MERGECOPY Merges the colors of the source rectangle with the specified pattern by using the Boolean AND operator.
MERGEPAINT Merges the colors of the inverted source rectangle with the colors of the destination rectangle by using the Boolean OR operator.
NOTSRCCOPY Copies the inverted source rectangle to the destination.
NOTSRCERASE Combines the colors of the source and destination rectangles by using the Boolean OR operator and then inverts the resultant color.
PATCOPY Copies the specified pattern into the destination bitmap.
PATINVERT Combines the colors of the specified pattern with the colors of the destination rectangle by using the Boolean XOR operator.
PATPAINT Combines the colors of the pattern with the colors of the inverted source rectangle by using the Boolean OR operator. The result of this operation is combined with the colors of the destination rectangle by using the Boolean OR operator.
SRCAND Combines the colors of the source and destination rectangles by using the Boolean AND operator.
SRCCOPY Copies the source rectangle directly to the destination rectangle.
SRCERASE Combines the inverted colors of the destination rectangle with the colors of the source rectangle by using the Boolean AND operator.
SRCINVERT Combines the colors of the source and destination rectangles by using the Boolean XOR operator.
SRCPAINT Combines the colors of the source and destination rectangles by using the Boolean OR operator.
WHITENESS Fills the destination rectangle using the color associated with index 1 in the physical palette. (This color is white for the default physical palette.)


Return Values

If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

If a rotation or shear transformation is in effect in the source device context, BitBlt returns an error. If other transformations exist in the source device context (and a matching transformation is not in effect in the destination device context), the rectangle in the destination device context is stretched, compressed, or rotated as necessary.
If the color formats of the source and destination device contexts do not match, the BitBlt function converts the source color format to match the destination format.

When an enhanced metafile is being recorded, an error occurs if the source device context identifies an enhanced-metafile device context.
Not all devices support the BitBlt function. For more information, see the RC_BITBLT raster capability entry in GetDeviceCaps.
BitBlt returns an error if the source and destination device contexts represent different devices.

See Also

GetDeviceCaps, MaskBlt, PlgBlt, StretchBlt
 
Timer的频率设置不宜太低(>100),太低则不准确,同时占用将增大,

可以使用高精度的Timer,

要实现平滑流畅的移动,用线程加循环吧,如果用Timer想实现平滑,每秒要24针呢。
 
现在,我采用了高精度定时器.但有以下问题;
var
Form1: TForm1;
timeraccuracy:uint;
hTimeID:uint;
procedure TimeProc(UTimerID,Umessage:uint;dwuser,dw1,dw2:Dword) stdcall;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
tc:timecaps;
begin
if timegetdevcaps(@tc,sizeof(timecaps))=timerr_noerror then
timeraccuracy:=min(max(tc.wPeriodMin,1),tc.wPeriodMax);
timebeginperiod(timeraccuracy);
hTimeID:=timesetevent(20,timeraccuracy,@TimeProc,1,1);
button1.Enabled:=false;
button2.Enabled:=true
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
timekillevent(hTimeID);
timeEndperiod(timeraccuracy);
button2.Enabled:=false;
button1.Enabled:=true;
end;

procedure TimeProc(UTimerID,Umessage:Uint;dwuser,dw1,dw2:Dword) stdcall;
begin
form1.edit1.text:=inttostr(htimeID);
end;

end.

在timekillevent(hTimeID); 后,定时器停止,可为什么不能关调定时器打开的线程.而且参数htimeID随着定时器打开次数增多,不断增大:16,32,48,64.....
另外,如果加入下面的代码,在定时器运行时,按下Button3,程序的线程数变为6,这是为什么????
procedure TForm1.Button3Click(Sender: TObject);
begin
opendialog1.execute;
end;.
 
多人接受答案了。
 

Similar threads

D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部