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.10 by elmex, Fri Apr 7 16:04:03 2006 UTC vs.
Revision 1.20 by elmex, Fri Apr 7 19:58:55 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines