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.34 by elmex, Sat Apr 8 18:18:09 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::Widget; 20use Crossfire::Client::Widget;
21 21
22our $VERSION = '0.1';
23
24my $MAX_FPS = 30;
25my $TICKS_PER_FRAME = int 1000 / $MAX_FPS - 1; # min ticks per frame
26
22our $FACECACHE; 27our $FACECACHE;
23
24our $VERSION = '0.1';
25
26our %GL_EXT;
27 28
28our $CFG; 29our $CFG;
29our $CONN; 30our $CONN;
30 31
31our $WIDTH; 32our $WIDTH;
32our $HEIGHT; 33our $HEIGHT;
33our $FULLSCREEN; 34our $FULLSCREEN;
34 35
35our $UIFONT; 36our $FONTSIZE;
36 37
37our $SDL_TIMER; 38our $SDL_TIMER;
38our $SDL_APP; 39our $SDL_APP;
39our $SDL_EV = new SDL::Event; 40our $SDL_EV;
40our %SDL_CB; 41our %SDL_CB;
41 42
42our @GL_INIT; # hooks called on every gl init
43
44our $ALT_ENTER_MESSAGE; 43our $ALT_ENTER_MESSAGE;
44our $STATUS_LINE;
45 45
46my $last_refresh;
47my %ANIMATE;
48my $refresh_handler;
49
46our $tw; # Test widget #d# 50our ($tw, $te); # Test widget #d#
47 51
48sub init_screen { 52sub init_screen {
49 $SDL_APP = new SDL::App 53 $SDL_APP = new SDL::App
50 -flags => SDL_ANYFORMAT | SDL_HWSURFACE, 54 -flags => SDL_ANYFORMAT | SDL_HWSURFACE,
51 -title => "Crossfire+ Client", 55 -title => "Crossfire+ Client",
52 -width => $WIDTH, 56 -width => $WIDTH,
53 -height => $HEIGHT, 57 -height => $HEIGHT,
54 -opengl => 1, 58 -opengl => 1,
55 -red_size => 8, 59 -red_size => 5,
56 -green_size => 8, 60 -green_size => 5,
57 -blue_size => 8, 61 -blue_size => 5,
62 -alpha_size => 0,
58 -double_buffer => 1, 63 -double_buffer => 1,
59 -fullscreen => $FULLSCREEN, 64 -fullscreen => $FULLSCREEN,
60 -resizeable => 0; 65 -resizeable => 0;
61 66
62 %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions; 67 $SDL_EV = new SDL::Event;
68 $SDL_EV->set_unicode (1);
63 69
64 $GL_EXT{GL_ARB_texture_non_power_of_two} 70 $SDL_TIMER = add Glib::Timeout 1000/50, sub {
65 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;
66 73
67 $UIFONT = SDL::TTFOpenFont Crossfire::Client::find_rcfile "uifont.ttf", $HEIGHT / 40 74 1
68 or die "TTFOpenFont: $!"; 75 };
69 76
70 $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label 0, $HEIGHT - $HEIGHT / 40, 10, $UIFONT, "Alt-Enter toggles fullscreen mode"; 77 $last_refresh = SDL::GetTicks;
71 $ALT_ENTER_MESSAGE->move (0, $HEIGHT - ($ALT_ENTER_MESSAGE->size_request)[1]);
72 $ALT_ENTER_MESSAGE->activate;
73 78
74 # Test code #d# 79 Crossfire::Client::gl_init;
75 $tw = new Crossfire::Client::Widget::Window;
76 $tw->add (my $lbl = new Crossfire::Client::Widget::Label 0, $HEIGHT - $HEIGHT / 40, 10, $UIFONT, "Foo in the garden!");
77# $tw = new Crossfire::Client::Widget::Label 0, $HEIGHT - $HEIGHT / 40, 10, $UIFONT, "Foo in the garden!";
78 80
79 $tw->move (0, $HEIGHT - 50); 81 $FONTSIZE = int $HEIGHT / 50;
80 $tw->activate;
81 # Test code end #d#
82 82
83 #############################################################################
84
83 glClearColor 0, 0, 0, 0; 85 glClearColor 0.45, 0.45, 0.45, 1;
84 86
85 glEnable GL_TEXTURE_2D; 87 glEnable GL_TEXTURE_2D;
88 glEnable GL_COLOR_MATERIAL;
86 glShadeModel GL_FLAT; 89 glShadeModel GL_FLAT;
87 glDisable GL_DEPTH_TEST; 90 glDisable GL_DEPTH_TEST;
88 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 91 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
89 92
90 glMatrixMode GL_PROJECTION; 93 #############################################################################
91 glLoadIdentity; 94
92 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);
93 99
94 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);
95 104
96 $_->() 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;
97} 143}
98 144
99sub start_game { 145sub start_game {
100 $SDL_TIMER = add Glib::Timeout 1000/100, sub {
101 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
102 while $SDL_EV->poll;
103
104 1
105 };
106
107 $WIDTH = $CFG->{width}; 146 $WIDTH = $CFG->{width};
108 $HEIGHT = $CFG->{height}; 147 $HEIGHT = $CFG->{height};
109 $FULLSCREEN = 0; 148 $FULLSCREEN = 0;
110 149
111 init_screen; 150 init_screen;
151
152 my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32;
112 153
113 $CONN = new conn 154 $CONN = new conn
114 host => $CFG->{host}, 155 host => $CFG->{host},
115 port => $CFG->{port}, 156 port => $CFG->{port},
116 user => $CFG->{user}, 157 user => $CFG->{user},
117 pass => $CFG->{password}; 158 pass => $CFG->{password},
159 mapw => $mapsize,
160 maph => $mapsize,
161 ;
162
163 Crossfire::Client::lowdelay fileno $CONN->{fh};
118} 164}
119 165
120sub stop_game { 166sub stop_game {
121 remove Glib::Source $SDL_TIMER; 167 remove Glib::Source $refresh_handler if $refresh_handler;
168 undef $refresh_handler;
122 169
123 undef $SDL_APP; 170 undef $CONN;
124 SDL::Quit; 171 destroy_screen;
125} 172}
126
127 173
128sub force_refresh { 174sub force_refresh {
129 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
130 glClear GL_COLOR_BUFFER_BIT; 182 glClear GL_COLOR_BUFFER_BIT;
131 183
132 $_->draw for @Crossfire::Client::Widget::ACTIVE_WIDGETS; 184 $Crossfire::Client::Widget::TOPLEVEL->draw;
133 185
134 SDL::GLSwapBuffers; 186 SDL::GLSwapBuffers;
135} 187}
136
137my $refresh_handler;
138 188
139sub refresh { 189sub refresh {
140 $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
141 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 {
142 undef $refresh_handler; 212 undef $refresh_handler;
213 0
143 0 214 }
144 }; 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};
145} 229}
146 230
147%SDL_CB = ( 231%SDL_CB = (
148 SDL_QUIT() => sub { 232 SDL_QUIT() => sub {
149 main_quit Gtk2; 233 main_quit Gtk2;
164 }, 248 },
165 SDL_KEYUP() => sub { 249 SDL_KEYUP() => sub {
166 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); 250 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV);
167 }, 251 },
168 SDL_MOUSEMOTION() => sub { 252 SDL_MOUSEMOTION() => sub {
169 warn "sdl motion\n";#d# 253 Crossfire::Client::Widget::feed_sdl_motion_event ($SDL_EV);
170 }, 254 },
171 SDL_MOUSEBUTTONDOWN() => sub { 255 SDL_MOUSEBUTTONDOWN() => sub {
172 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); 256 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV);
173 }, 257 },
174 SDL_MOUSEBUTTONUP() => sub { 258 SDL_MOUSEBUTTONUP() => sub {
175 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV); 259 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV);
176 }, 260 },
177 SDL_ACTIVEEVENT() => sub { 261 SDL_ACTIVEEVENT() => sub {
178 warn "active\n";#d# 262 printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d#
179 }, 263 },
180); 264);
181 265
182@conn::ISA = Crossfire::Protocol::; 266@conn::ISA = Crossfire::Protocol::;
183 267
188} 272}
189 273
190sub conn::map_scroll { 274sub conn::map_scroll {
191 my ($self, $dx, $dy) = @_; 275 my ($self, $dx, $dy) = @_;
192 276
193 refresh; 277# refresh;
194} 278}
195 279
196sub conn::map_clear { 280sub conn::map_clear {
197 my ($self) = @_; 281 my ($self) = @_;
198 282
199 refresh; 283# refresh;
200} 284}
201 285
202sub conn::face_find { 286sub conn::face_find {
203 my ($self, $face) = @_; 287 my ($self, $face) = @_;
204 288
230 } 314 }
231 $ent->set_text ($value); 315 $ent->set_text ($value);
232 $ent->signal_connect (changed => sub { 316 $ent->signal_connect (changed => sub {
233 my ($ent) = @_; 317 my ($ent) = @_;
234 $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 }
235 }); 325 });
236} 326}
237 327
238sub run_config_dialog { 328sub run_config_dialog {
239 my (%events) = @_; 329 my (%events) = @_;
241 my $w = Gtk2::Window->new; 331 my $w = Gtk2::Window->new;
242 332
243 my @cfg = ( 333 my @cfg = (
244 [qw/Host host/], 334 [qw/Host host/],
245 [qw/Port port/], 335 [qw/Port port/],
336 [qw/Mapsize% mapsize/],
246 [qw/Username user/], 337 [qw/Username user/],
247 [qw/Password password/], 338 [qw/Password password/],
248 ); 339 );
249 340
250 my $cfg = {}; 341 my $cfg = {};
297 my $cb = $events{login} || sub {}; 388 my $cb = $events{login} || sub {};
298 $cb->($::CFG->{user}, $::CFG->{password}); 389 $cb->($::CFG->{user}, $::CFG->{password});
299 }); 390 });
300 $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);
301 $cb->signal_connect (clicked => sub { 392 $cb->signal_connect (clicked => sub {
302 my $cb = $events{login} || sub {}; 393 my $cb = $events{logout} || sub {};
303 $cb->(); 394 $cb->();
304 }); 395 });
305 $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);
306 $cb->signal_connect (clicked => sub { $w->destroy }); 397 $cb->signal_connect (clicked => sub { $w->destroy });
307 398
312 403
313 404
314############################################################################# 405#############################################################################
315 406
316SDL::Init SDL_INIT_EVERYTHING; 407SDL::Init SDL_INIT_EVERYTHING;
317SDL::TTFInit;
318 408
319my $mapwidget = Crossfire::Client::Widget::MapWidget->new; 409my $mapwidget = Crossfire::Client::Widget::MapWidget->new;
320 410
321$mapwidget->activate; 411$Crossfire::Client::Widget::TOPLEVEL->add ($mapwidget);
322$mapwidget->focus_in; 412$mapwidget->focus_in;
323 413
324Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; 414Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc";
325 415
326$CFG ||= { 416$CFG ||= {
327 width => 640, 417 width => 640,
328 height => 480, 418 height => 480,
419 mapsize => 100,
329 fullscreen => 0, 420 fullscreen => 0,
330 host => "crossfire.schmorp.de", 421 host => "crossfire.schmorp.de",
331 port => 13327, 422 port => 13327,
332}; 423};
333 424
425Crossfire::Client::set_font Crossfire::Client::find_rcfile "uifont.ttf";
426
334$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; 427$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
335 428
336run_config_dialog 429run_config_dialog
337 login => sub { start_game }, 430 login => sub { start_game },
338 logout => sub { stop_game }; 431 logout => sub { stop_game };

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines