ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
(Generate patch)

Comparing deliantra/Deliantra-Client/bin/pclient (file contents):
Revision 1.23 by elmex, Fri Apr 7 20:15:01 2006 UTC vs.
Revision 1.51 by root, Sun Apr 9 21:34:49 2006 UTC

1#!/opt/bin/perl 1#!/opt/bin/perl
2
2use strict; 3use strict;
4use utf8;
3 5
4use Glib; 6use Glib;
5use Gtk2 -init; 7use Gtk2 -init;
6 8
7use SDL; 9use SDL;
13 15
14use Crossfire; 16use Crossfire;
15use Crossfire::Client; 17use Crossfire::Client;
16use Crossfire::Protocol; 18use Crossfire::Protocol;
17 19
18use Crossfire::Client::Util;
19use Crossfire::Client::Widget; 20use Crossfire::Client::Widget;
20 21
21our $FACECACHE; 22our $FACECACHE;
22 23
23our $VERSION = '0.1'; 24our $VERSION = '0.1';
25
26our %GL_EXT;
24 27
25our $CFG; 28our $CFG;
26our $CONN; 29our $CONN;
30
31our $WIDTH;
32our $HEIGHT;
33our $FULLSCREEN;
34
35our $FONTSIZE;
27 36
28our $SDL_TIMER; 37our $SDL_TIMER;
29our $SDL_APP; 38our $SDL_APP;
30our $SDL_EV = new SDL::Event; 39our $SDL_EV = new SDL::Event;
31our %SDL_CB; 40our %SDL_CB;
32 41
33our @GL_INIT; # hooks called on every gl init 42our @GL_INIT; # hooks called on every gl init
34 43
44our $ALT_ENTER_MESSAGE;
45our $STATUS_LINE;
46
47our $TOPLEVEL;
48
49our $tw; # Test widget #d#
50
51my $last_refresh;
52my %ANIMATE;
53my $refresh_handler;
54
35sub init_screen { 55sub init_screen {
36 $SDL_APP = new SDL::App 56 $SDL_APP = new SDL::App
37 -flags => SDL_ANYFORMAT | SDL_HWSURFACE, 57 -flags => SDL_ANYFORMAT | SDL_HWSURFACE,
38 -title => "Crossfire+ Client", 58 -title => "Crossfire+ Client",
39 -width => $CFG->{width}, 59 -width => $WIDTH,
40 -height => $CFG->{height}, 60 -height => $HEIGHT,
41 -opengl => 1, 61 -opengl => 1,
42 -red_size => 8, 62 -red_size => 5,
43 -green_size => 8, 63 -green_size => 5,
44 -blue_size => 8, 64 -blue_size => 5,
65 -alpha_size => 0,
45 -double_buffer => 1, 66 -double_buffer => 1,
46 -fullscreen => $CFG->{fullscreen}, 67 -fullscreen => $FULLSCREEN,
47 -resizeable => 0; 68 -resizeable => 0;
48 69
70 $last_refresh = SDL::GetTicks;
71
72 %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions;
73
74 $GL_EXT{GL_ARB_texture_non_power_of_two}
75 or warn "WARNING: non-power-of-two opengl extension required";
76
77 $FONTSIZE = int $HEIGHT / 50;
78
79 $STATUS_LINE = new Crossfire::Client::Widget::Label
80 0, $HEIGHT * 59 / 60 - $FONTSIZE, 1, $FONTSIZE,
81 "";
82 $TOPLEVEL->add ($STATUS_LINE);
83
84 $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label
85 0, $HEIGHT * 59 / 60, 1, $HEIGHT / 60,
86 "Use <b>Alt-Enter</b> to toggle fullscreen mode";
87 $TOPLEVEL->add ($ALT_ENTER_MESSAGE);
88
89 # Test code #d#
90 unless ($tw) { # haha...
91 $tw = new Crossfire::Client::Widget::Animator;
92 my $lbl1 = new Crossfire::Client::Widget::Label
93 0, 0, 10, $FONTSIZE, "<i>This</i> is a\n<u>TEST</u>!\nOf a themed\nFrame!";
94 my $lbl2 = new Crossfire::Client::Widget::Label
95 0, 0, 10, $FONTSIZE, "LBL2";
96
97 my $vb = new Crossfire::Client::Widget::VBox;
98 my $f = new Crossfire::Client::Widget::FancyFrame;
99 my $f2 = new Crossfire::Client::Widget::FancyFrame;
100 $f->add ($lbl1);
101 $f2->add ($lbl2);
102 $vb->add ($f);
103 $vb->add ($f2, 1);
104
105 $tw->add ($vb);
106 $tw->w (400);
107 $tw->h (300);
108 $tw->move ($WIDTH - 200, 0);
109 $tw->moveto (0, 0);
110 $TOPLEVEL->add ($tw);
111
112# $f->move ($WIDTH - 200, 0);
113# $TOPLEVEL->add ($f);
114 }
115
49 glClearColor 0, 0, 0, 0; 116 glClearColor 0, 0, 0, 0;
50 117
51 glEnable GL_TEXTURE_2D; 118 glEnable GL_TEXTURE_2D;
52 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 119 glEnable GL_COLOR_MATERIAL;
53 glShadeModel GL_FLAT; 120 glShadeModel GL_FLAT;
54 glDisable GL_DEPTH_TEST; 121 glDisable GL_DEPTH_TEST;
55 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 122 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
56 glEnable GL_BLEND; 123
124 $_->() for @GL_INIT;
125}
126
127sub start_game {
128 $SDL_TIMER = add Glib::Timeout 1000/50, sub {
129 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
130 while $SDL_EV->poll;
131
132 1
133 };
134
135 $WIDTH = $CFG->{width};
136 $HEIGHT = $CFG->{height};
137 $FULLSCREEN = 0;
138
139 init_screen;
140
141 my $mapsize = List::Util::min 64, List::Util::max 11, int $HEIGHT * $CFG->{mapsize} * 0.01 / 32;
142
143 $CONN = new conn
144 host => $CFG->{host},
145 port => $CFG->{port},
146 user => $CFG->{user},
147 pass => $CFG->{password},
148 mapw => $mapsize,
149 maph => $mapsize,
150 ;
151
152 Crossfire::Client::lowdelay fileno $CONN->{fh};
153}
154
155sub stop_game {
156 remove Glib::Source $SDL_TIMER;
157 remove Glib::Source $refresh_handler if $refresh_handler;
158 undef $refresh_handler;
159
160 undef $SDL_APP;
161 undef $CONN;
162 SDL::Quit;
163}
164
165
166sub force_refresh {
167 glViewport 0, 0, $WIDTH, $HEIGHT;
57 168
58 glMatrixMode GL_PROJECTION; 169 glMatrixMode GL_PROJECTION;
59 glLoadIdentity; 170 glLoadIdentity;
60 glOrtho 0, $CFG->{width} / 32, $CFG->{height} / 32, 0, -100 , 100; 171 glOrtho 0, $WIDTH, $HEIGHT, 0, -6000 , 6000;
61
62 glMatrixMode GL_MODELVIEW; 172 glMatrixMode GL_MODELVIEW;
63 173
64 $_->() for @GL_INIT; 174 glClear GL_COLOR_BUFFER_BIT;
65}
66 175
67sub start_game { 176 $TOPLEVEL->draw;
68 $SDL_TIMER = add Glib::Timeout 1000/20, sub { 177
69 while ($SDL_EV->poll) { 178 SDL::GLSwapBuffers;
70 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->(); 179}
180
181sub refresh {
182 $refresh_handler ||= add Glib::Idle sub {
183 return unless $SDL_APP;
184
185 my $next_refresh = SDL::GetTicks;
186 my $interval = ($next_refresh - $last_refresh) * 0.001;
187 $last_refresh = $next_refresh;
188
189 force_refresh;
190 $_->animate ($interval) for grep $_, values %ANIMATE;
191
192 if (%ANIMATE) {
193 1
194 } else {
195 undef $refresh_handler;
196 0
71 } 197 }
72
73 1
74 }; 198 };
75
76 init_screen;
77
78 $CONN = new conn
79 host => $CFG->{host},
80 port => $CFG->{port};
81} 199}
82 200
83sub stop_game { 201sub animation_start {
84 remove Glib::Source $SDL_TIMER; 202 my ($widget) = @_;
203 $ANIMATE{$widget} = $widget;
204 Scalar::Util::weaken $ANIMATE{$widget};
85 205
86 undef $SDL_APP; 206 refresh;
87 SDL::Quit;
88} 207}
89 208
90sub refresh { 209sub animation_stop {
91 glClear GL_COLOR_BUFFER_BIT; 210 my ($widget) = @_;
92 211 delete $ANIMATE{$widget};
93 $_->draw for values %Crossfire::Client::Widget::ACTIVE_WIDGETS;
94
95 SDL::GLSwapBuffers;
96} 212}
97 213
98%SDL_CB = ( 214%SDL_CB = (
99 SDL_QUIT() => sub { 215 SDL_QUIT() => sub {
100 main_quit Gtk2; 216 main_quit Gtk2;
105 refresh; 221 refresh;
106 }, 222 },
107 SDL_KEYDOWN() => sub { 223 SDL_KEYDOWN() => sub {
108 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) { 224 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) {
109 # alt-enter 225 # alt-enter
110 $CFG->{fullscreen} = !$CFG->{fullscreen}; 226 $FULLSCREEN = !$FULLSCREEN;
111 init_screen; 227 init_screen;
112 } else { 228 } else {
113 Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV); 229 Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV);
114 } 230 }
115 }, 231 },
139} 255}
140 256
141sub conn::map_scroll { 257sub conn::map_scroll {
142 my ($self, $dx, $dy) = @_; 258 my ($self, $dx, $dy) = @_;
143 259
144 refresh; 260# refresh;
145} 261}
146 262
147sub conn::map_clear { 263sub conn::map_clear {
148 my ($self) = @_; 264 my ($self) = @_;
149 265
150 refresh; 266# refresh;
151} 267}
152 268
153sub conn::face_find { 269sub conn::face_find {
154 my ($self, $face) = @_; 270 my ($self, $face) = @_;
155 271
160 my ($self, $face) = @_; 276 my ($self, $face) = @_;
161 277
162 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; 278 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
163 279
164 $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image}; 280 $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image};
281}
282
283sub conn::query {
284 my ($self, $flags, $prompt) = @_;
285
286 warn "<<<<QUERY:$flags:$prompt>>>\n";#d#
165} 287}
166 288
167sub gtk_add_cfg_field { 289sub gtk_add_cfg_field {
168 my ($tbl, $cfg, $klbl, $key, $value) = @_; 290 my ($tbl, $cfg, $klbl, $key, $value) = @_;
169 my $i = $cfg->{_i}++; 291 my $i = $cfg->{_i}++;
175 } 297 }
176 $ent->set_text ($value); 298 $ent->set_text ($value);
177 $ent->signal_connect (changed => sub { 299 $ent->signal_connect (changed => sub {
178 my ($ent) = @_; 300 my ($ent) = @_;
179 $cfg->{$key} = $ent->get_text; 301 $cfg->{$key} = $ent->get_text;
302 # TODO: Mapsize should be a slider in the game gui
303 if ($key eq 'mapsize' and $cfg->{$key} > 100) {
304 $cfg->{$key} = 100;
305 } elsif ($key eq 'mapsize' and $cfg->{$key} < 50) {
306 $cfg->{$key} = 50;
307 }
180 }); 308 });
181} 309}
182 310
183sub run_config_dialog { 311sub run_config_dialog {
184 my (%events) = @_; 312 my (%events) = @_;
186 my $w = Gtk2::Window->new; 314 my $w = Gtk2::Window->new;
187 315
188 my @cfg = ( 316 my @cfg = (
189 [qw/Host host/], 317 [qw/Host host/],
190 [qw/Port port/], 318 [qw/Port port/],
319 [qw/Mapsize% mapsize/],
191 [qw/Username user/], 320 [qw/Username user/],
192 [qw/Password password/], 321 [qw/Password password/],
193 ); 322 );
194 323
195 my $cfg = {}; 324 my $cfg = {};
242 my $cb = $events{login} || sub {}; 371 my $cb = $events{login} || sub {};
243 $cb->($::CFG->{user}, $::CFG->{password}); 372 $cb->($::CFG->{user}, $::CFG->{password});
244 }); 373 });
245 $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5); 374 $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5);
246 $cb->signal_connect (clicked => sub { 375 $cb->signal_connect (clicked => sub {
247 my $cb = $events{login} || sub {}; 376 my $cb = $events{logout} || sub {};
248 $cb->(); 377 $cb->();
249 }); 378 });
250 $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5); 379 $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5);
251 $cb->signal_connect (clicked => sub { $w->destroy }); 380 $cb->signal_connect (clicked => sub { $w->destroy });
252 381
256} 385}
257 386
258 387
259############################################################################# 388#############################################################################
260 389
261SDL::Init(SDL_INIT_EVERYTHING()); 390SDL::Init SDL_INIT_EVERYTHING;
391
392$TOPLEVEL = Crossfire::Client::Widget::Toplevel->new;
262 393
263my $mapwidget = Crossfire::Client::Widget::MapWidget->new; 394my $mapwidget = Crossfire::Client::Widget::MapWidget->new;
264 395
265find_rcfile "uifont.ttf"; 396$TOPLEVEL->add ($mapwidget);
266
267$mapwidget->activate;
268$mapwidget->focus_in; 397$mapwidget->focus_in;
269 398
270Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; 399Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc";
271
272$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
273 400
274$CFG ||= { 401$CFG ||= {
275 width => 640, 402 width => 640,
276 height => 480, 403 height => 480,
404 mapsize => 100,
277 fullscreen => 0, 405 fullscreen => 0,
278 host => "crossfire.schmorp.de", 406 host => "crossfire.schmorp.de",
279 port => 13327, 407 port => 13327,
280}; 408};
281 409
410Crossfire::Client::set_font Crossfire::Client::find_rcfile "uifont.ttf";
411
412$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
413
282run_config_dialog 414run_config_dialog
283 login => sub { start_game }, 415 login => sub { start_game },
284 logout => sub { stop_game }; 416 logout => sub { stop_game };
285 417
286main Gtk2; 418main Gtk2;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines