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.55 by root, Sun Apr 9 22:27:52 2006 UTC vs.
Revision 1.73 by root, Tue Apr 11 19:31:19 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::Client;
18use Crossfire::Protocol; 17use Crossfire::Protocol;
19 18
20use Crossfire::Client::Widget; 19use CFClient;
20use CFClient::UI;
21
22our $VERSION = '0.1';
23
24my $MAX_FPS = 30;
25my $TICKS_PER_FRAME = int 1000 / $MAX_FPS - 1; # min ticks per frame
21 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
36our $NOW;
37
38our $MAPWIDGET;
35our $FONTSIZE; 39our $FONTSIZE;
36our $FOCUS;
37our $HOVER;
38 40
39our $SDL_TIMER; 41our $SDL_TIMER;
40our $SDL_APP; 42our $SDL_APP;
41our $SDL_EV = new SDL::Event; 43our $SDL_EV;
42our %SDL_CB; 44our %SDL_CB;
43
44our @GL_INIT; # hooks called on every gl init
45 45
46our $ALT_ENTER_MESSAGE; 46our $ALT_ENTER_MESSAGE;
47our $STATUS_LINE; 47our $STATUS_LINE;
48 48our $DEBUG_STATUS;
49our $TOPLEVEL;
50
51our $tw; # Test widget #d#
52 49
53my $last_refresh; 50my $last_refresh;
54my %ANIMATE; 51my %ANIMATE;
55my $refresh_handler; 52my $refresh_handler;
53
54our ($tw, $te); # Test widget #d#
56 55
57sub init_screen { 56sub init_screen {
58 $SDL_APP = new SDL::App 57 $SDL_APP = new SDL::App
59 -flags => SDL_ANYFORMAT | SDL_HWSURFACE, 58 -flags => SDL_ANYFORMAT | SDL_HWSURFACE,
60 -title => "Crossfire+ Client", 59 -title => "Crossfire+ Client",
67 -alpha_size => 0, 66 -alpha_size => 0,
68 -double_buffer => 1, 67 -double_buffer => 1,
69 -fullscreen => $FULLSCREEN, 68 -fullscreen => $FULLSCREEN,
70 -resizeable => 0; 69 -resizeable => 0;
71 70
71 $SDL_EV = new SDL::Event;
72 $SDL_EV->set_unicode (1);
73
74 $SDL_TIMER = add Glib::Timeout 1000/50, sub {
75 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
76 while $SDL_EV->poll;
77
78 1
79 };
80
72 $last_refresh = SDL::GetTicks; 81 $last_refresh = SDL::GetTicks;
73 82
74 %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions; 83 CFClient::gl_init;
75
76 $GL_EXT{GL_ARB_texture_non_power_of_two}
77 or warn "WARNING: non-power-of-two opengl extension required";
78 84
79 $FONTSIZE = int $HEIGHT / 50; 85 $FONTSIZE = int $HEIGHT / 50;
80 86
81 ############################################################################# 87 #############################################################################
82 88
83 glClearColor 0, 0, 0, 0; 89 glClearColor 0.45, 0.45, 0.45, 1;
84 90
85 glEnable GL_TEXTURE_2D; 91 glEnable GL_TEXTURE_2D;
86 glEnable GL_COLOR_MATERIAL; 92 glEnable GL_COLOR_MATERIAL;
87 glShadeModel GL_FLAT; 93 glShadeModel GL_FLAT;
88 glDisable GL_DEPTH_TEST; 94 glDisable GL_DEPTH_TEST;
89 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 95 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
90 96
91 $_->() for @GL_INIT;
92
93 ############################################################################# 97 #############################################################################
98
99 $DEBUG_STATUS = new CFClient::UI::Label;
100 $CFClient::UI::TOPLEVEL->add ($DEBUG_STATUS);
94 101
95 $STATUS_LINE = new Crossfire::Client::Widget::Label 102 $STATUS_LINE = new CFClient::UI::Label
96 0, $HEIGHT * 59 / 60 - $FONTSIZE, 1, $FONTSIZE, 103 y => $HEIGHT * 59 / 60 - $FONTSIZE;
97 "";
98 $TOPLEVEL->add ($STATUS_LINE); 104 $CFClient::UI::TOPLEVEL->add ($STATUS_LINE);
99 105
100 $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label 106 $ALT_ENTER_MESSAGE = new CFClient::UI::Label
101 0, $HEIGHT * 59 / 60, 1, $HEIGHT / 60, 107 y => $HEIGHT * 59 / 60,
108 height => $HEIGHT / 60,
102 "Use <b>Alt-Enter</b> to toggle fullscreen mode"; 109 text => "Use <b>Alt-Enter</b> to toggle fullscreen mode";
103 $TOPLEVEL->add ($ALT_ENTER_MESSAGE); 110 $CFClient::UI::TOPLEVEL->add ($ALT_ENTER_MESSAGE);
104 111
105 # Test code #d# 112 $MAPWIDGET = new CFClient::UI::MapWidget;
106 unless ($tw) { # haha... 113 $CFClient::UI::TOPLEVEL->add ($MAPWIDGET);
107 $tw = new Crossfire::Client::Widget::Animator; 114 $MAPWIDGET->focus_in;
108 my $lbl1 = new Crossfire::Client::Widget::Label 115}
109 0, 0, 10, $FONTSIZE, "<i>This</i> is a\n<u>TEST</u>!\nOf a themed\nFrame!";
110 my $lbl2 = new Crossfire::Client::Widget::Label
111 0, 0, 10, $FONTSIZE, "LBL2";
112 116
113 my $vb = new Crossfire::Client::Widget::VBox; 117sub destroy_screen {
114 my $f = new Crossfire::Client::Widget::FancyFrame; 118 remove Glib::Source $SDL_TIMER;
115 my $f2 = new Crossfire::Client::Widget::FancyFrame; 119 undef $SDL_APP;
116 $f->add ($lbl1); 120 undef $SDL_EV;
117 $f2->add ($lbl2); 121 SDL::Quit;
118 $vb->add ($f); 122}
119 $vb->add ($f2, 1);
120 123
121 $tw->add ($vb); 124sub config_dialog {
122 $tw->w (400); 125 my $dialog = new CFClient::UI::FancyFrame x => 300, y => 100,
123 $tw->h (300); 126 child => (my $vbox = new CFClient::UI::VBox);
124 $tw->move ($WIDTH - 200, 0); 127 $vbox->add (new CFClient::UI::Label align => 0, text => "Setup");
125 $tw->moveto (0, 0);
126 $TOPLEVEL->add ($tw);
127 128
128# $f->move ($WIDTH - 200, 0); 129 $CFClient::UI::TOPLEVEL->add ($dialog);
129# $TOPLEVEL->add ($f);
130 }
131} 130}
132 131
133sub start_game { 132sub start_game {
134 $SDL_TIMER = add Glib::Timeout 1000/50, sub {
135 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
136 while $SDL_EV->poll;
137
138 1
139 };
140
141 $WIDTH = $CFG->{width}; 133 $WIDTH = $CFG->{width};
142 $HEIGHT = $CFG->{height}; 134 $HEIGHT = $CFG->{height};
143 $FULLSCREEN = 0; 135 $FULLSCREEN = 0;
144 136
145 init_screen; 137 init_screen;
146 138
139 config_dialog;
140
147 my $mapsize = List::Util::min 64, List::Util::max 11, int $HEIGHT * $CFG->{mapsize} * 0.01 / 32; 141 my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32;
148 142
149 $CONN = new conn 143 $CONN = new conn
150 host => $CFG->{host}, 144 host => $CFG->{host},
151 port => $CFG->{port}, 145 port => $CFG->{port},
152 user => $CFG->{user}, 146 user => $CFG->{user},
153 pass => $CFG->{password}, 147 pass => $CFG->{password},
154 mapw => $mapsize, 148 mapw => $mapsize,
155 maph => $mapsize, 149 maph => $mapsize,
156 ; 150 ;
157 151
158 Crossfire::Client::lowdelay fileno $CONN->{fh}; 152 CFClient::lowdelay fileno $CONN->{fh};
159} 153}
160 154
161sub stop_game { 155sub stop_game {
162 remove Glib::Source $SDL_TIMER;
163 remove Glib::Source $refresh_handler if $refresh_handler; 156 remove Glib::Source $refresh_handler if $refresh_handler;
164 undef $refresh_handler; 157 undef $refresh_handler;
165 158
166 undef $SDL_APP;
167 undef $CONN; 159 undef $CONN;
168 SDL::Quit; 160 destroy_screen;
169} 161}
170
171 162
172sub force_refresh { 163sub force_refresh {
173 glViewport 0, 0, $WIDTH, $HEIGHT; 164 glViewport 0, 0, $WIDTH, $HEIGHT;
174 165
175 glMatrixMode GL_PROJECTION; 166 glMatrixMode GL_PROJECTION;
176 glLoadIdentity; 167 glLoadIdentity;
177 glOrtho 0, $WIDTH, $HEIGHT, 0, -6000 , 6000; 168 glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000;
178 glMatrixMode GL_MODELVIEW; 169 glMatrixMode GL_MODELVIEW;
170 glLoadIdentity;
179 171
180 glClear GL_COLOR_BUFFER_BIT; 172 glClear GL_COLOR_BUFFER_BIT;
181 173
182 $TOPLEVEL->draw; 174 $CFClient::UI::TOPLEVEL->draw;
183 175
184 SDL::GLSwapBuffers; 176 SDL::GLSwapBuffers;
185} 177}
178
179sub debug {
180 $DEBUG_STATUS->set_text ($_[0]);
181 $DEBUG_STATUS->size_allocate ($DEBUG_STATUS->size_request);
182 $DEBUG_STATUS->move ($WIDTH - $DEBUG_STATUS->{w}, 0);
183}
184
185my $FPS;
186 186
187sub refresh { 187sub refresh {
188 $refresh_handler ||= add Glib::Idle sub { 188 $refresh_handler ||= add Glib::Idle sub {
189 return unless $SDL_APP; 189 if ($SDL_APP) {
190 $NOW = SDL::GetTicks;
190 191
191 my $next_refresh = SDL::GetTicks; 192 if ($NOW - $last_refresh < $TICKS_PER_FRAME) {
193 SDL::Delay $TICKS_PER_FRAME - ($NOW - $last_refresh);
194 $NOW = SDL::GetTicks;
195 }
196
192 my $interval = ($next_refresh - $last_refresh) * 0.001; 197 my $interval = ($NOW - $last_refresh) * 0.001;
193 $last_refresh = $next_refresh; 198 $last_refresh = $NOW;
194 199
200 if ($interval) {
201 $FPS ||= 1 / $interval;
202 $FPS = $FPS * 0.96 + (1 / $interval) * 0.04;
203 debug sprintf "%5.02f", $FPS;
204 }
205
195 force_refresh; 206 force_refresh;
196 $_->animate ($interval) for grep $_, values %ANIMATE; 207 $_->animate ($interval) for grep $_, values %ANIMATE;
197 208
198 if (%ANIMATE) { 209 if (%ANIMATE) {
210 1
211 } else {
212 undef $refresh_handler;
213 0
199 1 214 }
200 } else { 215 } else {
201 undef $refresh_handler; 216 undef $refresh_handler;
202 0 217 0
203 } 218 }
204 }; 219 };
230 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) { 245 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) {
231 # alt-enter 246 # alt-enter
232 $FULLSCREEN = !$FULLSCREEN; 247 $FULLSCREEN = !$FULLSCREEN;
233 init_screen; 248 init_screen;
234 } else { 249 } else {
235 Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV); 250 CFClient::UI::feed_sdl_key_down_event ($SDL_EV);
236 } 251 }
237 }, 252 },
238 SDL_KEYUP() => sub { 253 SDL_KEYUP() => sub {
239 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); 254 CFClient::UI::feed_sdl_key_up_event ($SDL_EV);
240 }, 255 },
241 SDL_MOUSEMOTION() => sub { 256 SDL_MOUSEMOTION() => sub {
242 my ($x, $y) = ($SDL_EV->motion_x, $SDL_EV->motion_y); 257 CFClient::UI::feed_sdl_motion_event ($SDL_EV);
243 $HOVER = $TOPLEVEL->find_widget ($x, $y);
244 $self->refresh;
245
246 warn "mouse $x, $y = $HOVER\n";
247 }, 258 },
248 SDL_MOUSEBUTTONDOWN() => sub { 259 SDL_MOUSEBUTTONDOWN() => sub {
249 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); 260 CFClient::UI::feed_sdl_button_down_event ($SDL_EV);
250 }, 261 },
251 SDL_MOUSEBUTTONUP() => sub { 262 SDL_MOUSEBUTTONUP() => sub {
252 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV); 263 CFClient::UI::feed_sdl_button_up_event ($SDL_EV);
253 }, 264 },
254 SDL_ACTIVEEVENT() => sub { 265 SDL_ACTIVEEVENT() => sub {
255 warn "active\n";#d# 266# printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d#
256 }, 267 },
257); 268);
258 269
259@conn::ISA = Crossfire::Protocol::; 270@conn::ISA = Crossfire::Protocol::;
260 271
261sub conn::map_update { 272sub conn::map_update {
262 my ($self, $dirty) = @_; 273 my ($self, $dirty) = @_;
263 274
264 refresh; 275 $MAPWIDGET->update;
265} 276}
266 277
267sub conn::map_scroll { 278sub conn::map_scroll {
268 my ($self, $dx, $dy) = @_; 279 my ($self, $dx, $dy) = @_;
269 280
285sub conn::face_update { 296sub conn::face_update {
286 my ($self, $face) = @_; 297 my ($self, $face) = @_;
287 298
288 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; 299 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
289 300
290 $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image}; 301 $face->{texture} = new_from_image CFClient::Texture delete $face->{image};
291} 302}
292 303
293sub conn::query { 304sub conn::query {
294 my ($self, $flags, $prompt) = @_; 305 my ($self, $flags, $prompt) = @_;
295 306
368 $cb->signal_connect (clicked => sub { 379 $cb->signal_connect (clicked => sub {
369 for (keys %$cfg) { 380 for (keys %$cfg) {
370 $::CFG->{$_} = $cfg->{$_} 381 $::CFG->{$_} = $cfg->{$_}
371 if $_ ne '_i'; 382 if $_ ne '_i';
372 } 383 }
373 Crossfire::Client::write_cfg "$Crossfire::VARDIR/pclientrc"; 384 CFClient::write_cfg "$CFrossfire::VARDIR/pclientrc";
374 }); 385 });
375 $hb->pack_start (my $cb = Gtk2::Button->new ("login"), 1, 1, 5); 386 $hb->pack_start (my $cb = Gtk2::Button->new ("login"), 1, 1, 5);
376 $cb->signal_connect (clicked => sub { 387 $cb->signal_connect (clicked => sub {
377 for (keys %$cfg) { 388 for (keys %$cfg) {
378 $::CFG->{$_} = $cfg->{$_} 389 $::CFG->{$_} = $cfg->{$_}
397 408
398############################################################################# 409#############################################################################
399 410
400SDL::Init SDL_INIT_EVERYTHING; 411SDL::Init SDL_INIT_EVERYTHING;
401 412
402$TOPLEVEL = Crossfire::Client::Widget::Toplevel->new;
403
404my $mapwidget = Crossfire::Client::Widget::MapWidget->new;
405
406$TOPLEVEL->add ($mapwidget);
407$mapwidget->focus_in;
408
409Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; 413CFClient::read_cfg "$Crossfire::VARDIR/pclientrc";
410 414
411$CFG ||= { 415$CFG ||= {
412 width => 640, 416 width => 640,
413 height => 480, 417 height => 480,
414 mapsize => 100, 418 mapsize => 100,
415 fullscreen => 0, 419 fullscreen => 0,
416 host => "crossfire.schmorp.de", 420 host => "crossfire.schmorp.de",
417 port => 13327, 421 port => 13327,
418}; 422};
419 423
420Crossfire::Client::set_font Crossfire::Client::find_rcfile "uifont.ttf"; 424{
425 my @fonts = map CFClient::find_rcfile $_, qw(uifont.ttf uifontb.ttf uifonti.ttf uifontbi.ttf);
426
427 CFClient::add_font $_ for @fonts;
428 CFClient::set_font $fonts[0];
429}
421 430
422$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; 431$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
423 432
424run_config_dialog 433run_config_dialog
425 login => sub { start_game }, 434 login => sub { start_game },

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines