Author Topic: openGL Class example  (Read 6562 times)

0 Members and 1 Guest are viewing this topic.

Frankolinox

  • Guest
openGL Class example
« on: March 12, 2013, 01:38:53 AM »
a) here's a little "openGL Class" example with a pyramid and a grid.

b) my question:
is there any example for a "cWindows" class example (like Jose Roca did it for powerbasic) for simple "helloWin" sdk win api?

a) example code:
Code: [Select]
'updated 6/18/2011
'08:47 22/06/2011 CP
'2011.06.30 00:55 KS
'2013.03.11 09:30 Frankolino :-)

  '$ FileName "t.exe"
  'include "..\..\inc\RTL32.inc"
  'include "..\..\inc\RTL64.inc"


'When you see a method that starts with Create
'that means you must also at the end call an
'appropriate method that starts with Destroy

'Parameters/Arguements have an "a" prefix standing for arguement
'
'OxygenBasic Folder "projectsB"/"GLWINDOW"

#include "cGLWindow3.inc"


class ctest

    dim cGLWindow win
    static sys    lucy  'TTFont
    static sys    funny 'TTFont
    dim cTimerLowRes  t
    dim as sys i,j,lList,lPos,lSize

    method main()
    '============

        win.Constructor(aInputProc = @Input, aRenderProc = @Render)
        win.Create ( aWidth = 800, aHeight = 600 )'( aWidth = 1024, aHeight = 768 )
        lucy  = win.CreateTTFont "Lucida Console", 16
        funny = win.CreateTTFont "Comic Sans MS" , 12
        t.Constructor
        win.MsgLoop
        win.Destructor
    end method

    sub Render() callback
        static float rotation = 0
        glClearColor 0, 0, 0, 0
        glClear GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
        glLoadIdentity
        glPushMatrix
            glTranslatef 0, 0, -5
            glRotatef rotation, 0, 1, 0
            rotation+=.5
            if  rotation  > 360 then
                rotation -= 360
            end if
            'glBegin GL_TRIANGLES
            '    glColor3f 1, 0, 0   glVertex2f     0,  1
            '    glColor3f 0, 1, 0   glVertex2f  0.87, -0.5
            '    glColor3f 0, 0, 1   glVertex2f -0.87, -0.5
            'glEnd
            glbegin GL_TRIANGLE_FAN
                glColor3f   0, 128, 255 : glVertex3f  0.0,  1.0,  0.0
                glColor3f 255,   0,   0 : glVertex3f -1.0, -1.0,  1.0
                glColor3f 255, 255, 255 : glVertex3f  1.0, -1.0,  1.0
                glColor3f   0,   0, 255 : glVertex3f  1.0, -1.0, -1.0
                glColor3f   0, 255,   0 : glVertex3f -1.0, -1.0, -1.0
                glColor3f 255,   0,   0 : glVertex3f -1.0, -1.0,  1.0
            glend
  
        GLBegin GL_LINES          
        GLColor3f 0,255,0        
        
        'For i = -lSize To lSize
        For i = -10 To 10
          'For j = -lSize To lSize
          For j = -10 To 10
            'glVertex3f -lSize, lPos,   j    
            glVertex3f -10, -1,   j
            'glVertex3f  lSize, lPos,   j    
            glVertex3f  10, -1,   j    
            
            'glVertex3f  i,  lPos, -lSize    
            glVertex3f  i,  -1, -10    
            'glVertex3f  i,  lPos,  lSize    
            glVertex3f  i,  -1,  10    

          Next
        Next

      GLEnd  

        glPopMatrix
        win.BeginTextMode
            win.SetTTFont lucy
            glColor3f 0, 255, 0
            win.Print 0.1 * Win.GetWidth, 0.75 * win.GetHeight, "Just Testing the GLWindow Class"
            win.SetTTFont funny
            glColor3f 255, 200, 0
            string p=str(t.GetFPS(),2)
            win.Print 0.1 * Win.GetWidth,  .25 * Win.GetHeight, "Comic Sans MS   fps: " p
        win.EndTextMode
        win.Flip
    end sub


    function Shutdown() as bool
        win.Destroy()
        win.DestroyTTFont lucy
        win.DestroyTTFont funny
        return 1
    end function


    sub Input() callback
        select case win.KeyEvent 'read once and clear
            case 0
                exit sub

            case VK_ESCAPE
                Shutdown

            case "Q"
                Shutdown

            case VK_RETURN
                win.Reset
                win.DestroyTTFont lucy
                win.DestroyTTFont funny
                win.ToggleFullScreen
                win.Remake
                lucy  = win.CreateTTFont "Lucida Console", 16
                funny = win.CreateTTFont "Comic Sans MS" , 12
        end select
    end sub

    sub drawPyramid()
 
  glbegin GL_TRIANGLE_FAN
    glColor3f   0, 128, 255 : glVertex3f  0.0,  1.0,  0.0
    glColor3f 255,   0,   0 : glVertex3f -1.0, -1.0,  1.0
    glColor3f 255, 255, 255 : glVertex3f  1.0, -1.0,  1.0
    glColor3f   0,   0, 255 : glVertex3f  1.0, -1.0, -1.0
    glColor3f   0, 255,   0 : glVertex3f -1.0, -1.0, -1.0
    glColor3f 255,   0,   0 : glVertex3f -1.0, -1.0,  1.0
  glend
  
end sub

end class

ctest t
t.main



you need the #include "cGLWindow3.inc" file for it : OxygenBasic Folder "projectsB"/"GLWINDOW"

best regards, frank

X

Aurel

  • Guest
Re: openGL Class example
« Reply #1 on: March 12, 2013, 04:41:49 AM »
Frank
What is exactly this  cWindows class for PowerBasic  ?
Or do you try translate this class to Oxygen Basic code if is in include files ?

Charles Pegge

  • Guest
Re: openGL Class example
« Reply #2 on: March 12, 2013, 05:37:43 AM »

It uses Kent's GLWindow API in ProjectsB/GLWindow.

Nice demo Frank. Perhaps we can include it in the O2 package.

We have not heard from Kent for months. I trust he has not been kidnapped by the Greys :)

Charles

JRS

  • Guest
Re: openGL Class example
« Reply #3 on: March 12, 2013, 05:56:53 AM »
Quote
We have not heard from Kent for months. I trust he has not been kidnapped by the Greys

The same goes for AIR. (Armando) I haven't heard from him in a while and he didn't respond to my e-mail asking if he was okay. I'm concerned.


Aurel

  • Guest
Re: openGL Class example
« Reply #4 on: March 12, 2013, 08:54:29 AM »
Frank
I download Jose winapi_119
and found cWindow class.
Code: [Select]
' =====================================================================================
   ' Window creation
   ' =====================================================================================
   METHOD CreateWindow ( _
      BYVAL hParent AS DWORD, _             ' // Parent window handle
      BYVAL strTitle AS STRING, _           ' // Window caption
      BYVAL x AS LONG, _                    ' // Horizontal position
      BYVAL y AS LONG, _                    ' // Vertical position
      BYVAL nWidth AS LONG, _               ' // Window width
      BYVAL nHeight AS LONG, _              ' // Window height
      BYVAL dwStyle AS DWORD, _             ' // Window style(s)
      BYVAL dwExStyle AS DWORD, _           ' // Extended style
      BYVAL lpfnWndProc AS DWORD _          ' // Address of the callback function
      ) AS DWORD                            ' // Window handle

      ' // Exit if the window has already been created
      IF m_hwnd THEN EXIT METHOD
      ' // Centered window
'      IF x = 0 AND y = 0 THEN
'         x = (GetSystemMetrics(%SM_CXSCREEN) - nWidth) \ 2
'         y = (GetSystemMetrics(%SM_CYSCREEN) - nHeight) \ 2
'      END IF
      ' // Register a class
      m_wAtom = CWindow_RegisterClass(m_hInstance, lpfnWndProc)
      IF m_wAtom = 0 THEN EXIT METHOD
      ' // Create the window
      LOCAL dwClass AS DWORD
      dwClass = MAK(DWORD, m_wAtom, 0)
      IF BITS(LONG, dwStyle) = -1 THEN dwStyle = %WS_OVERLAPPEDWINDOW OR %WS_CLIPCHILDREN OR %WS_CLIPSIBLINGS
      IF BITS(LONG, dwExStyle) = -1 THEN dwExStyle = %WS_EX_CONTROLPARENT OR %WS_EX_WINDOWEDGE
      LOCAL tCreateParams AS CWindow_CREATEPARAMS
      tCreateParams.pWindow = OBJPTR(ME)
      m_hwnd = CreateWindowEx(dwExStyle, BYVAL dwClass, BYCOPY strTitle, dwStyle, _
               x * m_rx, y * m_ry, nWidth * m_rx, nHeight * m_ry, hParent, %NULL, m_hInstance, tCreateParams)
      IF m_hwnd = %NULL THEN EXIT METHOD
      ' // Store a pointer to the class
      SetClassLong m_hwnd, 0, OBJPTR(ME)
      ' // Retrieve the class name
      GetClassName(m_hwnd, m_szClassName, SIZEOF(m_szClassName))
      ' // Set the font
      SendMessage m_hwnd, %WM_SETFONT, m_hFont, %FALSE
      ' // Show the window
      IF (dwStyle AND %WS_MAXIMIZE) = %WS_MAXIMIZE THEN
         ShowWindow m_hwnd, %SW_MAXIMIZE
      ELSEIF (dwStyle AND %WS_MINIMIZE) = %WS_MINIMIZE THEN
         ShowWindow m_hwnd, %SW_MINIMIZE
      ELSE
         ShowWindow m_hwnd, %SW_SHOW
      END IF
      ' // Update the window
      UpdateWindow m_hwnd
      ' // Return the window handle
      METHOD = m_hwnd
   END METHOD

I can say that this looks very good and i think that can be translated to Oxygen with some
changes , first we must ask Charles ...what he think ?
« Last Edit: March 12, 2013, 10:25:14 AM by Aurel »

Frankolinox

  • Guest
Re: openGL Class example
« Reply #5 on: March 12, 2013, 10:09:05 AM »
thanks for feedback, all

I need a startpoint where to begin with sdk window class. I cannot convert all things from jose's "cWindow class" into oxygen, that's for sure. aurel, the cWindow include file is mucho,mucho more longer and hard stuff and very, very good! I will check current class example for oxygen. and if you have any example at hand, dear charles, send it at the board

my idea is to make a gui (sdk) plus openGL window in it. somewhere in oxygen folders there are two openGL examples with two or four openGL windows and text editor. perhaps that could be a begin.

but I think it's better to "merge" a) win api sdk class with b) openGL class. but for this win api sdk class I need an entry or a begin. peter makes his own window for his games and exercises, aurel (and myself) for scintilla editor and other people serve themselve with own wrapper, I don't know (a little joke).

@charles, of course you can use this openGL example for your folders.

@kent: ok, say hello kent! you're living and I hope you are fine :-)

best regards, frank
« Last Edit: March 12, 2013, 10:35:42 AM by Frankolinox »

Aurel

  • Guest
Re: openGL Class example
« Reply #6 on: March 12, 2013, 10:30:35 AM »
Frank
I think that we don't need whole cWindow include but yes as you say we need start point for
translating this great wrapper ,eh yes ..it is written in OOP way  >:(
but again i think that we have all commands in oxygen for translation (i think... :-\)
I will try do something ...

Charles Pegge

  • Guest
Re: openGL Class example
« Reply #7 on: March 12, 2013, 12:29:15 PM »

A main window does not make a very good Object. It has too many interconnections with the rest of the program, so you end up breaking lots of holes through the encapsulation. Child windows make better objects.

Aurel

  • Guest
Re: openGL Class example
« Reply #8 on: March 12, 2013, 01:41:43 PM »
Quote
A main window does not make a very good Object. It has too many interconnections with the rest of the program, so you end up breaking lots of holes through the encapsulation. Child windows make better objects.
Yes Charles you are probably right it looks to me to that is to many connected with other things
but that is a way how it is created :-\
I am thinking how would be create window with macro , what you think about that ?

Charles Pegge

  • Guest
Re: openGL Class example
« Reply #9 on: March 12, 2013, 08:53:21 PM »
You can use macros as an alternative to callback functions for timer,keyboard, mouse messages etc. The macros need to be defined before WndProc. They will allow access to all the WndProc variables.

Aurel

  • Guest
Re: openGL Class example
« Reply #10 on: March 13, 2013, 10:46:21 PM »
Charles
Do you can add small example of macro as event message?

Charles Pegge

  • Guest
Re: openGL Class example
« Reply #11 on: March 14, 2013, 06:01:32 AM »
Yes Aurel,

This is the classical a Windows WndProc, using macros to handle individual message types.

New Oxygen also attached, (fixing a glitch with comments immediately following a macro call)

Code: [Select]
  $ filename "t.exe"
  '#include "../../inc/RTL32.inc"
  '#include "../../inc/RTL64.inc"
  #include "../../inc/MinWin.inc"


  #lookahead ' for procedures


  macro EventKeyDown()
  ====================
  select wParam
  case 27 : SendMessage hwnd, WM_CLOSE, 0, 0 'ESCAPE
  end Select
  end macro

  macro EventLbuttonDown()
  ========================
  print "left button"
  end macro

  macro EventRbuttonDown()
  ========================
  print "right button"
  end macro

  macro EventMouseMove()
  ======================
  end macro

  macro EventTimer()
  ==================
  end macro



  ==========
  'MAIN CODE
  ==========
 
  dim cmdline as asciiz ptr, inst as sys
  &cmdline=GetCommandLine
  inst=GetModuleHandle 0
  '
  'WINDOWS START
  '=============
  '
  WinMain inst,0,cmdline,SW_NORMAL
  end




  '--------------------------------------------------------------------
  Function WinMain(sys inst, prevInst, asciiz*cmdline, sys show) as sys
  '====================================================================


  WndClass wc
  MSG      wm

  sys hwnd, wwd, wht, wtx, wty, tax

  wc.style = CS_HREDRAW or CS_VREDRAW
  wc.lpfnWndProc = @WndProc
  wc.cbClsExtra =0
  wc.cbWndExtra =0   
  wc.hInstance =inst
  wc.hIcon=LoadIcon 0, IDI_APPLICATION
  wc.hCursor=LoadCursor 0,IDC_ARROW
  wc.hbrBackground = GetStockObject WHITE_BRUSH
  wc.lpszMenuName =null
  wc.lpszClassName = strptr "Demo"

  RegisterClass (@wc)
 
  Wwd = 320 : Wht = 200
  Tax = GetSystemMetrics SM_CXSCREEN
  Wtx = (Tax - Wwd) /2
  Tax = GetSystemMetrics SM_CYSCREEN
  Wty = (Tax - Wht) /2
 
  hwnd = CreateWindowEx 0,wc.lpszClassName,"OXYGEN BASIC",WS_OVERLAPPEDWINDOW,Wtx,Wty,Wwd,Wht,0,0,inst,0
  ShowWindow hwnd,SW_SHOW
  UpdateWindow hwnd
  '
  sys bRet
  '
  do while bRet := GetMessage (@wm, 0, 0, 0)
    if bRet = -1 then
      'show an error message
    else
      TranslateMessage @wm
      DispatchMessage @wm
    end if
  wend

  End Function



  dim as rect crect 'for WndProc and TimerProc

  '--------------------------------------------------------------
  function WndProc ( hWnd, wMsg, wParam, lparam ) as sys callback
  '==============================================================

    static as sys hdc
    static as String txt
    static as PaintStruct Paintst
 
    '==========
    select wMsg
    '==========
       
      case WM_CREATE
      '=============

      GetClientRect  hWnd,&cRect

      case WM_DESTROY
      '===============
         
      PostQuitMessage 0
       
      case WM_PAINT
      '============


      'TEXT
      'http://msdn.microsoft.com/en-us/library/dd144821(v=VS.85).aspx

      'DRAWING AND PAINTING
      'http://msdn.microsoft.com/en-us/library/dd162760(v=VS.85).aspx

      GetClientRect  hWnd,&cRect
      hDC=BeginPaint hWnd,&Paintst
      'style
      '0x20 DT_SINGLELINE
      '0x04 DT_VCENTER
      '0x01 DT_CENTER
      '0x25

      SetBkColor   hdc,yellow
      SetTextColor hdc,red
      DrawText hDC,"Hello World!",-1,&cRect,0x25
      EndPaint hWnd,&Paintst
       
      case WM_KEYDOWN     : EventKeyDown()
      case WM_LBUTTONDOWN : EventLbuttonDown()
      case WM_RBUTTONDOWN : EventRbuttonDown()
      case WM_MOUSEMOVE   : EventMouseMove()
      case WM_TIMER       : EventTimer()

      case else           : function=DefWindowProc hWnd,wMsg,wParam,lParam
       
    end select

  end function ' WndProc


X

Aurel

  • Guest
Re: openGL Class example
« Reply #12 on: March 14, 2013, 10:30:15 AM »
Thanks Charles...
I know that work on this way because i use in awinh.inc
Code: [Select]
Macro Default
DefWindowProc hWnd,wMsg,wParam,lParam
End Macro

I mean in first place is there a way to put complete winproc inside macro command
or is there a way to put specific function inside macro ?
of course must be defined first...

kryton9

  • Guest
Re: openGL Class example
« Reply #13 on: June 22, 2013, 11:17:07 PM »

....We have not heard from Kent for months. I trust he has not been kidnapped by the Greys :)

Charles

I have been checking in time to time. I broke my right arm in a fall. My first broken bone. Being right handed, it was tough. I am almost all healed up now.
In the meantime, I had been continuing my c++ studies and wrote a complete pong game using SFML 2 libraries( I could play it with my left hand using the up down arrow keys ).

I used code::blocks and gcc compiler to see how cross platform it would be. After I developed and got it working in Windows. I setup OSX and Linux virtual machines.
I decided to try OSX first, figuring it would be harder. Well Code::Blocks under OSX is very buggy at best. No way I could get it compile. So I moved the code over to xcode
and hammering away looking at SFML tutorials for xcode based examples, got it to compile and run fine under OSX.

Even using a very nicely written library like SFML, doing cross platform development is not for me. I decided to focus on Windows only development.

So the big question Charles, is Oxygen at a point where I should develop and not worry about core changes in the near future? If you remember, as I was developing things before in Oxygen, I was also writing help files as I need them because of my bad memory, but you had warned me not to continue till things got locked down more. So I am waiting for the go ahead, Oxygen is such a great language. I would love to get back to working with it and developing in it.

Aurel

  • Guest
Re: openGL Class example
« Reply #14 on: June 23, 2013, 01:54:48 AM »
Hi Kent ... :)
Nice to see you again ;)