本帖最后由 523066680 于 2017-7-23 12:52 编辑
从零创建起来的图表,还未完善,费力+真心难看。
- =info
- Auth: 523066680
- Date: 2017-07
- =cut
-
- use JSON;
- use Encode;
- use Try::Tiny;
- use Data::Dumper;
- use Time::HiRes qw/time sleep/;
- use IO::Handle;
- STDOUT->autoflush(1);
-
- use threads;
- use threads::shared;
-
- use OpenGL qw/ :all /;
- use OpenGL::Config;
- #use Font::Freetype;
-
- use LWP::UserAgent;
-
- our $user = "vic3";
- our $website = 'http://bbaass.tk/math/';
- our ($user, $website, $headers);
- our $ua = LWP::UserAgent->new( agent => 'Mozilla/5.0', timeout => 3 );
-
- our %ucolor :shared;
- our $users :shared;
-
- #使共享变量支持复合数据结构
- print "Loading data...";
- make_user_struct();
- print "Done\n";
-
- our $SIZE_X = 800;
- our $SIZE_Y = 800;
-
- our $WinID;
-
- threads->create( \&getData, 1);
-
- &Main();
-
- my $count = 0;
- my $delta;
- my $speed;
- my $time_a = time();
-
- INFORMATION:
- {
- sub getData
- {
- our ($ua, $website, $user);
- my $res;
- my $data;
- my $info;
- my $json = JSON->new->allow_nonref;
- $SIG{'BREAK'} = sub { threads->exit() };
-
- $res = $ua->post( $website, [ username => $user, send => 'reg' ] );
-
- while (1)
- {
- SUBW: while (1)
- {
- $res = $ua->post( $website, [ username => $user, send => 'user' ] );
- try { $data = $json->decode( $res->content ); last SUBW; }
- catch { sleep 2.0; printf "repeat\n" }
- }
-
- for my $e ( @{$data->{'user'}} )
- {
- ( $times, $name, $state ) = @$e;
- push @{$users->{$name}}, $times;
- }
-
- sleep 2.0;
- }
- }
-
- sub make_user_struct
- {
- our ($ua, $website, $user);
-
- my $data;
- my $json = JSON->new->allow_nonref;
-
- while (1)
- {
- $res = $ua->post( $website, [ username => $user, send => 'reg' ] );
- $res = $ua->post( $website, [ username => $user, send => 'user' ] );
- try { $data = $json->decode( $res->content ); last; }
- catch { sleep 2.0; printf "retry\n" }
- }
-
- my @colors = (
- [1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.5, 1.0],
- [1.0, 1.0, 0.0], [0.0, 1.0, 1.0], [1.0, 0.0, 1.0],
- [1.0, 1.0, 1.0],
- );
-
- my $hash;
- for my $e ( @{$data->{'user'}} )
- {
- ( $times, $name, $state ) = @$e;
- $hash->{$name} = [ $times ];
- }
-
- #建立颜色信息
- my $n = 0;
- for my $name ( sort keys %$hash )
- {
- $ucolor{$name} = shared_clone( $colors[$n++] );
- }
-
- $users = shared_clone( $hash );
- }
- }
-
- OPENGL:
- {
- sub display
- {
- glClear(GL_COLOR_BUFFER_BIT);
-
- glColor3f(1.0, 1.0, 1.0);
- glBegin(GL_LINES);
- glVertex3f(-200.0, 0.0, 0.0);
- glVertex3f(200.0, 0.0, 0.0);
- glVertex3f(0.0, -200.0, 0.0);
- glVertex3f(0.0, 200.0, 0.0);
- glEnd();
-
- my $ty = 0;
- for my $name ( sort keys %ucolor )
- {
- glColor3f( @{$ucolor{$name}} );
- glRasterPos2i(-10, $ty);
- printstr( $name );
- $ty+=2;
- }
-
- my $obj;
- for my $name ( keys %$users )
- {
- $obj = $users->{$name};
- glColor3f( @{$ucolor{$name}} );
- glBegin(GL_LINE_STRIP);
- for my $v ( 1 .. $#{$obj} )
- {
- glVertex3f( $v, $obj->[$v] - $obj->[0], 0.0 );
- }
- glEnd();
- }
-
- glutSwapBuffers();
- }
-
- sub init
- {
- glClearColor(0.0, 0.0, 0.0, 1.0);
- }
-
- sub idle
- {
- sleep 0.1;
- glutPostRedisplay();
- }
-
- sub Reshape
- {
- my $half = 100.0;
- glViewport(0.0,0.0, $SIZE_X, $SIZE_Y);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(-20.0, $half, -20.0, $half,-20.0,200.0);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- gluLookAt(0.0,0.0,100.0,0.0,0.0,0.0, 0.0,1.0,100.0);
- }
-
- sub hitkey
- {
- my $key = shift;
- if (lc(chr($key)) eq 'q')
- {
- glutDestroyWindow($WinID);
- }
- elsif ($key == 27)
- {
- glutDestroyWindow($WinID);
- }
- }
-
- sub Main
- {
- glutInit();
- glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE |GLUT_MULTISAMPLE );
- glutInitWindowSize($SIZE_X, $SIZE_Y);
- glutInitWindowPosition(1,1);
- our $WinID = glutCreateWindow("Show");
- &init();
- glutDisplayFunc(\&display);
- glutReshapeFunc(\&Reshape);
- glutKeyboardFunc(\&hitkey);
- glutIdleFunc(\&idle);
- glutMainLoop();
- }
-
- sub printstr
- {
- for my $i ( split("", $_[0]) )
- {
- #glutBitmapCharacter(GLUT_BITMAP_9_BY_15, ord($i));
- glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, ord($i));
- }
- }
-
- }
复制代码
|