summaryrefslogtreecommitdiff
path: root/progs/beos/demo.cpp
blob: c25eb93075cf3f2bb3d467525e02838f447356ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// $Id: demo.cpp,v 1.1 1999/08/19 00:55:40 jtg Exp $

// Simple BeOS GLView demo
// Written by Brian Paul
// This file is in the public domain.



#include <stdio.h>
#include <Application.h>
#include <Window.h>
#include <GLView.h>


class MyWindow : public BWindow
{
public:
   MyWindow(BRect frame);
   virtual bool QuitRequested();
};


MyWindow::MyWindow(BRect frame)
   : BWindow(frame, "demo", B_TITLED_WINDOW, B_NOT_ZOOMABLE)
{
   // no-op
}

bool MyWindow::QuitRequested()
{
   be_app->PostMessage(B_QUIT_REQUESTED);
   return true;
}


class MyGL : public BGLView
{
public:
   MyGL(BRect rect, char *name, ulong options);

//	virtual void AttachedToWindow();
   virtual void Draw(BRect updateRect);
   virtual void Pulse();
   virtual void FrameResized(float w, float h);
private:
   float mAngle;
};


MyGL::MyGL(BRect rect, char *name, ulong options)
   : BGLView(rect, name, B_FOLLOW_LEFT_RIGHT | B_FOLLOW_TOP_BOTTOM, 0, options)
{
   mAngle = 0.0;
}


#if 0
void MyGL::AttachedToWindow()
{
   BGLView::AttachedToWindow();
   LockGL();
   glClearColor(.7, .7, 0, 0);
   UnlockGL();
}
#endif


void MyGL::FrameResized(float w, float h)
{
    BGLView::FrameResized(w, h);

    printf("FrameResized\n");
    LockGL();
    BGLView::FrameResized(w,h);
    glViewport(0, 0, (int) (w + 1), (int) (h + 1));
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum(-1, 1, -1, 1, 10, 30);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0, 0, -18);
    UnlockGL();
}



void MyGL::Draw(BRect r)
{
    printf("MyGL::Draw\n");
    BGLView::Draw(r);
    LockGL();
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();
    glRotatef(mAngle, 0, 0, 1);
    glColor3f(0, 0, 1);
    glBegin(GL_POLYGON);
    glVertex2f(-1, -1);
    glVertex2f( 1, -1);
    glVertex2f( 1,  1);
    glVertex2f(-1,  1);
    glEnd();
    SwapBuffers();
    UnlockGL();
}


void MyGL::Pulse()
{
   printf("pulse\n");
   BGLView::Pulse();
   mAngle += 1.0;

   LockGL();
   glClear(GL_COLOR_BUFFER_BIT);
   glPushMatrix();
   glRotatef(mAngle, 0, 0, 1);
   glColor3f(0, 0, 1);
   glBegin(GL_POLYGON);
   glVertex2f(-1, -1);
   glVertex2f( 1, -1);
   glVertex2f( 1,  1);
   glVertex2f(-1,  1);
   glEnd();
   SwapBuffers();
   UnlockGL();
}



int main(int argc, char *argv[])
{
   BApplication *app = new BApplication("application/demo");

   // make top-level window
   int x = 500, y = 500;
   int w = 400, h = 400;
   MyWindow *win = new MyWindow(BRect(x, y, x + w, y + h));
   //	win->Lock();
   //	win->Unlock();
   win->Show();

   // Make OpenGL view and put it in the window
   MyGL *gl = new MyGL(BRect(5, 5, w-10, h-10), "GL", BGL_RGB | BGL_DOUBLE);
   //	MyGL *gl = new MyGL(BRect(5, 5, w-10, h-10), "GL", BGL_RGB );
   win->AddChild(gl);

   printf("calling app->Run\n");	
   app->Run();

   delete app;

   return 0;
}