1 |
root |
1.1 |
#!/opt/bin/perl |
2 |
|
|
|
3 |
root |
1.2 |
use strict; |
4 |
|
|
|
5 |
|
|
use Crossfire::Client; |
6 |
|
|
use Crossfire::Protocol; |
7 |
|
|
|
8 |
|
|
package Crossfire::Client; # uh, yeah |
9 |
|
|
|
10 |
|
|
use strict; |
11 |
|
|
|
12 |
root |
1.1 |
use SDL; |
13 |
|
|
use SDL::App; |
14 |
|
|
use SDL::Event; |
15 |
|
|
use SDL::Surface; |
16 |
root |
1.2 |
use SDL::OpenGL; |
17 |
|
|
use SDL::OpenGL::Constants; |
18 |
root |
1.1 |
|
19 |
|
|
my $conn; |
20 |
root |
1.2 |
my $app; |
21 |
|
|
|
22 |
|
|
my $WIDTH = 640; |
23 |
|
|
my $HEIGHT = 480; |
24 |
|
|
|
25 |
|
|
sub glinit { |
26 |
|
|
# nuke all gl context data |
27 |
|
|
|
28 |
|
|
$app = new SDL::App |
29 |
|
|
-title => "Crossfire+ Client", |
30 |
|
|
-width => $WIDTH, |
31 |
|
|
-height => $HEIGHT, |
32 |
|
|
-depth => 24, |
33 |
|
|
-opengl => 1, |
34 |
|
|
-double_buffer => 1, |
35 |
|
|
-resizeable => 0; |
36 |
|
|
|
37 |
|
|
glEnable GL_TEXTURE_2D; |
38 |
|
|
glShadeModel GL_FLAT; |
39 |
|
|
glDisable GL_DEPTH_TEST; |
40 |
|
|
glMatrixMode GL_PROJECTION; |
41 |
|
|
|
42 |
|
|
#glViewport 0, 0, $WIDTH, $HEIGHT; |
43 |
|
|
# re-bind all textures |
44 |
|
|
} |
45 |
|
|
|
46 |
|
|
sub refresh { |
47 |
|
|
glClearColor 0.5, 0.5, 0.7, 0; |
48 |
|
|
glClear GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT; |
49 |
|
|
|
50 |
|
|
glLoadIdentity; |
51 |
|
|
glOrtho 0, $WIDTH / 32, $HEIGHT / 32, 0, -1 , 1; |
52 |
|
|
|
53 |
|
|
my $map = $conn->{map}; |
54 |
root |
1.1 |
|
55 |
root |
1.2 |
for my $x (0 .. $conn->{mapw} - 1) { |
56 |
|
|
for my $y (0 .. $conn->{maph} - 1) { |
57 |
root |
1.1 |
|
58 |
root |
1.2 |
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 |
|
|
} |
80 |
root |
1.1 |
|
81 |
root |
1.2 |
SDL::GLSwapBuffers; |
82 |
root |
1.1 |
} |
83 |
|
|
|
84 |
|
|
my $ev = new SDL::Event; |
85 |
|
|
my %ev_cb; |
86 |
|
|
|
87 |
|
|
sub event(&$) { |
88 |
|
|
$ev_cb{$_[0]->()} = $_[1]; |
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
sub does(&) { shift } |
92 |
|
|
|
93 |
|
|
event {SDL_QUIT} does { |
94 |
|
|
exit; |
95 |
|
|
}; |
96 |
|
|
|
97 |
|
|
event {SDL_VIDEORESIZE} does { |
98 |
|
|
print "resize\n"; |
99 |
|
|
}; |
100 |
|
|
|
101 |
|
|
event {SDL_KEYDOWN} does { |
102 |
|
|
print "keypress\n"; |
103 |
|
|
}; |
104 |
|
|
|
105 |
|
|
event {SDL_KEYUP} does { |
106 |
|
|
print "keyup\n";#d# |
107 |
|
|
}; |
108 |
|
|
|
109 |
|
|
event {SDL_MOUSEMOTION} does { |
110 |
|
|
print "motion\n"; |
111 |
|
|
}; |
112 |
|
|
|
113 |
|
|
event {SDL_MOUSEBUTTONDOWN} does { |
114 |
|
|
print "button\n"; |
115 |
|
|
}; |
116 |
|
|
|
117 |
|
|
event {SDL_MOUSEBUTTONUP} does { |
118 |
|
|
print "buttup\n"; |
119 |
|
|
}; |
120 |
|
|
|
121 |
|
|
event {SDL_ACTIVEEVENT} does { |
122 |
|
|
print "active\n"; |
123 |
|
|
}; |
124 |
|
|
|
125 |
root |
1.2 |
package Crossfire::Client; |
126 |
root |
1.1 |
|
127 |
root |
1.2 |
@conn::ISA = Crossfire::Protocol::; |
128 |
root |
1.1 |
|
129 |
root |
1.2 |
sub conn::map_update { |
130 |
root |
1.1 |
my ($self, $dirty) = @_; |
131 |
|
|
|
132 |
root |
1.2 |
refresh; |
133 |
root |
1.1 |
} |
134 |
|
|
|
135 |
root |
1.2 |
sub conn::map_scroll { |
136 |
root |
1.1 |
my ($self, $dx, $dy) = @_; |
137 |
|
|
|
138 |
root |
1.2 |
refresh; |
139 |
root |
1.1 |
} |
140 |
|
|
|
141 |
root |
1.2 |
sub conn::map_clear { |
142 |
root |
1.1 |
my ($self) = @_; |
143 |
|
|
|
144 |
root |
1.2 |
refresh; |
145 |
root |
1.1 |
} |
146 |
|
|
|
147 |
root |
1.2 |
sub conn::face_update { |
148 |
root |
1.1 |
my ($self, $num, $face) = @_; |
149 |
|
|
|
150 |
|
|
warn "up face $self,$num,$face\n";#d# |
151 |
|
|
#TODO |
152 |
|
|
open my $fh, ">:raw", "/tmp/x~"; |
153 |
|
|
syswrite $fh, $face->{image}; |
154 |
|
|
close $fh; |
155 |
|
|
|
156 |
root |
1.2 |
my $surface = new SDL::Surface -name => "/tmp/x~"; |
157 |
root |
1.1 |
|
158 |
|
|
unlink "/tmp/x~"; |
159 |
|
|
|
160 |
root |
1.2 |
my ($tex) = @{glGenTextures 1}; |
161 |
|
|
glGetError(); |
162 |
root |
1.1 |
|
163 |
root |
1.2 |
$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 |
root |
1.1 |
} |
182 |
|
|
|
183 |
|
|
############################################################################# |
184 |
|
|
|
185 |
|
|
use Event; |
186 |
|
|
|
187 |
root |
1.2 |
glinit; |
188 |
|
|
|
189 |
root |
1.1 |
$conn = new conn |
190 |
|
|
host => "cf.schmorp.de", |
191 |
|
|
port => 13327; |
192 |
|
|
|
193 |
|
|
Event->timer (after => 0, interval => 1/20, hard => 1, cb => sub { |
194 |
|
|
while ($ev->poll) { |
195 |
|
|
($ev_cb{$ev->type} || sub { warn "unhandled event ", $ev->type })->(); |
196 |
|
|
} |
197 |
|
|
}); |
198 |
|
|
|
199 |
|
|
Event::loop; |
200 |
|
|
|
201 |
|
|
|