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.12 by elmex, Fri Apr 7 16:29:19 2006 UTC vs.
Revision 1.24 by root, Fri Apr 7 20:34:44 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines