diff --git a/sc2/ChangeLog b/sc2/ChangeLog index 00c1b665f..d70c47e27 100644 --- a/sc2/ChangeLog +++ b/sc2/ChangeLog @@ -1,4 +1,5 @@ Changes towards version 0.2: +- Lowered Drawable memory footprint, made Frame safer - Martin - Support for running without voice .ogg files present -PBlue - Slider should now work correctly everywhere -PhracturedBlue - Added 'nosound' driver and --sound=openal|mixsdl|none diff --git a/sc2/doc/devel/gfxlib b/sc2/doc/devel/gfxlib index 5ffef3633..82ce6b72e 100644 --- a/sc2/doc/devel/gfxlib +++ b/sc2/doc/devel/gfxlib @@ -43,14 +43,12 @@ display.h: defines a DISPLAY_INTERFACE and PDISPLAY_INTERFACE type (as well as a global _pCurDisplay). drawable.h: defines FRAME_DESC and DRAWABLE_DESC, and the pointer - types PFRAME_DESC and PDRAWABLE_DESC. FRAME_DESC includes - an element "DataOffs", which, horrifyingly, appears to be - a deliberate index past the end of the struct. Given a - PFRAME_DESC x, (void *)(x[x->DataOffs]) is a void pointer - castable to TFB_Image. Preliminary investigations into - this code make a conversion of DataOffs into a void - pointer both feasible and more efficient on modern - architectures.) + types PFRAME_DESC and PDRAWABLE_DESC. FRAME_DESC has a + TFB_Image pointer as a member. DRAWABLE_DESC currently + still uses a rather annoying technique where the last + member of a struct is a 1-element array, more memory than + that is actually allocated, and the array's bounds are + deliberately overflowed to get at multiple frames. font.h: defines FONT_DESC and PFONT_DESC. diff --git a/sc2/src/sc2code/libs/graphics/drawable.h b/sc2/src/sc2code/libs/graphics/drawable.h index 0e6d14012..422c389fa 100644 --- a/sc2/src/sc2code/libs/graphics/drawable.h +++ b/sc2/src/sc2code/libs/graphics/drawable.h @@ -20,6 +20,7 @@ #define _DRAWABLE_H #include +#include "tfb_draw.h" #define ValidPrimType(pt) ((pt)Object.Stamp.frame; - if (SrcFramePtr->DataOffs == 0) + if (!SrcFramePtr->image) { fprintf (stderr, "Non-existent image to blt()\n"); return; } - img = (TFB_Image *) ((BYTE *) SrcFramePtr + SrcFramePtr->DataOffs); + img = SrcFramePtr->image; if (TYPE_GET (_CurFramePtr->TypeIndexAndFlags) == SCREEN_DRAWABLE) { @@ -155,9 +155,7 @@ blt (PRECT pClipRect, PRIMITIVEPTR PrimPtr) { TFB_Image *dst_img; - dst_img = ((TFB_Image *) ((BYTE *) _CurFramePtr + - _CurFramePtr->DataOffs)); - + dst_img = _CurFramePtr->image; if (GetPrimType (PrimPtr) == STAMPFILL_PRIM) { @@ -218,8 +216,7 @@ fillrect_blt (PRECT pClipRect, PRIMITIVEPTR PrimPtr) SDL_Rect SDLRect; TFB_Image *img; - img = ((TFB_Image *) ((BYTE *) _CurFramePtr + - _CurFramePtr->DataOffs)); + img = _CurFramePtr->image; LockMutex (img->mutex); @@ -282,8 +279,7 @@ read_screen (PRECT lpRect, FRAMEPTR DstFramePtr) } else { - TFB_Image *img = (TFB_Image *) ((BYTE *) DstFramePtr + - DstFramePtr->DataOffs); + TFB_Image *img = DstFramePtr->image; TFB_DrawScreen_CopyToImage (img, lpRect, TFB_SCREEN_MAIN); } } @@ -292,40 +288,7 @@ static DRAWABLE alloc_image (COUNT NumFrames, DRAWABLE_TYPE DrawableType, CREATE_FLAGS flags, SIZE width, SIZE height) { - DWORD data_byte_count; - DRAWABLE Drawable; - - data_byte_count = 0; - if (flags & WANT_MASK) - data_byte_count += (DWORD) SCAN_WIDTH (width) * height; - if ((flags & WANT_PIXMAP) && DrawableType == RAM_DRAWABLE) - { - width = ((width << 1) + 3) & ~3; - data_byte_count += (DWORD) width * height; - } - - Drawable = AllocDrawable (NumFrames, data_byte_count * NumFrames); - if (Drawable) - { - if (DrawableType == RAM_DRAWABLE) - { - COUNT i; - DWORD data_offs; - DRAWABLEPTR DrawablePtr; - FRAMEPTR F; - - data_offs = sizeof (*F) * NumFrames; - DrawablePtr = LockDrawable (Drawable); - for (i = 0, F = &DrawablePtr->Frame[0]; i < NumFrames; ++i, ++F) - { - F->DataOffs = data_offs; - data_offs += data_byte_count - sizeof (*F); - } - UnlockDrawable (Drawable); - } - } - - return (Drawable); + return AllocDrawable (NumFrames, 0); } void (*func_array[]) (PRECT pClipRect, PRIMITIVEPTR PrimPtr) = diff --git a/sc2/src/sc2code/libs/graphics/sdl/3do_funcs.c b/sc2/src/sc2code/libs/graphics/sdl/3do_funcs.c index 8bdeb5b62..ef1b81ee2 100644 --- a/sc2/src/sc2code/libs/graphics/sdl/3do_funcs.c +++ b/sc2/src/sc2code/libs/graphics/sdl/3do_funcs.c @@ -227,9 +227,8 @@ _image_intersect (PIMAGE_BOX box1, PIMAGE_BOX box2, PRECT rect) Uint32 img1colourkey, img2colourkey; GetPixelFn getpixel1, getpixel2; - /* Image is (TFB_IMAGE*)(box1->FramePtr + box1->FramePtr->DataOffs) */ - img1 = ((TFB_Image*)((BYTE*)(box1->FramePtr) + box1->FramePtr->DataOffs))->NormalImg; - img2 = ((TFB_Image*)((BYTE*)(box2->FramePtr) + box2->FramePtr->DataOffs))->NormalImg; + img1 = (SDL_Surface *)box1->FramePtr->image->NormalImg; + img2 = (SDL_Surface *)box2->FramePtr->image->NormalImg; getpixel1 = getpixel_for(img1); getpixel2 = getpixel_for(img2); diff --git a/sc2/src/sc2code/libs/graphics/sdl/3do_getbody.c b/sc2/src/sc2code/libs/graphics/sdl/3do_getbody.c index ef0b3bbf3..e40beef63 100644 --- a/sc2/src/sc2code/libs/graphics/sdl/3do_getbody.c +++ b/sc2/src/sc2code/libs/graphics/sdl/3do_getbody.c @@ -56,11 +56,11 @@ process_image (FRAMEPTR FramePtr, SDL_Surface *img[], AniData *ani, int cel_ct) hx = ani[cel_ct].hotspot_x; hy = ani[cel_ct].hotspot_y; - FramePtr->DataOffs = (BYTE *)TFB_DrawImage_New (img[cel_ct]) - (BYTE *)FramePtr; + FramePtr->image = TFB_DrawImage_New (img[cel_ct]); - tfbimg = (TFB_Image *)((BYTE *)FramePtr + FramePtr->DataOffs); + tfbimg = FramePtr->image; tfbimg->colormap_index = ani[cel_ct].colormap_index; - img[cel_ct] = tfbimg->NormalImg; + img[cel_ct] = (SDL_Surface *)tfbimg->NormalImg; SetFrameHotSpot (FramePtr, MAKE_HOT_SPOT (hx, hy)); SetFrameBounds (FramePtr, img[cel_ct]->w, img[cel_ct]->h); @@ -123,8 +123,8 @@ process_font (FRAMEPTR FramePtr, SDL_Surface *img[], int cel_ct) img[cel_ct] = new_surf; - FramePtr->DataOffs = (BYTE *)TFB_DrawImage_New (img[cel_ct]) - (BYTE *)FramePtr; - img[cel_ct] = ((TFB_Image *)((BYTE *)FramePtr + FramePtr->DataOffs))->NormalImg; + FramePtr->image = TFB_DrawImage_New (img[cel_ct]); + img[cel_ct] = FramePtr->image->NormalImg; SetFrameHotSpot (FramePtr, MAKE_HOT_SPOT (hx, hy)); SetFrameBounds (FramePtr, img[cel_ct]->w, img[cel_ct]->h); @@ -144,10 +144,10 @@ FRAMEPTR stretch_frame (FRAMEPTR FramePtr, int neww, int newh, int destroy) NewFrame = CaptureDrawable ( CreateDrawable (type, (SIZE)neww, (SIZE)newh, 1) ); - tfbImg = (TFB_Image *)((BYTE *)(FramePtr) + FramePtr->DataOffs); + tfbImg = FramePtr->image; LockMutex (tfbImg->mutex); - src = tfbImg->NormalImg; - dst = ((TFB_Image *)((BYTE *)(NewFrame) + NewFrame->DataOffs))->NormalImg; + src = (SDL_Surface *)tfbImg->NormalImg; + dst = (SDL_Surface *)NewFrame->image->NormalImg; SDL_LockSurface (src); SDL_LockSurface (dst); @@ -172,9 +172,9 @@ void process_rgb_bmp (FRAMEPTR FramePtr, Uint32 *rgba, int maxx, int maxy) // convert 32-bit png font to indexed - tfbImg = (TFB_Image *)((BYTE *)(FramePtr) + FramePtr->DataOffs); + tfbImg = FramePtr->image; LockMutex (tfbImg->mutex); - img = tfbImg->NormalImg; + img = (SDL_Surface *)tfbImg->NormalImg; SDL_LockSurface (img); putpix = putpixel_for (img); @@ -192,9 +192,9 @@ void fill_frame_rgb (FRAMEPTR FramePtr, Uint32 color, int x0, int y0, int x, int TFB_Image *tfbImg; SDL_Rect rect; - tfbImg = (TFB_Image *)((BYTE *)(FramePtr) + FramePtr->DataOffs); + tfbImg = FramePtr->image; LockMutex (tfbImg->mutex); - img = tfbImg->NormalImg; + img = (SDL_Surface *)tfbImg->NormalImg; SDL_LockSurface (img); if (x0 == 0 && y0 == 0 && x == 0 && y == 0) SDL_FillRect(img, NULL, color); @@ -216,12 +216,12 @@ void arith_frame_blit (FRAMEPTR srcFrame, RECT *rsrc, FRAMEPTR dstFrame, RECT *r TFB_Image *srcImg, *dstImg; SDL_Surface *src, *dst; SDL_Rect srcRect, dstRect, *srp = NULL, *drp = NULL; - srcImg = (TFB_Image *)((BYTE *)(srcFrame) + srcFrame->DataOffs); - dstImg = (TFB_Image *)((BYTE *)(dstFrame) + dstFrame->DataOffs); + srcImg = srcFrame->image; + dstImg = dstFrame->image; LockMutex (srcImg->mutex); LockMutex (dstImg->mutex); - src = srcImg->NormalImg; - dst = dstImg->NormalImg; + src = (SDL_Surface *)srcImg->NormalImg; + dst = (SDL_Surface *)dstImg->NormalImg; if (rdst) { dstRect.x = rdst->corner.x; @@ -275,9 +275,9 @@ Uint32 **getpixelarray(FRAMEPTR FramePtr, int width, int height) GetPixelFn getpix; int x,y; - tfbImg = (TFB_Image *)((BYTE *)(FramePtr) + FramePtr->DataOffs); + tfbImg = FramePtr->image; LockMutex (tfbImg->mutex); - img = tfbImg->NormalImg; + img = (SDL_Surface *)tfbImg->NormalImg; SDL_LockSurface (img); getpix = getpixel_for (img); map=(Uint32 **)HMalloc (sizeof(Uint32 *) * (height + 1)); @@ -320,12 +320,12 @@ FRAMEPTR Build_Font_Effect (FRAMEPTR FramePtr, Uint32 from, Uint32 to, BYTE type ->FlagsAndIndex) >> FTYPE_SHIFT; NewFrame = CaptureDrawable ( CreateDrawable (FrameType, (SIZE)width, (SIZE)height, 1)); - tfbOrigImg = (TFB_Image *)((BYTE *)(FramePtr) + FramePtr->DataOffs); - OrigImg = tfbOrigImg->NormalImg; + tfbOrigImg = FramePtr->image; + OrigImg = (SDL_Surface *)tfbOrigImg->NormalImg; SDL_LockSurface (OrigImg); - tfbImg = (TFB_Image *)((BYTE *)(NewFrame) + NewFrame->DataOffs); - img = tfbImg->NormalImg; + tfbImg = NewFrame->image; + img = (SDL_Surface *)tfbImg->NormalImg; SDL_LockSurface (img); putpix = putpixel_for (img); @@ -401,7 +401,7 @@ FRAMEPTR Build_Font_Effect (FRAMEPTR FramePtr, Uint32 from, Uint32 to, BYTE type // r,g,b,a values supplied Uint32 frame_mapRGBA (FRAMEPTR FramePtr,Uint8 r, Uint8 g, Uint8 b, Uint8 a) { - SDL_Surface *img= ((TFB_Image *)((BYTE *)(FramePtr) + FramePtr->DataOffs))->NormalImg; + SDL_Surface *img= (SDL_Surface *)FramePtr->image->NormalImg; return (SDL_MapRGBA (img->format, r, g, b, a)); } @@ -522,13 +522,10 @@ _ReleaseCelData (MEM_HANDLE handle) { while (--FramePtr, cel_ct--) { - if (FramePtr->DataOffs) + TFB_Image *img = FramePtr->image; + if (img) { - TFB_Image *img; - - img = (TFB_Image *)((BYTE *)FramePtr + FramePtr->DataOffs); - FramePtr->DataOffs = 0; - + FramePtr->image = NULL; TFB_DrawScreen_DeleteImage (img); } } @@ -662,12 +659,10 @@ _ReleaseFontData (MEM_HANDLE handle) FramePtr = &FontPtr->CharDesc[cel_ct]; while (--FramePtr, cel_ct--) { - if (FramePtr->DataOffs) + TFB_Image *img = FramePtr->image; + if (img) { - TFB_Image *img; - - img = (TFB_Image *)((BYTE *)FramePtr + FramePtr->DataOffs); - FramePtr->DataOffs = 0; + FramePtr->image = NULL; TFB_DrawScreen_DeleteImage (img); } @@ -712,10 +707,6 @@ _request_drawable (COUNT NumFrames, DRAWABLE_TYPE DrawableType, imgw = width; imgh = height; - // commented out these when removing support for pre-scaling -Mika - // imgw = (flags & MAPPED_TO_DISPLAY) ? width * ScreenWidthActual / ScreenWidth : width; - // imgh = (flags & MAPPED_TO_DISPLAY) ? height * ScreenHeightActual / ScreenHeight : height; - FramePtr = &DrawablePtr->Frame[NumFrames - 1]; while (NumFrames--) { @@ -724,7 +715,7 @@ _request_drawable (COUNT NumFrames, DRAWABLE_TYPE DrawableType, if (DrawableType == RAM_DRAWABLE && (Image = TFB_DrawImage_New (TFB_DrawCanvas_New_TrueColor (imgw, imgh, FALSE)))) { - FramePtr->DataOffs = (BYTE *)Image - (BYTE *)FramePtr; + FramePtr->image = Image; } TYPE_SET (FramePtr->TypeIndexAndFlags, DrawableType); diff --git a/sc2/src/sc2code/libs/graphics/sdl/oscilloscope.c b/sc2/src/sc2code/libs/graphics/sdl/oscilloscope.c index f9272e684..be33c7866 100644 --- a/sc2/src/sc2code/libs/graphics/sdl/oscilloscope.c +++ b/sc2/src/sc2code/libs/graphics/sdl/oscilloscope.c @@ -30,7 +30,7 @@ static UBYTE scope_data[RADAR_WIDTH]; void InitOscilloscope (DWORD x, DWORD y, DWORD width, DWORD height, FRAME_DESC *f) { - TFB_Image *img = (TFB_Image *)((BYTE *)f + f->DataOffs); + TFB_Image *img = f->image; assert (((SDL_Surface *)img->NormalImg)->format->BytesPerPixel == 1); assert (((SDL_Surface *)img->NormalImg)->w == RADAR_WIDTH); @@ -106,7 +106,7 @@ Oscilloscope (DWORD grab_data) SDL_UnlockSurface (scope_surf); } - img = (TFB_Image *)((BYTE *)scope_frame + scope_frame->DataOffs); + img = scope_frame->image; LockMutex (img->mutex); SDL_BlitSurface (scope_surf, NULL, img->NormalImg, NULL); UnlockMutex (img->mutex); diff --git a/sc2/src/sc2code/libs/graphics/sdl/rndzoom.c b/sc2/src/sc2code/libs/graphics/sdl/rndzoom.c index 892abd34e..29035d4d9 100644 --- a/sc2/src/sc2code/libs/graphics/sdl/rndzoom.c +++ b/sc2/src/sc2code/libs/graphics/sdl/rndzoom.c @@ -562,9 +562,9 @@ void scale16xRandomizeFrame (FRAMEPTR FramePtr) { TFB_Image *tfbImg; - tfbImg = (TFB_Image *)((BYTE *)(FramePtr) + FramePtr->DataOffs); + tfbImg = FramePtr->image; LockMutex (tfbImg->mutex); - tfbImg->ScaledImg = random16xZoomSurfaceRGBA (tfbImg->NormalImg); + tfbImg->ScaledImg = random16xZoomSurfaceRGBA (tfbImg->NormalImg); tfbImg->scale = 4 << 8; UnlockMutex (tfbImg->mutex); }