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

Comparing deliantra/Deliantra-Client/Client.xs (file contents):
Revision 1.327 by root, Sun Nov 18 12:01:50 2018 UTC vs.
Revision 1.330 by root, Sun Nov 18 15:24:24 2018 UTC

287}; 287};
288 288
289struct mapgrid { 289struct mapgrid {
290 int x, y, w, h; 290 int x, y, w, h;
291 int ox, oy; /* offset to virtual global coordinate system */ 291 int ox, oy; /* offset to virtual global coordinate system */
292 std::vector<tileid> tile; 292 int faces; tileid *face2tile; // [faceid]
293 std::vector<maptex> tex; 293 int texs; maptex *tex; // [tileid]
294 294
295 int32_t rows; 295 int32_t rows;
296 maprow *row; 296 maprow *row;
297
298 ~mapgrid ()
299 {
300 clear_cells ();
301 }
302
303 void clear_cells ();
304}; 297};
305 298
306typedef mapgrid *DC__Map; 299typedef mapgrid *DC__Map;
307 300
308static char * 301template<typename T>
302static void
309prepend (char *ptr, int sze, int inc) 303prepend (T *&ptr, int sze, int inc)
310{ 304{
311 char *p; 305 T *p;
312 306
313 New (0, p, sze + inc, char); 307 Newxz (p, sze + inc, T);
314 Zero (p, inc, char);
315 Move (ptr, p + inc, sze, char); 308 Move (ptr, p + inc, sze, T);
316 Safefree (ptr); 309 Safefree (ptr);
317 310
318 return p; 311 ptr = p;
319} 312}
320 313
321static char * 314template<typename T>
315static void
322append (char *ptr, int sze, int inc) 316append (T *&ptr, int sze, int inc)
323{ 317{
324 Renew (ptr, sze + inc, char); 318 Renew (ptr, sze + inc, T);
325 Zero (ptr + sze, inc, char); 319 Zero (ptr + sze, inc, T);
326
327 return ptr;
328} 320}
329 321
330#define Append(type,ptr,sze,inc) (ptr) = (type *)append ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type)) 322static void
331#define Prepend(type,ptr,sze,inc) (ptr) = (type *)prepend ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type)) 323need_facenum (struct mapgrid *self, faceid face)
324{
325 while (self->faces <= face)
326 {
327 append (self->face2tile, self->faces, self->faces);
328 self->faces *= 2;
329 }
330}
331
332static void
333need_texid (struct mapgrid *self, int texid)
334{
335 while (self->texs <= texid)
336 {
337 append (self->tex, self->texs, self->texs);
338 self->texs *= 2;
339 }
340}
332 341
333static maprow * 342static maprow *
334map_get_row (mapgrid *self, int y) 343map_get_row (mapgrid *self, int y)
335{ 344{
336 if (0 > y) 345 if (0 > y)
337 { 346 {
338 int extend = - y + MAP_EXTEND_Y; 347 int extend = - y + MAP_EXTEND_Y;
339 Prepend (maprow, self->row, self->rows, extend); 348 prepend (self->row, self->rows, extend);
340 349
341 self->rows += extend; 350 self->rows += extend;
342 self->y += extend; 351 self->y += extend;
343 y += extend; 352 y += extend;
344 } 353 }
345 else if (y >= self->rows) 354 else if (y >= self->rows)
346 { 355 {
347 int extend = y - self->rows + MAP_EXTEND_Y; 356 int extend = y - self->rows + MAP_EXTEND_Y;
348 Append (maprow, self->row, self->rows, extend); 357 append (self->row, self->rows, extend);
349 self->rows += extend; 358 self->rows += extend;
350 } 359 }
351 360
352 return self->row + y; 361 return self->row + y;
353} 362}
363 } 372 }
364 373
365 if (row->c0 > x) 374 if (row->c0 > x)
366 { 375 {
367 int extend = row->c0 - x + MAP_EXTEND_X; 376 int extend = row->c0 - x + MAP_EXTEND_X;
368 Prepend (mapcell, row->col, row->c1 - row->c0, extend); 377 prepend (row->col, row->c1 - row->c0, extend);
369 row->c0 -= extend; 378 row->c0 -= extend;
370 } 379 }
371 else if (x >= row->c1) 380 else if (x >= row->c1)
372 { 381 {
373 int extend = x - row->c1 + MAP_EXTEND_X; 382 int extend = x - row->c1 + MAP_EXTEND_X;
374 Append (mapcell, row->col, row->c1 - row->c0, extend); 383 append (row->col, row->c1 - row->c0, extend);
375 row->c1 += extend; 384 row->c1 += extend;
376 } 385 }
377 386
378 return row->col + (x - row->c0); 387 return row->col + (x - row->c0);
379} 388}
382map_get_cell (mapgrid *self, int x, int y) 391map_get_cell (mapgrid *self, int x, int y)
383{ 392{
384 return row_get_cell (map_get_row (self, y), x); 393 return row_get_cell (map_get_row (self, y), x);
385} 394}
386 395
387void mapgrid::clear_cells () 396static void
397map_clear (mapgrid *self)
388{ 398{
389 int r; 399 int r;
390 400
391 for (r = 0; r < rows; r++) 401 for (r = 0; r < self->rows; r++)
392 Safefree (row[r].col); 402 Safefree (self->row[r].col);
393 403
394 Safefree (row); 404 Safefree (self->row);
395 405
396 x = 0; 406 self->x = 0;
397 y = 0; 407 self->y = 0;
398 ox = 0; 408 self->ox = 0;
399 oy = 0; 409 self->oy = 0;
400 row = 0; 410 self->row = 0;
401 rows = 0; 411 self->rows = 0;
402} 412}
403 413
404#define CELL_CLEAR(cell) \ 414#define CELL_CLEAR(cell) \
405 do { \ 415 do { \
406 if ((cell)->player) \ 416 if ((cell)->player) \
1886PROTOTYPES: DISABLE 1896PROTOTYPES: DISABLE
1887 1897
1888DC::Map 1898DC::Map
1889new (SV *klass) 1899new (SV *klass)
1890 CODE: 1900 CODE:
1891 RETVAL = new mapgrid; 1901 New (0, RETVAL, 1, mapgrid);
1892 RETVAL->x = 0; 1902 RETVAL->x = 0;
1893 RETVAL->y = 0; 1903 RETVAL->y = 0;
1894 RETVAL->w = 0; 1904 RETVAL->w = 0;
1895 RETVAL->h = 0; 1905 RETVAL->h = 0;
1896 RETVAL->ox = 0; 1906 RETVAL->ox = 0;
1897 RETVAL->oy = 0; 1907 RETVAL->oy = 0;
1908 RETVAL->faces = 8192; Newz (0, RETVAL->face2tile, RETVAL->faces, tileid);
1909 RETVAL->texs = 8192; Newz (0, RETVAL->tex , RETVAL->texs , maptex);
1898 RETVAL->rows = 0; 1910 RETVAL->rows = 0;
1899 RETVAL->row = 0; 1911 RETVAL->row = 0;
1900 OUTPUT: 1912 OUTPUT:
1901 RETVAL 1913 RETVAL
1902 1914
1903void 1915void
1904DESTROY (DC::Map self) 1916DESTROY (DC::Map self)
1905 CODE: 1917 CODE:
1906{ 1918{
1919 map_clear (self);
1920 Safefree (self->face2tile);
1921 Safefree (self->tex);
1907 delete self; 1922 Safefree (self);
1908} 1923}
1909 1924
1910void 1925void
1911resize (DC::Map self, int map_width, int map_height) 1926resize (DC::Map self, int map_width, int map_height)
1912 CODE: 1927 CODE:
1914 self->h = map_height; 1929 self->h = map_height;
1915 1930
1916void 1931void
1917clear (DC::Map self) 1932clear (DC::Map self)
1918 CODE: 1933 CODE:
1919 self->clear_cells (); 1934 map_clear (self);
1920 1935
1921void 1936void
1922set_tileid (DC::Map self, int face, int tile) 1937set_tileid (DC::Map self, int face, int tile)
1923 CODE: 1938 CODE:
1924{ 1939{
1925 if (self->tile.size () <= face) self->tile.resize (face + 1); 1940 need_facenum (self, face); self->face2tile [face] = tile;
1926 self->tile[face] = tile; 1941 need_texid (self, tile);
1927 if (self->tex.size () <= tile) self->tex .resize (tile + 1);
1928} 1942}
1929 1943
1930void 1944void
1931set_smooth (DC::Map self, int face, int smooth, int level) 1945set_smooth (DC::Map self, int face, int smooth, int level)
1932 CODE: 1946 CODE:
1933{ 1947{
1948 tileid texid;
1949 maptex *tex;
1950
1934 if (face < 0 || face >= self->tile.size ()) 1951 if (face < 0 || face >= self->faces)
1935 return; 1952 return;
1936 1953
1937 if (smooth < 0 || smooth >= self->tile.size ()) 1954 if (smooth < 0 || smooth >= self->faces)
1938 return; 1955 return;
1939 1956
1940 tileid texid = self->tile[face]; 1957 texid = self->face2tile [face];
1941 1958
1942 if (!texid) 1959 if (!texid)
1943 return; 1960 return;
1944 1961
1945 maptex &tex = self->tex[texid]; 1962 tex = self->tex + texid;
1946 tex.smoothtile = self->tile[smooth]; 1963 tex->smoothtile = self->face2tile [smooth];
1947 tex.smoothlevel = level; 1964 tex->smoothlevel = level;
1948} 1965}
1949 1966
1950void 1967void
1951set_texture (DC::Map self, int texid, int name, int w, int h, float s, float t, int r, int g, int b, int a) 1968set_texture (DC::Map self, int texid, int name, int w, int h, float s, float t, int r, int g, int b, int a)
1952 CODE: 1969 CODE:
1953{ 1970{
1954 if (self->tex.size () < texid) self->tex.resize (texid + 1); 1971 need_texid (self, texid);
1955 1972
1956 { 1973 {
1957 maptex &tex = self->tex[texid]; 1974 maptex *tex = self->tex + texid;
1958 1975
1959 tex.name = name; 1976 tex->name = name;
1960 tex.w = w; 1977 tex->w = w;
1961 tex.h = h; 1978 tex->h = h;
1962 tex.s = s; 1979 tex->s = s;
1963 tex.t = t; 1980 tex->t = t;
1964 tex.r = r; 1981 tex->r = r;
1965 tex.g = g; 1982 tex->g = g;
1966 tex.b = b; 1983 tex->b = b;
1967 tex.a = a; 1984 tex->a = a;
1968 } 1985 }
1969 1986
1970 // somewhat hackish, but for textures that require it, it really 1987 // somewhat hackish, but for textures that require it, it really
1971 // improves the look, and most others don't suffer. 1988 // improves the look, and most others don't suffer.
1972 glBindTexture (GL_TEXTURE_2D, name); 1989 glBindTexture (GL_TEXTURE_2D, name);
1978} 1995}
1979 1996
1980void 1997void
1981expire_textures (DC::Map self, int texid, int count) 1998expire_textures (DC::Map self, int texid, int count)
1982 PPCODE: 1999 PPCODE:
1983 for (; texid < self->tex.size () && count; ++texid, --count) 2000 for (; texid < self->texs && count; ++texid, --count)
1984 { 2001 {
1985 maptex &tex = self->tex[texid]; 2002 maptex *tex = self->tex + texid;
1986 2003
1987 if (tex.name) 2004 if (tex->name)
1988 { 2005 {
1989 if (tex.unused) 2006 if (tex->unused)
1990 { 2007 {
1991 tex.name = 0; 2008 tex->name = 0;
1992 tex.unused = 0; 2009 tex->unused = 0;
1993 XPUSHs (sv_2mortal (newSViv (texid))); 2010 XPUSHs (sv_2mortal (newSViv (texid)));
1994 } 2011 }
1995 else 2012 else
1996 tex.unused = 1; 2013 tex->unused = 1;
1997 } 2014 }
1998 } 2015 }
1999 2016
2000int 2017int
2001ox (DC::Map self) 2018ox (DC::Map self)
2035 self->ox += dx; self->x += dx; 2052 self->ox += dx; self->x += dx;
2036 self->oy += dy; self->y += dy; 2053 self->oy += dy; self->y += dy;
2037 2054
2038 while (self->y < 0) 2055 while (self->y < 0)
2039 { 2056 {
2040 Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y); 2057 prepend (self->row, self->rows, MAP_EXTEND_Y);
2041 2058
2042 self->rows += MAP_EXTEND_Y; 2059 self->rows += MAP_EXTEND_Y;
2043 self->y += MAP_EXTEND_Y; 2060 self->y += MAP_EXTEND_Y;
2044 } 2061 }
2045} 2062}
2112 } 2129 }
2113 2130
2114 for (z = 0; z <= 2; ++z) 2131 for (z = 0; z <= 2; ++z)
2115 if (flags & (4 >> z)) 2132 if (flags & (4 >> z))
2116 { 2133 {
2117 faceid face = (data[0] << 8) + data[1]; data += 2; 2134 faceid face = (data [0] << 8) + data [1]; data += 2;
2118 if (self->tile.size () <= face) self->tile.resize (face + 1); 2135 need_facenum (self, face);
2119 cell->tile[z] = self->tile[face]; 2136 cell->tile [z] = self->face2tile [face];
2120 2137
2121 if (cell->tile[z]) 2138 if (cell->tile [z])
2122 { 2139 {
2123 maptex &tex = self->tex[cell->tile[z]]; 2140 maptex *tex = self->tex + cell->tile [z];
2124 tex.unused = 0; 2141 tex->unused = 0;
2125 if (!tex.name) 2142 if (!tex->name)
2126 av_push (missing, newSViv (cell->tile [z])); 2143 av_push (missing, newSViv (cell->tile [z]));
2127 2144
2128 if (tex.smoothtile) 2145 if (tex->smoothtile)
2129 { 2146 {
2130 maptex &smooth = self->tex[tex.smoothtile]; 2147 maptex *smooth = self->tex + tex->smoothtile;
2131 smooth.unused = 0; 2148 smooth->unused = 0;
2132 if (!smooth.name) 2149 if (!smooth->name)
2133 av_push (missing, newSViv (tex.smoothtile)); 2150 av_push (missing, newSViv (tex->smoothtile));
2134 } 2151 }
2135 } 2152 }
2136 } 2153 }
2137 } 2154 }
2138 else 2155 else
2787 2804
2788 for (z = 0; z <= 2; z++) 2805 for (z = 0; z <= 2; z++)
2789 { 2806 {
2790 tileid t = tile [z]; 2807 tileid t = tile [z];
2791 2808
2792 if (t >= self->tex.size () || (t && !self->tex[t].name)) 2809 if (t >= self->texs || (t && !self->tex [t].name))
2793 { 2810 {
2794 PUSHs (sv_2mortal (newSViv (t))); 2811 PUSHs (sv_2mortal (newSViv (t)));
2795 if (self->tex.size () <= t) self->tex.resize (t + 1); 2812 need_texid (self, t);
2796 } 2813 }
2797 2814
2798 cell->tile[z] = t; 2815 cell->tile [z] = t;
2799 } 2816 }
2800 } 2817 }
2801 } 2818 }
2802 } 2819 }
2803 } 2820 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines