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.47 by elmex, Sun Apr 9 18:28:23 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;
45 45our $STATUS_LINE;
46our $TOPLEVEL; 46our $DEBUG_STATUS;
47
48our $tw; # Test widget #d#
49 47
50my $last_refresh; 48my $last_refresh;
49my %ANIMATE;
50my $refresh_handler;
51
52our ($tw, $te); # Test widget #d#
51 53
52sub init_screen { 54sub init_screen {
53 $SDL_APP = new SDL::App 55 $SDL_APP = new SDL::App
54 -flags => SDL_ANYFORMAT | SDL_HWSURFACE, 56 -flags => SDL_ANYFORMAT | SDL_HWSURFACE,
55 -title => "Crossfire+ Client", 57 -title => "Crossfire+ Client",
56 -width => $WIDTH, 58 -width => $WIDTH,
57 -height => $HEIGHT, 59 -height => $HEIGHT,
58 -opengl => 1, 60 -opengl => 1,
59 -red_size => 8, 61 -red_size => 5,
60 -green_size => 8, 62 -green_size => 5,
61 -blue_size => 8, 63 -blue_size => 5,
64 -alpha_size => 0,
62 -double_buffer => 1, 65 -double_buffer => 1,
63 -fullscreen => $FULLSCREEN, 66 -fullscreen => $FULLSCREEN,
64 -resizeable => 0; 67 -resizeable => 0;
65 68
69 $SDL_EV = new SDL::Event;
70 $SDL_EV->set_unicode (1);
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
66 $last_refresh = SDL::GetTicks; 79 $last_refresh = SDL::GetTicks;
67 80
68 %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions; 81 CFClient::gl_init;
69
70 $GL_EXT{GL_ARB_texture_non_power_of_two}
71 or warn "WARNING: non-power-of-two opengl extension required";
72 82
73 $FONTSIZE = int $HEIGHT / 50; 83 $FONTSIZE = int $HEIGHT / 50;
74 84
75 $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label 85 #############################################################################
76 0, $FONTSIZE, $HEIGHT - $FONTSIZE, $FONTSIZE,
77 "Use <b>Alt-Enter</b> to toggle fullscreen mode";
78 $ALT_ENTER_MESSAGE->move (0, $HEIGHT - ($ALT_ENTER_MESSAGE->size_request)[1]);
79 $TOPLEVEL->add ($ALT_ENTER_MESSAGE);
80 86
81 # Test code #d#
82 unless ($tw) { # haha...
83 $tw = new Crossfire::Client::Widget::Animator;
84 my $lbl1 = new Crossfire::Client::Widget::Label
85 0, 0, 10, $FONTSIZE, "<i>This</i> is a\n<u>TEST</u>!\nOf a themed\nFrame!";
86 my $lbl2 = new Crossfire::Client::Widget::Label
87 0, 0, 10, $FONTSIZE, "LBL2";
88
89 my $vb = new Crossfire::Client::Widget::VBox;
90 my $f = new Crossfire::Client::Widget::FancyFrame;
91 my $f2 = new Crossfire::Client::Widget::FancyFrame;
92 $f->add ($lbl1);
93 $f2->add ($lbl2);
94 $vb->add ($f);
95 $vb->add ($f2, 1);
96
97 $tw->add ($vb);
98 $tw->w (400);
99 $tw->h (300);
100 $tw->move ($WIDTH - 200, 0);
101 $tw->moveto (0, 0);
102 $TOPLEVEL->add ($tw);
103
104# $f->move ($WIDTH - 200, 0);
105# $TOPLEVEL->add ($f);
106 }
107
108 glClearColor 0, 0, 0, 0; 87 glClearColor 0.45, 0.45, 0.45, 1;
109 88
110 glEnable GL_TEXTURE_2D; 89 glEnable GL_TEXTURE_2D;
111 glEnable GL_COLOR_MATERIAL; 90 glEnable GL_COLOR_MATERIAL;
112 glShadeModel GL_FLAT; 91 glShadeModel GL_FLAT;
113 glDisable GL_DEPTH_TEST; 92 glDisable GL_DEPTH_TEST;
114 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 93 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
115 94
116 $_->() 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;
117} 145}
118 146
119sub start_game { 147sub start_game {
120 $SDL_TIMER = add Glib::Timeout 1000/100, sub {
121 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
122 while $SDL_EV->poll;
123
124 1
125 };
126
127 $WIDTH = $CFG->{width}; 148 $WIDTH = $CFG->{width};
128 $HEIGHT = $CFG->{height}; 149 $HEIGHT = $CFG->{height};
129 $FULLSCREEN = 0; 150 $FULLSCREEN = 0;
130 151
131 init_screen; 152 init_screen;
153
154 my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32;
132 155
133 $CONN = new conn 156 $CONN = new conn
134 host => $CFG->{host}, 157 host => $CFG->{host},
135 port => $CFG->{port}, 158 port => $CFG->{port},
136 user => $CFG->{user}, 159 user => $CFG->{user},
137 pass => $CFG->{password}, 160 pass => $CFG->{password},
138 mapw => 50, 161 mapw => $mapsize,
139 maph => 37, 162 maph => $mapsize,
140 ; 163 ;
141 164
142 Crossfire::Client::lowdelay fileno $CONN->{fh}; 165 CFClient::lowdelay fileno $CONN->{fh};
143} 166}
144 167
145sub stop_game { 168sub stop_game {
146 remove Glib::Source $SDL_TIMER; 169 remove Glib::Source $refresh_handler if $refresh_handler;
170 undef $refresh_handler;
147 171
148 undef $SDL_APP; 172 undef $CONN;
149 SDL::Quit; 173 destroy_screen;
150} 174}
151
152 175
153sub force_refresh { 176sub force_refresh {
154 glViewport 0, 0, $WIDTH, $HEIGHT; 177 glViewport 0, 0, $WIDTH, $HEIGHT;
155 178
156 glMatrixMode GL_PROJECTION; 179 glMatrixMode GL_PROJECTION;
157 glLoadIdentity; 180 glLoadIdentity;
158 glOrtho 0, $WIDTH, $HEIGHT, 0, -6000 , 6000; 181 glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000;
159 glMatrixMode GL_MODELVIEW; 182 glMatrixMode GL_MODELVIEW;
160 183
161 glClear GL_COLOR_BUFFER_BIT; 184 glClear GL_COLOR_BUFFER_BIT;
162 185
163 $TOPLEVEL->draw; 186 $CFClient::Widget::TOPLEVEL->draw;
164 187
165 SDL::GLSwapBuffers; 188 SDL::GLSwapBuffers;
166} 189}
167 190
168my %ANIMATE; 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}
169 196
170my $refresh_handler; 197my $FPS;
171 198
172sub refresh { 199sub refresh {
173 $refresh_handler ||= add Glib::Idle sub { 200 $refresh_handler ||= add Glib::Idle sub {
201 if ($SDL_APP) {
174 my $next_refresh = SDL::GetTicks; 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
175 my $interval = ($next_refresh - $last_refresh) * 0.001; 209 my $interval = ($next_refresh - $last_refresh) * 0.001;
176 $last_refresh = $next_refresh; 210 $last_refresh = $next_refresh;
177 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
178 force_refresh; 218 force_refresh;
179 $_->animate ($interval) for grep $_, values %ANIMATE; 219 $_->animate ($interval) for grep $_, values %ANIMATE;
180 220
181 if (%ANIMATE) { 221 if (%ANIMATE) {
222 1
223 } else {
224 undef $refresh_handler;
225 0
182 1 226 }
183 } else { 227 } else {
184 undef $refresh_handler; 228 undef $refresh_handler;
185 0 229 0
186 } 230 }
187 }; 231 };
213 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) {
214 # alt-enter 258 # alt-enter
215 $FULLSCREEN = !$FULLSCREEN; 259 $FULLSCREEN = !$FULLSCREEN;
216 init_screen; 260 init_screen;
217 } else { 261 } else {
218 Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV); 262 CFClient::Widget::feed_sdl_key_down_event ($SDL_EV);
219 } 263 }
220 }, 264 },
221 SDL_KEYUP() => sub { 265 SDL_KEYUP() => sub {
222 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); 266 CFClient::Widget::feed_sdl_key_up_event ($SDL_EV);
223 }, 267 },
224 SDL_MOUSEMOTION() => sub { 268 SDL_MOUSEMOTION() => sub {
225 warn "sdl motion\n";#d# 269 CFClient::Widget::feed_sdl_motion_event ($SDL_EV);
226 }, 270 },
227 SDL_MOUSEBUTTONDOWN() => sub { 271 SDL_MOUSEBUTTONDOWN() => sub {
228 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); 272 CFClient::Widget::feed_sdl_button_down_event ($SDL_EV);
229 }, 273 },
230 SDL_MOUSEBUTTONUP() => sub { 274 SDL_MOUSEBUTTONUP() => sub {
231 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV); 275 CFClient::Widget::feed_sdl_button_up_event ($SDL_EV);
232 }, 276 },
233 SDL_ACTIVEEVENT() => sub { 277 SDL_ACTIVEEVENT() => sub {
234 warn "active\n";#d# 278 printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d#
235 }, 279 },
236); 280);
237 281
238@conn::ISA = Crossfire::Protocol::; 282@conn::ISA = Crossfire::Protocol::;
239 283
240sub conn::map_update { 284sub conn::map_update {
241 my ($self, $dirty) = @_; 285 my ($self, $dirty) = @_;
242 286
243 refresh; 287 $MAPWIDGET->update;
244} 288}
245 289
246sub conn::map_scroll { 290sub conn::map_scroll {
247 my ($self, $dx, $dy) = @_; 291 my ($self, $dx, $dy) = @_;
248 292
264sub conn::face_update { 308sub conn::face_update {
265 my ($self, $face) = @_; 309 my ($self, $face) = @_;
266 310
267 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; 311 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
268 312
269 $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image}; 313 $face->{texture} = new_from_image CFClient::Texture delete $face->{image};
270} 314}
271 315
272sub conn::query { 316sub conn::query {
273 my ($self, $flags, $prompt) = @_; 317 my ($self, $flags, $prompt) = @_;
274 318
286 } 330 }
287 $ent->set_text ($value); 331 $ent->set_text ($value);
288 $ent->signal_connect (changed => sub { 332 $ent->signal_connect (changed => sub {
289 my ($ent) = @_; 333 my ($ent) = @_;
290 $cfg->{$key} = $ent->get_text; 334 $cfg->{$key} = $ent->get_text;
335 # TODO: Mapsize should be a slider in the game gui
291 if ($key eq 'mapsize' and $cfg->{$key} > 100) { 336 if ($key eq 'mapsize' and $cfg->{$key} > 100) {
292 $cfg->{$key} = 100; 337 $cfg->{$key} = 100;
293 $ent->set_text ('100');
294 } elsif ($key eq 'mapsize' and $cfg->{$key} < 0) { 338 } elsif ($key eq 'mapsize' and $cfg->{$key} < 50) {
295 $cfg->{$key} = 0; 339 $cfg->{$key} = 50;
296 $ent->set_text ('0');
297 } 340 }
298 }); 341 });
299} 342}
300 343
301sub run_config_dialog { 344sub run_config_dialog {
348 $cb->signal_connect (clicked => sub { 391 $cb->signal_connect (clicked => sub {
349 for (keys %$cfg) { 392 for (keys %$cfg) {
350 $::CFG->{$_} = $cfg->{$_} 393 $::CFG->{$_} = $cfg->{$_}
351 if $_ ne '_i'; 394 if $_ ne '_i';
352 } 395 }
353 Crossfire::Client::write_cfg "$Crossfire::VARDIR/pclientrc"; 396 CFClient::write_cfg "$CFrossfire::VARDIR/pclientrc";
354 }); 397 });
355 $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);
356 $cb->signal_connect (clicked => sub { 399 $cb->signal_connect (clicked => sub {
357 for (keys %$cfg) { 400 for (keys %$cfg) {
358 $::CFG->{$_} = $cfg->{$_} 401 $::CFG->{$_} = $cfg->{$_}
361 my $cb = $events{login} || sub {}; 404 my $cb = $events{login} || sub {};
362 $cb->($::CFG->{user}, $::CFG->{password}); 405 $cb->($::CFG->{user}, $::CFG->{password});
363 }); 406 });
364 $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);
365 $cb->signal_connect (clicked => sub { 408 $cb->signal_connect (clicked => sub {
366 my $cb = $events{login} || sub {}; 409 my $cb = $events{logout} || sub {};
367 $cb->(); 410 $cb->();
368 }); 411 });
369 $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);
370 $cb->signal_connect (clicked => sub { $w->destroy }); 413 $cb->signal_connect (clicked => sub { $w->destroy });
371 414
377 420
378############################################################################# 421#############################################################################
379 422
380SDL::Init SDL_INIT_EVERYTHING; 423SDL::Init SDL_INIT_EVERYTHING;
381 424
382$TOPLEVEL = Crossfire::Client::Widget::Toplevel->new;
383
384my $mapwidget = Crossfire::Client::Widget::MapWidget->new;
385
386$TOPLEVEL->add ($mapwidget);
387$mapwidget->focus_in;
388
389Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; 425CFClient::read_cfg "$Crossfire::VARDIR/pclientrc";
390 426
391$CFG ||= { 427$CFG ||= {
392 width => 640, 428 width => 640,
393 height => 480, 429 height => 480,
394 mapsize => 100, 430 mapsize => 100,
395 fullscreen => 0, 431 fullscreen => 0,
396 host => "crossfire.schmorp.de", 432 host => "crossfire.schmorp.de",
397 port => 13327, 433 port => 13327,
398}; 434};
399 435
400Crossfire::Client::set_font Crossfire::Client::find_rcfile "uifont.ttf"; 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}
401 442
402$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; 443$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
403 444
404run_config_dialog 445run_config_dialog
405 login => sub { start_game }, 446 login => sub { start_game },

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines