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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines