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.41 by elmex, Sun Apr 9 01:21:11 2006 UTC vs.
Revision 1.62 by root, Mon Apr 10 19:34:04 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';
25
26our %GL_EXT;
27 25
28our $CFG; 26our $CFG;
29our $CONN; 27our $CONN;
30 28
31our $WIDTH; 29our $WIDTH;
34 32
35our $FONTSIZE; 33our $FONTSIZE;
36 34
37our $SDL_TIMER; 35our $SDL_TIMER;
38our $SDL_APP; 36our $SDL_APP;
39our $SDL_EV = new SDL::Event; 37our $SDL_EV;
40our %SDL_CB; 38our %SDL_CB;
41 39
42our @GL_INIT; # hooks called on every gl init
43
44our $ALT_ENTER_MESSAGE; 40our $ALT_ENTER_MESSAGE;
41our $STATUS_LINE;
45 42
46our $TOPLEVEL; 43my $last_refresh;
44my %ANIMATE;
45my $refresh_handler;
47 46
48our $tw; # Test widget #d# 47our ($tw, $te); # Test widget #d#
49 48
50sub init_screen { 49sub init_screen {
51 $SDL_APP = new SDL::App 50 $SDL_APP = new SDL::App
52 -flags => SDL_ANYFORMAT | SDL_HWSURFACE, 51 -flags => SDL_ANYFORMAT | SDL_HWSURFACE,
53 -title => "Crossfire+ Client", 52 -title => "Crossfire+ Client",
54 -width => $WIDTH, 53 -width => $WIDTH,
55 -height => $HEIGHT, 54 -height => $HEIGHT,
56 -opengl => 1, 55 -opengl => 1,
57 -red_size => 8, 56 -red_size => 5,
58 -green_size => 8, 57 -green_size => 5,
59 -blue_size => 8, 58 -blue_size => 5,
59 -alpha_size => 0,
60 -double_buffer => 1, 60 -double_buffer => 1,
61 -fullscreen => $FULLSCREEN, 61 -fullscreen => $FULLSCREEN,
62 -resizeable => 0; 62 -resizeable => 0;
63 63
64 %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions; 64 $SDL_EV = new SDL::Event;
65 $SDL_EV->set_unicode (1);
65 66
66 $GL_EXT{GL_ARB_texture_non_power_of_two} 67 $SDL_TIMER = add Glib::Timeout 1000/50, sub {
67 or warn "WARNING: non-power-of-two opengl extension required"; 68 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
69 while $SDL_EV->poll;
70
71 1
72 };
73
74 $last_refresh = SDL::GetTicks;
75
76 Crossfire::Client::gl_init;
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 0.45, 0.45, 0.45, 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 #############################################################################
91
92 $STATUS_LINE = new Crossfire::Client::Widget::Label
93 0, $HEIGHT * 59 / 60 - $FONTSIZE, 1, $FONTSIZE,
94 "";
95 $Crossfire::Client::Widget::TOPLEVEL->add ($STATUS_LINE);
96
97 $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label
98 0, $HEIGHT * 59 / 60, 1, $HEIGHT / 60,
99 "Use <b>Alt-Enter</b> to toggle fullscreen mode";
100 $Crossfire::Client::Widget::TOPLEVEL->add ($ALT_ENTER_MESSAGE);
101
102 # Test code #d#
103 unless ($tw) { # haha...
104 $te = new Crossfire::Client::Widget::FancyFrame;
105 $te->add (new Crossfire::Client::Widget::Entry);
106 $te->move (300, 0, 2);
107 $Crossfire::Client::Widget::TOPLEVEL->add ($te);
108
109 $tw = new Crossfire::Client::Widget::Animator;
110 my $lbl1 = new Crossfire::Client::Widget::Label
111 0, 0, 10, $FONTSIZE, "<i>This</i> is a\n<u>TEST</u>!\nOf a themed\nFrame!";
112 my $lbl2 = new Crossfire::Client::Widget::Label
113 0, 0, 10, $FONTSIZE, "LBL2";
114
115 my $vb = new Crossfire::Client::Widget::VBox;
116 my $f = new Crossfire::Client::Widget::FancyFrame;
117 my $f2 = new Crossfire::Client::Widget::FancyFrame;
118 $f->add ($lbl1);
119 $f2->add ($lbl2);
120 $vb->add ($f);
121 $vb->add ($f2, 1);
122
123 $tw->add ($vb);
124 $tw->w (400);
125 $tw->h (300);
126 $tw->move ($WIDTH - 200, 0);
127 $tw->moveto (0, 0);
128 $Crossfire::Client::Widget::TOPLEVEL->add ($tw);
129
130# $f->move ($WIDTH - 200, 0);
131# $Crossfire::Client::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;
98} 140}
99 141
100sub start_game { 142sub start_game {
101 $SDL_TIMER = add Glib::Timeout 1000/100, sub {
102 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
103 while $SDL_EV->poll;
104
105 1
106 };
107
108 $WIDTH = $CFG->{width}; 143 $WIDTH = $CFG->{width};
109 $HEIGHT = $CFG->{height}; 144 $HEIGHT = $CFG->{height};
110 $FULLSCREEN = 0; 145 $FULLSCREEN = 0;
111 146
112 init_screen; 147 init_screen;
148
149 my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32;
113 150
114 $CONN = new conn 151 $CONN = new conn
115 host => $CFG->{host}, 152 host => $CFG->{host},
116 port => $CFG->{port}, 153 port => $CFG->{port},
117 user => $CFG->{user}, 154 user => $CFG->{user},
118 pass => $CFG->{password}, 155 pass => $CFG->{password},
119 mapw => 50, 156 mapw => $mapsize,
120 maph => 37, 157 maph => $mapsize,
121 ; 158 ;
159
160 Crossfire::Client::lowdelay fileno $CONN->{fh};
122} 161}
123 162
124sub stop_game { 163sub stop_game {
125 remove Glib::Source $SDL_TIMER; 164 remove Glib::Source $refresh_handler if $refresh_handler;
165 undef $refresh_handler;
126 166
127 undef $SDL_APP; 167 undef $CONN;
128 SDL::Quit; 168 destroy_screen;
129} 169}
130
131 170
132sub force_refresh { 171sub force_refresh {
133 glViewport 0, 0, $WIDTH, $HEIGHT; 172 glViewport 0, 0, $WIDTH, $HEIGHT;
134 173
135 glMatrixMode GL_PROJECTION; 174 glMatrixMode GL_PROJECTION;
136 glLoadIdentity; 175 glLoadIdentity;
137 glOrtho 0, $WIDTH, $HEIGHT, 0, -100 , 100; 176 glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000;
138 glMatrixMode GL_MODELVIEW; 177 glMatrixMode GL_MODELVIEW;
139 178
140 glClear GL_COLOR_BUFFER_BIT; 179 glClear GL_COLOR_BUFFER_BIT;
141 180
142 $TOPLEVEL->draw; 181 $Crossfire::Client::Widget::TOPLEVEL->draw;
143 182
144 SDL::GLSwapBuffers; 183 SDL::GLSwapBuffers;
145} 184}
146
147my $refresh_handler;
148 185
149sub refresh { 186sub refresh {
150 $refresh_handler ||= add Glib::Idle sub { 187 $refresh_handler ||= add Glib::Idle sub {
188 if ($SDL_APP) {
189 my $next_refresh = SDL::GetTicks;
190 my $interval = ($next_refresh - $last_refresh) * 0.001;
191 $last_refresh = $next_refresh;
192
151 force_refresh; 193 force_refresh;
194 $_->animate ($interval) for grep $_, values %ANIMATE;
195
196 if (%ANIMATE) {
197 1
198 } else {
199 undef $refresh_handler;
200 0
201 }
202 } else {
152 undef $refresh_handler; 203 undef $refresh_handler;
204 0
153 0 205 }
154 }; 206 };
207}
208
209sub animation_start {
210 my ($widget) = @_;
211 $ANIMATE{$widget} = $widget;
212 Scalar::Util::weaken $ANIMATE{$widget};
213
214 refresh;
215}
216
217sub animation_stop {
218 my ($widget) = @_;
219 delete $ANIMATE{$widget};
155} 220}
156 221
157%SDL_CB = ( 222%SDL_CB = (
158 SDL_QUIT() => sub { 223 SDL_QUIT() => sub {
159 main_quit Gtk2; 224 main_quit Gtk2;
174 }, 239 },
175 SDL_KEYUP() => sub { 240 SDL_KEYUP() => sub {
176 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); 241 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV);
177 }, 242 },
178 SDL_MOUSEMOTION() => sub { 243 SDL_MOUSEMOTION() => sub {
179 warn "sdl motion\n";#d# 244 Crossfire::Client::Widget::feed_sdl_motion_event ($SDL_EV);
180 }, 245 },
181 SDL_MOUSEBUTTONDOWN() => sub { 246 SDL_MOUSEBUTTONDOWN() => sub {
182 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); 247 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV);
183 }, 248 },
184 SDL_MOUSEBUTTONUP() => sub { 249 SDL_MOUSEBUTTONUP() => sub {
185 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV); 250 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV);
186 }, 251 },
187 SDL_ACTIVEEVENT() => sub { 252 SDL_ACTIVEEVENT() => sub {
188 warn "active\n";#d# 253 printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d#
189 }, 254 },
190); 255);
191 256
192@conn::ISA = Crossfire::Protocol::; 257@conn::ISA = Crossfire::Protocol::;
193 258
198} 263}
199 264
200sub conn::map_scroll { 265sub conn::map_scroll {
201 my ($self, $dx, $dy) = @_; 266 my ($self, $dx, $dy) = @_;
202 267
203 refresh; 268# refresh;
204} 269}
205 270
206sub conn::map_clear { 271sub conn::map_clear {
207 my ($self) = @_; 272 my ($self) = @_;
208 273
209 refresh; 274# refresh;
210} 275}
211 276
212sub conn::face_find { 277sub conn::face_find {
213 my ($self, $face) = @_; 278 my ($self, $face) = @_;
214 279
240 } 305 }
241 $ent->set_text ($value); 306 $ent->set_text ($value);
242 $ent->signal_connect (changed => sub { 307 $ent->signal_connect (changed => sub {
243 my ($ent) = @_; 308 my ($ent) = @_;
244 $cfg->{$key} = $ent->get_text; 309 $cfg->{$key} = $ent->get_text;
310 # TODO: Mapsize should be a slider in the game gui
311 if ($key eq 'mapsize' and $cfg->{$key} > 100) {
312 $cfg->{$key} = 100;
313 } elsif ($key eq 'mapsize' and $cfg->{$key} < 50) {
314 $cfg->{$key} = 50;
315 }
245 }); 316 });
246} 317}
247 318
248sub run_config_dialog { 319sub run_config_dialog {
249 my (%events) = @_; 320 my (%events) = @_;
251 my $w = Gtk2::Window->new; 322 my $w = Gtk2::Window->new;
252 323
253 my @cfg = ( 324 my @cfg = (
254 [qw/Host host/], 325 [qw/Host host/],
255 [qw/Port port/], 326 [qw/Port port/],
327 [qw/Mapsize% mapsize/],
256 [qw/Username user/], 328 [qw/Username user/],
257 [qw/Password password/], 329 [qw/Password password/],
258 ); 330 );
259 331
260 my $cfg = {}; 332 my $cfg = {};
307 my $cb = $events{login} || sub {}; 379 my $cb = $events{login} || sub {};
308 $cb->($::CFG->{user}, $::CFG->{password}); 380 $cb->($::CFG->{user}, $::CFG->{password});
309 }); 381 });
310 $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5); 382 $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5);
311 $cb->signal_connect (clicked => sub { 383 $cb->signal_connect (clicked => sub {
312 my $cb = $events{login} || sub {}; 384 my $cb = $events{logout} || sub {};
313 $cb->(); 385 $cb->();
314 }); 386 });
315 $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5); 387 $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5);
316 $cb->signal_connect (clicked => sub { $w->destroy }); 388 $cb->signal_connect (clicked => sub { $w->destroy });
317 389
323 395
324############################################################################# 396#############################################################################
325 397
326SDL::Init SDL_INIT_EVERYTHING; 398SDL::Init SDL_INIT_EVERYTHING;
327 399
328$TOPLEVEL = Crossfire::Client::Widget::Toplevel->new;
329
330my $mapwidget = Crossfire::Client::Widget::MapWidget->new; 400my $mapwidget = Crossfire::Client::Widget::MapWidget->new;
331 401
332$TOPLEVEL->add ($mapwidget); 402$Crossfire::Client::Widget::TOPLEVEL->add ($mapwidget);
333$mapwidget->focus_in; 403$mapwidget->focus_in;
334 404
335Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; 405Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc";
336 406
337$CFG ||= { 407$CFG ||= {
338 width => 640, 408 width => 640,
339 height => 480, 409 height => 480,
410 mapsize => 100,
340 fullscreen => 0, 411 fullscreen => 0,
341 host => "crossfire.schmorp.de", 412 host => "crossfire.schmorp.de",
342 port => 13327, 413 port => 13327,
343}; 414};
344 415
345Crossfire::Client::add_font Crossfire::Client::find_rcfile "uifont.ttf"; 416Crossfire::Client::set_font Crossfire::Client::find_rcfile "uifont.ttf";
346Crossfire::Client::set_font "[Bitstream Vera Sans Mono]";
347 417
348$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; 418$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
349 419
350run_config_dialog 420run_config_dialog
351 login => sub { start_game }, 421 login => sub { start_game },

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines