Resize WindowedScreen

Just starting out? Need help? Post your questions and find answers here.
KarLKoX
Enthusiast
Enthusiast
Posts: 681
Joined: Mon Oct 06, 2003 7:13 pm
Location: France
Contact:

Resize WindowedScreen

Post by KarLKoX »

Hello :!:

I am currently facing a situation where i have WindowedScreen blitting some pixels data : it all works.
Now, i have a scaling feature with this two options :
- stretch
- scale with aspect ratio

The first case works very good with the autoStretch flag in OpenWindowedScreen().
But how to allow the scaling with aspect ratio ?
I have quickly coded something to let you visualise my problem :

Code: Select all

Global hSprite.l, spr_w.l, spr_h.l
Global hScreen.l

Procedure SizeWindowHandler()
Protected realtime_width.l, realtime_height.l

  realtime_width = WindowWidth(main_window_id)
  realtime_height = WindowHeight(main_window_id)
  
  ;- Scale with stretch feature
  TransformSprite(hSprite, 0, 0, DesktopScaledX(realtime_width), 0, DesktopScaledX(realtime_width), DesktopScaledY(realtime_height), 0, DesktopScaledY(realtime_height))  
  
  ;- TODO
  ;- Scale with aspect ratio feature (a simple idea coule be)
  ; ResizeScreen(hSprite, DesktopScaledX(realtime_width), DesktopScaledY(realtime_height), someflags) 
  
  
  FlipBuffers() 
  ClearScreen(RGB(0, 0, 0))
  DisplaySprite(hSprite, 0, 0)
    
  Debug "SizeWindowHandler:: realtime_width = " + Str(realtime_width) + " realtime_height = " + Str(realtime_height) + " spr_w = " + Str(spr_w) + " spr_h = " + Str(spr_h)
EndProcedure

Procedure WindowedScreen_Create(hWnd.l, w, h)
  If hScreen
    CloseScreen()
    hScreen = #Null
  EndIf
  
  If hSprite
    FreeSprite(hSprite)
    hSprite = #Null
  EndIf
  
  hScreen = OpenWindowedScreen(WindowID(hWnd), 0, 0, w, h, #False, 0, 0, 0)
  If hScreen
      hSprite = LoadSprite(#PB_Any, #PB_Compiler_Home + "Examples/Sources/Data/PureBasicLogo.bmp")    
      spr_w = SpriteWidth(hSprite)
      spr_h = SpriteHeight(hSprite)
      ResizeWindow(hWnd, #PB_Ignore, #PB_Ignore, spr_w, spr_h)    
  EndIf
  
EndProcedure  
  
  If InitSprite() = 0
    MessageRequester("Error", "Error with InitSprite() !", 0)
    End
  EndIf
  
  If OpenWindow(0, 0, 0, 220, 160, "Realtime Scale", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget)
    WindowedScreen_Create(0, 320, 240)    
  EndIf
  
  BindEvent(#PB_Event_SizeWindow, @SizeWindowHandler())  
  
  Repeat
    Repeat
      Event = WindowEvent()
      
      Select Event 
        Case #PB_Event_CloseWindow
          End 
      EndSelect
    Until Event = 0
  
    FlipBuffers() 
    ClearScreen(RGB(0, 0, 0))
    DisplaySprite(hSprite, 0, 0)
    Delay(1)
    
  ForEver
I just need the possibility to resize the WindowedScreen(like PureBasic does :wink: ), i have all the stuff for calculating the correct width and height with the specified apsect ratio. (but if it is possible to add it, i am open :mrgreen: )
Thank you for your help :D
"Qui baise trop bouffe un poil." P. Desproges

http://karlkox.blogspot.com/
infratec
Always Here
Always Here
Posts: 7664
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Resize WindowedScreen

Post by infratec »

Ugly ...

Code: Select all

Global hSprite.i, spr_w.i, spr_h.i
Global hScreen.i
Global Ratio.f

Procedure SizeWindowHandler()
  
  Protected realtime_width.i, realtime_height.i
  
  realtime_width = WindowWidth(main_window_id)
  realtime_height = WindowHeight(main_window_id)
  
  ;- Scale with stretch feature
  ;TransformSprite(hSprite, 0, 0, DesktopScaledX(realtime_width), 0, DesktopScaledX(realtime_width), DesktopScaledY(realtime_height), 0, DesktopScaledY(realtime_height)) 
  
  ;- Scale with aspect ratio feature (a simple idea coule be)
  ; ResizeScreen(hSprite, DesktopScaledX(realtime_width), DesktopScaledY(realtime_height), someflags)
  
  ResizeWindow(0, #PB_Ignore, #PB_Ignore, DesktopScaledX(realtime_width), DesktopScaledY(realtime_width / Ratio))
  
  Debug "SizeWindowHandler:: realtime_width = " + Str(realtime_width) + " realtime_height = " + Str(realtime_height) + " spr_w = " + Str(spr_w) + " spr_h = " + Str(spr_h)
EndProcedure

Procedure WindowedScreen_Create(hWnd.i, w, h)
  
  If hScreen
    CloseScreen()
    hScreen = #Null
  EndIf
  
  If hSprite
    FreeSprite(hSprite)
    hSprite = #Null
  EndIf
  
  hScreen = OpenWindowedScreen(WindowID(hWnd), 0, 0, 1, 1, #True, 0, 0, 0)
  If hScreen
    hSprite = LoadSprite(#PB_Any, #PB_Compiler_Home + "Examples/Sources/Data/PureBasicLogo.bmp")
    If IsSprite(hSprite)
      spr_w = SpriteWidth(hSprite)
      spr_h = SpriteHeight(hSprite)
      Ratio = spr_w / spr_h
      
      FreeSprite(hSprite)
      CloseScreen()
      ResizeWindow(0, #PB_Ignore, #PB_Ignore, spr_w, spr_h)
      
      OpenWindowedScreen(WindowID(hWnd), 0, 0, spr_w, spr_h, #True, 0, 0, 0)
      hSprite = LoadSprite(#PB_Any, #PB_Compiler_Home + "Examples/Sources/Data/PureBasicLogo.bmp")
      
    EndIf
    
  EndIf
  
EndProcedure 




If InitSprite() = 0
  MessageRequester("Error", "Error with InitSprite() !", 0)
  End
EndIf

If OpenWindow(0, 0, 0, 0, 0, "Realtime Scale", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget)
  WindowedScreen_Create(0, 0, 0)   
EndIf

BindEvent(#PB_Event_SizeWindow, @SizeWindowHandler()) 

Repeat
  Repeat
    Event = WindowEvent()
    
    Select Event
      Case #PB_Event_CloseWindow
        End
    EndSelect
  Until Event = 0
  
  FlipBuffers()
  
  ClearScreen(RGB(0, 0, 0))
  DisplaySprite(hSprite, 0, 0)
  
  Delay(3)
  
ForEver
User avatar
Mijikai
Addict
Addict
Posts: 1520
Joined: Sun Sep 11, 2016 2:17 pm

Re: Resize WindowedScreen

Post by Mijikai »

Or abuse a gadget as host:

Code: Select all

EnableExplicit

Procedure.i Main()
  If InitSprite()
    If OpenWindow(0,0,0,1280,720,#Null$,#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget|#PB_Window_SizeGadget)
      SetWindowColor(0,0)
      CanvasGadget(0,0,0,1280,720)
      If OpenWindowedScreen(GadgetID(0),0,0,1280,720,#True,#Null,#Null,#PB_Screen_NoSynchronization)
        SetFrameRate(60)
        Repeat
          Repeat
            Select WindowEvent()
              Case #PB_Event_SizeWindow
                ;ResizeGadget(0,0,0,WindowWidth(0),WindowHeight(0));<- just to show that the screen resizes accordingt to the host gadget!
              Case #PB_Event_CloseWindow
                Break 2
              Case #Null
                Break
            EndSelect
          ForEver
          ClearScreen($FF00FF)
          FlipBuffers()
        ForEver
        CloseWindow(0)  
      EndIf  
    EndIf
  EndIf
  ProcedureReturn #Null
EndProcedure

Main()

End
Setting up this workaround only worked insinde the Main() function in my tests!?

Add code to calculate the new dimensions and xy offsets according the aspect ratio and it shoulbe be good.
(If you can live with that weird Main() function scope issue...)
KarLKoX
Enthusiast
Enthusiast
Posts: 681
Joined: Mon Oct 06, 2003 7:13 pm
Location: France
Contact:

Re: Resize WindowedScreen

Post by KarLKoX »

Hi !
Thank your for your answers but the code doesn't match my need :mrgreen:

In fact, i am blitting pixels into a sprite and display it on a WindowedScreen.
When setting the autoStretch to #true, the application Window is resized along with the WindowedScreen to match it's parent window's size.
Here, the result is the expected one. (and the stretch is really smooth, great job PB Team 8) )

Now, what i want is the same thing but not a full stretch.
I just want the possibiliy to keep the aspect ratio.
I managed to do it by closing/creating a new WindowedScreen and blitting a new scaled (with aspect ratio) Sprite in the SizeWindowHandler() binded event but the WindowedScreen is flickering to much for me :?
I also managed to do it by using TransformSprite, it is working but, here, the remaining problem is that there is no possibility to resize the WindowedScreen (the code in this thread) :cry:
I thought using the Canvas but, again, there is no resize possiblity (in realtime).

It's really frustrating not to be able to resize the WindowedScreen when purebasic can do it.

If you have any code or any idea to do so :|
Again, thank you for your help.

EDIT : found this viewtopic.php?p=356292#p356292 (9 years old :shock: )
"Qui baise trop bouffe un poil." P. Desproges

http://karlkox.blogspot.com/
User avatar
Mijikai
Addict
Addict
Posts: 1520
Joined: Sun Sep 11, 2016 2:17 pm

Re: Resize WindowedScreen

Post by Mijikai »

If you have code to calculate the correct size for the aspect ratio use it to caluclate
the new size of your output in relation to the screen size.

Put the new size (Width and Height) in here (use the example i posted before):

Code: Select all

ResizeGadget(0,0,0,Width,Height)
Now all that is left is the X and Y offset.
Easy:

Code: Select all

X = (WindowWidth(0) - Width) / 2
Y = (WindowHeight(0) - Height) / 2
Now its complete :)

Code: Select all

ResizeGadget(0,X,Y,Width,Height)
This essentially resizes the screen (the canvas is just the fake window used to trick the screen).

Good luck.

Edit:
I saw you found some older post :)
Its the same idea.
KarLKoX
Enthusiast
Enthusiast
Posts: 681
Joined: Mon Oct 06, 2003 7:13 pm
Location: France
Contact:

Re: Resize WindowedScreen

Post by KarLKoX »

Hi !
I finally managed to get your code working :!:
I can full strech or stretch with aspect ratio or no stretch (image is centered) : that's what I wanted :D
Sorry for the late response, i have +-20 000 lines of code, it wasn't easy to not break the existing :mrgreen:
And again thank you ! To both of you :)
"Qui baise trop bouffe un poil." P. Desproges

http://karlkox.blogspot.com/
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4995
Joined: Sun Apr 12, 2009 6:27 am

Re: Resize WindowedScreen

Post by RASHAD »

Hi KarLKoX
While you managed it already :)
I like to propose a another approach looks very simple

Code: Select all

Global hSprite.l, spr_w.l, spr_h.l
Global hScreen.l , aspectRatio.f ,objectx,objecty,objectwidth,objectheight


Procedure SizeWindowHandler()
  width = WindowWidth(0)
  height = WindowHeight(0)
  If height >= width 
    objectx = 0
    objecty = height/2-width*aspectRatio/2
    objectwidth = width
    objectheight = width/aspectRatio
  Else  
    objectx = width/2-height*aspectRatio/2
    objecty = 0
    objectwidth = height*aspectRatio
    objectheight = height
  EndIf
 
  ;- Scale with stretch feature
  TransformSprite(hSprite,objectx, objecty, objectwidth, objecty,objectwidth,objectheight,objectx,objectheight) 
  
 
  FlipBuffers()
  ClearScreen(RGB(0, 0, 0))
  DisplaySprite(hSprite, 0, 0)
   
  Debug "SizeWindowHandler:: realtime_width = " + Str(realtime_width) + " realtime_height = " + Str(realtime_height) + " spr_w = " + Str(spr_w) + " spr_h = " + Str(spr_h)
EndProcedure

Procedure WindowedScreen_Create(hWnd.l, w, h)
  If hScreen
    CloseScreen()
    hScreen = #Null
  EndIf
 
  If hSprite
    FreeSprite(hSprite)
    hSprite = #Null
  EndIf
 
  hScreen = OpenWindowedScreen(WindowID(hWnd), 0, 0, w, h,0, 0, 0, 0)
  If hScreen
      hSprite = LoadSprite(#PB_Any, #PB_Compiler_Home + "Examples/Sources/Data/PureBasicLogo.bmp")   
      width  = SpriteWidth(hSprite)
      height = SpriteHeight(hSprite)
      aspectRatio.f = width/height
      ResizeWindow(hWnd, #PB_Ignore, #PB_Ignore, width,height)   
  EndIf
 
EndProcedure 
 
  If InitSprite() = 0
    MessageRequester("Error", "Error with InitSprite() !", 0)
    End
  EndIf
 
  If OpenWindow(0, 0, 0, 220, 160, "Realtime Scale", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget)
    WindowedScreen_Create(0, 2000, 1000)   
  EndIf
 
  BindEvent(#PB_Event_SizeWindow, @SizeWindowHandler()) 
 
  Repeat
    Repeat
      Event = WindowEvent()
     
      Select Event
        Case #PB_Event_CloseWindow
          End
      EndSelect
    Until Event = 0
 
    FlipBuffers()
    ClearScreen(RGB(0, 0, 0))
    DisplaySprite(hSprite, 0, 0)
    Delay(1)
   
  ForEver
Egypt my love
KarLKoX
Enthusiast
Enthusiast
Posts: 681
Joined: Mon Oct 06, 2003 7:13 pm
Location: France
Contact:

Re: Resize WindowedScreen

Post by KarLKoX »

Hi RASHAD ! :)
Good tip !
I now have the choice 8) :lol:
"Qui baise trop bouffe un poil." P. Desproges

http://karlkox.blogspot.com/
Post Reply