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.6 by root, Thu Apr 6 21:15:11 2006 UTC vs.
Revision 1.31 by elmex, Sat Apr 8 16:34:47 2006 UTC

1#!/opt/bin/perl 1#!/opt/bin/perl
2 2
3use strict; 3use strict;
4use utf8;
4 5
5use Crossfire::Client; 6use Glib;
6use Crossfire::Protocol; 7use Gtk2 -init;
7
8package Crossfire::Client; # uh, yeah
9
10use strict;
11 8
12use SDL; 9use SDL;
13use SDL::App; 10use SDL::App;
14use SDL::Event; 11use SDL::Event;
15use SDL::Surface; 12use SDL::Surface;
16use SDL::OpenGL; 13use SDL::OpenGL;
17use SDL::OpenGL::Constants; 14use SDL::OpenGL::Constants;
18 15
19my $conn; 16use Crossfire;
20my $app; 17use Crossfire::Client;
18use Crossfire::Protocol;
21 19
22my $WIDTH = 640; 20use Crossfire::Client::Widget;
23my $HEIGHT = 480;
24 21
25sub glinit { 22our $FACECACHE;
26 # nuke all gl context data 23
27 24our $VERSION = '0.1';
25
26our %GL_EXT;
27
28our $CFG;
29our $CONN;
30
31our $WIDTH;
32our $HEIGHT;
33our $FULLSCREEN;
34
35our $UIFONT;
36
37our $SDL_TIMER;
38our $SDL_APP;
39our $SDL_EV = new SDL::Event;
40our %SDL_CB;
41
42our @GL_INIT; # hooks called on every gl init
43
44our $ALT_ENTER_MESSAGE;
45
46sub init_screen {
28 $app = new SDL::App 47 $SDL_APP = new SDL::App
29 -flags => SDL_ANYFORMAT | SDL_HWSURFACE, 48 -flags => SDL_ANYFORMAT | SDL_HWSURFACE,
30 -title => "Crossfire+ Client", 49 -title => "Crossfire+ Client",
31 -width => $WIDTH, 50 -width => $WIDTH,
32 -height => $HEIGHT, 51 -height => $HEIGHT,
33 -opengl => 1, 52 -opengl => 1,
34 -red_size => 8, 53 -red_size => 8,
35 -green_size => 8, 54 -green_size => 8,
36 -blue_size => 8, 55 -blue_size => 8,
37 -double_buffer => 1, 56 -double_buffer => 1,
57 -fullscreen => $FULLSCREEN,
38 -resizeable => 0; 58 -resizeable => 0;
39 59
60 %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions;
61
62 $GL_EXT{GL_ARB_texture_non_power_of_two}
63 or warn "WARNING: non-power-of-two opengl extension required";
64
65 $UIFONT = SDL::TTFOpenFont Crossfire::Client::find_rcfile "uifont.ttf", $HEIGHT / 40
66 or die "TTFOpenFont: $!";
67
68 $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label 0, $HEIGHT - $HEIGHT / 40, 10, $UIFONT, "Alt-Enter toggles fullscreen mode";
69 $ALT_ENTER_MESSAGE->move (0, $HEIGHT - ($ALT_ENTER_MESSAGE->size_request)[1]);
70 $ALT_ENTER_MESSAGE->activate;
71
72 glClearColor 0, 0, 0, 0;
73
40 glEnable GL_TEXTURE_2D; 74 glEnable GL_TEXTURE_2D;
41 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL;
42 glShadeModel GL_FLAT; 75 glShadeModel GL_FLAT;
43 glDisable GL_DEPTH_TEST; 76 glDisable GL_DEPTH_TEST;
77 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
78
44 glMatrixMode GL_PROJECTION; 79 glMatrixMode GL_PROJECTION;
45
46 glLoadIdentity; 80 glLoadIdentity;
47 glOrtho 0, $WIDTH / 32, $HEIGHT / 32, 0, -1 , 1; 81 glOrtho 0, $WIDTH, $HEIGHT, 0, -100 , 100;
48 82
49 # re-bind all textures 83 glMatrixMode GL_MODELVIEW;
84
85 $_->() for @GL_INIT;
50} 86}
87
88sub start_game {
89 $SDL_TIMER = add Glib::Timeout 1000/100, sub {
90 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
91 while $SDL_EV->poll;
92
93 1
94 };
95
96 $WIDTH = $CFG->{width};
97 $HEIGHT = $CFG->{height};
98 $FULLSCREEN = 0;
99
100 init_screen;
101
102 $CONN = new conn
103 host => $CFG->{host},
104 port => $CFG->{port},
105 user => $CFG->{user},
106 pass => $CFG->{password};
107}
108
109sub stop_game {
110 remove Glib::Source $SDL_TIMER;
111
112 undef $SDL_APP;
113 SDL::Quit;
114}
115
116
117sub force_refresh {
118 glViewport 0, 0, $WIDTH, $HEIGHT;
119 glClear GL_COLOR_BUFFER_BIT;
120
121 $_->draw for @Crossfire::Client::Widget::ACTIVE_WIDGETS;
122
123 SDL::GLSwapBuffers;
124}
125
126my $refresh_handler;
51 127
52sub refresh { 128sub refresh {
53 glClearColor 0.5, 0.5, 0.7, 0; 129 $refresh_handler ||= add Glib::Idle sub {
54 glClear GL_COLOR_BUFFER_BIT; 130 force_refresh;
131 undef $refresh_handler;
132 0
133 };
134}
55 135
56 my $map = $conn->{map}; 136%SDL_CB = (
57 137 SDL_QUIT() => sub {
58 for my $x (0 .. $conn->{mapw} - 1) { 138 main_quit Gtk2;
59 for my $y (0 .. $conn->{maph} - 1) { 139 },
60 140 SDL_VIDEORESIZE() => sub {
61 my $cell = $map->[$x][$y] 141 },
62 or next; 142 SDL_VIDEOEXPOSE() => sub {
63 143 refresh;
64 for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) { 144 },
65 my $tex = $conn->{face}[$num]{texture} || 0; 145 SDL_KEYDOWN() => sub {
66 146 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) {
67 glBindTexture GL_TEXTURE_2D, $tex; 147 # alt-enter
68 148 $FULLSCREEN = !$FULLSCREEN;
69 glColor 1,0.2,0.7; 149 init_screen;
70 glBegin GL_QUADS; 150 } else {
71 glTexCoord 0, 0; glVertex $x, $y; 151 Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV);
72 glTexCoord 0, 1; glVertex $x, $y + 0.9;
73 glTexCoord 1, 1; glVertex $x + 0.9, $y + 0.9;
74 glTexCoord 1, 0; glVertex $x + 0.9, $y;
75 glEnd;
76 }
77 } 152 }
78 } 153 },
79 154 SDL_KEYUP() => sub {
80 SDL::GLSwapBuffers; 155 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV);
81} 156 },
82 157 SDL_MOUSEMOTION() => sub {
83my $ev = new SDL::Event; 158 warn "sdl motion\n";#d#
84my %ev_cb; 159 },
85 160 SDL_MOUSEBUTTONDOWN() => sub {
86sub event(&$) { 161 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV);
87 $ev_cb{$_[0]->()} = $_[1]; 162 },
88} 163 SDL_MOUSEBUTTONUP() => sub {
89 164 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV);
90sub does(&) { shift } 165 },
91 166 SDL_ACTIVEEVENT() => sub {
92event {SDL_QUIT} does {
93 exit;
94};
95
96event {SDL_VIDEORESIZE} does {
97 print "resize\n";
98};
99
100event {SDL_VIDEOEXPOSE} does {
101 refresh;
102};
103
104event {SDL_KEYDOWN} does {
105 print "keypress\n";
106};
107
108event {SDL_KEYUP} does {
109 print "keyup\n";#d#
110};
111
112event {SDL_MOUSEMOTION} does {
113 print "motion\n";
114};
115
116event {SDL_MOUSEBUTTONDOWN} does {
117 print "button\n";
118};
119
120event {SDL_MOUSEBUTTONUP} does {
121 print "buttup\n";
122};
123
124event {SDL_ACTIVEEVENT} does {
125 print "active\n"; 167 warn "active\n";#d#
126}; 168 },
127 169);
128package Crossfire::Client;
129 170
130@conn::ISA = Crossfire::Protocol::; 171@conn::ISA = Crossfire::Protocol::;
131 172
132sub conn::map_update { 173sub conn::map_update {
133 my ($self, $dirty) = @_; 174 my ($self, $dirty) = @_;
145 my ($self) = @_; 186 my ($self) = @_;
146 187
147 refresh; 188 refresh;
148} 189}
149 190
191sub conn::face_find {
192 my ($self, $face) = @_;
193
194 $FACECACHE->{"$face->{chksum},$face->{name}"}
195}
196
150sub conn::face_update { 197sub conn::face_update {
151 my ($self, $num, $face) = @_; 198 my ($self, $face) = @_;
152 199
153 warn "up face $self,$num,$face\n";#d# 200 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
154 #TODO
155 open my $fh, ">:raw", "/tmp/x~";
156 syswrite $fh, $face->{image};
157 close $fh;
158 201
159 my $surface = new SDL::Surface -name => "/tmp/x~"; 202 $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image};
203}
160 204
161 unlink "/tmp/x~"; 205sub gtk_add_cfg_field {
206 my ($tbl, $cfg, $klbl, $key, $value) = @_;
207 my $i = $cfg->{_i}++;
208 $tbl->attach_defaults (my $lbl = Gtk2::Label->new ($klbl), 0, 1, $i, $i + 1);
209 $tbl->attach_defaults (my $ent = Gtk2::Entry->new, 1, 2, $i, $i + 1);
210 if ($key eq 'password') {
211 $ent->set_invisible_char ("*");
212 $ent->set (visibility => 0)
213 }
214 $ent->set_text ($value);
215 $ent->signal_connect (changed => sub {
216 my ($ent) = @_;
217 $cfg->{$key} = $ent->get_text;
218 });
219}
162 220
163 my ($tex) = @{glGenTextures 1}; 221sub run_config_dialog {
164 glGetError(); 222 my (%events) = @_;
165 223
166 $face->{texture} = $tex; 224 my $w = Gtk2::Window->new;
225
226 my @cfg = (
227 [qw/Host host/],
228 [qw/Port port/],
229 [qw/Username user/],
230 [qw/Password password/],
167 231 );
168 glBindTexture GL_TEXTURE_2D, $tex; 232
169 my $glerr=glGetError(); die "a: ".gluErrorString($glerr)."\n" if $glerr; 233 my $cfg = {};
234
235 my $a = SDL::ListModes (0, SDL_FULLSCREEN|SDL_HWSURFACE);
236 my @modes = map { [SDL::RectW ($_), SDL::RectH ($_)] } @$a;
237
238 $w->add (my $vb = Gtk2::VBox->new);
239 $vb->pack_start (my $t = Gtk2::Table->new (2, scalar @cfg), 0, 0, 0);
240 my $selmode = $::CFG->{width} . 'x' . $::CFG->{height};
241 $t->attach_defaults (Gtk2::Label->new ("Modes"), 0, 1, 0, 1);
242 $t->attach_defaults (my $cb = Gtk2::ComboBox->new_text, 1, 2, 0, 1);
243 my $i = 0;
244 my $act = 0;
245 for (map { "$_->[0]x$_->[1]" } reverse @modes) {
246 if ($_ eq $selmode) { $act = $i }
247 $cb->append_text ($_);
248 $i++;
249 }
250 $cb->set_active ($act);
251 $cb->signal_connect (changed => sub {
252 my ($cb) = @_;
253 my $txt = $cb->get_active_text;
254 if ($txt =~ m/(\d+)x(\d+)/) {
255 $::CFG->{width} = $1;
256 $::CFG->{height} = $2;
170 257 }
171 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; 258 });
172 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR;
173 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
174 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP;
175
176 $surface->rgba;
177 259
178 glTexImage2D GL_TEXTURE_2D, 0, 260 $cfg->{_i} = 1;
179 4, # components 261 for (@cfg) {
180 $surface->width, $surface->height, 262 gtk_add_cfg_field ($t, $cfg, $_->[0], $_->[1], $::CFG->{$_->[1]});
263 }
264
265 $vb->pack_start (my $hb = Gtk2::HBox->new, 0, 0, 0);
266 $hb->pack_start (my $cb = Gtk2::Button->new ("save"), 1, 1, 5);
267 $cb->signal_connect (clicked => sub {
268 for (keys %$cfg) {
269 $::CFG->{$_} = $cfg->{$_}
270 if $_ ne '_i';
181 0, 271 }
272 Crossfire::Client::write_cfg "$Crossfire::VARDIR/pclientrc";
273 });
274 $hb->pack_start (my $cb = Gtk2::Button->new ("login"), 1, 1, 5);
275 $cb->signal_connect (clicked => sub {
276 for (keys %$cfg) {
277 $::CFG->{$_} = $cfg->{$_}
278 if $_ ne '_i';
182 GL_RGBA, 279 }
183 GL_UNSIGNED_BYTE, 280 my $cb = $events{login} || sub {};
184 $surface->pixels; 281 $cb->($::CFG->{user}, $::CFG->{password});
185 my $glerr=glGetError(); die "Problem setting up 2d Texture (dimensions not a power of 2?)):".gluErrorString($glerr)."\n" if $glerr; 282 });
283 $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5);
284 $cb->signal_connect (clicked => sub {
285 my $cb = $events{login} || sub {};
286 $cb->();
287 });
288 $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5);
289 $cb->signal_connect (clicked => sub { $w->destroy });
290
291 $w->show_all;
292
293 $w->signal_connect (destroy => sub { Gtk2->main_quit });
186} 294}
295
187 296
188############################################################################# 297#############################################################################
189 298
190use Event; 299SDL::Init SDL_INIT_EVERYTHING;
300SDL::TTFInit;
191 301
192glinit; 302my $mapwidget = Crossfire::Client::Widget::MapWidget->new;
193 303
194$conn = new conn 304$mapwidget->activate;
305$mapwidget->focus_in;
306
307Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc";
308
309$CFG ||= {
310 width => 640,
311 height => 480,
312 fullscreen => 0,
195 host => "cf.schmorp.de", 313 host => "crossfire.schmorp.de",
196 port => 13327; 314 port => 13327,
197
198Event->timer (after => 0, interval => 1/20, hard => 1, cb => sub {
199 while ($ev->poll) {
200 ($ev_cb{$ev->type} || sub { warn "unhandled event ", $ev->type })->();
201 }
202}); 315};
203 316
204Event::loop; 317$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
205 318
319run_config_dialog
320 login => sub { start_game },
321 logout => sub { stop_game };
206 322
323main Gtk2;
324
325Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces";

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines