Oxygen Basic

Information => Open Forum => Topic started by: kryton9 on June 15, 2011, 02:02:19 AM

Title: my simple glwindow class in c++ for conversion
Post by: kryton9 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.
Title: Re: my simple glwindow class in c++ for conversion
Post by: Charles Pegge 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
Title: Re: my simple glwindow class in c++ for conversion
Post by: Charles Pegge 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
Title: Re: my simple glwindow class in c++ for conversion
Post by: Charles Pegge 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
Title: Re: my simple glwindow class in c++ for conversion
Post by: Charles Pegge 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
Title: Re: my simple glwindow class in c++ for conversion
Post by: Charles Pegge on June 19, 2011, 04:23:10 AM

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

Charles
Title: Re: my simple glwindow class in c++ for conversion
Post by: Charles Pegge 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
Title: Re: my simple glwindow class in c++ for conversion
Post by: kryton9 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 :)
Title: Re: my simple glwindow class in c++ for conversion
Post by: Charles Pegge 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
Title: Re: my simple glwindow class in c++ for conversion
Post by: kryton9 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.
Title: Re: my simple glwindow class in c++ for conversion
Post by: Charles Pegge 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.  
Title: Re: my simple glwindow class in c++ for conversion
Post by: kryton9 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?
Title: Re: my simple glwindow class in c++ for conversion
Post by: Charles Pegge 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
Title: Re: my simple glwindow class in c++ for conversion
Post by: Charles Pegge on June 21, 2011, 03:14:35 PM
Kent,

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


Charles
Title: Re: my simple glwindow class in c++ for conversion
Post by: kryton9 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.
Title: Re: my simple glwindow class in c++ for conversion
Post by: Charles Pegge on June 21, 2011, 09:36:44 PM

Yes. I see this happens when more than one window is run at the same time. Something gets locked in at the OS level. PortViewer2 does not show this behaviour so we do have a comparative model to work with.

Charles
Title: Re: my simple glwindow class in c++ for conversion
Post by: kryton9 on June 21, 2011, 10:17:58 PM
I terminated all the services running. And when I ran it just once and exited, it still left it as a service. I didn't restart, will restart my computer and do a test one more time with only 1 run to verify, it isn't from the perviouse executions...

Ok, back after the restart and test. Yes it still leaves it running in services. Hope it helps track down the culprit :)
Title: Re: my simple glwindow class in c++ for conversion
Post by: kryton9 on June 21, 2011, 10:32:05 PM
I didn't want to end my night with a problem, so the amazing thing I noticed is your executable Charles is 4K less than my code generated with MingW32 with the optimize for size flag turned on. That really impresses me! Kudos to you!!!
Title: Re: my simple glwindow class in c++ for conversion
Post by: Charles Pegge on June 21, 2011, 11:57:26 PM

Solved it!

I added a WM_DESTROY and a PostQuitMessage, and also prevent callbacks during the destroy process.

There are 2 IsRunning modes for the HandleMessages: one with Callbacks (2) and without callbacks (1)

Charles
Title: Re: my simple glwindow class in c++ for conversion
Post by: kryton9 on June 22, 2011, 02:14:01 AM
Charles, I couldn't get it to compile. I might be missing some stuff from my Oxygen.

Also, I was thinking. Just name this GLWindowClass instead of zxGLWindowClass. I don't want to mix up zx stuff with your nice clean minimal core of oxygen. I can always derive my class from GLWindow.
I will go back and change my previous posts so that it won't confuse future users.

[attachment deleted by admin]
Title: Re: my simple glwindow class in c++ for conversion
Post by: Charles Pegge on June 22, 2011, 03:12:36 AM
It looks as though it cannot find minwin. It should map correctly when you put the zx folder inside the projects folder.

I am still refining the code and making a few more adjustments to Oxygen. The exercise is to make the test 100% OOP, and ensure that functions inside classes have direct access to all the static members.

Yes we can make it GLWindow. This project folder is going to be quite volatile.

Charles

Title: Re: my simple glwindow class in c++ for conversion
Post by: kryton9 on June 22, 2011, 08:44:59 PM
I don't think I have the latest minwin.inc. I couldn't find the download link for the "in progress version". I am still using the last latest alpha 35

Ok, figured out something that caused confusion. The attached photo will show. The link for downloads takes us to an old page.

these links also point to old files:
http://oxygenbasic.sourceforge.net/

http://sourceforge.net/projects/oxygenbasic/

[attachment deleted by admin]
Title: Re: my simple glwindow class in c++ for conversion
Post by: Charles Pegge on June 22, 2011, 11:22:14 PM

Kent. Currently I only post the In-progress version to the OxygenBasic website. And SourceForge may lag by one version.

http://www.oxygenbasic.org/downloads.htm

Charles
Title: This compiles fun, but won't run.
Post by: kryton9 on June 25, 2011, 12:08:05 AM
Charles,

I went through and cleaned up the code to the way I like it so I could use it. There are a few places where I made slight changes, mainly in functions/methods where you had it like:
funcName
(
     ...
    list of parameters
    ... )

I made them;
funcName(  ...
    list of parameters
    ... )

Anyways this code won't run, so make sure you save it in another folder somewhere.

My guess is the ()

I noticed in most cases you can get by without them, but then in others you must use them.
I can imagine how hard that must be to parse that stuff. But it would be nice to have it standardized, either use them all the time or all the time to eliminate confusion.

[attachment deleted by admin]
Title: Re: my simple glwindow class in c++ for conversion
Post by: Charles Pegge on June 25, 2011, 01:34:18 AM

Hi Kent,

Fixed your program.

No problem with the prototype syntax.

You need to use the ":=" when assigning/comparing. Example:

            if not PixelFormat := ChoosePixelFormat hDC, @pfd then


Also missing dim:

WNDCLASS winClass

Charles
Title: Re: my simple glwindow class in c++ for conversion
Post by: kryton9 on June 25, 2011, 01:20:47 PM
Thanks Charles, oh didn't know about := 
I saw it but thought you were just testing the parser and that = was still allowed.

Thanks again as I will use this code to build upon a lot.
Title: Re: my simple glwindow class in c++ for conversion
Post by: Charles Pegge on June 25, 2011, 07:12:31 PM

I made a few more changes in the projects/GLWindows version, while fine-tuning Oxygen.

There are two ways to create class static variables. The first is to use the word static. This adds them to the class static table (which also contains method addresses). This second is to use the word dim. Class dim variable are visible inside the class but never outside. But you can of course use methods to access any of these indirectly. I think this is going to be the only reliable way of accessing statics.


Charles

Title: Re: my simple glwindow class in c++ for conversion
Post by: kryton9 on June 25, 2011, 09:12:41 PM
Sounds good Charles, once it is tested and you feel good with it, will add that to the quick reference I am putting together.

Now I call upon the Oracle of Oxygen...
I cleaned up and tried to make the class file and test program as clean as possible. I went through it 3 times and as far as I can tell it should work.
I am getting an error.

I tried using () after GetWidth, but it still didn't work.
Title: Re: my simple glwindow class in c++ for conversion
Post by: kryton9 on June 25, 2011, 11:05:13 PM
I renamed the download and files in the previous post. This will represent the class well. glSimpleWindowClass
This is because it combines some thing to make it simple for quick gl tests. For instance in the final glWindowClass
font will be pulled out into its own class. Also a timer class will be added to the final advance class.

With the timer class in place, even when you minimize the program, it will animate in the minimized preview that Windows 7 provides :)
It will be cool.
Title: Re: my simple glwindow class in c++ for conversion
Post by: Charles Pegge on June 26, 2011, 03:08:38 AM
Hi Kent,

Lost a few essential brackets, and a few other problems.

Named parameters require brackets f(a=4,b=4)

Also when using functions in an expression like this: a=f()*b.

Full screen mode was irrecoverable and required a PC restart.

If you want to refactor I can feed the changes into the current code.

There is a styling program we can adapt for producing preferred code layout.

What are your preferences?

Charles
Title: Re: my simple glwindow class in c++ for conversion
Post by: kryton9 on June 26, 2011, 01:12:36 PM
Thanks, I wasn't sure when () were needed and when not. Having simple rules as below is very beneficial. I will add to my notes.

About styles, I see how you and Peter came up with your style because you guys do assembly programming and work in plain text editors.
In viewing code in just black and white with no color highlighting, your style makes sense and is easier to read.
I find that when you use assembly style with highlighted syntax it is very hard to read.

I think the indented, 4 spaces, with highlighted syntax looks nice and is easy to read. Since I was using c++ the last 2 years or more
I got used to naming things differently because of case matters. I do prefer programming in case doesn't matter languages like BASIC,
but have to get used to it again.

I would hold off on an established style till we do some more real world classes to see what we come up against. As you saw, I came up
with a different naming scheme for variables than what we started out with. Will need to see how it works with other classes.

I will work on glSimpleWindowClass more today to see if I can get it working with your new tips. I also found a nice sub you have
in minwin for deleting libraries, so I changed my code to use that. So no more freelibrary in the class code, I call your sub and noted it in the code.
Title: Re: my simple glwindow class in c++ for conversion
Post by: kryton9 on June 26, 2011, 03:19:01 PM
Charles, I couldn't get my latest to work.
So I went back to reply # 24 in this thread and got your version. It runs, but Fullscreen does not work now.

So I then went back to reply #18 and got that version and that one doesn't run properly at all.

So something in the in progress oxygen has changed to mess them up, at least that is what seems to be?
Title: Re: my simple glwindow class in c++ for conversion
Post by: Charles Pegge on June 26, 2011, 09:25:09 PM

Hi Kent,

The full screen problem is fixed on this line
sc.dmSize       = sizeof sc

I assigned the wrong variable!

I would like to localise a few more variables in the WinCreate method, taking them out of the shared static space.

This also gets me thinking about refactoring tools. So we have a means of changing names safely as well as automating code layout adjustments.

I will post another update very shortly and let you know.

Charles
Title: Re: my simple glwindow class in c++ for conversion
Post by: kryton9 on June 26, 2011, 09:30:36 PM
I will wait for you to refactor things the way you want. That is the good thing about doing things like this is that it helps test out existing coding and framework and gives one better ideas about how to pull things out. Just like in doing glSimpleWindow, it is obvious that Font should be a separate class. As Timer will be. I am working on a timer class now. I got it written out but not compiling. I will post what I have before going to sleep, but I will be up for a few more hours.
Title: Re: my simple glwindow class in c++ for conversion
Post by: Aurel on October 18, 2012, 11:59:08 AM
I just peek into this topic and found some points
about how create multiple windows - am i right about this ?
or is missunderstand some things ..im not sure.

Similar thing i trying to do with standard windows ( not using openGL) but
i constantly stuck... >:(
So is there finished exemple about this thematic?
There's not matter if is openGL ..and OOP way
I just want to know how do this things and how stuff works...

Aurel

PS. maybe is this only way to minimize window coding and encapsulate wndProc with
just ordinary function or sub... :-\