Any alternative to cls?
Any alternative to cls?
Just screwing around with a few of my recent programs I've noticed that elininating cls in the loop increases my speed by about 20%. I'm only on a 900 mh computer...so the slowness might be multiplied. I have one program which I'm running about 18 FPS on aprox. 9500 calculations per loop and I get rid of the cls and it goes to 25-26 FPS. It just seems like a lot for clearing the screen buffer. maybe I'm wrong. I'm just wondering if anyone else noticed that and what other people may use. I'm just using freebasic graphics(fbgfx.bi)
And I also notice just the screenlock/screenunlock gives me the best performance instead of page flipping, etc. Does that sound accurate?
I'm not complaining or anything. I love this compiler...coming from a 14 year vb user...this is extremely fast.
And I also notice just the screenlock/screenunlock gives me the best performance instead of page flipping, etc. Does that sound accurate?
I'm not complaining or anything. I love this compiler...coming from a 14 year vb user...this is extremely fast.
-
- Site Admin
- Posts: 6323
- Joined: Jul 05, 2005 17:32
- Location: Manchester, Lancs
The fastest way to clear the screen is probably just to write zeroes to the screen memory:
ScreenClear is faster then Cls - although not drastically - and allows a limited amount of freedom with the color.
Note in this test that both ops cause a screen refresh, hampering the speeds a bit. The penalty won't apply if the screen is already locked.
Code: Select all
#include "crt.bi"
sub ScreenClear( byval col as ubyte = 0 )
dim as ubyte ptr scrbuff = any
dim as integer scrpitch = any, scrheight = any, scrsize = any
scrbuff = screenptr
if scrbuff = 0 then exit sub
screeninfo( , scrheight, , , scrpitch )
scrsize = scrpitch * scrheight
screenlock
clear( *scrbuff, col, scrsize )
screenunlock
end sub
dim as integer fps, frames = 0
dim as double t = timer
screenres 800, 600
do until len(inkey)
#if 0
cls
#else
ScreenClear( )
#endif
frames += 1
if abs(timer - t) >= 1 then
fps = frames
frames = 0
t = timer
windowtitle "fps: " & fps
end if
loop
Note in this test that both ops cause a screen refresh, hampering the speeds a bit. The penalty won't apply if the screen is already locked.
-
- Posts: 2655
- Joined: Aug 28, 2008 10:54
- Location: new york
Ya. I never had a problem with CLS in my main loop and I doubt that page flipping to an empty screen would be any faster, but you can try it.
It would entail setting multiple screens with
screenres x,y,colordepth, numpages
and then screencopy-ing the blank screen over the new one.
Let us know if its faster. CLS is a nice way to clean up a screen.
rb
It would entail setting multiple screens with
screenres x,y,colordepth, numpages
and then screencopy-ing the blank screen over the new one.
Let us know if its faster. CLS is a nice way to clean up a screen.
rb
-
- Posts: 2338
- Joined: May 31, 2005 9:59
- Location: Croatia
- Contact:
-
- Posts: 8586
- Joined: May 28, 2005 3:28
- Contact:
If there's not too much going on on the screen it's much faster to just draw over the old sprites with the background colour.
So you're potentially just doing small boxfills instead of the entire screen.
Code: Select all
circle (oldx, oldy),radius,backgroundcolour 'a boxfill would be even faster
circle (newx, newy),radius, spritecolour
-
- Posts: 2338
- Joined: May 31, 2005 9:59
- Location: Croatia
- Contact:
Thanks for all the ideas. It looks like line(0,0)-(ScreenWidth, ScreenHeight), 0, bf is a bit faster.
And I only used a box to fill what I need which saved some speed also.
ScreenClear() is flickery..I took the screenlock\screenunlock out of the sub and the flicker was gone.
The speed seems the same as cls.
I'll have to mess around with this a little further.
Thanks agian.
And I only used a box to fill what I need which saved some speed also.
ScreenClear() is flickery..I took the screenlock\screenunlock out of the sub and the flicker was gone.
Code: Select all
Sub ScreenClear( Byval col As Ubyte = 0 )
Dim As Ubyte Ptr scrbuff = Any
Dim As Integer scrpitch = Any, scrheight = Any, scrsize = Any
scrbuff = screenptr
If scrbuff = 0 Then Exit Sub
screeninfo( , scrheight, , , scrpitch )
scrsize = scrpitch * scrheight
Clear( *scrbuff, col, scrsize )
End Sub
I'll have to mess around with this a little further.
Thanks agian.
-
- Site Admin
- Posts: 6323
- Joined: Jul 05, 2005 17:32
- Location: Manchester, Lancs
What sort of flickering are you getting with ScreenClear?
Note: don't use it without the screenlock/screenunlock statements: you shouldn't write directly to the screen buffer while the screen is unlocked.
I won't be offended if you choose a different method, particularly given Line's simplicity and the ability to work on portions of the screen. I would be interested in feedback though, if the code's not working properly.
Note: don't use it without the screenlock/screenunlock statements: you shouldn't write directly to the screen buffer while the screen is unlocked.
I won't be offended if you choose a different method, particularly given Line's simplicity and the ability to work on portions of the screen. I would be interested in feedback though, if the code's not working properly.
It's a pretty extreme flickering as if you put cls before the screenlock in the loop. I have a screenlock/screenunlock in the main loop already and I thought the screenlock/screenunlock in the sub screenclear() might be working against each other somehow. So I pulled them out and the flicker went away. It works just as cls.
I also tried it the other way for the hell of it...Pulling the screenlock/screenunlock out of my main loop and leaving the sub unchanged and the flicker is still there.
I also tried leaving just the screenlock in the sub and just the screenunlock at the end of my loop and that worked with no flicker but with the same performance as cls.
If I'm locking the screen in the main loop wouldn't screenclear() just clear the screen? Why would it need to lock the screen if I already had a screenlock or page flipping?
I'm using version 0.18.5. Might that be a problem?
I'm very open to all methods. Everything is always a work in progress anyways. lol
Thanks for all the input. This is very interesting to me as I'm always looking for speed and another way to skin a cat. (i love cats btw)
I also tried it the other way for the hell of it...Pulling the screenlock/screenunlock out of my main loop and leaving the sub unchanged and the flicker is still there.
I also tried leaving just the screenlock in the sub and just the screenunlock at the end of my loop and that worked with no flicker but with the same performance as cls.
If I'm locking the screen in the main loop wouldn't screenclear() just clear the screen? Why would it need to lock the screen if I already had a screenlock or page flipping?
I'm using version 0.18.5. Might that be a problem?
I'm very open to all methods. Everything is always a work in progress anyways. lol
Thanks for all the input. This is very interesting to me as I'm always looking for speed and another way to skin a cat. (i love cats btw)
Ideally, you shouldn't put screenlock/unlock in a sub..It can create problems
You should just have a one pair in main loop and only put the drawing part between screenlock/unlock since if you have some code that loops infinitely or something, then it will freeze the program and you'll have to kill the process.
You should just have a one pair in main loop and only put the drawing part between screenlock/unlock since if you have some code that loops infinitely or something, then it will freeze the program and you'll have to kill the process.
-
- Posts: 1706
- Joined: May 27, 2005 6:34
- Location: Cambodia, Thailand, Lao, Ireland etc.
- Contact:
-
- Site Admin
- Posts: 6323
- Joined: Jul 05, 2005 17:32
- Location: Manchester, Lancs
Oh right, that is the problem.dpixel wrote:I'm using version 0.18.5. Might that be a problem?
The most recent version of FB allows locking the screen multiple times, unlocking the screen only when all the locks are cleared. This allows you to put lock/unlock statements in your sub without having to worry about whether the screen is already locked.
If you're sure the screen will already be locked when you call it, then it's OK to remove them. But if you're using the latest FB, then they just give you an additional level of safety.