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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines