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.30 by root, Sat Apr 8 13:36:26 2006 UTC vs.
Revision 1.63 by root, Mon Apr 10 22:53:50 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::Util;
21use Crossfire::Client::Widget; 20use Crossfire::Client::Widget;
22 21
22our $VERSION = '0.1';
23
24my $MAX_FPS = 30;
25my $TICKS_PER_FRAME = int 1000 / $MAX_FPS - 1; # min ticks per frame
26
23our $FACECACHE; 27our $FACECACHE;
24
25our $VERSION = '0.1';
26
27our %GL_EXT;
28 28
29our $CFG; 29our $CFG;
30our $CONN; 30our $CONN;
31 31
32our $WIDTH; 32our $WIDTH;
33our $HEIGHT; 33our $HEIGHT;
34our $FULLSCREEN; 34our $FULLSCREEN;
35 35
36our $UIFONT; 36our $FONTSIZE;
37 37
38our $SDL_TIMER; 38our $SDL_TIMER;
39our $SDL_APP; 39our $SDL_APP;
40our $SDL_EV = new SDL::Event; 40our $SDL_EV;
41our %SDL_CB; 41our %SDL_CB;
42 42
43our @GL_INIT; # hooks called on every gl init
44
45our $ALT_ENTER_MESSAGE; 43our $ALT_ENTER_MESSAGE;
44our $STATUS_LINE;
45
46my $last_refresh;
47my %ANIMATE;
48my $refresh_handler;
49
50our ($tw, $te); # Test widget #d#
46 51
47sub init_screen { 52sub init_screen {
48 $SDL_APP = new SDL::App 53 $SDL_APP = new SDL::App
49 -flags => SDL_ANYFORMAT | SDL_HWSURFACE, 54 -flags => SDL_ANYFORMAT | SDL_HWSURFACE,
50 -title => "Crossfire+ Client", 55 -title => "Crossfire+ Client",
51 -width => $WIDTH, 56 -width => $WIDTH,
52 -height => $HEIGHT, 57 -height => $HEIGHT,
53 -opengl => 1, 58 -opengl => 1,
54 -red_size => 8, 59 -red_size => 5,
55 -green_size => 8, 60 -green_size => 5,
56 -blue_size => 8, 61 -blue_size => 5,
62 -alpha_size => 0,
57 -double_buffer => 1, 63 -double_buffer => 1,
58 -fullscreen => $FULLSCREEN, 64 -fullscreen => $FULLSCREEN,
59 -resizeable => 0; 65 -resizeable => 0;
60 66
61 %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions; 67 $SDL_EV = new SDL::Event;
68 $SDL_EV->set_unicode (1);
62 69
63 $GL_EXT{GL_ARB_texture_non_power_of_two} 70 $SDL_TIMER = add Glib::Timeout 1000/50, sub {
64 or warn "WARNING: non-power-of-two opengl extension required"; 71 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
72 while $SDL_EV->poll;
65 73
66 $UIFONT = SDL::TTFOpenFont Crossfire::Client::find_rcfile "uifont.ttf", $HEIGHT / 40 74 1
67 or die "TTFOpenFont: $!"; 75 };
68 76
69 $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label 0, $HEIGHT - $HEIGHT / 40, 10, $UIFONT, "Alt-Enter toggles fullscreen mode"; 77 $last_refresh = SDL::GetTicks;
70 $ALT_ENTER_MESSAGE->move (0, $HEIGHT - ($ALT_ENTER_MESSAGE->size_request)[1]);
71 $ALT_ENTER_MESSAGE->activate;
72 78
79 Crossfire::Client::gl_init;
80
81 $FONTSIZE = int $HEIGHT / 50;
82
83 #############################################################################
84
73 glClearColor 0, 0, 0, 0; 85 glClearColor 0.45, 0.45, 0.45, 1;
74 86
75 glEnable GL_TEXTURE_2D; 87 glEnable GL_TEXTURE_2D;
88 glEnable GL_COLOR_MATERIAL;
76 glShadeModel GL_FLAT; 89 glShadeModel GL_FLAT;
77 glDisable GL_DEPTH_TEST; 90 glDisable GL_DEPTH_TEST;
78 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 91 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
79 92
80 glMatrixMode GL_PROJECTION; 93 #############################################################################
81 glLoadIdentity; 94
82 glOrtho 0, $WIDTH, $HEIGHT, 0, -100 , 100; 95 $STATUS_LINE = new Crossfire::Client::Widget::Label
96 0, $HEIGHT * 59 / 60 - $FONTSIZE, 1, $FONTSIZE,
97 "";
98 $Crossfire::Client::Widget::TOPLEVEL->add ($STATUS_LINE);
83 99
84 glMatrixMode GL_MODELVIEW; 100 $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label
101 0, $HEIGHT * 59 / 60, 1, $HEIGHT / 60,
102 "Use <b>Alt-Enter</b> to toggle fullscreen mode";
103 $Crossfire::Client::Widget::TOPLEVEL->add ($ALT_ENTER_MESSAGE);
85 104
86 $_->() for @GL_INIT; 105 # Test code #d#
106 unless ($tw) { # haha...
107 $te = new Crossfire::Client::Widget::FancyFrame;
108 $te->add (new Crossfire::Client::Widget::Entry);
109 $te->move (300, 0, 2);
110 $Crossfire::Client::Widget::TOPLEVEL->add ($te);
111
112 $tw = new Crossfire::Client::Widget::Animator;
113 my $lbl1 = new Crossfire::Client::Widget::Label
114 0, 0, 10, $FONTSIZE, "<i>This</i> is a\n<u>TEST</u>!\nOf a themed\nFrame!";
115 my $lbl2 = new Crossfire::Client::Widget::Label
116 0, 0, 10, $FONTSIZE, "LBL2";
117
118 my $vb = new Crossfire::Client::Widget::VBox;
119 my $f = new Crossfire::Client::Widget::FancyFrame;
120 my $f2 = new Crossfire::Client::Widget::FancyFrame;
121 $f->add ($lbl1);
122 $f2->add ($lbl2);
123 $vb->add ($f);
124 $vb->add ($f2, 1);
125
126 $tw->add ($vb);
127 $tw->w (400);
128 $tw->h (300);
129 $tw->move ($WIDTH - 200, 0);
130 $tw->moveto (0, 0);
131 $Crossfire::Client::Widget::TOPLEVEL->add ($tw);
132
133# $f->move ($WIDTH - 200, 0);
134# $Crossfire::Client::Widget::TOPLEVEL->add ($f);
135 }
136}
137
138sub destroy_screen {
139 remove Glib::Source $SDL_TIMER;
140 undef $SDL_APP;
141 undef $SDL_EV;
142 SDL::Quit;
87} 143}
88 144
89sub start_game { 145sub start_game {
90 $SDL_TIMER = add Glib::Timeout 1000/100, sub {
91 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
92 while $SDL_EV->poll;
93
94 1
95 };
96
97 $WIDTH = $CFG->{width}; 146 $WIDTH = $CFG->{width};
98 $HEIGHT = $CFG->{height}; 147 $HEIGHT = $CFG->{height};
99 $FULLSCREEN = 0; 148 $FULLSCREEN = 0;
100 149
101 init_screen; 150 init_screen;
151
152 my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32;
102 153
103 $CONN = new conn 154 $CONN = new conn
104 host => $CFG->{host}, 155 host => $CFG->{host},
105 port => $CFG->{port}, 156 port => $CFG->{port},
106 user => $CFG->{user}, 157 user => $CFG->{user},
107 pass => $CFG->{password}; 158 pass => $CFG->{password},
159 mapw => $mapsize,
160 maph => $mapsize,
161 ;
162
163 Crossfire::Client::lowdelay fileno $CONN->{fh};
108} 164}
109 165
110sub stop_game { 166sub stop_game {
111 remove Glib::Source $SDL_TIMER; 167 remove Glib::Source $refresh_handler if $refresh_handler;
168 undef $refresh_handler;
112 169
113 undef $SDL_APP; 170 undef $CONN;
114 SDL::Quit; 171 destroy_screen;
115} 172}
116
117 173
118sub force_refresh { 174sub force_refresh {
119 glViewport 0, 0, $WIDTH, $HEIGHT; 175 glViewport 0, 0, $WIDTH, $HEIGHT;
176
177 glMatrixMode GL_PROJECTION;
178 glLoadIdentity;
179 glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000;
180 glMatrixMode GL_MODELVIEW;
181
120 glClear GL_COLOR_BUFFER_BIT; 182 glClear GL_COLOR_BUFFER_BIT;
121 183
122 $_->draw for @Crossfire::Client::Widget::ACTIVE_WIDGETS; 184 $Crossfire::Client::Widget::TOPLEVEL->draw;
123 185
124 SDL::GLSwapBuffers; 186 SDL::GLSwapBuffers;
125} 187}
126
127my $refresh_handler;
128 188
129sub refresh { 189sub refresh {
130 $refresh_handler ||= add Glib::Idle sub { 190 $refresh_handler ||= add Glib::Idle sub {
191 if ($SDL_APP) {
192 my $next_refresh = SDL::GetTicks;
193
194 if ($next_refresh - $last_refresh < $TICKS_PER_FRAME) {
195 SDL::Delay $TICKS_PER_FRAME - ($next_refresh - $last_refresh);
196 $next_refresh = SDL::GetTicks;
197 }
198
199 my $interval = ($next_refresh - $last_refresh) * 0.001;
200 $last_refresh = $next_refresh;
201
131 force_refresh; 202 force_refresh;
203 $_->animate ($interval) for grep $_, values %ANIMATE;
204
205 if (%ANIMATE) {
206 1
207 } else {
208 undef $refresh_handler;
209 0
210 }
211 } else {
132 undef $refresh_handler; 212 undef $refresh_handler;
213 0
133 0 214 }
134 }; 215 };
216}
217
218sub animation_start {
219 my ($widget) = @_;
220 $ANIMATE{$widget} = $widget;
221 Scalar::Util::weaken $ANIMATE{$widget};
222
223 refresh;
224}
225
226sub animation_stop {
227 my ($widget) = @_;
228 delete $ANIMATE{$widget};
135} 229}
136 230
137%SDL_CB = ( 231%SDL_CB = (
138 SDL_QUIT() => sub { 232 SDL_QUIT() => sub {
139 main_quit Gtk2; 233 main_quit Gtk2;
154 }, 248 },
155 SDL_KEYUP() => sub { 249 SDL_KEYUP() => sub {
156 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); 250 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV);
157 }, 251 },
158 SDL_MOUSEMOTION() => sub { 252 SDL_MOUSEMOTION() => sub {
159 warn "sdl motion\n";#d# 253 Crossfire::Client::Widget::feed_sdl_motion_event ($SDL_EV);
160 }, 254 },
161 SDL_MOUSEBUTTONDOWN() => sub { 255 SDL_MOUSEBUTTONDOWN() => sub {
162 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); 256 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV);
163 }, 257 },
164 SDL_MOUSEBUTTONUP() => sub { 258 SDL_MOUSEBUTTONUP() => sub {
165 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV); 259 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV);
166 }, 260 },
167 SDL_ACTIVEEVENT() => sub { 261 SDL_ACTIVEEVENT() => sub {
168 warn "active\n";#d# 262 printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d#
169 }, 263 },
170); 264);
171 265
172@conn::ISA = Crossfire::Protocol::; 266@conn::ISA = Crossfire::Protocol::;
173 267
178} 272}
179 273
180sub conn::map_scroll { 274sub conn::map_scroll {
181 my ($self, $dx, $dy) = @_; 275 my ($self, $dx, $dy) = @_;
182 276
183 refresh; 277# refresh;
184} 278}
185 279
186sub conn::map_clear { 280sub conn::map_clear {
187 my ($self) = @_; 281 my ($self) = @_;
188 282
189 refresh; 283# refresh;
190} 284}
191 285
192sub conn::face_find { 286sub conn::face_find {
193 my ($self, $face) = @_; 287 my ($self, $face) = @_;
194 288
199 my ($self, $face) = @_; 293 my ($self, $face) = @_;
200 294
201 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; 295 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
202 296
203 $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image}; 297 $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image};
298}
299
300sub conn::query {
301 my ($self, $flags, $prompt) = @_;
302
303 warn "<<<<QUERY:$flags:$prompt>>>\n";#d#
204} 304}
205 305
206sub gtk_add_cfg_field { 306sub gtk_add_cfg_field {
207 my ($tbl, $cfg, $klbl, $key, $value) = @_; 307 my ($tbl, $cfg, $klbl, $key, $value) = @_;
208 my $i = $cfg->{_i}++; 308 my $i = $cfg->{_i}++;
214 } 314 }
215 $ent->set_text ($value); 315 $ent->set_text ($value);
216 $ent->signal_connect (changed => sub { 316 $ent->signal_connect (changed => sub {
217 my ($ent) = @_; 317 my ($ent) = @_;
218 $cfg->{$key} = $ent->get_text; 318 $cfg->{$key} = $ent->get_text;
319 # TODO: Mapsize should be a slider in the game gui
320 if ($key eq 'mapsize' and $cfg->{$key} > 100) {
321 $cfg->{$key} = 100;
322 } elsif ($key eq 'mapsize' and $cfg->{$key} < 50) {
323 $cfg->{$key} = 50;
324 }
219 }); 325 });
220} 326}
221 327
222sub run_config_dialog { 328sub run_config_dialog {
223 my (%events) = @_; 329 my (%events) = @_;
225 my $w = Gtk2::Window->new; 331 my $w = Gtk2::Window->new;
226 332
227 my @cfg = ( 333 my @cfg = (
228 [qw/Host host/], 334 [qw/Host host/],
229 [qw/Port port/], 335 [qw/Port port/],
336 [qw/Mapsize% mapsize/],
230 [qw/Username user/], 337 [qw/Username user/],
231 [qw/Password password/], 338 [qw/Password password/],
232 ); 339 );
233 340
234 my $cfg = {}; 341 my $cfg = {};
281 my $cb = $events{login} || sub {}; 388 my $cb = $events{login} || sub {};
282 $cb->($::CFG->{user}, $::CFG->{password}); 389 $cb->($::CFG->{user}, $::CFG->{password});
283 }); 390 });
284 $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5); 391 $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5);
285 $cb->signal_connect (clicked => sub { 392 $cb->signal_connect (clicked => sub {
286 my $cb = $events{login} || sub {}; 393 my $cb = $events{logout} || sub {};
287 $cb->(); 394 $cb->();
288 }); 395 });
289 $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5); 396 $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5);
290 $cb->signal_connect (clicked => sub { $w->destroy }); 397 $cb->signal_connect (clicked => sub { $w->destroy });
291 398
296 403
297 404
298############################################################################# 405#############################################################################
299 406
300SDL::Init SDL_INIT_EVERYTHING; 407SDL::Init SDL_INIT_EVERYTHING;
301SDL::TTFInit;
302 408
303my $mapwidget = Crossfire::Client::Widget::MapWidget->new; 409my $mapwidget = Crossfire::Client::Widget::MapWidget->new;
304 410
305$mapwidget->activate; 411$Crossfire::Client::Widget::TOPLEVEL->add ($mapwidget);
306$mapwidget->focus_in; 412$mapwidget->focus_in;
307 413
308Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; 414Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc";
309 415
310$CFG ||= { 416$CFG ||= {
311 width => 640, 417 width => 640,
312 height => 480, 418 height => 480,
419 mapsize => 100,
313 fullscreen => 0, 420 fullscreen => 0,
314 host => "crossfire.schmorp.de", 421 host => "crossfire.schmorp.de",
315 port => 13327, 422 port => 13327,
316}; 423};
317 424
425Crossfire::Client::set_font Crossfire::Client::find_rcfile "uifont.ttf";
426
318$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; 427$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
319 428
320run_config_dialog 429run_config_dialog
321 login => sub { start_game }, 430 login => sub { start_game },
322 logout => sub { stop_game }; 431 logout => sub { stop_game };

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines