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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines