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.37 by root, Sat Apr 8 22:25:37 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 $UIFONT; 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
50my $last_refresh;
51my %ANIMATE;
52my $refresh_handler;
53
46our $tw; # Test widget #d# 54our ($tw, $te); # Test widget #d#
47 55
48sub init_screen { 56sub init_screen {
49 $SDL_APP = new SDL::App 57 $SDL_APP = new SDL::App
50 -flags => SDL_ANYFORMAT | SDL_HWSURFACE, 58 -flags => SDL_ANYFORMAT | SDL_HWSURFACE,
51 -title => "Crossfire+ Client", 59 -title => "Crossfire+ Client",
52 -width => $WIDTH, 60 -width => $WIDTH,
53 -height => $HEIGHT, 61 -height => $HEIGHT,
54 -opengl => 1, 62 -opengl => 1,
55 -red_size => 8, 63 -red_size => 5,
56 -green_size => 8, 64 -green_size => 5,
57 -blue_size => 8, 65 -blue_size => 5,
66 -alpha_size => 0,
58 -double_buffer => 1, 67 -double_buffer => 1,
59 -fullscreen => $FULLSCREEN, 68 -fullscreen => $FULLSCREEN,
60 -resizeable => 0; 69 -resizeable => 0;
61 70
62 %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions; 71 $SDL_EV = new SDL::Event;
72 $SDL_EV->set_unicode (1);
63 73
64 $GL_EXT{GL_ARB_texture_non_power_of_two} 74 $SDL_TIMER = add Glib::Timeout 1000/50, sub {
65 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;
66 77
67 $UIFONT = SDL::TTFOpenFont Crossfire::Client::find_rcfile "uifont.ttf", $HEIGHT / 40 78 1
68 or die "TTFOpenFont: $!"; 79 };
69 80
70 $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label 0, $HEIGHT - $HEIGHT / 40, 10, $UIFONT, "Alt-Enter toggles fullscreen mode"; 81 $last_refresh = SDL::GetTicks;
71 $ALT_ENTER_MESSAGE->move (0, $HEIGHT - ($ALT_ENTER_MESSAGE->size_request)[1]);
72 $ALT_ENTER_MESSAGE->activate;
73 82
74 # Test code #d# 83 CFClient::gl_init;
75 my $frm = new Crossfire::Client::Widget::Frame;
76 $tw = new Crossfire::Client::Widget::Window;
77 $frm->add (new Crossfire::Client::Widget::Label 0, 0, 10, $UIFONT, "Foo in the garden!");
78 $tw->add ($frm);
79 84
80 $tw->move (0, $HEIGHT - 120); 85 $FONTSIZE = int $HEIGHT / 50;
81 $tw->activate;
82 # Test code end #d#
83 86
87 #############################################################################
88
84 glClearColor 0, 0, 0, 0; 89 glClearColor 0.45, 0.45, 0.45, 1;
85 90
86 glEnable GL_TEXTURE_2D; 91 glEnable GL_TEXTURE_2D;
87 glEnable GL_COLOR_MATERIAL; 92 glEnable GL_COLOR_MATERIAL;
88 glShadeModel GL_FLAT; 93 glShadeModel GL_FLAT;
89 glDisable GL_DEPTH_TEST; 94 glDisable GL_DEPTH_TEST;
90 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 95 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
91 96
92 $_->() 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;
93} 153}
94 154
95sub start_game { 155sub start_game {
96 $SDL_TIMER = add Glib::Timeout 1000/100, sub {
97 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
98 while $SDL_EV->poll;
99
100 1
101 };
102
103 $WIDTH = $CFG->{width}; 156 $WIDTH = $CFG->{width};
104 $HEIGHT = $CFG->{height}; 157 $HEIGHT = $CFG->{height};
105 $FULLSCREEN = 0; 158 $FULLSCREEN = 0;
106 159
107 init_screen; 160 init_screen;
161
162 my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32;
108 163
109 $CONN = new conn 164 $CONN = new conn
110 host => $CFG->{host}, 165 host => $CFG->{host},
111 port => $CFG->{port}, 166 port => $CFG->{port},
112 user => $CFG->{user}, 167 user => $CFG->{user},
113 pass => $CFG->{password}, 168 pass => $CFG->{password},
114 mapw => 50, 169 mapw => $mapsize,
115 maph => 37, 170 maph => $mapsize,
116 ; 171 ;
172
173 CFClient::lowdelay fileno $CONN->{fh};
117} 174}
118 175
119sub stop_game { 176sub stop_game {
120 remove Glib::Source $SDL_TIMER; 177 remove Glib::Source $refresh_handler if $refresh_handler;
178 undef $refresh_handler;
121 179
122 undef $SDL_APP; 180 undef $CONN;
123 SDL::Quit; 181 destroy_screen;
124} 182}
125
126 183
127sub force_refresh { 184sub force_refresh {
128 glViewport 0, 0, $WIDTH, $HEIGHT; 185 glViewport 0, 0, $WIDTH, $HEIGHT;
129 186
130 glMatrixMode GL_PROJECTION; 187 glMatrixMode GL_PROJECTION;
131 glLoadIdentity; 188 glLoadIdentity;
132 glOrtho 0, $WIDTH, $HEIGHT, 0, -100 , 100; 189 glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000;
133 glMatrixMode GL_MODELVIEW; 190 glMatrixMode GL_MODELVIEW;
134 191
135 glClear GL_COLOR_BUFFER_BIT; 192 glClear GL_COLOR_BUFFER_BIT;
136 193
137 $_->draw for @Crossfire::Client::Widget::ACTIVE_WIDGETS; 194 $CFClient::Widget::TOPLEVEL->draw;
138 195
139 SDL::GLSwapBuffers; 196 SDL::GLSwapBuffers;
140} 197}
141 198
142my $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;
143 206
144sub refresh { 207sub refresh {
145 $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
146 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 {
147 undef $refresh_handler; 236 undef $refresh_handler;
237 0
148 0 238 }
149 }; 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};
150} 253}
151 254
152%SDL_CB = ( 255%SDL_CB = (
153 SDL_QUIT() => sub { 256 SDL_QUIT() => sub {
154 main_quit Gtk2; 257 main_quit Gtk2;
162 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) {
163 # alt-enter 266 # alt-enter
164 $FULLSCREEN = !$FULLSCREEN; 267 $FULLSCREEN = !$FULLSCREEN;
165 init_screen; 268 init_screen;
166 } else { 269 } else {
167 Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV); 270 CFClient::Widget::feed_sdl_key_down_event ($SDL_EV);
168 } 271 }
169 }, 272 },
170 SDL_KEYUP() => sub { 273 SDL_KEYUP() => sub {
171 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); 274 CFClient::Widget::feed_sdl_key_up_event ($SDL_EV);
172 }, 275 },
173 SDL_MOUSEMOTION() => sub { 276 SDL_MOUSEMOTION() => sub {
174 warn "sdl motion\n";#d# 277 CFClient::Widget::feed_sdl_motion_event ($SDL_EV);
175 }, 278 },
176 SDL_MOUSEBUTTONDOWN() => sub { 279 SDL_MOUSEBUTTONDOWN() => sub {
177 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); 280 CFClient::Widget::feed_sdl_button_down_event ($SDL_EV);
178 }, 281 },
179 SDL_MOUSEBUTTONUP() => sub { 282 SDL_MOUSEBUTTONUP() => sub {
180 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV); 283 CFClient::Widget::feed_sdl_button_up_event ($SDL_EV);
181 }, 284 },
182 SDL_ACTIVEEVENT() => sub { 285 SDL_ACTIVEEVENT() => sub {
183 warn "active\n";#d# 286# printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d#
184 }, 287 },
185); 288);
186 289
187@conn::ISA = Crossfire::Protocol::; 290@conn::ISA = Crossfire::Protocol::;
188 291
189sub conn::map_update { 292sub conn::map_update {
190 my ($self, $dirty) = @_; 293 my ($self, $dirty) = @_;
191 294
192 refresh; 295 $MAPWIDGET->update;
193} 296}
194 297
195sub conn::map_scroll { 298sub conn::map_scroll {
196 my ($self, $dx, $dy) = @_; 299 my ($self, $dx, $dy) = @_;
197 300
198 refresh; 301# refresh;
199} 302}
200 303
201sub conn::map_clear { 304sub conn::map_clear {
202 my ($self) = @_; 305 my ($self) = @_;
203 306
204 refresh; 307# refresh;
205} 308}
206 309
207sub conn::face_find { 310sub conn::face_find {
208 my ($self, $face) = @_; 311 my ($self, $face) = @_;
209 312
213sub conn::face_update { 316sub conn::face_update {
214 my ($self, $face) = @_; 317 my ($self, $face) = @_;
215 318
216 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; 319 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
217 320
218 $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image}; 321 $face->{texture} = new_from_image CFClient::Texture delete $face->{image};
219} 322}
220 323
221sub conn::query { 324sub conn::query {
222 my ($self, $flags, $prompt) = @_; 325 my ($self, $flags, $prompt) = @_;
223 326
235 } 338 }
236 $ent->set_text ($value); 339 $ent->set_text ($value);
237 $ent->signal_connect (changed => sub { 340 $ent->signal_connect (changed => sub {
238 my ($ent) = @_; 341 my ($ent) = @_;
239 $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 }
240 }); 349 });
241} 350}
242 351
243sub run_config_dialog { 352sub run_config_dialog {
244 my (%events) = @_; 353 my (%events) = @_;
246 my $w = Gtk2::Window->new; 355 my $w = Gtk2::Window->new;
247 356
248 my @cfg = ( 357 my @cfg = (
249 [qw/Host host/], 358 [qw/Host host/],
250 [qw/Port port/], 359 [qw/Port port/],
360 [qw/Mapsize% mapsize/],
251 [qw/Username user/], 361 [qw/Username user/],
252 [qw/Password password/], 362 [qw/Password password/],
253 ); 363 );
254 364
255 my $cfg = {}; 365 my $cfg = {};
289 $cb->signal_connect (clicked => sub { 399 $cb->signal_connect (clicked => sub {
290 for (keys %$cfg) { 400 for (keys %$cfg) {
291 $::CFG->{$_} = $cfg->{$_} 401 $::CFG->{$_} = $cfg->{$_}
292 if $_ ne '_i'; 402 if $_ ne '_i';
293 } 403 }
294 Crossfire::Client::write_cfg "$Crossfire::VARDIR/pclientrc"; 404 CFClient::write_cfg "$CFrossfire::VARDIR/pclientrc";
295 }); 405 });
296 $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);
297 $cb->signal_connect (clicked => sub { 407 $cb->signal_connect (clicked => sub {
298 for (keys %$cfg) { 408 for (keys %$cfg) {
299 $::CFG->{$_} = $cfg->{$_} 409 $::CFG->{$_} = $cfg->{$_}
302 my $cb = $events{login} || sub {}; 412 my $cb = $events{login} || sub {};
303 $cb->($::CFG->{user}, $::CFG->{password}); 413 $cb->($::CFG->{user}, $::CFG->{password});
304 }); 414 });
305 $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);
306 $cb->signal_connect (clicked => sub { 416 $cb->signal_connect (clicked => sub {
307 my $cb = $events{login} || sub {}; 417 my $cb = $events{logout} || sub {};
308 $cb->(); 418 $cb->();
309 }); 419 });
310 $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);
311 $cb->signal_connect (clicked => sub { $w->destroy }); 421 $cb->signal_connect (clicked => sub { $w->destroy });
312 422
317 427
318 428
319############################################################################# 429#############################################################################
320 430
321SDL::Init SDL_INIT_EVERYTHING; 431SDL::Init SDL_INIT_EVERYTHING;
322SDL::TTFInit;
323 432
324my $mapwidget = Crossfire::Client::Widget::MapWidget->new;
325
326$mapwidget->activate;
327$mapwidget->focus_in;
328
329Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; 433CFClient::read_cfg "$Crossfire::VARDIR/pclientrc";
330 434
331$CFG ||= { 435$CFG ||= {
332 width => 640, 436 width => 640,
333 height => 480, 437 height => 480,
438 mapsize => 100,
334 fullscreen => 0, 439 fullscreen => 0,
335 host => "crossfire.schmorp.de", 440 host => "crossfire.schmorp.de",
336 port => 13327, 441 port => 13327,
337}; 442};
338 443
444{
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}
450
339$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; 451$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
340 452
341run_config_dialog 453run_config_dialog
342 login => sub { start_game }, 454 login => sub { start_game },
343 logout => sub { stop_game }; 455 logout => sub { stop_game };

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines