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.41 by elmex, Sun Apr 9 01:21:11 2006 UTC vs.
Revision 1.71 by root, Tue Apr 11 17:02:36 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines