ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
(Generate patch)

Comparing deliantra/Deliantra-Client/bin/pclient (file contents):
Revision 1.7 by elmex, Thu Apr 6 21:43:17 2006 UTC vs.
Revision 1.13 by root, Fri Apr 7 16:30:23 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines