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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines