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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines