OSDN Git Service

6b0b9576d663c93623726670f35eb9ed48bcf93f
[android-x86/external-mesa.git] / progs / beos / demo.cpp
1 // $Id: demo.cpp,v 1.2 2004/08/14 09:59:16 phoudoin Exp $
2
3 // Simple BeOS GLView demo
4 // Written by Brian Paul
5 // Changes by Philippe Houdoin
6 // This file is in the public domain.
7
8
9
10 #include <stdio.h>
11 #include <Application.h>
12 #include <Window.h>
13 #include <GLView.h>
14
15 class MyGL : public BGLView
16 {
17 public:
18         MyGL(BRect rect, char *name, ulong options);
19
20         virtual void AttachedToWindow();
21         virtual void Pulse();
22         virtual void FrameResized(float w, float h);
23
24 private:
25         void Render();
26         void Reshape(float w, float h);
27         float mAngle;
28 };
29
30
31 class MyWindow : public BWindow
32 {
33 public:
34         MyWindow(BRect frame);
35         virtual bool QuitRequested();
36 };
37
38
39 MyWindow::MyWindow(BRect frame)
40    : BWindow(frame, "demo", B_TITLED_WINDOW, B_NOT_ZOOMABLE)
41 {
42    // Make OpenGL view and put it in the window
43    BRect r = Bounds();
44    r.InsetBy(5, 5);
45    
46    MyGL *gl = new MyGL(r, "GL", BGL_RGB | BGL_DOUBLE);
47    AddChild(gl);
48    SetPulseRate(1000000 / 30);
49 }
50
51 bool MyWindow::QuitRequested()
52 {
53    be_app->PostMessage(B_QUIT_REQUESTED);
54    return true;
55 }
56
57
58
59 MyGL::MyGL(BRect rect, char *name, ulong options)
60    : BGLView(rect, name, B_FOLLOW_ALL_SIDES, B_PULSE_NEEDED, options)
61 {
62         mAngle = 0.0;
63 }
64
65
66 void MyGL::AttachedToWindow()
67 {
68         BGLView::AttachedToWindow();
69
70         LockGL();
71         glClearColor(0.7, 0.7, 0, 0);
72         Reshape(Bounds().Width(), Bounds().Height());
73         UnlockGL();
74 }
75
76
77 void MyGL::FrameResized(float w, float h)
78 {
79         BGLView::FrameResized(w, h);
80
81         LockGL();
82         Reshape(w, h);
83         UnlockGL();
84
85         Render();
86 }
87
88
89 void MyGL::Pulse()
90 {
91         mAngle += 1.0;
92         Render();
93 }
94
95
96 void MyGL::Render()
97 {
98     LockGL();
99
100     glClear(GL_COLOR_BUFFER_BIT);
101     
102     glPushMatrix();
103
104     glRotated(mAngle, 0, 0, 1);
105     glColor3f(0, 0, 1);
106
107     glBegin(GL_POLYGON);
108     glVertex2f(-1, -1);
109     glVertex2f( 1, -1);
110     glVertex2f( 1,  1);
111     glVertex2f(-1,  1);
112     glEnd();
113
114         glPopMatrix();
115
116     SwapBuffers();
117
118     UnlockGL();
119 }
120
121
122 void MyGL::Reshape(float w, float h)
123 {
124         glViewport(0, 0, (int) (w + 1), (int) (h + 1));
125         glMatrixMode(GL_PROJECTION);
126         glLoadIdentity();
127         glFrustum(-1, 1, -1, 1, 10, 30);
128         glMatrixMode(GL_MODELVIEW);
129         glLoadIdentity();
130         glTranslatef(0, 0, -18);
131 }
132
133
134 int main(int argc, char *argv[])
135 {
136    BApplication *app = new BApplication("application/demo");
137
138    // make top-level window
139    MyWindow *win = new MyWindow(BRect(100, 100, 500, 500));
140    win->Show();
141
142     app->Run();
143
144    delete app;
145
146    return 0;
147 }