Output to a specific monitor

Advanced game related topics
andrey2023
New User
New User
Posts: 7
Joined: Mon Mar 06, 2023 5:55 am

Output to a specific monitor

Post by andrey2023 »

Good afternoon
Can you please tell me how to display the image on a specific monitor? Those. I have two monitors and I want the 2d image to be displayed on a specific monitor!

Code: Select all

If InitSprite() And InitKeyboard() And OpenScreen(1024,768,16,"")
  Repeat
    FlipBuffers()
    If StartDrawing(ScreenOutput())
      DrawText(0,0,"Press ESC to exit")
      StopDrawing()
    EndIf
    ExamineKeyboard()
    If KeyboardPushed(#PB_Key_Escape)
      End
    EndIf
  ForEver
EndIf
User avatar
yuki
Enthusiast
Enthusiast
Posts: 101
Joined: Sat Mar 31, 2018 9:09 pm

Re: Output to a specific monitor

Post by yuki »

Really hope this isn't spam :P If not, welcome!

AFAIK there's no way to control the output monitor for OpenScreen(...).

As a workaround, you can make use of borderless/windowed fullscreen. For example:

Code: Select all

EnableExplicit

#SCREEN_TARGET_DESKTOP_ID = 1

InitSprite()
InitKeyboard()
ExamineDesktops()

Define wndMain.i = OpenWindow(#PB_Any,
                            DesktopX(#SCREEN_TARGET_DESKTOP_ID),
                            DesktopY(#SCREEN_TARGET_DESKTOP_ID),
                            DesktopWidth(#SCREEN_TARGET_DESKTOP_ID),
                            DesktopHeight(#SCREEN_TARGET_DESKTOP_ID),
                            "Title",
                            #PB_Window_BorderLess)
SetWindowColor(wndMain, 0)
StickyWindow(wndMain, #True)

OpenWindowedScreen(WindowID(wndMain), 0, 0, WindowWidth(wndMain), WindowHeight(wndMain))

; Match frequency of the target desktop. If you ever move to a different desktop, you'll
; want to have the target frame-rate updated as well.
Define targetFrameRate.i = DesktopFrequency(#SCREEN_TARGET_DESKTOP)
If targetFrameRate
  SetFrameRate(targetFrameRate)
EndIf

Define shouldExit.i = #False
Define isInactive.i = #False

Repeat
  ; Make sure to drain the event queue.
  Repeat
    Select WindowEvent()
      Case #PB_Event_CloseWindow
        shouldExit = #True
      Case #PB_Event_ActivateWindow
        SetWindowState(wndMain, #PB_Window_Normal)
        isInactive = #False
      Case #PB_Event_DeactivateWindow
        SetWindowState(wndMain, #PB_Window_Minimize)
        isInactive = #True
      Case #PB_Event_None
        Break
    EndSelect
  ForEver
  
  ; Skip render if we're inactive/unfocused.
  If isInactive
    Continue
  EndIf
  
  ; Render.
  ClearScreen(0)
  If StartDrawing(ScreenOutput())
    DrawText(0,0,"Press ESC to exit")
    StopDrawing()
  EndIf
  FlipBuffers()
  
  ; Process keyboard inputs.
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Escape)
    shouldExit = #True
  EndIf
Until shouldExit
In this example, the screen/window is automatically hidden when any other window receives focus (including the desktop). This behaviour can of course be adjusted.

Simply change #SCREEN_TARGET_DESKTOP_ID to the ID of the desktop you desire. Generally you'd want to give the user some means of configuring this.

Tip: we can't change the backing size of windowed screens at runtime, so it can be a hassle to deal with monitors/windows of differing resolutions (where auto-stretch results in quality loss). As a workaround, you can disable auto-stretch and simply create your screen with bounds consisting of the greatest width/height discovered from ExamineDesktops(). Then, render only in the bounds held by the window rather than the screen. This will allow for gracefully switching between fullscreen-mode, windowed-mode, different monitors, as well as resizing. (this has a performance hit but it's generally negligible)

Tip: prefer drawing to and rendering with sprites rather than ScreenOutput(), for the best performance across platforms.
andrey2023
New User
New User
Posts: 7
Joined: Mon Mar 06, 2023 5:55 am

Re: Output to a specific monitor

Post by andrey2023 »

thanks, that's what you need!
Post Reply