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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines