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.50 by root, Sun Apr 9 21:05:50 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;
34 35
35our $FONTSIZE; 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;
45 44our $STATUS_LINE;
46our $TOPLEVEL;
47
48our $tw; # Test widget #d#
49 45
50my $last_refresh; 46my $last_refresh;
51my %ANIMATE; 47my %ANIMATE;
52my $refresh_handler; 48my $refresh_handler;
49
50our ($tw, $te); # Test widget #d#
53 51
54sub init_screen { 52sub init_screen {
55 $SDL_APP = new SDL::App 53 $SDL_APP = new SDL::App
56 -flags => SDL_ANYFORMAT | SDL_HWSURFACE, 54 -flags => SDL_ANYFORMAT | SDL_HWSURFACE,
57 -title => "Crossfire+ Client", 55 -title => "Crossfire+ Client",
58 -width => $WIDTH, 56 -width => $WIDTH,
59 -height => $HEIGHT, 57 -height => $HEIGHT,
60 -opengl => 1, 58 -opengl => 1,
61 -red_size => 8, 59 -red_size => 5,
62 -green_size => 8, 60 -green_size => 5,
63 -blue_size => 8, 61 -blue_size => 5,
62 -alpha_size => 0,
64 -double_buffer => 1, 63 -double_buffer => 1,
65 -fullscreen => $FULLSCREEN, 64 -fullscreen => $FULLSCREEN,
66 -resizeable => 0; 65 -resizeable => 0;
67 66
67 $SDL_EV = new SDL::Event;
68 $SDL_EV->set_unicode (1);
69
70 $SDL_TIMER = add Glib::Timeout 1000/50, sub {
71 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
72 while $SDL_EV->poll;
73
74 1
75 };
76
68 $last_refresh = SDL::GetTicks; 77 $last_refresh = SDL::GetTicks;
69 78
70 %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions; 79 Crossfire::Client::gl_init;
71
72 $GL_EXT{GL_ARB_texture_non_power_of_two}
73 or warn "WARNING: non-power-of-two opengl extension required";
74 80
75 $FONTSIZE = int $HEIGHT / 50; 81 $FONTSIZE = int $HEIGHT / 50;
76 82
83 #############################################################################
84
85 glClearColor 0.45, 0.45, 0.45, 1;
86
87 glEnable GL_TEXTURE_2D;
88 glEnable GL_COLOR_MATERIAL;
89 glShadeModel GL_FLAT;
90 glDisable GL_DEPTH_TEST;
91 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
92
93 #############################################################################
94
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);
99
77 $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label 100 $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label
78 0, $FONTSIZE, $HEIGHT - $FONTSIZE, $FONTSIZE, 101 0, $HEIGHT * 59 / 60, 1, $HEIGHT / 60,
79 "Use <b>Alt-Enter</b> to toggle fullscreen mode"; 102 "Use <b>Alt-Enter</b> to toggle fullscreen mode";
80 $ALT_ENTER_MESSAGE->move (0, $HEIGHT - ($ALT_ENTER_MESSAGE->size_request)[1]); 103 $Crossfire::Client::Widget::TOPLEVEL->add ($ALT_ENTER_MESSAGE);
81 $TOPLEVEL->add ($ALT_ENTER_MESSAGE);
82 104
83 # Test code #d# 105 # Test code #d#
84 unless ($tw) { # haha... 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
85 $tw = new Crossfire::Client::Widget::Animator; 112 $tw = new Crossfire::Client::Widget::Animator;
86 my $lbl1 = new Crossfire::Client::Widget::Label 113 my $lbl1 = new Crossfire::Client::Widget::Label
87 0, 0, 10, $FONTSIZE, "<i>This</i> is a\n<u>TEST</u>!\nOf a themed\nFrame!"; 114 0, 0, 10, $FONTSIZE, "<i>This</i> is a\n<u>TEST</u>!\nOf a themed\nFrame!";
88 my $lbl2 = new Crossfire::Client::Widget::Label 115 my $lbl2 = new Crossfire::Client::Widget::Label
89 0, 0, 10, $FONTSIZE, "LBL2"; 116 0, 0, 10, $FONTSIZE, "LBL2";
99 $tw->add ($vb); 126 $tw->add ($vb);
100 $tw->w (400); 127 $tw->w (400);
101 $tw->h (300); 128 $tw->h (300);
102 $tw->move ($WIDTH - 200, 0); 129 $tw->move ($WIDTH - 200, 0);
103 $tw->moveto (0, 0); 130 $tw->moveto (0, 0);
104 $TOPLEVEL->add ($tw); 131 $Crossfire::Client::Widget::TOPLEVEL->add ($tw);
105 132
106# $f->move ($WIDTH - 200, 0); 133# $f->move ($WIDTH - 200, 0);
107# $TOPLEVEL->add ($f); 134# $Crossfire::Client::Widget::TOPLEVEL->add ($f);
108 } 135 }
136}
109 137
110 glClearColor 0, 0, 0, 0; 138sub destroy_screen {
111 139 remove Glib::Source $SDL_TIMER;
112 glEnable GL_TEXTURE_2D; 140 undef $SDL_APP;
113 glEnable GL_COLOR_MATERIAL; 141 undef $SDL_EV;
114 glShadeModel GL_FLAT; 142 SDL::Quit;
115 glDisable GL_DEPTH_TEST;
116 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
117
118 $_->() for @GL_INIT;
119} 143}
120 144
121sub start_game { 145sub start_game {
122 $SDL_TIMER = add Glib::Timeout 1000/50, sub {
123 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
124 while $SDL_EV->poll;
125
126 1
127 };
128
129 $WIDTH = $CFG->{width}; 146 $WIDTH = $CFG->{width};
130 $HEIGHT = $CFG->{height}; 147 $HEIGHT = $CFG->{height};
131 $FULLSCREEN = 0; 148 $FULLSCREEN = 0;
132 149
133 init_screen; 150 init_screen;
134 151
135 my $mapsize = List::Util::min 64, List::Util::max 11, int $HEIGHT * $CFG->{mapsize} * 0.01 / 32; 152 my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32;
136 153
137 $CONN = new conn 154 $CONN = new conn
138 host => $CFG->{host}, 155 host => $CFG->{host},
139 port => $CFG->{port}, 156 port => $CFG->{port},
140 user => $CFG->{user}, 157 user => $CFG->{user},
145 162
146 Crossfire::Client::lowdelay fileno $CONN->{fh}; 163 Crossfire::Client::lowdelay fileno $CONN->{fh};
147} 164}
148 165
149sub stop_game { 166sub stop_game {
150 remove Glib::Source $SDL_TIMER;
151 remove Glib::Source $refresh_handler; 167 remove Glib::Source $refresh_handler if $refresh_handler;
152 undef $refresh_handler; 168 undef $refresh_handler;
153 169
154 undef $SDL_APP;
155 undef $CONN; 170 undef $CONN;
156 SDL::Quit; 171 destroy_screen;
157} 172}
158
159 173
160sub force_refresh { 174sub force_refresh {
161 glViewport 0, 0, $WIDTH, $HEIGHT; 175 glViewport 0, 0, $WIDTH, $HEIGHT;
162 176
163 glMatrixMode GL_PROJECTION; 177 glMatrixMode GL_PROJECTION;
164 glLoadIdentity; 178 glLoadIdentity;
165 glOrtho 0, $WIDTH, $HEIGHT, 0, -6000 , 6000; 179 glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000;
166 glMatrixMode GL_MODELVIEW; 180 glMatrixMode GL_MODELVIEW;
167 181
168 glClear GL_COLOR_BUFFER_BIT; 182 glClear GL_COLOR_BUFFER_BIT;
169 183
170 $TOPLEVEL->draw; 184 $Crossfire::Client::Widget::TOPLEVEL->draw;
171 185
172 SDL::GLSwapBuffers; 186 SDL::GLSwapBuffers;
173} 187}
174 188
175sub refresh { 189sub refresh {
176 $refresh_handler ||= add Glib::Idle sub { 190 $refresh_handler ||= add Glib::Idle sub {
177 return unless $SDL_APP; 191 if ($SDL_APP) {
178
179 my $next_refresh = SDL::GetTicks; 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
180 my $interval = ($next_refresh - $last_refresh) * 0.001; 199 my $interval = ($next_refresh - $last_refresh) * 0.001;
181 $last_refresh = $next_refresh; 200 $last_refresh = $next_refresh;
182 201
183 force_refresh; 202 force_refresh;
184 $_->animate ($interval) for grep $_, values %ANIMATE; 203 $_->animate ($interval) for grep $_, values %ANIMATE;
185 204
186 if (%ANIMATE) { 205 if (%ANIMATE) {
206 1
207 } else {
208 undef $refresh_handler;
209 0
187 1 210 }
188 } else { 211 } else {
189 undef $refresh_handler; 212 undef $refresh_handler;
190 0 213 0
191 } 214 }
192 }; 215 };
225 }, 248 },
226 SDL_KEYUP() => sub { 249 SDL_KEYUP() => sub {
227 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); 250 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV);
228 }, 251 },
229 SDL_MOUSEMOTION() => sub { 252 SDL_MOUSEMOTION() => sub {
230 warn "sdl motion\n";#d# 253 Crossfire::Client::Widget::feed_sdl_motion_event ($SDL_EV);
231 }, 254 },
232 SDL_MOUSEBUTTONDOWN() => sub { 255 SDL_MOUSEBUTTONDOWN() => sub {
233 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); 256 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV);
234 }, 257 },
235 SDL_MOUSEBUTTONUP() => sub { 258 SDL_MOUSEBUTTONUP() => sub {
236 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV); 259 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV);
237 }, 260 },
238 SDL_ACTIVEEVENT() => sub { 261 SDL_ACTIVEEVENT() => sub {
239 warn "active\n";#d# 262 printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d#
240 }, 263 },
241); 264);
242 265
243@conn::ISA = Crossfire::Protocol::; 266@conn::ISA = Crossfire::Protocol::;
244 267
381 404
382############################################################################# 405#############################################################################
383 406
384SDL::Init SDL_INIT_EVERYTHING; 407SDL::Init SDL_INIT_EVERYTHING;
385 408
386$TOPLEVEL = Crossfire::Client::Widget::Toplevel->new;
387
388my $mapwidget = Crossfire::Client::Widget::MapWidget->new; 409my $mapwidget = Crossfire::Client::Widget::MapWidget->new;
389 410
390$TOPLEVEL->add ($mapwidget); 411$Crossfire::Client::Widget::TOPLEVEL->add ($mapwidget);
391$mapwidget->focus_in; 412$mapwidget->focus_in;
392 413
393Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; 414Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc";
394 415
395$CFG ||= { 416$CFG ||= {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines