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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines