diff --git a/alienblaster/project/jni/sdl_blitpool/Android.mk b/alienblaster/project/jni/sdl_blitpool/Android.mk new file mode 100644 index 000000000..1e2e20e18 --- /dev/null +++ b/alienblaster/project/jni/sdl_blitpool/Android.mk @@ -0,0 +1,18 @@ +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) + +LOCAL_MODULE := sdl_blitpool + +LOCAL_CFLAGS := -I$(LOCAL_PATH) -I$(LOCAL_PATH)/../sdl/include + +LOCAL_CPP_EXTENSION := .cpp + +LOCAL_SRC_FILES := SDL_blitpool.c + +LOCAL_SHARED_LIBRARIES := sdl +LOCAL_STATIC_LIBRARIES := +LOCAL_LDLIBS := + +include $(BUILD_SHARED_LIBRARY) + diff --git a/alienblaster/project/jni/sdl_blitpool/SDL_blitpool.c b/alienblaster/project/jni/sdl_blitpool/SDL_blitpool.c new file mode 100644 index 000000000..fe54539fb --- /dev/null +++ b/alienblaster/project/jni/sdl_blitpool/SDL_blitpool.c @@ -0,0 +1,1850 @@ +/* +// +// Blit operation pool for the SDL. +// Copyright (C) 2005 Strangebug (S.Miura) [strangebug art hotmail.co.jp] +// +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// +// SDL_BlitPool.c : 25/Feb/2005 : created +// +*/ + + + + +#include /* malloc/free */ +#include + +#include "SDL.h" +#include "SDL_BlitPool.h" + + + + +/* */ + + + +#ifndef NO_ALLOC_FAIL /* allocator will not fail */ +# define IF_ALLOC_FAIL(statement) /* empty */ +#else +# define IF_ALLOC_FAIL(statement) statement +#endif + + +/* view divided surface (for debug). */ +#ifdef PAINT_DIVIDED +# define IF_PAINT_DIVIDED(statement) statement +#else +# define IF_PAINT_DIVIDED(statement) /* empty */ +#endif + +/* view separated surface (for debug). */ +#ifdef PAINT_SEPARATED +# define IF_PAINT_SEPARATED(statement) statement +#else +# define IF_PAINT_SEPARATED(statement) /* empty */ +#endif + + +#ifndef IF_DEBUG +# ifdef NDEBUG +# define IF_DEBUG(statement) /* empty */ +# else +# define IF_DEBUG(statement) statement +# endif +#endif + + +/* */ + + + + +#if IF_DEBUG(1) + 0 +# include +#endif + + + + +typedef struct BlitEntry_tag { + + SDL_Surface *srcsurf; /* BlitSurface */ + Uint32 color; /* FillColor */ + BlitEntryFlag type; /* Surface/Fill: BlitEntryFlag */ + BlitEntryFlag trans; /* Transparency: BlitEntryFlag */ + + SDL_Rect srcrect; + BlitPool_BoundingBox destbox; /* Destination position bouding box (not rect) */ + + SDL_Rect destrect; + struct BlitEntry_tag *next, *prev; /* Double link list */ + + struct { + int optimize; + } flag; + +} BlitEntry; + + + +struct BlitPool_tag { /* typedefed BlitPool in SDL_BlitPool.h */ + + SDL_Surface *destsurf; + BlitEntry *head, *tail; /* Double link list */ + + int num_entry; + + allocator_func allocator; + releaser_func releaser; + + struct { + int converted_to_destrect; + } flag; +}; + + + + + +static void *DefaultAllocator(unsigned long nbyte); +static void DefaultReleaser(void *p); +static BlitEntry *AllocateBlitEntry(BlitPool *pool); +static void ReleaseBlitEntry(BlitPool *pool, BlitEntry *entry); +static BlitEntry *DuplicateBlitEntry(BlitPool *pool, BlitEntry *base); + + +static void ConvertBBoxToRect(BlitPool *pool); +static int RemoveOverlapArea(BlitPool *pool); +static int RemoveOutsideEntry(BlitPool *pool); + + + +static void AddEntryToTail(BlitPool *pool, BlitEntry *entry); + +static void RemoveEntry(BlitPool *pool, BlitEntry *entry); +static void DeleteEntry(BlitPool *pool, BlitEntry *entry); + +static int StrEq(unsigned char *str0, unsigned char *str1, int n); +static void AddEntryToNext(BlitPool *pool, BlitEntry *entry, BlitEntry *new_entry); + +/* unuse +static void AddEntryToPrev(BlitPool *pool, BlitEntry *entry, BlitEntry *new_entry); +static void AddEntryToHead(BlitPool *pool, BlitEntry *entry); +*/ + + + +BlitPool *BlitPool_CreatePool(Optional SDL_Surface *destsurf) +/* +// This allocate a new pool. +*/ +{ + BlitPool *p; + + + p = (BlitPool *)malloc(sizeof(*p)); + if (p == NULL) { + return NULL; + } + + p->head = NULL; + p->tail = NULL; + p->num_entry = 0; + p->destsurf = destsurf; + p->flag.converted_to_destrect = 0; + p->allocator = DefaultAllocator; + p->releaser = DefaultReleaser; + + return p; +} + + + + +static void *DefaultAllocator(unsigned long nbyte) +{ + return malloc(nbyte); +} + + + + +static void DefaultReleaser(void *p) +{ + free(p); +} + + + + +void BlitPool_DeletePool(BlitPool *pool) +{ + assert(pool != NULL); + + BlitPool_ReleaseEntry(pool); + + free(pool); +} + + + + +void BlitPool_SetAllocator(BlitPool *pool, allocator_func allocator, releaser_func releaser) +{ + assert(pool != NULL); + assert(allocator != NULL); + assert(releaser != NULL); + + assert(pool->head == NULL); + assert(pool->num_entry == 0); + + pool->allocator = allocator; + pool->releaser = releaser; +} + + + + +void BlitPool_ReleaseEntry(BlitPool *pool) +{ + BlitEntry *p, *next; + + + assert(pool != NULL); + + + for (p = pool->head; p != NULL; p = next) { + + next = p->next; + + ReleaseBlitEntry(pool, p); + } + + pool->head = NULL; + pool->tail = NULL; + pool->num_entry = 0; + pool->flag.converted_to_destrect = 0; +} + + + + +int BlitPool_Post( + BlitPool *pool, + BlitEntryFlag flag, + SDL_Surface *src, + Optional SDL_Rect *srcrect, + Optional SDL_Rect *destrect, + Uint32 color +) +/* +// This post the new operation to a pool. +*/ +{ + BlitEntry *p; + + + assert(pool != NULL); + + p = AllocateBlitEntry(pool); + if (p == NULL) { + return 1; + } + + assert(flag < BLIT_FLAG_TERM); + + p->type = flag & BLIT_TYPE_MASK; + p->srcsurf = src; + p->color = color; + p->trans = flag & BLIT_ALPHA_MASK; + p->flag.optimize = flag & BLIT_EXEC_MASK; + if (p->flag.optimize == 0) { + p->flag.optimize = BLIT_EXEC_OPTIMIZE; + } + + + assert(p->type == BLIT_TYPE_SURFACE || p->type == BLIT_TYPE_COLORFILL || p->type == BLIT_TYPE_EMPTY); + assert(p->trans == BLIT_ALPHA_TRANSPARENT || p->trans == BLIT_ALPHA_OPAQUE); + assert(p->flag.optimize == BLIT_EXEC_OPTIMIZE || p->flag.optimize == BLIT_EXEC_NO_OPTIMIZE); + + + if (p->type == BLIT_TYPE_SURFACE) { + if (srcrect == NULL) { + p->srcrect.w = src->w; + p->srcrect.h = src->h; + p->srcrect.x = p->srcrect.y = 0; + + } else { + p->srcrect = *srcrect; + } + } else { + assert(src == NULL); + assert(srcrect == NULL); + + if (destrect == NULL) { + p->srcrect.w = pool->destsurf->w; + p->srcrect.h = pool->destsurf->h; + } else { + p->srcrect.w = destrect->w; + p->srcrect.h = destrect->h; + } + } + + if (p->srcrect.w == 0 || p->srcrect.h == 0) { + ReleaseBlitEntry(pool, p); + return 1; + } + + + /* + // Not use destrect in BlitPool. + // It will be update rect after. + */ + if (destrect == NULL) { + p->destbox.x0 = p->destbox.y0 = 0; + + } else { + p->destbox.x0 = destrect->x; + p->destbox.y0 = destrect->y; + + destrect->w = p->srcrect.w; + destrect->h = p->srcrect.h; + } + + p->destbox.x1 = p->destbox.x0 + p->srcrect.w; + p->destbox.y1 = p->destbox.y0 + p->srcrect.h; + + AddEntryToTail(pool, p); + pool->flag.converted_to_destrect = 0; + + return 0; +} + + + + + +int BlitPool_PostByDescription( + BlitPool *pool, + Optional SDL_Surface *surf, + unsigned char *area_description_str +) +/* +// area_description_str like: "pos=(45, 12):O:(0,0)-(12,12):(45,23)-(62, 24):pos=(0,0):T:(12,98)-(1,65)" +// pos=(x,y) : destination position. +// (x0,y0)-(x1,y1) : source surface rectangle. +// O/T : T=Transparens(default), O=Opaque. +// col=(r, g, b, a) : colorfill mode. it's format be surf->format. +// +// * Don't forget delimiter ':'. +*/ +{ + unsigned char *p; + SDL_Rect dest_rect, src_rect, blit_dest_rect; + BlitEntryFlag blit_type; + int flag_fillmode; + int post_result; + Uint32 color; + + + #define SkipSpace(p) while (*p == ' ' | *p == '\t' | *p == '\n') { p += 1; } + #define SkipDigit(p) while (*p == '0' | *p == '1' | *p == '2' | *p == '3' | *p == '4' | \ + *p == '5' | *p == '6' | *p == '7' | *p == '8' | *p == '9') { p += 1; } + #define CheckAndSkip_OrDead(ch) if (*p != ch) { return 1; } else { p += 1; SkipSpace(p); } + + + assert(pool != NULL); + assert(surf != NULL); + assert(area_description_str != NULL); + + p = area_description_str; + dest_rect.x = dest_rect.y = 0; + blit_type = BLIT_ALPHA_TRANSPARENT; + flag_fillmode = 0; + color = 0; + + while (*p != '\0') { + + if (*p == ':') { + p += 1; + } + + SkipSpace(p); + if (StrEq(p, "pos", 3) == 0) { + p += 3; + assert(*(p-1) == 's'); + SkipSpace(p); + + CheckAndSkip_OrDead('='); + CheckAndSkip_OrDead('('); + dest_rect.x = atoi(p); + SkipDigit(p); + SkipSpace(p); + CheckAndSkip_OrDead(','); + + dest_rect.y = atoi(p); + SkipDigit(p); + SkipSpace(p); + CheckAndSkip_OrDead(')'); + continue; + } + + if (StrEq(p, "col", 3) == 0) { + Uint8 r, g, b, a; + + p += 3; + assert(*(p-1) == 'l'); + SkipSpace(p); + + #define GetCol(elem) \ + elem = atoi(p);\ + SkipDigit(p);\ + SkipSpace(p); + + CheckAndSkip_OrDead('='); + CheckAndSkip_OrDead('('); + GetCol(r); + CheckAndSkip_OrDead(','); + GetCol(g); + CheckAndSkip_OrDead(','); + GetCol(b); + CheckAndSkip_OrDead(','); + GetCol(a); + CheckAndSkip_OrDead(')'); + + color = SDL_MapRGBA(surf->format, r, g, b, a); + + #undef GetCol + + flag_fillmode = 1; + blit_type = BLIT_ALPHA_OPAQUE; + + continue; + } + + if (flag_fillmode == 0 && (*p == 'T' || *p == 'O')) { + if (*p == 'T') { + blit_type = BLIT_ALPHA_TRANSPARENT; + } else if (*p == 'O') { + blit_type = BLIT_ALPHA_OPAQUE; + } else { + assert(0); + } + p += 1; + SkipSpace(p); + continue; + } + + CheckAndSkip_OrDead('('); + src_rect.x = atoi(p); + SkipDigit(p); + SkipSpace(p); + + CheckAndSkip_OrDead(','); + src_rect.y = atoi(p); + SkipDigit(p); + SkipSpace(p); + + CheckAndSkip_OrDead(')'); + CheckAndSkip_OrDead('-'); + CheckAndSkip_OrDead('('); + src_rect.w = atoi(p); + SkipDigit(p); + SkipSpace(p); + + CheckAndSkip_OrDead(','); + src_rect.h = atoi(p); + SkipDigit(p); + SkipSpace(p); + CheckAndSkip_OrDead(')'); + + assert((Sint32)src_rect.x < (Sint32)src_rect.w); + assert((Sint32)src_rect.y < (Sint32)src_rect.h); + + + src_rect.w -= src_rect.x; + src_rect.h -= src_rect.y; + blit_dest_rect.x = src_rect.x + dest_rect.x; + blit_dest_rect.y = src_rect.y + dest_rect.y; + + if (flag_fillmode) { + blit_dest_rect.w = src_rect.w; + blit_dest_rect.h = src_rect.h; + post_result = BlitPool_PostFill(pool, &blit_dest_rect, color, blit_type); + } else { + post_result = BlitPool_PostSurface(pool, surf, &src_rect, &blit_dest_rect, blit_type); + } + + if (post_result) { + return 1; + } + } + + + #undef SkipSpace + #undef SkipDigit + #undef CheckAndSkip_OrDead + + return 0; +} + + + + +void BlitPool_PostPool( + BlitPool *dest_pool, + BlitPool *src_pool, + Optional SDL_Rect *offset +) +/* +// Commit(add) the entries. +*/ +{ + BlitEntry *new_entry, *src_entry; + SDL_Rect zerorect; + + assert(dest_pool != NULL); + assert(src_pool != NULL); + + if (offset == NULL) { + zerorect.x = zerorect.y = 0; + offset = &zerorect; + } + + for (src_entry = src_pool->head; src_entry != NULL; src_entry = src_entry->next) { + + new_entry = DuplicateBlitEntry(dest_pool, src_entry); + + IF_ALLOC_FAIL( if (new_entry == NULL) return ); + + new_entry->destbox.x0 += offset->x; + new_entry->destbox.y0 += offset->y; + new_entry->destbox.x1 += offset->x; + new_entry->destbox.y1 += offset->y; + + AddEntryToTail(dest_pool, new_entry); + dest_pool->flag.converted_to_destrect = 0; + } +} + + + + +void BlitPool_Execute(BlitPool *pool) +/* +// This execute the blit_surface and the color_fill. +*/ +{ + BlitEntry *p; + + + assert(pool != NULL); + assert(pool->destsurf != NULL); + + if (pool->num_entry == 0) { + return; + } + + + ConvertBBoxToRect(pool); + + + for (p = pool->head; p != NULL; p = p->next) { + + /* + // if (type == BLIT_TYPE_SURFACE) { + // p->destrect: destination position. + // p->srcrect: source position and rectangle size. + // } else { + // p->destrect: destination position and rectangle size. + // } + */ + + switch (p->type) { + + case BLIT_TYPE_SURFACE: + SDL_BlitSurface( + p->srcsurf, &p->srcrect, + pool->destsurf, &p->destrect + ); + break; + + case BLIT_TYPE_COLORFILL: + SDL_FillRect(pool->destsurf, &p->destrect, p->color); + break; + + case BLIT_TYPE_EMPTY: + /* + // Empty operation. + */ + break; + + IF_DEBUG(default: assert(0)); + } + } +} + + + + +void BlitPool_Optimize(BlitPool *pool, BlitOptimizeFlag flag) +/* +// Apply optimizations. +*/ +{ + + if (flag & BLIT_OPT_REMOVEOUTSIDE) { + pool->flag.converted_to_destrect = 0; + RemoveOutsideEntry(pool); + } + + if (flag & BLIT_OPT_REMOVEOVERLAP) { + pool->flag.converted_to_destrect = 0; + if (RemoveOverlapArea(pool)) { + /* allocation failed */; + } + } +} + + + + +static void ConvertBBoxToRect(BlitPool *pool) +{ + BlitEntry *p; + + + assert(pool != NULL); + + if (pool->flag.converted_to_destrect) { + return; + } + + for (p = pool->head; p != NULL; p = p->next) { + + BlitPoolUtil_BBoxToRect(&p->destbox, &p->destrect); + + if (p->type == BLIT_TYPE_SURFACE) { + p->srcrect.w = p->destbox.x1 - p->destrect.x; + p->srcrect.h = p->destbox.y1 - p->destrect.y; + } + } + + pool->flag.converted_to_destrect = 1; +} + + + + +int BlitPool_GetRectCount(BlitPool *pool) +{ + return pool->num_entry; +} + + + + +int BlitPool_GetUpdateRects(BlitPool *pool, SDL_Rect *rectbuf, int size, void **update_rects_obj) +/* +// This gets the rects of update, to the rectbuf. +// for SDL_UpdateRects. +// return number is count of the updated rects, < size +// example: +// +// BlitPool_Execute(pool); +// { +// int numrect; +// SDL_Rect rectbuf[10]; +// void *p; +// +// p = BlitPool_GetUpdateRectsObj(pool); +// while ((numrect = BlitPool_GetUpdateRects(pool, rectbuf, sizeof(rectbuf)/sizeof(rectbuf[0]), &p)) > 0) { +// SDL_UpdateRects(screen, numrect, rectbuf); +// } +// } +*/ +{ + int n; + BlitEntry *p; + + + assert(pool != NULL); + assert(rectbuf != NULL); + assert(size > 0); + + if (*update_rects_obj == NULL) { + return 0; + } + + if (pool->flag.converted_to_destrect) { + ; + } else { + ConvertBBoxToRect(pool); + } + + n = 0; + p = (BlitEntry *) *update_rects_obj; + + while (p != NULL && n < size) { + rectbuf[n++] = p->destrect; + p = p->next; + } + + *update_rects_obj = (p == NULL) ? NULL : p->prev; + + return n; +} + + + + +void *BlitPool_GetUpdateRectsObj(BlitPool *pool) +{ + assert(pool != NULL); + + return (void *)pool->head; +} + + + + +Uint32 BlitPool_GetArea(BlitPool *pool) +{ + BlitEntry *p; + Uint32 area; + + + assert(pool != NULL); + + ConvertBBoxToRect(pool); + + area = 0; + for (p = pool->head; p != NULL; p = p->next) { + + if (p->type == BLIT_TYPE_SURFACE) { + area += p->srcrect.w * p->srcrect.h; + + } else if (p->type == BLIT_TYPE_COLORFILL) { + area += p->destrect.w * p->destrect.h; + + } else { + assert(0); + } + } + + return area; +} + + + + +static int RemoveOverlapArea(BlitPool *pool) +/* +// This apply remove the overlapped rect and divide the back surface. +// If return not 0 then fail operation. +*/ +{ + Uint8 code; + SDL_Rect srcrect; + BlitPool_BoundingBox destbox; + BlitEntry *new_entry; + BlitEntry *front, *back, *back_next; + int is_overlapped; + int divided; + + + assert(pool != NULL); + + + for (back = pool->head; back != NULL; back = back_next) { + + back_next = back->next; + + /* skip no optimization entry */ + if (back->flag.optimize == BLIT_EXEC_NO_OPTIMIZE) { + continue; + } + + for (front = back->next; front != NULL; front = front->next) { + + assert(back != front); + + if (front->flag.optimize == BLIT_EXEC_NO_OPTIMIZE | /* skip no optimization entry */ + front->trans == BLIT_ALPHA_TRANSPARENT /* skip transparent surface */ + ) { + continue; + } + + assert((back->destbox.x1 - back->destbox.x0) > 0); + assert((back->destbox.y1 - back->destbox.y0) > 0); + assert((front->destbox.x1 - front->destbox.x0) > 0); + assert((front->destbox.y1 - front->destbox.y0) > 0); + + is_overlapped = BlitPoolUtil_GetOverlapCode( + &code, + &front->destbox, + &back->destbox + ); + + if (is_overlapped == 0) { + continue; + } + + assert(code <= 0x0f); + + + /* + // Divide the back rects. + */ + + divided = 0; + + + #define AddNewEntryBegin() \ + srcrect.x = back->srcrect.x; \ + srcrect.y = back->srcrect.y; \ + destbox = back->destbox; + + #define AddNewEntryEnd() \ + assert(destbox.x1 >= destbox.x0); \ + assert(destbox.y1 >= destbox.y0); \ + if ((destbox.x1 - destbox.x0) <= 0 | \ + (destbox.y1 - destbox.y0) <= 0 \ + ) { \ + ; \ + } else { \ + new_entry = DuplicateBlitEntry(pool, back); \ + IF_ALLOC_FAIL(if (new_entry == NULL) return 1);\ + AddEntryToNext(pool, back, new_entry); \ + new_entry->srcrect.x = srcrect.x; \ + new_entry->srcrect.y = srcrect.y; \ + new_entry->destbox = destbox; \ + IF_PAINT_DIVIDED(new_entry->type = BLIT_TYPE_COLORFILL);\ + IF_PAINT_DIVIDED(new_entry->color = (Uint32)new_entry*80);\ + assert(back->next == new_entry); \ + back_next = new_entry; \ + divided += 1; \ + } + + switch (code) { + case 0: + /* + // 00 00 + // + // *----* + // | o----o + // *--|ffff| + // o----o + */ + + + /* + // Perfect overlapped? + // (back->destbox.x1 - back->destbox.x0) - (back->destbox.x1 - front->destbox.x0) == 0 + // back width - overlapped rect width == 0 + // + // = back->destbox.x1 - back->destbox.x0 - back->destbox.x1 + front->destbox.x0 + // = front->destbox.x0 - back->destbox.x0 + */ + if ((front->destbox.x0 - back->destbox.x0) | /* == || */ + (front->destbox.y0 - back->destbox.y0) + ) { + ; + } else { + break; + } + + /* Top - Left */ + AddNewEntryBegin(); + destbox.y1 = front->destbox.y0; + AddNewEntryEnd(); + + /* Left */ + AddNewEntryBegin(); + srcrect.y += front->destbox.y0 - back->destbox.y0; + destbox.y0 = front->destbox.y0; + destbox.x1 = front->destbox.x0; + AddNewEntryEnd(); + break; + + case 1: + /* + // 00 01 + // + // *----------* + // | o----o | + // *--|ffff|--* + // o----o + */ + + /* WholeTop */ + AddNewEntryBegin(); + destbox.y1 = front->destbox.y0; + AddNewEntryEnd(); + + /* Left */ + AddNewEntryBegin(); + srcrect.y += front->destbox.y0 - back->destbox.y0; + destbox.y0 = front->destbox.y0; + destbox.x1 = front->destbox.x0; + AddNewEntryEnd(); + + /* Right */ + AddNewEntryBegin(); + srcrect.x += front->destbox.x1 - back->destbox.x0; + srcrect.y += front->destbox.y0 - back->destbox.y0; + destbox.x0 = front->destbox.x1; + destbox.y0 = front->destbox.y0; + AddNewEntryEnd(); + break; + + case 2: + /* + // 00 10 + // + // *----* + // | | + // o----------o + // | ffff | + // o----------o + */ + + /* Top */ + AddNewEntryBegin(); + destbox.y1 = front->destbox.y0; + AddNewEntryEnd(); + break; + + case 3: + /* + // 00 11 + // + // *-----* + // | | + // o----o | + // |ffff|---* + // o----o + */ + + /* Top - Right */ + AddNewEntryBegin(); + destbox.y1 = front->destbox.y0; + AddNewEntryEnd(); + + /* Right */ + AddNewEntryBegin(); + srcrect.x += front->destbox.x1 - back->destbox.x0; + srcrect.y += front->destbox.y0 - back->destbox.y0; + destbox.x0 = front->destbox.x1; + destbox.y0 = front->destbox.y0; + AddNewEntryEnd(); + break; + + case 4: + /* + // 00 11 + // + // *----* + // | o---o + // | |fff| + // | o---o + // *----* + */ + + + /* Top - Left */ + AddNewEntryBegin(); + destbox.y1 = front->destbox.y0; + AddNewEntryEnd(); + + /* Bottom - Left */ + AddNewEntryBegin(); + srcrect.y += front->destbox.y1 - back->destbox.y0; + destbox.y0 = front->destbox.y1; + AddNewEntryEnd(); + + + /* Left */ + AddNewEntryBegin(); + srcrect.y += front->destbox.y0 - back->destbox.y0; + destbox.y0 = front->destbox.y0; + destbox.x1 = front->destbox.x0; + destbox.y1 = front->destbox.y1; + AddNewEntryEnd(); + break; + + case 5: + /* + // 01 01 + // + // *-------* + // | o--o | + // | |ff| | + // | o--o | + // *-------* + */ + + /* WholeTop */ + AddNewEntryBegin(); + destbox.y1 = front->destbox.y0; + AddNewEntryEnd(); + + /* WholeBottom */ + AddNewEntryBegin(); + srcrect.y += front->destbox.y1 - back->destbox.y0; + destbox.y0 = front->destbox.y1; + AddNewEntryEnd(); + + /* Left */ + AddNewEntryBegin(); + srcrect.y += front->destbox.y0 - back->destbox.y0; + destbox.y0 = front->destbox.y0; + destbox.x1 = front->destbox.x0; + destbox.y1 = front->destbox.y1; + AddNewEntryEnd(); + + /* Right */ + AddNewEntryBegin(); + srcrect.x += front->destbox.x1 - back->destbox.x0; + srcrect.y += front->destbox.y0 - back->destbox.y0; + destbox.x0 = front->destbox.x1; + destbox.y0 = front->destbox.y0; + destbox.y1 = front->destbox.y1; + AddNewEntryEnd(); + break; + + case 6: + /* + // 01 10 + // + // *-* + // o-----o + // o-----o + // *-* + */ + + /* Top */ + AddNewEntryBegin(); + destbox.y1 = front->destbox.y0; + AddNewEntryEnd(); + + /* Bottom */ + AddNewEntryBegin(); + srcrect.y += front->destbox.y1 - back->destbox.y0; + destbox.y0 = front->destbox.y1; + AddNewEntryEnd(); + break; + + case 7: + /* + // 01 11 + // + // *---* + // o---o | + // |fff| | + // o---o | + // *---* + */ + + /* Top - Right */ + AddNewEntryBegin(); + destbox.y1 = front->destbox.y0; + AddNewEntryEnd(); + + /* Bottom - Right */ + AddNewEntryBegin(); + srcrect.y += front->destbox.y1 - back->destbox.y0; + destbox.y0 = front->destbox.y1; + AddNewEntryEnd(); + + /* Right */ + AddNewEntryBegin(); + srcrect.x += front->destbox.x1 - back->destbox.x0; + srcrect.y += front->destbox.y0 - back->destbox.y0; + destbox.x0 = front->destbox.x1; + destbox.y0 = front->destbox.y0; + destbox.y1 = front->destbox.y1; + AddNewEntryEnd(); + break; + + case 8: + /* + // 10 00 + // + // o---o + // *-| | + // | |fff| + // *-| | + // o---o + */ + + /* Left */ + AddNewEntryBegin(); + destbox.x1 = front->destbox.x0; + AddNewEntryEnd(); + break; + + case 9: /* 10 01 */ + /* + // 10 01 + // + // o---o + // *-| |-* + // | |fff| | + // *-| |-* + // o---o + */ + + /* Left */ + AddNewEntryBegin(); + destbox.x1 = front->destbox.x0; + AddNewEntryEnd(); + + /* Right */ + AddNewEntryBegin(); + srcrect.x += front->destbox.x1 - back->destbox.x0; + destbox.x0 = front->destbox.x1; + AddNewEntryEnd(); + break; + + case 10: /* 10 10 */ + /* + // 10 10 + // Perfect Overlap. + */ + break; + + case 11: /* 10 11 */ + /* + // 10 11 + // + // o---o + // | |-* + // |fff| | + // | |-* + // o---o + */ + + /* Right */ + AddNewEntryBegin(); + srcrect.x += front->destbox.x1 - back->destbox.x0; + destbox.x0 = front->destbox.x1; + AddNewEntryEnd(); + break; + + case 12: + /* + // 11 00 + // + // o-----o + // |ffff | + // *--| ffff| + // | o-----o + // *----* + */ + + /* Bottom - Left */ + AddNewEntryBegin(); + srcrect.y += front->destbox.y1 - back->destbox.y0; + destbox.y0 = front->destbox.y1; + AddNewEntryEnd(); + + /* Left */ + AddNewEntryBegin(); + destbox.x1 = front->destbox.x0; + destbox.y1 = front->destbox.y1; + AddNewEntryEnd(); + break; + + case 13: + /* + // 11 01 + // + // o-----o + // |ffff | + // *--| ffff|--* + // | o-----o | + // *-----------* + */ + + /* WholeBottom */ + AddNewEntryBegin(); + srcrect.y += front->destbox.y1 - back->destbox.y0; + destbox.y0 = front->destbox.y1; + AddNewEntryEnd(); + + /* Left */ + AddNewEntryBegin(); + destbox.x1 = front->destbox.x0; + destbox.y1 = front->destbox.y1; + AddNewEntryEnd(); + + /* Right */ + AddNewEntryBegin(); + srcrect.x += front->destbox.x1 - back->destbox.x0; + destbox.x0 = front->destbox.x1; + destbox.y1 = front->destbox.y1; + AddNewEntryEnd(); + break; + + case 14: + /* + // 11 10 + // + // o----------o + // | ffff | + // o----------o + // *----* + */ + + /* Bottom */ + AddNewEntryBegin(); + srcrect.y += front->destbox.y1 - back->destbox.y0; + destbox.y0 = front->destbox.y1; + AddNewEntryEnd(); + break; + + case 15: + /* + // 11 11 + // + // o----o + // |ffff|--* + // o----o | + // *----* + */ + + /* Bottom - Right */ + AddNewEntryBegin(); + srcrect.y += front->destbox.y1 - back->destbox.y0; + destbox.y0 = front->destbox.y1; + AddNewEntryEnd(); + + /* Right */ + AddNewEntryBegin(); + srcrect.x += front->destbox.x1 - back->destbox.x0; + destbox.x0 = front->destbox.x1; + destbox.y1 = front->destbox.y1; + AddNewEntryEnd(); + break; + + IF_DEBUG(default: assert(0 && "FATAL: in RemoveOverlapArea")); + } + + assert(back != front); + + DeleteEntry(pool, back); + + if (divided) { + back = back_next; + back_next = back->next; + + } else { + break; /* only deleted */ + } + } + } + + return 0; + + #undef AddNewEntryBegin + #undef AddNewEntryEnd +} + + + + +static int RemoveOutsideEntry(BlitPool *pool) +/* +// Remove outside of the destination surface. +// return removed entry count. +*/ +{ + BlitEntry *p, *next; + Uint8 code; + BlitPool_BoundingBox destsurfbox; + int is_overlapped; + int removed_count; + + + assert(pool != NULL); + + if (pool->destsurf == NULL) { + /* impossible! */ + return 0; + } + + if (pool->head == NULL || pool->num_entry == 0) { + return 0; + } + + destsurfbox.x0 = 0; + destsurfbox.y0 = 0; + destsurfbox.x1 = pool->destsurf->w; + destsurfbox.y1 = pool->destsurf->h; + + removed_count = 0; + + for (p = pool->head; p != NULL; p = next) { + + next = p->next; + + is_overlapped = BlitPoolUtil_GetOverlapCode( + &code, + &p->destbox, /* destination bouding box */ + &destsurfbox /* destination surface box */ + ); + + if (is_overlapped == 0) { + /* + // Outside of the destination surface. + */ + + /* + // future: clipping p->destbox. + */ + + removed_count += 1; + DeleteEntry(pool, p); + } + } + + return removed_count; +} + + + + +int BlitPoolUtil_GetOverlapCode( + Uint8 *out_code, /* Must not NULL */ + BlitPool_BoundingBox *f, /* front */ + BlitPool_BoundingBox *b /* back */ +) +/* +// This check the overlapped rect. +// +// if (overlapped(front_rect, back_rect)) { +// *out_code = code; +// return 1; +// } else { +// return 0; +// } +*/ +{ + Uint8 code; /* use low nibble */ + + enum { + Overlapped = 1, + NotOverlapped = 0 + }; + + + assert(out_code != NULL); + assert(f != NULL); + assert(b != NULL); + + + assert((f->x1 - f->x0) > 0); + assert((f->y1 - f->y0) > 0); + assert((b->x1 - b->x0) > 0); + assert((b->y1 - b->y0) > 0); + + + /* + // It's overlapped? + // If overlapped then make the overlap rect, + // by just switch jump. + */ + + + /* + // Get MSB of 32bit signed integer. + */ + #define GetMSB(val) ((Uint32)(val) & 0x80000000) + + + /* + // Y X + // code: ... b3 b2 b1 b0 bn == bit n + // b1,b0 = subtract back from front of X axis + // b3,b2 = of Y axis + */ + code = (GetMSB(f->x0 - b->x0) >> (2 + 28)) | /* b1 */ + (GetMSB(f->x1 - b->x1) >> (3 + 28)) | /* b0 */ + (GetMSB(f->y0 - b->y0) >> (0 + 28)) | /* b3 */ + (GetMSB(f->y1 - b->y1) >> (1 + 28)); /* b2 */ + + + assert(code <= 0x0f); + + + /* + // pattern: 00 + // f: *----* *----* + // b: *----* or *----* + */ + #define OutsideCheck00(axis) ((b->axis##1 - f->axis##0) <= 0) + + /* + // pattern: 01 + // f: *--* + // b: *-------* + */ + #define OutsideCheck01(axis) ( IF_DEBUG((f->axis##1 - f->axis##0) <= 0) + 0 ) + + /* + // pattern: 11 + // f: *----* *----* + // b: *----* or *----* + */ + #define OutsideCheck11(axis) ((f->axis##1 - b->axis##0) <= 0) + + /* + // pattern: 10 + // f: *-------* + // b: *---* + */ + #define OutsideCheck10(axis) ( IF_DEBUG((b->axis##1 - b->axis##0) <= 0) + 0 ) + + + + #define CaseOfCode_Overlap(codeval, codesymY, codesymX) \ + case codeval: \ + if (OutsideCheck##codesymX(x) | OutsideCheck##codesymY(y)) { /* == || */ \ + return NotOverlapped; \ + } \ + *out_code = code; \ + return Overlapped; + + + switch (code) { + CaseOfCode_Overlap(0, 00, 00); + CaseOfCode_Overlap(1, 00, 01); + CaseOfCode_Overlap(2, 00, 10); + CaseOfCode_Overlap(3, 00, 11); + + CaseOfCode_Overlap(4, 01, 00); + CaseOfCode_Overlap(5, 01, 01); + CaseOfCode_Overlap(6, 01, 10); + CaseOfCode_Overlap(7, 01, 11); + + CaseOfCode_Overlap(8, 10, 00); + CaseOfCode_Overlap(9, 10, 01); + CaseOfCode_Overlap(10, 10, 10); + CaseOfCode_Overlap(11, 10, 11); + + CaseOfCode_Overlap(12, 11, 00); + CaseOfCode_Overlap(13, 11, 01); + CaseOfCode_Overlap(14, 11, 10); + CaseOfCode_Overlap(15, 11, 11); + } + + + #undef CaseOfCode_Overlap + #undef OutsideCheck00 + #undef OutsideCheck01 + #undef OutsideCheck11 + #undef OutsideCheck10 + + /* Not comming */ + assert(0 && "BlitPoolUtil_GetOverlapCode: Illigal area!"); + + return Overlapped; +} + + + + +void BlitPoolUtil_CalcOverlapArea( + BlitPool_BoundingBox *out_overlap_box, + Uint8 code, + BlitPool_BoundingBox *f, + BlitPool_BoundingBox *b +) +/* +// This calculate the overlapped rect. +// bouding box f and b must be overlapped. +*/ +{ + assert(out_overlap_box != NULL); + assert(f != NULL); + assert(b != NULL); + assert(code <= 0x0f); + + + #define CalcOverlapBox00(axis) \ + out_overlap_box->axis##0 = f->axis##0; \ + out_overlap_box->axis##1 = b->axis##1 + + #define CalcOverlapBox01(axis) \ + out_overlap_box->axis##0 = f->axis##0; \ + out_overlap_box->axis##1 = f->axis##1 + + #define CalcOverlapBox11(axis) \ + out_overlap_box->axis##0 = b->axis##0; \ + out_overlap_box->axis##1 = f->axis##1 + + #define CalcOverlapBox10(axis) \ + out_overlap_box->axis##0 = b->axis##0; \ + out_overlap_box->axis##1 = b->axis##1 + + + + #define CaseOfCode_CalcOverlap(codeval, codesymY, codesymX) \ + case codeval: \ + CalcOverlapBox##codesymX(x);\ + CalcOverlapBox##codesymY(y);\ + break; + + + switch (code) { + CaseOfCode_CalcOverlap(0, 00, 00); + CaseOfCode_CalcOverlap(1, 00, 01); + CaseOfCode_CalcOverlap(2, 00, 10); + CaseOfCode_CalcOverlap(3, 00, 11); + + CaseOfCode_CalcOverlap(4, 01, 00); + CaseOfCode_CalcOverlap(5, 01, 01); + CaseOfCode_CalcOverlap(6, 01, 10); + CaseOfCode_CalcOverlap(7, 01, 11); + + CaseOfCode_CalcOverlap(8, 10, 00); + CaseOfCode_CalcOverlap(9, 10, 01); + CaseOfCode_CalcOverlap(10, 10, 10); + CaseOfCode_CalcOverlap(11, 10, 11); + + CaseOfCode_CalcOverlap(12, 11, 00); + CaseOfCode_CalcOverlap(13, 11, 01); + CaseOfCode_CalcOverlap(14, 11, 10); + CaseOfCode_CalcOverlap(15, 11, 11); + } + + + #undef CaseOfCode_CalcOverlap + #undef CalcOverlapBox00 + #undef CalcOverlapBox01 + #undef CalcOverlapBox11 + #undef CalcOverlapBox10 +} + + + + + +static void AddEntryToNext(BlitPool *pool, BlitEntry *entry, BlitEntry *new_entry) +/* +// This add a new entry to a next node of a [entry]. +// +// [entry] <-> [next] +// | +// V +// [entry] <-> [new_entry] <-> [next] +*/ +{ + assert(pool != NULL); + assert(entry != NULL); + assert(new_entry != NULL); + + + if (pool->head == NULL) { + AddEntryToTail(pool, entry); + return; + + } else { + BlitEntry *next; /* next node of [entry] */ + + next = entry->next; + + /* link: entry <-> new_entry */ + entry->next = new_entry; + new_entry->prev = entry; + + /* link: new_entry <-> next */ + new_entry->next = next; + if (next != NULL) { + next->prev = new_entry; + } + + pool->num_entry += 1; + } +} + + + +#if 0 +/* unuse */ +static void AddEntryToPrev(BlitPool *pool, BlitEntry *entry, BlitEntry *new_entry) +/* +// This add a new entry to a prev node of a [entry]. +// +// [prev] <-> [entry] +// | +// V +// [prev] <-> [new_entry] <-> [entry] +*/ +{ + assert(pool != NULL); + assert(entry != NULL); + assert(new_entry != NULL); + + + if (pool->head == NULL) { + AddEntryToHead(pool, entry); + return; + + } else { + BlitEntry *prev; /* next node of [entry] */ + + prev = entry->prev; + + /* link: entry <-> new_entry */ + entry->prev = new_entry; + new_entry->next = entry; + + /* link: new_entry <-> prev */ + new_entry->prev = prev; + if (prev != NULL) { + prev->next = new_entry; + } + + pool->num_entry += 1; + } +} + +static void AddEntryToHead(BlitPool *pool, BlitEntry *entry) +/* +// This add a new entry to head of list. +*/ +{ + assert(pool != NULL); + assert(entry != NULL); + + entry->next = NULL; + entry->prev = NULL; + + if (pool->head == NULL) { + pool->head = entry; + pool->tail = entry; + + } else { + pool->head->next = entry; + entry->next = pool->head; + + pool->head = entry; + } + + pool->num_entry += 1; +} +#endif + + + + +static void AddEntryToTail(BlitPool *pool, BlitEntry *entry) +/* +// This add a new entry to tail of list. +*/ +{ + assert(pool != NULL); + assert(entry != NULL); + + entry->next = NULL; + entry->prev = NULL; + + if (pool->head == NULL) { + pool->head = entry; + pool->tail = entry; + + } else { + pool->tail->next = entry; + entry->prev = pool->tail; + + pool->tail = entry; + } + + pool->num_entry += 1; +} + + + + +static void RemoveEntry(BlitPool *pool, BlitEntry *entry) +/* +// This unlink a entry from a pool(list). +*/ +{ + BlitEntry *next ,*prev; + + + assert(pool != NULL); + assert(entry != NULL); + + next = entry->next; + prev = entry->prev; + + if (prev == NULL) { + assert(entry == pool->head); + pool->head = next; + if (next != NULL) { + next->prev = NULL; + } + + } else { + prev->next = next; + } + + if (next == NULL) { + assert(entry == pool->tail); + pool->tail = prev; + if (prev != NULL) { + prev->next = NULL; + } + + } else { + next->prev = prev; + } + + pool->num_entry -= 1; +} + + + + +static void DeleteEntry(BlitPool *pool, BlitEntry *entry) +/* +// This delete a entry from a pool(list). +*/ +{ + assert(pool != NULL); + assert(entry != NULL); + + + RemoveEntry(pool, entry); + + ReleaseBlitEntry(pool, entry); +} + + + + +void BlitPoolUtil_RectToBBox(SDL_Rect *srcrect, BlitPool_BoundingBox *destbox) +{ + assert(destbox != NULL); + assert(srcrect != NULL); + + destbox->x0 = srcrect->x; + destbox->y0 = srcrect->y; + destbox->x1 = destbox->x0 + srcrect->w; + destbox->y1 = destbox->y0 + srcrect->h; +} + + + + +void BlitPoolUtil_BBoxToRect(BlitPool_BoundingBox *srcbox, SDL_Rect *destrect) +{ + assert(destrect != NULL); + assert(srcbox != NULL); + + destrect->x = srcbox->x0; + destrect->y = srcbox->y0; + destrect->w = srcbox->x1 - destrect->x; + destrect->h = srcbox->y1 - destrect->y; +} + + + + +static BlitEntry *AllocateBlitEntry(BlitPool *pool) +{ + assert(pool != NULL); + + return (BlitEntry *)pool->allocator(sizeof(BlitEntry)); +} + + + + +static void ReleaseBlitEntry(BlitPool *pool, BlitEntry *entry) +{ + assert(pool != NULL); + assert(entry != NULL); + + pool->releaser(entry); +} + + + + +static BlitEntry *DuplicateBlitEntry(BlitPool *pool, BlitEntry *base) +{ + BlitEntry *p; + + + assert(pool != NULL); + assert(base != NULL); + + p = AllocateBlitEntry(pool); + + + #if IF_ALLOC_FAIL(1) + 0 + + if (p == NULL) { + return NULL; + } + + #endif + + + *p = *base; /* or memcpy */ + + p->next = NULL; + p->prev = NULL; + + return p; +} + + + + + +static int StrEq(unsigned char *str0, unsigned char *str1, int n) +{ + int i; + + + assert(str0 != NULL); + assert(str1 != NULL); + + for (i = 0; i < n; i++) { + if (str0[i] != str1[i]) { + return 1; + } + } + + return 0; +} + + + + +static const unsigned char * const stg_BlitPool_Sign = "SDL_BlitPool by Strangebug"; + + + diff --git a/alienblaster/project/jni/sdl_blitpool/SDL_blitpool.h b/alienblaster/project/jni/sdl_blitpool/SDL_blitpool.h new file mode 100644 index 000000000..2f0715f5f --- /dev/null +++ b/alienblaster/project/jni/sdl_blitpool/SDL_blitpool.h @@ -0,0 +1,258 @@ +/* +// +// Blit operation pool for the SDL. +// Copyright (C) 2005 Strangebug (S.Miura) [strangebug art hotmail.co.jp] +// +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// +// SDL_BlitPool.c : 25/Feb/2005 : created +// +*/ + + +#ifndef INCLUDED_SDL_BLITPOOL_H +#define INCLUDED_SDL_BLITPOOL_H + + + +#include "SDL.h" +#include "begin_code.h" + + +#ifdef __cplusplus +extern "C" { +#endif + + + + +#ifndef Optional +# define Optional +#endif + + + + +typedef enum { + + /* BlitType */ + BLIT_TYPE_SURFACE = 1 << 0, + BLIT_TYPE_COLORFILL = 1 << 1, + BLIT_TYPE_EMPTY = 1 << 2, + + #define BLIT_TYPE_MASK (BLIT_TYPE_SURFACE | BLIT_TYPE_COLORFILL | BLIT_TYPE_EMPTY) + + + /* BlitTransparency */ + BLIT_ALPHA_TRANSPARENT = 1 << 3, /* Surface have transparency pixel */ + BLIT_ALPHA_OPAQUE = 1 << 4, /* Surface not have transparency pixel */ + + #define BLIT_ALPHA_MASK (BLIT_ALPHA_TRANSPARENT | BLIT_ALPHA_OPAQUE) + + + /* BlitExecOption */ + BLIT_EXEC_OPTIMIZE = 1 << 7, /* do optimize */ + BLIT_EXEC_NO_OPTIMIZE = 1 << 8, /* do not optimize */ + + #define BLIT_EXEC_MASK (BLIT_EXEC_OPTIMIZE | BLIT_EXEC_NO_OPTIMIZE) + + + BLIT_FLAG_TERM = 1 << 9 /* terminater */ + +} BlitEntryFlag; + + + + +typedef enum { + BLIT_OPT_REMOVEOVERLAP = 1 << 0, /* remove overlap area */ + BLIT_OPT_REMOVEOUTSIDE = 1 << 1, /* remove outside of destination surface */ + + BLIT_OPT_ALL = BLIT_OPT_REMOVEOVERLAP | BLIT_OPT_REMOVEOUTSIDE + +} BlitOptimizeFlag; + + + +typedef struct BlitPool_tag BlitPool; /* Pool object */ + + +typedef void *(*allocator_func)(unsigned long nbyte); +typedef void (*releaser_func)(void *p); + + + +typedef struct { + /* + // Box: (x0, y0) - (x1, y1) + */ + + Sint16 x0, y0, x1, y1; + +} BlitPool_BoundingBox; + + + + + +/* +// Create/Delete a BlitPool object. +*/ +extern BlitPool *BlitPool_CreatePool(Optional SDL_Surface *destsurf); + +extern void BlitPool_DeletePool(BlitPool *pool); + +extern void BlitPool_SetAllocator(BlitPool *pool, allocator_func allocator, releaser_func releaser); + + + + +/* +// Release all of entries. +*/ +extern void BlitPool_ReleaseEntry(BlitPool *pool); + + + + +/* +// Blit/Fill operation post. +*/ +extern int BlitPool_Post( + BlitPool *pool, + BlitEntryFlag flag, + SDL_Surface *src, + Optional SDL_Rect *srcrect, + Optional SDL_Rect *destrect, + Uint32 color +); + +#define BlitPool_PostSurface(pool, src, srcrect, destrect, flag) \ + BlitPool_Post(pool, BLIT_TYPE_SURFACE | (flag), src, srcrect, destrect, 0) + +#define BlitPool_PostFill(pool, destrect, color, flag) \ + BlitPool_Post(pool, BLIT_TYPE_COLORFILL | (flag), NULL, NULL, destrect, color) + + + + +/* +// Commit pool. +// src_pool don't release. +// offset use only (x, y). +*/ +extern void BlitPool_PostPool( + BlitPool *dest_pool, + BlitPool *src_pool, + Optional SDL_Rect *offset +); + + + + +/* +// Post separated surface/fill by area_description_str. +// +// area_description_str like: "pos=(45, 12):O:(0,0)-(12,12):(45,23)-(62, 24):pos=(0,0):col=(255,255,255,255):(12,98)-(1,65)" +// pos=(x,y) : destination position. default=(0,0). +// (x0,y0)-(x1,y1) : source surface rectangle. +// O/T : T=Transparens(default), O=Opaque. +// col=(r, g, b, a) : colorfill mode. it's format be surf->format. +// +// * Don't forget delimiter ':'. +*/ +extern int BlitPool_PostByDescription( + BlitPool *pool, + Optional SDL_Surface *surf, + unsigned char *area_description_str +); + + + + +/* +// Optimize the operations and execute blit. +*/ +extern void BlitPool_Optimize(BlitPool *pool, BlitOptimizeFlag flag); + +extern void BlitPool_Execute(BlitPool *pool); + + + + +/* +// For update. +// example: { +// int numrect; +// SDL_Rect rectbuf[10]; +// +// while ((numrect = BlitPool_GetUpdateRects(pool, rectbuf, sizeof(rectbuf)/sizeof(rectbuf[0]))) > 0) { +// SDL_UpdateRects(screen, numrect, rectbuf); +// } +// } +*/ +extern int BlitPool_GetRectCount(BlitPool *pool); + +extern void *BlitPool_GetUpdateRectsObj(BlitPool *pool); + +extern int BlitPool_GetUpdateRects(BlitPool *pool, SDL_Rect *rectbuf, int size, void **update_rects_obj); + + + + + +/* +// Get update area. +*/ +extern Uint32 BlitPool_GetArea(BlitPool *pool); + + + + +/* +// Utilities. +*/ + +extern int BlitPoolUtil_GetOverlapCode( + Uint8 *out_code, /* Must not NULL */ + BlitPool_BoundingBox *f, + BlitPool_BoundingBox *b +); + +extern void BlitPoolUtil_CalcOverlapArea( + BlitPool_BoundingBox *out_overlap_box, /* Must not NULL */ + Uint8 code, + BlitPool_BoundingBox *f, + BlitPool_BoundingBox *b +); + +extern void BlitPoolUtil_RectToBBox(SDL_Rect *src, BlitPool_BoundingBox *dest); + +extern void BlitPoolUtil_BBoxToRect(BlitPool_BoundingBox *src, SDL_Rect *dest); + + + + +#ifdef __cplusplus +} /* for extern "c" { */ +#endif + + +#include "close_code.h" + + +#endif /* INCLUDED_SDL_BLITPOOL_H */ diff --git a/alienblaster/project/jni/sdl_blitpool/look_divide.c b/alienblaster/project/jni/sdl_blitpool/look_divide.c new file mode 100644 index 000000000..d64de247c --- /dev/null +++ b/alienblaster/project/jni/sdl_blitpool/look_divide.c @@ -0,0 +1,184 @@ +/* +// Look divide. +// please set compiler option, macro define [PAINT_DIVIDED] +*/ + + +#include +#include +#include +#include + +#include "SDL.h" +#include "SDL_BlitPool.h" + + + +void MouseEvent(SDL_MouseButtonEvent *event); +void Render(void); + +SDL_Surface *g_ScreenSurface; +SDL_Rect g_Rect1, g_Rect2; + +SDL_Rect g_ClearRect[256]; +int g_NumClearRects; + +int main(int argc, char *argv[]) +{ + SDL_Event event; + int running; + + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0) { + fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError()); + return 1; + } + + g_ScreenSurface = SDL_SetVideoMode( + 640, 480, 32, + SDL_SWSURFACE | SDL_DOUBLEBUF | SDL_RESIZABLE /* | SDL_FULLSCREEN */ + ); + + running = 1; + while (running) { + while (SDL_PollEvent(&event)) { + switch(event.type){ + case SDL_QUIT: + running = 0; + break; + case SDL_MOUSEBUTTONDOWN: + case SDL_MOUSEBUTTONUP: + MouseEvent(&event.button); + break; + case SDL_VIDEORESIZE: + g_ScreenSurface = SDL_SetVideoMode( + event.resize.w, + event.resize.h, + g_ScreenSurface->format->BitsPerPixel, + g_ScreenSurface->flags + ); + } + } + + Render(); + } + + SDL_FreeSurface(g_ScreenSurface); + SDL_Quit(); + return 0; +} + +void MouseEvent(SDL_MouseButtonEvent *event) +{ +} + + + +void Render(void) +{ + int x, y; + BlitPool *pool; + SDL_Rect rect; + + + pool = BlitPool_CreatePool(g_ScreenSurface); + + BlitPool_PostFill( + pool, + NULL, + SDL_MapRGBA(g_ScreenSurface->format, 0, 0, 0, 10), + BLIT_ALPHA_OPAQUE + ); + + rect.x = 40; + rect.y = 40; + rect.w = 100; + rect.h = 100; + BlitPool_PostFill( + pool, + &rect, + 0xff116699, + BLIT_ALPHA_OPAQUE + ); + + rect.x = 220; + rect.y = 40; + rect.w = 200; + rect.h = 200; + BlitPool_PostFill( + pool, + &rect, + 0xff0011ff, + BLIT_ALPHA_OPAQUE + ); + + rect.x = 40; + rect.y = 160; + rect.w = 50; + rect.h = 50; + BlitPool_PostFill( + pool, + &rect, + 0xffff1101, + BLIT_ALPHA_OPAQUE + ); + + rect.x = 40; + rect.y = 260; + rect.w = 200; + rect.h = 50; + BlitPool_PostFill( + pool, + &rect, + 0xff113694, + BLIT_ALPHA_OPAQUE + ); + + rect.x = 300; + rect.y = 260; + rect.w = 50; + rect.h = 200; + BlitPool_PostFill( + pool, + &rect, + 0xff001100, + BLIT_ALPHA_OPAQUE + ); + + + SDL_GetMouseState(&x, &y); + rect.w = 100; + rect.h = 100; + rect.x = x - rect.w / 2; + rect.y = y - rect.h / 2; + BlitPool_PostFill( + pool, + &rect, + 0x00ffffff, + BLIT_ALPHA_OPAQUE + ); + + + BlitPool_Optimize(pool, BLIT_OPT_ALL); + BlitPool_Execute(pool); + + + #if 0 + { + int numrect; + SDL_Rect rectbuf[50]; + void *p; + + p = BlitPool_GetUpdateRectsObj(pool); + while ((numrect = BlitPool_GetUpdateRects(pool, rectbuf, sizeof(rectbuf)/sizeof(rectbuf[0]), &p)) > 0) { + SDL_UpdateRects(g_ScreenSurface, numrect, rectbuf); + } + } + #else + SDL_Flip(g_ScreenSurface); + #endif + + BlitPool_DeletePool(pool); +} + + + diff --git a/alienblaster/project/jni/sdl_blitpool/planet.c b/alienblaster/project/jni/sdl_blitpool/planet.c new file mode 100644 index 000000000..0f7bfed71 --- /dev/null +++ b/alienblaster/project/jni/sdl_blitpool/planet.c @@ -0,0 +1,705 @@ +/* +// Blit pool test: big planet. +// press b key to switch UseBlitPool/Unuse. +*/ + + +#include +#include +#include +#include + +#include "SDL.h" +#include "SDL_BlitPool.h" + + + +#define MAX_BUFFER 256 +#define MAX_PLANET (MAX_BUFFER * 2) + + +#define PATH_IMG_PLANET "bigplanet.bmp" +#define EPSILON 1.0e-3 + + +struct Vector2 { + float x, y; +}; + +struct Planet { + struct Vector2 Position; + struct Vector2 Acceleration; + + SDL_Rect ClearRect; + float Weight; +}; + + +struct { + + struct Planet PlanetArray[MAX_PLANET]; + Uint32 PlanetCount; + Uint32 PlanetSize; + + BlitPool *PlanetPool; + + struct Video { + SDL_Surface *ScreenSurface; + SDL_Surface *PlanetSurface; + + Uint32 FramePerSecond; + Uint32 FrameCounter; + + struct Vector2 ViewportOffset; + } Video; + + struct MouseState { + struct Vector2 Position; + struct Vector2 Acceleration; + int FlagViewportDragging; + } MouseState; + + struct { + int CleanFill; + int Wall; + int UseBlitPool; + } Option; +} g; + + +void MainLoop(void); +int Initialize(void); +int InitializeVideo(void); +void DeInitialize(void); + +int EventProcess(void); +void Step(Uint32 dt); +void Render(void); +void RenderUseBlitPool(void); + +void MouseEvent(SDL_MouseButtonEvent *event); +void Clear(void); +void UpdateMouseState(void); +void UpdateTitleBar(void); + +int CreatePlanet(float weight); +void DeleteAllPlanet(void); + + +void ApplyGravidy(Uint32 dt); +void ApplyMove(Uint32 dt); +void ApplyFrameCollide(void); +void ApplyGeneralResistance(void); + +void Vec2_Plus(struct Vector2 *dest, struct Vector2 *src); +void Vec2_Minus(struct Vector2 *dest, struct Vector2 *src); +void Vec2_Multi(struct Vector2 *dest, float t); +float Vec2_Norm(struct Vector2 *vec); +void Vec2_Normalize(struct Vector2 *vec); + + + + +int main(int argc, char *argv[]) +{ + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0) { + fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError()); + return 1; + } + + if (Initialize()) { + goto end; + } + + printf("press b key to switch UseBlitPool/Unuse.\n"); + + MainLoop(); + +end: + DeInitialize(); + SDL_Quit(); + return 0; +} + + + +int Initialize(void) +{ + if (InitializeVideo()) { + return 1; + } + + g.PlanetCount = 0; + g.PlanetSize = g.Video.PlanetSurface->w; + + g.Option.CleanFill = 1; + g.Option.Wall = 1; + g.Option.UseBlitPool = 1; /* switch by b key */ + + g.MouseState.FlagViewportDragging = 0; + + return 0; +} + +int InitializeVideo(void) +{ + struct Video *p; + + + p = &g.Video; + + p->ScreenSurface = SDL_SetVideoMode( + 640, 480, 32, + SDL_SWSURFACE | SDL_RESIZABLE /* | SDL_FULLSCREEN */ + ); + + { /* 青を透明カラーに */ + SDL_Surface *s; + + s = SDL_LoadBMP(PATH_IMG_PLANET); + if (s == NULL) { + return 1; + } + if (SDL_SetColorKey(s, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(s->format, 0, 0, 0xff))) { + return 1; + } + p->PlanetSurface = SDL_DisplayFormatAlpha(s); + SDL_FreeSurface(s); + } + + if (p->ScreenSurface == NULL || p->PlanetSurface == NULL) { + return 1; + } + + assert((p->PlanetSurface->w == p->PlanetSurface->h) && "planet Image must be square (width == height)"); + + p->FramePerSecond = 0; + p->FrameCounter = 0; + + p->ViewportOffset.x = + p->ViewportOffset.y = 0; + + + /* Make the planet surfaces pool */ + g.PlanetPool = BlitPool_CreatePool(NULL); + + BlitPool_PostByDescription( + g.PlanetPool, + p->PlanetSurface, + "pos=(0,0):" + "T:(29,0)-(170,200):" + "T:(0,29)-(200,170):" + "O:(29,29)-(170,170):" + ); + + BlitPool_Optimize(g.PlanetPool, BLIT_OPT_REMOVEOVERLAP); + + SDL_FillRect(p->ScreenSurface, NULL, SDL_MapRGBA(p->ScreenSurface->format, 0, 0, 0, 255)); + + return 0; +} + + +void DeInitialize(void) +{ + BlitPool_DeletePool(g.PlanetPool); + SDL_FreeSurface(g.Video.PlanetSurface); + SDL_FreeSurface(g.Video.ScreenSurface); +} + + +void MainLoop(void) +{ + Uint32 tick, pretick, fpstick, dt; + void (*per_second_operator[])(void) = { ApplyGeneralResistance, UpdateTitleBar, NULL }; + int i; + + + Clear(); + + UpdateTitleBar(); + + pretick = fpstick = SDL_GetTicks(); + + for (;;) { + + tick = SDL_GetTicks(); + dt = tick - pretick; + + #if 0 + if (dt <= 0) { + continue; + } + #endif + + if (EventProcess()) { + break; + } + + if ((tick - fpstick) >= 1000) { + + fpstick = tick; + + g.Video.FramePerSecond = g.Video.FrameCounter; + g.Video.FrameCounter = 0; + + for (i = 0; per_second_operator[i] != NULL; i++) { + per_second_operator[i](); + } + } + + Step(dt); + + if (g.Option.UseBlitPool) { + RenderUseBlitPool(); + } else { + Render(); + } + + g.Video.FrameCounter += 1; + + pretick = tick; + } +} + + + +void ApplyGeneralResistance(void) +{ + Uint32 i; + + for (i = 0; i < g.PlanetCount; i++) { + Vec2_Multi(&g.PlanetArray[i].Acceleration, 0.90); /* すべてのオブジェクトの速度を減衰 */ + } +} + + +int EventProcess(void) +{ + SDL_Event event; + SDL_Surface *surf; + Uint32 i; + + + surf = g.Video.ScreenSurface; + + UpdateMouseState(); + + if (g.MouseState.FlagViewportDragging) { + g.Video.ViewportOffset.x -= g.MouseState.Acceleration.x; + g.Video.ViewportOffset.y -= g.MouseState.Acceleration.y; + } + + while (SDL_PollEvent(&event)) { + switch(event.type){ + case SDL_QUIT: + return 1; + + case SDL_KEYDOWN: + switch (event.key.keysym.sym) { + case SDLK_ESCAPE: return 1; /* 終了 */ + case SDLK_w: g.Option.Wall ^= 1; break; /* 壁への衝突判定フラグ */ + case SDLK_b: g.Option.UseBlitPool ^= 1; break; /* Blitをプールするか */ + case SDLK_SPACE: Clear(); break; /* 惑星&画面クリア */ + case SDLK_a: CreatePlanet(1.0); break; /* 惑星生成 */ + + case SDLK_f: + SDL_FillRect(surf, NULL, SDL_MapRGBA(surf->format, 0, 0, 0, 0)); + SDL_Flip(surf); + break; /* 画面クリア */ + case SDLK_s: /* ストップ(全速度0) */ + for (i = 0; i < g.PlanetCount; i++) { + g.PlanetArray[i].Acceleration.x = 0; + g.PlanetArray[i].Acceleration.y = 0; + } + break; + } + break; + + case SDL_MOUSEBUTTONDOWN: + case SDL_MOUSEBUTTONUP: + MouseEvent(&event.button); + break; + + case SDL_VIDEORESIZE: + g.Video.ScreenSurface = SDL_SetVideoMode( + event.resize.w, + event.resize.h, + surf->format->BitsPerPixel, + surf->flags + ); + SDL_FreeSurface(surf); + break; + } + } + + return 0; +} + + +void MouseEvent(SDL_MouseButtonEvent *event) +{ + if (event->button == SDL_BUTTON_LEFT && event->type == SDL_MOUSEBUTTONDOWN) { + CreatePlanet(1.0); + } +} + +void Clear(void) +{ + DeleteAllPlanet(); + + SDL_FillRect(g.Video.ScreenSurface, NULL, SDL_MapRGBA(g.Video.ScreenSurface->format, 0, 0, 0, 255)); + SDL_Flip(g.Video.ScreenSurface); + + g.MouseState.Position.x = g.Video.ScreenSurface->w / 2; + g.MouseState.Position.y = g.Video.ScreenSurface->h / 2; + + g.MouseState.Acceleration.x = + g.MouseState.Acceleration.y = 0; + + g.Video.ViewportOffset.x = + g.Video.ViewportOffset.y = 0; +} + +void UpdateMouseState(void) +{ + int x, y; + + + SDL_GetMouseState(&x, &y); + g.MouseState.Position.x = x; + g.MouseState.Position.y = y; + + SDL_GetRelativeMouseState(&x, &y); + g.MouseState.Acceleration.x = x; + g.MouseState.Acceleration.y = y; +} + + + +void UpdateTitleBar(void) +{ + SDL_Surface *surf; + unsigned char str[MAX_BUFFER]; + static const unsigned char *title = "PlanetSDL"; + + + surf = g.Video.ScreenSurface; + + sprintf(str, + "%s | %2lu fps | [%lu x %lu x %lu bit] | %lu planet | [space] reset | [s] stop | [b]UsePool=%d", + title, g.Video.FramePerSecond, surf->clip_rect.w, surf->h, surf->format->BitsPerPixel, g.PlanetCount, g.Option.UseBlitPool + ); + + SDL_WM_SetCaption(str, title); +} + + + +void Step(Uint32 dt) +{ + if (dt == 0) { + return; + } + + ApplyGravidy(dt); /* 惑星間重力 */ + + if (g.Option.Wall) { + ApplyFrameCollide(); /* 画面フレームとの衝突判定 */ + } + + ApplyMove(dt); /* 加速度適応 */ +} + + +void ApplyGravidy(Uint32 dt) +{ + Uint32 i, j; + float weight, distance, force, limit, delta; + struct Vector2 vec1, vec2; + register struct Planet *planet1, *planet2; + + const float G = 30.0; + + + limit = g.PlanetSize / 3.0f; + delta = dt; + + assert(limit >= 0.5 && "limit must be >= 0.5"); + + for (i = 0; i < g.PlanetCount; i++) { + + planet1 = &g.PlanetArray[i]; + + for (j = i + 1; j < g.PlanetCount; j++) { + + planet2 = &g.PlanetArray[j]; + + vec1 = planet1->Position; + Vec2_Minus(&vec1, &planet2->Position); + + distance = Vec2_Norm(&vec1); + + weight = planet1->Weight * planet2->Weight; + + if (distance <= limit) { + distance = limit; + } + + force = -G * weight / (distance * distance) * delta; + + Vec2_Normalize(&vec1); /* 惑星間の単位方向ベクトル */ + vec2 = vec1; + + Vec2_Multi(&vec1, force / planet1->Weight); + Vec2_Plus(&planet1->Acceleration, &vec1); + + Vec2_Multi(&vec2, force / -planet2->Weight); + Vec2_Plus(&planet2->Acceleration, &vec2); + } + } +} + + +void ApplyMove(Uint32 dt) +{ + register Uint32 i; + register float delta; + struct Vector2 acc; + + + delta = dt / 10.0f; + + for (i = 0; i < g.PlanetCount; i++) { + acc = g.PlanetArray[i].Acceleration; + Vec2_Multi(&acc, delta); + Vec2_Plus(&g.PlanetArray[i].Position, &acc); + } +} + + +void ApplyFrameCollide(void) +{ + float frame_width, frame_height; + Uint32 i; + struct Planet *planet; + register float planet_radius; + + + frame_width = g.Video.ScreenSurface->w; + frame_height = g.Video.ScreenSurface->h; + planet_radius = g.PlanetSize / 2; + + for (i = 0; i < g.PlanetCount; i++) { + + planet = &g.PlanetArray[i]; + + if (planet->Position.x < planet_radius){ + planet->Position.x = planet_radius; + planet->Acceleration.x *= -1; + + } else if (planet->Position.x > (frame_width - planet_radius)){ + planet->Position.x = frame_width - planet_radius; + planet->Acceleration.x *= -1; + } + + if (planet->Position.y < planet_radius){ + planet->Position.y = planet_radius; + planet->Acceleration.y *= -1; + + } else if (planet->Position.y > (frame_height - planet_radius)){ + planet->Position.y = frame_height - planet_radius; + planet->Acceleration.y *= -1; + } + } +} + + + +void Render(void) +{ + SDL_Surface *surf; + Uint32 i; + SDL_Rect rect; + Sint32 offset; + Uint32 numrect; + SDL_Rect rectbuf[MAX_PLANET * 2]; + + + surf = g.Video.ScreenSurface; + rect.x = rect.y = 0; + rect.w = rect.h = 0; + offset = g.PlanetSize / 2; + numrect = 0; + + for (i = 0; i < g.PlanetCount; i++) { + SDL_FillRect(surf, &g.PlanetArray[i].ClearRect, SDL_MapRGBA(surf->format, 0, 0, 0, 255)); + rectbuf[numrect++] = g.PlanetArray[i].ClearRect; + } + + + /* 描画 */ + + for (i = 0; i < g.PlanetCount; i++) { + rect.x = g.PlanetArray[i].Position.x - offset - g.Video.ViewportOffset.x; + rect.y = g.PlanetArray[i].Position.y - offset - g.Video.ViewportOffset.y; + + SDL_BlitSurface(g.Video.PlanetSurface, NULL, surf, &rect); + + g.PlanetArray[i].ClearRect = rect; + rectbuf[numrect++] = rect; + } + + SDL_UpdateRects(surf, numrect, rectbuf); + SDL_Flip(surf); +} + + + +static unsigned char g_buf[1024 * 512]; +static unsigned char *g_p; +#define InitAllocator() g_p = g_buf + +static void *Allocator(unsigned long nbyte) +{ + return (void *)((g_p += nbyte) - nbyte); +} +static void Releaser(void *p) +{ + assert(p != NULL); +} + + + +void RenderUseBlitPool(void) +{ + Uint32 i; + SDL_Rect rect; + Sint32 offset; + BlitPool *pool; + SDL_Surface *screen, *planet; + + + rect.x = rect.y = 0; + offset = g.PlanetSize / 2; + screen = g.Video.ScreenSurface; + planet = g.Video.PlanetSurface; + rect.w = rect.h = g.PlanetSize; + pool = BlitPool_CreatePool(screen); + + BlitPool_SetAllocator(pool, Allocator, Releaser); + InitAllocator(); + + for (i = 0; i < g.PlanetCount; i++) { + BlitPool_PostFill( + pool, + &g.PlanetArray[i].ClearRect, + SDL_MapRGBA(screen->format, 0, 0, 0, 255), + BLIT_ALPHA_OPAQUE + ); + } + + for (i = 0; i < g.PlanetCount; i++) { + + rect.x = g.PlanetArray[i].Position.x - offset - g.Video.ViewportOffset.x; + rect.y = g.PlanetArray[i].Position.y - offset - g.Video.ViewportOffset.y; + + BlitPool_PostPool(pool, g.PlanetPool, &rect); + + g.PlanetArray[i].ClearRect = rect; + } + + BlitPool_Optimize(pool, BLIT_OPT_ALL); + BlitPool_Execute(pool); + + + if ((BlitPool_GetArea(pool) / (float)(screen->w * screen->h)) >= 0.8) { /* update area ratio */ + + SDL_Flip(screen); + + } else { + int numrect; + SDL_Rect rectbuf[1024]; + void *p; + + p = BlitPool_GetUpdateRectsObj(pool); /* work object */ + while ((numrect = BlitPool_GetUpdateRects(pool, rectbuf, sizeof(rectbuf)/sizeof(rectbuf[0]), &p)) > 0) { + SDL_UpdateRects(screen, numrect, rectbuf); + } + } + + + BlitPool_DeletePool(pool); +} + +int CreatePlanet(float weight) +{ + struct Planet *new_planet; + + + if (g.PlanetCount >= MAX_PLANET) { + return 1; + } + + new_planet = &g.PlanetArray[g.PlanetCount]; + g.PlanetCount += 1; + + new_planet->Position = g.MouseState.Position; + new_planet->Acceleration = g.MouseState.Acceleration; + new_planet->Weight = weight; + + /* 調整 */ + new_planet->Position.x += g.Video.ViewportOffset.x; + new_planet->Position.y += g.Video.ViewportOffset.y; + + return 0; +} + + +void DeleteAllPlanet(void) +{ + g.PlanetCount = 0; +} + + +void Vec2_Plus(struct Vector2 *dest, struct Vector2 *src) +{ + dest->x += src->x; + dest->y += src->y; +} + +void Vec2_Minus(struct Vector2 *dest, struct Vector2 *src) +{ + dest->x -= src->x; + dest->y -= src->y; +} + +void Vec2_Multi(struct Vector2 *dest, float t) +{ + dest->x *= t; + dest->y *= t; +} + +float Vec2_Norm(struct Vector2 *vec) +{ + return sqrt(vec->x * vec->x + vec->y * vec->y); +} + +void Vec2_Normalize(struct Vector2 *vec) +{ + float norm; + + norm = Vec2_Norm(vec); + + if (norm <= EPSILON && norm >= -EPSILON) { + + vec->x = 1; + vec->y = 0; + + } else { + vec->x /= norm; + vec->y /= norm; + } +} + + diff --git a/alienblaster/project/jni/sdl_net/Android.mk b/alienblaster/project/jni/sdl_net/Android.mk new file mode 100644 index 000000000..5df879b36 --- /dev/null +++ b/alienblaster/project/jni/sdl_net/Android.mk @@ -0,0 +1,18 @@ +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) + +LOCAL_MODULE := sdl_net + +LOCAL_CFLAGS := -I$(LOCAL_PATH) -I$(LOCAL_PATH)/../sdl/include + +LOCAL_CPP_EXTENSION := .cpp + +LOCAL_SRC_FILES := $(notdir $(wildcard $(LOCAL_PATH)/*.c)) + +LOCAL_SHARED_LIBRARIES := sdl +LOCAL_STATIC_LIBRARIES := +LOCAL_LDLIBS := + +include $(BUILD_SHARED_LIBRARY) + diff --git a/alienblaster/project/jni/sdl_net/CHANGES b/alienblaster/project/jni/sdl_net/CHANGES new file mode 100644 index 000000000..150d83c18 --- /dev/null +++ b/alienblaster/project/jni/sdl_net/CHANGES @@ -0,0 +1,83 @@ +1.2.7: +Joakim L. Gilje - Sat Jul 14 22:54:37 PDT 2007 + * Set server TCP sockets to blocking mode on Mac OS X, Solaris, etc. + +1.2.6: +Sam Lantinga - Sun Apr 30 01:48:40 PDT 2006 + * Added gcc-fat.sh for generating Universal binaries on Mac OS X + * Updated libtool support to version 1.5.22 +Sam Lantinga - Wed Nov 19 00:23:44 PST 2003 + * Updated libtool support for new mingw32 DLL build process +Shard - Thu, 05 Jun 2003 09:30:20 -0500 + * Fixed compiling on BeOS, which may not have SO_BROADCAST +Kyle Davenport - Sat, 19 Apr 2003 17:13:31 -0500 + * Added .la files to the development RPM, fixing RPM build on RedHat 8 + +1.2.5: +Luc-Olivier de Charri鑽e - Sun, 05 Jan 2003 22:04:29 +0100 + * Added support for sending UDP broadcast packets +Sam Lantinga - Sun Oct 20 20:54:41 PDT 2002 + * Added shared library support for MacOS X +Sam Lantinga - Sat Aug 24 18:16:08 PDT 2002 + * It is now safe to nest calls to SDLNet_Init() / SDLNet_Quit() +Gatan de Menten - Sat Aug 24 18:08:39 PDT 2002 + * Fixed UDP virtual address bind bug + +1.2.4: +Sam Lantinga - Sat Apr 13 07:49:47 PDT 2002 + * Updated autogen.sh for new versions of automake + * Specify the SDL API calling convention (C by default) +Stephane Magnenat - Wed Feb 13 15:28:04 PST 2002 + * Sockets are created with the SO_REUSEADDR flag by default +Juergen Wind - Wed Feb 13 09:21:55 PST 2002 + * Fixed data alignment problems on IRIX + +1.2.3: +Sam Lantinga - Fri Oct 26 07:15:28 PDT 2001 + * Fixed byte order read/write macros on sparc +Jonathan Atkins - Sun Sep 23 10:44:27 PDT 2001 + * Fixed non-blocking socket flags on Windows + +1.2.2: +Sam Lantinga - Sun Jul 22 16:41:44 PDT 2001 + * Added Project Builder projects for building MacOS X framework +Masahiro Minami - Sun, 27 May 2001 02:10:35 +0900 + * Added working MacOS Open Transport support + +1.2.1: +Sam Lantinga - Tue Apr 17 11:42:13 PDT 2001 + * Cleaned up swap function definitions in SDL_net.h + * Added the swap functions back in for binary compatibility +Paul Jenner - Sat, 14 Apr 2001 09:20:38 -0700 (PDT) + * Added support for building RPM directly from tar archive + +1.2.0: +Sam Lantinga - Wed Apr 4 12:42:20 PDT 2001 + * Synchronized release version with SDL 1.2.0 + +1.1.2: +Sam Lantinga - Sat Feb 10 16:33:59 PST 2001 + * SDL_net works with the sockets API out of the box on MacOS X. +Paul S Jenner - Sun, 4 Feb 2001 03:58:44 -0800 (PST) + * Added an RPM spec file +Patrick Levin - Mon, 8 Jan 2001 23:20:11 +0100 + * Fixed non-blocking socket modes on Win32 +John Lawrence - Mon, 13 Nov 2000 10:39:48 -0800 + * Fixed compile problem with MSVC++ (type casting) + +1.1.1: +Sam Lantinga - Sat Jul 1 15:20:51 PDT 2000 + * Modified chat example to work with GUIlib 1.1.0 +Roy Wood - Fri Jun 30 10:41:05 PDT 2000 + * A few MacOS fixes (not yet complete) + +1.1.0: +Andreas Umbach - Sat May 27 14:44:06 PDT 2000 + * Suggested non-blocking server sockets + * Suggested setting TCP_NODELAY by default +Roy Wood - Sat May 27 14:41:42 PDT 2000 + * Ported to MacOS (not yet complete) + +1.0.2: +Miguel Angel Blanch - Sat, 22 Apr 2000 23:06:05 + * Implemented SDLNet_ResolveIP() diff --git a/alienblaster/project/jni/sdl_net/COPYING b/alienblaster/project/jni/sdl_net/COPYING new file mode 100644 index 000000000..2cba2ac74 --- /dev/null +++ b/alienblaster/project/jni/sdl_net/COPYING @@ -0,0 +1,458 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + + END OF TERMS AND CONDITIONS diff --git a/alienblaster/project/jni/sdl_net/README b/alienblaster/project/jni/sdl_net/README new file mode 100644 index 000000000..d6b4bd48e --- /dev/null +++ b/alienblaster/project/jni/sdl_net/README @@ -0,0 +1,27 @@ + +SDL_net 1.2 + +The latest version of this library is available from: +http://www.libsdl.org/projects/SDL_net/ + +This is an example portable network library for use with SDL. +It is available under the GNU Library General Public License. +The API can be found in the file SDL_net.h +This library supports UNIX, Windows, MacOS Classic, MacOS X, +BeOS and QNX. + +The demo program is a chat client and server. +The chat client requires the sample GUI library available at: +http://www.libsdl.org/projects/GUIlib/ +The chat client connects to the server via TCP, registering itself. +The server sends back a list of connected clients, and keeps the +client updated with the status of other clients. +Every line of text from a client is sent via UDP to every other client. + +Note that this isn't necessarily how you would want to write a chat +program, but it demonstrates how to use the basic features of the +network library. + +Enjoy! + -Sam Lantinga and Roy Wood + diff --git a/alienblaster/project/jni/sdl_net/SDL_net.h b/alienblaster/project/jni/sdl_net/SDL_net.h new file mode 100644 index 000000000..ffab34728 --- /dev/null +++ b/alienblaster/project/jni/sdl_net/SDL_net.h @@ -0,0 +1,444 @@ +/* + SDL_net: An example cross-platform network library for use with SDL + Copyright (C) 1997-2004 Sam Lantinga + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + Sam Lantinga + slouken@libsdl.org +*/ + +/* $Id: SDL_net.h 3281 2007-07-15 05:58:56Z slouken $ */ + +#ifndef _SDL_NET_H +#define _SDL_NET_H + +#include "SDL.h" +#include "SDL_endian.h" +#include "SDL_version.h" +#include "begin_code.h" + + + +/* Set up for C function definitions, even when using C++ */ +#ifdef __cplusplus +extern "C" { +#endif + +/* Printable format: "%d.%d.%d", MAJOR, MINOR, PATCHLEVEL +*/ +#define SDL_NET_MAJOR_VERSION 1 +#define SDL_NET_MINOR_VERSION 2 +#define SDL_NET_PATCHLEVEL 7 + +/* This macro can be used to fill a version structure with the compile-time + * version of the SDL_net library. + */ +#define SDL_NET_VERSION(X) \ +{ \ + (X)->major = SDL_NET_MAJOR_VERSION; \ + (X)->minor = SDL_NET_MINOR_VERSION; \ + (X)->patch = SDL_NET_PATCHLEVEL; \ +} + +/* This function gets the version of the dynamically linked SDL_net library. + it should NOT be used to fill a version structure, instead you should + use the SDL_NET_VERSION() macro. + */ +extern DECLSPEC const SDL_version * SDLCALL SDLNet_Linked_Version(void); + +/* Initialize/Cleanup the network API + SDL must be initialized before calls to functions in this library, + because this library uses utility functions from the SDL library. +*/ +extern DECLSPEC int SDLCALL SDLNet_Init(void); +extern DECLSPEC void SDLCALL SDLNet_Quit(void); + +/***********************************************************************/ +/* IPv4 hostname resolution API */ +/***********************************************************************/ + +typedef struct { + Uint32 host; /* 32-bit IPv4 host address */ + Uint16 port; /* 16-bit protocol port */ +} IPaddress; + +/* Resolve a host name and port to an IP address in network form. + If the function succeeds, it will return 0. + If the host couldn't be resolved, the host portion of the returned + address will be INADDR_NONE, and the function will return -1. + If 'host' is NULL, the resolved host will be set to INADDR_ANY. + */ +#ifndef INADDR_ANY +#define INADDR_ANY 0x00000000 +#endif +#ifndef INADDR_NONE +#define INADDR_NONE 0xFFFFFFFF +#endif +#ifndef INADDR_BROADCAST +#define INADDR_BROADCAST 0xFFFFFFFF +#endif +extern DECLSPEC int SDLCALL SDLNet_ResolveHost(IPaddress *address, const char *host, Uint16 port); + +/* Resolve an ip address to a host name in canonical form. + If the ip couldn't be resolved, this function returns NULL, + otherwise a pointer to a static buffer containing the hostname + is returned. Note that this function is not thread-safe. +*/ +extern DECLSPEC const char * SDLCALL SDLNet_ResolveIP(IPaddress *ip); + + +/***********************************************************************/ +/* TCP network API */ +/***********************************************************************/ + +typedef struct _TCPsocket *TCPsocket; + +/* Open a TCP network socket + If ip.host is INADDR_NONE or INADDR_ANY, this creates a local server + socket on the given port, otherwise a TCP connection to the remote + host and port is attempted. The address passed in should already be + swapped to network byte order (addresses returned from + SDLNet_ResolveHost() are already in the correct form). + The newly created socket is returned, or NULL if there was an error. +*/ +extern DECLSPEC TCPsocket SDLCALL SDLNet_TCP_Open(IPaddress *ip); + +/* Accept an incoming connection on the given server socket. + The newly created socket is returned, or NULL if there was an error. +*/ +extern DECLSPEC TCPsocket SDLCALL SDLNet_TCP_Accept(TCPsocket server); + +/* Get the IP address of the remote system associated with the socket. + If the socket is a server socket, this function returns NULL. +*/ +extern DECLSPEC IPaddress * SDLCALL SDLNet_TCP_GetPeerAddress(TCPsocket sock); + +/* Send 'len' bytes of 'data' over the non-server socket 'sock' + This function returns the actual amount of data sent. If the return value + is less than the amount of data sent, then either the remote connection was + closed, or an unknown socket error occurred. +*/ +extern DECLSPEC int SDLCALL SDLNet_TCP_Send(TCPsocket sock, const void *data, + int len); + +/* Receive up to 'maxlen' bytes of data over the non-server socket 'sock', + and store them in the buffer pointed to by 'data'. + This function returns the actual amount of data received. If the return + value is less than or equal to zero, then either the remote connection was + closed, or an unknown socket error occurred. +*/ +extern DECLSPEC int SDLCALL SDLNet_TCP_Recv(TCPsocket sock, void *data, int maxlen); + +/* Close a TCP network socket */ +extern DECLSPEC void SDLCALL SDLNet_TCP_Close(TCPsocket sock); + + +/***********************************************************************/ +/* UDP network API */ +/***********************************************************************/ + +/* The maximum channels on a a UDP socket */ +#define SDLNET_MAX_UDPCHANNELS 32 +/* The maximum addresses bound to a single UDP socket channel */ +#define SDLNET_MAX_UDPADDRESSES 4 + +typedef struct _UDPsocket *UDPsocket; +typedef struct { + int channel; /* The src/dst channel of the packet */ + Uint8 *data; /* The packet data */ + int len; /* The length of the packet data */ + int maxlen; /* The size of the data buffer */ + int status; /* packet status after sending */ + IPaddress address; /* The source/dest address of an incoming/outgoing packet */ +} UDPpacket; + +/* Allocate/resize/free a single UDP packet 'size' bytes long. + The new packet is returned, or NULL if the function ran out of memory. + */ +extern DECLSPEC UDPpacket * SDLCALL SDLNet_AllocPacket(int size); +extern DECLSPEC int SDLCALL SDLNet_ResizePacket(UDPpacket *packet, int newsize); +extern DECLSPEC void SDLCALL SDLNet_FreePacket(UDPpacket *packet); + +/* Allocate/Free a UDP packet vector (array of packets) of 'howmany' packets, + each 'size' bytes long. + A pointer to the first packet in the array is returned, or NULL if the + function ran out of memory. + */ +extern DECLSPEC UDPpacket ** SDLCALL SDLNet_AllocPacketV(int howmany, int size); +extern DECLSPEC void SDLCALL SDLNet_FreePacketV(UDPpacket **packetV); + + +/* Open a UDP network socket + If 'port' is non-zero, the UDP socket is bound to a local port. + The 'port' should be given in native byte order, but is used + internally in network (big endian) byte order, in addresses, etc. + This allows other systems to send to this socket via a known port. +*/ +extern DECLSPEC UDPsocket SDLCALL SDLNet_UDP_Open(Uint16 port); + +/* Bind the address 'address' to the requested channel on the UDP socket. + If the channel is -1, then the first unbound channel will be bound with + the given address as it's primary address. + If the channel is already bound, this new address will be added to the + list of valid source addresses for packets arriving on the channel. + If the channel is not already bound, then the address becomes the primary + address, to which all outbound packets on the channel are sent. + This function returns the channel which was bound, or -1 on error. +*/ +extern DECLSPEC int SDLCALL SDLNet_UDP_Bind(UDPsocket sock, int channel, IPaddress *address); + +/* Unbind all addresses from the given channel */ +extern DECLSPEC void SDLCALL SDLNet_UDP_Unbind(UDPsocket sock, int channel); + +/* Get the primary IP address of the remote system associated with the + socket and channel. If the channel is -1, then the primary IP port + of the UDP socket is returned -- this is only meaningful for sockets + opened with a specific port. + If the channel is not bound and not -1, this function returns NULL. + */ +extern DECLSPEC IPaddress * SDLCALL SDLNet_UDP_GetPeerAddress(UDPsocket sock, int channel); + +/* Send a vector of packets to the the channels specified within the packet. + If the channel specified in the packet is -1, the packet will be sent to + the address in the 'src' member of the packet. + Each packet will be updated with the status of the packet after it has + been sent, -1 if the packet send failed. + This function returns the number of packets sent. +*/ +extern DECLSPEC int SDLCALL SDLNet_UDP_SendV(UDPsocket sock, UDPpacket **packets, int npackets); + +/* Send a single packet to the specified channel. + If the channel specified in the packet is -1, the packet will be sent to + the address in the 'src' member of the packet. + The packet will be updated with the status of the packet after it has + been sent. + This function returns 1 if the packet was sent, or 0 on error. + + NOTE: + The maximum size of the packet is limited by the MTU (Maximum Transfer Unit) + of the transport medium. It can be as low as 250 bytes for some PPP links, + and as high as 1500 bytes for ethernet. +*/ +extern DECLSPEC int SDLCALL SDLNet_UDP_Send(UDPsocket sock, int channel, UDPpacket *packet); + +/* Receive a vector of pending packets from the UDP socket. + The returned packets contain the source address and the channel they arrived + on. If they did not arrive on a bound channel, the the channel will be set + to -1. + The channels are checked in highest to lowest order, so if an address is + bound to multiple channels, the highest channel with the source address + bound will be returned. + This function returns the number of packets read from the network, or -1 + on error. This function does not block, so can return 0 packets pending. +*/ +extern DECLSPEC int SDLCALL SDLNet_UDP_RecvV(UDPsocket sock, UDPpacket **packets); + +/* Receive a single packet from the UDP socket. + The returned packet contains the source address and the channel it arrived + on. If it did not arrive on a bound channel, the the channel will be set + to -1. + The channels are checked in highest to lowest order, so if an address is + bound to multiple channels, the highest channel with the source address + bound will be returned. + This function returns the number of packets read from the network, or -1 + on error. This function does not block, so can return 0 packets pending. +*/ +extern DECLSPEC int SDLCALL SDLNet_UDP_Recv(UDPsocket sock, UDPpacket *packet); + +/* Close a UDP network socket */ +extern DECLSPEC void SDLCALL SDLNet_UDP_Close(UDPsocket sock); + + +/***********************************************************************/ +/* Hooks for checking sockets for available data */ +/***********************************************************************/ + +typedef struct _SDLNet_SocketSet *SDLNet_SocketSet; + +/* Any network socket can be safely cast to this socket type */ +typedef struct { + int ready; +} *SDLNet_GenericSocket; + +/* Allocate a socket set for use with SDLNet_CheckSockets() + This returns a socket set for up to 'maxsockets' sockets, or NULL if + the function ran out of memory. + */ +extern DECLSPEC SDLNet_SocketSet SDLCALL SDLNet_AllocSocketSet(int maxsockets); + +/* Add a socket to a set of sockets to be checked for available data */ +#define SDLNet_TCP_AddSocket(set, sock) \ + SDLNet_AddSocket(set, (SDLNet_GenericSocket)sock) +#define SDLNet_UDP_AddSocket(set, sock) \ + SDLNet_AddSocket(set, (SDLNet_GenericSocket)sock) +extern DECLSPEC int SDLCALL SDLNet_AddSocket(SDLNet_SocketSet set, SDLNet_GenericSocket sock); + +/* Remove a socket from a set of sockets to be checked for available data */ +#define SDLNet_TCP_DelSocket(set, sock) \ + SDLNet_DelSocket(set, (SDLNet_GenericSocket)sock) +#define SDLNet_UDP_DelSocket(set, sock) \ + SDLNet_DelSocket(set, (SDLNet_GenericSocket)sock) +extern DECLSPEC int SDLCALL SDLNet_DelSocket(SDLNet_SocketSet set, SDLNet_GenericSocket sock); + +/* This function checks to see if data is available for reading on the + given set of sockets. If 'timeout' is 0, it performs a quick poll, + otherwise the function returns when either data is available for + reading, or the timeout in milliseconds has elapsed, which ever occurs + first. This function returns the number of sockets ready for reading, + or -1 if there was an error with the select() system call. +*/ +extern DECLSPEC int SDLCALL SDLNet_CheckSockets(SDLNet_SocketSet set, Uint32 timeout); + +/* After calling SDLNet_CheckSockets(), you can use this function on a + socket that was in the socket set, to find out if data is available + for reading. +*/ +#define SDLNet_SocketReady(sock) \ + ((sock != NULL) && ((SDLNet_GenericSocket)sock)->ready) + +/* Free a set of sockets allocated by SDL_NetAllocSocketSet() */ +extern DECLSPEC void SDLCALL SDLNet_FreeSocketSet(SDLNet_SocketSet set); + + +/***********************************************************************/ +/* Platform-independent data conversion functions */ +/***********************************************************************/ + +/* Write a 16/32 bit value to network packet buffer */ +extern DECLSPEC void SDLCALL SDLNet_Write16(Uint16 value, void *area); +extern DECLSPEC void SDLCALL SDLNet_Write32(Uint32 value, void *area); + +/* Read a 16/32 bit value from network packet buffer */ +extern DECLSPEC Uint16 SDLCALL SDLNet_Read16(void *area); +extern DECLSPEC Uint32 SDLCALL SDLNet_Read32(void *area); + +/***********************************************************************/ +/* Error reporting functions */ +/***********************************************************************/ + +/* We'll use SDL's functions for error reporting */ +#define SDLNet_SetError SDL_SetError +#define SDLNet_GetError SDL_GetError + +/* I'm eventually going to try to disentangle SDL_net from SDL, thus making + SDL_net an independent X-platform networking toolkit. Not today though.... + +extern no_parse_DECLSPEC void SDLCALL SDLNet_SetError(const char *fmt, ...); +extern no_parse_DECLSPEC char * SDLCALL SDLNet_GetError(void); +*/ + + +/* Inline macro functions to read/write network data */ + +/* Warning, some systems have data access alignment restrictions */ +#if defined(sparc) || defined(mips) +#define SDL_DATA_ALIGNED 1 +#endif +#ifndef SDL_DATA_ALIGNED +#define SDL_DATA_ALIGNED 0 +#endif + +/* Write a 16 bit value to network packet buffer */ +#if !SDL_DATA_ALIGNED +#define SDLNet_Write16(value, areap) \ + (*(Uint16 *)(areap) = SDL_SwapBE16(value)) +#else +#if SDL_BYTEORDER == SDL_BIG_ENDIAN +#define SDLNet_Write16(value, areap) \ +do \ +{ \ + Uint8 *area = (Uint8 *)(areap); \ + area[0] = (value >> 8) & 0xFF; \ + area[1] = value & 0xFF; \ +} while ( 0 ) +#else +#define SDLNet_Write16(value, areap) \ +do \ +{ \ + Uint8 *area = (Uint8 *)(areap); \ + area[1] = (value >> 8) & 0xFF; \ + area[0] = value & 0xFF; \ +} while ( 0 ) +#endif +#endif /* !SDL_DATA_ALIGNED */ + +/* Write a 32 bit value to network packet buffer */ +#if !SDL_DATA_ALIGNED +#define SDLNet_Write32(value, areap) \ + *(Uint32 *)(areap) = SDL_SwapBE32(value); +#else +#if SDL_BYTEORDER == SDL_BIG_ENDIAN +#define SDLNet_Write32(value, areap) \ +do \ +{ \ + Uint8 *area = (Uint8 *)(areap); \ + area[0] = (value >> 24) & 0xFF; \ + area[1] = (value >> 16) & 0xFF; \ + area[2] = (value >> 8) & 0xFF; \ + area[3] = value & 0xFF; \ +} while ( 0 ) +#else +#define SDLNet_Write32(value, areap) \ +do \ +{ \ + Uint8 *area = (Uint8 *)(areap); \ + area[3] = (value >> 24) & 0xFF; \ + area[2] = (value >> 16) & 0xFF; \ + area[1] = (value >> 8) & 0xFF; \ + area[0] = value & 0xFF; \ +} while ( 0 ) +#endif +#endif /* !SDL_DATA_ALIGNED */ + +/* Read a 16 bit value from network packet buffer */ +#if !SDL_DATA_ALIGNED +#define SDLNet_Read16(areap) \ + (SDL_SwapBE16(*(Uint16 *)(areap))) +#else +#if SDL_BYTEORDER == SDL_BIG_ENDIAN +#define SDLNet_Read16(areap) \ + ((((Uint8 *)areap)[0] << 8) | ((Uint8 *)areap)[1] << 0) +#else +#define SDLNet_Read16(areap) \ + ((((Uint8 *)areap)[1] << 8) | ((Uint8 *)areap)[0] << 0) +#endif +#endif /* !SDL_DATA_ALIGNED */ + +/* Read a 32 bit value from network packet buffer */ +#if !SDL_DATA_ALIGNED +#define SDLNet_Read32(areap) \ + (SDL_SwapBE32(*(Uint32 *)(areap))) +#else +#if SDL_BYTEORDER == SDL_BIG_ENDIAN +#define SDLNet_Read32(areap) \ + ((((Uint8 *)areap)[0] << 24) | (((Uint8 *)areap)[1] << 16) | \ + (((Uint8 *)areap)[2] << 8) | ((Uint8 *)areap)[3] << 0) +#else +#define SDLNet_Read32(areap) \ + ((((Uint8 *)areap)[3] << 24) | (((Uint8 *)areap)[2] << 16) | \ + (((Uint8 *)areap)[1] << 8) | ((Uint8 *)areap)[0] << 0) +#endif +#endif /* !SDL_DATA_ALIGNED */ + +#ifdef MACOS_OPENTRANSPORT +#endif +/* Ends C function definitions when using C++ */ +#ifdef __cplusplus +} +#endif +#include "close_code.h" + +#endif /* _SDL_NET_H */ diff --git a/alienblaster/project/jni/sdl_net/SDLnet.c b/alienblaster/project/jni/sdl_net/SDLnet.c new file mode 100644 index 000000000..c2f87f54f --- /dev/null +++ b/alienblaster/project/jni/sdl_net/SDLnet.c @@ -0,0 +1,430 @@ +/* + SDL_net: An example cross-platform network library for use with SDL + Copyright (C) 1997-2004 Sam Lantinga + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + Sam Lantinga + slouken@libsdl.org +*/ + +/* $Id: SDLnet.c 2207 2006-04-20 16:48:25Z slouken $ */ + +#include + +#include "SDL_byteorder.h" + +#include "SDLnetsys.h" +#include "SDL_net.h" + + +const SDL_version *SDLNet_Linked_Version(void) +{ + static SDL_version linked_version; + SDL_NET_VERSION(&linked_version); + return(&linked_version); +} + +/* Since the UNIX/Win32/BeOS code is so different from MacOS, + we'll just have two completely different sections here. +*/ +static int SDLNet_started = 0; + +#ifdef MACOS_OPENTRANSPORT + +#include + +typedef struct +{ + Uint8 stat; + InetSvcRef dns; +}DNSStatus, *DNSStatusRef; + +enum +{ + dnsNotReady = 0, + dnsReady = 1, + dnsResolved = 2, + dnsError = 255 +}; + +//static InetSvcRef dns = 0; +static DNSStatus dnsStatus; +Uint32 OTlocalhost = 0; + +/* We need a notifier for opening DNS.*/ +/* ( 010311 masahiro minami) */ +static pascal void OpenDNSNotifier( + void* context, OTEventCode code, OTResult result, void* cookie ) +{ + switch( code ) + { + case T_OPENCOMPLETE: + // DNS is ready now. + if( result == kOTNoError ) + { + dnsStatus.dns = (InetSvcRef)cookie; + dnsStatus.stat = dnsReady; + } + else + { + SDLNet_SetError("T_DNRSTRINGTOADDRCOMPLETE event returned an error"); + dnsStatus.dns = NULL; + dnsStatus.stat = dnsError; + } + break; + case T_DNRSTRINGTOADDRCOMPLETE: + // DNR resolved the name to address + // WORK IN PROGRESS (TODO ) + dnsStatus.stat = dnsResolved; + break; + default: + if( result != kOTNoError ) + dnsStatus.stat = dnsError; + } + // Is there anything else to be done here ??? + // ( 010311 masahiro minami ) + // (TODO) +} + +/* Local functions for initializing and cleaning up the DNS resolver */ +static int OpenDNS(void) +{ + int retval; + OSStatus status; + + retval = 0; + status = OTAsyncOpenInternetServices( + kDefaultInternetServicesPath, 0, OpenDNSNotifier, NULL); + if ( status == noErr ) { + InetInterfaceInfo info; + + dnsStatus.stat = dnsNotReady; + + while( dnsStatus.stat != dnsError && dnsStatus.dns == NULL) + { + // what's to be done ? Yield ? WaitNextEvent ? or what ? + // ( 010311 masahiro minami ) + //YieldToAnyThread(); + } + /* Get the address of the local system - + What should it be if ethernet is off? + */ + OTInetGetInterfaceInfo(&info, kDefaultInetInterface); + OTlocalhost = info.fAddress; + } else { + SDLNet_SetError("Unable to open DNS handle"); + retval = status; + } + + return(retval); +} + +static void CloseDNS(void) +{ + if ( dnsStatus.dns ) { + OTCloseProvider(dnsStatus.dns); + dnsStatus.dns = 0; + dnsStatus.stat = dnsNotReady; + } + + OTlocalhost = 0; +} + +/* Initialize/Cleanup the network API */ +int SDLNet_Init(void) +{ + OSStatus status; + int retval; + + dnsStatus.stat = dnsNotReady; + dnsStatus.dns = 0; + + + retval = 0; + if ( ! SDLNet_started ) { + status = InitOpenTransport(); + if ( status == noErr ) { + retval = OpenDNS(); + if ( retval < 0 ) { + SDLNet_Quit(); + } + } else { + SDLNet_SetError("Unable to initialize Open Transport"); + retval = status; + } + } + if ( retval == 0 ) { + ++SDLNet_started; + } + return(retval); +} + +void SDLNet_Quit(void) +{ + if ( SDLNet_started == 0 ) { + return; + } + if ( --SDLNet_started == 0 ) { + CloseDNS(); + CloseOpenTransport(); + } +} + +/* Resolve a host name and port to an IP address in network form */ +int SDLNet_ResolveHost(IPaddress *address, const char *host, Uint16 port) +{ + int retval = 0; + + /* Perform the actual host resolution */ + if ( host == NULL ) { + address->host = INADDR_ANY; + } else { +/* int a[4]; + + address->host = INADDR_NONE; + + if ( sscanf(host, "%d.%d.%d.%d", a, a+1, a+2, a+3) == 4 ) { + if ( !(a[0] & 0xFFFFFF00) && !(a[1] & 0xFFFFFF00) && + !(a[2] & 0xFFFFFF00) && !(a[3] & 0xFFFFFF00) ) { + address->host = ((a[0] << 24) | + (a[1] << 16) | + (a[2] << 8) | a[3]); + if ( address->host == 0x7F000001 ) { + address->host = OTlocalhost; + } + } + } + + if ( address->host == INADDR_NONE ) {*/ + InetHostInfo hinfo; + + /* Check for special case - localhost */ + if ( strcmp(host, "localhost") == 0 ) + return(SDLNet_ResolveHost(address, "127.0.0.1", port)); + + /* Have OpenTransport resolve the hostname for us */ + retval = OTInetStringToAddress(dnsStatus.dns, (char *)host, &hinfo); + if (retval == noErr) { + while( dnsStatus.stat != dnsResolved ) + {WaitNextEvent(everyEvent, 0, 1, NULL );} + address->host = hinfo.addrs[0]; + } + //} + } + + address->port = SDL_SwapBE16(port); + + /* Return the status */ + return(retval); +} + +/* Resolve an ip address to a host name in canonical form. + If the ip couldn't be resolved, this function returns NULL, + otherwise a pointer to a static buffer containing the hostname + is returned. Note that this function is not thread-safe. +*/ +/* MacOS implementation by Roy Wood + */ +const char *SDLNet_ResolveIP(IPaddress *ip) +{ + if (ip != nil) + { + InetHost theIP; + static InetDomainName theInetDomainName; + OSStatus theOSStatus; + + + /* Default result will be null string */ + + theInetDomainName[0] = '\0'; + + + /* Do a reverse DNS lookup */ + + theIP = ip->host; + + theOSStatus = OTInetAddressToName(dnsStatus.dns,theIP,theInetDomainName); + + /* If successful, return the result */ + + if (theOSStatus == kOTNoError) + { + while( dnsStatus.stat != dnsResolved ) + { /*should we yield or what ? */ } + return(theInetDomainName); + } + } + + SDLNet_SetError("Can't perform reverse DNS lookup"); + + return(NULL); +} + +#else /* !MACOS_OPENTRANSPORT */ + +#ifndef __USE_W32_SOCKETS +#include +#endif + +/* Initialize/Cleanup the network API */ +int SDLNet_Init(void) +{ + if ( !SDLNet_started ) { +#ifdef __USE_W32_SOCKETS + /* Start up the windows networking */ + WORD version_wanted = MAKEWORD(1,1); + WSADATA wsaData; + + if ( WSAStartup(version_wanted, &wsaData) != 0 ) { + SDLNet_SetError("Couldn't initialize Winsock 1.1\n"); + return(-1); + } +#else + /* SIGPIPE is generated when a remote socket is closed */ + void (*handler)(int); + handler = signal(SIGPIPE, SIG_IGN); + if ( handler != SIG_DFL ) { + signal(SIGPIPE, handler); + } +#endif + } + ++SDLNet_started; + return(0); +} +void SDLNet_Quit(void) +{ + if ( SDLNet_started == 0 ) { + return; + } + if ( --SDLNet_started == 0 ) { +#ifdef __USE_W32_SOCKETS + /* Clean up windows networking */ + if ( WSACleanup() == SOCKET_ERROR ) { + if ( WSAGetLastError() == WSAEINPROGRESS ) { + WSACancelBlockingCall(); + WSACleanup(); + } + } +#else + /* Restore the SIGPIPE handler */ + void (*handler)(int); + handler = signal(SIGPIPE, SIG_DFL); + if ( handler != SIG_IGN ) { + signal(SIGPIPE, handler); + } +#endif + } +} + +/* Resolve a host name and port to an IP address in network form */ +int SDLNet_ResolveHost(IPaddress *address, const char *host, Uint16 port) +{ + int retval = 0; + + /* Perform the actual host resolution */ + if ( host == NULL ) { + address->host = INADDR_ANY; + } else { + address->host = inet_addr(host); + if ( address->host == INADDR_NONE ) { + struct hostent *hp; + + hp = gethostbyname(host); + if ( hp ) { + memcpy(&address->host,hp->h_addr,hp->h_length); + } else { + retval = -1; + } + } + } + address->port = SDL_SwapBE16(port); + + /* Return the status */ + return(retval); +} + +/* Resolve an ip address to a host name in canonical form. + If the ip couldn't be resolved, this function returns NULL, + otherwise a pointer to a static buffer containing the hostname + is returned. Note that this function is not thread-safe. +*/ +/* Written by Miguel Angel Blanch. + * Main Programmer of Arianne RPG. + * http://come.to/arianne_rpg + */ +const char *SDLNet_ResolveIP(IPaddress *ip) +{ + struct hostent *hp; + + hp = gethostbyaddr((char *)&ip->host, 4, AF_INET); + if ( hp != NULL ) { + return hp->h_name; + } + return NULL; +} + +#endif /* MACOS_OPENTRANSPORT */ + +#if !SDL_DATA_ALIGNED /* function versions for binary compatibility */ + +/* Write a 16 bit value to network packet buffer */ +#undef SDLNet_Write16 +void SDLNet_Write16(Uint16 value, void *areap) +{ + (*(Uint16 *)(areap) = SDL_SwapBE16(value)); +} + +/* Write a 32 bit value to network packet buffer */ +#undef SDLNet_Write32 +void SDLNet_Write32(Uint32 value, void *areap) +{ + *(Uint32 *)(areap) = SDL_SwapBE32(value); +} + +/* Read a 16 bit value from network packet buffer */ +#undef SDLNet_Read16 +Uint16 SDLNet_Read16(void *areap) +{ + return (SDL_SwapBE16(*(Uint16 *)(areap))); +} + +/* Read a 32 bit value from network packet buffer */ +#undef SDLNet_Read32 +Uint32 SDLNet_Read32(void *areap) +{ + return (SDL_SwapBE32(*(Uint32 *)(areap))); +} + +#endif /* !SDL_DATA_ALIGNED */ + + +#ifdef USE_GUSI_SOCKETS + +/* Configure Socket Factories */ + +void GUSISetupFactories() +{ + GUSIwithInetSockets(); +} + +/* Configure File Devices */ + +void GUSISetupDevices() +{ + return; +} + +#endif /* USE_GUSI_SOCKETS */ diff --git a/alienblaster/project/jni/sdl_net/SDLnetTCP.c b/alienblaster/project/jni/sdl_net/SDLnetTCP.c new file mode 100644 index 000000000..ad6fe4ee8 --- /dev/null +++ b/alienblaster/project/jni/sdl_net/SDLnetTCP.c @@ -0,0 +1,953 @@ +/* + SDL_net: An example cross-platform network library for use with SDL + Copyright (C) 1997-2004 Sam Lantinga + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + Sam Lantinga + slouken@libsdl.org +*/ + +/* $Id: SDLnetTCP.c 3280 2007-07-15 05:55:42Z slouken $ */ + +#include "SDLnetsys.h" +#include "SDL_net.h" + +/* The network API for TCP sockets */ + +/* Since the UNIX/Win32/BeOS code is so different from MacOS, + we'll just have two completely different sections here. +*/ + +#ifdef MACOS_OPENTRANSPORT + +#include +#include +#include +#include +#include + +struct _TCPsocket { + int ready; + SOCKET channel; + + // These are taken from GUSI interface. + // I'm not sure if it's really necessary here yet + // ( masahiro minami ) + // ( 01/02/19 ) + OTEventCode curEvent; + OTEventCode newEvent; + OTEventCode event; + OTEventCode curCompletion; + OTEventCode newCompletion; + OTEventCode completion; + OSStatus error; + TEndpointInfo info; + Boolean readShutdown; + Boolean writeShutdown; + Boolean connected; + OTConfigurationRef config; // Master configuration. you can clone this. + TCPsocket nextListener; + // ( end of new members --- masahiro minami + + IPaddress remoteAddress; + IPaddress localAddress; + int sflag; + + // Maybe we don't need this---- it's from original SDL_net + // (masahiro minami) + // ( 01/02/20 ) + int rcvdPassConn; +}; + +// To be used in WaitNextEvent() here and there.... +// (010311 masahiro minami) +EventRecord macEvent; + +#if TARGET_API_MAC_CARBON +/* for Carbon */ +OTNotifyUPP notifier; +#endif + +/* Input: ep - endpointref on which to negotiate the option + enableReuseIPMode - desired option setting - true/false + Return: kOTNoError indicates that the option was successfully negotiated + OSStatus is an error if < 0, otherwise, the status field is + returned and is > 0. + + IMPORTANT NOTE: The endpoint is assumed to be in synchronous more, otherwise + this code will not function as desired +*/ + +/* +NOTE: As this version is written async way, we don't use this function... +(010526) masahiro minami +*/ +/* +OSStatus DoNegotiateIPReuseAddrOption(EndpointRef ep, Boolean enableReuseIPMode) + +{ + UInt8 buf[kOTFourByteOptionSize]; // define buffer for fourByte Option size + TOption* opt; // option ptr to make items easier to access + TOptMgmt req; + TOptMgmt ret; + OSStatus err; + + if (!OTIsSynchronous(ep)) + { + return (-1); + } + opt = (TOption*)buf; // set option ptr to buffer + req.opt.buf = buf; + req.opt.len = sizeof(buf); + req.flags = T_NEGOTIATE; // negotiate for option + + ret.opt.buf = buf; + ret.opt.maxlen = kOTFourByteOptionSize; + + opt->level = INET_IP; // dealing with an IP Level function + opt->name = IP_REUSEADDR; + opt->len = kOTFourByteOptionSize; + opt->status = 0; + *(UInt32*)opt->value = enableReuseIPMode; // set the desired option level, true or false + + err = OTOptionManagement(ep, &req, &ret); + + // if no error then return the option status value + if (err == kOTNoError) + { + if (opt->status != T_SUCCESS) + err = opt->status; + else + err = kOTNoError; + } + + return err; +} +*/ + +/* A helper function for Mac OpenTransport support*/ +// This function is a complete copy from GUSI +// ( masahiro minami ) +// ( 01/02/19 ) +static __inline__ Uint32 CompleteMask(OTEventCode code) +{ + return 1 << (code & 0x1F); +} + +/* Notifier for async OT calls */ +static pascal void AsyncTCPNotifier( TCPsocket sock, OTEventCode code, + OTResult result, void* cookie ) +{ + +#ifdef DEBUG_NET + printf("AsyncTCPNotifier got an event : 0x%8.8x\n", code ); +#endif + + switch( code & 0x7f000000L) + { + case 0: + sock->newEvent |= code; + result = 0; + break; + case kCOMPLETEEVENT: + if(!(code & 0x00FFFFE0 )) + sock->newCompletion |= CompleteMask( code ); + if( code == T_OPENCOMPLETE ) + sock->channel = (SOCKET)(cookie); + break; + default: + if( code != kOTProviderWillClose ) + result = 0; + } + // Do we need these ???? TODO + // sock->SetAsyncMacError( result ); + // sock->Wakeup(); +} + +/* Retrieve OT event */ +// This function is taken from GUSI interface. +// ( 01/02/19 masahiro minami ) +static void AsyncTCPPopEvent( TCPsocket sock ) +{ + // Make sure OT calls are not interrupted + // Not sure if we really need this. + OTEnterNotifier( sock->channel ); + + sock->event |= (sock->curEvent = sock->newEvent ); + sock->completion |= ( sock->curCompletion = sock->newCompletion ); + sock->newEvent = sock->newCompletion = 0; + + OTLeaveNotifier( sock->channel ); + + if( sock->curEvent & T_UDERR) + { + // We just clear the error. + // Should we feed this back to users ? + // (TODO ) + OTRcvUDErr( sock->channel, NULL ); + +#ifdef DEBUG_NET + printf("AsyncTCPPopEvent T_UDERR recognized"); +#endif + } + + // Remote is disconnecting... + if( sock->curEvent & ( T_DISCONNECT | T_ORDREL )) + { + sock->readShutdown = true; + } + + if( sock->curEvent &T_CONNECT ) + { + // Ignore the info of remote (second parameter). + // Shoule we care ? + // (TODO) + OTRcvConnect( sock->channel, NULL ); + sock->connected = 1; + } + + if( sock->curEvent & T_ORDREL ) + { + OTRcvOrderlyDisconnect( sock->channel ); + } + + if( sock->curEvent & T_DISCONNECT ) + { + OTRcvDisconnect( sock->channel, NULL ); + } + + // Do we need to ? + // (masahiro minami) + //YieldToAnyThread(); +} + +/* Create a new TCPsocket */ +// Because TCPsocket structure gets bigger and bigger, +// I think we'd better have a constructor function and delete function. +// ( 01/02/25 masahiro minami ) +static TCPsocket AsyncTCPNewSocket() +{ + TCPsocket sock; + + sock = (TCPsocket)malloc(sizeof(*sock)); + if ( sock == NULL ) { + SDLNet_SetError("Out of memory"); + return NULL; + } + + sock->newEvent = 0; + sock->event = 0; + sock->curEvent = 0; + sock->newCompletion = 0; + sock->completion = 0; + sock->curCompletion = 0; + //sock->info = NULL; + sock->readShutdown = sock->writeShutdown = sock->connected = false; + sock->error = 0; + sock->config = NULL; + sock->nextListener = NULL; + sock->sflag = 0; + return sock; +} + +// hmmm.... do we need this ??? +// ( 01/02/25 masahiro minami) +static void AsycnTCPDeleteSocket( TCPsocket sock ) +{ + SDLNet_TCP_Close( sock ); +} +/* Open a TCP network socket + If 'remote' is NULL, this creates a local server socket on the given port, + otherwise a TCP connection to the remote host and port is attempted. + The newly created socket is returned, or NULL if there was an error. + + ( re-written by masahiro minami + Now endpoint is created in Async mode. + 01/02/20 ) +*/ +TCPsocket SDLNet_TCP_Open(IPaddress *ip) +{ + EndpointRef dummy = NULL; + + TCPsocket sock = AsyncTCPNewSocket(); + if( ! sock) + return NULL; + + // Determin whether bind locally, or connect to remote + if ( (ip->host != INADDR_NONE) && (ip->host != INADDR_ANY) ) + { + // ######## Connect to remote + OTResult stat; + InetAddress inAddr; + TBind bindReq; + + // Open endpoint + sock->error = OTAsyncOpenEndpoint( + OTCreateConfiguration(kTCPName), NULL, &(sock->info), + (OTNotifyProcPtr)(AsyncTCPNotifier), + sock ); + + AsyncTCPPopEvent( sock ); + while( !sock->error && !( sock->completion & CompleteMask(T_OPENCOMPLETE))) + { + //SetThreadState( kCurrentThreadID, kReadyThreadState, kNoThreadID ); + //YieldToAnyThread(); + //WaitNextEvent(everyEvent, &macEvent, 1, NULL); + AsyncTCPPopEvent( sock ); + } + + if( !sock->channel ) + { + SDLNet_SetError("OTAsyncOpenEndpoint failed --- client socket could not be opened"); + goto error_return; + } + + // Set blocking mode + // I'm not sure if this is a good solution.... + // Check out Apple's sample code, OT Virtual Server + // ( 010314 masahiro minami) + + sock->error = OTSetBlocking( sock->channel ); + if( sock->error != kOTNoError ) + { + SDLNet_SetError("OTSetBlocking() returned an error"); + goto error_return; + } + + // Bind the socket + OTInitInetAddress(&inAddr, 0, 0 ); + bindReq.addr.len = sizeof( InetAddress ); + bindReq.addr.buf = (unsigned char*)&inAddr; + bindReq.qlen = 0; + + sock->error = OTBind( sock->channel, &bindReq, NULL ); + AsyncTCPPopEvent(sock); + while( !sock->error && !( sock->completion & CompleteMask(T_BINDCOMPLETE))) + { + //YieldToAnyThread(); + //WaitNextEvent(everyEvent, &macEvent, 1, NULL); + AsyncTCPPopEvent(sock); + } + + + switch( stat = OTGetEndpointState( sock->channel )) + { + InetAddress inAddr; + TCall sndCall; + OTResult res; + + case T_OUTCON: + SDLNet_SetError("SDLNet_Open() failed -- T_OUTCON"); + goto error_return; + break; + case T_IDLE: + sock->readShutdown = false; + sock->writeShutdown = false; + sock->event &=~T_CONNECT; + + OTMemzero(&sndCall, sizeof(TCall)); + OTInitInetAddress(&inAddr, ip->port, ip->host ); + sndCall.addr.len = sizeof(InetAddress); + sndCall.addr.buf = (unsigned char*)&inAddr; + sock->connected = 0; + res = OTConnect( sock->channel, &sndCall, NULL ); + AsyncTCPPopEvent(sock); + while( sock->error == kOTNoDataErr || !sock->connected ) + AsyncTCPPopEvent(sock); + break; + default: + // What's to be done ? (TODO) + SDLNet_SetError("SDLNet_TCP_Open() failed -- EndpointState not good"); + goto error_return; + + } + if( !(sock->event & (T_CONNECT|T_DISCONNECT))) + goto error_return; + + AsyncTCPPopEvent( sock ); + while( !(sock->event & (T_CONNECT|T_DISCONNECT))) + { + AsyncTCPPopEvent( sock ); + } + // OTConnect successfull + if( sock->event & T_CONNECT) + { + sock->remoteAddress.host = inAddr.fHost; + sock->remoteAddress.port = inAddr.fPort; + sock->sflag = false; + } + else + { + // OTConnect failed + sock->event &= ~T_DISCONNECT; + goto error_return; + } + } + else + { + // ######## Bind locally + TBind bindReq; + InetAddress inAddr; + + // First, get InetInterfaceInfo. + // I don't search for all of them. + // Does that matter ? + + sock->error = OTAsyncOpenEndpoint( + OTCreateConfiguration("tilisten, tcp"), NULL, &(sock->info), + (OTNotifyProcPtr)(AsyncTCPNotifier), + sock); + AsyncTCPPopEvent( sock ); + while( !sock->error && !( sock->completion & CompleteMask( T_OPENCOMPLETE))) + { + AsyncTCPPopEvent( sock ); + } + + if( ! sock->channel ) + { + SDLNet_SetError("OTAsyncOpenEndpoint failed --- server socket could not be opened"); + goto error_return; + } + + // Create a master OTConfiguration + sock->config = OTCreateConfiguration(kTCPName); + if( ! sock->config ) + { + SDLNet_SetError("Could not create master OTConfiguration"); + goto error_return; + } + + // Bind the socket + OTInitInetAddress(&inAddr, ip->port, 0 ); + inAddr.fAddressType = AF_INET; + bindReq.addr.len = sizeof( InetAddress ); + bindReq.addr.buf = (unsigned char*)&inAddr; + bindReq.qlen = 35; // This number is NOT well considered. (TODO) + sock->localAddress.host = inAddr.fHost; + sock->localAddress.port = inAddr.fPort; + sock->sflag = true; + + sock->error = OTBind( sock->channel, &bindReq, NULL ); + AsyncTCPPopEvent(sock); + while( !sock->error && !( sock->completion & CompleteMask(T_BINDCOMPLETE))) + { + AsyncTCPPopEvent(sock); + } + if( sock->error != kOTNoError ) + { + SDLNet_SetError("Could not bind server socket"); + goto error_return; + } + + if( dummy ) + OTCloseProvider( dummy ); + + } + + sock->ready = 0; + return sock; + + error_return: + if( dummy ) + OTCloseProvider( dummy ); + SDLNet_TCP_Close( sock ); + return NULL; +} + +/* Accept an incoming connection on the given server socket. + The newly created socket is returned, or NULL if there was an error. +*/ +TCPsocket SDLNet_TCP_Accept(TCPsocket server) +{ + + /* Only server sockets can accept */ + if ( ! server->sflag ) { + SDLNet_SetError("Only server sockets can accept()"); + return(NULL); + } + server->ready = 0; + + /* Accept a new TCP connection on a server socket */ + { + InetAddress peer; + TCall peerinfo; + TCPsocket sock = NULL; + Boolean mustListen = false; + OTResult err; + + memset(&peerinfo, 0, (sizeof peerinfo )); + peerinfo.addr.buf = (Uint8 *) &peer; + peerinfo.addr.maxlen = sizeof(peer); + + while( mustListen || !sock ) + { + // OTListen + // We do NOT block ---- right thing ? (TODO) + err = OTListen( server->channel, &peerinfo ); + + if( err ) + goto error_return; + else + { + mustListen = false; + sock = AsyncTCPNewSocket(); + if( ! sock ) + goto error_return; + } + } + if( sock ) + { + // OTAsyncOpenEndpoint + server->error = OTAsyncOpenEndpoint( OTCloneConfiguration( server->config ), + NULL, &(sock->info), (OTNotifyProcPtr)AsyncTCPNotifier, sock ); + AsyncTCPPopEvent( sock ); + while( !sock->error && !( sock->completion & CompleteMask( T_OPENCOMPLETE))) + { + AsyncTCPPopEvent( sock ); + } + if( ! sock->channel ) + { + mustListen = false; + goto error_return; + } + + // OTAccept + server->completion &= ~(CompleteMask(T_ACCEPTCOMPLETE)); + server->error = OTAccept( server->channel, sock->channel, &peerinfo ); + AsyncTCPPopEvent( server ); + while( !(server->completion & CompleteMask(T_ACCEPTCOMPLETE))) + { + AsyncTCPPopEvent( server ); + } + + switch( server->error ) + { + case kOTLookErr: + switch( OTLook(server->channel )) + { + case T_LISTEN: + mustListen = true; + break; + case T_DISCONNECT: + goto error_return; + } + break; + case 0: + sock->nextListener = server->nextListener; + server->nextListener = sock; + sock->remoteAddress.host = peer.fHost; + sock->remoteAddress.port = peer.fPort; + return sock; + // accept successful + break; + default: + free( sock ); + } + } + sock->remoteAddress.host = peer.fHost; + sock->remoteAddress.port = peer.fPort; + sock->sflag = 0; + sock->ready = 0; + + /* The socket is ready */ + return(sock); + + // Error; close the socket and return + error_return: + SDLNet_TCP_Close(sock); + return(NULL); + } +} + +/* Get the IP address of the remote system associated with the socket. + If the socket is a server socket, this function returns NULL. +*/ +IPaddress *SDLNet_TCP_GetPeerAddress(TCPsocket sock) +{ + if ( sock->sflag ) { + return(NULL); + } + return(&sock->remoteAddress); +} + +/* Send 'len' bytes of 'data' over the non-server socket 'sock' + This function returns the actual amount of data sent. If the return value + is less than the amount of data sent, then either the remote connection was + closed, or an unknown socket error occurred. +*/ +int SDLNet_TCP_Send(TCPsocket sock, const void *datap, int len) +{ + const Uint8 *data = (const Uint8 *)datap; /* For pointer arithmetic */ + int sent, left; + + /* Server sockets are for accepting connections only */ + if ( sock->sflag ) { + SDLNet_SetError("Server sockets cannot send"); + return(-1); + } + + /* Keep sending data until it's sent or an error occurs */ + left = len; + sent = 0; + errno = 0; + do { + len = OTSnd(sock->channel, (void *)data, left, 0); + if (len == kOTFlowErr) + len = 0; + if ( len > 0 ) { + sent += len; + left -= len; + data += len; + } + // Do we need to ? + // ( masahiro minami ) + // (TODO) + //WaitNextEvent(everyEvent, &macEvent, 1, NULL); + //AsyncTCPPopEvent(sock); + } while ( (left > 0) && (len > 0) ); + + return(sent); +} + +/* Receive up to 'maxlen' bytes of data over the non-server socket 'sock', + and store them in the buffer pointed to by 'data'. + This function returns the actual amount of data received. If the return + value is less than or equal to zero, then either the remote connection was + closed, or an unknown socket error occurred. +*/ +int SDLNet_TCP_Recv(TCPsocket sock, void *data, int maxlen) +{ + int len = 0; + OSStatus res; + /* Server sockets are for accepting connections only */ + if ( sock->sflag ) { + SDLNet_SetError("Server sockets cannot receive"); + return(-1); + } + + do + { + res = OTRcv(sock->channel, data, maxlen-len, 0); + if (res > 0) { + len = res; + } + +#ifdef DEBUG_NET + if ( res != kOTNoDataErr ) + printf("SDLNet_TCP_Recv received ; %d\n", res ); +#endif + + AsyncTCPPopEvent(sock); + if( res == kOTLookErr ) + { + res = OTLook(sock->channel ); + continue; + } + } while ( (len == 0) && (res == kOTNoDataErr) ); + + sock->ready = 0; + if ( len == 0 ) { /* Open Transport error */ +#ifdef DEBUG_NET + printf("Open Transport error: %d\n", res); +#endif + return(-1); + } + return(len); +} + +/* Close a TCP network socket */ +void SDLNet_TCP_Close(TCPsocket sock) +{ + if ( sock != NULL ) { + if ( sock->channel != INVALID_SOCKET ) { + //closesocket(sock->channel); + OTSndOrderlyDisconnect( sock->channel ); + } + free(sock); + } +} + +#else /* !MACOS_OPENTRANSPORT */ + +struct _TCPsocket { + int ready; + SOCKET channel; + IPaddress remoteAddress; + IPaddress localAddress; + int sflag; +}; + +/* Open a TCP network socket + If 'remote' is NULL, this creates a local server socket on the given port, + otherwise a TCP connection to the remote host and port is attempted. + The newly created socket is returned, or NULL if there was an error. +*/ +TCPsocket SDLNet_TCP_Open(IPaddress *ip) +{ + TCPsocket sock; + struct sockaddr_in sock_addr; + + /* Allocate a TCP socket structure */ + sock = (TCPsocket)malloc(sizeof(*sock)); + if ( sock == NULL ) { + SDLNet_SetError("Out of memory"); + goto error_return; + } + + /* Open the socket */ + sock->channel = socket(AF_INET, SOCK_STREAM, 0); + if ( sock->channel == INVALID_SOCKET ) { + SDLNet_SetError("Couldn't create socket"); + goto error_return; + } + + /* Connect to remote, or bind locally, as appropriate */ + if ( (ip->host != INADDR_NONE) && (ip->host != INADDR_ANY) ) { + + // ######### Connecting to remote + + memset(&sock_addr, 0, sizeof(sock_addr)); + sock_addr.sin_family = AF_INET; + sock_addr.sin_addr.s_addr = ip->host; + sock_addr.sin_port = ip->port; + + /* Connect to the remote host */ + if ( connect(sock->channel, (struct sockaddr *)&sock_addr, + sizeof(sock_addr)) == SOCKET_ERROR ) { + SDLNet_SetError("Couldn't connect to remote host"); + goto error_return; + } + sock->sflag = 0; + } else { + + // ########## Binding locally + + memset(&sock_addr, 0, sizeof(sock_addr)); + sock_addr.sin_family = AF_INET; + sock_addr.sin_addr.s_addr = INADDR_ANY; + sock_addr.sin_port = ip->port; + +/* + * Windows gets bad mojo with SO_REUSEADDR: + * http://www.devolution.com/pipermail/sdl/2005-September/070491.html + * --ryan. + */ +#ifndef WIN32 + /* allow local address reuse */ + { int yes = 1; + setsockopt(sock->channel, SOL_SOCKET, SO_REUSEADDR, (char*)&yes, sizeof(yes)); + } +#endif + + /* Bind the socket for listening */ + if ( bind(sock->channel, (struct sockaddr *)&sock_addr, + sizeof(sock_addr)) == SOCKET_ERROR ) { + SDLNet_SetError("Couldn't bind to local port"); + goto error_return; + } + if ( listen(sock->channel, 5) == SOCKET_ERROR ) { + SDLNet_SetError("Couldn't listen to local port"); + goto error_return; + } + + /* Set the socket to non-blocking mode for accept() */ +#if defined(__BEOS__) && defined(SO_NONBLOCK) + /* On BeOS r5 there is O_NONBLOCK but it's for files only */ + { + long b = 1; + setsockopt(sock->channel, SOL_SOCKET, SO_NONBLOCK, &b, sizeof(b)); + } +#elif defined(O_NONBLOCK) + { + fcntl(sock->channel, F_SETFL, O_NONBLOCK); + } +#elif defined(WIN32) + { + unsigned long mode = 1; + ioctlsocket (sock->channel, FIONBIO, &mode); + } +#elif defined(__OS2__) + { + int dontblock = 1; + ioctl(sock->channel, FIONBIO, &dontblock); + } +#else +#warning How do we set non-blocking mode on other operating systems? +#endif + sock->sflag = 1; + } + sock->ready = 0; + +#ifdef TCP_NODELAY + /* Set the nodelay TCP option for real-time games */ + { int yes = 1; + setsockopt(sock->channel, IPPROTO_TCP, TCP_NODELAY, (char*)&yes, sizeof(yes)); + } +#endif /* TCP_NODELAY */ + + /* Fill in the channel host address */ + sock->remoteAddress.host = sock_addr.sin_addr.s_addr; + sock->remoteAddress.port = sock_addr.sin_port; + + /* The socket is ready */ + return(sock); + +error_return: + SDLNet_TCP_Close(sock); + return(NULL); +} + +/* Accept an incoming connection on the given server socket. + The newly created socket is returned, or NULL if there was an error. +*/ +TCPsocket SDLNet_TCP_Accept(TCPsocket server) +{ + TCPsocket sock; + struct sockaddr_in sock_addr; + int sock_alen; + + /* Only server sockets can accept */ + if ( ! server->sflag ) { + SDLNet_SetError("Only server sockets can accept()"); + return(NULL); + } + server->ready = 0; + + /* Allocate a TCP socket structure */ + sock = (TCPsocket)malloc(sizeof(*sock)); + if ( sock == NULL ) { + SDLNet_SetError("Out of memory"); + goto error_return; + } + + /* Accept a new TCP connection on a server socket */ + sock_alen = sizeof(sock_addr); + sock->channel = accept(server->channel, (struct sockaddr *)&sock_addr, +#ifdef USE_GUSI_SOCKETS + (unsigned int *)&sock_alen); +#else + &sock_alen); +#endif + if ( sock->channel == SOCKET_ERROR ) { + SDLNet_SetError("accept() failed"); + goto error_return; + } +#ifdef WIN32 + { + /* passing a zero value, socket mode set to block on */ + unsigned long mode = 0; + ioctlsocket (sock->channel, FIONBIO, &mode); + } +#elif defined(O_NONBLOCK) + { + int flags = fcntl(sock->channel, F_GETFL, 0); + fcntl(sock->channel, F_SETFL, flags & ~O_NONBLOCK); + } +#endif /* WIN32 */ + sock->remoteAddress.host = sock_addr.sin_addr.s_addr; + sock->remoteAddress.port = sock_addr.sin_port; + + sock->sflag = 0; + sock->ready = 0; + + /* The socket is ready */ + return(sock); + +error_return: + SDLNet_TCP_Close(sock); + return(NULL); +} + +/* Get the IP address of the remote system associated with the socket. + If the socket is a server socket, this function returns NULL. +*/ +IPaddress *SDLNet_TCP_GetPeerAddress(TCPsocket sock) +{ + if ( sock->sflag ) { + return(NULL); + } + return(&sock->remoteAddress); +} + +/* Send 'len' bytes of 'data' over the non-server socket 'sock' + This function returns the actual amount of data sent. If the return value + is less than the amount of data sent, then either the remote connection was + closed, or an unknown socket error occurred. +*/ +int SDLNet_TCP_Send(TCPsocket sock, const void *datap, int len) +{ + const Uint8 *data = (const Uint8 *)datap; /* For pointer arithmetic */ + int sent, left; + + /* Server sockets are for accepting connections only */ + if ( sock->sflag ) { + SDLNet_SetError("Server sockets cannot send"); + return(-1); + } + + /* Keep sending data until it's sent or an error occurs */ + left = len; + sent = 0; + errno = 0; + do { + len = send(sock->channel, (const char *) data, left, 0); + if ( len > 0 ) { + sent += len; + left -= len; + data += len; + } + } while ( (left > 0) && ((len > 0) || (errno == EINTR)) ); + + return(sent); +} + +/* Receive up to 'maxlen' bytes of data over the non-server socket 'sock', + and store them in the buffer pointed to by 'data'. + This function returns the actual amount of data received. If the return + value is less than or equal to zero, then either the remote connection was + closed, or an unknown socket error occurred. +*/ +int SDLNet_TCP_Recv(TCPsocket sock, void *data, int maxlen) +{ + int len; + + /* Server sockets are for accepting connections only */ + if ( sock->sflag ) { + SDLNet_SetError("Server sockets cannot receive"); + return(-1); + } + + errno = 0; + do { + len = recv(sock->channel, (char *) data, maxlen, 0); + } while ( errno == EINTR ); + + sock->ready = 0; + return(len); +} + +/* Close a TCP network socket */ +void SDLNet_TCP_Close(TCPsocket sock) +{ + if ( sock != NULL ) { + if ( sock->channel != INVALID_SOCKET ) { + closesocket(sock->channel); + } + free(sock); + } +} + +#endif /* MACOS_OPENTRANSPORT */ diff --git a/alienblaster/project/jni/sdl_net/SDLnetUDP.c b/alienblaster/project/jni/sdl_net/SDLnetUDP.c new file mode 100644 index 000000000..d7268269e --- /dev/null +++ b/alienblaster/project/jni/sdl_net/SDLnetUDP.c @@ -0,0 +1,797 @@ +/* + SDL_net: An example cross-platform network library for use with SDL + Copyright (C) 1997-2004 Sam Lantinga + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + Sam Lantinga + slouken@libsdl.org +*/ + +/* $Id: SDLnetUDP.c 1192 2004-01-04 17:41:55Z slouken $ */ + +#include "SDLnetsys.h" +#include "SDL_net.h" +#ifdef MACOS_OPENTRANSPORT +#include +#endif + +struct _UDPsocket { + int ready; + SOCKET channel; + IPaddress address; + +#ifdef MACOS_OPENTRANSPORT + OTEventCode newEvent; + OTEventCode event; + OTEventCode curEvent; + OTEventCode newCompletion; + OTEventCode completion; + OTEventCode curCompletion; + TEndpointInfo info; + Boolean readShutdown; + Boolean writeShutdown; + OSStatus error; + OTConfigurationRef config; // Master configuration. you can clone this. +#endif /* MACOS_OPENTRANSPORT */ + + struct UDP_channel { + int numbound; + IPaddress address[SDLNET_MAX_UDPADDRESSES]; + } binding[SDLNET_MAX_UDPCHANNELS]; +}; + +#ifdef MACOS_OPENTRANSPORT + +/* A helper function for Mac OpenTransport support*/ +// This function is a complete copy from GUSI +// ( masahiro minami ) +// ( 01/02/19 ) +// +// I guess this function should be put in SDLnet.c +// ( 010315 masahiro minami) +// (TODO) +static __inline__ Uint32 CompleteMask(OTEventCode code) +{ + return 1 << (code & 0x1F); +} + +/* Notifier for async OT calls */ +// This function is completely same as AsyncTCPNotifier, +// except for the argument, UDPsocket / TCPsocket +// ( 010315 masahiro minami) +static pascal void AsyncUDPNotifier( UDPsocket sock, OTEventCode code, + OTResult result, void* cookie ) +{ + switch( code & 0x7f000000L) + { + case 0: + sock->newEvent |= code; + result = 0; + break; + case kCOMPLETEEVENT: + if(!(code & 0x00FFFFE0 )) + sock->newCompletion |= CompleteMask( code ); + if( code == T_OPENCOMPLETE ) + sock->channel = (SOCKET)(cookie); + break; + default: + if( code != kOTProviderWillClose ) + result = 0; + } + // Do we need these ???? TODO + // sock->SetAsyncMacError( result ); + // sock->Wakeup(); + + // Do we need to ? + //YieldToAnyThread(); +} + +/* Retrieve OT event */ +// This function is completely same as AsyncTCPPopEvent, +// except for the argument, UDPsocket / TCPsocket +// ( 010315 masahiro minami) +static void AsyncUDPPopEvent( UDPsocket sock ) +{ + // Make sure OT calls are not interrupted + // Not sure if we really need this. + OTEnterNotifier( sock->channel ); + + sock->event |= (sock->curEvent = sock->newEvent ); + sock->completion |= ( sock->curCompletion = sock->newCompletion ); + sock->newEvent = sock->newCompletion = 0; + + OTLeaveNotifier( sock->channel ); + + if( sock->curEvent & T_UDERR) + { + // We just clear the error. + // Should we feed this back to users ? + // (TODO ) + OTRcvUDErr( sock->channel, NULL ); + } + + // Remote is disconnecting... + if( sock->curEvent & ( T_DISCONNECT | T_ORDREL )) + { + sock->readShutdown = true; + } + + if( sock->curEvent &T_CONNECT) + { + // Ignore the info of remote (second parameter). + // Shoule we care ? + // (TODO) + OTRcvConnect( sock->channel, NULL ); + } + + if( sock->curEvent & T_ORDREL ) + { + OTRcvOrderlyDisconnect( sock->channel ); + } + + if( sock->curEvent & T_DISCONNECT ) + { + OTRcvDisconnect( sock->channel, NULL ); + } + + // Should we ?? + // (010318 masahiro minami + //YieldToAnyThread(); +} + +/* Create a new UDPsocket */ +// Because TCPsocket structure gets bigger and bigger, +// I think we'd better have a constructor function and delete function. +// ( 01/02/25 masahiro minami ) +/*static*/ UDPsocket AsyncUDPNewSocket() +{ + UDPsocket sock; + + sock = (UDPsocket)malloc(sizeof(*sock)); + if ( sock == NULL ) { + SDLNet_SetError("Out of memory"); + return NULL; + } + + sock->newEvent = 0; + sock->event = 0; + sock->curEvent = 0; + sock->newCompletion = 0; + sock->completion = 0; + sock->curCompletion = 0; + //sock->info = NULL; + sock->readShutdown = sock->writeShutdown = false; + sock->error = 0; + sock->config = NULL; + + return sock; +} + +#endif /* MACOS_OPENTRANSPORT */ + +/* Allocate/free a single UDP packet 'size' bytes long. + The new packet is returned, or NULL if the function ran out of memory. + */ +extern UDPpacket *SDLNet_AllocPacket(int size) +{ + UDPpacket *packet; + int error; + + + error = 1; + packet = (UDPpacket *)malloc(sizeof(*packet)); + if ( packet != NULL ) { + packet->maxlen = size; + packet->data = (Uint8 *)malloc(size); + if ( packet->data != NULL ) { + error = 0; + } + } + if ( error ) { + SDLNet_FreePacket(packet); + packet = NULL; + } + return(packet); +} +int SDLNet_ResizePacket(UDPpacket *packet, int newsize) +{ + Uint8 *newdata; + + newdata = (Uint8 *)malloc(newsize); + if ( newdata != NULL ) { + free(packet->data); + packet->data = newdata; + packet->maxlen = newsize; + } + return(packet->maxlen); +} +extern void SDLNet_FreePacket(UDPpacket *packet) +{ + if ( packet ) { + if ( packet->data ) + free(packet->data); + free(packet); + } +} + +/* Allocate/Free a UDP packet vector (array of packets) of 'howmany' packets, + each 'size' bytes long. + A pointer to the packet array is returned, or NULL if the function ran out + of memory. + */ +UDPpacket **SDLNet_AllocPacketV(int howmany, int size) +{ + UDPpacket **packetV; + + packetV = (UDPpacket **)malloc((howmany+1)*sizeof(*packetV)); + if ( packetV != NULL ) { + int i; + for ( i=0; ierror = OTAsyncOpenEndpoint( + OTCreateConfiguration(kUDPName),0, &(sock->info), + (OTNotifyProcPtr)AsyncUDPNotifier, sock ); + AsyncUDPPopEvent( sock ); + while( !sock->error && !( sock->completion & CompleteMask(T_OPENCOMPLETE))) + { + AsyncUDPPopEvent( sock ); + } + if( sock->error ) + { + SDLNet_SetError("Could not open UDP socket"); + goto error_return; + } + // Should we ?? + // (01/05/03 minami + OTSetBlocking( sock->channel ); + } +#else + sock->channel = socket(AF_INET, SOCK_DGRAM, 0); +#endif /* MACOS_OPENTRANSPORT */ + + if ( sock->channel == INVALID_SOCKET ) + { + SDLNet_SetError("Couldn't create socket"); + goto error_return; + } + +#ifdef MACOS_OPENTRANSPORT + { + InetAddress required, assigned; + TBind req_addr, assigned_addr; + OSStatus status; + InetInterfaceInfo info; + + memset(&assigned_addr, 0, sizeof(assigned_addr)); + assigned_addr.addr.maxlen = sizeof(assigned); + assigned_addr.addr.len = sizeof(assigned); + assigned_addr.addr.buf = (UInt8 *) &assigned; + + if ( port ) { + status = OTInetGetInterfaceInfo( &info, kDefaultInetInterface ); + if( status != kOTNoError ) + goto error_return; + OTInitInetAddress(&required, port, info.fAddress ); + req_addr.addr.maxlen = sizeof( required ); + req_addr.addr.len = sizeof( required ); + req_addr.addr.buf = (UInt8 *) &required; + + sock->error = OTBind(sock->channel, &req_addr, &assigned_addr); + } else { + sock->error = OTBind(sock->channel, nil, &assigned_addr ); + } + AsyncUDPPopEvent(sock); + + while( !sock->error && !(sock->completion & CompleteMask(T_BINDCOMPLETE))) + { + AsyncUDPPopEvent(sock); + } + if (sock->error != noErr) + { + SDLNet_SetError("Couldn't bind to local port, OTBind() = %d",(int) status); + goto error_return; + } + + sock->address.host = assigned.fHost; + sock->address.port = assigned.fPort; + +#ifdef DEBUG_NET + printf("UDP open host = %d, port = %d\n", assigned.fHost, assigned.fPort ); +#endif + } +#else + /* Bind locally, if appropriate */ + if ( port ) + { + struct sockaddr_in sock_addr; + memset(&sock_addr, 0, sizeof(sock_addr)); + sock_addr.sin_family = AF_INET; + sock_addr.sin_addr.s_addr = INADDR_ANY; + sock_addr.sin_port = SDL_SwapBE16(port); + + /* Bind the socket for listening */ + if ( bind(sock->channel, (struct sockaddr *)&sock_addr, + sizeof(sock_addr)) == SOCKET_ERROR ) { + SDLNet_SetError("Couldn't bind to local port"); + goto error_return; + } + /* Fill in the channel host address */ + sock->address.host = sock_addr.sin_addr.s_addr; + sock->address.port = sock_addr.sin_port; + } + +#ifdef SO_BROADCAST + /* Allow LAN broadcasts with the socket */ + { int yes = 1; + setsockopt(sock->channel, SOL_SOCKET, SO_BROADCAST, (char*)&yes, sizeof(yes)); + } +#endif +#endif /* MACOS_OPENTRANSPORT */ + + /* The socket is ready */ + + return(sock); + +error_return: +#ifdef MACOS_OPENTRANSPORT + if( dummy ) + OTCloseProvider( dummy ); +#endif + SDLNet_UDP_Close(sock); + + return(NULL); +} + +/* Verify that the channel is in the valid range */ +static int ValidChannel(int channel) +{ + if ( (channel < 0) || (channel >= SDLNET_MAX_UDPCHANNELS) ) { + SDLNet_SetError("Invalid channel"); + return(0); + } + return(1); +} + +/* Bind the address 'address' to the requested channel on the UDP socket. + If the channel is -1, then the first unbound channel will be bound with + the given address as it's primary address. + If the channel is already bound, this new address will be added to the + list of valid source addresses for packets arriving on the channel. + If the channel is not already bound, then the address becomes the primary + address, to which all outbound packets on the channel are sent. + This function returns the channel which was bound, or -1 on error. +*/ +int SDLNet_UDP_Bind(UDPsocket sock, int channel, IPaddress *address) +{ + struct UDP_channel *binding; + + if ( channel == -1 ) { + for ( channel=0; channel < SDLNET_MAX_UDPCHANNELS; ++channel ) { + binding = &sock->binding[channel]; + if ( binding->numbound < SDLNET_MAX_UDPADDRESSES ) { + break; + } + } + } else { + if ( ! ValidChannel(channel) ) { + return(-1); + } + binding = &sock->binding[channel]; + } + if ( binding->numbound == SDLNET_MAX_UDPADDRESSES ) { + SDLNet_SetError("No room for new addresses"); + return(-1); + } + binding->address[binding->numbound++] = *address; + return(channel); +} + +/* Unbind all addresses from the given channel */ +void SDLNet_UDP_Unbind(UDPsocket sock, int channel) +{ + if ( (channel >= 0) && (channel < SDLNET_MAX_UDPCHANNELS) ) { + sock->binding[channel].numbound = 0; + } +} + +/* Get the primary IP address of the remote system associated with the + socket and channel. + If the channel is not bound, this function returns NULL. + */ +IPaddress *SDLNet_UDP_GetPeerAddress(UDPsocket sock, int channel) +{ + IPaddress *address; + + address = NULL; + switch (channel) { + case -1: + /* Return the actual address of the socket */ + address = &sock->address; + break; + default: + /* Return the address of the bound channel */ + if ( ValidChannel(channel) && + (sock->binding[channel].numbound > 0) ) { + address = &sock->binding[channel].address[0]; + } + break; + } + return(address); +} + +/* Send a vector of packets to the the channels specified within the packet. + If the channel specified in the packet is -1, the packet will be sent to + the address in the 'src' member of the packet. + Each packet will be updated with the status of the packet after it has + been sent, -1 if the packet send failed. + This function returns the number of packets sent. +*/ +int SDLNet_UDP_SendV(UDPsocket sock, UDPpacket **packets, int npackets) +{ + int numsent, i, j; + struct UDP_channel *binding; + int status; +#ifndef MACOS_OPENTRANSPORT + int sock_len; + struct sockaddr_in sock_addr; + + /* Set up the variables to send packets */ + sock_len = sizeof(sock_addr); +#endif + + numsent = 0; + for ( i=0; ichannel < 0 ) + { +#ifdef MACOS_OPENTRANSPORT + TUnitData OTpacket; + InetAddress address; + + memset(&OTpacket, 0, sizeof(OTpacket)); + OTpacket.addr.buf = (Uint8 *)&address; + OTpacket.addr.len = (sizeof address); + OTpacket.udata.buf = packets[i]->data; + OTpacket.udata.len = packets[i]->len; + OTInitInetAddress(&address, packets[i]->address.port, packets[i]->address.host); +#ifdef DEBUG_NET + printf("Packet send address: 0x%8.8x:%d, length = %d\n", packets[i]->address.host, packets[i]->address.port, packets[i]->len); +#endif + + status = OTSndUData(sock->channel, &OTpacket); +#ifdef DEBUG_NET + printf("SDLNet_UDP_SendV OTSndUData return value is ;%d\n", status ); +#endif + + AsyncUDPPopEvent( sock ); + packets[i]->status = status; + + if (status == noErr) + { + ++numsent; + } +#else + sock_addr.sin_addr.s_addr = packets[i]->address.host; + sock_addr.sin_port = packets[i]->address.port; + sock_addr.sin_family = AF_INET; + status = sendto(sock->channel, + packets[i]->data, packets[i]->len, 0, + (struct sockaddr *)&sock_addr,sock_len); + if ( status >= 0 ) + { + packets[i]->status = status; + ++numsent; + } +#endif /* MACOS_OPENTRANSPORT */ + } + else + { + /* Send to each of the bound addresses on the channel */ +#ifdef DEBUG_NET + printf("SDLNet_UDP_SendV sending packet to channel = %d\n", packets[i]->channel ); +#endif + + binding = &sock->binding[packets[i]->channel]; + + for ( j=binding->numbound-1; j>=0; --j ) + { +#ifdef MACOS_OPENTRANSPORT + TUnitData OTpacket; + InetAddress address; + + OTInitInetAddress(&address, binding->address[j].port,binding->address[j].host); +#ifdef DEBUG_NET + printf("Packet send address: 0x%8.8x:%d, length = %d\n", binding->address[j].host, binding->address[j].port, packets[i]->len); +#endif + memset(&OTpacket, 0, sizeof(OTpacket)); + OTpacket.addr.buf = (Uint8 *)&address; + OTpacket.addr.len = (sizeof address); + OTpacket.udata.buf = packets[i]->data; + OTpacket.udata.len = packets[i]->len; + + status = OTSndUData(sock->channel, &OTpacket); +#ifdef DEBUG_NET + printf("SDLNet_UDP_SendV OTSndUData returne value is;%d\n", status ); +#endif + AsyncUDPPopEvent(sock); + packets[i]->status = status; + + if (status == noErr) + { + ++numsent; + } + +#else + sock_addr.sin_addr.s_addr = binding->address[j].host; + sock_addr.sin_port = binding->address[j].port; + sock_addr.sin_family = AF_INET; + status = sendto(sock->channel, + packets[i]->data, packets[i]->len, 0, + (struct sockaddr *)&sock_addr,sock_len); + if ( status >= 0 ) + { + packets[i]->status = status; + ++numsent; + } +#endif /* MACOS_OPENTRANSPORT */ + } + } + } + + return(numsent); +} + +int SDLNet_UDP_Send(UDPsocket sock, int channel, UDPpacket *packet) +{ + /* This is silly, but... */ + packet->channel = channel; + return(SDLNet_UDP_SendV(sock, &packet, 1)); +} + +/* Returns true if a socket is has data available for reading right now */ +static int SocketReady(SOCKET sock) +{ + int retval = 0; +#ifdef MACOS_OPENTRANSPORT + OTResult status; +#else + struct timeval tv; + fd_set mask; +#endif + +#ifdef MACOS_OPENTRANSPORT + //status = OTGetEndpointState(sock); + status = OTLook(sock); + if( status > 0 ) + retval = 1; + +/* switch( status ) + { +// case T_IDLE: + case T_DATAXFER: +// case T_INREL: + retval = 1; + break; + default: + OTCountDataBytes( sock, &numBytes ); + if( numBytes ) + retval = 1; + }*/ +#else + /* Check the file descriptors for available data */ + do { + errno = 0; + + /* Set up the mask of file descriptors */ + FD_ZERO(&mask); + FD_SET(sock, &mask); + + /* Set up the timeout */ + tv.tv_sec = 0; + tv.tv_usec = 0; + + /* Look! */ + retval = select(sock+1, &mask, NULL, NULL, &tv); + } while ( errno == EINTR ); +#endif /* MACOS_OPENTRANSPORT */ + + return(retval == 1); +} + +/* Receive a vector of pending packets from the UDP socket. + The returned packets contain the source address and the channel they arrived + on. If they did not arrive on a bound channel, the the channel will be set + to -1. + This function returns the number of packets read from the network, or -1 + on error. This function does not block, so can return 0 packets pending. +*/ +extern int SDLNet_UDP_RecvV(UDPsocket sock, UDPpacket **packets) +{ + int numrecv, i, j; + struct UDP_channel *binding; +#ifdef MACOS_OPENTRANSPORT + TUnitData OTpacket; + OTFlags flags; + InetAddress address; +#else + int sock_len; + struct sockaddr_in sock_addr; +#endif + + numrecv = 0; + while ( packets[numrecv] && SocketReady(sock->channel) ) + { + UDPpacket *packet; + + packet = packets[numrecv]; + +#ifdef MACOS_OPENTRANSPORT + memset(&OTpacket, 0, sizeof(OTpacket)); + OTpacket.addr.buf = (Uint8 *)&address; + OTpacket.addr.maxlen = (sizeof address); + OTpacket.udata.buf = packet->data; + OTpacket.udata.maxlen = packet->maxlen; + + packet->status = OTRcvUData(sock->channel, &OTpacket, &flags); +#ifdef DEBUG_NET + printf("Packet status: %d\n", packet->status); +#endif + AsyncUDPPopEvent(sock); + if (packet->status == noErr) + { + packet->len = OTpacket.udata.len; + packet->address.host = address.fHost; + packet->address.port = address.fPort; +#ifdef DEBUG_NET + printf("Packet address: 0x%8.8x:%d, length = %d\n", packet->address.host, packet->address.port, packet->len); +#endif + } +#else + sock_len = sizeof(sock_addr); + packet->status = recvfrom(sock->channel, + packet->data, packet->maxlen, 0, + (struct sockaddr *)&sock_addr, +#ifdef USE_GUSI_SOCKETS + (unsigned int *)&sock_len); +#else + &sock_len); +#endif + if ( packet->status >= 0 ) { + packet->len = packet->status; + packet->address.host = sock_addr.sin_addr.s_addr; + packet->address.port = sock_addr.sin_port; + } +#endif + if (packet->status >= 0) + { + packet->channel = -1; + + for (i=(SDLNET_MAX_UDPCHANNELS-1); i>=0; --i ) + { + binding = &sock->binding[i]; + + for ( j=binding->numbound-1; j>=0; --j ) + { + if ( (packet->address.host == binding->address[j].host) && + (packet->address.port == binding->address[j].port) ) + { + packet->channel = i; + goto foundit; /* break twice */ + } + } + } +foundit: + ++numrecv; + } + + else + { + packet->len = 0; + } + } + + sock->ready = 0; + + return(numrecv); +} + +/* Receive a single packet from the UDP socket. + The returned packet contains the source address and the channel it arrived + on. If it did not arrive on a bound channel, the the channel will be set + to -1. + This function returns the number of packets read from the network, or -1 + on error. This function does not block, so can return 0 packets pending. +*/ +int SDLNet_UDP_Recv(UDPsocket sock, UDPpacket *packet) +{ + UDPpacket *packets[2]; + + /* Receive a packet array of 1 */ + packets[0] = packet; + packets[1] = NULL; + return(SDLNet_UDP_RecvV(sock, packets)); +} + +/* Close a UDP network socket */ +extern void SDLNet_UDP_Close(UDPsocket sock) +{ + if ( sock != NULL ) + { + if ( sock->channel != INVALID_SOCKET ) + { +#ifdef MACOS_OPENTRANSPORT + OTUnbind(sock->channel); + OTCloseProvider(sock->channel); +#else + closesocket(sock->channel); +#endif /* MACOS_OPENTRANSPORT */ + } + + free(sock); + } +} + diff --git a/alienblaster/project/jni/sdl_net/SDLnetselect.c b/alienblaster/project/jni/sdl_net/SDLnetselect.c new file mode 100644 index 000000000..7f5b6aeba --- /dev/null +++ b/alienblaster/project/jni/sdl_net/SDLnetselect.c @@ -0,0 +1,226 @@ +/* + SDL_net: An example cross-platform network library for use with SDL + Copyright (C) 1997-2004 Sam Lantinga + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + Sam Lantinga + slouken@libsdl.org +*/ + +/* $Id: SDLnetselect.c 1192 2004-01-04 17:41:55Z slouken $ */ + +#include "SDLnetsys.h" +#include "SDL_net.h" + +/* The select() API for network sockets */ + +struct SDLNet_Socket { + int ready; + SOCKET channel; +#ifdef MACOS_OPENTRANSPORT + OTEventCode curEvent; +#endif +}; + +struct _SDLNet_SocketSet { + int numsockets; + int maxsockets; + struct SDLNet_Socket **sockets; +}; + +/* Allocate a socket set for use with SDLNet_CheckSockets() + This returns a socket set for up to 'maxsockets' sockets, or NULL if + the function ran out of memory. + */ +SDLNet_SocketSet SDLNet_AllocSocketSet(int maxsockets) +{ + struct _SDLNet_SocketSet *set; + int i; + + set = (struct _SDLNet_SocketSet *)malloc(sizeof(*set)); + if ( set != NULL ) { + set->numsockets = 0; + set->maxsockets = maxsockets; + set->sockets = (struct SDLNet_Socket **)malloc + (maxsockets*sizeof(*set->sockets)); + if ( set->sockets != NULL ) { + for ( i=0; isockets[i] = NULL; + } + } else { + free(set); + set = NULL; + } + } + return(set); +} + +/* Add a socket to a set of sockets to be checked for available data */ +int SDLNet_AddSocket(SDLNet_SocketSet set, SDLNet_GenericSocket sock) +{ + if ( sock != NULL ) { + if ( set->numsockets == set->maxsockets ) { + SDLNet_SetError("socketset is full"); + return(-1); + } + set->sockets[set->numsockets++] = (struct SDLNet_Socket *)sock; + } + return(set->numsockets); +} + +/* Remove a socket from a set of sockets to be checked for available data */ +int SDLNet_DelSocket(SDLNet_SocketSet set, SDLNet_GenericSocket sock) +{ + int i; + + if ( sock != NULL ) { + for ( i=0; inumsockets; ++i ) { + if ( set->sockets[i] == (struct SDLNet_Socket *)sock ) { + break; + } + } + if ( i == set->numsockets ) { + SDLNet_SetError("socket not found in socketset"); + return(-1); + } + --set->numsockets; + for ( ; inumsockets; ++i ) { + set->sockets[i] = set->sockets[i+1]; + } + } + return(set->numsockets); +} + +/* This function checks to see if data is available for reading on the + given set of sockets. If 'timeout' is 0, it performs a quick poll, + otherwise the function returns when either data is available for + reading, or the timeout in milliseconds has elapsed, which ever occurs + first. This function returns the number of sockets ready for reading, + or -1 if there was an error with the select() system call. +*/ +#ifdef MACOS_OPENTRANSPORT +int SDLNet_CheckSockets(SDLNet_SocketSet set, Uint32 timeout) +{ +Uint32 stop; +int numReady; + + /* Loop, polling the network devices */ + + stop = SDL_GetTicks() + timeout; + + do + { + OTResult status; + size_t numBytes; + int i; + + numReady = 0; + + for (i = set->numsockets-1;i >= 0;--i) + { + status = OTLook( set->sockets[i]->channel ); + if( status > 0 ) + { + switch( status ) + { + case T_UDERR: + OTRcvUDErr( set->sockets[i]->channel , nil); + break; + case T_DISCONNECT: + OTRcvDisconnect( set->sockets[i]->channel, nil ); + break; + case T_ORDREL: + OTRcvOrderlyDisconnect(set->sockets[i]->channel ); + break; + case T_CONNECT: + OTRcvConnect( set->sockets[i]->channel, nil ); + break; + + + default: + set->sockets[i]->ready = 1; + ++numReady; + } + } + else if( OTCountDataBytes(set->sockets[i]->channel, &numBytes ) != kOTNoDataErr ) + { + set->sockets[i]->ready = 1; + ++numReady; + } + else + set->sockets[i]->ready = 0; + } + + } while (!numReady && (SDL_GetTicks() < stop)); + + return(numReady); +} +#else +int SDLNet_CheckSockets(SDLNet_SocketSet set, Uint32 timeout) +{ + int i; + SOCKET maxfd; + int retval; + struct timeval tv; + fd_set mask; + + /* Find the largest file descriptor */ + maxfd = 0; + for ( i=set->numsockets-1; i>=0; --i ) { + if ( set->sockets[i]->channel > maxfd ) { + maxfd = set->sockets[i]->channel; + } + } + + /* Check the file descriptors for available data */ + do { + errno = 0; + + /* Set up the mask of file descriptors */ + FD_ZERO(&mask); + for ( i=set->numsockets-1; i>=0; --i ) { + FD_SET(set->sockets[i]->channel, &mask); + } + + /* Set up the timeout */ + tv.tv_sec = timeout/1000; + tv.tv_usec = (timeout%1000)*1000; + + /* Look! */ + retval = select(maxfd+1, &mask, NULL, NULL, &tv); + } while ( errno == EINTR ); + + /* Mark all file descriptors ready that have data available */ + if ( retval > 0 ) { + for ( i=set->numsockets-1; i>=0; --i ) { + if ( FD_ISSET(set->sockets[i]->channel, &mask) ) { + set->sockets[i]->ready = 1; + } + } + } + return(retval); +} +#endif /* MACOS_OPENTRANSPORT */ + +/* Free a set of sockets allocated by SDL_NetAllocSocketSet() */ +extern void SDLNet_FreeSocketSet(SDLNet_SocketSet set) +{ + if ( set ) { + free(set->sockets); + free(set); + } +} + diff --git a/alienblaster/project/jni/sdl_net/SDLnetsys.h b/alienblaster/project/jni/sdl_net/SDLnetsys.h new file mode 100644 index 000000000..f6c956f42 --- /dev/null +++ b/alienblaster/project/jni/sdl_net/SDLnetsys.h @@ -0,0 +1,84 @@ +/* + SDL_net: An example cross-platform network library for use with SDL + Copyright (C) 1997-2004 Sam Lantinga + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + Sam Lantinga + slouken@libsdl.org +*/ + +/* $Id: SDLnetsys.h 1720 2005-11-23 07:57:10Z icculus $ */ + +/* Include normal system headers */ +#include +#include +#include +#include + +#ifdef macintosh +#ifndef USE_GUSI_SOCKETS +#define MACOS_OPENTRANSPORT +//#error Open Transport driver is broken +#endif +#endif /* macintosh */ + +/* Include system network headers */ +#ifdef MACOS_OPENTRANSPORT +#include +#include +#else +#if defined(__WIN32__) || defined(WIN32) +#define __USE_W32_SOCKETS +#include +#else /* UNIX */ +#ifdef __OS2__ +#include +#include +#endif +#include +#include +#include +#include +#ifndef __BEOS__ +#include +#endif +#ifdef linux /* FIXME: what other platforms have this? */ +#include +#endif +#include +#include +#endif /* WIN32 */ +#endif /* Open Transport */ + +/* System-dependent definitions */ +#ifdef MACOS_OPENTRANSPORT +//#define closesocket OTCloseProvider +#define closesocket OTSndOrderlyDisconnect +#define SOCKET EndpointRef +#define INVALID_SOCKET kOTInvalidEndpointRef +#else +#ifndef __USE_W32_SOCKETS +#ifdef __OS2__ +#define closesocket soclose +#else /* !__OS2__ */ +#define closesocket close +#endif /* __OS2__ */ +#define SOCKET int +#define INVALID_SOCKET -1 +#define SOCKET_ERROR -1 +#endif /* __USE_W32_SOCKETS */ +#endif /* Open Transport */ +