Sei sulla pagina 1di 10

Ring Documentation, Release 1.5.

deltaMove = 0.5
on GLUT_KEY_DOWN
deltaMove = -0.5
off

func releaseKey

key = glutEventKey()

switch key
on GLUT_KEY_UP
deltaMove = 0
on GLUT_KEY_DOWN
deltaMove = 0
off

// -----------------------------------
// MOUSE
// -----------------------------------

func mouseMove
xx = glutEventX()
yy = glutEventY()

// this will only be true when the left button is down


if xOrigin >= 0

// update deltaAngle
deltaAngle = (xx - xOrigin) * 0.001

// update camera's direction


lx = sin(angle + deltaAngle)
lz = -cos(angle + deltaAngle)
ok

func mouseButton

button = glutEventButton()
state = glutEventState()
xx = glutEventX()
yy = glutEventY()

// only start motion if the left button is pressed


if button = GLUT_LEFT_BUTTON
// when the button is released
if state = GLUT_UP
angle += deltaAngle
xOrigin = -1
else
// state = GLUT_DOWN
xOrigin = xx
ok
ok

54.14. Frames Per Second 525


Ring Documentation, Release 1.5.1

// -----------------------------------
// MENUS
// -----------------------------------

func processMenuStatus

status = glutEventStatus()

if status = GLUT_MENU_IN_USE
menuFlag = 1
else
menuFlag = 0
ok

func processMainMenu

// nothing to do in here
// all actions are for submenus

func processFillMenu

option = glutEventValue()

switch option

on C_FILL
glPolygonMode(GL_FRONT, GL_FILL)
on C_LINE
glPolygonMode(GL_FRONT, GL_LINE)
off

func processFontMenu

option = glutEventValue()

switch (option) {
on C_INT_GLUT_BITMAP_8_BY_13
font = GLUT_BITMAP_8_BY_13
on C_INT_GLUT_BITMAP_9_BY_15
font = GLUT_BITMAP_9_BY_15
on C_INT_GLUT_BITMAP_TIMES_ROMAN_10
font = GLUT_BITMAP_TIMES_ROMAN_10
on C_INT_GLUT_BITMAP_TIMES_ROMAN_24
font = GLUT_BITMAP_TIMES_ROMAN_24
on C_INT_GLUT_BITMAP_HELVETICA_10
font = GLUT_BITMAP_HELVETICA_10
on C_INT_GLUT_BITMAP_HELVETICA_12
font = GLUT_BITMAP_HELVETICA_12
on C_INT_GLUT_BITMAP_HELVETICA_18
font = GLUT_BITMAP_HELVETICA_18
off

func processColorMenu

option = glutEventValue()

54.14. Frames Per Second 526


Ring Documentation, Release 1.5.1

switch option
on C_RED
red = 1.0
green = 0.0
blue = 0.0
on C_GREEN
red = 0.0
green = 1.0
blue = 0.0
on C_BLUE
red = 0.0
green = 0.0
blue = 1.0
on C_ORANGE
red = 1.0
green = 0.5
blue = 0.5
off

func createPopupMenus

fontMenu = glutCreateMenu(:processFontMenu)

glutAddMenuEntry("BITMAP_8_BY_13 ",C_INT_GLUT_BITMAP_8_BY_13 )
glutAddMenuEntry("BITMAP_9_BY_15",C_INT_GLUT_BITMAP_9_BY_15 )
glutAddMenuEntry("BITMAP_TIMES_ROMAN_10 ",C_INT_GLUT_BITMAP_TIMES_ROMAN_10 )
glutAddMenuEntry("BITMAP_TIMES_ROMAN_24",C_INT_GLUT_BITMAP_TIMES_ROMAN_24 )
glutAddMenuEntry("BITMAP_HELVETICA_10 ",C_INT_GLUT_BITMAP_HELVETICA_10 )
glutAddMenuEntry("BITMAP_HELVETICA_12",C_INT_GLUT_BITMAP_HELVETICA_12 )
glutAddMenuEntry("BITMAP_HELVETICA_18",C_INT_GLUT_BITMAP_HELVETICA_18 )

fillMenu = glutCreateMenu(:processFillMenu)

glutAddMenuEntry("Fill",C_FILL)
glutAddMenuEntry("Line",C_LINE)

colorMenu = glutCreateMenu(:processColorMenu)
glutAddMenuEntry("Red",C_RED);
glutAddMenuEntry("Blue",C_BLUE);
glutAddMenuEntry("Green",C_GREEN);
glutAddMenuEntry("Orange",C_ORANGE);

mainMenu = glutCreateMenu(:processMainMenu)

glutAddSubMenu("Polygon Mode", fillMenu)


glutAddSubMenu("Color", colorMenu)
glutAddSubMenu("Font",fontMenu)
// attach the menu to the right button
glutAttachMenu(GLUT_RIGHT_BUTTON)

// this will allow us to know if the menu is active


glutMenuStatusFunc(:processMenuStatus)

// -----------------------------------
// MAIN
// -----------------------------------

54.14. Frames Per Second 527


Ring Documentation, Release 1.5.1

func main

// init GLUT and create window


glutInit()
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA)
glutInitWindowPosition(100,100)
glutInitWindowSize(320,320)
glutCreateWindow("RingFreeGLUT - Test - 9 SnowMan")

// register callbacks
glutDisplayFunc(:renderScene)
glutReshapeFunc(:changeSize)
glutIdleFunc(:renderScene)

glutIgnoreKeyRepeat(1)
glutKeyboardFunc(:processNormalKeys)
glutSpecialFunc(:pressKey)
glutSpecialUpFunc(:releaseKey)

// here are the two new functions


glutMouseFunc(:mouseButton)
glutMotionFunc(:mouseMove)

// OpenGL init
glEnable(GL_DEPTH_TEST)
glEnable(GL_CULL_FACE)

// init Menus
createPopupMenus()

// enter GLUT event processing cycle


glutMainLoop()

Screen Shots:
The First screen shot

54.14. Frames Per Second 528


Ring Documentation, Release 1.5.1

The Second screen shot

54.15 Make a Cube using RingOpenGL and RingFreeGLUT

Example:
load "freeglut.ring"
load "opengl21lib.ring"

// ----------------------------------------------------------

54.15. Make a Cube using RingOpenGL and RingFreeGLUT 529


Ring Documentation, Release 1.5.1

// Global Variables
// ----------------------------------------------------------
rotate_y=0
rotate_x=0

// ----------------------------------------------------------
// display() Callback function
// ----------------------------------------------------------
func display

// Clear screen and Z-buffer


glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)

// Reset transformations
glLoadIdentity()

// Rotate when user changes rotate_x and rotate_y


glRotatef( rotate_x, 1.0, 0.0, 0.0 )
glRotatef( rotate_y, 0.0, 1.0, 0.0 )

//Multi-colored side - FRONT


glBegin(GL_POLYGON)

glColor3f( 1.0, 0.0, 0.0 ) glVertex3f( 0.5, -0.5, -0.5 ) # P1 is red


glColor3f( 0.0, 1.0, 0.0 ) glVertex3f( 0.5, 0.5, -0.5 ) # P2 is green
glColor3f( 0.0, 0.0, 1.0 ) glVertex3f( -0.5, 0.5, -0.5 ) # P3 is blue
glColor3f( 1.0, 0.0, 1.0 ) glVertex3f( -0.5, -0.5, -0.5 ) # P4 is purple

glEnd()

// White side - BACK


glBegin(GL_POLYGON)
glColor3f( 1.0, 1.0, 1.0 )
glVertex3f( 0.5, -0.5, 0.5 )
glVertex3f( 0.5, 0.5, 0.5 )
glVertex3f( -0.5, 0.5, 0.5 )
glVertex3f( -0.5, -0.5, 0.5 )
glEnd()

// Purple side - RIGHT


glBegin(GL_POLYGON)
glColor3f( 1.0, 0.0, 1.0 )
glVertex3f( 0.5, -0.5, -0.5 )
glVertex3f( 0.5, 0.5, -0.5 )
glVertex3f( 0.5, 0.5, 0.5 )
glVertex3f( 0.5, -0.5, 0.5 )
glEnd()

// Green side - LEFT


glBegin(GL_POLYGON)
glColor3f( 0.0, 1.0, 0.0 )
glVertex3f( -0.5, -0.5, 0.5 )
glVertex3f( -0.5, 0.5, 0.5 )
glVertex3f( -0.5, 0.5, -0.5 )
glVertex3f( -0.5, -0.5, -0.5 )
glEnd()

// Blue side - TOP

54.15. Make a Cube using RingOpenGL and RingFreeGLUT 530


Ring Documentation, Release 1.5.1

glBegin(GL_POLYGON)
glColor3f( 0.0, 0.0, 1.0 )
glVertex3f( 0.5, 0.5, 0.5 )
glVertex3f( 0.5, 0.5, -0.5 )
glVertex3f( -0.5, 0.5, -0.5 )
glVertex3f( -0.5, 0.5, 0.5 )
glEnd()

// Red side - BOTTOM


glBegin(GL_POLYGON)
glColor3f( 1.0, 0.0, 0.0 )
glVertex3f( 0.5, -0.5, -0.5 )
glVertex3f( 0.5, -0.5, 0.5 )
glVertex3f( -0.5, -0.5, 0.5 )
glVertex3f( -0.5, -0.5, -0.5 )
glEnd()

glFlush()
glutSwapBuffers()

// ----------------------------------------------------------
// specialKeys() Callback Function
// ----------------------------------------------------------
func specialKeys

key = glutEventKey()

// Right arrow - increase rotation by 5 degree


switch Key

on GLUT_KEY_RIGHT
rotate_y += 5

// Left arrow - decrease rotation by 5 degree


on GLUT_KEY_LEFT
rotate_y -= 5

on GLUT_KEY_UP
rotate_x += 5

on GLUT_KEY_DOWN
rotate_x -= 5

off

// Request display update


glutPostRedisplay()

// ----------------------------------------------------------
// main() function
// ----------------------------------------------------------
func main

// Initialize GLUT and process user parameters


glutInit()

54.15. Make a Cube using RingOpenGL and RingFreeGLUT 531


Ring Documentation, Release 1.5.1

// Request double buffered true color window with Z-buffer


glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH)

// Create window
glutCreateWindow("Awesome Cube")

// Enable Z-buffer depth test


glEnable(GL_DEPTH_TEST)

// Callback functions
glutDisplayFunc(:display)
glutSpecialFunc(:specialKeys)

// Pass control to GLUT for events


glutMainLoop()

// Return to OS

Screen Shot:

54.15. Make a Cube using RingOpenGL and RingFreeGLUT 532


CHAPTER

FIFTYFIVE

DESKTOP AND MOBILE DEVELOPMENT USING RINGQT

In this chapter we will learn how to use the Qt framework classes in our Ring applications to create Desktop and
Mobile Applications.

55.1 The First GUI Application

In this example we will create an application to ask the user about his/her name. When the user type the name in the
textbox then click on Say Hello button, the textbox value will be updated by adding Hello to the name.
Load "guilib.ring"

MyApp = New qApp {

win1 = new qWidget() {

setwindowtitle("Hello World")
setGeometry(100,100,370,250)

label1 = new qLabel(win1) {


settext("What is your name ?")
setGeometry(10,20,350,30)
setalignment(Qt_AlignHCenter)
}

btn1 = new qpushbutton(win1) {


setGeometry(10,200,100,30)
settext("Say Hello")
setclickevent("pHello()")
}

btn1 = new qpushbutton(win1) {


setGeometry(150,200,100,30)
settext("Close")
setclickevent("pClose()")
}

lineedit1 = new qlineedit(win1) {


setGeometry(10,100,350,30)
}

show()
}

533
Ring Documentation, Release 1.5.1

exec()
}

Func pHello
lineedit1.settext( "Hello " + lineedit1.text())

Func pClose
MyApp.quit()

Program Output:
At first we type the name in the textbox

Then we click on the say hello button

55.1. The First GUI Application 534

Potrebbero piacerti anche