1 | #!/opt/bin/perl |
1 | #!/opt/bin/perl |
2 | |
|
|
3 | use strict; |
2 | use strict; |
4 | |
3 | |
5 | use Crossfire::Client; |
4 | use Glib; |
6 | use Crossfire::Protocol; |
5 | use Gtk2 -init; |
7 | |
|
|
8 | package Crossfire::Client; # uh, yeah |
|
|
9 | |
|
|
10 | use strict; |
|
|
11 | |
6 | |
12 | use SDL; |
7 | use SDL; |
13 | use SDL::App; |
8 | use SDL::App; |
14 | use SDL::Event; |
9 | use SDL::Event; |
15 | use SDL::Surface; |
10 | use SDL::Surface; |
16 | use SDL::OpenGL; |
11 | use SDL::OpenGL; |
17 | use SDL::OpenGL::Constants; |
12 | use SDL::OpenGL::Constants; |
18 | |
13 | |
19 | my $conn; |
14 | use Crossfire; |
20 | my $app; |
15 | use Crossfire::Client; |
|
|
16 | use Crossfire::Protocol; |
21 | |
17 | |
22 | my $WIDTH = 640; |
18 | use Client::Util; |
23 | my $HEIGHT = 480; |
19 | use Client::Widget; |
24 | |
20 | |
25 | sub glinit { |
21 | our $VERSION = '0.1'; |
26 | # nuke all gl context data |
22 | |
27 | |
23 | our $CFG; |
|
|
24 | our $CONN; |
|
|
25 | |
|
|
26 | our $SDL_TIMER; |
|
|
27 | our $SDL_APP; |
|
|
28 | our $SDL_EV = new SDL::Event; |
|
|
29 | our %SDL_CB; |
|
|
30 | |
|
|
31 | our @GL_INIT; # hooks called on every gl init |
|
|
32 | |
|
|
33 | sub init_screen { |
28 | $app = new SDL::App |
34 | $SDL_APP = new SDL::App |
|
|
35 | -flags => SDL_ANYFORMAT | SDL_HWSURFACE, |
29 | -title => "Crossfire+ Client", |
36 | -title => "Crossfire+ Client", |
30 | -width => $WIDTH, |
37 | -width => $CFG->{width}, |
31 | -height => $HEIGHT, |
38 | -height => $CFG->{height}, |
32 | -depth => 24, |
|
|
33 | -opengl => 1, |
39 | -opengl => 1, |
|
|
40 | -red_size => 8, |
|
|
41 | -green_size => 8, |
|
|
42 | -blue_size => 8, |
34 | -double_buffer => 1, |
43 | -double_buffer => 1, |
|
|
44 | -fullscreen => $CFG->{fullscreen}, |
35 | -resizeable => 0; |
45 | -resizeable => 0; |
36 | |
46 | |
37 | glEnable GL_TEXTURE_2D; |
47 | glEnable GL_TEXTURE_2D; |
|
|
48 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
38 | glShadeModel GL_FLAT; |
49 | glShadeModel GL_FLAT; |
39 | glDisable GL_DEPTH_TEST; |
50 | glDisable GL_DEPTH_TEST; |
|
|
51 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
52 | glEnable GL_BLEND; |
|
|
53 | |
40 | glMatrixMode GL_PROJECTION; |
54 | glMatrixMode GL_PROJECTION; |
|
|
55 | glLoadIdentity; |
|
|
56 | glOrtho 0, $CFG->{width} / 32, $CFG->{height} / 32, 0, -1 , 1; |
41 | |
57 | |
42 | #glViewport 0, 0, $WIDTH, $HEIGHT; |
58 | $_->() for @GL_INIT; |
43 | # re-bind all textures |
59 | } |
|
|
60 | |
|
|
61 | sub start_game { |
|
|
62 | $SDL_TIMER = add Glib::Timeout 1000/20, sub { |
|
|
63 | while ($SDL_EV->poll) { |
|
|
64 | ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->(); |
|
|
65 | } |
|
|
66 | |
|
|
67 | 1 |
|
|
68 | }; |
|
|
69 | |
|
|
70 | init_screen; |
|
|
71 | |
|
|
72 | $CONN = new conn |
|
|
73 | host => $CFG->{host}, |
|
|
74 | port => $CFG->{port}; |
|
|
75 | } |
|
|
76 | |
|
|
77 | sub stop_game { |
|
|
78 | remove Glib::Source $SDL_TIMER; |
|
|
79 | |
|
|
80 | undef $SDL_APP; |
|
|
81 | SDL::Quit; |
44 | } |
82 | } |
45 | |
83 | |
46 | sub refresh { |
84 | sub refresh { |
47 | glClearColor 0.5, 0.5, 0.7, 0; |
85 | glClearColor 0, 0, 0, 0; |
48 | glClear GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT; |
86 | glClear GL_COLOR_BUFFER_BIT; |
49 | |
87 | |
50 | glLoadIdentity; |
88 | for (values %Client::Widget::ACTIVE_WIDGETS) { |
51 | glOrtho 0, $WIDTH / 32, $HEIGHT / 32, 0, -1 , 1; |
89 | $_->draw |
52 | |
|
|
53 | my $map = $conn->{map}; |
|
|
54 | |
|
|
55 | for my $x (0 .. $conn->{mapw} - 1) { |
|
|
56 | for my $y (0 .. $conn->{maph} - 1) { |
|
|
57 | |
|
|
58 | my $cell = $map->[$x][$y] |
|
|
59 | or next; |
|
|
60 | |
|
|
61 | for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) { |
|
|
62 | my $tex = $conn->{face}[$num]{texture} ||= do { |
|
|
63 | $conn->send ("askface $num") unless $conn->{face}[$num]{askface}++; |
|
|
64 | |
|
|
65 | 0 |
|
|
66 | |
|
|
67 | }; |
|
|
68 | |
|
|
69 | glBindTexture GL_TEXTURE_2D, $tex; |
|
|
70 | |
|
|
71 | glBegin GL_QUADS; |
|
|
72 | glTexCoord 0, 0; glVertex $x, $y; |
|
|
73 | glTexCoord 1, 0; glVertex $x + 1, $y; |
|
|
74 | glTexCoord 1, 1; glVertex $x + 1, $y + 1; |
|
|
75 | glTexCoord 0, 1; glVertex $x, $y + 1; |
|
|
76 | glEnd; |
|
|
77 | } |
|
|
78 | } |
|
|
79 | } |
90 | } |
80 | |
91 | |
81 | SDL::GLSwapBuffers; |
92 | SDL::GLSwapBuffers; |
82 | } |
93 | } |
83 | |
94 | |
84 | my $ev = new SDL::Event; |
95 | %SDL_CB = ( |
85 | my %ev_cb; |
96 | SDL_QUIT() => sub { |
86 | |
97 | warn "sdl quit\n";#d# |
87 | sub event(&$) { |
|
|
88 | $ev_cb{$_[0]->()} = $_[1]; |
|
|
89 | } |
|
|
90 | |
|
|
91 | sub does(&) { shift } |
|
|
92 | |
|
|
93 | event {SDL_QUIT} does { |
|
|
94 | exit; |
98 | exit; |
95 | }; |
99 | }, |
96 | |
100 | SDL_VIDEORESIZE() => sub { |
97 | event {SDL_VIDEORESIZE} does { |
101 | }, |
98 | print "resize\n"; |
102 | SDL_VIDEOEXPOSE() => sub { |
99 | }; |
103 | refresh; |
100 | |
104 | }, |
101 | event {SDL_KEYDOWN} does { |
105 | SDL_KEYDOWN() => sub { |
102 | print "keypress\n"; |
106 | if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) { |
103 | }; |
107 | # alt-enter |
104 | |
108 | $CFG->{fullscreen} = !$CFG->{fullscreen}; |
105 | event {SDL_KEYUP} does { |
109 | init_screen; |
106 | print "keyup\n";#d# |
110 | } else { |
107 | }; |
111 | Client::Widget::feed_sdl_key_down_event ($SDL_EV); |
108 | |
112 | } |
109 | event {SDL_MOUSEMOTION} does { |
113 | }, |
110 | print "motion\n"; |
114 | SDL_KEYUP() => sub { |
111 | }; |
115 | Client::Widget::feed_sdl_key_up_event ($SDL_EV); |
112 | |
116 | }, |
113 | event {SDL_MOUSEBUTTONDOWN} does { |
117 | SDL_MOUSEMOTION() => sub { |
114 | print "button\n"; |
118 | warn "sdl motion\n";#d# |
115 | }; |
119 | }, |
116 | |
120 | SDL_MOUSEBUTTONDOWN() => sub { |
117 | event {SDL_MOUSEBUTTONUP} does { |
121 | Client::Widget::feed_sdl_button_down_event ($SDL_EV); |
118 | print "buttup\n"; |
122 | }, |
119 | }; |
123 | SDL_MOUSEBUTTONUP() => sub { |
120 | |
124 | Client::Widget::feed_sdl_button_up_event ($SDL_EV); |
121 | event {SDL_ACTIVEEVENT} does { |
125 | }, |
|
|
126 | SDL_ACTIVEEVENT() => sub { |
122 | print "active\n"; |
127 | warn "active\n";#d# |
123 | }; |
128 | }, |
124 | |
129 | ); |
125 | package Crossfire::Client; |
|
|
126 | |
130 | |
127 | @conn::ISA = Crossfire::Protocol::; |
131 | @conn::ISA = Crossfire::Protocol::; |
128 | |
132 | |
129 | sub conn::map_update { |
133 | sub conn::map_update { |
130 | my ($self, $dirty) = @_; |
134 | my ($self, $dirty) = @_; |
… | |
… | |
145 | } |
149 | } |
146 | |
150 | |
147 | sub conn::face_update { |
151 | sub conn::face_update { |
148 | my ($self, $num, $face) = @_; |
152 | my ($self, $num, $face) = @_; |
149 | |
153 | |
150 | warn "up face $self,$num,$face\n";#d# |
154 | $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image}; |
151 | #TODO |
|
|
152 | open my $fh, ">:raw", "/tmp/x~"; |
|
|
153 | syswrite $fh, $face->{image}; |
|
|
154 | close $fh; |
|
|
155 | |
|
|
156 | my $surface = new SDL::Surface -name => "/tmp/x~"; |
|
|
157 | |
|
|
158 | unlink "/tmp/x~"; |
|
|
159 | |
|
|
160 | my ($tex) = @{glGenTextures 1}; |
|
|
161 | glGetError(); |
|
|
162 | |
|
|
163 | $face->{texture} = $tex; |
|
|
164 | |
|
|
165 | glBindTexture GL_TEXTURE_2D, $tex; |
|
|
166 | my $glerr=glGetError(); die "a: ".gluErrorString($glerr)."\n" if $glerr; |
|
|
167 | |
|
|
168 | # glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
|
|
169 | # glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR; |
|
|
170 | |
|
|
171 | $surface->rgba; |
|
|
172 | |
|
|
173 | glTexImage2D GL_TEXTURE_2D, 0, |
|
|
174 | 4, # components |
|
|
175 | $surface->width, $surface->height, |
|
|
176 | 0, |
|
|
177 | GL_RGBA, |
|
|
178 | GL_UNSIGNED_BYTE, |
|
|
179 | $surface->pixels; |
|
|
180 | my $glerr=glGetError(); die "Problem setting up 2d Texture (dimensions not a power of 2?)):".gluErrorString($glerr)."\n" if $glerr; |
|
|
181 | } |
155 | } |
182 | |
156 | |
183 | ############################################################################# |
157 | ############################################################################# |
184 | |
158 | |
185 | use Event; |
159 | my $mapwidget = Client::MapWidget->new; |
186 | |
160 | |
187 | glinit; |
161 | $mapwidget->activate; |
|
|
162 | $mapwidget->focus_in; |
188 | |
163 | |
189 | $conn = new conn |
164 | Client::Util::read_cfg "$Crossfire::VARDIR/pclientrc"; |
190 | host => "cf.schmorp.de", |
|
|
191 | port => 13327; |
|
|
192 | |
165 | |
193 | Event->timer (after => 0, interval => 1/20, hard => 1, cb => sub { |
166 | $CFG ||= { |
194 | while ($ev->poll) { |
167 | width => 640, |
195 | ($ev_cb{$ev->type} || sub { warn "unhandled event ", $ev->type })->(); |
168 | height => 480, |
196 | } |
169 | fullscreen => 0, |
|
|
170 | host => "crossfire.schmorp.de", |
|
|
171 | port => 13327, |
197 | }); |
172 | }; |
198 | |
173 | |
199 | Event::loop; |
174 | Client::Util::run_config_dialog |
|
|
175 | login => sub { start_game }, |
|
|
176 | logout => sub { stop_game }; |
200 | |
177 | |
201 | |
178 | main Gtk2; |