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