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.65 by root, Tue Apr 11 12:27:51 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;
10use SDL::Surface; 12use SDL::Surface;
11use SDL::OpenGL; 13use SDL::OpenGL;
12use SDL::OpenGL::Constants; 14use SDL::OpenGL::Constants;
13 15
14use Crossfire; 16use Crossfire;
17use Crossfire::Protocol;
18
15use Crossfire::Client; 19use Crossfire::Client;
16use Crossfire::Protocol;
17
18use Crossfire::Client::Util;
19use Crossfire::Client::Widget; 20use Crossfire::Client::Widget;
20 21
22our $VERSION = '0.1';
23
24my $MAX_FPS = 30;
25my $TICKS_PER_FRAME = int 1000 / $MAX_FPS - 1; # min ticks per frame
26
21our $FACECACHE; 27our $FACECACHE;
22
23our $VERSION = '0.1';
24 28
25our $CFG; 29our $CFG;
26our $CONN; 30our $CONN;
27 31
28our $GUIFONT; 32our $WIDTH;
33our $HEIGHT;
34our $FULLSCREEN;
35
36our $FONTSIZE;
29 37
30our $SDL_TIMER; 38our $SDL_TIMER;
31our $SDL_APP; 39our $SDL_APP;
32our $SDL_EV = new SDL::Event; 40our $SDL_EV;
33our %SDL_CB; 41our %SDL_CB;
34 42
35our @GL_INIT; # hooks called on every gl init 43our $ALT_ENTER_MESSAGE;
44our $STATUS_LINE;
45our $DEBUG_STATUS;
46
47my $last_refresh;
48my %ANIMATE;
49my $refresh_handler;
50
51our ($tw, $te); # Test widget #d#
36 52
37sub init_screen { 53sub init_screen {
38 $SDL_APP = new SDL::App 54 $SDL_APP = new SDL::App
39 -flags => SDL_ANYFORMAT | SDL_HWSURFACE, 55 -flags => SDL_ANYFORMAT | SDL_HWSURFACE,
40 -title => "Crossfire+ Client", 56 -title => "Crossfire+ Client",
41 -width => $CFG->{width}, 57 -width => $WIDTH,
42 -height => $CFG->{height}, 58 -height => $HEIGHT,
43 -opengl => 1, 59 -opengl => 1,
44 -red_size => 8, 60 -red_size => 5,
45 -green_size => 8, 61 -green_size => 5,
46 -blue_size => 8, 62 -blue_size => 5,
63 -alpha_size => 0,
47 -double_buffer => 1, 64 -double_buffer => 1,
48 -fullscreen => $CFG->{fullscreen}, 65 -fullscreen => $FULLSCREEN,
49 -resizeable => 0; 66 -resizeable => 0;
50 67
68 $SDL_EV = new SDL::Event;
69 $SDL_EV->set_unicode (1);
70
71 $SDL_TIMER = add Glib::Timeout 1000/50, sub {
72 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
73 while $SDL_EV->poll;
74
75 1
76 };
77
78 $last_refresh = SDL::GetTicks;
79
80 Crossfire::Client::gl_init;
81
82 $FONTSIZE = int $HEIGHT / 50;
83
84 #############################################################################
85
51 glClearColor 0, 0, 0, 0; 86 glClearColor 0.45, 0.45, 0.45, 1;
52 87
53 glEnable GL_TEXTURE_2D; 88 glEnable GL_TEXTURE_2D;
54 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 89 glEnable GL_COLOR_MATERIAL;
55 glShadeModel GL_FLAT; 90 glShadeModel GL_FLAT;
56 glDisable GL_DEPTH_TEST; 91 glDisable GL_DEPTH_TEST;
57 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 92 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
58 glEnable GL_BLEND; 93
94 #############################################################################
95
96 $DEBUG_STATUS = new Crossfire::Client::Widget::Label 0, 0, 1, $FONTSIZE, "";
97 $Crossfire::Client::Widget::TOPLEVEL->add ($DEBUG_STATUS);
98
99 $STATUS_LINE = new Crossfire::Client::Widget::Label
100 0, $HEIGHT * 59 / 60 - $FONTSIZE, 1, $FONTSIZE,
101 "";
102 $Crossfire::Client::Widget::TOPLEVEL->add ($STATUS_LINE);
103
104 $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label
105 0, $HEIGHT * 59 / 60, 1, $HEIGHT / 60,
106 "Use <b>Alt-Enter</b> to toggle fullscreen mode";
107 $Crossfire::Client::Widget::TOPLEVEL->add ($ALT_ENTER_MESSAGE);
108
109 # Test code #d#
110 unless ($tw) { # haha...
111 $te = new Crossfire::Client::Widget::FancyFrame;
112 $te->add (new Crossfire::Client::Widget::Entry);
113 $te->move (300, 0, 2);
114 $Crossfire::Client::Widget::TOPLEVEL->add ($te);
115
116 $tw = new Crossfire::Client::Widget::Animator;
117 my $lbl1 = new Crossfire::Client::Widget::Label
118 0, 0, 10, $FONTSIZE, "<i>This</i> is a\n<u>TEST</u>!\nOf a themed\nFrame!";
119 my $lbl2 = new Crossfire::Client::Widget::Label
120 0, 0, 10, $FONTSIZE, "LBL2";
121
122 my $vb = new Crossfire::Client::Widget::VBox;
123 my $f = new Crossfire::Client::Widget::FancyFrame;
124 my $f2 = new Crossfire::Client::Widget::FancyFrame;
125 $f->add ($lbl1);
126 $f2->add ($lbl2);
127 $vb->add ($f);
128 $vb->add ($f2, 1);
129
130 $tw->add ($vb);
131 $tw->w (400);
132 $tw->h (300);
133 $tw->move ($WIDTH - 200, 0);
134 $tw->moveto (0, 0);
135 $Crossfire::Client::Widget::TOPLEVEL->add ($tw);
136
137# $f->move ($WIDTH - 200, 0);
138# $Crossfire::Client::Widget::TOPLEVEL->add ($f);
139 }
140}
141
142sub destroy_screen {
143 remove Glib::Source $SDL_TIMER;
144 undef $SDL_APP;
145 undef $SDL_EV;
146 SDL::Quit;
147}
148
149sub start_game {
150 $WIDTH = $CFG->{width};
151 $HEIGHT = $CFG->{height};
152 $FULLSCREEN = 0;
153
154 init_screen;
155
156 my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32;
157
158 $CONN = new conn
159 host => $CFG->{host},
160 port => $CFG->{port},
161 user => $CFG->{user},
162 pass => $CFG->{password},
163 mapw => $mapsize,
164 maph => $mapsize,
165 ;
166
167 Crossfire::Client::lowdelay fileno $CONN->{fh};
168}
169
170sub stop_game {
171 remove Glib::Source $refresh_handler if $refresh_handler;
172 undef $refresh_handler;
173
174 undef $CONN;
175 destroy_screen;
176}
177
178sub force_refresh {
179 glViewport 0, 0, $WIDTH, $HEIGHT;
59 180
60 glMatrixMode GL_PROJECTION; 181 glMatrixMode GL_PROJECTION;
61 glLoadIdentity; 182 glLoadIdentity;
62 glOrtho 0, $CFG->{width}, $CFG->{height}, 0, -100 , 100; 183 glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000;
63
64 glMatrixMode GL_MODELVIEW; 184 glMatrixMode GL_MODELVIEW;
65 185
66 $_->() for @GL_INIT; 186 glClear GL_COLOR_BUFFER_BIT;
67}
68 187
69my $label;#d# 188 $Crossfire::Client::Widget::TOPLEVEL->draw;
70 189
71sub start_game { 190 SDL::GLSwapBuffers;
72 $SDL_TIMER = add Glib::Timeout 1000/20, sub { 191}
73 while ($SDL_EV->poll) { 192
74 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->(); 193sub debug {
194 $DEBUG_STATUS->set_text ($_[0]);
195 $DEBUG_STATUS->size_allocate ($DEBUG_STATUS->size_request);
196 $DEBUG_STATUS->move ($WIDTH - $DEBUG_STATUS->{w}, 0);
197}
198
199my $FPS;
200
201sub refresh {
202 $refresh_handler ||= add Glib::Idle sub {
203 if ($SDL_APP) {
204 my $next_refresh = SDL::GetTicks;
205
206 if ($next_refresh - $last_refresh < $TICKS_PER_FRAME) {
207 SDL::Delay $TICKS_PER_FRAME - ($next_refresh - $last_refresh);
208 $next_refresh = SDL::GetTicks;
209 }
210
211 my $interval = ($next_refresh - $last_refresh) * 0.001;
212 $last_refresh = $next_refresh;
213
214 if ($interval) {
215 $FPS ||= 1 / $interval;
216 $FPS = $FPS * 0.96 + (1 / $interval) * 0.04;
217 debug sprintf "%5.02f\n", $FPS;
218 }
219
220 force_refresh;
221 $_->animate ($interval) for grep $_, values %ANIMATE;
222
223 if (%ANIMATE) {
224 1
225 } else {
226 undef $refresh_handler;
227 0
228 }
229 } else {
230 undef $refresh_handler;
231 0
75 } 232 }
76
77 1
78 }; 233 };
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} 234}
89 235
90sub stop_game { 236sub animation_start {
91 remove Glib::Source $SDL_TIMER; 237 my ($widget) = @_;
238 $ANIMATE{$widget} = $widget;
239 Scalar::Util::weaken $ANIMATE{$widget};
92 240
93 undef $SDL_APP; 241 refresh;
94 SDL::Quit;
95} 242}
96 243
97sub refresh { 244sub animation_stop {
98 glClear GL_COLOR_BUFFER_BIT; 245 my ($widget) = @_;
99 246 delete $ANIMATE{$widget};
100 $_->draw for values %Crossfire::Client::Widget::ACTIVE_WIDGETS;
101
102 SDL::GLSwapBuffers;
103} 247}
104 248
105%SDL_CB = ( 249%SDL_CB = (
106 SDL_QUIT() => sub { 250 SDL_QUIT() => sub {
107 main_quit Gtk2; 251 main_quit Gtk2;
112 refresh; 256 refresh;
113 }, 257 },
114 SDL_KEYDOWN() => sub { 258 SDL_KEYDOWN() => sub {
115 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) { 259 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) {
116 # alt-enter 260 # alt-enter
117 $CFG->{fullscreen} = !$CFG->{fullscreen}; 261 $FULLSCREEN = !$FULLSCREEN;
118 init_screen; 262 init_screen;
119 } else { 263 } else {
120 Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV); 264 Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV);
121 } 265 }
122 }, 266 },
123 SDL_KEYUP() => sub { 267 SDL_KEYUP() => sub {
124 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); 268 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV);
125 }, 269 },
126 SDL_MOUSEMOTION() => sub { 270 SDL_MOUSEMOTION() => sub {
127 warn "sdl motion\n";#d# 271 Crossfire::Client::Widget::feed_sdl_motion_event ($SDL_EV);
128 }, 272 },
129 SDL_MOUSEBUTTONDOWN() => sub { 273 SDL_MOUSEBUTTONDOWN() => sub {
130 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); 274 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV);
131 }, 275 },
132 SDL_MOUSEBUTTONUP() => sub { 276 SDL_MOUSEBUTTONUP() => sub {
133 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV); 277 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV);
134 }, 278 },
135 SDL_ACTIVEEVENT() => sub { 279 SDL_ACTIVEEVENT() => sub {
136 warn "active\n";#d# 280 printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d#
137 }, 281 },
138); 282);
139 283
140@conn::ISA = Crossfire::Protocol::; 284@conn::ISA = Crossfire::Protocol::;
141 285
146} 290}
147 291
148sub conn::map_scroll { 292sub conn::map_scroll {
149 my ($self, $dx, $dy) = @_; 293 my ($self, $dx, $dy) = @_;
150 294
151 refresh; 295# refresh;
152} 296}
153 297
154sub conn::map_clear { 298sub conn::map_clear {
155 my ($self) = @_; 299 my ($self) = @_;
156 300
157 refresh; 301# refresh;
158} 302}
159 303
160sub conn::face_find { 304sub conn::face_find {
161 my ($self, $face) = @_; 305 my ($self, $face) = @_;
162 306
167 my ($self, $face) = @_; 311 my ($self, $face) = @_;
168 312
169 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; 313 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
170 314
171 $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image}; 315 $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image};
316}
317
318sub conn::query {
319 my ($self, $flags, $prompt) = @_;
320
321 warn "<<<<QUERY:$flags:$prompt>>>\n";#d#
172} 322}
173 323
174sub gtk_add_cfg_field { 324sub gtk_add_cfg_field {
175 my ($tbl, $cfg, $klbl, $key, $value) = @_; 325 my ($tbl, $cfg, $klbl, $key, $value) = @_;
176 my $i = $cfg->{_i}++; 326 my $i = $cfg->{_i}++;
182 } 332 }
183 $ent->set_text ($value); 333 $ent->set_text ($value);
184 $ent->signal_connect (changed => sub { 334 $ent->signal_connect (changed => sub {
185 my ($ent) = @_; 335 my ($ent) = @_;
186 $cfg->{$key} = $ent->get_text; 336 $cfg->{$key} = $ent->get_text;
337 # TODO: Mapsize should be a slider in the game gui
338 if ($key eq 'mapsize' and $cfg->{$key} > 100) {
339 $cfg->{$key} = 100;
340 } elsif ($key eq 'mapsize' and $cfg->{$key} < 50) {
341 $cfg->{$key} = 50;
342 }
187 }); 343 });
188} 344}
189 345
190sub run_config_dialog { 346sub run_config_dialog {
191 my (%events) = @_; 347 my (%events) = @_;
193 my $w = Gtk2::Window->new; 349 my $w = Gtk2::Window->new;
194 350
195 my @cfg = ( 351 my @cfg = (
196 [qw/Host host/], 352 [qw/Host host/],
197 [qw/Port port/], 353 [qw/Port port/],
354 [qw/Mapsize% mapsize/],
198 [qw/Username user/], 355 [qw/Username user/],
199 [qw/Password password/], 356 [qw/Password password/],
200 ); 357 );
201 358
202 my $cfg = {}; 359 my $cfg = {};
249 my $cb = $events{login} || sub {}; 406 my $cb = $events{login} || sub {};
250 $cb->($::CFG->{user}, $::CFG->{password}); 407 $cb->($::CFG->{user}, $::CFG->{password});
251 }); 408 });
252 $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5); 409 $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5);
253 $cb->signal_connect (clicked => sub { 410 $cb->signal_connect (clicked => sub {
254 my $cb = $events{login} || sub {}; 411 my $cb = $events{logout} || sub {};
255 $cb->(); 412 $cb->();
256 }); 413 });
257 $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5); 414 $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5);
258 $cb->signal_connect (clicked => sub { $w->destroy }); 415 $cb->signal_connect (clicked => sub { $w->destroy });
259 416
267 424
268SDL::Init SDL_INIT_EVERYTHING; 425SDL::Init SDL_INIT_EVERYTHING;
269 426
270my $mapwidget = Crossfire::Client::Widget::MapWidget->new; 427my $mapwidget = Crossfire::Client::Widget::MapWidget->new;
271 428
272$mapwidget->activate; 429$Crossfire::Client::Widget::TOPLEVEL->add ($mapwidget);
273$mapwidget->focus_in; 430$mapwidget->focus_in;
274 431
275Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; 432Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc";
276 433
277$CFG ||= { 434$CFG ||= {
278 width => 640, 435 width => 640,
279 height => 480, 436 height => 480,
437 mapsize => 100,
280 fullscreen => 0, 438 fullscreen => 0,
281 host => "crossfire.schmorp.de", 439 host => "crossfire.schmorp.de",
282 port => 13327, 440 port => 13327,
283}; 441};
284 442
443{
444 my @fonts = map Crossfire::Client::find_rcfile $_, qw(uifont.ttf uifontb.ttf uifonti.ttf uifontbi.ttf);
445
446 Crossfire::Client::add_font $_ for @fonts;
447 Crossfire::Client::set_font $fonts[0];
448}
449
285$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; 450$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 451
292run_config_dialog 452run_config_dialog
293 login => sub { start_game }, 453 login => sub { start_game },
294 logout => sub { stop_game }; 454 logout => sub { stop_game };
295 455

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines