ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
Revision: 1.8
Committed: Fri Apr 7 08:42:56 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.7: +10 -6 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
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.8 # glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
42 root 1.2 glShadeModel GL_FLAT;
43     glDisable GL_DEPTH_TEST;
44     glMatrixMode GL_PROJECTION;
45 root 1.8 glBlendFunc GL_SRC_ALPHA, GL_ZERO;
46     glEnable GL_BLEND;
47 root 1.2
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.8 # my $darkness = $cell->[3] * (1 / 255);
67     # glColor $darkness, $darkness, $darkness;
68    
69 root 1.2 for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) {
70 root 1.3 my $tex = $conn->{face}[$num]{texture} || 0;
71 root 1.2
72     glBindTexture GL_TEXTURE_2D, $tex;
73    
74     glBegin GL_QUADS;
75     glTexCoord 0, 0; glVertex $x, $y;
76 root 1.8 glTexCoord 0, 1; glVertex $x, $y + 1;
77     glTexCoord 1, 1; glVertex $x + 1, $y + 1;
78     glTexCoord 1, 0; glVertex $x + 1, $y;
79 root 1.2 glEnd;
80     }
81     }
82     }
83 root 1.1
84 root 1.2 SDL::GLSwapBuffers;
85 root 1.1 }
86    
87     my $ev = new SDL::Event;
88     my %ev_cb;
89    
90     sub event(&$) {
91     $ev_cb{$_[0]->()} = $_[1];
92     }
93    
94     sub does(&) { shift }
95    
96     event {SDL_QUIT} does {
97     exit;
98     };
99    
100     event {SDL_VIDEORESIZE} does {
101     print "resize\n";
102     };
103    
104 root 1.4 event {SDL_VIDEOEXPOSE} does {
105     refresh;
106     };
107    
108 root 1.1 event {SDL_KEYDOWN} does {
109     print "keypress\n";
110     };
111    
112     event {SDL_KEYUP} does {
113     print "keyup\n";#d#
114     };
115    
116     event {SDL_MOUSEMOTION} does {
117     print "motion\n";
118     };
119    
120     event {SDL_MOUSEBUTTONDOWN} does {
121     print "button\n";
122     };
123    
124     event {SDL_MOUSEBUTTONUP} does {
125     print "buttup\n";
126     };
127    
128     event {SDL_ACTIVEEVENT} does {
129     print "active\n";
130     };
131    
132 root 1.2 package Crossfire::Client;
133 root 1.1
134 root 1.2 @conn::ISA = Crossfire::Protocol::;
135 root 1.1
136 root 1.2 sub conn::map_update {
137 root 1.1 my ($self, $dirty) = @_;
138    
139 root 1.2 refresh;
140 root 1.1 }
141    
142 root 1.2 sub conn::map_scroll {
143 root 1.1 my ($self, $dx, $dy) = @_;
144    
145 root 1.2 refresh;
146 root 1.1 }
147    
148 root 1.2 sub conn::map_clear {
149 root 1.1 my ($self) = @_;
150    
151 root 1.2 refresh;
152 root 1.1 }
153    
154 root 1.2 sub conn::face_update {
155 root 1.1 my ($self, $num, $face) = @_;
156    
157     warn "up face $self,$num,$face\n";#d#
158     #TODO
159     open my $fh, ">:raw", "/tmp/x~";
160     syswrite $fh, $face->{image};
161     close $fh;
162    
163 root 1.2 my $surface = new SDL::Surface -name => "/tmp/x~";
164 root 1.1
165     unlink "/tmp/x~";
166    
167 root 1.2 my ($tex) = @{glGenTextures 1};
168     glGetError();
169 root 1.1
170 root 1.2 $face->{texture} = $tex;
171    
172     glBindTexture GL_TEXTURE_2D, $tex;
173     my $glerr=glGetError(); die "a: ".gluErrorString($glerr)."\n" if $glerr;
174    
175 root 1.5 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
176 elmex 1.7 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR;
177 root 1.6 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
178     glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP;
179 root 1.2
180     $surface->rgba;
181    
182     glTexImage2D GL_TEXTURE_2D, 0,
183     4, # components
184     $surface->width, $surface->height,
185     0,
186     GL_RGBA,
187     GL_UNSIGNED_BYTE,
188     $surface->pixels;
189     my $glerr=glGetError(); die "Problem setting up 2d Texture (dimensions not a power of 2?)):".gluErrorString($glerr)."\n" if $glerr;
190 root 1.1 }
191    
192     #############################################################################
193    
194     use Event;
195    
196 root 1.2 glinit;
197    
198 root 1.1 $conn = new conn
199     host => "cf.schmorp.de",
200     port => 13327;
201    
202     Event->timer (after => 0, interval => 1/20, hard => 1, cb => sub {
203     while ($ev->poll) {
204     ($ev_cb{$ev->type} || sub { warn "unhandled event ", $ev->type })->();
205     }
206     });
207    
208     Event::loop;
209    
210