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 |
root |
1.5 |
-flags => SDL_ANYFORMAT | SDL_HWSURFACE, |
30 |
|
|
-title => "Crossfire+ Client", |
31 |
|
|
-width => $WIDTH, |
32 |
|
|
-height => $HEIGHT, |
33 |
|
|
-opengl => 1, |
34 |
|
|
-red_size => 8, |
35 |
|
|
-green_size => 8, |
36 |
|
|
-blue_size => 8, |
37 |
root |
1.2 |
-double_buffer => 1, |
38 |
root |
1.5 |
-resizeable => 0; |
39 |
root |
1.2 |
|
40 |
|
|
glEnable GL_TEXTURE_2D; |
41 |
root |
1.9 |
glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
42 |
root |
1.2 |
glShadeModel GL_FLAT; |
43 |
|
|
glDisable GL_DEPTH_TEST; |
44 |
root |
1.9 |
glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
45 |
root |
1.8 |
glEnable GL_BLEND; |
46 |
root |
1.2 |
|
47 |
root |
1.9 |
glMatrixMode GL_PROJECTION; |
48 |
root |
1.4 |
glLoadIdentity; |
49 |
|
|
glOrtho 0, $WIDTH / 32, $HEIGHT / 32, 0, -1 , 1; |
50 |
|
|
|
51 |
root |
1.2 |
# re-bind all textures |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
sub refresh { |
55 |
root |
1.8 |
glClearColor 0, 0, 0, 0; |
56 |
root |
1.5 |
glClear GL_COLOR_BUFFER_BIT; |
57 |
root |
1.2 |
|
58 |
|
|
my $map = $conn->{map}; |
59 |
root |
1.1 |
|
60 |
root |
1.2 |
for my $x (0 .. $conn->{mapw} - 1) { |
61 |
|
|
for my $y (0 .. $conn->{maph} - 1) { |
62 |
root |
1.1 |
|
63 |
root |
1.2 |
my $cell = $map->[$x][$y] |
64 |
|
|
or next; |
65 |
|
|
|
66 |
root |
1.9 |
my $darkness = $cell->[3] * (1 / 255); |
67 |
|
|
my $darkness = 0.8 + 0.2*rand; |
68 |
|
|
glColor $darkness, $darkness, $darkness; |
69 |
root |
1.8 |
|
70 |
root |
1.2 |
for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) { |
71 |
root |
1.3 |
my $tex = $conn->{face}[$num]{texture} || 0; |
72 |
root |
1.2 |
|
73 |
|
|
glBindTexture GL_TEXTURE_2D, $tex; |
74 |
|
|
|
75 |
|
|
glBegin GL_QUADS; |
76 |
|
|
glTexCoord 0, 0; glVertex $x, $y; |
77 |
root |
1.8 |
glTexCoord 0, 1; glVertex $x, $y + 1; |
78 |
|
|
glTexCoord 1, 1; glVertex $x + 1, $y + 1; |
79 |
|
|
glTexCoord 1, 0; glVertex $x + 1, $y; |
80 |
root |
1.2 |
glEnd; |
81 |
|
|
} |
82 |
|
|
} |
83 |
|
|
} |
84 |
root |
1.1 |
|
85 |
root |
1.2 |
SDL::GLSwapBuffers; |
86 |
root |
1.1 |
} |
87 |
|
|
|
88 |
|
|
my $ev = new SDL::Event; |
89 |
|
|
my %ev_cb; |
90 |
|
|
|
91 |
|
|
sub event(&$) { |
92 |
|
|
$ev_cb{$_[0]->()} = $_[1]; |
93 |
|
|
} |
94 |
|
|
|
95 |
|
|
sub does(&) { shift } |
96 |
|
|
|
97 |
|
|
event {SDL_QUIT} does { |
98 |
|
|
exit; |
99 |
|
|
}; |
100 |
|
|
|
101 |
|
|
event {SDL_VIDEORESIZE} does { |
102 |
|
|
print "resize\n"; |
103 |
|
|
}; |
104 |
|
|
|
105 |
root |
1.4 |
event {SDL_VIDEOEXPOSE} does { |
106 |
|
|
refresh; |
107 |
|
|
}; |
108 |
|
|
|
109 |
root |
1.1 |
event {SDL_KEYDOWN} does { |
110 |
|
|
print "keypress\n"; |
111 |
|
|
}; |
112 |
|
|
|
113 |
|
|
event {SDL_KEYUP} does { |
114 |
|
|
print "keyup\n";#d# |
115 |
|
|
}; |
116 |
|
|
|
117 |
|
|
event {SDL_MOUSEMOTION} does { |
118 |
|
|
print "motion\n"; |
119 |
|
|
}; |
120 |
|
|
|
121 |
|
|
event {SDL_MOUSEBUTTONDOWN} does { |
122 |
|
|
print "button\n"; |
123 |
|
|
}; |
124 |
|
|
|
125 |
|
|
event {SDL_MOUSEBUTTONUP} does { |
126 |
|
|
print "buttup\n"; |
127 |
|
|
}; |
128 |
|
|
|
129 |
|
|
event {SDL_ACTIVEEVENT} does { |
130 |
|
|
print "active\n"; |
131 |
|
|
}; |
132 |
|
|
|
133 |
root |
1.2 |
package Crossfire::Client; |
134 |
root |
1.1 |
|
135 |
root |
1.2 |
@conn::ISA = Crossfire::Protocol::; |
136 |
root |
1.1 |
|
137 |
root |
1.2 |
sub conn::map_update { |
138 |
root |
1.1 |
my ($self, $dirty) = @_; |
139 |
|
|
|
140 |
root |
1.2 |
refresh; |
141 |
root |
1.1 |
} |
142 |
|
|
|
143 |
root |
1.2 |
sub conn::map_scroll { |
144 |
root |
1.1 |
my ($self, $dx, $dy) = @_; |
145 |
|
|
|
146 |
root |
1.2 |
refresh; |
147 |
root |
1.1 |
} |
148 |
|
|
|
149 |
root |
1.2 |
sub conn::map_clear { |
150 |
root |
1.1 |
my ($self) = @_; |
151 |
|
|
|
152 |
root |
1.2 |
refresh; |
153 |
root |
1.1 |
} |
154 |
|
|
|
155 |
root |
1.2 |
sub conn::face_update { |
156 |
root |
1.1 |
my ($self, $num, $face) = @_; |
157 |
|
|
|
158 |
|
|
warn "up face $self,$num,$face\n";#d# |
159 |
root |
1.9 |
use Gtk2; |
160 |
root |
1.1 |
|
161 |
root |
1.9 |
my $pb = new Gtk2::Gdk::PixbufLoader; |
162 |
|
|
$pb->write ($face->{image}); |
163 |
|
|
$pb->close; |
164 |
root |
1.1 |
|
165 |
root |
1.9 |
$pb = $pb->get_pixbuf; |
166 |
|
|
$pb = $pb->add_alpha (0, 0, 0, 0); |
167 |
root |
1.1 |
|
168 |
root |
1.9 |
glGetError(); |
169 |
root |
1.2 |
my ($tex) = @{glGenTextures 1}; |
170 |
root |
1.1 |
|
171 |
root |
1.2 |
$face->{texture} = $tex; |
172 |
|
|
|
173 |
|
|
glBindTexture GL_TEXTURE_2D, $tex; |
174 |
|
|
my $glerr=glGetError(); die "a: ".gluErrorString($glerr)."\n" if $glerr; |
175 |
|
|
|
176 |
root |
1.5 |
glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
177 |
elmex |
1.7 |
glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR; |
178 |
root |
1.6 |
glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
179 |
|
|
glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
180 |
root |
1.2 |
|
181 |
|
|
glTexImage2D GL_TEXTURE_2D, 0, |
182 |
root |
1.9 |
GL_RGBA8, |
183 |
|
|
$pb->get_width, $pb->get_height, |
184 |
root |
1.2 |
0, |
185 |
|
|
GL_RGBA, |
186 |
|
|
GL_UNSIGNED_BYTE, |
187 |
root |
1.9 |
$pb->get_pixels; |
188 |
root |
1.2 |
my $glerr=glGetError(); die "Problem setting up 2d Texture (dimensions not a power of 2?)):".gluErrorString($glerr)."\n" if $glerr; |
189 |
root |
1.1 |
} |
190 |
|
|
|
191 |
|
|
############################################################################# |
192 |
|
|
|
193 |
|
|
use Event; |
194 |
|
|
|
195 |
root |
1.2 |
glinit; |
196 |
|
|
|
197 |
root |
1.1 |
$conn = new conn |
198 |
|
|
host => "cf.schmorp.de", |
199 |
|
|
port => 13327; |
200 |
|
|
|
201 |
|
|
Event->timer (after => 0, interval => 1/20, hard => 1, cb => sub { |
202 |
|
|
while ($ev->poll) { |
203 |
|
|
($ev_cb{$ev->type} || sub { warn "unhandled event ", $ev->type })->(); |
204 |
|
|
} |
205 |
|
|
}); |
206 |
|
|
|
207 |
|
|
Event::loop; |
208 |
|
|
|
209 |
|
|
|