Code Addendum 02 - gforth: OpenGL Graphics Lesson 05

Code Addendum 02 - gforth: OpenGL Graphics Lesson 05

Post by Tim Trusse » Fri, 09 Jul 2010 00:48:43


===[ Code Addendum 02 ]============================================
\ gforth: OpenGL Graphics Lesson 05
\ ===================================================================
\ File: opengllib-1.05.fs
\ Author: Jeff Molofee
\ Linux Version: Ti Leggett
\ gForth Version: Timothy Trussell, 07/07/2010
\ Description: 3D Shapes
\ Forth System: gforth-0.7.0
\ Linux System: Ubuntu v10.04 LTS i386, kernel 2.6.31-23
\ C++ Compiler: gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)
\ ===================================================================
\ NeHe Productions
\ http://nehe.gamedev.net/
\ ===================================================================
\ OpenGL Tutorial Lesson 05
\ ===================================================================
\ This code was created by Jeff Molofee '99
\ (ported to Linux/SDL by Ti Leggett '01)
\ Visit Jeff at http://nehe.gamedev.net/
\ ===================================================================

require ~/.gforth/opengl-libs/mini-opengl-current.fs
require ~/.gforth/opengl-libs/mini-sdl-1.00.fs
require ~/.gforth/opengl-libs/sdlkeysym.fs

\ ---[ Prototype Listing ]-------------------------------------------
\ : HandleKeyPress ( &event -- )
\ : InitGL ( -- boolean )
\ : ShutDown ( -- )
\ : Reset-FPS-Counter ( -- )
\ : Display-FPS ( -- )
\ : DrawGLScene ( -- boolean )
\ ------------------------------------------------[End Prototypes]---

\ ---[ Rotation Variables ]------------------------------------------

fvariable r-tri
fvariable r-quad

0e r-tri F!
0e r-quad F!

\ ---[ HandleKeyPress ]----------------------------------------------
\ function to handle key press events:
\ ESC exits the program
\ F1 toggles between fullscreen and windowed modes

: HandleKeyPress ( &event -- )
sdl-keyboard-event-keysym sdl-keysym-sym uw@
case
SDLK_ESCAPE of TRUE to opengl-exit-flag endof
SDLK_F1 of screen sdl-wm-togglefullscreen drop endof
endcase
;

\ ---[ InitGL ]------------------------------------------------------
\ general OpenGL initialization function

: InitGL ( -- boolean )
\ Enable smooth shading
GL_SMOOTH gl-shade-model
\ Set the background black
0e 0e 0e 0e gl-clear-color
\ Depth buffer setup
1e gl-clear-depth
\ Enable depth testing
GL_DEPTH_TEST gl-enable
\ Type of depth test to do
GL_LEQUAL gl-depth-func
\ Really nice perspective calculations
GL_PERSPECTIVE_CORRECTION_HINT GL_NICEST gl-hint
\ Return a good value
TRUE
;

\ ---[ ShutDown ]----------------------------------------------------
\ Close down the system gracefully ;-)

: ShutDown ( -- )
FALSE to opengl-exit-flag \ reset these flag for next time
sdl-quit \ close down the SDL systems
;

fvariable fps-seconds
fvariable fps-count
0 value fps-ticks
0 value fps-t0
0 value fps-frames
0 value fps-line

\ ---[ Reset-FPS-Counter ]-------------------------------------------

: Reset-FPS-Counter ( -- )
sdl-get-ticks to fps-t0
0 to fps-frames
;

\ ---[ Display-FPS ]-------------------------------------------------

: Display-FPS ( -- )
sdl-get-ticks to fps-ticks
fps-ticks fps-t0 - 1000 >= if
fps-ticks fps-t0 - S>F 1000e F/ fps-seco