summaryrefslogtreecommitdiff
path: root/progs/beos/demo.cpp
blob: 6b0b9576d663c93623726670f35eb9ed48bcf93f (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
// $Id: demo.cpp,v 1.2 2004/08/14 09:59:16 phoudoin Exp $

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



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

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

	virtual void AttachedToWindow();
	virtual void Pulse();
	virtual void FrameResized(float w, float h);

private:
	void Render();
	void Reshape(float w, float h);
	float mAngle;
};


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


MyWindow::MyWindow(BRect frame)
   : BWindow(frame, "demo", B_TITLED_WINDOW, B_NOT_ZOOMABLE)
{
   // Make OpenGL view and put it in the window
   BRect r = Bounds();
   r.InsetBy(5, 5);
   
   MyGL *gl = new MyGL(r, "GL", BGL_RGB | BGL_DOUBLE);
   AddChild(gl);
   SetPulseRate(1000000 / 30);
}

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



MyGL::MyGL(BRect rect, char *name, ulong options)
   : BGLView(rect, name, B_FOLLOW_ALL_SIDES, B_PULSE_NEEDED, options)
{
	mAngle = 0.0;
}


void MyGL::AttachedToWindow()
{
	BGLView::AttachedToWindow();

	LockGL();
	glClearColor(0.7, 0.7, 0, 0);
	Reshape(Bounds().Width(), Bounds().Height());
	UnlockGL();
}


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

	LockGL();
	Reshape(w, h);
	UnlockGL();

	Render();
}


void MyGL::Pulse()
{
	mAngle += 1.0;
	Render();
}


void MyGL::Render()
{
    LockGL();

    glClear(GL_COLOR_BUFFER_BIT);
    
    glPushMatrix();

    glRotated(mAngle, 0, 0, 1);
    glColor3f(0, 0, 1);

    glBegin(GL_POLYGON);
    glVertex2f(-1, -1);
    glVertex2f( 1, -1);
    glVertex2f( 1,  1);
    glVertex2f(-1,  1);
    glEnd();

	glPopMatrix();

    SwapBuffers();

    UnlockGL();
}


void MyGL::Reshape(float w, float 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);
}


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

   // make top-level window
   MyWindow *win = new MyWindow(BRect(100, 100, 500, 500));
   win->Show();

    app->Run();

   delete app;

   return 0;
}