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.30 by root, Sat Apr 8 13:36:26 2006 UTC vs.
Revision 1.74 by root, Tue Apr 11 20:44:49 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
20use Crossfire::Client::Util; 19use CFClient;
21use Crossfire::Client::Widget; 20use CFClient::UI;
21
22our $VERSION = '0.1';
23
24my $MAX_FPS = 30;
25my $TICKS_PER_FRAME = int 1000 / $MAX_FPS - 1; # min ticks per frame
22 26
23our $FACECACHE; 27our $FACECACHE;
24
25our $VERSION = '0.1';
26
27our %GL_EXT;
28 28
29our $CFG; 29our $CFG;
30our $CONN; 30our $CONN;
31 31
32our $WIDTH; 32our $WIDTH;
33our $HEIGHT; 33our $HEIGHT;
34our $FULLSCREEN; 34our $FULLSCREEN;
35 35
36our $NOW;
37
38our $MAPWIDGET;
36our $UIFONT; 39our $FONTSIZE;
37 40
38our $SDL_TIMER; 41our $SDL_TIMER;
39our $SDL_APP; 42our $SDL_APP;
40our $SDL_EV = new SDL::Event; 43our $SDL_EV;
41our %SDL_CB; 44our %SDL_CB;
42 45
43our @GL_INIT; # hooks called on every gl init
44
45our $ALT_ENTER_MESSAGE; 46our $ALT_ENTER_MESSAGE;
47our $STATUS_LINE;
48our $DEBUG_STATUS;
49
50my $last_refresh;
51my %ANIMATE;
52my $refresh_handler;
53
54our ($tw, $te); # Test widget #d#
46 55
47sub init_screen { 56sub init_screen {
48 $SDL_APP = new SDL::App 57 $SDL_APP = new SDL::App
49 -flags => SDL_ANYFORMAT | SDL_HWSURFACE, 58 -flags => SDL_ANYFORMAT | SDL_HWSURFACE,
50 -title => "Crossfire+ Client", 59 -title => "Crossfire+ Client",
51 -width => $WIDTH, 60 -width => $WIDTH,
52 -height => $HEIGHT, 61 -height => $HEIGHT,
53 -opengl => 1, 62 -opengl => 1,
54 -red_size => 8, 63 -red_size => 5,
55 -green_size => 8, 64 -green_size => 5,
56 -blue_size => 8, 65 -blue_size => 5,
66 -alpha_size => 0,
57 -double_buffer => 1, 67 -double_buffer => 1,
58 -fullscreen => $FULLSCREEN, 68 -fullscreen => $FULLSCREEN,
59 -resizeable => 0; 69 -resizeable => 0;
60 70
61 %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions; 71 $SDL_EV = new SDL::Event;
72 $SDL_EV->set_unicode (1);
62 73
63 $GL_EXT{GL_ARB_texture_non_power_of_two} 74 $SDL_TIMER = add Glib::Timeout 1000/50, sub {
64 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;
65 77
66 $UIFONT = SDL::TTFOpenFont Crossfire::Client::find_rcfile "uifont.ttf", $HEIGHT / 40 78 1
67 or die "TTFOpenFont: $!"; 79 };
68 80
69 $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label 0, $HEIGHT - $HEIGHT / 40, 10, $UIFONT, "Alt-Enter toggles fullscreen mode"; 81 $last_refresh = SDL::GetTicks;
70 $ALT_ENTER_MESSAGE->move (0, $HEIGHT - ($ALT_ENTER_MESSAGE->size_request)[1]);
71 $ALT_ENTER_MESSAGE->activate;
72 82
83 CFClient::gl_init;
84
85 $FONTSIZE = int $HEIGHT / 50;
86
87 #############################################################################
88
73 glClearColor 0, 0, 0, 0; 89 glClearColor 0.45, 0.45, 0.45, 1;
74 90
75 glEnable GL_TEXTURE_2D; 91 glEnable GL_TEXTURE_2D;
92 glEnable GL_COLOR_MATERIAL;
76 glShadeModel GL_FLAT; 93 glShadeModel GL_FLAT;
77 glDisable GL_DEPTH_TEST; 94 glDisable GL_DEPTH_TEST;
78 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 95 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
79 96
80 glMatrixMode GL_PROJECTION; 97 #############################################################################
81 glLoadIdentity;
82 glOrtho 0, $WIDTH, $HEIGHT, 0, -100 , 100;
83 98
84 glMatrixMode GL_MODELVIEW; 99 $DEBUG_STATUS = new CFClient::UI::Label;
100 $CFClient::UI::TOPLEVEL->add ($DEBUG_STATUS);
101
102 $STATUS_LINE = new CFClient::UI::Label
103 y => $HEIGHT * 59 / 60 - $FONTSIZE;
104 $CFClient::UI::TOPLEVEL->add ($STATUS_LINE);
85 105
86 $_->() for @GL_INIT; 106 $ALT_ENTER_MESSAGE = new CFClient::UI::Label
107 y => $HEIGHT * 59 / 60,
108 height => $HEIGHT / 60,
109 text => "Use <b>Alt-Enter</b> to toggle fullscreen mode";
110 $CFClient::UI::TOPLEVEL->add ($ALT_ENTER_MESSAGE);
111
112 $MAPWIDGET = new CFClient::UI::MapWidget;
113 $CFClient::UI::TOPLEVEL->add ($MAPWIDGET);
114 $MAPWIDGET->focus_in;
115}
116
117sub destroy_screen {
118 remove Glib::Source $SDL_TIMER;
119 undef $SDL_APP;
120 undef $SDL_EV;
121 SDL::Quit;
122}
123
124sub config_dialog {
125 my $dialog = new CFClient::UI::FancyFrame x => 300, y => 100,
126 child => (my $vbox = new CFClient::UI::VBox);
127 $vbox->add (new CFClient::UI::Label align => 0, text => "Setup");
128 $vbox->add (my $table = new CFClient::UI::Table);
129
130 $table->add (0, 0, new CFClient::UI::Label text => "Video Mode");
131 $table->add (0, 1, new CFClient::UI::Label text => "Host");
132 $table->add (0, 2, new CFClient::UI::Label text => "Port");
133 $table->add (0, 3, new CFClient::UI::Label text => "Username");
134 $table->add (0, 4, new CFClient::UI::Label text => "Password", hidden => 1);
135 $table->add (0, 5, new CFClient::UI::Label text => "Server Map Size");
136
137 $CFClient::UI::TOPLEVEL->add ($dialog);
87} 138}
88 139
89sub start_game { 140sub start_game {
90 $SDL_TIMER = add Glib::Timeout 1000/100, sub {
91 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
92 while $SDL_EV->poll;
93
94 1
95 };
96
97 $WIDTH = $CFG->{width}; 141 $WIDTH = $CFG->{width};
98 $HEIGHT = $CFG->{height}; 142 $HEIGHT = $CFG->{height};
99 $FULLSCREEN = 0; 143 $FULLSCREEN = 0;
100 144
101 init_screen; 145 init_screen;
146
147 config_dialog;
148
149 my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32;
102 150
103 $CONN = new conn 151 $CONN = new conn
104 host => $CFG->{host}, 152 host => $CFG->{host},
105 port => $CFG->{port}, 153 port => $CFG->{port},
106 user => $CFG->{user}, 154 user => $CFG->{user},
107 pass => $CFG->{password}; 155 pass => $CFG->{password},
156 mapw => $mapsize,
157 maph => $mapsize,
158 ;
159
160 CFClient::lowdelay fileno $CONN->{fh};
108} 161}
109 162
110sub stop_game { 163sub stop_game {
111 remove Glib::Source $SDL_TIMER; 164 remove Glib::Source $refresh_handler if $refresh_handler;
165 undef $refresh_handler;
112 166
113 undef $SDL_APP; 167 undef $CONN;
114 SDL::Quit; 168 destroy_screen;
115} 169}
116
117 170
118sub force_refresh { 171sub force_refresh {
119 glViewport 0, 0, $WIDTH, $HEIGHT; 172 glViewport 0, 0, $WIDTH, $HEIGHT;
173
174 glMatrixMode GL_PROJECTION;
175 glLoadIdentity;
176 glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000;
177 glMatrixMode GL_MODELVIEW;
178 glLoadIdentity;
179
120 glClear GL_COLOR_BUFFER_BIT; 180 glClear GL_COLOR_BUFFER_BIT;
121 181
122 $_->draw for @Crossfire::Client::Widget::ACTIVE_WIDGETS; 182 $CFClient::UI::TOPLEVEL->draw;
123 183
124 SDL::GLSwapBuffers; 184 SDL::GLSwapBuffers;
125} 185}
126 186
127my $refresh_handler; 187sub debug {
188 $DEBUG_STATUS->set_text ($_[0]);
189 $DEBUG_STATUS->size_allocate ($DEBUG_STATUS->size_request);
190 $DEBUG_STATUS->move ($WIDTH - $DEBUG_STATUS->{w}, 0);
191}
192
193my $FPS;
128 194
129sub refresh { 195sub refresh {
130 $refresh_handler ||= add Glib::Idle sub { 196 $refresh_handler ||= add Glib::Idle sub {
197 if ($SDL_APP) {
198 $NOW = SDL::GetTicks;
199
200 if ($NOW - $last_refresh < $TICKS_PER_FRAME) {
201 SDL::Delay $TICKS_PER_FRAME - ($NOW - $last_refresh);
202 $NOW = SDL::GetTicks;
203 }
204
205 my $interval = ($NOW - $last_refresh) * 0.001;
206 $last_refresh = $NOW;
207
208 if ($interval) {
209 $FPS ||= 1 / $interval;
210 $FPS = $FPS * 0.96 + (1 / $interval) * 0.04;
211 debug sprintf "%5.02f", $FPS;
212 }
213
131 force_refresh; 214 force_refresh;
215 $_->animate ($interval) for grep $_, values %ANIMATE;
216
217 if (%ANIMATE) {
218 1
219 } else {
220 undef $refresh_handler;
221 0
222 }
223 } else {
132 undef $refresh_handler; 224 undef $refresh_handler;
225 0
133 0 226 }
134 }; 227 };
228}
229
230sub animation_start {
231 my ($widget) = @_;
232 $ANIMATE{$widget} = $widget;
233 Scalar::Util::weaken $ANIMATE{$widget};
234
235 refresh;
236}
237
238sub animation_stop {
239 my ($widget) = @_;
240 delete $ANIMATE{$widget};
135} 241}
136 242
137%SDL_CB = ( 243%SDL_CB = (
138 SDL_QUIT() => sub { 244 SDL_QUIT() => sub {
139 main_quit Gtk2; 245 main_quit Gtk2;
147 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) { 253 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) {
148 # alt-enter 254 # alt-enter
149 $FULLSCREEN = !$FULLSCREEN; 255 $FULLSCREEN = !$FULLSCREEN;
150 init_screen; 256 init_screen;
151 } else { 257 } else {
152 Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV); 258 CFClient::UI::feed_sdl_key_down_event ($SDL_EV);
153 } 259 }
154 }, 260 },
155 SDL_KEYUP() => sub { 261 SDL_KEYUP() => sub {
156 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); 262 CFClient::UI::feed_sdl_key_up_event ($SDL_EV);
157 }, 263 },
158 SDL_MOUSEMOTION() => sub { 264 SDL_MOUSEMOTION() => sub {
159 warn "sdl motion\n";#d# 265 CFClient::UI::feed_sdl_motion_event ($SDL_EV);
160 }, 266 },
161 SDL_MOUSEBUTTONDOWN() => sub { 267 SDL_MOUSEBUTTONDOWN() => sub {
162 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); 268 CFClient::UI::feed_sdl_button_down_event ($SDL_EV);
163 }, 269 },
164 SDL_MOUSEBUTTONUP() => sub { 270 SDL_MOUSEBUTTONUP() => sub {
165 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV); 271 CFClient::UI::feed_sdl_button_up_event ($SDL_EV);
166 }, 272 },
167 SDL_ACTIVEEVENT() => sub { 273 SDL_ACTIVEEVENT() => sub {
168 warn "active\n";#d# 274# printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d#
169 }, 275 },
170); 276);
171 277
172@conn::ISA = Crossfire::Protocol::; 278@conn::ISA = Crossfire::Protocol::;
173 279
174sub conn::map_update { 280sub conn::map_update {
175 my ($self, $dirty) = @_; 281 my ($self, $dirty) = @_;
176 282
177 refresh; 283 $MAPWIDGET->update;
178} 284}
179 285
180sub conn::map_scroll { 286sub conn::map_scroll {
181 my ($self, $dx, $dy) = @_; 287 my ($self, $dx, $dy) = @_;
182 288
183 refresh; 289# refresh;
184} 290}
185 291
186sub conn::map_clear { 292sub conn::map_clear {
187 my ($self) = @_; 293 my ($self) = @_;
188 294
189 refresh; 295# refresh;
190} 296}
191 297
192sub conn::face_find { 298sub conn::face_find {
193 my ($self, $face) = @_; 299 my ($self, $face) = @_;
194 300
198sub conn::face_update { 304sub conn::face_update {
199 my ($self, $face) = @_; 305 my ($self, $face) = @_;
200 306
201 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; 307 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
202 308
203 $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image}; 309 $face->{texture} = new_from_image CFClient::Texture delete $face->{image};
310}
311
312sub conn::query {
313 my ($self, $flags, $prompt) = @_;
314
315 warn "<<<<QUERY:$flags:$prompt>>>\n";#d#
204} 316}
205 317
206sub gtk_add_cfg_field { 318sub gtk_add_cfg_field {
207 my ($tbl, $cfg, $klbl, $key, $value) = @_; 319 my ($tbl, $cfg, $klbl, $key, $value) = @_;
208 my $i = $cfg->{_i}++; 320 my $i = $cfg->{_i}++;
214 } 326 }
215 $ent->set_text ($value); 327 $ent->set_text ($value);
216 $ent->signal_connect (changed => sub { 328 $ent->signal_connect (changed => sub {
217 my ($ent) = @_; 329 my ($ent) = @_;
218 $cfg->{$key} = $ent->get_text; 330 $cfg->{$key} = $ent->get_text;
331 # TODO: Mapsize should be a slider in the game gui
332 if ($key eq 'mapsize' and $cfg->{$key} > 100) {
333 $cfg->{$key} = 100;
334 } elsif ($key eq 'mapsize' and $cfg->{$key} < 50) {
335 $cfg->{$key} = 50;
336 }
219 }); 337 });
220} 338}
221 339
222sub run_config_dialog { 340sub run_config_dialog {
223 my (%events) = @_; 341 my (%events) = @_;
225 my $w = Gtk2::Window->new; 343 my $w = Gtk2::Window->new;
226 344
227 my @cfg = ( 345 my @cfg = (
228 [qw/Host host/], 346 [qw/Host host/],
229 [qw/Port port/], 347 [qw/Port port/],
348 [qw/Mapsize% mapsize/],
230 [qw/Username user/], 349 [qw/Username user/],
231 [qw/Password password/], 350 [qw/Password password/],
232 ); 351 );
233 352
234 my $cfg = {}; 353 my $cfg = {};
268 $cb->signal_connect (clicked => sub { 387 $cb->signal_connect (clicked => sub {
269 for (keys %$cfg) { 388 for (keys %$cfg) {
270 $::CFG->{$_} = $cfg->{$_} 389 $::CFG->{$_} = $cfg->{$_}
271 if $_ ne '_i'; 390 if $_ ne '_i';
272 } 391 }
273 Crossfire::Client::write_cfg "$Crossfire::VARDIR/pclientrc"; 392 CFClient::write_cfg "$CFrossfire::VARDIR/pclientrc";
274 }); 393 });
275 $hb->pack_start (my $cb = Gtk2::Button->new ("login"), 1, 1, 5); 394 $hb->pack_start (my $cb = Gtk2::Button->new ("login"), 1, 1, 5);
276 $cb->signal_connect (clicked => sub { 395 $cb->signal_connect (clicked => sub {
277 for (keys %$cfg) { 396 for (keys %$cfg) {
278 $::CFG->{$_} = $cfg->{$_} 397 $::CFG->{$_} = $cfg->{$_}
281 my $cb = $events{login} || sub {}; 400 my $cb = $events{login} || sub {};
282 $cb->($::CFG->{user}, $::CFG->{password}); 401 $cb->($::CFG->{user}, $::CFG->{password});
283 }); 402 });
284 $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5); 403 $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5);
285 $cb->signal_connect (clicked => sub { 404 $cb->signal_connect (clicked => sub {
286 my $cb = $events{login} || sub {}; 405 my $cb = $events{logout} || sub {};
287 $cb->(); 406 $cb->();
288 }); 407 });
289 $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5); 408 $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5);
290 $cb->signal_connect (clicked => sub { $w->destroy }); 409 $cb->signal_connect (clicked => sub { $w->destroy });
291 410
296 415
297 416
298############################################################################# 417#############################################################################
299 418
300SDL::Init SDL_INIT_EVERYTHING; 419SDL::Init SDL_INIT_EVERYTHING;
301SDL::TTFInit;
302 420
303my $mapwidget = Crossfire::Client::Widget::MapWidget->new;
304
305$mapwidget->activate;
306$mapwidget->focus_in;
307
308Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; 421CFClient::read_cfg "$Crossfire::VARDIR/pclientrc";
309 422
310$CFG ||= { 423$CFG ||= {
311 width => 640, 424 width => 640,
312 height => 480, 425 height => 480,
426 mapsize => 100,
313 fullscreen => 0, 427 fullscreen => 0,
314 host => "crossfire.schmorp.de", 428 host => "crossfire.schmorp.de",
315 port => 13327, 429 port => 13327,
316}; 430};
317 431
432{
433 my @fonts = map CFClient::find_rcfile $_, qw(uifont.ttf uifontb.ttf uifonti.ttf uifontbi.ttf);
434
435 CFClient::add_font $_ for @fonts;
436 CFClient::set_font $fonts[0];
437}
438
318$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; 439$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
319 440
320run_config_dialog 441run_config_dialog
321 login => sub { start_game }, 442 login => sub { start_game },
322 logout => sub { stop_game }; 443 logout => sub { stop_game };

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines