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.60 by root, Mon Apr 10 11:56:28 2006 UTC vs.
Revision 1.69 by root, Tue Apr 11 14:04:27 2006 UTC

14use SDL::OpenGL::Constants; 14use SDL::OpenGL::Constants;
15 15
16use Crossfire; 16use Crossfire;
17use Crossfire::Protocol; 17use Crossfire::Protocol;
18 18
19use Crossfire::Client; 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; 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;
45our $STATUS_LINE; 45our $STATUS_LINE;
46our $DEBUG_STATUS;
46 47
47my $last_refresh; 48my $last_refresh;
48my %ANIMATE; 49my %ANIMATE;
49my $refresh_handler; 50my $refresh_handler;
50 51
66 -resizeable => 0; 67 -resizeable => 0;
67 68
68 $SDL_EV = new SDL::Event; 69 $SDL_EV = new SDL::Event;
69 $SDL_EV->set_unicode (1); 70 $SDL_EV->set_unicode (1);
70 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
71 $last_refresh = SDL::GetTicks; 79 $last_refresh = SDL::GetTicks;
72 80
73 %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions; 81 CFClient::gl_init;
74
75 $GL_EXT{GL_ARB_texture_non_power_of_two}
76 or warn "WARNING: non-power-of-two opengl extension required";
77 82
78 $FONTSIZE = int $HEIGHT / 50; 83 $FONTSIZE = int $HEIGHT / 50;
79 84
80 ############################################################################# 85 #############################################################################
81 86
82 glClearColor 0.5, 0.5, 0.5, 1; 87 glClearColor 0.45, 0.45, 0.45, 1;
83 88
84 glEnable GL_TEXTURE_2D; 89 glEnable GL_TEXTURE_2D;
85 glEnable GL_COLOR_MATERIAL; 90 glEnable GL_COLOR_MATERIAL;
86 glShadeModel GL_FLAT; 91 glShadeModel GL_FLAT;
87 glDisable GL_DEPTH_TEST; 92 glDisable GL_DEPTH_TEST;
88 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 93 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
89 94
90 $_->() for @GL_INIT;
91
92 ############################################################################# 95 #############################################################################
96
97 $DEBUG_STATUS = new CFClient::Widget::Label;
98 $CFClient::Widget::TOPLEVEL->add ($DEBUG_STATUS);
93 99
94 $STATUS_LINE = new Crossfire::Client::Widget::Label 100 $STATUS_LINE = new CFClient::Widget::Label
95 0, $HEIGHT * 59 / 60 - $FONTSIZE, 1, $FONTSIZE, 101 y => $HEIGHT * 59 / 60 - $FONTSIZE;
96 "";
97 $Crossfire::Client::Widget::TOPLEVEL->add ($STATUS_LINE); 102 $CFClient::Widget::TOPLEVEL->add ($STATUS_LINE);
98 103
99 $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label 104 $ALT_ENTER_MESSAGE = new CFClient::Widget::Label
100 0, $HEIGHT * 59 / 60, 1, $HEIGHT / 60, 105 y => $HEIGHT * 59 / 60,
106 height => $HEIGHT / 60,
101 "Use <b>Alt-Enter</b> to toggle fullscreen mode"; 107 text => "Use <b>Alt-Enter</b> to toggle fullscreen mode";
102 $Crossfire::Client::Widget::TOPLEVEL->add ($ALT_ENTER_MESSAGE); 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;
103 113
104 # Test code #d# 114 # Test code #d#
105 unless ($tw) { # haha... 115 unless ($tw) { # haha...
106 $te = new Crossfire::Client::Widget::FancyFrame; 116 $te = new CFClient::Widget::FancyFrame x => 300, z => 1;
107 $te->add (new Crossfire::Client::Widget::Entry); 117 $te->add (new CFClient::Widget::Entry);
108 $te->move (300, 0, 2);
109 $Crossfire::Client::Widget::TOPLEVEL->add ($te); 118 $CFClient::Widget::TOPLEVEL->add ($te);
110 119
111 $tw = new Crossfire::Client::Widget::Animator; 120 $tw = new CFClient::Widget::Animator x => $WIDTH - 200, w => 600, h => 300;
112 my $lbl1 = new Crossfire::Client::Widget::Label 121 my $lbl1 = new CFClient::Widget::Label text => "<i>This</i> is a\n<u>TEST</u>!\nOf a themed\nFrame!";
113 0, 0, 10, $FONTSIZE, "<i>This</i> is a\n<u>TEST</u>!\nOf a themed\nFrame!";
114 my $lbl2 = new Crossfire::Client::Widget::Label 122 my $lbl2 = new CFClient::Widget::Label text => "LBL2";
115 0, 0, 10, $FONTSIZE, "LBL2";
116
117 my $vb = new Crossfire::Client::Widget::VBox; 123 my $vb = new CFClient::Widget::VBox;
118 my $f = new Crossfire::Client::Widget::FancyFrame; 124 my $f = new CFClient::Widget::FancyFrame;
119 my $f2 = new Crossfire::Client::Widget::FancyFrame; 125 my $f2 = new CFClient::Widget::FancyFrame;
120 $f->add ($lbl1); 126 $f->add ($lbl1);
121 $f2->add ($lbl2); 127 $f2->add ($lbl2);
122 $vb->add ($f); 128 $vb->add ($f);
123 $vb->add ($f2, 1); 129 $vb->add ($f2, 1);
124 130
125 $tw->add ($vb); 131 $tw->add ($vb);
126 $tw->w (400);
127 $tw->h (300);
128 $tw->move ($WIDTH - 200, 0);
129 $tw->moveto (0, 0); 132 $tw->moveto (0, 0);
130 $Crossfire::Client::Widget::TOPLEVEL->add ($tw); 133 $CFClient::Widget::TOPLEVEL->add ($tw);
131 134
132# $f->move ($WIDTH - 200, 0); 135# $f->move ($WIDTH - 200, 0);
133# $Crossfire::Client::Widget::TOPLEVEL->add ($f); 136# $CFClient::Widget::TOPLEVEL->add ($f);
134 } 137 }
135} 138}
136 139
140sub destroy_screen {
141 remove Glib::Source $SDL_TIMER;
142 undef $SDL_APP;
143 undef $SDL_EV;
144 SDL::Quit;
145}
146
137sub start_game { 147sub start_game {
138 $SDL_TIMER = add Glib::Timeout 1000/50, sub {
139 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
140 while $SDL_EV->poll;
141
142 1
143 };
144
145 $WIDTH = $CFG->{width}; 148 $WIDTH = $CFG->{width};
146 $HEIGHT = $CFG->{height}; 149 $HEIGHT = $CFG->{height};
147 $FULLSCREEN = 0; 150 $FULLSCREEN = 0;
148 151
149 init_screen; 152 init_screen;
157 pass => $CFG->{password}, 160 pass => $CFG->{password},
158 mapw => $mapsize, 161 mapw => $mapsize,
159 maph => $mapsize, 162 maph => $mapsize,
160 ; 163 ;
161 164
162 Crossfire::Client::lowdelay fileno $CONN->{fh}; 165 CFClient::lowdelay fileno $CONN->{fh};
163} 166}
164 167
165sub stop_game { 168sub stop_game {
166 remove Glib::Source $SDL_TIMER;
167 remove Glib::Source $refresh_handler if $refresh_handler; 169 remove Glib::Source $refresh_handler if $refresh_handler;
168 undef $refresh_handler; 170 undef $refresh_handler;
169 171
170 undef $SDL_APP;
171 undef $CONN; 172 undef $CONN;
172 SDL::Quit; 173 destroy_screen;
173} 174}
174 175
175sub force_refresh { 176sub force_refresh {
176 glViewport 0, 0, $WIDTH, $HEIGHT; 177 glViewport 0, 0, $WIDTH, $HEIGHT;
177 178
180 glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000; 181 glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000;
181 glMatrixMode GL_MODELVIEW; 182 glMatrixMode GL_MODELVIEW;
182 183
183 glClear GL_COLOR_BUFFER_BIT; 184 glClear GL_COLOR_BUFFER_BIT;
184 185
185 $Crossfire::Client::Widget::TOPLEVEL->draw; 186 $CFClient::Widget::TOPLEVEL->draw;
186 187
187 SDL::GLSwapBuffers; 188 SDL::GLSwapBuffers;
188} 189}
190
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;
189 198
190sub refresh { 199sub refresh {
191 $refresh_handler ||= add Glib::Idle sub { 200 $refresh_handler ||= add Glib::Idle sub {
192 if ($SDL_APP) { 201 if ($SDL_APP) {
193 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
194 my $interval = ($next_refresh - $last_refresh) * 0.001; 209 my $interval = ($next_refresh - $last_refresh) * 0.001;
195 $last_refresh = $next_refresh; 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 }
196 217
197 force_refresh; 218 force_refresh;
198 $_->animate ($interval) for grep $_, values %ANIMATE; 219 $_->animate ($interval) for grep $_, values %ANIMATE;
199 220
200 if (%ANIMATE) { 221 if (%ANIMATE) {
236 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) {
237 # alt-enter 258 # alt-enter
238 $FULLSCREEN = !$FULLSCREEN; 259 $FULLSCREEN = !$FULLSCREEN;
239 init_screen; 260 init_screen;
240 } else { 261 } else {
241 Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV); 262 CFClient::Widget::feed_sdl_key_down_event ($SDL_EV);
242 } 263 }
243 }, 264 },
244 SDL_KEYUP() => sub { 265 SDL_KEYUP() => sub {
245 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); 266 CFClient::Widget::feed_sdl_key_up_event ($SDL_EV);
246 }, 267 },
247 SDL_MOUSEMOTION() => sub { 268 SDL_MOUSEMOTION() => sub {
248 Crossfire::Client::Widget::feed_sdl_motion_event ($SDL_EV); 269 CFClient::Widget::feed_sdl_motion_event ($SDL_EV);
249 }, 270 },
250 SDL_MOUSEBUTTONDOWN() => sub { 271 SDL_MOUSEBUTTONDOWN() => sub {
251 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); 272 CFClient::Widget::feed_sdl_button_down_event ($SDL_EV);
252 }, 273 },
253 SDL_MOUSEBUTTONUP() => sub { 274 SDL_MOUSEBUTTONUP() => sub {
254 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV); 275 CFClient::Widget::feed_sdl_button_up_event ($SDL_EV);
255 }, 276 },
256 SDL_ACTIVEEVENT() => sub { 277 SDL_ACTIVEEVENT() => sub {
257 printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d# 278 printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d#
258 }, 279 },
259); 280);
261@conn::ISA = Crossfire::Protocol::; 282@conn::ISA = Crossfire::Protocol::;
262 283
263sub conn::map_update { 284sub conn::map_update {
264 my ($self, $dirty) = @_; 285 my ($self, $dirty) = @_;
265 286
266 refresh; 287 $MAPWIDGET->update;
267} 288}
268 289
269sub conn::map_scroll { 290sub conn::map_scroll {
270 my ($self, $dx, $dy) = @_; 291 my ($self, $dx, $dy) = @_;
271 292
287sub conn::face_update { 308sub conn::face_update {
288 my ($self, $face) = @_; 309 my ($self, $face) = @_;
289 310
290 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; 311 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
291 312
292 $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image}; 313 $face->{texture} = new_from_image CFClient::Texture delete $face->{image};
293} 314}
294 315
295sub conn::query { 316sub conn::query {
296 my ($self, $flags, $prompt) = @_; 317 my ($self, $flags, $prompt) = @_;
297 318
370 $cb->signal_connect (clicked => sub { 391 $cb->signal_connect (clicked => sub {
371 for (keys %$cfg) { 392 for (keys %$cfg) {
372 $::CFG->{$_} = $cfg->{$_} 393 $::CFG->{$_} = $cfg->{$_}
373 if $_ ne '_i'; 394 if $_ ne '_i';
374 } 395 }
375 Crossfire::Client::write_cfg "$Crossfire::VARDIR/pclientrc"; 396 CFClient::write_cfg "$CFrossfire::VARDIR/pclientrc";
376 }); 397 });
377 $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);
378 $cb->signal_connect (clicked => sub { 399 $cb->signal_connect (clicked => sub {
379 for (keys %$cfg) { 400 for (keys %$cfg) {
380 $::CFG->{$_} = $cfg->{$_} 401 $::CFG->{$_} = $cfg->{$_}
399 420
400############################################################################# 421#############################################################################
401 422
402SDL::Init SDL_INIT_EVERYTHING; 423SDL::Init SDL_INIT_EVERYTHING;
403 424
404my $mapwidget = Crossfire::Client::Widget::MapWidget->new;
405
406$Crossfire::Client::Widget::TOPLEVEL->add ($mapwidget);
407$mapwidget->focus_in;
408
409Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; 425CFClient::read_cfg "$Crossfire::VARDIR/pclientrc";
410 426
411$CFG ||= { 427$CFG ||= {
412 width => 640, 428 width => 640,
413 height => 480, 429 height => 480,
414 mapsize => 100, 430 mapsize => 100,
415 fullscreen => 0, 431 fullscreen => 0,
416 host => "crossfire.schmorp.de", 432 host => "crossfire.schmorp.de",
417 port => 13327, 433 port => 13327,
418}; 434};
419 435
420Crossfire::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}
421 442
422$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; 443$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
423 444
424run_config_dialog 445run_config_dialog
425 login => sub { start_game }, 446 login => sub { start_game },

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines