Author Topic: How to size the window in OpenGL  (Read 4505 times)

0 Members and 1 Guest are viewing this topic.

chrisc

  • Guest
Re: How to size the window in OpenGL
« Reply #15 on: April 16, 2018, 06:31:13 AM »
Thanxx  a lot everyone         :)
this forum is the best in the world

Chris Boss

  • Guest
Re: How to size the window in OpenGL
« Reply #16 on: April 17, 2018, 09:08:11 AM »
Windows handles the maximum size of the window if your window is resizable. When it resizes (%WM_SIZE message) the key is to properly get the client area size and then rescale the viewport for the openGL RC.

The GetClientRect API can provide the client area of the new window size. Then make a call to the following API's:

wglMakeCurrent

makes the openGL RC the current one.

glViewPort

to define the new area for the RC using the new window size.




Arnold

  • Guest
Re: How to size the window - for Mike
« Reply #17 on: April 22, 2018, 03:30:06 AM »
Hi Mike,

will this CenterWindow function work better? I have not much possibilities to check with different displays.

Roland

Code: OxygenBasic
  1. $ filename "Centerwindow.exe"
  2.  
  3. 'uses rtl32
  4. 'uses rtl64
  5.  
  6. uses WinUtil
  7.  
  8. #autodim off
  9.  
  10. % ID_BUTTON1=100
  11. sys hButton1
  12.  
  13.  
  14. declare sub CenterWindow(sys hwnd, int place=0)
  15.  
  16. sys hInstance=inst
  17. MainWindow 400,300,WS_OVERLAPPEDWINDOW
  18.  
  19.  
  20. function WndProc(sys hwnd, uMsg, wParam, lParam) as sys callback
  21.  
  22.     select uMsg
  23.    
  24.         case WM_CREATE
  25.             hButton1 = CreateWindowEx (0,"button", "Start Demo",
  26.                                        WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON | WS_TABSTOP,
  27.                                       150, 200, 100, 30, hwnd, ID_BUTTON1,
  28.                                       hInstance, NULL)
  29.    
  30.         case WM_COMMAND
  31.             if loword(wParam) = ID_BUTTON1 then
  32.                int x
  33.                for x=1 to 4
  34.                  CenterWindow(hwnd, x)
  35.                  Sleep 1000 'wait a second
  36.               next
  37.                CenterWindow(hwnd)  
  38.             end if  
  39.        
  40.         case WM_CLOSE
  41.             DestroyWindow(hwnd)
  42.        
  43.         case WM_DESTROY
  44.             PostQuitMessage(0)
  45.        
  46.         case else
  47.             return DefWindowProc(hwnd, uMsg, wParam, lParam)
  48.            
  49.     end select
  50.    
  51.     return 0
  52. end function
  53.  
  54. ==============================
  55.  
  56. sub CenterWindow(sys hwnd, int place=0)
  57.     '0=centre, 1=left,2=top,3=right,4=bottom centered
  58.    int hpos,vpos,WaWidth,WaHeight
  59.     int spinfo[4]
  60.  
  61.     RECT rc
  62.     GetWindowRect(hwnd,&rc)
  63.     width=rc.right-rc.left
  64.     height=rc.bottom-rc.top
  65.  
  66.     SystemParametersInfo 0x0030,0,@spinfo,0    'SPI_GETWORKAREA=0x30
  67.    indexbase 1
  68.     WaWidth=spinfo[3]
  69.     WaHeight=spinfo[4]
  70.  
  71.     select place
  72.     case 0 : hpos=(wawidth-width)>>1 : vpos=(WaHeight-height-32)>>1 'centre
  73.    case 1 : hpos=0 : vpos=(WaHeight-height-32)>>1             'left center
  74.    case 2 : hpos=(wawidth-width)>>1 : vpos=0                  'top center
  75.    case 3 : hpos=wawidth-width : vpos=(WaHeight-height-32)>>1 'right center
  76.    case 4 : hpos=(wawidth-width)>>1 : vpos=waHeight-height    'bottom center
  77.    case else : hpos=rc.left : vpos=rc.top    'do nothing
  78.    end select
  79.        
  80.     SetWindowPos (hWnd, 0, hpos, vpos, WaWidth, WaHeight, 1) 'SWP_NOSIZE=1
  81. end sub
  82.  

chrisc

  • Guest
Re: How to size the window in OpenGL
« Reply #18 on: April 22, 2018, 08:54:20 AM »
Hello Roland

you need to declare  width and height  at the start of the sub CenterWindow()
or you can't compile it

Code: [Select]
sub CenterWindow(sys hwnd, int place=0)
int width , height

Mike Lobanovsky

  • Guest
Re: How to size the window in OpenGL
« Reply #19 on: April 22, 2018, 10:53:32 AM »
Hehe, that's a nice idea for a demo, Roland! :)

But no, your implementation of CenterWindow() is not correct. It doesn't take into account the actual values of the unobscured area's top left and bottom right corners obtained in spinfo[]. (Chris is right saying your code as-is cannot be compiled at all)

This is how it should be:
Code: OxygenBasic
  1. $ filename "Centerwindow.exe"
  2.  
  3. 'uses rtl32
  4. 'uses rtl64
  5.  
  6. uses WinUtil
  7.  
  8. #autodim off
  9.  
  10. % ID_BUTTON1=100
  11. sys hButton1
  12.  
  13.  
  14. declare sub CenterWindow(sys hwnd, int place=0)
  15.  
  16. sys hInstance=inst
  17. MainWindow 400,300,WS_OVERLAPPEDWINDOW
  18.  
  19.  
  20. function WndProc(sys hwnd, uMsg, wParam, lParam) as sys callback
  21.  
  22.     select uMsg
  23.    
  24.         case WM_CREATE
  25.             hButton1 = CreateWindowEx (0,"button", "Start Demo",
  26.                                        WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON | WS_TABSTOP,
  27.                                       150, 200, 100, 30, hwnd, ID_BUTTON1,
  28.                                       hInstance, NULL)
  29.    
  30.         case WM_COMMAND
  31.             if loword(wParam) = ID_BUTTON1 then
  32.                int x
  33.                for x=0 to 4
  34.                  CenterWindow(hwnd, x)
  35.                  Sleep 1000 'wait a second
  36.               next
  37.                CenterWindow(hwnd)  
  38.             end if  
  39.        
  40.         case WM_CLOSE
  41.             DestroyWindow(hwnd)
  42.        
  43.         case WM_DESTROY
  44.             PostQuitMessage(0)
  45.        
  46.         case else
  47.             return DefWindowProc(hwnd, uMsg, wParam, lParam)
  48.            
  49.     end select
  50.    
  51.     return 0
  52. end function
  53.  
  54. ==============================
  55.  
  56. sub CenterWindow(sys hwnd, int place=0)
  57.     '0=center, 1=left,2=top,3=right,4=bottom centered
  58.    int spinfo[4]
  59.     RECT rc
  60.  
  61.     GetWindowRect(hwnd,&rc)
  62.  
  63.     int hpos=rc.left,vpos=rc.top
  64.     int width=rc.right-rc.left
  65.     int height=rc.bottom-rc.top
  66.  
  67.     SystemParametersInfo 0x0030,0,@spinfo,0    'SPI_GETWORKAREA=0x30
  68.  
  69.     indexbase 1
  70.     int horz=spinfo[1]+(spinfo[3]-spinfo[1]-width)>>1
  71.     int vert=spinfo[2]+(spinfo[4]-spinfo[2]-height)>>1
  72.  
  73.     select place
  74.     case 0 : hpos=horz : vpos=vert             'center
  75.    case 1 : hpos=spinfo[1] : vpos=vert        'left center
  76.    case 2 : hpos=horz : vpos=spinfo[2]        'top center
  77.    case 3 : hpos=spinfo[3]-width : vpos=vert  'right center
  78.    case 4 : hpos=horz : vpos=spinfo[4]-height 'bottom center
  79.    end select
  80.        
  81.     SetWindowPos (hWnd, 0, hpos, vpos, width, height, 4) 'SWP_NOZORDER=4
  82. end sub

Also, this implementation only runs on the primary monitor. To make it work on multiple monitor setups, it should use GetMonitorInfo() rather than SystemParametersInfo().

chrisc

  • Guest
Re: How to size the window in OpenGL
« Reply #20 on: April 22, 2018, 01:02:57 PM »
Thanxx Mike and Roland

i have modified the program to generate randomize background color windows for each location
but not successful ?  maybe some one can modify it to produce those random background color windows

Code: [Select]
' http://www.oxygenbasic.org/forum/index.php?topic=1636.msg17936;topicseen#msg17936
'  Centerwindow.o2bas
 $ filename "Centerwindow.exe"
     
   
    uses rtl64
     
    uses WinUtil

' for randomizer
! GetTickCount lib "kernel32.dll" alias "GetTickCount" () as dword
! srand cdecl lib "msvcrt.dll" (int seed)
! rand cdecl lib "msvcrt.dll" () as int

    uses corewin
    Use Chaos
    seed=GetTickCount


    #autodim off
     
    % ID_BUTTON1=100
    sys hButton1
     

   '============================
' RGB function for O2
function RGB(int rcc, gcc, bcc) as int
    return (rcc + gcc*256 + bcc*65536)
end Function

' ==============================
'  RANDOMIZE TIMER; use once per the entire script
sub Randomize()
  srand GetTickCount()
end sub



  '==========================================
' draws with color gradient with random colors
SUB DrawGradientRnd (BYVAL hDC AS DWORD)
   LOCAL rectFill AS RECT, rectClient AS RECT, fStep AS SINGLE
  LOCAL  hBrush AS DWORD, lOnBand AS LONG
   GetClientRect WindowFromDC(hDC), rectClient
   fStep = rectClient.bottom / 75

 '  local LastBand as Long
  ' LastBand =  irnd( 189, 230 )

   FOR lOnBand = 0 TO  199
      SetRect rectFill, 0, lOnBand * fStep, rectClient.right + 1, (lOnBand + 1) * fStep
      ' paint the background -- change the first 2 colors R and G
      ' to vary the color gradient
      hBrush = CreateSolidBrush(rgb(230-  irnd( 9, 30 ), 240, 230 - lOnBand))
      Fillrect hDC, rectFill, hBrush
      DeleteObject hBrush
   NEXT

END SUB


    declare sub CenterWindow(sys hwnd, int place=0)
     
    sys hInstance=inst
     Randomize()
    MainWindow 400,300,WS_OVERLAPPEDWINDOW
     
     



'============================================
    function WndProc(sys hwnd, uMsg, wParam, lParam) as sys callback
       int x
       local hdc as sys

        select uMsg
       
            case WM_CREATE
                hButton1 = CreateWindowEx (0,"button", "Start Demo",
                                           WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON | WS_TABSTOP,
                                          150, 200, 100, 30, hwnd, ID_BUTTON1,
                                          hInstance, NULL)
       
            case WM_COMMAND
                if loword(wParam) = ID_BUTTON1 then
                               
                   for x=0 to 4
                     CenterWindow(hwnd, x)
                     Sleep 1000 'wait a second
                   next
                   CenterWindow(hwnd) 
                end if   

           
 'graphic/drawing events
 'sent when the window background must be erased such as resizing
       CASE %WM_ERASEBKGND     
          hDC = wParam
  '      Pass the DC of the region to repaint
          DrawGradientRnd  hDC           
          FUNCTION = 1
          EXIT FUNCTION



            case WM_CLOSE
                DestroyWindow(hwnd)
           
            case WM_DESTROY
                PostQuitMessage(0)
           
            case else
                return DefWindowProc(hwnd, uMsg, wParam, lParam)
               
        end select
       
        return 0
    end function
     


    ==============================
    sub CenterWindow(sys hwnd, int place=0)
        '0=center, 1=left,2=top,3=right,4=bottom centered
        int spinfo[4]
        RECT rc
     
        GetWindowRect(hwnd,&rc)
     
        int hpos=rc.left,vpos=rc.top
        int width=rc.right-rc.left
        int height=rc.bottom-rc.top
     
        SystemParametersInfo 0x0030,0,@spinfo,0    'SPI_GETWORKAREA=0x30
     
        indexbase 1
        int horz=spinfo[1]+(spinfo[3]-spinfo[1]-width)>>1
        int vert=spinfo[2]+(spinfo[4]-spinfo[2]-height)>>1
     
        select place
        case 0 : hpos=horz : vpos=vert             'center
        case 1 : hpos=spinfo[1] : vpos=vert        'left center
        case 2 : hpos=horz : vpos=spinfo[2]        'top center
        case 3 : hpos=spinfo[3]-width : vpos=vert  'right center
        case 4 : hpos=horz : vpos=spinfo[4]-height 'bottom center
        end select
           
        SetWindowPos (hWnd, 0, hpos, vpos, width, height, 4) 'SWP_NOZORDER=4
    end sub

chrisc

  • Guest
Re: How to size the window in OpenGL
« Reply #21 on: April 22, 2018, 01:20:16 PM »
also,  how to disable the button1 while the window is moving around within the loop for x = 0 t0 4 below

since the user can always click on this button again and may cause problem to the program?

Code: [Select]
case WM_COMMAND
                if loword(wParam) = ID_BUTTON1 then
                               
                   for x=0 to 4
                     CenterWindow(hwnd, x)
                     Sleep 1000 'wait a second
                   next
                   CenterWindow(hwnd)
                end if   

chrisc

  • Guest
Re: How to size the window in OpenGL
« Reply #22 on: April 22, 2018, 01:39:51 PM »
this could be a solution ?  i have use redraw window for each step of the loop

as during window redraw, user cannot click on the button1 as the button1 is not visible

but i still not able to randomize the display of the color background?

Code: [Select]
' http://www.oxygenbasic.org/forum/index.php?topic=1636.msg17936;topicseen#msg17936
'  Centerwindow.o2bas
 $ filename "Centerwindow.exe"
     
   
    uses rtl64
     
    uses WinUtil

' for randomizer
! GetTickCount lib "kernel32.dll" alias "GetTickCount" () as dword
! srand cdecl lib "msvcrt.dll" (int seed)
! rand cdecl lib "msvcrt.dll" () as int

    uses corewin
    Use Chaos
    seed=GetTickCount


    #autodim off
     
    % ID_BUTTON1=100
    sys hButton1

'   for redrawing the window
    %RDW_INVALIDATE   0x0001
    %RDW_ERASE           0x0004

   '============================
' RGB function for O2
function RGB(int rcc, gcc, bcc) as int
    return (rcc + gcc*256 + bcc*65536)
end Function

' ==============================
'  RANDOMIZE TIMER; use once per the entire script
sub Randomize()
  srand GetTickCount()
end sub



  '==========================================
' draws with color gradient with random colors
SUB DrawGradientRnd (BYVAL hDC AS DWORD)
   LOCAL rectFill AS RECT, rectClient AS RECT, fStep AS SINGLE
  LOCAL  hBrush AS DWORD, lOnBand AS LONG
   GetClientRect WindowFromDC(hDC), rectClient
   fStep = rectClient.bottom / 75

 '  local LastBand as Long
  ' LastBand =  irnd( 189, 230 )

   FOR lOnBand = 0 TO  199
      SetRect rectFill, 0, lOnBand * fStep, rectClient.right + 1, (lOnBand + 1) * fStep
      ' paint the background -- change the first 2 colors R and G
      ' to vary the color gradient
      hBrush = CreateSolidBrush(rgb(230-  irnd( 9, 30 ), 240, 230 - lOnBand))
      Fillrect hDC, rectFill, hBrush
      DeleteObject hBrush
   NEXT

END SUB


    declare sub CenterWindow(sys hwnd, int place=0)
     
    sys hInstance=inst
     Randomize()
    MainWindow 400,300,WS_OVERLAPPEDWINDOW
     
     



'============================================
    function WndProc(sys hwnd, uMsg, wParam, lParam) as sys callback
       int x
       local hdc as sys

        select uMsg
       
            case WM_CREATE
                hButton1 = CreateWindowEx (0,"button", "Start Demo",
                                           WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON | WS_TABSTOP,
                                          150, 200, 100, 30, hwnd, ID_BUTTON1,
                                          hInstance, NULL)
       
            case WM_COMMAND
                if loword(wParam) = ID_BUTTON1 then
                               
                   for x=0 to 4
                       RedrawWindow(hwnd, NULL, NULL, RDW_ERASE or RDW_INVALIDATE)
                     CenterWindow(hwnd, x)
                     Sleep 1000 'wait a second
                   next
                   CenterWindow(hwnd) 
                end if   

           
 'graphic/drawing events
 'sent when the window background must be erased such as resizing
       CASE %WM_ERASEBKGND     
          hDC = wParam
  '      Pass the DC of the region to repaint
          DrawGradientRnd  hDC           
          FUNCTION = 1
          EXIT FUNCTION



            case WM_CLOSE
                DestroyWindow(hwnd)
           
            case WM_DESTROY
                PostQuitMessage(0)
           
            case else
                return DefWindowProc(hwnd, uMsg, wParam, lParam)
               
        end select
       
        return 0
    end function
     


    ==============================
    sub CenterWindow(sys hwnd, int place=0)
        '0=center, 1=left,2=top,3=right,4=bottom centered
        int spinfo[4]
        RECT rc
     
        GetWindowRect(hwnd,&rc)
     
        int hpos=rc.left,vpos=rc.top
        int width=rc.right-rc.left
        int height=rc.bottom-rc.top
     
        SystemParametersInfo 0x0030,0,@spinfo,0    'SPI_GETWORKAREA=0x30
     
        indexbase 1
        int horz=spinfo[1]+(spinfo[3]-spinfo[1]-width)>>1
        int vert=spinfo[2]+(spinfo[4]-spinfo[2]-height)>>1
     
        select place
        case 0 : hpos=horz : vpos=vert             'center
        case 1 : hpos=spinfo[1] : vpos=vert        'left center
        case 2 : hpos=horz : vpos=spinfo[2]        'top center
        case 3 : hpos=spinfo[3]-width : vpos=vert  'right center
        case 4 : hpos=horz : vpos=spinfo[4]-height 'bottom center
        end select
           
        SetWindowPos (hWnd, 0, hpos, vpos, width, height, 4) 'SWP_NOZORDER=4
    end sub

Arnold

  • Guest
Re: How to size the window in OpenGL
« Reply #23 on: April 22, 2018, 02:28:11 PM »
Thank you Mike for the corrections. I will look at GetMonitorInfo() tomorrow.
For me compiling my example code above to 32-bit and 64-bit worked (on my 32-bit pc). This is indeed a surprise because #autodim off should have indicated an error. I used Oxygen.dll of 29/03/2018 and I compiled with gxo2.exe and co2.exe. So I do not know at the moment what is different with my installation.

Roland

Mike Lobanovsky

  • Guest
Re: How to size the window in OpenGL
« Reply #24 on: April 22, 2018, 03:08:02 PM »
@Roland:

I used Oxygen.dll of 29/03/2018 ...

That's what I do too.

Quote
... I compiled with gxo2.exe and co2.exe.

No, you can't compile your script verbatim because it has both 'uses rtl32 and 'uses rtl64 remmed out. The only thing you can do with it is run it in the JIT mode. Try F5 in your OxIde and see what happens.

@Chris:

how to disable the button1 while the window is moving around within the loop for x = 0 t0 4 below

Use the EnableWindow() API where FALSE = 0 and TRUE = 1.

chrisc

  • Guest
Re: How to size the window in OpenGL
« Reply #25 on: April 22, 2018, 03:30:17 PM »
Thanxx Mike

Code: [Select]
     EnableWindow hButton1, %FALSE

Arnold

  • Guest
Re: How to size the window in OpenGL
« Reply #26 on: April 22, 2018, 11:02:04 PM »
Hi Mike,

I comment out: uses rtl32 or uses rtl64 to run in JIT mode, but usually I test every case.

This morning I tried the previous example with the original installation of 03/29 and this time I got the error messages. Perhaps the difference arrises from these lines in the newer winutil.inc:
  #ifndef width
    int width=640
    int height=480
  #endif

but I am not sure about this.

Roland

Mike Lobanovsky

  • Guest
Re: How to size the window in OpenGL
« Reply #27 on: April 23, 2018, 05:33:45 AM »
... the difference arrises from these lines in the newer winutil.inc:
  #ifndef width
    int width=640
    int height=480
  #endif

Hi Roland,

Exactly!

chrisc

  • Guest
Re: How to size the window in OpenGL
« Reply #28 on: April 23, 2018, 06:16:42 AM »
Hello Roland

i was able to compile using the latest inc for the program without errors
i atteched these inc and program for you to test


Arnold

  • Guest
Re: How to size the window in OpenGL
« Reply #29 on: April 23, 2018, 07:30:33 AM »
Hi Chris,

it is really not necessary to upload the complete inc folder for your demo. I can run your app without using this folder - believe me. And for developing and testing I do not use the rtl32/rtl64 include files because this is faster under normal circumstances. The only problem here was why I do not get error messages if I forget to declare height and width. And this was caused because I use a different winutil.inc.

Roland
« Last Edit: April 23, 2018, 07:49:03 AM by Arnold »