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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines