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.35 by root, Sat Apr 8 18:48:35 2006 UTC vs.
Revision 1.61 by root, Mon Apr 10 11:57:17 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';
30 30
31our $WIDTH; 31our $WIDTH;
32our $HEIGHT; 32our $HEIGHT;
33our $FULLSCREEN; 33our $FULLSCREEN;
34 34
35our $UIFONT; 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
47my $last_refresh;
48my %ANIMATE;
49my $refresh_handler;
50
46our $tw; # Test widget #d# 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
68 $SDL_EV = new SDL::Event;
69 $SDL_EV->set_unicode (1);
70
71 $last_refresh = SDL::GetTicks;
72
62 %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions; 73 %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions;
63 74
64 $GL_EXT{GL_ARB_texture_non_power_of_two} 75 $GL_EXT{GL_ARB_texture_non_power_of_two}
65 or warn "WARNING: non-power-of-two opengl extension required"; 76 or warn "WARNING: non-power-of-two opengl extension required";
66 77
67 $UIFONT = SDL::TTFOpenFont Crossfire::Client::find_rcfile "uifont.ttf", $HEIGHT / 40 78 $FONTSIZE = int $HEIGHT / 50;
68 or die "TTFOpenFont: $!";
69 79
70 $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label 0, $HEIGHT - $HEIGHT / 40, 10, $UIFONT, "Alt-Enter toggles fullscreen mode"; 80 #############################################################################
71 $ALT_ENTER_MESSAGE->move (0, $HEIGHT - ($ALT_ENTER_MESSAGE->size_request)[1]);
72 $ALT_ENTER_MESSAGE->activate;
73 81
74 # Test code #d#
75 $tw = new Crossfire::Client::Widget::Window;
76 $tw->add (my $lbl = new Crossfire::Client::Widget::Label 0, 0, 10, $UIFONT, "Foo in the garden!");
77# $tw = new Crossfire::Client::Widget::Label 0, $HEIGHT - $HEIGHT / 40, 10, $UIFONT, "Foo in the garden!";
78
79 $tw->move (0, 0);
80 $tw->activate;
81 # Test code end #d#
82
83 glClearColor 0, 0, 0, 0; 82 glClearColor 0.7, 0.7, 0.7, 1;
84 83
85 glEnable GL_TEXTURE_2D; 84 glEnable GL_TEXTURE_2D;
85 glEnable GL_COLOR_MATERIAL;
86 glShadeModel GL_FLAT; 86 glShadeModel GL_FLAT;
87 glDisable GL_DEPTH_TEST; 87 glDisable GL_DEPTH_TEST;
88 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 88 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
89 89
90 $_->() 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 }
91} 135}
92 136
93sub start_game { 137sub start_game {
94 $SDL_TIMER = add Glib::Timeout 1000/100, sub { 138 $SDL_TIMER = add Glib::Timeout 1000/50, sub {
95 ($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 })->()
96 while $SDL_EV->poll; 140 while $SDL_EV->poll;
97 141
98 1 142 1
99 }; 143 };
101 $WIDTH = $CFG->{width}; 145 $WIDTH = $CFG->{width};
102 $HEIGHT = $CFG->{height}; 146 $HEIGHT = $CFG->{height};
103 $FULLSCREEN = 0; 147 $FULLSCREEN = 0;
104 148
105 init_screen; 149 init_screen;
150
151 my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32;
106 152
107 $CONN = new conn 153 $CONN = new conn
108 host => $CFG->{host}, 154 host => $CFG->{host},
109 port => $CFG->{port}, 155 port => $CFG->{port},
110 user => $CFG->{user}, 156 user => $CFG->{user},
111 pass => $CFG->{password}; 157 pass => $CFG->{password},
158 mapw => $mapsize,
159 maph => $mapsize,
160 ;
161
162 Crossfire::Client::lowdelay fileno $CONN->{fh};
112} 163}
113 164
114sub stop_game { 165sub stop_game {
115 remove Glib::Source $SDL_TIMER; 166 remove Glib::Source $SDL_TIMER;
167 remove Glib::Source $refresh_handler if $refresh_handler;
168 undef $refresh_handler;
116 169
117 undef $SDL_APP; 170 undef $SDL_APP;
171 undef $CONN;
118 SDL::Quit; 172 SDL::Quit;
119} 173}
120
121 174
122sub force_refresh { 175sub force_refresh {
123 glViewport 0, 0, $WIDTH, $HEIGHT; 176 glViewport 0, 0, $WIDTH, $HEIGHT;
124 177
125 glMatrixMode GL_PROJECTION; 178 glMatrixMode GL_PROJECTION;
126 glLoadIdentity; 179 glLoadIdentity;
127 glOrtho 0, $WIDTH, $HEIGHT, 0, -100 , 100; 180 glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000;
128 glMatrixMode GL_MODELVIEW; 181 glMatrixMode GL_MODELVIEW;
129 182
130 glClear GL_COLOR_BUFFER_BIT; 183 glClear GL_COLOR_BUFFER_BIT;
131 184
132 $_->draw for @Crossfire::Client::Widget::ACTIVE_WIDGETS; 185 $Crossfire::Client::Widget::TOPLEVEL->draw;
133 186
134 SDL::GLSwapBuffers; 187 SDL::GLSwapBuffers;
135} 188}
136
137my $refresh_handler;
138 189
139sub refresh { 190sub refresh {
140 $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
141 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 {
142 undef $refresh_handler; 207 undef $refresh_handler;
208 0
143 0 209 }
144 }; 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};
145} 224}
146 225
147%SDL_CB = ( 226%SDL_CB = (
148 SDL_QUIT() => sub { 227 SDL_QUIT() => sub {
149 main_quit Gtk2; 228 main_quit Gtk2;
164 }, 243 },
165 SDL_KEYUP() => sub { 244 SDL_KEYUP() => sub {
166 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); 245 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV);
167 }, 246 },
168 SDL_MOUSEMOTION() => sub { 247 SDL_MOUSEMOTION() => sub {
169 warn "sdl motion\n";#d# 248 Crossfire::Client::Widget::feed_sdl_motion_event ($SDL_EV);
170 }, 249 },
171 SDL_MOUSEBUTTONDOWN() => sub { 250 SDL_MOUSEBUTTONDOWN() => sub {
172 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); 251 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV);
173 }, 252 },
174 SDL_MOUSEBUTTONUP() => sub { 253 SDL_MOUSEBUTTONUP() => sub {
175 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV); 254 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV);
176 }, 255 },
177 SDL_ACTIVEEVENT() => sub { 256 SDL_ACTIVEEVENT() => sub {
178 warn "active\n";#d# 257 printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d#
179 }, 258 },
180); 259);
181 260
182@conn::ISA = Crossfire::Protocol::; 261@conn::ISA = Crossfire::Protocol::;
183 262
188} 267}
189 268
190sub conn::map_scroll { 269sub conn::map_scroll {
191 my ($self, $dx, $dy) = @_; 270 my ($self, $dx, $dy) = @_;
192 271
193 refresh; 272# refresh;
194} 273}
195 274
196sub conn::map_clear { 275sub conn::map_clear {
197 my ($self) = @_; 276 my ($self) = @_;
198 277
199 refresh; 278# refresh;
200} 279}
201 280
202sub conn::face_find { 281sub conn::face_find {
203 my ($self, $face) = @_; 282 my ($self, $face) = @_;
204 283
230 } 309 }
231 $ent->set_text ($value); 310 $ent->set_text ($value);
232 $ent->signal_connect (changed => sub { 311 $ent->signal_connect (changed => sub {
233 my ($ent) = @_; 312 my ($ent) = @_;
234 $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 }
235 }); 320 });
236} 321}
237 322
238sub run_config_dialog { 323sub run_config_dialog {
239 my (%events) = @_; 324 my (%events) = @_;
241 my $w = Gtk2::Window->new; 326 my $w = Gtk2::Window->new;
242 327
243 my @cfg = ( 328 my @cfg = (
244 [qw/Host host/], 329 [qw/Host host/],
245 [qw/Port port/], 330 [qw/Port port/],
331 [qw/Mapsize% mapsize/],
246 [qw/Username user/], 332 [qw/Username user/],
247 [qw/Password password/], 333 [qw/Password password/],
248 ); 334 );
249 335
250 my $cfg = {}; 336 my $cfg = {};
297 my $cb = $events{login} || sub {}; 383 my $cb = $events{login} || sub {};
298 $cb->($::CFG->{user}, $::CFG->{password}); 384 $cb->($::CFG->{user}, $::CFG->{password});
299 }); 385 });
300 $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);
301 $cb->signal_connect (clicked => sub { 387 $cb->signal_connect (clicked => sub {
302 my $cb = $events{login} || sub {}; 388 my $cb = $events{logout} || sub {};
303 $cb->(); 389 $cb->();
304 }); 390 });
305 $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);
306 $cb->signal_connect (clicked => sub { $w->destroy }); 392 $cb->signal_connect (clicked => sub { $w->destroy });
307 393
312 398
313 399
314############################################################################# 400#############################################################################
315 401
316SDL::Init SDL_INIT_EVERYTHING; 402SDL::Init SDL_INIT_EVERYTHING;
317SDL::TTFInit;
318 403
319my $mapwidget = Crossfire::Client::Widget::MapWidget->new; 404my $mapwidget = Crossfire::Client::Widget::MapWidget->new;
320 405
321$mapwidget->activate; 406$Crossfire::Client::Widget::TOPLEVEL->add ($mapwidget);
322$mapwidget->focus_in; 407$mapwidget->focus_in;
323 408
324Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; 409Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc";
325 410
326$CFG ||= { 411$CFG ||= {
327 width => 640, 412 width => 640,
328 height => 480, 413 height => 480,
414 mapsize => 100,
329 fullscreen => 0, 415 fullscreen => 0,
330 host => "crossfire.schmorp.de", 416 host => "crossfire.schmorp.de",
331 port => 13327, 417 port => 13327,
332}; 418};
333 419
420Crossfire::Client::set_font Crossfire::Client::find_rcfile "uifont.ttf";
421
334$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; 422$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
335 423
336run_config_dialog 424run_config_dialog
337 login => sub { start_game }, 425 login => sub { start_game },
338 logout => sub { stop_game }; 426 logout => sub { stop_game };

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines