I am currently working on a program which is trying to refresh the screen at a rate of ~60hz.
Unfortunately, this is painfully *slow* :(
The screen size I am working on is 160x144 magnified 4x (that is really 640x576).
This is my current code:
Code: Select all
#MACRO DetectScreenChanges()
LcdModifiedFirstY = 255
LcdModifiedFirstX = 255
LcdModifiedLastX = 0
FOR y = 0 TO 143
LcdModifiedScanline(y) = 0
FOR x = 0 TO 159
IF ScreenBuffer(x, y) <> ScreenOldBuffer(x, y) THEN
ScreenOldBuffer(x, y) = ScreenBuffer(x, y)
IF LcdModifiedScanline(y) = 0 THEN
LcdModifiedScanline(y) = 1
IF (LcdModifiedFirstY > y) THEN LcdModifiedFirstY = y
LcdModifiedLastY = y
END IF
IF (LcdModifiedFirstX > x) THEN LcdModifiedFirstX = x
IF (LcdModifiedLastX < x) THEN LcdModifiedLastX = x
END IF
NEXT x
NEXT y
#ENDMACRO
SUB RefreshScreen()
DIM AS UINTEGER x, y
DetectScreenChanges()
IF (LcdModifiedFirstY < 255) THEN
DIM AS UINTEGER a, b, c, d
DIM AS UBYTE ScaleStepX, ScaleStepY
DIM AS UINTEGER PTR sPtr32 = Screenptr
SCREENLOCK()
FOR ScaleStepY = 0 TO (GraphicScaleFactor - 1) STEP FastScale
a = (GraphicWidth * ScaleStepY) + ScreenBorderRight + (ScreenBorderTop * GraphicWidth)
FOR y = LcdModifiedFirstY TO LcdModifiedLastY
IF (LcdModifiedScanline(y) = 1) THEN
b = y * GraphicWidth * GraphicScaleFactor
FOR x = LcdModifiedFirstX TO LcdModifiedLastX
c = (x * GraphicScaleFactor) + b + a
FOR ScaleStepX = 0 TO (GraphicScaleFactor - 1)
sPtr32[c + ScaleStepX] = ScreenPalette32(ScreenBuffer(x, y))
NEXT ScaleStepX
NEXT x
END IF
NEXT y
NEXT ScaleStepY
SCREENUNLOCK()
END IF
END SUB
Variables ScaleStepX and ScaleStepY are both equal to 4 (but could be anything, to scale the screen more or less), and the variable "FastScale" is always equal to "1".
There is also a routine called "DetectScrenChanges()", which is detecting if the screen changed or not, and feeding variables LcdModifiedFirstX, LcdModifiedFirstY, LcdModifiedLastX, LcdModifiedLastY. (this way I don't have to redraw the screen if its content hasn't changed).
I am also keeping track of modifications on every line, to avoid redrawing lines of screen which weren't changed since last refresh (that's what the LcdModifiedScanline() array is about).
Is there any chance for me to speed up this code? I wonder if using SDL would improve the refresh rate speed?