Oxygen Basic

Information => Open Forum => Topic started by: kryton9 on June 02, 2012, 11:05:02 PM

Title: callback function vrs virtual method in a class
Post by: kryton9 on June 02, 2012, 11:05:02 PM
Charles, if you remember when we worked on the GLWindow class, we used callback functions for the render and input procedures.  I am completely rewriting that class, cGLWindow will have virtual methods render and input. The user of the class would derive their own class from cGLWindow and write their code in those virtual methods.

Which of these 2 ways should I select? Which would perform the best?
Title: Re: callback function vrs virtual method in a class
Post by: Charles Pegge on June 03, 2012, 07:14:03 AM
Hi Kent,

Callbacks are easier. Macros might be better still, depending on how much of cGLWindow you want to expose.

Stitching up WndProc in a class is not ideal. Usually the solutions are more complex than the classical code.

I think all the multiple couplings make the code quite difficult to debug. Anyway, I'll continue looking into this Window Architecture issue.

Charles
Title: Re: callback function vrs virtual method in a class
Post by: Charles Pegge on June 03, 2012, 11:45:24 AM
The code below is a modified Create method. It contains a wndproc link, supplying an object pointer so that WinProc can be redefined as a proper method with access to all the members of the class, instead of being a function with access restricted to static members only.

With this configuration, none of the class members have to be static, and new classes can be derived from cGLWindow without complication.

           'WndProc interface function
            '==========================
            '
            static thisObject, pwin
            '
            thisObject=@this
            pWin=@WindowProc#sys#sys#sys#sys
            '
            function WndProc( sys mhWnd, uMsg, wParam, lParam ) as sys callback
                return call pWin(thisObject,mhWnd, uMsg, wParam, lParam )
            end function
    

Code: OxygenBasic
  1.  
  2.         method Create( sys aWidth   =  800,
  3.                        sys aHeight  =  600,
  4.                        sys aBits    =   32,
  5.                        sys aFullscreen = 0,
  6.                        string aCaption = "Simple GLWindow",
  7.                        sys aLeft = 0,
  8.                        sys aTop  = 0,
  9.                        sys aIcon = 0,
  10.                        sys aMenu = 0 ) as sys
  11.  
  12.             mWidth     = aWidth
  13.             mHeight    = aHeight
  14.             mBPP       = aBits
  15.             mLeft      = aLeft
  16.             mTop       = aTop
  17.             mCaption   = aCaption
  18.             mClassName = "GLWindow"
  19.  
  20.             mWindowRect.left   = mLeft
  21.             mWindowRect.right  = mWidth
  22.             mWindowRect.top    = mTop
  23.             mWindowRect.bottom = mHeight
  24.  
  25.             mFullScreen = aFullscreen
  26.  
  27.             mhInstance  = GetModuleHandle 0
  28.             '
  29.            'WndProc interface function
  30.            '==========================
  31.            '
  32.            static thisObject, pwin
  33.             '
  34.            thisObject=@this
  35.             pWin=@WindowProc#sys#sys#sys#sys
  36.             '
  37.            function WndProc( sys mhWnd, uMsg, wParam, lParam ) as sys callback
  38.                 return call pWin(thisObject,mhWnd, uMsg, wParam, lParam )
  39.             end function
  40.             '
  41.            WNDCLASS winClass
  42.             winClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC
  43.             winClass.lpfnWndProc   = @WndProc
  44.             winClass.cbClsExtra    = 0
  45.             winClass.cbWndExtra    = 0
  46.             winClass.hInstance     = mhInstance
  47.             winClass.hIcon         = LoadIcon NULL, IDI_WINLOGO
  48.             winClass.hCursor       = LoadCursor NULL, IDC_ARROW
  49.             winClass.hbrBackground = NULL
  50.             winClass.lpszMenuName  = NULL
  51.             winClass.lpszClassName = *mClassName
  52.             if not RegisterClass @winClass then
  53.                 'error
  54.                MessageBox NULL, "RegisterClass", "ERROR", MB_OK
  55.                 return 0
  56.             end if
  57.             Remake()
  58.         end method
  59.  

Charles
Title: Re: callback function vrs virtual method in a class
Post by: kryton9 on June 03, 2012, 12:43:50 PM
Thanks for another way to do things example Charles.

What I want to do is make a window class for opengl that is very flexible. It can be:
1. simple glwindow with 1 viewport
2. simple glwindow with many viewports
3. 2 or more glwindows with 1 viewport each
4. 2 or more glwindows with many viewports each

The multiple windows would be able to be moved to different monitors.
So that the main monitor could be the actual game window fullscreen for instance. The second monitor could have other windows that could show maps for a level, statistics for the game, perhaps alternate views such as side, back, top and front for flight sims.

I was looking through the OOP examples folder and refreshed my memory on some of theoptions. I saw the interface examples, but couldn't I also just use virtual methods?
Title: Re: callback function vrs virtual method in a class
Post by: Charles Pegge on June 03, 2012, 05:34:10 PM

I'm not familiar with dual monitor technology. I presume there are some OpenGl functions to channel instructions to each video processor.

I think multiple child Opengl Windows will work on many but not all cards. Petr knows of problems with some ATI cards as I recall. I found it was okay to use a common hRC and simply switch the hDCs to build the scene frames in each child window. (Using NVidia)

Child Windows require somewhat different handling than the main window and they all have to be coordinated when resizing or stretching.

Not sure what what you mean by virtual methods, Kent. That could mean several things but having separate objects for each viewport or child window would make sense, and the basic architecture would be similar to what you have so far.

Charles
Title: Re: callback function vrs virtual method in a class
Post by: kryton9 on June 03, 2012, 08:13:58 PM
It is fun working on this stuff and Oxygen is just amazing. Everyday it surprises me in how flexible it is. I don't how you put it together so quickly. I was trying to follow how you might have gone about it, but I kept running into the which came first the chicken or the egg. Its like where do you begin. I am amazed!
Title: Re: callback function vrs virtual method in a class
Post by: Charles Pegge on June 04, 2012, 01:56:48 AM

After a few adventures with machine code strings, and the Assembler, I started with a toy compiler which could just about manage a=b+c using integers. Then it was just a matter of extending its capabilities in all directions. Definitely not top-down design. :)

Charles
Title: Re: callback function vrs virtual method in a class
Post by: Aurel on July 27, 2012, 11:19:21 AM
Hi people..
I don't know how i miss this topic.
Charles what i need to compile this example ?
This might be solution for what i talk under GUI topic about elimination
of long code with DefWindowProc retrurn.
What you think?

PS.I don't like OOP but looks like good option...

Aurel
Title: Re: callback function vrs virtual method in a class
Post by: kryton9 on July 27, 2012, 03:43:12 PM
Aurel, it can also be done without classes.

Perhaps it would be better if the forums were split into 2 parts. Flat Procedural Oxygen and OOP Oxygen?

In reviewing things that I forgot this passed month. Here is the difference between the two.

In flat coding you would do this:

MyFunction( IDofObject, WhatIWantToDoValues)

Here MyFunction would then depending on the IDofObject
Probably have a Switch statement to decide how to apply the function
for this type of object. Here for all new objects, the framework
code would need to be updated.



In OOP it is:

Object.MyFunction(WhatIWantToDoValues)

Here the Object would know what to do.
So code is self contatined and can be added without changing
old frame code for a new object type.


That is one of the key benefits of OOP. There are many more.
Title: Re: callback function vrs virtual method in a class
Post by: Aurel on July 27, 2012, 04:04:09 PM
Quote
Aurel, it can also be done without classes.

Hi Kent...
But how to do that, i am now totally confused ???
As you see from my posts i am searching for simple way to open window and
message processing without so much typing everytime :
Wndproc(sys h,m,w,l) as sys callback
...
...
select m
...
...
default
return Defwindowproc(h,m,w,l)

again..and again for each window.
Do you can provide example....?
Title: Re: callback function vrs virtual method in a class
Post by: kryton9 on July 27, 2012, 05:07:40 PM
I will work on one tonight for you without OOP. Will post when finished and working.

updated:
I could not get it to work. Charles will have to look at it. I can't get the callback to work.
I tried @, & with it aInputProc =  and without, and no luck.

window.inc and test window.o2bas are the only 2 files you need to look at. The others are the includes working in oxygen from VB6 cleaned up files.

X
Title: Re: callback function vrs virtual method in a class
Post by: Aurel on July 28, 2012, 12:49:07 AM
Thank you Kent... ;)
Yes you right ...not work because last parameter is not recognized.
I have think that this might be solution and i really like how you do this. :)
Kent ...i don't know if we can define last parameter as function
maybe on this way will work... ::)

X
Title: Re: callback function vrs virtual method in a class
Post by: kryton9 on July 28, 2012, 07:06:03 PM
I will keep working on it, but I think it needs Charles input to figure it out. I will try another idea I had today while out and about and see if I can get that to work.
Update: This is another approach. It compiles and runs, but the windows does not show up. It runs for very short time then terminates if you watch the processes in your system. Perhaps you can see what I am doing wrong. I am tired and will call it quits for now. I switched to using Charles
MinWin.inc to see if that would work. When I used my headers, it would give an error at CreateWindowEx. With the WinMin.inc it doesn't give any error.

X
Title: Re: callback function vrs virtual method in a class
Post by: Aurel on July 28, 2012, 11:26:10 PM
Thank you Kent.. :)
I will try it right now... ;)

Yes i agree ...this need Charles input of course..
Title: Re: callback function vrs virtual method in a class
Post by: Aurel on July 29, 2012, 12:54:42 AM
Unfortunately Kent don't work as all ours try-s.
It looks to me that only way might be Charles example with this pointer.
But i don't know how to compile & run this example... ???
Title: Re: callback function vrs virtual method in a class
Post by: Aurel on July 29, 2012, 01:36:10 AM
Uff i try this way,but im not sure exactly what im doing ::)
Compiler says that some elements of win are missing like mWidth but is there .. >:(
here is micro.inc
Code: [Select]
'micro.inc by Aurel
'Last Modified:
'2012.july,29

'myWin variables have a "m" prefix for them
'Parameters/Arguements have an "a" prefix for arguement
#lookahead
Type WNDCLASS
 'cbSize        as long
 Style         as long
 lpfnwndproc   as long
 cbClsextra    as long
 cbWndExtra    as long
 hInstance     as long
 hIcon         as long
' hIconSm       AS long
 hCursor       as long
 hbrBackground as long
 lpszMenuName  as long
 lpszClassName as long
End Type

Type LARGE_INTEGER
lowpart AS INT
highpart AS INT
End Type


Type PointApi
 x as long
 y as long
End Type

Type MSG
 hwnd    as long
 message as long
 wParam  as long
 lParam  as long
 time    as long
 pt      as PointApi
End Type

Type RECT
 Left   as Long
 Top    as Long
 Right  as Long
 Bottom as Long
End Type

% CS_DBLCLKS     = 0x8
% CS_OWNDC       = 32
% SW_SHOW        = 5
% PM_REMOVE      = 1
% PM_NOREMOVE    = 0
% IDI_APPLICATION = 32512
% IDC_ARROW      = 32512

% WM_SETICON     = &H80
% WM_KILLFOCUS = 0x8 
% WM_CREATE      = 1
% WM_DESTROY     = 2
% WM_PAINT       = 15
% WM_QUIT        = 18
% WM_SIZE        = 5
% WM_MOVE        = 3
% WM_CHAR        = 258
% WM_KEYLAST     = &H108
% WM_KEYFIRST    = &H100
% WM_KEYDOWN     = 256
% WM_MOUSEMOVE   = 512
% WM_MBUTTONDOWN = 519
% WM_LBUTTONDOWN = 513
% WM_RBUTTONDOWN = 516
% WM_LBUTTONUP   = 514
% WM_RBUTTONUP   = 517
% WM_MBUTTONUP   = 520
% WM_TIMER       = 275
% WM_WINDOWPOSCHANGED = &H47
% WM_NOTIFY      = 0x004E
% WM_SETFONT = &H30
% WM_COMMAND = 0x111
% BN_CLICKED = 0
% WM_PARENTNOTIFY = 0x210
% WM_SETTEXT  = &HC
% WM_GETTEXT = 0xD

% WS_CLIPSIBLINGS = 0x4000000
% WS_CLIPCHILDREN = 0x2000000
% WS_SYSMENU     = 524288
% WS_THICKFRAME = 0x40000
% WS_CAPTION = 0xC00000
% WS_OVERLAPPED  = 0x0 
% WS_MINMAXSIZE  =
% WS_POPUP       = 0x80000000
% WS_DLGFRAME    = 0x400000
% WS_MAXIMIZE    = &H1000000
% WS_MINIMIZEBOX = &H20000
% WS_MAXIMIZEBOX = 0x10000
% WS_BORDER      = &H800000
% WS_CHILD = 0x40000000
% WS_VISIBLE = 0x10000000
% WS_VSCROLL = 0x200000
% WS_HSCROLL = 0x100000
'minmaxsize-> overlappedwindow
% WS_MINMAXSIZE =(WS_OVERLAPPED Or WS_VISIBLE Or WS_CAPTION Or WS_SYSMENU Or WS_THICKFRAME Or WS_MINIMIZEBOX Or WS_MAXIMIZEBOX)


Dim kernel32,user32,gdi32,riched32
kernel32 = LoadLibrary "kernel32.dll"
user32   = LoadLibrary "user32.dll"
gdi32    = LoadLibrary "gdi32.dll"
riched32 =  LoadLibrary "riched32.dll"

Bind kernel32
(
  GetCommandLine  GetCommandLineA   
  GetModuleHandle GetModuleHandleA 
  ExitProcess     ExitProcess
  sleep           Sleep
  Beep            Beep   


Bind user32
(
  LoadIcon         LoadIconA       
  LoadCursor       LoadCursorA       
  RegisterClass    RegisterClassA
  RegisterClassEx  RegisterClassExA   
  MessageBox       MessageBoxA       
  SendMessage      SendMessageA
  GetMessage       GetMessageA
  PeekMessage      PeekMessageA
  TranslateMessage TranslateMessage
  DispatchMessage  DispatchMessageA
  PostQuitMessage  PostQuitMessage 
  PostMessage      PostMessageA
  CreateWindowEx   CreateWindowExA   
  ShowWindow       ShowWindow       
  UpdateWindow     UpdateWindow     
  DefWindowProc    DefWindowProcA
  InvalidateRect   InvalidateRect
  ValidateRect     ValidateRect
  GetSystemMetrics GetSystemMetrics
  ReleaseDC        ReleaseDC
  GetDC            GetDC
  BeginPaint        BeginPaint
  EndPaint          EndPaint   
  ShowCursor       ShowCursor
  GetAsyncKeyState GetAsyncKeyState
)
 
Bind gdi32
(
  GetStockObject   GetStockObject
  CreateSolidBrush CreateSolidBrush
  SetBkMode        SetBkMode
  SetBkColor       SetBkColor


declare sub input()

'define Msg,Class & ClassEx //////////////////////////////////
Dim wm as MSG

Dim rc as RECT
Dim wcx as WndClass
'--------------------------------------------------------------------
Type TWINDOW
sys aw
sys ah
string aCaption
sys mWidth 
sys mHeight 
sys mLeft   
sys mTop     
string mCaption
string mClassName
sys mhInstance
sys mStyle
sys mHwnd
sys mIsActive
sys mIsRunning
sys mMsg
End Type

'--------------------------------------------------------------------
TWINDOW win
static  tWindow, win 
   tWindow = @this 
   win = @WindowProc#sys#sys#sys#sys 
'---------------------------------------------------------------------
FUNCTION WindowProc( sys mhwnd, uMsg, wParam, lParam ) as sys callback
   
   ' RETURN DefWindowProc hWnd, uMsg, wParam, lParam
 Return CALL Win(tWindow,mhWnd, uMsg, wParam, lParam )
END FUNCTION
'-------------------------------------------------------------------
Function MakeWindow(
                    sys aw=800,
                    sys ah=600,   
                    string aCaption = "Oxygen Window"
                    )
    win.mWidth     = aw
    win.mHeight    = ah
    win.mLeft      = 0
    win.mTop       = 0
    win.mCaption   = aCaption
    win.mClassName = "OxygenWindow"

   

    win.mhInstance  = GetModuleHandle 0
    '
    WNDCLASS winClass
    winClass.style         = CS_OWNDC | CS_DBLCLKS
    winClass.lpfnWndProc   = &WindowProc
    winClass.cbClsExtra    = 0
    winClass.cbWndExtra    = 0
    winClass.hInstance     = win.mhInstance
    winClass.hIcon         = LoadIcon 0, IDI_APPLICATION
    winClass.hCursor       = LoadCursor 0, IDC_ARROW
    winClass.hbrBackground = 0
    winClass.lpszMenuName  = 0
    winClass.lpszClassName = win.mClassName
    if not RegisterClass(winClass) then
        'error
        MessageBox 0, "RegisterClass", "ERROR", 32
        return 0
    end if
   
    win.mStyle = WS_MINMAXSIZE
   
    'win.mWindowRect <= 0, 0, win.mWidth, win.mHeight
   
    win.mhWnd = CreateWindowEx( 0,
                                win.mClassName,
                                "Oxygen Window", 'win.mCaption,
                                WS_MINMAXSIZE, 'win.mStyle,
                                win.mLeft,
                                win.mTop,
                                win.mWidth ,
                                win.mHeight,
                                0,
                                0,
                                win.mhInstance,
                                0
                              )
    if not win.mhWnd then
        'Error
        MessageBox NULL, "CreateWindowEx:", "ERROR",32
        return 0
    end if

    win.mIsActive  = 1
    win.mIsRunning = 1
   
    ShowWindow win.mhWnd, SW_SHOW
    UpdateWindow win.mhWnd

    return win.mhWnd
end function

sub MessageLoop()
    while win.mIsRunning
       ' win.mMenuCommand = NULL
        while PeekMessage    win.mMSG, NULL, 0, 0, PM_REMOVE
            TranslateMessage win.mMSG
            DispatchMessage  win.mMSG
        wend
        input()
    wend
end sub



And test code:
Code: [Select]
#include "micro.inc"

MakeWindow (800, 600, "Simple Window")

MessageLoop()



sub input()
    select &wm
        case 0
            exit sub
     
        case WM_DESTROY
            PostQuitMessage 0
    end select
end sub

Looks like search in the dark.... ::)
Title: Re: callback function vrs virtual method in a class
Post by: Peter on July 29, 2012, 07:15:35 AM
Hi kent and Aurel,

You mean this here.
Start Test Window.o2bas and tell me, is this Okay?

X
Title: Re: callback function vrs virtual method in a class
Post by: Aurel on July 29, 2012, 09:44:59 AM
Hi Peter...
It work but not as properly because processor is constantly on 100%.
I think that your way can be on right track...hmmm
KeyEvent work but nothing else i mean intercepting other messages.
Thank you man  :)
Title: Re: callback function vrs virtual method in a class
Post by: kryton9 on July 29, 2012, 10:42:35 AM
I am glad you figured out how to make it run Peter. Thanks.
It does eat up cpu all the time on my netbook also, but runs smoothly.
Title: Re: callback function vrs virtual method in a class
Post by: Aurel on July 29, 2012, 12:50:33 PM
Kent...
I hope that you know that is not good using 100% of processor.
Problem is in two loops inside fn MessageLoop().
I have search on net and found Euphoria way with long include file.
BUT because Euphoria is interpreter all functions depend on reading source file
from disk... :-\

Interesting thing is that she use ID for each window,and ID for callback...
Code is a very complicated.... >:(
Title: Re: callback function vrs virtual method in a class
Post by: kryton9 on July 29, 2012, 02:14:19 PM
Aurel, you pointed out about the cpu being eaten up and I did some tests, both on my netbook(weak) and notebook(strong).

I ran code from Peter's demos ufo3. My GLWindow demo and compared it with a visual studio 6 MFC window with message handling.
There is something wrong in oxygen with the windows making and handling messages. Because Peter is using his own win.h and on mine I am using either winmin.inc by Charles and in other tests the includes from VB6 translated.

My netbook runs at around 20% cpu usage with nothing. My notebook at cpu 0% with nothing running.
With MFC program, my notebook cpu was still at 0%. But both my GLWindow and Peters UFO3 take up my notebook cpu to 12%
On my netbook, it varies all the time from 70% to 100%.

Something in the headers or oxygen is definitely effecting cpu usage more than it should.
Title: Re: callback function vrs virtual method in a class
Post by: Aurel on July 29, 2012, 02:17:56 PM
Uff after some tweaking i decide to remove PeekMessage and replace them with GetMessage.
Yeah ..look like interpreted stuff... ::)
window.inc
Code: [Select]
'window.inc by Kent Sarikaya
'Last Modified:
'2012.07.27 KS

'myWin variables have a "m" prefix for them
'Parameters/Arguements have an "a" prefix for arguement

include "MinWin.inc"

declare sub OnClose(sys hwnd)

type tWindow
        sys mTop, mLeft, mWidth, mHeight
        string mCaption, mClassName
        sys mMenuCommand
        sys mMouseX, mMouseY, mMouseButton, mKeydown
        sys mChangeSize, mIsActive, mIsRunning

        sys mKeys[256]

        sys  mhInstance, mhDC, mhWnd
        RECT mWindowRect
        MSG  mMSG
        sys  mExStyle, mStyle
end type
dim win as tWindow
 

function WindowProc(sys hwnd, uMsg, wParam, lParam ) as sys callback
    select uMsg
       'onClose------------
        case WM_DESTROY
IF hwnd<>0
          OnClose(hwnd)
END IF
        return 0
       '-----------------   

    end select
'default.............................................
    return DefWindowProc hWnd, uMsg, wParam, lParam
'....................................................
end function

function MakeWindow(sys aWidth, aHeight, string aCaption) as sys
    win.mWidth     = aWidth
    win.mHeight    = aHeight
    win.mLeft      = 0
    win.mTop       = 0
    win.mCaption   = 0 'aCaption
    win.mClassName = strptr "OxygenWindow"

    win.mWindowRect.left   = win.mLeft
    win.mWindowRect.right  = win.mWidth
    win.mWindowRect.top    = win.mTop
    win.mWindowRect.bottom = win.mHeight

    win.mhInstance  = GetModuleHandle 0
    '
    WNDCLASS winClass
    winClass.style = CS_HREDRAW | CS_VREDRAW
    winClass.lpfnWndProc   = @WindowProc
    winClass.cbClsExtra    = 0
    winClass.cbWndExtra    = 0
    winClass.hInstance     = win.mhInstance
    winClass.hIcon         = LoadIcon 0, IDI_WINLOGO
    winClass.hCursor       = LoadCursor 0, IDC_ARROW
    winClass.hbrBackground = GetStockObject WHITE_BRUSH 'NULL
    winClass.lpszMenuName  = 0
    winClass.lpszClassName = StrPtr "win"
   
    RegisterClass &winClass

    win.mStyle = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX
    win.mWindowRect <= 0, 0, win.mWidth, win.mHeight
    win.mhWnd = CreateWindowEx 0,"win"," Peter",WS_OVERLAPPEDWINDOW,0,0,640,480,0,0,win.mhInstance,0
   
    win.mIsActive  = 1
    win.mIsRunning = 1
   
    ShowWindow win.mhWnd, SW_SHOW
    UpdateWindow win.mhWnd
    return win.mhWnd
end function

sub MessageLoop()
    MSG wm
   'while win.mIsRunning
        'win.mMenuCommand = 0
if @wm >  0
'input()
        while GetMessage(&wm, 0, 0, 0)<>0           
    TranslateMessage &wm
            DispatchMessage  &wm       
        wend
     
    end if
end sub

function GetWidth() as sys
    return win.mWidth
end function


function GetHeight() as sys
    return win.mHeight
end function


function KeyEvent() as sys 'read once and clear
    return win.mKeydown
    win.mKeydown = 0      '<-- That is nonsens here!
end function


function MenuDown( WORD ID ) as sys
    if win.mMenuCommand = ID then return 1
end function


function KeyDown( sys ucKey ) as sys
    if win.mKeys[ucKey] then return 1
end function


function KeyUp( sys ucKey ) as sys
    if win.mKeys[ucKey] = 0 then return 1
end function


function MouseDown( sys button ) as sys
    if win.mMouseButton and button then return 1
end function


function MouseUp( sys button ) as sys
    if ( win.mMouseButton xor -1 ) and button then return 1
end function


And here is test...
Code: [Select]
'test window.o2bas
'by Kent Sarikaya 2012 July
'modifyed by Peter Wirlauber
'and modifyed by Aurel

include "window.inc"
! Beep Lib "kernel32.dll" (sys dwFreq, dwDuration) As sys
sys win
win = MakeWindow 800, 600, "Test OnClose()"

MessageLoop()

SUB OnClose(win)
Beep 1200,100
print "Thanks..."   
Return PostQuitMessage 0
END SUB
Title: Re: callback function vrs virtual method in a class
Post by: Aurel on July 29, 2012, 02:28:36 PM
Kent
I am not sure that something is wrong with oxygen inside but can be(problem with wndsclassex ).
But from my expirience with awinh.inc most thing work as espected ( i mean in a C-way).
When you test upper code you will see that work ok.
By the way Peter way is excellent for GDI examples and games but not very well for GUI stuff.
Strange thing is in GL examples who are not work very well on mine computers .
Another point i have found somwhere on GDI - C examples .
Messsage loop is written in a different way ,i must found where i put all this examples  ::)

Hmm i will try incorprate some of this into awinh.inc or in micro.inc
just to look how respond.

 
Title: Re: callback function vrs virtual method in a class
Post by: kryton9 on July 29, 2012, 02:44:22 PM
For code that draws or renders, I see that we need to put that into the ON_IDLE section. I think that is what is eating up cpu.
Title: Re: callback function vrs virtual method in a class
Post by: Aurel on July 29, 2012, 04:10:47 PM
Hmm problematic thing in this way is that because in test code
subroutine event must be written ,if is event not written linker tell
function not defined...
Title: This is it, I think.... :)
Post by: kryton9 on July 29, 2012, 07:33:11 PM
Ok this version is lean, mean and clean and setup to handle anything you want Aurel.

X
Title: Re: callback function vrs virtual method in a class
Post by: Aurel on July 30, 2012, 01:04:32 PM
Quote
Ok this version is lean, mean and clean and setup to handle anything you want Aurel.

Hey..is this some kind of joke ::)
Nothing is captured here ,infact it is almost same method (C-way) which i already use Kent.
Did you forget awinh.inc.
insde is :
Code: [Select]
Function WndProc(byval hWnd as long,byval wMsg as long, byval wParam as long,byval lparam as long) as long callback

SELECT wMsg
'----------------------------

CASE WM_DESTROY
PostQuitMessage 0
'------------------------------------------
case WM_SIZE
GetSize(win,x,y,w,h)
MoveWindow(richedit1,160,80,(w-rw/2)+64,(h-56)-32 ,1)
MoveWindow(edit1,160,54,(w-rw/2)+64,23 ,1)
'-------------------------------------------------------------
CASE WM_COMMAND
controlID = LoWord(wParam) 'get control ID
notifyCode = HiWord(wParam) 'get notification message

Select controlID
  CASE b0ID
If notifycode=0  
SendMessage richedit1,WM_SETTEXT,0,""
End If
  CASE b1ID
If notifycode=0  
doOpen()
End If
  CASE b2ID
    If notifycode=0
UpdateWindow(win)
   SetControlText()
End If
 CASE b3ID
If notifycode=0
LoadFromFile()
End If
 CASE b4ID
If notifycode=0
GetLineCount()
End If
 CASE b5ID
If notifycode=0
GetCSize()
End If




End Select
'-----------------------------------------------------
END SELECT



So ...where is this improvement...oh..my.
Only thing which you cover is message loop,right?
This is not solution at all.
Peter comes closer with his example but have problem with declered sub which must be
added if is added in message selection.

I don't know why Charles don't respond yet with his OOP example  ???
Title: Re: callback function vrs virtual method in a class
Post by: Charles Pegge on July 30, 2012, 02:10:08 PM
There is Kent's GlWindow example. This is fully OOPified and uses callbacks  for rendering and keyboard functions. As you can see, the architecture is not trivial.

projectsB/glWindow

If you want simplicity, I would stay with the standard wndproc.

Charles
Title: Re: callback function vrs virtual method in a class
Post by: Aurel on July 30, 2012, 02:16:18 PM
Yes Charles i know for that GLWindow example and i understand that architecture
is not trivial.
I don't want to force OOP over standard things.
I want simplicity of course but must there be way to simplify things like in other compilers,right?
If all this methods fail i will give up defenately from all this mumbo-jumbo.
Just another example from GUI-oop zip from fb:
Code: [Select]
function TForm.WindowProc(hDlg as hwnd,Msg as uint,wParam as wparam,lParam as lparam) as integer
    dim as TWinControl ptr WinControl = iif(CreationData, CreationData, GetWinControl(hDlg))
    dim as TMessage message = type(hDlg,Msg,wParam,lParam,0,WinControl)
    if WinControl then
       WinControl->ProcessMessage(message)
       cast(TForm ptr,WinControl)->ProcessMessage(message)
       return message.result
    end if
    return message.result
end function

function TForm.ClientWindowProc(hDlg as hwnd,Msg as uint,wParam as wparam,lParam as lparam) as integer
    dim as TWinControl ptr WinControl = iif(CreationData, CreationData, GetWinControl(hDlg))
    dim as TMessage message = type(hDlg,Msg,wParam,lParam,0,WinControl)
    if WinControl then
       WinControl->ProcessMessage(message)
       dim as WndClassEx wc
       wc.cbSize = sizeof(wc)
       if GetClassInfoEx(0,"MDIClIENT",@wc) then
          message.result = CallWindowProc(wc.lpfnWndProc,message.Handle,message.Msg,message.wParam,message.lParam)
       end if
       return message.result
    end if
    return message.result
end function
Title: Re: callback function vrs virtual method in a class
Post by: Charles Pegge on July 30, 2012, 03:09:59 PM

Looking at that sort of code gives me a migraine. Can you tell me what it is supposed to do,  :)

The main purpose of OOP is to bring order, simplicity and clarity to a program. When you have a number of similar but independent components in a program then OOP is a very good solution.

I would generally start out with straight procedural coding then bunch things into classes only to make the code shorter or simpler.

Charles
Title: Re: callback function vrs virtual method in a class
Post by: Aurel on July 30, 2012, 10:05:39 PM
Quote
Can you tell me what it is supposed to do

This two functions are part of GUI-oop written in free basic by eodor i think.
I think that im constantly miss one api function CallWindowProc().

Ha migrene...yeah i already have one big with all this to ;D
Title: Re: callback function vrs virtual method in a class
Post by: Aurel on July 31, 2012, 05:52:47 AM
I have found one interesting example from VB6 site.
Compiler refuse to register class ,what i do wrong ::)
Code: [Select]
'This project needs one form
' Also set StartupObject to 'Sub Main'
' original from VB6
Type WNDCLASS
style As Long
lpfnwndproc As Long
cbClsextra As Long
cbWndExtra2 As Long
hInstance As Long
hIcon As Long
hCursor As Long
hbrBackground As Long
lpszMenuName As String
lpszClassName As String
End Type

Type POINTAPI
x As Long
y As Long
End Type

Type Msg
hWnd As Long
message As Long
wParam As Long
lParam As Long
time As Long
pt As POINTAPI
End Type

' Class styles
Public Const CS_VREDRAW = &H1
Public Const CS_HREDRAW = &H2
Public Const CS_KEYCVTWINDOW = &H4
Public Const CS_DBLCLKS = &H8
Public Const CS_OWNDC = &H20
Public Const CS_CLASSDC = &H40
Public Const CS_PARENTDC = &H80
Public Const CS_NOKEYCVT = &H100
Public Const CS_NOCLOSE = &H200
Public Const CS_SAVEBITS = &H800
Public Const CS_BYTEALIGNCLIENT = &H1000
Public Const CS_BYTEALIGNWINDOW = &H2000
Public Const CS_PUBLICCLASS = &H4000
' Window styles
Public Const WS_OVERLAPPED = &H0&
Public Const WS_POPUP = &H80000000
Public Const WS_CHILD = &H40000000
Public Const WS_MINIMIZE = &H20000000
Public Const WS_VISIBLE = &H10000000
Public Const WS_DISABLED = &H8000000
Public Const WS_CLIPSIBLINGS = &H4000000
Public Const WS_CLIPCHILDREN = &H2000000
Public Const WS_MAXIMIZE = &H1000000
Public Const WS_CAPTION = &HC00000 ' WS_BORDER Or WS_DLGFRAME
Public Const WS_BORDER = &H800000
Public Const WS_DLGFRAME = &H400000
Public Const WS_VSCROLL = &H200000
Public Const WS_HSCROLL = &H100000
Public Const WS_SYSMENU = &H80000
Public Const WS_THICKFRAME = &H40000
Public Const WS_GROUP = &H20000
Public Const WS_TABSTOP = &H10000
Public Const WS_MINIMIZEBOX = &H20000
Public Const WS_MAXIMIZEBOX = &H10000
Public Const WS_TILED = WS_OVERLAPPED
Public Const WS_ICONIC = WS_MINIMIZE
Public Const WS_SIZEBOX = WS_THICKFRAME
Public Const WS_OVERLAPPEDWINDOW = (WS_OVERLAPPED Or WS_CAPTION Or WS_SYSMENU Or WS_THICKFRAME Or WS_MINIMIZEBOX Or WS_MAXIMIZEBOX)
Public Const WS_TILEDWINDOW = WS_OVERLAPPEDWINDOW
Public Const WS_POPUPWINDOW = (WS_POPUP Or WS_BORDER Or WS_SYSMENU)
Public Const WS_CHILDWINDOW = (WS_CHILD)
' ExWindowStyles
Public Const WS_EX_DLGMODALFRAME = &H1&
Public Const WS_EX_NOPARENTNOTIFY = &H4&
Public Const WS_EX_TOPMOST = &H8&
Public Const WS_EX_ACCEPTFILES = &H10&
Public Const WS_EX_TRANSPARENT = &H20&
' Color constants
Public Const COLOR_SCROLLBAR = 0
Public Const COLOR_BACKGROUND = 1
Public Const COLOR_ACTIVECAPTION = 2
Public Const COLOR_INACTIVECAPTION = 3
Public Const COLOR_MENU = 4
Public Const COLOR_WINDOW = 5
Public Const COLOR_WINDOWFRAME = 6
Public Const COLOR_MENUTEXT = 7
Public Const COLOR_WINDOWTEXT = 8
Public Const COLOR_CAPTIONTEXT = 9
Public Const COLOR_ACTIVEBORDER = 10
Public Const COLOR_INACTIVEBORDER = 11
Public Const COLOR_APPWORKSPACE = 12
Public Const COLOR_HIGHLIGHT = 13
Public Const COLOR_HIGHLIGHTTEXT = 14
Public Const COLOR_BTNFACE = 15
Public Const COLOR_BTNSHADOW = 16
Public Const COLOR_GRAYTEXT = 17
Public Const COLOR_BTNTEXT = 18
Public Const COLOR_INACTIVECAPTIONTEXT = 19
Public Const COLOR_BTNHIGHLIGHT = 20
' Window messages
Public Const WM_NULL = &H0
Public Const WM_CREATE = &H1
Public Const WM_DESTROY = &H2
Public Const WM_MOVE = &H3
Public Const WM_SIZE = &H5
' ShowWindow commands
Public Const SW_HIDE = 0
Public Const SW_SHOWNORMAL = 1
Public Const SW_NORMAL = 1
Public Const SW_SHOWMINIMIZED = 2
Public Const SW_SHOWMAXIMIZED = 3
Public Const SW_MAXIMIZE = 3
Public Const SW_SHOWNOACTIVATE = 4
Public Const SW_SHOW = 5
Public Const SW_MINIMIZE = 6
Public Const SW_SHOWMINNOACTIVE = 7
Public Const SW_SHOWNA = 8
Public Const SW_RESTORE = 9
Public Const SW_SHOWDEFAULT = 10
Public Const SW_MAX = 10
' Standard ID's of cursors
Public Const IDC_ARROW = 32512&
Public Const IDC_IBEAM = 32513&
Public Const IDC_WAIT = 32514&
Public Const IDC_CROSS = 32515&
Public Const IDC_UPARROW = 32516&
Public Const IDC_SIZE = 32640&
Public Const IDC_ICON = 32641&
Public Const IDC_SIZENWSE = 32642&
Public Const IDC_SIZENESW = 32643&
Public Const IDC_SIZEWE = 32644&
Public Const IDC_SIZENS = 32645&
Public Const IDC_SIZEALL = 32646&
Public Const IDC_NO = 32648&
Public Const IDC_APPSTARTING = 32650&
Public Const GWL_WNDPROC = -4

'---- Declarations
Declare Function RegisterClass Lib "user32" Alias "RegisterClassA" (wc As WNDCLASS) As Long
Declare Function UnregisterClass Lib "user32" Alias "UnregisterClassA" (ByVal lpClassName As String, ByVal hInstance As Long) As Long
Declare Function CreateWindowEx Lib "user32" Alias "CreateWindowExA" (ByVal dwExStyle As Long, ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, lpParam As Any) As Long
Declare Function DefWindowProc Lib "user32" Alias "DefWindowProcA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Declare Sub PostQuitMessage Lib "user32" (ByVal nExitCode As Long)
Declare Function GetMessage Lib "user32" Alias "GetMessageA" (lpMsg As Msg, ByVal hWnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long) As Long
Declare Function TranslateMessage Lib "user32" (lpMsg As Msg) As Long
Declare Function DispatchMessage Lib "user32" Alias "DispatchMessageA" (lpMsg As Msg) As Long
Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
Declare Function LoadCursor Lib "user32" Alias "LoadCursorA" (ByVal hInstance As Long, ByVal lpCursorName As Any) As Long
Declare Function DefMDIChildProc Lib "user32" Alias "DefMDIChildProcA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
' Define information of the window (pointed to by hWnd)
Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Dim wndcls As WNDCLASS

'look forward
#lookahead

Dim hwnd2 As Long, hwnd3 As Long, old_proc As Long, new_proc As Long
Public Sub Main()
Dim lngTemp As Long
' Register class
If MyRegisterClass Then
' Window created?
If MyCreateWindow Then
' Change the button's procedures
' Point to new address
new_proc = GetMyWndProc(@ButtonProc)
old_proc = SetWindowLong(hwnd2, GWL_WNDPROC, new_proc)
' Message loop
MyMessageLoop
End If
' Unregister Class
MyUnregisterClass
End If
End Sub

Function MyRegisterClass()as bool
' WNDCLASS-structure
'Dim wndcls As WNDCLASS  ' original code
wndcls.style = CS_HREDRAW | CS_VREDRAW
wndcls.lpfnwndproc = GetMyWndProc(@MyWndProc)
wndcls.cbClsextra = 0
wndcls.cbWndExtra2 = 0
wndcls.hInstance = App.hInstance
wndcls.hIcon = 0
wndcls.hCursor = LoadCursor(0, IDC_ARROW)
wndcls.hbrBackground = COLOR_WINDOW
wndcls.lpszMenuName = 0
wndcls.lpszClassName = "myWindowClass"
' Register class
MyRegisterClass = RegisterClass(&wndcls)
Function = MyRegisterClass
End Function

Sub MyUnregisterClass()
UnregisterClass "myWindowClass", App.hInstance
End Sub

Function MyCreateWindow() As Bool
Dim hWnd As Long
' Create the window
hWnd = CreateWindowEx(0, "myWindowClass", "My Window", WS_OVERLAPPEDWINDOW, 0, 0, 400, 300, 0, 0, App.hInstance, ByVal 0&)
' The Button and Textbox are child windows
hwnd2 = CreateWindowEx(0, "Button", "My button", WS_CHILD, 50, 55, 100, 25, hWnd, 0, App.hInstance,0)
hwnd3 = CreateWindowEx(0, "edit", "My textbox", WS_CHILD, 50, 25, 100, 25, hWnd, 0, App.hInstance, 0)
If hWnd <> 0 Then ShowWindow hWnd, SW_SHOWNORMAL
' Show them
ShowWindow hwnd2, SW_SHOWNORMAL
ShowWindow hwnd3, SW_SHOWNORMAL
' Go back
MyCreateWindow = (hWnd <> 0)
End Function

Function MyWndProc(ByVal hWnd As Long, ByVal message As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Select Case message
Case WM_DESTROY
' Destroy window
PostQuitMessage (0)
End Select
' calls the default window procedure
MyWndProc = DefWindowProc(hWnd, message, wParam, lParam)
End Function

Function GetMyWndProc(ByVal lWndProc As Long) As Long
GetMyWndProc = lWndProc
End Function

Sub MyMessageLoop()
Dim aMsg As Msg
Do While GetMessage(aMsg, 0, 0, 0)
DispatchMessage aMsg
Loop
End Sub

Function ButtonProc(ByVal hWnd As Long, ByVal message As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim x As Integer
If (message = 533) Then
x = MsgBox("You clicked on the button", vbOKOnly)
End If
' calls the window procedure
ButtonProc = CallWindowProc(old_proc, hWnd, message, wParam, lParam)
End Function