Author Topic: my simple glwindow class in c++ for conversion  (Read 10943 times)

0 Members and 1 Guest are viewing this topic.

kryton9

  • Guest
my simple glwindow class in c++ for conversion
« on: June 15, 2011, 02:02:19 AM »
I have edited this post and will do subsequent older posts in this thread to not confuse future users.
There was code listed here that we used as a basis for developing a GLWindow Class for Oxygen, it is not necessary anymore so removed.
« Last Edit: June 22, 2011, 02:21:50 AM by kryton9 »

Charles Pegge

  • Guest
Re: my simple glwindow class in c++ for conversion
« Reply #1 on: June 15, 2011, 10:14:30 PM »

Thanks Kent,

I am studying your code. One thing Oxygen does not support at present: method definitions being both inside and outside the class definition but I am investigating how this might be enabled.

Charles

Charles Pegge

  • Guest
Re: my simple glwindow class in c++ for conversion
« Reply #2 on: June 15, 2011, 10:51:23 PM »
I have translated quite a bit already (I find the best way to read code is to write it :) ). here is the glWindow class without the external methods:

Code: OxygenBasic
  1. '=============
  2. class GLWindow
  3. '=============
  4.  
  5. private
  6.         //the window's variables
  7.         sys hInstance, hWnd, hDC, hRC
  8.  
  9.         static zstring* className
  10.  
  11.         //font variables
  12.         sys Font
  13.  
  14.         method WindowProc( sys hWnd, Msg, wParam, lParam ) as sys
  15. public
  16.  
  17.         //the window's vars
  18.  
  19.         static int Width
  20.         static int Height
  21.         static int BPP; //bits per pixel
  22.         static word menuCommand
  23.  
  24.         //input related vars
  25.  
  26.         static int MouseX, MouseY
  27.         static int MouseButton
  28.         static int Keys[256]
  29.  
  30.         static sys ChangeSize
  31.         static sys Active
  32.  
  33.  
  34.         method Init( int X, Y, iWidth, iHeight, Bits, char* Title, word icon=0, menu=0 ) as int
  35.         method Shutdown()
  36.  
  37.         //font routines
  38.         method CreateTTFont( char* szFontName, int iSize )
  39.         method DestroyFont()
  40.         method Print( int X, Y, char* PrintString) '...
  41.        method BeginTextMode()
  42.         method EndTextMode()
  43.  
  44.         method BeginRendering()
  45.         method EndRendering()
  46.  
  47.         method HandleMessages() as sys
  48.  
  49.         method GetHInstance( void ) as sys {return hInstance }
  50.  
  51.         method GetHWnd() as sys {       return hWnd }
  52.  
  53.         method MenuDown( WORD ID ) as sys { if menuCommand==ID {return 1} }
  54.  
  55.         method KeyDown( unsigned char ucKey ) as sys { if Keys[ucKey] {return 1} }
  56.  
  57.         method KeyUp( unsigned char ucKey ) as sys { if not Keys[ucKey] {return 1} }
  58.  
  59.         method GetMouseX() as sys { return MouseX }
  60.  
  61.         method GetMouseY() as sys { return MouseY }
  62.  
  63.         method MouseDown( WORD button ) as sys { if MouseButton & button {return 1} }
  64.  
  65.         method MouseUp( WORD button ) as sys { if not MouseButton & button {return 1} }
  66.  
  67.         method IsActive() as sys { return Active }
  68.  
  69.         method Resize( int iWidth,iHeight )
  70.  
  71. end class
  72.  
  73.  
  74.  

Charles
« Last Edit: June 15, 2011, 10:53:32 PM by Charles Pegge »

Charles Pegge

  • Guest
Re: my simple glwindow class in c++ for conversion
« Reply #3 on: June 18, 2011, 12:02:48 AM »

Running an application with several independent windows is an interesting idea. To share data between the windows you can either use "shared memory" or use shared files - like running a database over a local area network.

Charles

Charles Pegge

  • Guest
Re: my simple glwindow class in c++ for conversion
« Reply #4 on: June 18, 2011, 11:11:49 PM »

Thanks Kent,

I'll get to work on it.

DEVMODE and its POINTL will go into minwin.

Charles

Charles Pegge

  • Guest
Re: my simple glwindow class in c++ for conversion
« Reply #5 on: June 19, 2011, 04:23:10 AM »

It's mostly adding new equates and a few functions to minwin.

Charles

Charles Pegge

  • Guest
Re: my simple glwindow class in c++ for conversion
« Reply #6 on: June 20, 2011, 02:10:46 AM »

Nearly there, Kent.

One thing I realised: you can't use a method as a callback. Not for a WndProc anyway. You will get stack corruption due the absence of a 'This' pointer.

My solution is to encapsulate the WndProc inside a a privat method like this

Code: OxygenBasic
  1.  
  2.      private
  3.  
  4.         method AddrWindowProc() as sys
  5.         '=============================
  6.        '
  7.        return @WindowProc
  8.         '
  9.            function WindowProc( sys hWnd, uMsg, wParam, lParam ) as sys callback
  10.             '====================================================================
  11.            zxGLWindow *this
  12.                 select uMsg
  13.                 case WM_SYSCOMMAND
  14.                     select wParam
  15.                         case SC_SCREENSAVE
  16.                         case SC_MONITORPOWER
  17.                     end select
  18.                    
  19.                 case WM_COMMAND
  20.                     this.MenuCommand = LOWORD wParam
  21.  
  22.                 case WM_KEYDOWN
  23.                     this.Keys[ wParam ] = 1
  24.  
  25.                 case WM_KEYUP
  26.                     this.Keys[ wParam ] = 0
  27.  
  28.                 case WM_MOUSEMOVE:
  29.                     this.MouseX = LOWORD lParam
  30.                     this.MouseY = HIWORD lParam
  31.                     this.MouseButton = wParam
  32.  
  33.                 case WM_CLOSE
  34.                     PostQuitMessage 0
  35.  
  36.                 case WM_DESTROY
  37.                     PostQuitMessage 0
  38.  
  39.                 case WM_SIZE
  40.                     this.Width = LOWORD lParam
  41.                     this.Height = HIWORD lParam
  42.                     this.ChangeSize = 1
  43.                 case else
  44.                     return DefWindowProc hWnd, uMsg, wParam, lParam
  45.                 end select
  46.             end function
  47.         end method
  48.  

Charles

kryton9

  • Guest
Re: my simple glwindow class in c++ for conversion
« Reply #7 on: June 20, 2011, 02:39:51 AM »
In my c++ version, I had to make it a static method, but still left the callback in there.
static LRESULT CALLBACK WindowProc( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam );

I also found this after reading your post and solution:
http://www.codeproject.com/KB/winsdk/wndproctoclassproc.aspx

Never would have thought of that. Can't wait to see your results and see it working :)

Charles Pegge

  • Guest
Re: my simple glwindow class in c++ for conversion
« Reply #8 on: June 20, 2011, 08:50:12 PM »

It's all working smoothly now, including flicker-free resizing, but I have 2 Oxygen bugs I wish to pursue.

Here is the compacted main program. I'm using callbacks so it is no longer necessary to expose the message loop.

Code: OxygenBasic
  1.  
  2.   'updated 6/18/2011
  3.  
  4.   '04:56 21/06/2011 CP
  5.  
  6.   #include "g.inc"
  7.  
  8.   'global vars
  9.  
  10.   '
  11.  zxGLWindow win
  12.   sys lucy 'TTFont
  13.  sys funny 'TTFont
  14.  
  15.   '
  16.  'main program
  17.  '
  18.  win.constructor
  19.   win.RenderCallBack @Render
  20.   win.InputCallBack @input
  21.   win.Create (iwidth=800, iheight=600)
  22.   lucy = win.CreateTTFont "Lucida Console", 16
  23.   funny = win.CreateTTFont "Comic Sans MS", 12
  24.   win.HandleMessages
  25.   win.destructor
  26.  
  27.   'end program
  28.  
  29.  
  30.  
  31.   '==========
  32.  ' functions
  33.  '==========
  34.  
  35.   sub Render() callback
  36.   '====================
  37.  
  38.     static float rotation = 0
  39.     glClearColor 0, 0, 0, 0
  40.         glClear GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
  41.         glLoadIdentity
  42.         glPushMatrix
  43.             glTranslatef 0, 0, -5
  44.             glRotatef rotation, 0, 1, 0
  45.             rotation+=.5
  46.             if rotation>360 then rotation-=360
  47.             glBegin GL_TRIANGLES
  48.             glColor3f 1, 0, 0 glVertex2f 0, 1
  49.             glColor3f 0, 1, 0 glVertex2f 0.87,  -0.5
  50.             glColor3f 0, 0, 1 glVertex2f -0.87, -0.5
  51.             glEnd
  52.         glPopMatrix
  53.         'render some text to the screen
  54.        win.BeginTextMode
  55.             'set the color for the text
  56.            win.Font = lucy
  57.             glColor3f 0, 255, 0
  58.             win.Print win.Width * 0.1, win.Height * 0.75, "Just Testing the GLWindow Class"
  59.             win.Font = funny
  60.             glColor3f 255, 200, 0
  61.             win.Print win.Width * 0.1, win.Height * 0.25, "Testing Comic Sans MS"
  62.         win.EndTextMode
  63.         glFlush
  64.         SwapBuffers win.hDC
  65.  
  66.   end sub
  67.  
  68.  
  69.   function Shutdown() as bool callback
  70.   '===================================
  71.         win.Destroy()
  72.         win.DestroyTTFont lucy
  73.         win.DestroyTTFont funny
  74.         return 1
  75.   end function
  76.  
  77.  
  78.   sub Input() callback
  79.   '===================
  80.    'exit the program
  81.    if win.KeyDown VK_ESCAPE then shutdown
  82.     'exit the program
  83.    if win.KeyDown 0x51 then shutdown
  84.  
  85.     if win.KeyDown VK_RETURN then
  86.         win.Keys[ VK_RETURN ] = 0
  87.         win.DestroyWin()
  88.         win.DestroyTTFont lucy
  89.         win.DestroyTTFont funny
  90.         win.Fullscreen = 1-win.Fullscreen ' bool op
  91.        win.CreateWin()
  92.         lucy = win.CreateTTFont "Lucida Console", 16
  93.         funny = win.CreateTTFont "Comic Sans MS", 12
  94.     end if
  95.  
  96.   end sub
  97.  
  98.  
  99.  

Charles

kryton9

  • Guest
Re: my simple glwindow class in c++ for conversion
« Reply #9 on: June 20, 2011, 09:19:12 PM »
I am glad you got it working Charles. I get confused about callbacks... Glut is criticized because it uses callbacks, but then in other cases callbacks are considered good. I guess when I research more about it, it will make more sense.

I can't run the code yet, but you said you were still making changes to Oxygen so I understand why. Thanks for your help...

I hope to develop zx both for c++ and also oxygen, so the tricky code to get all of this work will be great code sample for further translations hopefully.

Charles Pegge

  • Guest
Re: my simple glwindow class in c++ for conversion
« Reply #10 on: June 21, 2011, 01:18:02 AM »
Kent,

Your input and render routines make ideal call backs. They expose exactly what is needed up front. Having callbacks for lots of individual windows messages would be too much.

The function addresses are stored in sys variables. Then called when needed:

this expression checks for null before making the callback

if mycallback then call mycallback

Here is the the message loop

Code: OxygenBasic
  1.         method HandleMessages()
  2.         '======================
  3.          '
  4.          while IsRunning
  5.             if RenderFun then call RenderFun
  6.             MenuCommand = NULL
  7.             while PeekMessage @mesg, NULL, 0, 0, PM_REMOVE
  8.                 TranslateMessage @mesg
  9.                 DispatchMessage @mesg
  10.             wend
  11.             if InputFun then call InputFun
  12.           wend
  13.         end method
  14.  

kryton9

  • Guest
Re: my simple glwindow class in c++ for conversion
« Reply #11 on: June 21, 2011, 04:08:48 AM »
Oh I see, so your class is able to call routines declared and defined outside of the class without knowing its name.  That is powerful stuff.

And so if you over use it like apparently Glut does, this is no good because you are calling too many functions when direct code would be more efficient?

Charles Pegge

  • Guest
Re: my simple glwindow class in c++ for conversion
« Reply #12 on: June 21, 2011, 08:11:21 AM »

CallBacks give you plenty of flexibility. Perhaps the best known example is a Sort algorithm which uses a callback to compare two elements. The Sort works on an array of indexes and does not need to know anything about the data itself. The callback is used to decide which is the greater or lesser of two indexed data elements.

I've hardly looked at GLUT. It does not terminate properly in MS Windows - no one has fixed it, but there is openGlut and FreeGlut. I don't think those message callbacks give you significant coding advantage.

Charles

Charles Pegge

  • Guest
Re: my simple glwindow class in c++ for conversion
« Reply #13 on: June 21, 2011, 03:14:35 PM »
Kent,

I have included the code in the /projects folder in Oxygen.zip (in progress).


Charles
« Last Edit: June 22, 2011, 03:14:59 AM by Charles Pegge »

kryton9

  • Guest
Re: my simple glwindow class in c++ for conversion
« Reply #14 on: June 21, 2011, 08:14:26 PM »
Charles,  the zxGLWindow.exe  did not terminate properly and left copies running in task manager, even though the window closed and went away like it should. I just noticed it after my computer was running very slowly... so I checked Task Manager and that was it. I had many copies running under services.