File: tk_demo.pl

package info (click to toggle)
libopengl-perl 0.7000%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 2,308 kB
  • sloc: perl: 10,178; ansic: 8,576; makefile: 113
file content (104 lines) | stat: -rwxr-xr-x 2,210 bytes parent folder | download | duplicates (8)
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
#!/usr/local/bin/perl 
#
# This is an example of combining the tk module and opengl
# You have to have TK installed for this to work.
# this program opens a window and when you hit a key in
# the window a callback that does some opengl stuff is
# executed.  
# Yes, this is a totally lame program, but its a proof
# of concept sort of thing.
# We'll get something better next time :-)
#

use lib ('blib');
use strict;
use Tk;
use OpenGL;

my $top = MainWindow->new();

my $kid;

sub CreateKid { 
  my $par = shift;
  my $id = $par->WindowId;
  print " window id: $id -> ", (sprintf '%#x', $$id),"\n";
  my ($w, $h) = ($par->Width, $par->Height);
  my ($xbord, $ybord) = (int($w/8), int($h/8));
  $kid = glpOpenWindow( x => $xbord, y => $ybord, width=> ($w-2*$xbord),
			height=> ($h-2*$ybord),parent=>$$id);
}

sub ResetKid {
  return unless $kid;
  my $par = shift;
  my $w = $par->Width;
  my $h = $par->Height;
  my ($xbord, $ybord) = (int($w/8), int($h/8));
  $w = $w-2*$xbord;
  $h = $h-2*$ybord;
  glpMoveResizeWindow($xbord,$ybord,$w,$h);
  glViewport(0,0,$w,$h);
  print "viewport $w x $h, origin $xbord, $ybord\n";
  DrawKid();
}

my $pending = 0;
sub DrawKid {
	return unless $kid;
	return if $pending++;
	$top->DoWhenIdle(\&DrawKid_do);
}
sub DrawKid_do {
	return unless $kid;
	$pending = 0;
	print "Drawing...\n";
	glClearColor(0,0,1,1);
	glClear(GL_COLOR_BUFFER_BIT);
	glOrtho(-1,1,-1,1,-1,1);
	
	glColor3f(1,0,0);
	glBegin(GL_POLYGON);
	  glVertex2f(-0.5,-0.5);
	  glVertex2f(-0.5, 0.5);
	  glVertex2f( 0.5, 0.5);
	  glVertex2f( 0.5,-0.5);
	glEnd();
	glFlush();
}
sub DrawKid1 {
	return unless $kid;
	print "Visibility change\n";
	DrawKid;
}
sub DrawKid2 {
	return unless $kid;
	print "Expose change\n";
	DrawKid;
}

sub DoKey
{
	my $w = shift;
	return if $kid;
	CreateKid $w;
	DrawKid;
}

sub DoMouse
{
	shift;
	my ($b,$p) = (shift,shift);
	print "mouse-$b $p\n";
}


$top->bind("<Any-KeyPress>",\&DoKey);
$top->bind("<Any-ButtonPress>",[\&DoMouse, Ev('b'), Ev('@')]);
$top->bind("<KeyPress-q>",[$top, 'destroy']);
$top->bind("<KeyPress-Escape>",[$top, 'destroy']);
$top->bind("<Configure>",\&ResetKid);
$top->bind("<Visibility>",\&DrawKid1);
$top->bind("<Expose>",\&DrawKid2);

Tk::MainLoop();