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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines