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.39 by root, Sun Apr 9 00:09:50 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
43my $last_refresh;
44my %ANIMATE;
45my $refresh_handler;
46
46our $tw; # Test widget #d# 47our ($tw, $te); # Test widget #d#
47 48
48sub init_screen { 49sub init_screen {
49 $SDL_APP = new SDL::App 50 $SDL_APP = new SDL::App
50 -flags => SDL_ANYFORMAT | SDL_HWSURFACE, 51 -flags => SDL_ANYFORMAT | SDL_HWSURFACE,
51 -title => "Crossfire+ Client", 52 -title => "Crossfire+ Client",
52 -width => $WIDTH, 53 -width => $WIDTH,
53 -height => $HEIGHT, 54 -height => $HEIGHT,
54 -opengl => 1, 55 -opengl => 1,
55 -red_size => 8, 56 -red_size => 5,
56 -green_size => 8, 57 -green_size => 5,
57 -blue_size => 8, 58 -blue_size => 5,
59 -alpha_size => 0,
58 -double_buffer => 1, 60 -double_buffer => 1,
59 -fullscreen => $FULLSCREEN, 61 -fullscreen => $FULLSCREEN,
60 -resizeable => 0; 62 -resizeable => 0;
61 63
62 %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions; 64 $SDL_EV = new SDL::Event;
65 $SDL_EV->set_unicode (1);
63 66
64 $GL_EXT{GL_ARB_texture_non_power_of_two} 67 $SDL_TIMER = add Glib::Timeout 1000/50, sub {
65 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;
66 70
71 1
72 };
73
74 $last_refresh = SDL::GetTicks;
75
76 Crossfire::Client::gl_init;
77
67 $FONTSIZE = int $HEIGHT / 40; 78 $FONTSIZE = int $HEIGHT / 50;
68 79
69 $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label 0, $FONTSIZE, $HEIGHT - $FONTSIZE, $FONTSIZE, "Alt-Enter toggles fullscreen mode"; 80 #############################################################################
70 $ALT_ENTER_MESSAGE->move (0, $HEIGHT - ($ALT_ENTER_MESSAGE->size_request)[1]);
71 $ALT_ENTER_MESSAGE->activate;
72 81
73 # Test code #d#
74 {
75 my $frm = new Crossfire::Client::Widget::Frame;
76 $tw = new Crossfire::Client::Widget::Window;
77 $frm->add (new Crossfire::Client::Widget::Label 0, 0, 10, $FONTSIZE, "Foo in the garden!");
78 $tw->add ($frm);
79
80 $tw->move (0, $HEIGHT - 120);
81 $tw->activate;
82 }
83
84 glClearColor 0, 0, 0, 0; 82 glClearColor 0.45, 0.45, 0.45, 1;
85 83
86 glEnable GL_TEXTURE_2D; 84 glEnable GL_TEXTURE_2D;
87 glEnable GL_COLOR_MATERIAL; 85 glEnable GL_COLOR_MATERIAL;
88 glShadeModel GL_FLAT; 86 glShadeModel GL_FLAT;
89 glDisable GL_DEPTH_TEST; 87 glDisable GL_DEPTH_TEST;
90 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 88 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
91 89
92 $_->() 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;
93} 140}
94 141
95sub start_game { 142sub start_game {
96 $SDL_TIMER = add Glib::Timeout 1000/100, sub {
97 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
98 while $SDL_EV->poll;
99
100 1
101 };
102
103 $WIDTH = $CFG->{width}; 143 $WIDTH = $CFG->{width};
104 $HEIGHT = $CFG->{height}; 144 $HEIGHT = $CFG->{height};
105 $FULLSCREEN = 0; 145 $FULLSCREEN = 0;
106 146
107 init_screen; 147 init_screen;
148
149 my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32;
108 150
109 $CONN = new conn 151 $CONN = new conn
110 host => $CFG->{host}, 152 host => $CFG->{host},
111 port => $CFG->{port}, 153 port => $CFG->{port},
112 user => $CFG->{user}, 154 user => $CFG->{user},
113 pass => $CFG->{password}, 155 pass => $CFG->{password},
114 mapw => 50, 156 mapw => $mapsize,
115 maph => 37, 157 maph => $mapsize,
116 ; 158 ;
159
160 Crossfire::Client::lowdelay fileno $CONN->{fh};
117} 161}
118 162
119sub stop_game { 163sub stop_game {
120 remove Glib::Source $SDL_TIMER; 164 remove Glib::Source $refresh_handler if $refresh_handler;
165 undef $refresh_handler;
121 166
122 undef $SDL_APP; 167 undef $CONN;
123 SDL::Quit; 168 destroy_screen;
124} 169}
125
126 170
127sub force_refresh { 171sub force_refresh {
128 glViewport 0, 0, $WIDTH, $HEIGHT; 172 glViewport 0, 0, $WIDTH, $HEIGHT;
129 173
130 glMatrixMode GL_PROJECTION; 174 glMatrixMode GL_PROJECTION;
131 glLoadIdentity; 175 glLoadIdentity;
132 glOrtho 0, $WIDTH, $HEIGHT, 0, -100 , 100; 176 glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000;
133 glMatrixMode GL_MODELVIEW; 177 glMatrixMode GL_MODELVIEW;
134 178
135 glClear GL_COLOR_BUFFER_BIT; 179 glClear GL_COLOR_BUFFER_BIT;
136 180
137 $_->draw for @Crossfire::Client::Widget::ACTIVE_WIDGETS; 181 $Crossfire::Client::Widget::TOPLEVEL->draw;
138 182
139 SDL::GLSwapBuffers; 183 SDL::GLSwapBuffers;
140} 184}
141
142my $refresh_handler;
143 185
144sub refresh { 186sub refresh {
145 $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
146 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 {
147 undef $refresh_handler; 203 undef $refresh_handler;
204 0
148 0 205 }
149 }; 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};
150} 220}
151 221
152%SDL_CB = ( 222%SDL_CB = (
153 SDL_QUIT() => sub { 223 SDL_QUIT() => sub {
154 main_quit Gtk2; 224 main_quit Gtk2;
169 }, 239 },
170 SDL_KEYUP() => sub { 240 SDL_KEYUP() => sub {
171 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); 241 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV);
172 }, 242 },
173 SDL_MOUSEMOTION() => sub { 243 SDL_MOUSEMOTION() => sub {
174 warn "sdl motion\n";#d# 244 Crossfire::Client::Widget::feed_sdl_motion_event ($SDL_EV);
175 }, 245 },
176 SDL_MOUSEBUTTONDOWN() => sub { 246 SDL_MOUSEBUTTONDOWN() => sub {
177 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); 247 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV);
178 }, 248 },
179 SDL_MOUSEBUTTONUP() => sub { 249 SDL_MOUSEBUTTONUP() => sub {
180 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV); 250 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV);
181 }, 251 },
182 SDL_ACTIVEEVENT() => sub { 252 SDL_ACTIVEEVENT() => sub {
183 warn "active\n";#d# 253 printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d#
184 }, 254 },
185); 255);
186 256
187@conn::ISA = Crossfire::Protocol::; 257@conn::ISA = Crossfire::Protocol::;
188 258
193} 263}
194 264
195sub conn::map_scroll { 265sub conn::map_scroll {
196 my ($self, $dx, $dy) = @_; 266 my ($self, $dx, $dy) = @_;
197 267
198 refresh; 268# refresh;
199} 269}
200 270
201sub conn::map_clear { 271sub conn::map_clear {
202 my ($self) = @_; 272 my ($self) = @_;
203 273
204 refresh; 274# refresh;
205} 275}
206 276
207sub conn::face_find { 277sub conn::face_find {
208 my ($self, $face) = @_; 278 my ($self, $face) = @_;
209 279
235 } 305 }
236 $ent->set_text ($value); 306 $ent->set_text ($value);
237 $ent->signal_connect (changed => sub { 307 $ent->signal_connect (changed => sub {
238 my ($ent) = @_; 308 my ($ent) = @_;
239 $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 }
240 }); 316 });
241} 317}
242 318
243sub run_config_dialog { 319sub run_config_dialog {
244 my (%events) = @_; 320 my (%events) = @_;
246 my $w = Gtk2::Window->new; 322 my $w = Gtk2::Window->new;
247 323
248 my @cfg = ( 324 my @cfg = (
249 [qw/Host host/], 325 [qw/Host host/],
250 [qw/Port port/], 326 [qw/Port port/],
327 [qw/Mapsize% mapsize/],
251 [qw/Username user/], 328 [qw/Username user/],
252 [qw/Password password/], 329 [qw/Password password/],
253 ); 330 );
254 331
255 my $cfg = {}; 332 my $cfg = {};
302 my $cb = $events{login} || sub {}; 379 my $cb = $events{login} || sub {};
303 $cb->($::CFG->{user}, $::CFG->{password}); 380 $cb->($::CFG->{user}, $::CFG->{password});
304 }); 381 });
305 $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);
306 $cb->signal_connect (clicked => sub { 383 $cb->signal_connect (clicked => sub {
307 my $cb = $events{login} || sub {}; 384 my $cb = $events{logout} || sub {};
308 $cb->(); 385 $cb->();
309 }); 386 });
310 $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);
311 $cb->signal_connect (clicked => sub { $w->destroy }); 388 $cb->signal_connect (clicked => sub { $w->destroy });
312 389
320 397
321SDL::Init SDL_INIT_EVERYTHING; 398SDL::Init SDL_INIT_EVERYTHING;
322 399
323my $mapwidget = Crossfire::Client::Widget::MapWidget->new; 400my $mapwidget = Crossfire::Client::Widget::MapWidget->new;
324 401
325$mapwidget->activate; 402$Crossfire::Client::Widget::TOPLEVEL->add ($mapwidget);
326$mapwidget->focus_in; 403$mapwidget->focus_in;
327 404
328Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; 405Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc";
329 406
330$CFG ||= { 407$CFG ||= {
331 width => 640, 408 width => 640,
332 height => 480, 409 height => 480,
410 mapsize => 100,
333 fullscreen => 0, 411 fullscreen => 0,
334 host => "crossfire.schmorp.de", 412 host => "crossfire.schmorp.de",
335 port => 13327, 413 port => 13327,
336}; 414};
337 415
416Crossfire::Client::set_font Crossfire::Client::find_rcfile "uifont.ttf";
417
338$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; 418$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
339 419
340run_config_dialog 420run_config_dialog
341 login => sub { start_game }, 421 login => sub { start_game },
342 logout => sub { stop_game }; 422 logout => sub { stop_game };

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines