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.27 by root, Fri Apr 7 21:07:29 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
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
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;
145}
146
147sub start_game {
148 $WIDTH = $CFG->{width};
149 $HEIGHT = $CFG->{height};
150 $FULLSCREEN = 0;
151
152 init_screen;
153
154 my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32;
155
156 $CONN = new conn
157 host => $CFG->{host},
158 port => $CFG->{port},
159 user => $CFG->{user},
160 pass => $CFG->{password},
161 mapw => $mapsize,
162 maph => $mapsize,
163 ;
164
165 CFClient::lowdelay fileno $CONN->{fh};
166}
167
168sub stop_game {
169 remove Glib::Source $refresh_handler if $refresh_handler;
170 undef $refresh_handler;
171
172 undef $CONN;
173 destroy_screen;
174}
175
176sub force_refresh {
177 glViewport 0, 0, $WIDTH, $HEIGHT;
178
63 glMatrixMode GL_PROJECTION; 179 glMatrixMode GL_PROJECTION;
64 glLoadIdentity; 180 glLoadIdentity;
65 glOrtho 0, $CFG->{width}, $CFG->{height}, 0, -100 , 100; 181 glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000;
66
67 glMatrixMode GL_MODELVIEW; 182 glMatrixMode GL_MODELVIEW;
68 183
69 $_->() for @GL_INIT; 184 glClear GL_COLOR_BUFFER_BIT;
70}
71 185
72my $label;#d# 186 $CFClient::Widget::TOPLEVEL->draw;
73 187
74sub start_game { 188 SDL::GLSwapBuffers;
75 $SDL_TIMER = add Glib::Timeout 1000/20, sub { 189}
76 while ($SDL_EV->poll) { 190
77 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->(); 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;
198
199sub refresh {
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
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 {
228 undef $refresh_handler;
229 0
78 } 230 }
79
80 1
81 }; 231 };
82
83 init_screen;
84
85 $label = new Crossfire::Client::Widget::Label 500, 10, 1, $UIFONT, "Testü[]";
86 $label->activate;
87
88 $CONN = new conn
89 host => $CFG->{host},
90 port => $CFG->{port};
91} 232}
92 233
93sub stop_game { 234sub animation_start {
94 remove Glib::Source $SDL_TIMER; 235 my ($widget) = @_;
236 $ANIMATE{$widget} = $widget;
237 Scalar::Util::weaken $ANIMATE{$widget};
95 238
96 undef $SDL_APP; 239 refresh;
97 SDL::Quit;
98} 240}
99 241
100sub refresh { 242sub animation_stop {
101 glClear GL_COLOR_BUFFER_BIT; 243 my ($widget) = @_;
102 244 delete $ANIMATE{$widget};
103 $_->draw for @Crossfire::Client::Widget::ACTIVE_WIDGETS;
104
105 SDL::GLSwapBuffers;
106} 245}
107 246
108%SDL_CB = ( 247%SDL_CB = (
109 SDL_QUIT() => sub { 248 SDL_QUIT() => sub {
110 main_quit Gtk2; 249 main_quit Gtk2;
115 refresh; 254 refresh;
116 }, 255 },
117 SDL_KEYDOWN() => sub { 256 SDL_KEYDOWN() => sub {
118 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) {
119 # alt-enter 258 # alt-enter
120 $CFG->{fullscreen} = !$CFG->{fullscreen}; 259 $FULLSCREEN = !$FULLSCREEN;
121 init_screen; 260 init_screen;
122 } else { 261 } else {
123 Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV); 262 CFClient::Widget::feed_sdl_key_down_event ($SDL_EV);
124 } 263 }
125 }, 264 },
126 SDL_KEYUP() => sub { 265 SDL_KEYUP() => sub {
127 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); 266 CFClient::Widget::feed_sdl_key_up_event ($SDL_EV);
128 }, 267 },
129 SDL_MOUSEMOTION() => sub { 268 SDL_MOUSEMOTION() => sub {
130 warn "sdl motion\n";#d# 269 CFClient::Widget::feed_sdl_motion_event ($SDL_EV);
131 }, 270 },
132 SDL_MOUSEBUTTONDOWN() => sub { 271 SDL_MOUSEBUTTONDOWN() => sub {
133 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); 272 CFClient::Widget::feed_sdl_button_down_event ($SDL_EV);
134 }, 273 },
135 SDL_MOUSEBUTTONUP() => sub { 274 SDL_MOUSEBUTTONUP() => sub {
136 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV); 275 CFClient::Widget::feed_sdl_button_up_event ($SDL_EV);
137 }, 276 },
138 SDL_ACTIVEEVENT() => sub { 277 SDL_ACTIVEEVENT() => sub {
139 warn "active\n";#d# 278 printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d#
140 }, 279 },
141); 280);
142 281
143@conn::ISA = Crossfire::Protocol::; 282@conn::ISA = Crossfire::Protocol::;
144 283
145sub conn::map_update { 284sub conn::map_update {
146 my ($self, $dirty) = @_; 285 my ($self, $dirty) = @_;
147 286
148 refresh; 287 $MAPWIDGET->update;
149} 288}
150 289
151sub conn::map_scroll { 290sub conn::map_scroll {
152 my ($self, $dx, $dy) = @_; 291 my ($self, $dx, $dy) = @_;
153 292
154 refresh; 293# refresh;
155} 294}
156 295
157sub conn::map_clear { 296sub conn::map_clear {
158 my ($self) = @_; 297 my ($self) = @_;
159 298
160 refresh; 299# refresh;
161} 300}
162 301
163sub conn::face_find { 302sub conn::face_find {
164 my ($self, $face) = @_; 303 my ($self, $face) = @_;
165 304
169sub conn::face_update { 308sub conn::face_update {
170 my ($self, $face) = @_; 309 my ($self, $face) = @_;
171 310
172 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; 311 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
173 312
174 $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image}; 313 $face->{texture} = new_from_image CFClient::Texture delete $face->{image};
314}
315
316sub conn::query {
317 my ($self, $flags, $prompt) = @_;
318
319 warn "<<<<QUERY:$flags:$prompt>>>\n";#d#
175} 320}
176 321
177sub gtk_add_cfg_field { 322sub gtk_add_cfg_field {
178 my ($tbl, $cfg, $klbl, $key, $value) = @_; 323 my ($tbl, $cfg, $klbl, $key, $value) = @_;
179 my $i = $cfg->{_i}++; 324 my $i = $cfg->{_i}++;
185 } 330 }
186 $ent->set_text ($value); 331 $ent->set_text ($value);
187 $ent->signal_connect (changed => sub { 332 $ent->signal_connect (changed => sub {
188 my ($ent) = @_; 333 my ($ent) = @_;
189 $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 }
190 }); 341 });
191} 342}
192 343
193sub run_config_dialog { 344sub run_config_dialog {
194 my (%events) = @_; 345 my (%events) = @_;
196 my $w = Gtk2::Window->new; 347 my $w = Gtk2::Window->new;
197 348
198 my @cfg = ( 349 my @cfg = (
199 [qw/Host host/], 350 [qw/Host host/],
200 [qw/Port port/], 351 [qw/Port port/],
352 [qw/Mapsize% mapsize/],
201 [qw/Username user/], 353 [qw/Username user/],
202 [qw/Password password/], 354 [qw/Password password/],
203 ); 355 );
204 356
205 my $cfg = {}; 357 my $cfg = {};
239 $cb->signal_connect (clicked => sub { 391 $cb->signal_connect (clicked => sub {
240 for (keys %$cfg) { 392 for (keys %$cfg) {
241 $::CFG->{$_} = $cfg->{$_} 393 $::CFG->{$_} = $cfg->{$_}
242 if $_ ne '_i'; 394 if $_ ne '_i';
243 } 395 }
244 Crossfire::Client::write_cfg "$Crossfire::VARDIR/pclientrc"; 396 CFClient::write_cfg "$CFrossfire::VARDIR/pclientrc";
245 }); 397 });
246 $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);
247 $cb->signal_connect (clicked => sub { 399 $cb->signal_connect (clicked => sub {
248 for (keys %$cfg) { 400 for (keys %$cfg) {
249 $::CFG->{$_} = $cfg->{$_} 401 $::CFG->{$_} = $cfg->{$_}
252 my $cb = $events{login} || sub {}; 404 my $cb = $events{login} || sub {};
253 $cb->($::CFG->{user}, $::CFG->{password}); 405 $cb->($::CFG->{user}, $::CFG->{password});
254 }); 406 });
255 $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);
256 $cb->signal_connect (clicked => sub { 408 $cb->signal_connect (clicked => sub {
257 my $cb = $events{login} || sub {}; 409 my $cb = $events{logout} || sub {};
258 $cb->(); 410 $cb->();
259 }); 411 });
260 $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);
261 $cb->signal_connect (clicked => sub { $w->destroy }); 413 $cb->signal_connect (clicked => sub { $w->destroy });
262 414
267 419
268 420
269############################################################################# 421#############################################################################
270 422
271SDL::Init SDL_INIT_EVERYTHING; 423SDL::Init SDL_INIT_EVERYTHING;
272SDL::TTFInit;
273 424
274my $mapwidget = Crossfire::Client::Widget::MapWidget->new;
275
276$mapwidget->activate;
277$mapwidget->focus_in;
278
279Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; 425CFClient::read_cfg "$Crossfire::VARDIR/pclientrc";
280 426
281$CFG ||= { 427$CFG ||= {
282 width => 640, 428 width => 640,
283 height => 480, 429 height => 480,
430 mapsize => 100,
284 fullscreen => 0, 431 fullscreen => 0,
285 host => "crossfire.schmorp.de", 432 host => "crossfire.schmorp.de",
286 port => 13327, 433 port => 13327,
287}; 434};
288 435
436{
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}
442
289$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; 443$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
290 444
291run_config_dialog 445run_config_dialog
292 login => sub { start_game }, 446 login => sub { start_game },
293 logout => sub { stop_game }; 447 logout => sub { stop_game };

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines