--- deliantra/Deliantra-Client/Client.xs 2007/07/19 23:29:16 1.206 +++ deliantra/Deliantra-Client/Client.xs 2007/08/11 06:34:26 1.222 @@ -19,6 +19,11 @@ #ifdef _WIN32 # undef pipe +// microsoft vs. C +# define sqrtf(x) sqrt(x) +# define roundf(x) (int)(x) +# define atan2f(x,y) atan2(x,y) +# define M_PI 3.14159265f #endif #include @@ -27,6 +32,8 @@ #include #include +#define USE_RWOPS 1 // for SDL_mixer:LoadMUS_RW + #include #include #include @@ -114,6 +121,8 @@ #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; @@ -435,6 +444,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 @@ -759,7 +811,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); @@ -770,6 +822,9 @@ int Mix_AllocateChannels (int numchans = -1) +const char * +Mix_GetError () + void lowdelay (int fd, int val = 1) CODE: @@ -820,7 +875,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) @@ -1302,6 +1357,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)); @@ -1499,14 +1557,16 @@ } } -void +SV * map1a_update (CFPlus::Map self, SV *data_, int extmap) CODE: { uint8_t *data = (uint8_t *)SvPVbyte_nolen (data_); uint8_t *data_end = (uint8_t *)SvEND (data_); mapcell *cell; - int x, y, flags; + int x, y, z, flags; + AV *missing = newAV (); + RETVAL = newRV_noinc ((SV *)missing); while (data < data_end - 1) { @@ -1567,31 +1627,34 @@ cell->darkness = *data++ + 1; } - if (flags & 4) - { - faceid face = (data [0] << 8) + data [1]; data += 2; - need_facenum (self, face); - cell->tile [0] = self->face2tile [face]; - } + for (z = 0; z <= 2; ++z) + if (flags & (4 >> z)) + { + faceid face = (data [0] << 8) + data [1]; data += 2; + need_facenum (self, face); + cell->tile [z] = self->face2tile [face]; - if (flags & 2) - { - faceid face = (data [0] << 8) + data [1]; data += 2; - need_facenum (self, face); - cell->tile [1] = self->face2tile [face]; - } + if (cell->tile [z]) + { + maptex *tex = self->tex + cell->tile [z]; + if (!tex->name) + av_push (missing, newSViv (cell->tile [z])); - if (flags & 1) - { - faceid face = (data [0] << 8) + data [1]; data += 2; - need_facenum (self, face); - cell->tile [2] = self->face2tile [face]; - } + if (tex->smoothtile) + { + maptex *smooth = self->tex + tex->smoothtile; + if (!smooth->name) + av_push (missing, newSViv (tex->smoothtile)); + } + } + } } else cell->darkness = 0; } } + OUTPUT: + RETVAL SV * mapmap (CFPlus::Map self, int x0, int y0, int w, int h) @@ -1675,7 +1738,7 @@ glBegin (GL_QUADS); - last_name = 0; + last_name = -1; mx += self->x; my += self->y; @@ -1720,8 +1783,7 @@ 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 @@ -1735,6 +1797,9 @@ 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); @@ -2191,14 +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 @@ -2210,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 @@ -2227,21 +2416,26 @@ volume (int volume = -1) PROTOTYPE: ;$ CODE: + if (items > 0) + volume = CLAMP (volume, 0, 128); RETVAL = Mix_VolumeMusic (volume); OUTPUT: RETVAL -int +void fade_out (int ms) CODE: - RETVAL = Mix_FadeOutMusic (ms); - OUTPUT: - RETVAL + 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 @@ -2257,12 +2451,10 @@ OUTPUT: RETVAL -int +void fade_in_pos (CFPlus::MixMusic self, int loops, int ms, double position) CODE: - RETVAL = Mix_FadeInMusicPos (self, loops, ms, position); - OUTPUT: - RETVAL + Mix_FadeInMusicPos (self, loops, ms, position); MODULE = CFPlus PACKAGE = CFPlus::OpenGL @@ -2348,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), @@ -2356,6 +2549,7 @@ 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), @@ -2460,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) @@ -2476,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: @@ -2494,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); @@ -2513,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); @@ -2574,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); +} +