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.83 by root, Wed Apr 12 15:35:55 2006 UTC vs.
Revision 1.95 by root, Thu Apr 13 23:07:58 2006 UTC

1#!/opt/bin/perl 1#!/opt/bin/perl
2 2
3use strict; 3use strict;
4use utf8; 4use utf8;
5 5
6use Glib; 6use Time::HiRes 'time';
7use Gtk2 -init; 7use Event;
8 8
9use SDL; 9use SDL;
10use SDL::App; 10use SDL::App;
11use SDL::Event; 11use SDL::Event;
12use SDL::Surface; 12use SDL::Surface;
13use SDL::OpenGL; 13use SDL::OpenGL;
14use SDL::OpenGL::Constants;
15 14
16use Crossfire; 15use Crossfire;
17use Crossfire::Protocol; 16use Crossfire::Protocol;
18 17
19use CFClient; 18use CFClient;
20use CFClient::UI; 19use CFClient::UI;
21 20
22our $VERSION = '0.1'; 21our $VERSION = '0.1';
23 22
24my $MAX_FPS = 60; 23my $MAX_FPS = 500;
25my $TICKS_PER_FRAME = int 1000 / $MAX_FPS - 1; # min ticks per frame 24my $MIN_FPS = 5; # unused as of yet
26 25
27our $FACECACHE; 26our $FACECACHE;
27
28our $LAST_REFRESH;
29our $NOW;
28 30
29our $CFG; 31our $CFG;
30our $CONN; 32our $CONN;
31our $FAST; # fast, low-quality mode 33our $FAST; # fast, low-quality mode, possibly useful for software-rendering
32 34
33our @SDL_MODES; 35our @SDL_MODES;
34our $WIDTH; 36our $WIDTH;
35our $HEIGHT; 37our $HEIGHT;
36our $FULLSCREEN; 38our $FULLSCREEN;
37 39
38our $NOW; 40our $MAP;
39
40our $MAPWIDGET; 41our $MAPWIDGET;
41our $FONTSIZE; 42our $FONTSIZE;
42 43
43our $SDL_TIMER; 44our $SDL_ACTIVE;
44our $SDL_APP;
45our $SDL_EV; 45our $SDL_EV;
46our %SDL_CB; 46our %SDL_CB;
47 47
48our $ALT_ENTER_MESSAGE; 48our $ALT_ENTER_MESSAGE;
49our $STATUS_LINE; 49our $STATUS_LINE;
50our $DEBUG_STATUS; 50our $DEBUG_STATUS;
51
52my $last_refresh;
53my %ANIMATE;
54my $refresh_handler;
55 51
56sub status { 52sub status {
57 $STATUS_LINE->set_text ($_[0]); 53 $STATUS_LINE->set_text ($_[0]);
58 my ($w, $h) = $STATUS_LINE->size_request; 54 my ($w, $h) = $STATUS_LINE->size_request;
59 $STATUS_LINE->size_allocate (0, $HEIGHT - $ALT_ENTER_MESSAGE->{h} - $h, $w, $h); 55 $STATUS_LINE->size_allocate (0, $HEIGHT - $ALT_ENTER_MESSAGE->{h} - $h, $w, $h);
63 $DEBUG_STATUS->set_text ($_[0]); 59 $DEBUG_STATUS->set_text ($_[0]);
64 my ($w, $h) = $DEBUG_STATUS->size_request; 60 my ($w, $h) = $DEBUG_STATUS->size_request;
65 $DEBUG_STATUS->size_allocate ($WIDTH - $w, 0, $w, $h); 61 $DEBUG_STATUS->size_allocate ($WIDTH - $w, 0, $w, $h);
66} 62}
67 63
68sub config_dialog { 64sub start_game {
69 my $dialog = new CFClient::UI::FancyFrame x => 300, y => 100, 65 status "logging in...";
70 child => (my $vbox = new CFClient::UI::VBox);
71 $vbox->add (new CFClient::UI::Label align => 0, text => "Client Setup");
72 $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]);
73 66
74 $table->add (0, 0, new CFClient::UI::Label align => 1, text => "Video Mode"); 67 my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32;
75 $table->add (1, 0, my $hbox = new CFClient::UI::HBox);
76 68
77 $hbox->add (my $mode_slider = new CFClient::UI::Slider expand => 1, req_w => 100, range => [$CFG->{sdl_mode}, 0, scalar @SDL_MODES, 1]); 69 $MAP = new CFClient::Map $mapsize, $mapsize;
78 $hbox->add (my $mode_label = new CFClient::UI::Label height => $FONTSIZE * 0.8);
79
80 $mode_slider->connect (changed => sub {
81 my ($self, $value) = @_;
82
83 $CFG->{sdl_mode} = $self->{range}[0] = $value = int $value;
84 $mode_label->set_text (sprintf "%dx%d", @{$SDL_MODES[$value]});
85 });
86 $mode_slider->emit (changed => $mode_slider->{range}[0]);
87
88 $table->add (1, 1, new CFClient::UI::Button expand => 1, align => 0, text => "Apply", connect_activate => sub {
89 destroy_screen ();
90 ($WIDTH, $HEIGHT) = @{ $SDL_MODES[$CFG->{sdl_mode}] };
91 $FULLSCREEN = $CFG->{fullscreen};
92 init_screen ();
93 });
94 70
95 $vbox->add (new CFClient::UI::Label align => 0, text => "Server");
96 $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]);
97 $table->add (0, 2, new CFClient::UI::Label align => 1, text => "Host");
98 $table->add (1, 2, my $host = new CFClient::UI::Entry text => $CFG->{host});
99
100 $table->add (0, 3, new CFClient::UI::Label align => 1, text => "Port");
101 $table->add (1, 3, my $port = new CFClient::UI::Entry text => $CFG->{port});
102
103 $table->add (0, 4, new CFClient::UI::Label align => 1, text => "Username");
104 $table->add (1, 4, my $user = new CFClient::UI::Entry text => $CFG->{user});
105
106 $table->add (0, 5, new CFClient::UI::Label align => 1, text => "Password");
107 $table->add (1, 5, my $pass = new CFClient::UI::Entry text => $CFG->{password}, hidden => 1);
108
109 $table->add (0, 6, new CFClient::UI::Label align => 1, text => "Map Size");
110 $table->add (1, 6, new CFClient::UI::Slider
111 req_w => 100,
112 range => [$CFG->{mapsize}, 10, 100 + 1, 1],
113 connect_changed => sub {
114 my ($self, $value) = @_;
115
116 $CFG->{mapsize} = $self->{range}[0] = $value = int $value;
117 },
118 );
119
120 $table->add (1, 7, new CFClient::UI::Button expand => 1, align => 0, text => "Login", connect_activate => sub {
121 warn "login\n";
122 });
123
124 $vbox->add (my $hbox = new CFClient::UI::HBox);
125
126 $hbox->add (new CFClient::UI::Button expand => 1, align => 0, text => "Save", connect_activate => sub {
127 CFClient::write_cfg "$CFrossfire::VARDIR/pclientrc";
128 status "Configuration Saved";
129 });
130 $CFClient::UI::TOPLEVEL->add ($dialog);
131}
132
133sub init_screen {
134 $SDL_APP = new SDL::App
135 -flags => SDL_ANYFORMAT | SDL_HWSURFACE,
136 -title => "Crossfire+ Client",
137 -width => $WIDTH,
138 -height => $HEIGHT,
139 -opengl => 1,
140 -red_size => 5,
141 -green_size => 5,
142 -blue_size => 5,
143 -alpha_size => 0,
144 -double_buffer => 1,
145 -fullscreen => $FULLSCREEN,
146 -resizeable => 0;
147
148 $SDL_EV = new SDL::Event;
149 $SDL_EV->set_unicode (1);
150
151 $SDL_TIMER = add Glib::Timeout 1000 / $MAX_FPS, sub {
152 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
153 while $SDL_EV->poll;
154
155 1
156 };
157
158 $last_refresh = SDL::GetTicks;
159
160 CFClient::gl_init;
161
162 $FONTSIZE = int $HEIGHT / 40;
163
164 #############################################################################
165
166 $DEBUG_STATUS = new CFClient::UI::Label padding => 0;
167 $CFClient::UI::TOPLEVEL->add ($DEBUG_STATUS);
168
169 $STATUS_LINE = new CFClient::UI::Label
170 padding => 0,
171 y => $HEIGHT * 49 / 50 - $FONTSIZE;
172 $CFClient::UI::TOPLEVEL->add ($STATUS_LINE);
173
174 $ALT_ENTER_MESSAGE = new CFClient::UI::Label
175 padding => 0,
176 y => $HEIGHT * 49 / 50,
177 height => $HEIGHT / 50,
178 text => "Use <b>Alt-Enter</b> to toggle fullscreen mode";
179 $CFClient::UI::TOPLEVEL->add ($ALT_ENTER_MESSAGE);
180
181 $MAPWIDGET = new CFClient::UI::MapWidget;
182 $CFClient::UI::TOPLEVEL->add ($MAPWIDGET);
183 $MAPWIDGET->focus_in;
184
185 config_dialog;
186}
187
188sub destroy_screen {
189 $CFClient::UI::TOPLEVEL->{children} = [];
190 remove Glib::Source $SDL_TIMER;
191 undef $SDL_APP;
192 undef $SDL_EV;
193 SDL::Quit;
194}
195
196sub start_game {
197 $WIDTH = $CFG->{width};
198 $HEIGHT = $CFG->{height};
199 $FULLSCREEN = 0;
200
201 init_screen;
202
203 my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32;
204
205 $CONN = new conn 71 $CONN = new conn
206 host => $CFG->{host}, 72 host => $CFG->{host},
207 port => $CFG->{port}, 73 port => $CFG->{port},
208 user => $CFG->{user}, 74 user => $CFG->{user},
209 pass => $CFG->{password}, 75 pass => $CFG->{password},
210 mapw => $mapsize, 76 mapw => $mapsize,
211 maph => $mapsize, 77 maph => $mapsize,
212 ; 78 ;
213 79
80 status "login successful";
81
214 CFClient::lowdelay fileno $CONN->{fh}; 82 CFClient::lowdelay fileno $CONN->{fh};
215} 83}
216 84
217sub stop_game { 85sub stop_game {
218 remove Glib::Source $refresh_handler if $refresh_handler;
219 undef $refresh_handler;
220
221 undef $CONN; 86 undef $CONN;
87}
88
89sub config_dialog {
90 my $dialog = new CFClient::UI::FancyFrame x => 300, y => 100,
91 child => (my $vbox = new CFClient::UI::VBox);
92 $vbox->add (new CFClient::UI::Label align => 0, text => "Client Setup");
93 $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]);
94
95 $table->add (0, 0, new CFClient::UI::Label align => 1, text => "Video Mode");
96 $table->add (1, 0, my $hbox = new CFClient::UI::HBox);
97
98 $hbox->add (my $mode_slider = new CFClient::UI::Slider expand => 1, req_w => 100, range => [$CFG->{sdl_mode}, 0, scalar @SDL_MODES, 1]);
99 $hbox->add (my $mode_label = new CFClient::UI::Label height => $FONTSIZE * 0.8);
100
101 $mode_slider->connect (changed => sub {
102 my ($self, $value) = @_;
103
104 $CFG->{sdl_mode} = $self->{range}[0] = $value = int $value;
105 $mode_label->set_text (sprintf "%dx%d", @{$SDL_MODES[$value]});
106 });
107 $mode_slider->emit (changed => $mode_slider->{range}[0]);
108
109 $table->add (0, 1, new CFClient::UI::Label align => 1, text => "Fullscreen");
110 $table->add (1, 1, new CFClient::UI::CheckBox state => $CFG->{fullscreen}, connect_changed => sub {
111 my ($self, $value) = @_;
112 $CFG->{fullscreen} = $value;
113 });
114
115 $table->add (0, 2, new CFClient::UI::Label align => 1, text => "Fast &amp; Ugly");
116 $table->add (1, 2, new CFClient::UI::CheckBox state => $CFG->{fast}, connect_changed => sub {
117 my ($self, $value) = @_;
118 $CFG->{fast} = $value;
119 });
120
121 $table->add (0, 3, new CFClient::UI::Label align => 1, text => "Fog of War");
122 $table->add (1, 3, new CFClient::UI::Slider range => [$CFG->{fow_intensity}, 0, 1 + 0.001, 0.001], connect_changed => sub {
123 my ($self, $value) = @_;
124 $CFG->{fow_intensity} = $value;
125 });
126
127 $table->add (0, 4, new CFClient::UI::Label align => 1, text => "Smooth FoW");
128 $table->add (1, 4, new CFClient::UI::CheckBox state => $CFG->{fow_smooth}, connect_changed => sub {
129 my ($self, $value) = @_;
130 $CFG->{fow_smooth} = $value;
131 });
132
133 $table->add (1, 5, new CFClient::UI::Button expand => 1, align => 0, text => "Apply", connect_activate => sub {
222 destroy_screen; 134 destroy_screen ();
135 init_screen ();
136 });
137
138 $vbox->add (new CFClient::UI::Label align => 0, text => "Server Setup");
139 $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]);
140 $table->add (0, 2, new CFClient::UI::Label align => 1, text => "Host");
141 $table->add (1, 2, my $host = new CFClient::UI::Entry text => $CFG->{host});
142
143 $table->add (0, 3, new CFClient::UI::Label align => 1, text => "Port");
144 $table->add (1, 3, my $port = new CFClient::UI::Entry text => $CFG->{port});
145
146 $table->add (0, 4, new CFClient::UI::Label align => 1, text => "Username");
147 $table->add (1, 4, my $user = new CFClient::UI::Entry text => $CFG->{user});
148
149 $table->add (0, 5, new CFClient::UI::Label align => 1, text => "Password");
150 $table->add (1, 5, my $pass = new CFClient::UI::Entry text => $CFG->{password}, hidden => 1);
151
152 $table->add (0, 6, new CFClient::UI::Label align => 1, text => "Map Size");
153 $table->add (1, 6, new CFClient::UI::Slider
154 req_w => 100,
155 range => [$CFG->{mapsize}, 10, 100 + 1, 1],
156 connect_changed => sub {
157 my ($self, $value) = @_;
158
159 $CFG->{mapsize} = $self->{range}[0] = $value = int $value;
160 },
161 );
162
163 $table->add (1, 7, new CFClient::UI::Button expand => 1, align => 0, text => "Login", connect_activate => sub {
164 start_game;
165 });
166
167 $vbox->add (my $hbox = new CFClient::UI::HBox);
168
169 $hbox->add (new CFClient::UI::Button expand => 1, align => 0, text => "Save", connect_activate => sub {
170 CFClient::write_cfg "$Crossfire::VARDIR/pclientrc";
171 status "Configuration Saved";
172 });
173 $CFClient::UI::TOPLEVEL->add ($dialog);
223} 174}
175
176sub sdl_init {
177 SDL::Init SDL_INIT_AUDIO | SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE
178 and die "SDL::Init failed!\n";
179}
180
181sub init_screen {
182 sdl_init;
183
184 ($WIDTH, $HEIGHT) = @{ $SDL_MODES[$CFG->{sdl_mode}] };
185 $FULLSCREEN = $CFG->{fullscreen};
186 $FAST = $CFG->{fast};
187
188 SDL::GLSetAttribute SDL_GL_RED_SIZE, 5;
189 SDL::GLSetAttribute SDL_GL_GREEN_SIZE, 5;
190 SDL::GLSetAttribute SDL_GL_BLUE_SIZE, 5;
191 SDL::GLSetAttribute SDL_GL_ALPHA_SIZE, 0;
192
193 SDL::GLSetAttribute SDL_GL_ACCUM_RED_SIZE, 0;
194 SDL::GLSetAttribute SDL_GL_ACCUM_GREEN_SIZE, 0;
195 SDL::GLSetAttribute SDL_GL_ACCUM_BLUE_SIZE, 0;
196 SDL::GLSetAttribute SDL_GL_ACCUM_ALPHA_SIZE, 0;
197
198 SDL::GLSetAttribute SDL_GL_DOUBLEBUFFER, 1;
199 SDL::GLSetAttribute SDL_GL_BUFFER_SIZE, 15;
200 SDL::GLSetAttribute SDL_GL_DEPTH_SIZE, 0;
201
202 SDL::SetVideoMode $WIDTH, $HEIGHT, 0,
203 SDL_HWSURFACE | SDL_ANYFORMAT | SDL_OPENGL | SDL_DOUBLEBUF
204 | ($FULLSCREEN ? SDL_FULLSCREEN : 0)
205 or die "SDL::SetVideoMode failed!\n";
206
207 SDL::WMSetCaption "Crossfire+ Client", "Crossfire+";
208
209 $SDL_EV = new SDL::Event;
210 $SDL_EV->set_unicode (1);
211
212 $SDL_ACTIVE = 1;
213
214 $LAST_REFRESH = time - 0.01;
215
216 CFClient::gl_init;
217
218 $FONTSIZE = int $HEIGHT / 40;
219
220 #############################################################################
221
222 $DEBUG_STATUS = new CFClient::UI::Label padding => 0;
223 $CFClient::UI::TOPLEVEL->add ($DEBUG_STATUS);
224
225 $STATUS_LINE = new CFClient::UI::Label
226 padding => 0,
227 y => $HEIGHT * 49 / 50 - $FONTSIZE;
228 $CFClient::UI::TOPLEVEL->add ($STATUS_LINE);
229
230 $ALT_ENTER_MESSAGE = new CFClient::UI::Label
231 padding => 0,
232 y => $HEIGHT * 49 / 50,
233 height => $HEIGHT / 50,
234 text => "Use <b>Alt-Enter</b> to toggle fullscreen mode";
235 $CFClient::UI::TOPLEVEL->add ($ALT_ENTER_MESSAGE);
236
237 $MAPWIDGET = new CFClient::UI::MapWidget;
238 $CFClient::UI::TOPLEVEL->add ($MAPWIDGET);
239 $MAPWIDGET->focus_in;
240
241 config_dialog;
242}
243
244sub destroy_screen {
245 $CFClient::UI::TOPLEVEL->{children} = [];
246 undef $SDL_ACTIVE;
247 undef $SDL_EV;
248 SDL::Quit;
249}
250
251my %animate_object;
252my $animate_timer;
253
254my $want_refresh;
255my $can_refresh;
256
257my $fps = 9;
224 258
225sub force_refresh { 259sub force_refresh {
260 $fps = $fps * 0.95 + 1 / ($NOW - $LAST_REFRESH) * 0.05;
261 debug sprintf "%3.2f", $fps;
262
263# $want_refresh = 0;#d#
264 $can_refresh = 0;
265
226 glViewport 0, 0, $WIDTH, $HEIGHT; 266 glViewport 0, 0, $WIDTH, $HEIGHT;
227 267
228 glMatrixMode GL_PROJECTION; 268 glMatrixMode GL_PROJECTION;
229 glLoadIdentity; 269 glLoadIdentity;
230 glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000; 270 glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000;
231 glMatrixMode GL_MODELVIEW; 271 glMatrixMode GL_MODELVIEW;
232 glLoadIdentity; 272 glLoadIdentity;
233 273
274 glClearColor +($CFG->{fow_intensity}) x 3, 1;
234 glClear GL_COLOR_BUFFER_BIT; 275 glClear GL_COLOR_BUFFER_BIT;
235 276
236 $CFClient::UI::TOPLEVEL->draw; 277 $CFClient::UI::TOPLEVEL->draw;
237 278
238 SDL::GLSwapBuffers; 279 SDL::GLSwapBuffers;
239}
240 280
241my $FPS; 281 $LAST_REFRESH = $NOW;
282}
283
284my $refresh_watcher = Event->timer (after => 0, hard => 1, interval => 1 / $MAX_FPS, cb => sub {
285 $NOW = time;
286
287 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
288 while $SDL_EV->poll;
289
290 if (%animate_object) {
291 $_->animate ($LAST_REFRESH - $NOW) for values %animate_object;
292 $want_refresh++;
293 }
294
295 if ($want_refresh) {
296 force_refresh;
297 } else {
298 $can_refresh = 1;
299 }
300});
242 301
243sub refresh { 302sub refresh {
244 $refresh_handler ||= add Glib::Idle sub { 303 $want_refresh++;
245 if ($SDL_APP) {
246 $NOW = SDL::GetTicks;
247
248 if ($NOW - $last_refresh < $TICKS_PER_FRAME) {
249 SDL::Delay $TICKS_PER_FRAME - ($NOW - $last_refresh);
250 $NOW = SDL::GetTicks;
251 }
252
253 my $interval = ($NOW - $last_refresh) * 0.001;
254 $last_refresh = $NOW;
255
256 if ($interval) {
257 $FPS ||= 1 / $interval;
258 $FPS = $FPS * 0.9 + (1 / $interval) * 0.1;
259 debug sprintf "%5.02f", $FPS;
260 }
261
262 force_refresh;
263 $_->animate ($interval) for grep $_, values %ANIMATE;
264
265 if (%ANIMATE) {
266 1
267 } else {
268 undef $refresh_handler;
269 0
270 }
271 } else {
272 undef $refresh_handler;
273 0
274 }
275 };
276} 304}
277 305
278sub animation_start { 306sub animation_start {
279 my ($widget) = @_; 307 my ($widget) = @_;
280 $ANIMATE{$widget} = $widget; 308 $animate_object{$widget} = $widget;
281 Scalar::Util::weaken $ANIMATE{$widget};
282
283 refresh;
284} 309}
285 310
286sub animation_stop { 311sub animation_stop {
287 my ($widget) = @_; 312 my ($widget) = @_;
288 delete $ANIMATE{$widget}; 313 delete $animate_object{$widget};
314}
315
316@conn::ISA = Crossfire::Protocol::;
317
318sub conn::user_send {
319 my ($self, $command) = @_;
320
321 $self->send ($command);
322 status $command;
323}
324
325sub conn::feed_map1a {
326 my ($self, $data) = @_;
327
328# $self->Crossfire::Protocol::feed_map1a ($data);
329
330 $MAP->scroll (delete $self->{delayed_scroll_x}, delete $self->{delayed_scroll_y});
331 $MAP->map1a_update ($data);
332 $MAPWIDGET->update;
333}
334
335#sub conn::map_update {
336# my ($self, $dirty) = @_;
337#
338# $MAPWIDGET->update;
339#}
340
341sub conn::map_clear {
342 my ($self) = @_;
343
344 $MAP->clear;
345
346# refresh;
347}
348
349sub conn::face_find {
350 my ($self, $face) = @_;
351
352 $FACECACHE->{"$face->{chksum},$face->{name}"}
353}
354
355sub conn::face_update {
356 my ($self, $facenum, $face) = @_;
357
358 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
359
360 my $tex = $face->{texture} = new_from_image CFClient::Texture delete $face->{image};
361
362 $MAP->set_texture ($facenum, @$tex{qw(name w h s t)});
363 $MAPWIDGET->update;
364}
365
366sub conn::query {
367 my ($self, $flags, $prompt) = @_;
368
369 warn "<<<<QUERY:$flags:$prompt>>>\n";#d#
289} 370}
290 371
291%SDL_CB = ( 372%SDL_CB = (
292 SDL_QUIT() => sub { 373 SDL_QUIT() => sub {
293 main_quit Gtk2; 374 Event::unloop -1;
294 }, 375 },
295 SDL_VIDEORESIZE() => sub { 376 SDL_VIDEORESIZE() => sub {
296 }, 377 },
297 SDL_VIDEOEXPOSE() => sub { 378 SDL_VIDEOEXPOSE() => sub {
298 refresh; 379 refresh;
322 SDL_ACTIVEEVENT() => sub { 403 SDL_ACTIVEEVENT() => sub {
323# printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d# 404# printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d#
324 }, 405 },
325); 406);
326 407
327@conn::ISA = Crossfire::Protocol::;
328
329sub conn::map_update {
330 my ($self, $dirty) = @_;
331
332 $MAPWIDGET->update;
333}
334
335sub conn::map_scroll {
336 my ($self, $dx, $dy) = @_;
337
338# refresh;
339}
340
341sub conn::map_clear {
342 my ($self) = @_;
343
344# refresh;
345}
346
347sub conn::face_find {
348 my ($self, $face) = @_;
349
350 $FACECACHE->{"$face->{chksum},$face->{name}"}
351}
352
353sub conn::face_update {
354 my ($self, $face) = @_;
355
356 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
357
358 $face->{texture} = new_from_image CFClient::Texture delete $face->{image};
359}
360
361sub conn::query {
362 my ($self, $flags, $prompt) = @_;
363
364 warn "<<<<QUERY:$flags:$prompt>>>\n";#d#
365}
366
367sub gtk_add_cfg_field {
368 my ($tbl, $cfg, $klbl, $key, $value) = @_;
369 my $i = $cfg->{_i}++;
370 $tbl->attach_defaults (my $lbl = Gtk2::Label->new ($klbl), 0, 1, $i, $i + 1);
371 $tbl->attach_defaults (my $ent = Gtk2::Entry->new, 1, 2, $i, $i + 1);
372 if ($key eq 'password') {
373 $ent->set_invisible_char ("*");
374 $ent->set (visibility => 0)
375 }
376 $ent->set_text ($value);
377 $ent->signal_connect (changed => sub {
378 my ($ent) = @_;
379 $cfg->{$key} = $ent->get_text;
380 # TODO: Mapsize should be a slider in the game gui
381 if ($key eq 'mapsize' and $cfg->{$key} > 100) {
382 $cfg->{$key} = 100;
383 } elsif ($key eq 'mapsize' and $cfg->{$key} < 50) {
384 $cfg->{$key} = 50;
385 }
386 });
387}
388
389sub run_config_dialog {
390 my (%events) = @_;
391
392 my $w = Gtk2::Window->new;
393
394 my @cfg = (
395 [qw/Host host/],
396 [qw/Port port/],
397 [qw/Mapsize% mapsize/],
398 [qw/Username user/],
399 [qw/Password password/],
400 );
401
402 my $cfg = {};
403
404 $w->add (my $vb = Gtk2::VBox->new);
405 $vb->pack_start (my $t = Gtk2::Table->new (2, scalar @cfg), 0, 0, 0);
406 my $selmode = $::CFG->{width} . 'x' . $::CFG->{height};
407 $t->attach_defaults (Gtk2::Label->new ("Modes"), 0, 1, 0, 1);
408 $t->attach_defaults (my $cb = Gtk2::ComboBox->new_text, 1, 2, 0, 1);
409 my $i = 0;
410 my $act = 0;
411 for (map { "$_->[0]x$_->[1]" } @SDL_MODES) {
412 if ($_ eq $selmode) { $act = $i }
413 $cb->append_text ($_);
414 $i++;
415 }
416 $cb->set_active ($act);
417 $cb->signal_connect (changed => sub {
418 my ($cb) = @_;
419 my $txt = $cb->get_active_text;
420 if ($txt =~ m/(\d+)x(\d+)/) {
421 $::CFG->{width} = $1;
422 $::CFG->{height} = $2;
423 }
424 });
425
426 $cfg->{_i} = 1;
427 for (@cfg) {
428 gtk_add_cfg_field ($t, $cfg, $_->[0], $_->[1], $::CFG->{$_->[1]});
429 }
430
431 $vb->pack_start (my $hb = Gtk2::HBox->new, 0, 0, 0);
432 $hb->pack_start (my $cb = Gtk2::Button->new ("save"), 1, 1, 5);
433 $cb->signal_connect (clicked => sub {
434 for (keys %$cfg) {
435 $::CFG->{$_} = $cfg->{$_}
436 if $_ ne '_i';
437 }
438 CFClient::write_cfg "$CFrossfire::VARDIR/pclientrc";
439 });
440 $hb->pack_start (my $cb = Gtk2::Button->new ("login"), 1, 1, 5);
441 $cb->signal_connect (clicked => sub {
442 for (keys %$cfg) {
443 $::CFG->{$_} = $cfg->{$_}
444 if $_ ne '_i';
445 }
446 my $cb = $events{login} || sub {};
447 $cb->($::CFG->{user}, $::CFG->{password});
448 });
449 $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5);
450 $cb->signal_connect (clicked => sub {
451 my $cb = $events{logout} || sub {};
452 $cb->();
453 });
454 $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5);
455 $cb->signal_connect (clicked => sub { $w->destroy });
456
457 $w->show_all;
458
459 $w->signal_connect (destroy => sub { Gtk2->main_quit });
460}
461
462
463############################################################################# 408#############################################################################
464 409
465SDL::Init SDL_INIT_EVERYTHING;
466
467@SDL_MODES = reverse map [SDL::RectW ($_), SDL::RectH ($_)],
468 @{ SDL::ListModes 0, SDL_FULLSCREEN|SDL_HWSURFACE };
469
470CFClient::read_cfg "$Crossfire::VARDIR/pclientrc"; 410CFClient::read_cfg "$Crossfire::VARDIR/pclientrc";
471 411
472$CFG ||= { 412my %DEF_CFG = (
473 width => 640, 413 width => 640,
474 height => 480, 414 height => 480,
415 fast => 0,
416 fow_intensity => 0.45,
417 fow_smooth => 0,
475 fullscreen => 0, 418 fullscreen => 0,
476 sdl_mode => 0, 419 sdl_mode => 0,
477 mapsize => 100, 420 mapsize => 100,
478 host => "crossfire.schmorp.de", 421 host => "crossfire.schmorp.de",
479 port => 13327, 422 port => 13327,
480}; 423);
424
425while (my ($k, $v) = each %DEF_CFG) {
426 $CFG->{$k} = $v unless exists $CFG->{$k};
427}
428
429sdl_init;
430
431@SDL_MODES = reverse
432 grep $_->[0] >= 640 && $_->[1] >= 480,
433 map [SDL::RectW ($_), SDL::RectH ($_)],
434 @{ SDL::ListModes 0, SDL_FULLSCREEN | SDL_HWSURFACE | SDL_OPENGL };
435
436@SDL_MODES or CFClient::fatal "Unable to find a usable video mode\n(hardware accelerated opengl fullscreen)";
437
438$CFG->{sdl_mode} = 0 if $CFG->{sdl_mode} > @SDL_MODES;
439
440init_screen;
481 441
482{ 442{
483 my @fonts = map CFClient::find_rcfile $_, qw(uifont.ttf uifontb.ttf uifonti.ttf uifontbi.ttf); 443 my @fonts = map CFClient::find_rcfile $_, qw(uifont.ttf uifontb.ttf uifonti.ttf uifontbi.ttf);
484 444
485 CFClient::add_font $_ for @fonts; 445 CFClient::add_font $_ for @fonts;
486 CFClient::set_font $fonts[0]; 446 CFClient::set_font $fonts[0];
487} 447}
488 448
489$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; 449$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
490 450
491run_config_dialog 451Event::loop;
492 login => sub { start_game },
493 logout => sub { stop_game };
494
495main Gtk2;
496 452
497Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces"; 453Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces";
498 454

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines