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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines