Qbasicnews.com

Full Version: Buffer scaling Sub
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'v made a sub for copying a 320x240-16bit buffer to a 640x480-16bit screen. I want to use the 320x240x16 screen mode, but the mode is not supported any all computers, so I need a backup plan Smile

I'v tried to optimize it, but its to slow. Can somebody help me speed it up?

This code is made for the sdl lib, but I havent changed it to work with gfxlib(wich I'm gonna use)

Code:
'buffer and video are Dim'ed as SDL_Surface ptr'rs

sub xCopyBuffer
   dim srcOfset as unsigned integer,  srcPixel as ushort ptr
   dim dstOfset1 as unsigned integer, dstOfset2 as unsigned integer
   dim dstPixel as unsigned integer ptr
   dim x as integer, y as integer, doublepixel as unsigned integer
  
   srcOfset = 0
   dstOfset1 = 0
   dstOfset2 = video->pitch
  
   SDL_LockSurface(video)
   for y = 0 to 239
      for x = 0 to 639 step 2
         srcPixel = buffer->pixels + SrcOfset + x
        
         doublepixel = *srcPixel
         doublepixel = doublepixel shl 16
         doublepixel = doublepixel or *srcPixel
        
         dstPixel = video->pixels + DstOfset1 + (x*2)
         *dstPixel = doublepixel
         dstPixel = video->pixels + DstOfset2 + (x*2)
         *dstPixel = doublepixel
      next
      
      srcOfset = srcOfset + buffer->pitch
      dstOfset1 = dstOfset1 + (video->pitch*2)
      dstOfset2 = dstOfset2 + (video->pitch*2)
   next
  
   SDL_UnlockSurface(video)
   SDL_Updaterect Video,0,0,0,0
end sub
Search for Rel's scale2x algo he posted a while ago. I think you could also check his website
Thanx, I'll look into it to night

Heh 2min and someone helps out Big Grin
scale2x is not what Im looking for, I want the blocky scaling not rounding the edges. I want the code to make the 320x240 buffer look the same in a 640x480 mode as it would look in a 320x240 mode, but my algo is to slow..
Rel's demo scaled the bitmap twice, with the "blocky" method and with the "smart method"
Are you reading pixels from the video surface? Try allocating a software surface, because a hardware one will be a magnitude slower, as reading for VRAM is painful slow.
I have ported my xCopyBuffer from sdl to gfxlib, but I have come across a problem. Heres a working example:

Code:
declare sub xCopyBuffer()

TYPE SCREENINFOTYPE
    driver_name AS STRING
    w AS INTEGER
    h AS INTEGER
    depth AS INTEGER
    pitch AS INTEGER
    bpp AS INTEGER
    mask_color AS INTEGER
    num_pages AS INTEGER
    flags AS INTEGER
END TYPE

dim shared buffer(76801) as ushort, info as screeninfotype ptr

buffer(0) = 320 shl 3
buffer(1) = 240

screen 18, 16, 0
info = screeninfo
PRINT STR$(info->w) + "x" + STR$(info->h) + "x" + STR$(info->depth);
PRINT " using " + info->driver_name + " driver"
PRINT "pitch:"; STR$(info->pitch)
do:loop until inkey$ <> ""

for i = 2 to 76801
   buffer(i) = rnd*65000
next

'line buffer, (0,0) - (319,239), RGB(255,255,255)
xCopyBuffer
do:loop until inkey$ <> ""

end


sub xCopyBuffer
   dim srcOfset as unsigned integer,  srcPixel as ushort ptr
   dim dstOfset1 as unsigned integer, dstOfset2 as unsigned integer
   dim dstPixel as unsigned integer ptr
   dim x as integer, y as integer, doublepixel as unsigned integer
  
   srcOfset = 0
   dstOfset1 = 0
   dstOfset2 = info->pitch
  
   screenlock
   for y = 0 to 239
      for x = 0 to 639 step 2
         srcPixel = varptr(buffer(2)) + SrcOfset + x
        
         doublepixel = *srcPixel
         doublepixel = doublepixel shl 16
         doublepixel = doublepixel or *srcPixel
        
         dstPixel = screenptr + DstOfset1 + (x*2)
         *dstPixel = doublepixel
         dstPixel = screenptr + DstOfset2 + (x*2)
         *dstPixel = doublepixel
      next
      srcOfset = srcOfset + 640
      dstOfset1 = dstOfset1 + (info->pitch*2)
      dstOfset2 = dstOfset2 + (info->pitch*2)
   next
   screenunlock
end sub

If you try to add the Line command that I have commented out, it crashes.. and I cant understand why. Could someone please help, coz this has troubled me for a couple of days now :barf: