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.42 by root, Sun Apr 9 01:35:40 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::Widget; 20use Crossfire::Client::Widget;
21 21
22our $FACECACHE; 22our $FACECACHE;
23 23
24our $VERSION = '0.1'; 24our $VERSION = '0.1';
34 34
35our $FONTSIZE; 35our $FONTSIZE;
36 36
37our $SDL_TIMER; 37our $SDL_TIMER;
38our $SDL_APP; 38our $SDL_APP;
39our $SDL_EV = new SDL::Event; 39our $SDL_EV;
40our %SDL_CB; 40our %SDL_CB;
41 41
42our @GL_INIT; # hooks called on every gl init 42our @GL_INIT; # hooks called on every gl init
43 43
44our $ALT_ENTER_MESSAGE; 44our $ALT_ENTER_MESSAGE;
45our $STATUS_LINE;
45 46
46our $TOPLEVEL; 47my $last_refresh;
48my %ANIMATE;
49my $refresh_handler;
47 50
48our $tw; # Test widget #d# 51our ($tw, $te); # Test widget #d#
49 52
50sub init_screen { 53sub init_screen {
51 $SDL_APP = new SDL::App 54 $SDL_APP = new SDL::App
52 -flags => SDL_ANYFORMAT | SDL_HWSURFACE, 55 -flags => SDL_ANYFORMAT | SDL_HWSURFACE,
53 -title => "Crossfire+ Client", 56 -title => "Crossfire+ Client",
54 -width => $WIDTH, 57 -width => $WIDTH,
55 -height => $HEIGHT, 58 -height => $HEIGHT,
56 -opengl => 1, 59 -opengl => 1,
57 -red_size => 8, 60 -red_size => 5,
58 -green_size => 8, 61 -green_size => 5,
59 -blue_size => 8, 62 -blue_size => 5,
63 -alpha_size => 0,
60 -double_buffer => 1, 64 -double_buffer => 1,
61 -fullscreen => $FULLSCREEN, 65 -fullscreen => $FULLSCREEN,
62 -resizeable => 0; 66 -resizeable => 0;
63 67
68 $SDL_EV = new SDL::Event;
69 $SDL_EV->set_unicode (1);
70
71 $last_refresh = SDL::GetTicks;
72
64 %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions; 73 %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions;
65 74
66 $GL_EXT{GL_ARB_texture_non_power_of_two} 75 $GL_EXT{GL_ARB_texture_non_power_of_two}
67 or warn "WARNING: non-power-of-two opengl extension required"; 76 or warn "WARNING: non-power-of-two opengl extension required";
68 77
69 $FONTSIZE = int $HEIGHT / 50; 78 $FONTSIZE = int $HEIGHT / 50;
70 79
71 $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label 80 #############################################################################
72 0, $FONTSIZE, $HEIGHT - $FONTSIZE, $FONTSIZE,
73 "Use <b>Alt-Enter</b> to toggle fullscreen mode";
74 $ALT_ENTER_MESSAGE->move (0, $HEIGHT - ($ALT_ENTER_MESSAGE->size_request)[1]);
75 $TOPLEVEL->add ($ALT_ENTER_MESSAGE);
76 81
77 # Test code #d#
78 {
79 my $frm = new Crossfire::Client::Widget::Frame;
80 $tw = new Crossfire::Client::Widget::Window;
81 $frm->add (my $te = new Crossfire::Client::Widget::TextEntry 0, 0, 10, $FONTSIZE, "Foo in the garden!");
82 $tw->add ($frm);
83
84 $tw->move (0, $HEIGHT - 120);
85 $TOPLEVEL->add ($tw);
86# $te->focus_in;
87 }
88
89 glClearColor 0, 0, 0, 0; 82 glClearColor 1, 1, 1, 1;
90 83
91 glEnable GL_TEXTURE_2D; 84 glEnable GL_TEXTURE_2D;
92 glEnable GL_COLOR_MATERIAL; 85 glEnable GL_COLOR_MATERIAL;
93 glShadeModel GL_FLAT; 86 glShadeModel GL_FLAT;
94 glDisable GL_DEPTH_TEST; 87 glDisable GL_DEPTH_TEST;
95 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 88 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
96 89
97 $_->() for @GL_INIT; 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 }
98} 135}
99 136
100sub start_game { 137sub start_game {
101 $SDL_TIMER = add Glib::Timeout 1000/100, sub { 138 $SDL_TIMER = add Glib::Timeout 1000/50, sub {
102 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->() 139 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
103 while $SDL_EV->poll; 140 while $SDL_EV->poll;
104 141
105 1 142 1
106 }; 143 };
108 $WIDTH = $CFG->{width}; 145 $WIDTH = $CFG->{width};
109 $HEIGHT = $CFG->{height}; 146 $HEIGHT = $CFG->{height};
110 $FULLSCREEN = 0; 147 $FULLSCREEN = 0;
111 148
112 init_screen; 149 init_screen;
150
151 my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32;
113 152
114 $CONN = new conn 153 $CONN = new conn
115 host => $CFG->{host}, 154 host => $CFG->{host},
116 port => $CFG->{port}, 155 port => $CFG->{port},
117 user => $CFG->{user}, 156 user => $CFG->{user},
118 pass => $CFG->{password}, 157 pass => $CFG->{password},
119 mapw => 50, 158 mapw => $mapsize,
120 maph => 37, 159 maph => $mapsize,
121 ; 160 ;
161
162 Crossfire::Client::lowdelay fileno $CONN->{fh};
122} 163}
123 164
124sub stop_game { 165sub stop_game {
125 remove Glib::Source $SDL_TIMER; 166 remove Glib::Source $SDL_TIMER;
167 remove Glib::Source $refresh_handler if $refresh_handler;
168 undef $refresh_handler;
126 169
127 undef $SDL_APP; 170 undef $SDL_APP;
171 undef $CONN;
128 SDL::Quit; 172 SDL::Quit;
129} 173}
130
131 174
132sub force_refresh { 175sub force_refresh {
133 glViewport 0, 0, $WIDTH, $HEIGHT; 176 glViewport 0, 0, $WIDTH, $HEIGHT;
134 177
135 glMatrixMode GL_PROJECTION; 178 glMatrixMode GL_PROJECTION;
136 glLoadIdentity; 179 glLoadIdentity;
137 glOrtho 0, $WIDTH, $HEIGHT, 0, -100 , 100; 180 glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000;
138 glMatrixMode GL_MODELVIEW; 181 glMatrixMode GL_MODELVIEW;
139 182
140 glClear GL_COLOR_BUFFER_BIT; 183 glClear GL_COLOR_BUFFER_BIT;
141 184
142 $TOPLEVEL->draw; 185 $Crossfire::Client::Widget::TOPLEVEL->draw;
143 186
144 SDL::GLSwapBuffers; 187 SDL::GLSwapBuffers;
145} 188}
146
147my $refresh_handler;
148 189
149sub refresh { 190sub refresh {
150 $refresh_handler ||= add Glib::Idle sub { 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
151 force_refresh; 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 {
152 undef $refresh_handler; 207 undef $refresh_handler;
208 0
153 0 209 }
154 }; 210 };
211}
212
213sub animation_start {
214 my ($widget) = @_;
215 $ANIMATE{$widget} = $widget;
216 Scalar::Util::weaken $ANIMATE{$widget};
217
218 refresh;
219}
220
221sub animation_stop {
222 my ($widget) = @_;
223 delete $ANIMATE{$widget};
155} 224}
156 225
157%SDL_CB = ( 226%SDL_CB = (
158 SDL_QUIT() => sub { 227 SDL_QUIT() => sub {
159 main_quit Gtk2; 228 main_quit Gtk2;
174 }, 243 },
175 SDL_KEYUP() => sub { 244 SDL_KEYUP() => sub {
176 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); 245 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV);
177 }, 246 },
178 SDL_MOUSEMOTION() => sub { 247 SDL_MOUSEMOTION() => sub {
179 warn "sdl motion\n";#d# 248 Crossfire::Client::Widget::feed_sdl_motion_event ($SDL_EV);
180 }, 249 },
181 SDL_MOUSEBUTTONDOWN() => sub { 250 SDL_MOUSEBUTTONDOWN() => sub {
182 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); 251 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV);
183 }, 252 },
184 SDL_MOUSEBUTTONUP() => sub { 253 SDL_MOUSEBUTTONUP() => sub {
185 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV); 254 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV);
186 }, 255 },
187 SDL_ACTIVEEVENT() => sub { 256 SDL_ACTIVEEVENT() => sub {
188 warn "active\n";#d# 257 printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d#
189 }, 258 },
190); 259);
191 260
192@conn::ISA = Crossfire::Protocol::; 261@conn::ISA = Crossfire::Protocol::;
193 262
198} 267}
199 268
200sub conn::map_scroll { 269sub conn::map_scroll {
201 my ($self, $dx, $dy) = @_; 270 my ($self, $dx, $dy) = @_;
202 271
203 refresh; 272# refresh;
204} 273}
205 274
206sub conn::map_clear { 275sub conn::map_clear {
207 my ($self) = @_; 276 my ($self) = @_;
208 277
209 refresh; 278# refresh;
210} 279}
211 280
212sub conn::face_find { 281sub conn::face_find {
213 my ($self, $face) = @_; 282 my ($self, $face) = @_;
214 283
240 } 309 }
241 $ent->set_text ($value); 310 $ent->set_text ($value);
242 $ent->signal_connect (changed => sub { 311 $ent->signal_connect (changed => sub {
243 my ($ent) = @_; 312 my ($ent) = @_;
244 $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 }
245 }); 320 });
246} 321}
247 322
248sub run_config_dialog { 323sub run_config_dialog {
249 my (%events) = @_; 324 my (%events) = @_;
251 my $w = Gtk2::Window->new; 326 my $w = Gtk2::Window->new;
252 327
253 my @cfg = ( 328 my @cfg = (
254 [qw/Host host/], 329 [qw/Host host/],
255 [qw/Port port/], 330 [qw/Port port/],
331 [qw/Mapsize% mapsize/],
256 [qw/Username user/], 332 [qw/Username user/],
257 [qw/Password password/], 333 [qw/Password password/],
258 ); 334 );
259 335
260 my $cfg = {}; 336 my $cfg = {};
307 my $cb = $events{login} || sub {}; 383 my $cb = $events{login} || sub {};
308 $cb->($::CFG->{user}, $::CFG->{password}); 384 $cb->($::CFG->{user}, $::CFG->{password});
309 }); 385 });
310 $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);
311 $cb->signal_connect (clicked => sub { 387 $cb->signal_connect (clicked => sub {
312 my $cb = $events{login} || sub {}; 388 my $cb = $events{logout} || sub {};
313 $cb->(); 389 $cb->();
314 }); 390 });
315 $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);
316 $cb->signal_connect (clicked => sub { $w->destroy }); 392 $cb->signal_connect (clicked => sub { $w->destroy });
317 393
323 399
324############################################################################# 400#############################################################################
325 401
326SDL::Init SDL_INIT_EVERYTHING; 402SDL::Init SDL_INIT_EVERYTHING;
327 403
328$TOPLEVEL = Crossfire::Client::Widget::Toplevel->new;
329
330my $mapwidget = Crossfire::Client::Widget::MapWidget->new; 404my $mapwidget = Crossfire::Client::Widget::MapWidget->new;
331 405
332$TOPLEVEL->add ($mapwidget); 406$Crossfire::Client::Widget::TOPLEVEL->add ($mapwidget);
333$mapwidget->focus_in; 407$mapwidget->focus_in;
334 408
335Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; 409Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc";
336 410
337$CFG ||= { 411$CFG ||= {
338 width => 640, 412 width => 640,
339 height => 480, 413 height => 480,
414 mapsize => 100,
340 fullscreen => 0, 415 fullscreen => 0,
341 host => "crossfire.schmorp.de", 416 host => "crossfire.schmorp.de",
342 port => 13327, 417 port => 13327,
343}; 418};
344 419

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines