--- deliantra/Deliantra-Client/Client.xs 2007/04/21 23:54:23 1.189 +++ deliantra/Deliantra-Client/Client.xs 2007/08/09 11:02:08 1.220 @@ -8,6 +8,11 @@ # pragma warning(disable:4761) #endif +//#define DEBUG 1 +#if DEBUG +# include +#endif + #include "EXTERN.h" #include "perl.h" #include "XSUB.h" @@ -22,7 +27,10 @@ #include #include +#define USE_RWOPS 1 // for SDL_mixer:LoadMUS_RW + #include +#include #include #include #include @@ -67,7 +75,8 @@ #undef GL_FUNC } gl; -static void gl_BlendFuncSeparate (GLenum sa, GLenum da, GLenum saa, GLenum daa) +static void +gl_BlendFuncSeparate (GLenum sa, GLenum da, GLenum saa, GLenum daa) { if (gl.BlendFuncSeparate) gl.BlendFuncSeparate (sa, da, saa, daa); @@ -77,12 +86,38 @@ glBlendFunc (sa, da); } +static GLuint +gen_texture () +{ + GLuint name; + + if (AvFILL (texture_av) >= 0) + name = (GLuint)(size_t)av_pop (texture_av); + else + glGenTextures (1, &name); + + return name; +} + +static void +del_texture (GLuint name) +{ + /* make a half-assed attempt at returning the memory used by the texture */ + /* textures are frequently being reused by cfplus anyway */ + /*glBindTexture (GL_TEXTURE_2D, name);*/ + /*glTexImage2D (GL_TEXTURE_2D, 0, GL_ALPHA, 0, 0, 0, GL_ALPHA, GL_UNSIGNED_BYTE, 0);*/ + av_push (texture_av, (SV *)(size_t)name); + glDeleteTextures (1, &name); +} + #include "texcache.c" #include "pango-font.c" #include "pango-fontmap.c" #include "pango-render.c" +typedef IV CFPlus__Channel; +typedef SDL_RWops *CFPlus__RW; typedef Mix_Chunk *CFPlus__MixChunk; typedef Mix_Music *CFPlus__MixMusic; @@ -404,6 +439,49 @@ #define SDLK_MODIFIER_MIN 300 #define SDLK_MODIFIER_MAX 314 +/******************************************************************************/ + +static GV *draw_x_gv, *draw_y_gv, *draw_w_gv, *draw_h_gv; +static GV *hover_gv; + +static int +within_widget (SV *widget, NV x, NV y) +{ + HV *self; + SV **svp; + NV wx, ww, wy, wh; + + if (!SvROK (widget)) + return 0; + + self = (HV *)SvRV (widget); + + if (SvTYPE (self) != SVt_PVHV) + return 0; + + svp = hv_fetch (self, "y", 1, 0); wy = svp ? SvNV (*svp) : 0.; + if (y < wy) + return 0; + + svp = hv_fetch (self, "h", 1, 0); wh = svp ? SvNV (*svp) : 0.; + if (y >= wy + wh) + return 0; + + svp = hv_fetch (self, "x", 1, 0); wx = svp ? SvNV (*svp) : 0.; + if (x < wx) + return 0; + + svp = hv_fetch (self, "w", 1, 0); ww = svp ? SvNV (*svp) : 0.; + if (x >= wx + ww) + return 0; + + svp = hv_fetch (self, "can_events", sizeof ("can_events") - 1, 0); + if (!svp || !SvTRUE (*svp)) + return 0; + + return 1; +} + MODULE = CFPlus PACKAGE = CFPlus PROTOTYPES: ENABLE @@ -559,6 +637,9 @@ opengl_context = pango_opengl_font_map_create_context ((PangoOpenGLFontMap *)opengl_fontmap); } +char * +SDL_GetError () + int SDL_Init (U32 flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | PARACHUTE) @@ -566,22 +647,22 @@ SDL_Quit () void -SDL_ListModes () +SDL_ListModes (int rgb, int alpha) 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_RED_SIZE , rgb); + SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, rgb); + SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE , rgb); + SDL_GL_SetAttribute (SDL_GL_ALPHA_SIZE, alpha); SDL_GL_SetAttribute (SDL_GL_BUFFER_SIZE, 15); - SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 0); + SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE , 0); - SDL_GL_SetAttribute (SDL_GL_ACCUM_RED_SIZE, 0); + 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_BLUE_SIZE , 0); SDL_GL_SetAttribute (SDL_GL_ACCUM_ALPHA_SIZE, 0); SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1); @@ -590,42 +671,51 @@ SDL_GL_SetAttribute (SDL_GL_SWAP_CONTROL, 1); #endif - 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))); + if ((*m)->w >= 640 && (*m)->h >= 480) + { + AV *av = newAV (); + av_push (av, newSViv ((*m)->w)); + av_push (av, newSViv ((*m)->h)); + av_push (av, newSViv (rgb)); + av_push (av, newSViv (alpha)); + XPUSHs (sv_2mortal (newRV_noinc ((SV *)av))); + } ++m; } } -char * -SDL_GetError () - int -SDL_SetVideoMode (int w, int h, int fullscreen) +SDL_SetVideoMode (int w, int h, int rgb, int alpha, int fullscreen) CODE: +{ + SDL_EnableUNICODE (1); + SDL_EnableKeyRepeat (SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); + + SDL_GL_SetAttribute (SDL_GL_RED_SIZE , rgb); + SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, rgb); + SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE , rgb); + SDL_GL_SetAttribute (SDL_GL_ALPHA_SIZE, alpha); + RETVAL = !!SDL_SetVideoMode ( w, h, 0, SDL_OPENGL | (fullscreen ? SDL_FULLSCREEN : 0) ); + if (RETVAL) { av_clear (texture_av); SDL_WM_SetCaption ("Crossfire+ Client " VERSION, "Crossfire+"); -# define GL_FUNC(ptr,name) gl.name = (ptr)SDL_GL_GetProcAddress ("gl" # name); -# include "glfunc.h" -# undef GL_FUNC - +#define GL_FUNC(ptr,name) gl.name = (ptr)SDL_GL_GetProcAddress ("gl" # name); +#include "glfunc.h" +#undef GL_FUNC } +} OUTPUT: RETVAL @@ -635,13 +725,17 @@ char * SDL_GetKeyName (int sym) +int +SDL_GetAppState () + void -SDL_PollEvent () +poll_events () PPCODE: { SDL_Event ev; - while (SDL_PollEvent (&ev)) + SDL_PumpEvents (); + while (SDL_PeepEvents (&ev, 1, SDL_GETEVENT, SDL_ALLEVENTS) > 0) { HV *hv = newHV (); hv_store (hv, "type", 4, newSViv (ev.type), 0); @@ -663,13 +757,31 @@ break; case SDL_MOUSEMOTION: - hv_store (hv, "mod", 3, newSViv (SDL_GetModState ()), 0); + { + int state = ev.motion.state; + int x = ev.motion.x; + int y = ev.motion.y; + int xrel = ev.motion.xrel; + int yrel = ev.motion.yrel; + + /* do simplistic event compression */ + while (SDL_PeepEvents (&ev, 1, SDL_PEEKEVENT, SDL_EVENTMASK (SDL_MOUSEMOTION)) > 0 + && state == ev.motion.state) + { + xrel += ev.motion.xrel; + yrel += ev.motion.yrel; + x = ev.motion.x; + y = ev.motion.y; + SDL_PeepEvents (&ev, 1, SDL_GETEVENT, SDL_EVENTMASK (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); + hv_store (hv, "mod", 3, newSViv (SDL_GetModState ()), 0); + hv_store (hv, "state", 5, newSViv (state), 0); + hv_store (hv, "x", 1, newSViv (x), 0); + hv_store (hv, "y", 1, newSViv (y), 0); + hv_store (hv, "xrel", 4, newSViv (xrel), 0); + hv_store (hv, "yrel", 4, newSViv (yrel), 0); + } break; case SDL_MOUSEBUTTONDOWN: @@ -694,7 +806,7 @@ } int -Mix_OpenAudio (int frequency = 48000, int format = MIX_DEFAULT_FORMAT, int channels = 1, int chunksize = 2048) +Mix_OpenAudio (int frequency = 44100, int format = MIX_DEFAULT_FORMAT, int channels = 2, int chunksize = 1024) POSTCALL: Mix_HookMusicFinished (music_finished); Mix_ChannelFinished (channel_finished); @@ -705,6 +817,9 @@ int Mix_AllocateChannels (int numchans = -1) +const char * +Mix_GetError () + void lowdelay (int fd, int val = 1) CODE: @@ -755,7 +870,7 @@ SDL_Surface *surface, *surface2; SDL_PixelFormat fmt; SDL_RWops *rw = ix - ? SDL_RWFromFile (image, "r") + ? SDL_RWFromFile (image, "rb") : SDL_RWFromConstMem (image, image_len); if (!rw) @@ -859,8 +974,19 @@ _exit (retval); #endif +void +debug () + CODE: +{ +#if DEBUG + VALGRIND_DO_LEAK_CHECK; +#endif +} + MODULE = CFPlus PACKAGE = CFPlus::Font +PROTOTYPES: DISABLE + CFPlus::Font new_from_file (SV *class, char *path, int id = 0) CODE: @@ -880,13 +1006,17 @@ void make_default (CFPlus::Font self) + PROTOTYPE: $ CODE: default_font = self; MODULE = CFPlus PACKAGE = CFPlus::Layout +PROTOTYPES: DISABLE + void reset_glyph_cache () + PROTOTYPE: CODE: tc_clear (); @@ -1183,40 +1313,29 @@ MODULE = CFPlus PACKAGE = CFPlus::Texture +PROTOTYPES: ENABLE + +int minpot (int n) + void -pad2pot (SV *data_, SV *w_, SV *h_) +pad (SV *data_, int ow, int oh, int nw, int nh) CODE: { - int ow = SvIV (w_); - int oh = SvIV (h_); - - if (ow && oh) + if ((nw != ow || nh != oh) && SvOK (data_)) { - int nw = minpot (ow); - int nh = minpot (oh); + STRLEN datalen; + char *data = SvPVbyte (data_, datalen); + int bpp = datalen / (ow * oh); + SV *result_ = sv_2mortal (newSV (nw * nh * bpp)); + + SvPOK_only (result_); + SvCUR_set (result_, nw * nh * bpp); + + memset (SvPVX (result_), 0, nw * nh * bpp); + while (oh--) + memcpy (SvPVX (result_) + oh * nw * bpp, data + oh * ow * bpp, ow * bpp); - if (nw != ow || nh != oh) - { - if (SvOK (data_)) - { - STRLEN datalen; - char *data = SvPVbyte (data_, datalen); - int bpp = datalen / (ow * oh); - SV *result_ = sv_2mortal (newSV (nw * nh * bpp)); - - SvPOK_only (result_); - SvCUR_set (result_, nw * nh * bpp); - - memset (SvPVX (result_), 0, nw * nh * bpp); - while (oh--) - memcpy (SvPVX (result_) + oh * nw * bpp, data + oh * ow * bpp, ow * bpp); - - sv_setsv (data_, result_); - } - - sv_setiv (w_, nw); - sv_setiv (h_, nh); - } + sv_setsv (data_, result_); } } @@ -1233,6 +1352,9 @@ float t = SvNV (*hv_fetch (hv, "t", 1, 1)); int name = SvIV (*hv_fetch (hv, "name", 4, 1)); + if (name <= 0) + XSRETURN_EMPTY; + if (items < 5) { w = SvNV (*hv_fetch (hv, "w", 1, 1)); @@ -1269,8 +1391,21 @@ } } +IV texture_valid_2d (GLint internalformat, GLsizei w, GLsizei h, GLenum format, GLenum type) + CODE: +{ + GLint width; + glTexImage2D (GL_PROXY_TEXTURE_2D, 0, internalformat, w, h, 0, format, type, 0); + glGetTexLevelParameteriv (GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width); + RETVAL = width > 0; +} + OUTPUT: + RETVAL + MODULE = CFPlus PACKAGE = CFPlus::Map +PROTOTYPES: DISABLE + CFPlus::Map new (SV *class) CODE: @@ -1566,7 +1701,7 @@ OUTPUT: RETVAL -void +SV * draw (CFPlus::Map self, int mx, int my, int sw, int sh, int T) CODE: { @@ -1576,6 +1711,8 @@ smooth_key skey; int x, y, z; int last_name; + AV *missing = newAV (); + RETVAL = newRV_noinc ((SV *)missing); // thats current max. sorry. if (sw > 255) sw = 255; @@ -1593,7 +1730,7 @@ glBegin (GL_QUADS); - last_name = 0; + last_name = -1; mx += self->x; my += self->y; @@ -1638,21 +1775,26 @@ if (tile) { maptex tex = self->tex [tile]; - int px = (x + 1) * T - tex.w; - int py = (y + 1) * T - tex.h; + int px, py; // suppressing texture state switches here // is only moderately effective, but worth the extra effort if (last_name != tex.name) { if (!tex.name) - tex = self->tex [2]; /* missing, replace by noface */ + { + av_push (missing, newSViv (tile)); + tex = self->tex [2]; /* missing, replace by noface */ + } glEnd (); glBindTexture (GL_TEXTURE_2D, last_name = tex.name); glBegin (GL_QUADS); } + px = (x + 1) * T - tex.w; + py = (y + 1) * T - tex.h; + glTexCoord2f (0 , 0 ); glVertex2f (px , py ); glTexCoord2f (0 , tex.t); glVertex2f (px , py + tex.h); glTexCoord2f (tex.s, tex.t); glVertex2f (px + tex.w, py + tex.h); @@ -1742,7 +1884,7 @@ if (!(bits & 0x1000) && skey->level == level - && level >= smooth_max [skey->x][skey->y]) + && level > smooth_max [skey->x][skey->y]) { maptex tex = self->tex [skey->tile]; int px = (((int)skey->x) - 1) * T; @@ -1757,7 +1899,10 @@ if (last_name != tex.name) { if (!tex.name) - continue; // smoothing not yet available + { + av_push (missing, newSViv (skey->tile)); + continue; // smoothing not yet available + } glEnd (); glBindTexture (GL_TEXTURE_2D, last_name = tex.name); @@ -1828,6 +1973,8 @@ } } } + OUTPUT: + RETVAL void draw_magicmap (CFPlus::Map self, int dx, int dy, int w, int h, unsigned char *data) @@ -1894,37 +2041,80 @@ PPCODE: { int x, y; - 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 sw1 = sw + 2; + int sh1 = sh + 2; + int sh3 = sh * 3; + int sw34 = (sw * 3 + 3) & ~3; + uint8_t *darkness1 = (uint8_t *)malloc (sw1 * sh1); + SV *darkness3_sv = sv_2mortal (newSV (sw34 * sh3)); + uint8_t *darkness3 = (uint8_t *)SvPVX (darkness3_sv); + + SvPOK_only (darkness3_sv); + SvCUR_set (darkness3_sv, sw34 * sh3); - mx += self->x; - my += self->y; + mx += self->x - 1; + my += self->y - 1; - for (y = 0; y < sh; y++) + memset (darkness1, 255, sw1 * sh1); + + for (y = 0; y < sh1; y++) if (0 <= y + my && y + my < self->rows) { maprow *row = self->row + (y + my); - for (x = 0; x < sw; x++) + for (x = 0; x < sw1; x++) if (row->c0 <= x + mx && x + mx < row->c1) { mapcell *cell = row->col + (x + mx - row->c0); - darkness[y * sw4 + x] = cell->darkness + darkness1 [y * sw1 + x] = cell->darkness ? 255 - (cell->darkness - 1) : 255 - FOW_DARKNESS; } } + for (y = 0; y < sh; ++y) + for (x = 0; x < sw; ++x) + { + uint8_t d11 = darkness1 [(y ) * sw1 + x ]; + uint8_t d21 = darkness1 [(y ) * sw1 + x + 1]; + uint8_t d31 = darkness1 [(y ) * sw1 + x + 2]; + uint8_t d12 = darkness1 [(y + 1) * sw1 + x ]; + uint8_t d22 = darkness1 [(y + 1) * sw1 + x + 1]; + uint8_t d32 = darkness1 [(y + 1) * sw1 + x + 2]; + uint8_t d13 = darkness1 [(y + 2) * sw1 + x ]; + uint8_t d23 = darkness1 [(y + 2) * sw1 + x + 1]; + uint8_t d33 = darkness1 [(y + 2) * sw1 + x + 2]; + + uint8_t r11 = (d11 + d21 + d12) / 3; + uint8_t r21 = d21; + uint8_t r31 = (d21 + d31 + d32) / 3; + + uint8_t r12 = d12; + uint8_t r22 = d22; + uint8_t r32 = d32; + + uint8_t r13 = (d13 + d23 + d12) / 3; + uint8_t r23 = d23; + uint8_t r33 = (d23 + d33 + d32) / 3; + + darkness3 [(y * 3 ) * sw34 + (x * 3 )] = MAX (d22, r11); + darkness3 [(y * 3 ) * sw34 + (x * 3 + 1)] = MAX (d22, r21); + darkness3 [(y * 3 ) * sw34 + (x * 3 + 2)] = MAX (d22, r31); + darkness3 [(y * 3 + 1) * sw34 + (x * 3 )] = MAX (d22, r12); + darkness3 [(y * 3 + 1) * sw34 + (x * 3 + 1)] = MAX (d22, r22); + darkness3 [(y * 3 + 1) * sw34 + (x * 3 + 2)] = MAX (d22, r32); + darkness3 [(y * 3 + 2) * sw34 + (x * 3 )] = MAX (d22, r13); + darkness3 [(y * 3 + 2) * sw34 + (x * 3 + 1)] = MAX (d22, r23); + darkness3 [(y * 3 + 2) * sw34 + (x * 3 + 2)] = MAX (d22, r33); + } + + free (darkness1); + EXTEND (SP, 3); - PUSHs (sv_2mortal (newSViv (sw4))); - PUSHs (sv_2mortal (newSViv (sh))); - PUSHs (darkness_sv); + PUSHs (sv_2mortal (newSViv (sw34))); + PUSHs (sv_2mortal (newSViv (sh3))); + PUSHs (darkness3_sv); } SV * @@ -2066,12 +2256,125 @@ } } +MODULE = CFPlus PACKAGE = CFPlus::RW + +CFPlus::RW +new (SV *class, SV *data_sv) + CODE: +{ + STRLEN datalen; + char *data = SvPVbyte (data_sv, datalen); + + RETVAL = SDL_RWFromConstMem (data, datalen); +} + OUTPUT: + RETVAL + +CFPlus::RW +new_from_file (SV *class, const char *path, const char *mode = "rb") + CODE: + RETVAL = SDL_RWFromFile (path, mode); + OUTPUT: + RETVAL + +# fails on win32: +# CFPlus.xs(2268) : error C2059: syntax error : '(' +#void +#close (CFPlus::RW self) +# CODE: +# (self->(close)) (self); + +MODULE = CFPlus PACKAGE = CFPlus::Channel + +PROTOTYPES: DISABLE + +CFPlus::Channel +find () + CODE: +{ + RETVAL = Mix_GroupAvailable (-1); + + if (RETVAL < 0) + { + RETVAL = Mix_GroupOldest (-1); + + if (RETVAL < 0) + XSRETURN_UNDEF; + + Mix_HaltChannel (RETVAL); + } + + Mix_UnregisterAllEffects (RETVAL); + Mix_Volume (RETVAL, 128); +} + OUTPUT: + RETVAL + +void +halt (CFPlus::Channel self) + CODE: + Mix_HaltChannel (self); + +void +expire (CFPlus::Channel self, int ticks = -1) + CODE: + Mix_ExpireChannel (self, ticks); + +void +fade_out (CFPlus::Channel self, int ticks = -1) + CODE: + Mix_FadeOutChannel (self, ticks); + +int +volume (CFPlus::Channel self, int volume) + CODE: + RETVAL = Mix_Volume (self, CLAMP (volume, 0, 128)); + OUTPUT: + RETVAL + +void +unregister_all_effects (CFPlus::Channel self) + CODE: + Mix_UnregisterAllEffects (self); + +void +set_panning (CFPlus::Channel self, int left, int right) + CODE: + left = CLAMP (left , 0, 255); + right = CLAMP (right, 0, 255); + Mix_SetPanning (self, left, right); + +void +set_distance (CFPlus::Channel self, int distance) + CODE: + Mix_SetDistance (self, CLAMP (distance, 0, 255)); + +void +set_position (CFPlus::Channel self, int angle, int distance) + CODE: + +void +set_position_r (CFPlus::Channel self, int dx, int dy, int maxdistance) + CODE: +{ + int distance = sqrtf (dx * dx + dy * dy) * (255.f / sqrtf (maxdistance * maxdistance)); + int angle = 360 + (int)roundf (atan2f (dx, -dy) * 180.f / (float)M_PI); + Mix_SetPosition (self, angle, CLAMP (distance, 0, 255)); +} + +void +set_reverse_stereo (CFPlus::Channel self, int flip) + CODE: + Mix_SetReverseStereo (self, flip); + MODULE = CFPlus PACKAGE = CFPlus::MixChunk +PROTOTYPES: DISABLE + CFPlus::MixChunk -new_from_file (SV *class, char *path) +new (SV *class, CFPlus::RW rwops) CODE: - RETVAL = Mix_LoadWAV (path); + RETVAL = Mix_LoadWAV_RW (rwops, 1); OUTPUT: RETVAL @@ -2083,14 +2386,27 @@ int volume (CFPlus::MixChunk self, int volume = -1) CODE: + if (items > 1) + volume = CLAMP (volume, 0, 128); RETVAL = Mix_VolumeChunk (self, volume); OUTPUT: RETVAL -int -play (CFPlus::MixChunk self, int channel = -1, int loops = 0, int ticks = -1) +CFPlus::Channel +play (CFPlus::MixChunk self, CFPlus::Channel channel = -1, int loops = 0, int ticks = -1) CODE: +{ RETVAL = Mix_PlayChannelTimed (channel, self, loops, ticks); + + if (RETVAL < 0) + XSRETURN_UNDEF; + + if (channel < 0) + { + Mix_UnregisterAllEffects (RETVAL); + Mix_Volume (RETVAL, 128); + } +} OUTPUT: RETVAL @@ -2098,15 +2414,28 @@ int volume (int volume = -1) + PROTOTYPE: ;$ CODE: + if (items > 0) + volume = CLAMP (volume, 0, 128); RETVAL = Mix_VolumeMusic (volume); OUTPUT: RETVAL +void +fade_out (int ms) + CODE: + Mix_FadeOutMusic (ms); + +void +halt () + CODE: + Mix_HaltMusic (); + CFPlus::MixMusic -new_from_file (SV *class, char *path) +new (SV *class, CFPlus::RW rwops) CODE: - RETVAL = Mix_LoadMUS (path); + RETVAL = Mix_LoadMUS_RW (rwops); OUTPUT: RETVAL @@ -2122,8 +2451,15 @@ OUTPUT: RETVAL +void +fade_in_pos (CFPlus::MixMusic self, int loops, int ms, double position) + CODE: + Mix_FadeInMusicPos (self, loops, ms, position); + MODULE = CFPlus PACKAGE = CFPlus::OpenGL +PROTOTYPES: ENABLE + BOOT: { HV *stash = gv_stashpv ("CFPlus::OpenGL", 1); @@ -2132,6 +2468,9 @@ IV iv; } *civ, const_iv[] = { # define const_iv(name) { # name, (IV)name } + const_iv (GL_VENDOR), + const_iv (GL_VERSION), + const_iv (GL_EXTENSIONS), const_iv (GL_COLOR_MATERIAL), const_iv (GL_SMOOTH), const_iv (GL_FLAT), @@ -2145,6 +2484,7 @@ const_iv (GL_RESCALE_NORMAL), const_iv (GL_FRONT), const_iv (GL_BACK), + const_iv (GL_AUX0), const_iv (GL_AND), const_iv (GL_ONE), const_iv (GL_ZERO), @@ -2168,6 +2508,8 @@ const_iv (GL_FLOAT), const_iv (GL_UNSIGNED_INT_8_8_8_8_REV), const_iv (GL_COMPILE), + const_iv (GL_PROXY_TEXTURE_1D), + const_iv (GL_PROXY_TEXTURE_2D), const_iv (GL_TEXTURE_1D), const_iv (GL_TEXTURE_2D), const_iv (GL_TEXTURE_ENV), @@ -2198,6 +2540,7 @@ const_iv (GL_CONVOLUTION_2D), const_iv (GL_CONVOLUTION_BORDER_MODE), const_iv (GL_CONSTANT_BORDER), + const_iv (GL_POINTS), const_iv (GL_LINES), const_iv (GL_LINE_STRIP), const_iv (GL_LINE_LOOP), @@ -2206,8 +2549,15 @@ const_iv (GL_TRIANGLES), const_iv (GL_TRIANGLE_STRIP), const_iv (GL_TRIANGLE_FAN), + const_iv (GL_POLYGON), const_iv (GL_PERSPECTIVE_CORRECTION_HINT), + const_iv (GL_POINT_SMOOTH_HINT), + const_iv (GL_LINE_SMOOTH_HINT), + const_iv (GL_POLYGON_SMOOTH_HINT), + const_iv (GL_GENERATE_MIPMAP_HINT), const_iv (GL_FASTEST), + const_iv (GL_DONT_CARE), + const_iv (GL_NICEST), const_iv (GL_V2F), const_iv (GL_V3F), const_iv (GL_T2F_V3F), @@ -2243,6 +2593,20 @@ OUTPUT: RETVAL +const char *glGetString (GLenum pname) + +GLint glGetInteger (GLenum pname) + CODE: + glGetIntegerv (pname, &RETVAL); + OUTPUT: + RETVAL + +GLdouble glGetDouble (GLenum pname) + CODE: + glGetDoublev (pname, &RETVAL); + OUTPUT: + RETVAL + int glGetError () void glFinish () @@ -2290,6 +2654,8 @@ # near_ and far_ are due to microsofts buggy "c" compiler void glOrtho (double left, double right, double bottom, double top, double near_, double far_) +PROTOTYPES: DISABLE + void glViewport (int x, int y, int width, int height) void glScissor (int x, int y, int width, int height) @@ -2306,12 +2672,7 @@ CODE: glRotatef (angle, x, y, z); -void glBegin (int mode) - -void glEnd () - void glColor (float r, float g, float b, float a = 1.0) - PROTOTYPE: @ ALIAS: glColor_premultiply = 1 CODE: @@ -2324,12 +2685,6 @@ // microsoft visual "c" rounds instead of truncating... glColor4f (r, g, b, a); -void glInterleavedArrays (int format, int stride, char *data) - -void glDrawElements (int mode, int count, int type, char *indices) - -# 1.2 void glDrawRangeElements (int mode, int start, int end - void glRasterPos (float x, float y, float z = 0.) CODE: glRasterPos3f (0, 0, z); @@ -2343,6 +2698,26 @@ CODE: glTexCoord2f (s, t); +void glRect (float x1, float y1, float x2, float y2) + CODE: + glRectf (x1, y1, x2, y2); + +PROTOTYPES: ENABLE + +void glBegin (int mode) + +void glEnd () + +void glPointSize (GLfloat size) + +void glLineWidth (GLfloat width) + +void glInterleavedArrays (int format, int stride, char *data) + +void glDrawElements (int mode, int count, int type, char *indices) + +# 1.2 void glDrawRangeElements (int mode, int start, int end + void glTexEnv (int target, int pname, float param) CODE: glTexEnvf (target, pname, param); @@ -2374,32 +2749,20 @@ void glDrawPixels (int width, int height, int format, int type, char *pixels) +void glPixelZoom (float x, float y) + void glCopyPixels (int x, int y, int width, int height, int type = GL_COLOR) int glGenTexture () CODE: -{ - GLuint name; - if (AvFILL (texture_av) >= 0) - name = (GLuint)(size_t)av_pop (texture_av); - else - glGenTextures (1, &name); - RETVAL = name; -} + RETVAL = gen_texture (); OUTPUT: RETVAL void glDeleteTexture (int name) CODE: -{ - /* make a half-assed attempt at returning the memory used by the texture */ - /* textures are frequently being reused by cfplus anyway */ - glBindTexture (GL_TEXTURE_2D, name); - glTexImage2D (GL_TEXTURE_2D, 0, GL_ALPHA, 0, 0, 0, GL_ALPHA, GL_UNSIGNED_BYTE, 0); - av_push (texture_av, (SV *)(size_t)name); - /*glDeleteTextures (1, &name_);*/ -} - + del_texture (name); + int glGenList () CODE: RETVAL = glGenLists (1); @@ -2416,3 +2779,114 @@ void glCallList (int list) +MODULE = CFPlus PACKAGE = CFPlus::UI::Base + +PROTOTYPES: DISABLE + +void +find_widget (SV *self, NV x, NV y) + PPCODE: +{ + if (within_widget (self, x, y)) + XPUSHs (self); +} + +BOOT: +{ + hover_gv = gv_fetchpv ("CFPlus::UI::HOVER", 1, SVt_NV); + + draw_x_gv = gv_fetchpv ("CFPlus::UI::Base::draw_x", 1, SVt_NV); + draw_y_gv = gv_fetchpv ("CFPlus::UI::Base::draw_y", 1, SVt_NV); + draw_w_gv = gv_fetchpv ("CFPlus::UI::Base::draw_w", 1, SVt_NV); + draw_h_gv = gv_fetchpv ("CFPlus::UI::Base::draw_h", 1, SVt_NV); +} + +void +draw (SV *self) + CODE: +{ + HV *hv; + SV **svp; + NV x, y, w, h; + SV *draw_x_sv = GvSV (draw_x_gv); + SV *draw_y_sv = GvSV (draw_y_gv); + SV *draw_w_sv = GvSV (draw_w_gv); + SV *draw_h_sv = GvSV (draw_h_gv); + SV *hover; + double draw_x, draw_y, draw_w, draw_h; + + if (!SvROK (self)) + croak ("CFPlus::Base::draw: %s not a reference", SvPV_nolen (self)); + + hv = (HV *)SvRV (self); + + if (SvTYPE (hv) != SVt_PVHV) + croak ("CFPlus::Base::draw: %s not a hashref", SvPV_nolen (self)); + + svp = hv_fetch (hv, "w", 1, 0); w = svp ? SvNV (*svp) : 0.; + svp = hv_fetch (hv, "h", 1, 0); h = svp ? SvNV (*svp) : 0.; + + if (!h || !w) + XSRETURN_EMPTY; + + svp = hv_fetch (hv, "x", 1, 0); x = svp ? SvNV (*svp) : 0.; + svp = hv_fetch (hv, "y", 1, 0); y = svp ? SvNV (*svp) : 0.; + + draw_x = SvNV (draw_x_sv) + x; + draw_y = SvNV (draw_y_sv) + y; + + if (draw_x + w < 0 || draw_x >= SvNV (draw_w_sv) + || draw_y + h < 0 || draw_y >= SvNV (draw_h_sv)) + XSRETURN_EMPTY; + + sv_setnv (draw_x_sv, draw_x); + sv_setnv (draw_y_sv, draw_y); + + glPushMatrix (); + glTranslated (x, y, 0); + + if (SvROK (GvSV (hover_gv)) && SvRV (GvSV (hover_gv)) == (SV *)hv) + { + svp = hv_fetch (hv, "can_hover", sizeof ("can_hover") - 1, 0); + + if (svp && SvTRUE (*svp)) + { + glColor4f (1*0.2f, 0.8*0.2f, 0.5*0.2f, 0.2f); + glEnable (GL_BLEND); + glBlendFunc (GL_ONE, GL_ONE_MINUS_SRC_ALPHA); + glBegin (GL_QUADS); + glVertex2f (0, 0); + glVertex2f (w, 0); + glVertex2f (w, h); + glVertex2f (0, h); + glEnd (); + glDisable (GL_BLEND); + } + } +#if 0 + if ($ENV{CFPLUS_DEBUG} & 1) { + glPushMatrix; + glColor 1, 1, 0, 1; + glTranslate 0.375, 0.375; + glBegin GL_LINE_LOOP; + glVertex 0 , 0; + glVertex $self->{w} - 1, 0; + glVertex $self->{w} - 1, $self->{h} - 1; + glVertex 0 , $self->{h} - 1; + glEnd; + glPopMatrix; + #CFPlus::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; + } +#endif + PUSHMARK (SP); + XPUSHs (self); + PUTBACK; + call_method ("_draw", G_VOID | G_DISCARD); + SPAGAIN; + + glPopMatrix (); + + draw_x = draw_x - x; sv_setnv (draw_x_sv, draw_x); + draw_y = draw_y - y; sv_setnv (draw_y_sv, draw_y); +} +