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.61 by root, Mon Apr 10 11:57:17 2006 UTC vs.
Revision 1.70 by root, Tue Apr 11 14:36:02 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.7, 0.7, 0.7, 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...
116 my $w = new CFClient::Widget::Window
117 w => 300, h => 300,
106 $te = new Crossfire::Client::Widget::FancyFrame; 118 child => (my $te = new CFClient::Widget::FancyFrame);
119 $CFClient::Widget::TOPLEVEL->add ($w);
107 $te->add (new Crossfire::Client::Widget::Entry); 120 $te->add (new CFClient::Widget::Entry);
108 $te->move (300, 0, 2);
109 $Crossfire::Client::Widget::TOPLEVEL->add ($te);
110 121
111 $tw = new Crossfire::Client::Widget::Animator; 122 $tw = new CFClient::Widget::Animator x => $WIDTH - 200, w => 600, h => 300;
112 my $lbl1 = new Crossfire::Client::Widget::Label 123 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 124 my $lbl2 = new CFClient::Widget::Label text => "LBL2";
115 0, 0, 10, $FONTSIZE, "LBL2";
116
117 my $vb = new Crossfire::Client::Widget::VBox; 125 my $vb = new CFClient::Widget::VBox;
118 my $f = new Crossfire::Client::Widget::FancyFrame; 126 my $f = new CFClient::Widget::FancyFrame;
119 my $f2 = new Crossfire::Client::Widget::FancyFrame; 127 my $f2 = new CFClient::Widget::FancyFrame;
120 $f->add ($lbl1); 128 $f->add ($lbl1);
121 $f2->add ($lbl2); 129 $f2->add ($lbl2);
122 $vb->add ($f); 130 $vb->add ($f);
123 $vb->add ($f2, 1); 131 $vb->add ($f2, 1);
124 132
125 $tw->add ($vb); 133 $tw->add ($vb);
126 $tw->w (400);
127 $tw->h (300);
128 $tw->move ($WIDTH - 200, 0);
129 $tw->moveto (0, 0); 134 $tw->moveto (0, 0);
130 $Crossfire::Client::Widget::TOPLEVEL->add ($tw); 135 $CFClient::Widget::TOPLEVEL->add ($tw);
131 136
132# $f->move ($WIDTH - 200, 0); 137# $f->move ($WIDTH - 200, 0);
133# $Crossfire::Client::Widget::TOPLEVEL->add ($f); 138# $CFClient::Widget::TOPLEVEL->add ($f);
134 } 139 }
135} 140}
136 141
142sub destroy_screen {
143 remove Glib::Source $SDL_TIMER;
144 undef $SDL_APP;
145 undef $SDL_EV;
146 SDL::Quit;
147}
148
137sub start_game { 149sub 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}; 150 $WIDTH = $CFG->{width};
146 $HEIGHT = $CFG->{height}; 151 $HEIGHT = $CFG->{height};
147 $FULLSCREEN = 0; 152 $FULLSCREEN = 0;
148 153
149 init_screen; 154 init_screen;
157 pass => $CFG->{password}, 162 pass => $CFG->{password},
158 mapw => $mapsize, 163 mapw => $mapsize,
159 maph => $mapsize, 164 maph => $mapsize,
160 ; 165 ;
161 166
162 Crossfire::Client::lowdelay fileno $CONN->{fh}; 167 CFClient::lowdelay fileno $CONN->{fh};
163} 168}
164 169
165sub stop_game { 170sub stop_game {
166 remove Glib::Source $SDL_TIMER;
167 remove Glib::Source $refresh_handler if $refresh_handler; 171 remove Glib::Source $refresh_handler if $refresh_handler;
168 undef $refresh_handler; 172 undef $refresh_handler;
169 173
170 undef $SDL_APP;
171 undef $CONN; 174 undef $CONN;
172 SDL::Quit; 175 destroy_screen;
173} 176}
174 177
175sub force_refresh { 178sub force_refresh {
176 glViewport 0, 0, $WIDTH, $HEIGHT; 179 glViewport 0, 0, $WIDTH, $HEIGHT;
177 180
180 glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000; 183 glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000;
181 glMatrixMode GL_MODELVIEW; 184 glMatrixMode GL_MODELVIEW;
182 185
183 glClear GL_COLOR_BUFFER_BIT; 186 glClear GL_COLOR_BUFFER_BIT;
184 187
185 $Crossfire::Client::Widget::TOPLEVEL->draw; 188 $CFClient::Widget::TOPLEVEL->draw;
186 189
187 SDL::GLSwapBuffers; 190 SDL::GLSwapBuffers;
188} 191}
192
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;
189 200
190sub refresh { 201sub refresh {
191 $refresh_handler ||= add Glib::Idle sub { 202 $refresh_handler ||= add Glib::Idle sub {
192 if ($SDL_APP) { 203 if ($SDL_APP) {
193 my $next_refresh = SDL::GetTicks; 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
194 my $interval = ($next_refresh - $last_refresh) * 0.001; 211 my $interval = ($next_refresh - $last_refresh) * 0.001;
195 $last_refresh = $next_refresh; 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 }
196 219
197 force_refresh; 220 force_refresh;
198 $_->animate ($interval) for grep $_, values %ANIMATE; 221 $_->animate ($interval) for grep $_, values %ANIMATE;
199 222
200 if (%ANIMATE) { 223 if (%ANIMATE) {
236 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) {
237 # alt-enter 260 # alt-enter
238 $FULLSCREEN = !$FULLSCREEN; 261 $FULLSCREEN = !$FULLSCREEN;
239 init_screen; 262 init_screen;
240 } else { 263 } else {
241 Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV); 264 CFClient::Widget::feed_sdl_key_down_event ($SDL_EV);
242 } 265 }
243 }, 266 },
244 SDL_KEYUP() => sub { 267 SDL_KEYUP() => sub {
245 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); 268 CFClient::Widget::feed_sdl_key_up_event ($SDL_EV);
246 }, 269 },
247 SDL_MOUSEMOTION() => sub { 270 SDL_MOUSEMOTION() => sub {
248 Crossfire::Client::Widget::feed_sdl_motion_event ($SDL_EV); 271 CFClient::Widget::feed_sdl_motion_event ($SDL_EV);
249 }, 272 },
250 SDL_MOUSEBUTTONDOWN() => sub { 273 SDL_MOUSEBUTTONDOWN() => sub {
251 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); 274 CFClient::Widget::feed_sdl_button_down_event ($SDL_EV);
252 }, 275 },
253 SDL_MOUSEBUTTONUP() => sub { 276 SDL_MOUSEBUTTONUP() => sub {
254 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV); 277 CFClient::Widget::feed_sdl_button_up_event ($SDL_EV);
255 }, 278 },
256 SDL_ACTIVEEVENT() => sub { 279 SDL_ACTIVEEVENT() => sub {
257 printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d# 280# printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d#
258 }, 281 },
259); 282);
260 283
261@conn::ISA = Crossfire::Protocol::; 284@conn::ISA = Crossfire::Protocol::;
262 285
263sub conn::map_update { 286sub conn::map_update {
264 my ($self, $dirty) = @_; 287 my ($self, $dirty) = @_;
265 288
266 refresh; 289 $MAPWIDGET->update;
267} 290}
268 291
269sub conn::map_scroll { 292sub conn::map_scroll {
270 my ($self, $dx, $dy) = @_; 293 my ($self, $dx, $dy) = @_;
271 294
287sub conn::face_update { 310sub conn::face_update {
288 my ($self, $face) = @_; 311 my ($self, $face) = @_;
289 312
290 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; 313 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
291 314
292 $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image}; 315 $face->{texture} = new_from_image CFClient::Texture delete $face->{image};
293} 316}
294 317
295sub conn::query { 318sub conn::query {
296 my ($self, $flags, $prompt) = @_; 319 my ($self, $flags, $prompt) = @_;
297 320
370 $cb->signal_connect (clicked => sub { 393 $cb->signal_connect (clicked => sub {
371 for (keys %$cfg) { 394 for (keys %$cfg) {
372 $::CFG->{$_} = $cfg->{$_} 395 $::CFG->{$_} = $cfg->{$_}
373 if $_ ne '_i'; 396 if $_ ne '_i';
374 } 397 }
375 Crossfire::Client::write_cfg "$Crossfire::VARDIR/pclientrc"; 398 CFClient::write_cfg "$CFrossfire::VARDIR/pclientrc";
376 }); 399 });
377 $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);
378 $cb->signal_connect (clicked => sub { 401 $cb->signal_connect (clicked => sub {
379 for (keys %$cfg) { 402 for (keys %$cfg) {
380 $::CFG->{$_} = $cfg->{$_} 403 $::CFG->{$_} = $cfg->{$_}
399 422
400############################################################################# 423#############################################################################
401 424
402SDL::Init SDL_INIT_EVERYTHING; 425SDL::Init SDL_INIT_EVERYTHING;
403 426
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"; 427CFClient::read_cfg "$Crossfire::VARDIR/pclientrc";
410 428
411$CFG ||= { 429$CFG ||= {
412 width => 640, 430 width => 640,
413 height => 480, 431 height => 480,
414 mapsize => 100, 432 mapsize => 100,
415 fullscreen => 0, 433 fullscreen => 0,
416 host => "crossfire.schmorp.de", 434 host => "crossfire.schmorp.de",
417 port => 13327, 435 port => 13327,
418}; 436};
419 437
420Crossfire::Client::set_font Crossfire::Client::find_rcfile "uifont.ttf"; 438{
439 my @fonts = map CFClient::find_rcfile $_, qw(uifont.ttf uifontb.ttf uifonti.ttf uifontbi.ttf);
440
441 CFClient::add_font $_ for @fonts;
442 CFClient::set_font $fonts[0];
443}
421 444
422$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; 445$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
423 446
424run_config_dialog 447run_config_dialog
425 login => sub { start_game }, 448 login => sub { start_game },

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines