--- deliantra/Deliantra-Client/Client.xs 2006/04/17 21:52:42 1.47 +++ deliantra/Deliantra-Client/Client.xs 2006/04/19 21:38:04 1.53 @@ -1,3 +1,7 @@ +#ifdef _WIN32 +# include +#endif + #include "EXTERN.h" #include "perl.h" #include "XSUB.h" @@ -7,6 +11,7 @@ #include #include +#include #include #include @@ -15,18 +20,29 @@ #include #include -#include -#include -#include -#include - -#include +#ifndef _WIN32 +# include +# include +# include +# include +# include +#else + typedef unsigned char uint8_t; + typedef unsigned short uint16_t; + typedef unsigned int uint32_t; + typedef signed char int8_t; + typedef signed short int16_t; + typedef signed int int32_t; +#endif #define FOW_DARKNESS 32 #define MAP_EXTEND_X 32 #define MAP_EXTEND_Y 512 +typedef Mix_Chunk *CFClient__MixChunk; +typedef Mix_Music *CFClient__MixMusic; + static PangoContext *context; static PangoFontMap *fontmap; @@ -99,7 +115,7 @@ int texs; maptex *tex; - uint32_t rows; + int32_t rows; maprow *row; } *CFClient__Map; @@ -204,6 +220,7 @@ map_blank (CFClient__Map self, int x0, int y0, int w, int h) { int x, y; + maprow *row; for (y = y0; y < y0 + h; y++) if (y >= 0) @@ -211,7 +228,7 @@ if (y >= self->rows) break; - maprow *row = self->row + y; + row = self->row + y; for (x = x0; x < x0 + w; x++) if (x >= row->c0) @@ -230,15 +247,234 @@ BOOT: { - fontmap = pango_ft2_font_map_new (); - pango_ft2_font_map_set_default_substitute ((PangoFT2FontMap *)fontmap, substitute_func, 0, 0); - context = pango_ft2_font_map_create_context ((PangoFT2FontMap *)fontmap); + HV *stash = gv_stashpv ("CFClient", 1); + static const struct { + const char *name; + IV iv; + } *civ, const_iv[] = { +# define const_iv(name) { # name, (IV)name } + const_iv (SDL_ACTIVEEVENT), + const_iv (SDL_KEYDOWN), + const_iv (SDL_KEYUP), + const_iv (SDL_MOUSEMOTION), + const_iv (SDL_MOUSEBUTTONDOWN), + const_iv (SDL_MOUSEBUTTONUP), + const_iv (SDL_JOYAXISMOTION), + const_iv (SDL_JOYBALLMOTION), + const_iv (SDL_JOYHATMOTION), + const_iv (SDL_JOYBUTTONDOWN), + const_iv (SDL_JOYBUTTONUP), + const_iv (SDL_QUIT), + const_iv (SDL_SYSWMEVENT), + const_iv (SDL_EVENT_RESERVEDA), + const_iv (SDL_EVENT_RESERVEDB), + const_iv (SDL_VIDEORESIZE), + const_iv (SDL_VIDEOEXPOSE), + const_iv (SDL_USEREVENT), + const_iv (SDLK_KP0), + const_iv (SDLK_KP1), + const_iv (SDLK_KP2), + const_iv (SDLK_KP3), + const_iv (SDLK_KP4), + const_iv (SDLK_KP5), + const_iv (SDLK_KP6), + const_iv (SDLK_KP7), + const_iv (SDLK_KP8), + const_iv (SDLK_KP9), + const_iv (SDLK_KP_PERIOD), + const_iv (SDLK_KP_DIVIDE), + const_iv (SDLK_KP_MULTIPLY), + const_iv (SDLK_KP_MINUS), + const_iv (SDLK_KP_PLUS), + const_iv (SDLK_KP_ENTER), + const_iv (SDLK_KP_EQUALS), + const_iv (SDLK_UP), + const_iv (SDLK_DOWN), + const_iv (SDLK_RIGHT), + const_iv (SDLK_LEFT), + const_iv (SDLK_INSERT), + const_iv (SDLK_HOME), + const_iv (SDLK_END), + const_iv (SDLK_PAGEUP), + const_iv (SDLK_PAGEDOWN), + const_iv (SDLK_F1), + const_iv (SDLK_F2), + const_iv (SDLK_F3), + const_iv (SDLK_F4), + const_iv (SDLK_F5), + const_iv (SDLK_F6), + const_iv (SDLK_F7), + const_iv (SDLK_F8), + const_iv (SDLK_F9), + const_iv (SDLK_F10), + const_iv (SDLK_F11), + const_iv (SDLK_F12), + const_iv (SDLK_F13), + const_iv (SDLK_F14), + const_iv (SDLK_F15), + const_iv (SDLK_NUMLOCK), + const_iv (SDLK_CAPSLOCK), + const_iv (SDLK_SCROLLOCK), + const_iv (SDLK_RSHIFT), + const_iv (SDLK_LSHIFT), + const_iv (SDLK_RCTRL), + const_iv (SDLK_LCTRL), + const_iv (SDLK_RALT), + const_iv (SDLK_LALT), + const_iv (SDLK_RMETA), + const_iv (SDLK_LMETA), + const_iv (SDLK_LSUPER), + const_iv (SDLK_RSUPER), + const_iv (SDLK_MODE), + const_iv (SDLK_COMPOSE), + const_iv (SDLK_HELP), + const_iv (SDLK_PRINT), + const_iv (SDLK_SYSREQ), + const_iv (SDLK_BREAK), + const_iv (SDLK_MENU), + const_iv (SDLK_POWER), + const_iv (SDLK_EURO), + const_iv (SDLK_UNDO), + const_iv (KMOD_NONE), + const_iv (KMOD_LSHIFT), + const_iv (KMOD_RSHIFT), + const_iv (KMOD_LCTRL), + const_iv (KMOD_RCTRL), + const_iv (KMOD_LALT), + const_iv (KMOD_RALT), + const_iv (KMOD_LMETA), + const_iv (KMOD_RMETA), + const_iv (KMOD_NUM), + const_iv (KMOD_CAPS), + const_iv (KMOD_MODE), + const_iv (KMOD_CTRL), + const_iv (KMOD_SHIFT), + const_iv (KMOD_ALT), + const_iv (KMOD_META) +# undef const_iv + }; + + for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) + newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); + + fontmap = pango_ft2_font_map_new (); + pango_ft2_font_map_set_default_substitute ((PangoFT2FontMap *)fontmap, substitute_func, 0, 0); + context = pango_ft2_font_map_create_context ((PangoFT2FontMap *)fontmap); +} + +int +SDL_Init (U32 flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO) + +void +SDL_Quit () + +void +SDL_ListModes () + PPCODE: +{ + SDL_Rect **m; + + SDL_GL_SetAttribute (SDL_GL_RED_SIZE, 5); + SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, 5); + SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, 5); + SDL_GL_SetAttribute (SDL_GL_ALPHA_SIZE, 1); + + SDL_GL_SetAttribute (SDL_GL_ACCUM_RED_SIZE, 0); + SDL_GL_SetAttribute (SDL_GL_ACCUM_GREEN_SIZE, 0); + SDL_GL_SetAttribute (SDL_GL_ACCUM_BLUE_SIZE, 0); + SDL_GL_SetAttribute (SDL_GL_ACCUM_ALPHA_SIZE, 0); + + SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1); + SDL_GL_SetAttribute (SDL_GL_BUFFER_SIZE, 15); + SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 0); + + SDL_EnableUNICODE (1); + SDL_EnableKeyRepeat (SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); + + m = SDL_ListModes (0, SDL_FULLSCREEN | SDL_OPENGL); + + if (m && m != (SDL_Rect **)-1) + while (*m) + { + AV *av = newAV (); + av_push (av, newSViv ((*m)->w)); + av_push (av, newSViv ((*m)->h)); + XPUSHs (sv_2mortal (newRV_noinc ((SV *)av))); + + ++m; + } +} + +int +SDL_SetVideoMode (int w, int h, int fullscreen) + CODE: + RETVAL = !!SDL_SetVideoMode ( + w, h, 0, SDL_OPENGL | (fullscreen ? SDL_FULLSCREEN : 0) + ); + SDL_WM_SetCaption ("Crossfire+ Client " VERSION, "Crossfire+"); + OUTPUT: + RETVAL + +void +SDL_PollEvent () + PPCODE: +{ + SDL_Event ev; + + while (SDL_PollEvent (&ev)) + { + HV *hv = newHV (); + hv_store (hv, "type", 4, newSViv (ev.type), 0); + switch (ev.type) + { + case SDL_KEYDOWN: + case SDL_KEYUP: + hv_store (hv, "state", 5, newSViv (ev.key.state), 0); + hv_store (hv, "sym", 3, newSViv (ev.key.keysym.sym), 0); + hv_store (hv, "mod", 3, newSViv (ev.key.keysym.mod), 0); + hv_store (hv, "unicode", 7, newSViv (ev.key.keysym.unicode), 0); + break; + + case SDL_ACTIVEEVENT: + hv_store (hv, "gain", 4, newSViv (ev.active.gain), 0); + hv_store (hv, "state", 5, newSViv (ev.active.state), 0); + break; + + case SDL_MOUSEMOTION: + hv_store (hv, "state", 5, newSViv (ev.motion.state), 0); + hv_store (hv, "x", 1, newSViv (ev.motion.x), 0); + hv_store (hv, "y", 1, newSViv (ev.motion.y), 0); + hv_store (hv, "xrel", 4, newSViv (ev.motion.xrel), 0); + hv_store (hv, "yrel", 4, newSViv (ev.motion.yrel), 0); + break; + + case SDL_MOUSEBUTTONDOWN: + case SDL_MOUSEBUTTONUP: + hv_store (hv, "button", 6, newSViv (ev.button.button), 0); + hv_store (hv, "state", 5, newSViv (ev.button.state), 0); + hv_store (hv, "x", 1, newSViv (ev.button.x), 0); + hv_store (hv, "y", 1, newSViv (ev.button.y), 0); + } + + XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv))); + } } +int +Mix_OpenAudio (int frequency = 22050, int format = MIX_DEFAULT_FORMAT, int channels = 1, int chunksize = 512) + +void +Mix_CloseAudio () + +int +Mix_AllocateChannels (int numchans = -1) + void lowdelay (int fd, int val = 1) CODE: +#ifndef _WIN32 setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof (val)); +#endif char * gl_version () @@ -294,10 +530,17 @@ fmt.palette = NULL; fmt.BitsPerPixel = 32; fmt.BytesPerPixel = 4; +#if SDL_BYTEORDER == SDL_LIL_ENDIAN fmt.Rmask = 0x000000ff; fmt.Gmask = 0x0000ff00; fmt.Bmask = 0x00ff0000; fmt.Amask = 0xff000000; +#else + fmt.Rmask = 0xff000000; + fmt.Gmask = 0x00ff0000; + fmt.Bmask = 0x0000ff00; + fmt.Amask = 0x000000ff; +#endif fmt.Rloss = 0; fmt.Gloss = 0; fmt.Bloss = 0; @@ -321,7 +564,7 @@ SDL_UnlockSurface (surface2); PUSHs (sv_2mortal (newSViv (surface->flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA) ? GL_RGBA : GL_RGB))); PUSHs (sv_2mortal (newSViv (GL_RGBA))); - PUSHs (sv_2mortal (newSViv (GL_UNSIGNED_INT_8_8_8_8_REV))); + PUSHs (sv_2mortal (newSViv (GL_UNSIGNED_BYTE))); SDL_FreeSurface (surface); SDL_FreeSurface (surface2); @@ -355,7 +598,7 @@ void fatal (char *message) CODE: -#ifdef WIN32 +#ifdef _WIN32 MessageBox (0, message, "Crossfire+ Fatal Error", MB_OK | MB_ICONERROR | MB_SETFOREGROUND); #else fprintf (stderr, "FATAL: %s\n", message); @@ -582,17 +825,19 @@ self->texs *= 2; } - maptex *tex = self->tex + texid; + { + maptex *tex = self->tex + texid; - tex->name = name; - tex->w = w; - tex->h = h; - tex->s = s; - tex->t = t; - tex->r = r; - tex->g = g; - tex->b = b; - tex->a = a; + tex->name = name; + tex->w = w; + tex->h = h; + tex->s = s; + tex->t = t; + tex->r = r; + tex->g = g; + tex->b = b; + tex->a = a; + } } int @@ -640,15 +885,17 @@ { uint8_t *data = (uint8_t *)SvPVbyte_nolen (data_); uint8_t *data_end = (uint8_t *)SvEND (data_); + mapcell *cell; + int x, y, flags; while (data < data_end) { - int flags = (data [0] << 8) + data [1]; data += 2; + flags = (data [0] << 8) + data [1]; data += 2; - int x = ((flags >> 10) & 63) + self->x; - int y = ((flags >> 4) & 63) + self->y; + x = ((flags >> 10) & 63) + self->x; + y = ((flags >> 4) & 63) + self->y; - mapcell *cell = map_get_cell (self, x, y); + cell = map_get_cell (self, x, y); if (flags & 15) { @@ -748,15 +995,20 @@ draw (CFClient::Map self, int shift_x, int shift_y, int x0, int y0, int sw, int sh) PPCODE: { + int vx, vy; + int x, y, z; + int last_name; + mapface face; int sw4 = (sw + 3) & ~3; SV *darkness_sv = sv_2mortal (newSV (sw4 * sh)); uint8_t *darkness = (uint8_t *)SvPVX (darkness_sv); + memset (darkness, 255, sw4 * sh); SvPOK_only (darkness_sv); SvCUR_set (darkness_sv, sw4 * sh); - int vx = self->x + (self->w - sw) / 2 - shift_x; - int vy = self->y + (self->h - sh) / 2 - shift_y; + vx = self->x + (self->w - sw) / 2 - shift_x; + vy = self->y + (self->h - sh) / 2 - shift_y; /* int vx = self->vx = self->w >= sw @@ -775,12 +1027,10 @@ glEnable (GL_TEXTURE_2D); glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); - int x, y, z; - - int last_name = 0; - glBegin (GL_QUADS); + last_name = 0; + for (z = 0; z < 3; z++) for (y = 0; y < sh; y++) if (0 <= y + vy && y + vy < self->rows) @@ -796,7 +1046,7 @@ ? 255 - FOW_DARKNESS : 255 - cell->darkness; - mapface face = cell->face [z]; + face = cell->face [z]; if (face) { @@ -866,7 +1116,6 @@ if (row && row->c0 <= x && x < row->c1) { mapcell *cell = row->col + (x - row->c0); - uint8_t flags = 0; if (cell->face [0]) flags |= 1; @@ -910,13 +1159,14 @@ PPCODE: { int x, y, z; + int w, h; int x1, y1; if (*data++ != 0) return; /* version mismatch */ - int w = *data++ << 8; w |= *data++; - int h = *data++ << 8; h |= *data++; + w = *data++ << 8; w |= *data++; + h = *data++ << 8; h |= *data++; // we need to do this 'cause we don't keep an absolute coord system for rows // TODO: treat rows as we treat @@ -964,3 +1214,59 @@ } } +MODULE = CFClient PACKAGE = CFClient::MixChunk + +CFClient::MixChunk +new_from_file (SV *class, char *path) + CODE: + RETVAL = Mix_LoadWAV (path); + OUTPUT: + RETVAL + +void +DESTROY (CFClient::MixChunk self) + CODE: + Mix_FreeChunk (self); + +int +volume (CFClient::MixChunk self, int volume = -1) + CODE: + RETVAL = Mix_VolumeChunk (self, volume); + OUTPUT: + RETVAL + +int +play (CFClient::MixChunk self, int channel = -1, int loops = 0, int ticks = -1) + CODE: + RETVAL = Mix_PlayChannelTimed (channel, self, loops, ticks); + OUTPUT: + RETVAL + +MODULE = CFClient PACKAGE = CFClient::MixMusic + +int +volume (int volume = -1) + CODE: + RETVAL = Mix_VolumeMusic (volume); + OUTPUT: + RETVAL + +CFClient::MixMusic +new_from_file (SV *class, char *path) + CODE: + RETVAL = Mix_LoadMUS (path); + OUTPUT: + RETVAL + +void +DESTROY (CFClient::MixMusic self) + CODE: + Mix_FreeMusic (self); + +int +play (CFClient::MixMusic self, int loops = -1) + CODE: + RETVAL = Mix_PlayMusic (self, loops); + OUTPUT: + RETVAL +