From 528ec82d08ca3dde8869dd80eedda431c9ce9b56 Mon Sep 17 00:00:00 2001 From: mcmartin Date: Thu, 30 Jan 2003 07:47:41 +0000 Subject: [PATCH] Second phase of SDL-Graphics modularization. New primitives for drawing directly on TFB_Images and TFB_Canvases. git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@607 8092fc87-c524-0410-9efc-e669fe64eaf9 --- sc2/ChangeLog | 2 + sc2/src/msvc++/UrQuanMasters.dsp | 4 + sc2/src/sc2code/libs/graphics/drawcmd.h | 2 +- sc2/src/sc2code/libs/graphics/sdl/3do_blt.c | 8 +- sc2/src/sc2code/libs/graphics/sdl/Makeinfo | 3 +- sc2/src/sc2code/libs/graphics/sdl/canvas.c | 133 ++++++++++++++++++ .../sc2code/libs/graphics/sdl/oscilloscope.c | 20 +-- .../sc2code/libs/graphics/sdl/sdl_common.c | 129 +++-------------- sc2/src/sc2code/libs/graphics/tfb_draw.c | 15 +- sc2/src/sc2code/libs/graphics/tfb_draw.h | 15 +- 10 files changed, 186 insertions(+), 145 deletions(-) create mode 100644 sc2/src/sc2code/libs/graphics/sdl/canvas.c diff --git a/sc2/ChangeLog b/sc2/ChangeLog index cb43e3539..03ae67b5a 100644 --- a/sc2/ChangeLog +++ b/sc2/ChangeLog @@ -1,4 +1,6 @@ Changes towards version 0.2: +- Added a TFB_Canvas data buffer to let TFB_Image be more accessible +- Added new graphics primitives - Added 'smooth' scolling for ff, frev (similar to the 3DO) - PhracturedBlue - New streaming code for openal/mixsdl. supports ff/frev in subtitles -PBlue - New sound module "mixsdl" (experimental) -fOSSiL diff --git a/sc2/src/msvc++/UrQuanMasters.dsp b/sc2/src/msvc++/UrQuanMasters.dsp index b2692b0da..f6cd34d96 100644 --- a/sc2/src/msvc++/UrQuanMasters.dsp +++ b/sc2/src/msvc++/UrQuanMasters.dsp @@ -164,6 +164,10 @@ SOURCE=..\sc2code\libs\graphics\sdl\3do_getbody.c # End Source File # Begin Source File +SOURCE=..\sc2code\libs\graphics\sdl\canvas.c +# End Source File +# Begin Source File + SOURCE=..\sc2code\libs\graphics\sdl\dcqueue.c # End Source File # Begin Source File diff --git a/sc2/src/sc2code/libs/graphics/drawcmd.h b/sc2/src/sc2code/libs/graphics/drawcmd.h index 26e192a7f..970d4d51b 100644 --- a/sc2/src/sc2code/libs/graphics/drawcmd.h +++ b/sc2/src/sc2code/libs/graphics/drawcmd.h @@ -48,7 +48,7 @@ typedef struct tfb_dc_line typedef struct tfb_dc_rect { - int x, y, w, h; + RECT rect; int r, g, b; SCREEN destBuffer; } TFB_DrawCommand_Rect; diff --git a/sc2/src/sc2code/libs/graphics/sdl/3do_blt.c b/sc2/src/sc2code/libs/graphics/sdl/3do_blt.c index c2659514b..7600699a8 100644 --- a/sc2/src/sc2code/libs/graphics/sdl/3do_blt.c +++ b/sc2/src/sc2code/libs/graphics/sdl/3do_blt.c @@ -101,10 +101,10 @@ blt (PRECT pClipRect, PRIMITIVEPTR PrimPtr) { img->ScaledImg = new_surf; SDL_SetColors (img->ScaledImg, (SDL_Color *)img->Palette, 0, 256); - if (img->NormalImg->flags & SDL_SRCCOLORKEY) + if (((SDL_Surface *)img->NormalImg)->flags & SDL_SRCCOLORKEY) { SDL_SetColorKey (img->ScaledImg, SDL_SRCCOLORKEY, - img->NormalImg->format->colorkey); + ((SDL_Surface *)img->NormalImg)->format->colorkey); } } } @@ -135,7 +135,7 @@ blt (PRECT pClipRect, PRIMITIVEPTR PrimPtr) } else { - if (img->NormalImg->format->palette && img->colormap_index != -1) + if (((SDL_Surface *)img->NormalImg)->format->palette && img->colormap_index != -1) { TFB_ColorMapToRGB (palette, img->colormap_index); paletted = TRUE; @@ -232,7 +232,7 @@ fillrect_blt (PRECT pClipRect, PRIMITIVEPTR PrimPtr) SDLRect.w = (short) pClipRect->extent.width; SDLRect.h = (short) pClipRect->extent.height; - SDL_FillRect (img->NormalImg, &SDLRect, SDL_MapRGB (img->NormalImg->format, r, g, b)); + SDL_FillRect (img->NormalImg, &SDLRect, SDL_MapRGB (((SDL_Surface *)img->NormalImg)->format, r, g, b)); UnlockMutex (img->mutex); } diff --git a/sc2/src/sc2code/libs/graphics/sdl/Makeinfo b/sc2/src/sc2code/libs/graphics/sdl/Makeinfo index e2b0417f9..1efc7a0ec 100644 --- a/sc2/src/sc2code/libs/graphics/sdl/Makeinfo +++ b/sc2/src/sc2code/libs/graphics/sdl/Makeinfo @@ -1,2 +1,3 @@ uqm_CFILES="3do_blt.c 3do_funcs.c 3do_getbody.c dcqueue.c opengl.c - primitives.c pure.c rndzoom.c rotozoom.c sdl_common.c oscilloscope.c" + primitives.c pure.c rndzoom.c rotozoom.c sdl_common.c oscilloscope.c + canvas.c" diff --git a/sc2/src/sc2code/libs/graphics/sdl/canvas.c b/sc2/src/sc2code/libs/graphics/sdl/canvas.c new file mode 100644 index 000000000..e0394dff2 --- /dev/null +++ b/sc2/src/sc2code/libs/graphics/sdl/canvas.c @@ -0,0 +1,133 @@ +#include "SDL.h" +#include "sdl_common.h" +#include "libs/graphics/gfx_common.h" +#include "libs/graphics/sdl/primitives.h" + +typedef SDL_Surface *NativeCanvas; + +void +TFB_DrawCanvas_Line (int x1, int y1, int x2, int y2, int r, int g, int b, TFB_Canvas target) +{ + Uint32 color; + PutPixelFn screen_plot; + SDL_Rect rect; + + screen_plot = putpixel_for (target); + color = SDL_MapRGB (((NativeCanvas)target)->format, r, g, b); + + SDL_GetClipRect((NativeCanvas) target, &rect); + + /* Danger, Will Robinson! This code + looks VERY suspicious. It will end + up changing the slope of the + line! */ + + if (x1 < rect.x) + x1 = rect.x; + else if (x1 > rect.x + rect.w) + x1 = rect.x + rect.w; + + if (x2 < rect.x) + x2 = rect.x; + else if (x2 > rect.x + rect.w) + x2 = rect.x + rect.w; + + if (y1 < rect.y) + y1 = rect.y; + else if (y1 > rect.y + rect.h) + y1 = rect.y + rect.h; + + if (y2 < rect.y) + y2 = rect.y; + else if (y2 > rect.y + rect.h) + y2 = rect.y + rect.h; + + SDL_LockSurface ((NativeCanvas) target); + line (x1, y1, x2, y2, color, screen_plot, (NativeCanvas) target); + SDL_UnlockSurface ((NativeCanvas) target); +} + +void +TFB_DrawCanvas_Rect (PRECT rect, int r, int g, int b, TFB_Canvas target) +{ + SDL_Rect sr; + sr.x = rect->corner.x; + sr.y = rect->corner.y; + sr.w = rect->extent.width; + sr.h = rect->extent.height; + + SDL_FillRect((NativeCanvas)target, &sr, SDL_MapRGB(((NativeCanvas)target)->format, r, g, b)); +} + +void +TFB_DrawCanvas_Image (TFB_Image *img, int x, int y, BOOLEAN scaled, TFB_Palette *palette, TFB_Canvas target, int bn, int bd) +{ + SDL_Rect targetRect; + SDL_Surface *surf; + + if (img == 0) + { + fprintf (stderr, "ERROR: TFB_DrawCanvas_Image passed null image ptr\n"); + return; + } + + LockMutex (img->mutex); + targetRect.x = x; + targetRect.y = y; + + if (scaled) + surf = img->ScaledImg; + else + surf = img->NormalImg; + + if (surf->format->palette) + { + if (palette) + { + SDL_SetColors (surf, (SDL_Color*)palette, 0, 256); + } + else + { + SDL_SetColors (surf, (SDL_Color*)img->Palette, 0, 256); + } + } + + TFB_BlitSurface(surf, NULL, target, &targetRect, bn, bd); + UnlockMutex (img->mutex); +} + +void +TFB_DrawCanvas_FilledImage (TFB_Image *img, int x, int y, BOOLEAN scaled, int r, int g, int b, TFB_Canvas target, int bn, int bd) +{ + SDL_Rect targetRect; + SDL_Surface *surf; + int i; + SDL_Color pal[256]; + + if (img == 0) + { + fprintf (stderr, "ERROR: TFB_DrawCanvas_FilledImage passed null image ptr\n"); + return; + } + + LockMutex (img->mutex); + + targetRect.x = x; + targetRect.y = y; + + if (scaled) + surf = img->ScaledImg; + else + surf = img->NormalImg; + + for (i = 0; i < 256; i++) + { + pal[i].r = r; + pal[i].g = g; + pal[i].b = b; + } + SDL_SetColors (surf, pal, 0, 256); + + TFB_BlitSurface(surf, NULL, target, &targetRect, bn, bd); + UnlockMutex (img->mutex); +} diff --git a/sc2/src/sc2code/libs/graphics/sdl/oscilloscope.c b/sc2/src/sc2code/libs/graphics/sdl/oscilloscope.c index c481730aa..8d29d5ac5 100644 --- a/sc2/src/sc2code/libs/graphics/sdl/oscilloscope.c +++ b/sc2/src/sc2code/libs/graphics/sdl/oscilloscope.c @@ -35,30 +35,30 @@ InitOscilloscope (DWORD x, DWORD y, DWORD width, DWORD height, FRAME_DESC *f) { TFB_Image *img = (TFB_Image *)((BYTE *)f + f->DataOffs); - assert (img->NormalImg->format->BytesPerPixel == 1); - assert (img->NormalImg->w == RADAR_WIDTH); - assert (img->NormalImg->h == RADAR_HEIGHT); + assert (((SDL_Surface *)img->NormalImg)->format->BytesPerPixel == 1); + assert (((SDL_Surface *)img->NormalImg)->w == RADAR_WIDTH); + assert (((SDL_Surface *)img->NormalImg)->h == RADAR_HEIGHT); if (scope_init == 0) { int i; LockMutex (img->mutex); - scope_bg = SDL_CreateRGBSurface (SDL_SWSURFACE, img->NormalImg->w, - img->NormalImg->h, 8, + scope_bg = SDL_CreateRGBSurface (SDL_SWSURFACE, ((SDL_Surface *)img->NormalImg)->w, + ((SDL_Surface *)img->NormalImg)->h, 8, 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000); - scope_surf = SDL_CreateRGBSurface (SDL_SWSURFACE, img->NormalImg->w, - img->NormalImg->h, 8, + scope_surf = SDL_CreateRGBSurface (SDL_SWSURFACE, ((SDL_Surface *)img->NormalImg)->w, + ((SDL_Surface *)img->NormalImg)->h, 8, 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000); for (i = 0; i < 256; ++i) { scope_bg->format->palette->colors[i].r = scope_surf->format->palette->colors[i].r = - img->NormalImg->format->palette->colors[i].r; + ((SDL_Surface *)img->NormalImg)->format->palette->colors[i].r; scope_bg->format->palette->colors[i].g = scope_surf->format->palette->colors[i].g = - img->NormalImg->format->palette->colors[i].g; + ((SDL_Surface *)img->NormalImg)->format->palette->colors[i].g; scope_bg->format->palette->colors[i].b = scope_surf->format->palette->colors[i].b = - img->NormalImg->format->palette->colors[i].b; + ((SDL_Surface *)img->NormalImg)->format->palette->colors[i].b; } SDL_BlitSurface (img->NormalImg, NULL, scope_bg, NULL); diff --git a/sc2/src/sc2code/libs/graphics/sdl/sdl_common.c b/sc2/src/sc2code/libs/graphics/sdl/sdl_common.c index c9f1bc462..e3de82765 100644 --- a/sc2/src/sc2code/libs/graphics/sdl/sdl_common.c +++ b/sc2/src/sc2code/libs/graphics/sdl/sdl_common.c @@ -560,134 +560,47 @@ TFB_FlushGraphics () // Only call from main thread!! } case TFB_DRAWCOMMANDTYPE_IMAGE: { - SDL_Rect targetRect; - SDL_Surface *surf; TFB_Image *DC_image = (TFB_Image *)DC.data.image.image; + TFB_Palette *pal; - if (DC_image == 0) + if (DC.data.image.UsePalette) { - fprintf (stderr, "DCQ ERROR: IMAGE passed null image ptr\n"); - break; + pal = palette; } - - LockMutex (DC_image->mutex); - targetRect.x = DC.data.image.x; - targetRect.y = DC.data.image.y; - - if (DC.data.image.UseScaling) - surf = DC_image->ScaledImg; else - surf = DC_image->NormalImg; - - if (surf->format->palette) { - if (DC.data.image.UsePalette) - { - SDL_SetColors (surf, (SDL_Color*)palette, 0, 256); - } - else - { - SDL_SetColors (surf, (SDL_Color*)DC_image->Palette, 0, 256); - } + pal = DC_image->Palette; } - TFB_BlitSurface(surf, NULL, SDL_Screens[DC.data.image.destBuffer], &targetRect, DC.data.image.BlendNumerator, DC.data.image.BlendDenominator); - UnlockMutex (DC_image->mutex); + TFB_DrawCanvas_Image (DC_image, DC.data.image.x, DC.data.image.y, + DC.data.image.UseScaling, pal, + SDL_Screens[DC.data.image.destBuffer], + DC.data.image.BlendNumerator, DC.data.image.BlendDenominator); break; } case TFB_DRAWCOMMANDTYPE_FILLEDIMAGE: { - SDL_Rect targetRect; - SDL_Surface *surf; - int i; - TFB_Palette pal[256]; - TFB_Image *DC_image = (TFB_Image *)DC.data.filledimage.image; - - if (DC_image == 0) - { - fprintf (stderr, "DCQ ERROR: FILLEDIMAGE passed null image ptr\n"); - break; - } - LockMutex (DC_image->mutex); - - targetRect.x = DC.data.filledimage.x; - targetRect.y = DC.data.filledimage.y; - - if (DC.data.filledimage.UseScaling) - surf = DC_image->ScaledImg; - else - surf = DC_image->NormalImg; - - for (i = 0; i < 256; i++) - { - pal[i].r = DC.data.filledimage.r; - pal[i].g = DC.data.filledimage.g; - pal[i].b = DC.data.filledimage.b; - } - SDL_SetColors (surf, (SDL_Color*)pal, 0, 256); - - TFB_BlitSurface(surf, NULL, SDL_Screens[DC.data.filledimage.destBuffer], &targetRect, DC.data.filledimage.BlendNumerator, DC.data.filledimage.BlendDenominator); - UnlockMutex (DC_image->mutex); + TFB_DrawCanvas_FilledImage (DC.data.filledimage.image, DC.data.filledimage.x, DC.data.filledimage.y, + DC.data.filledimage.UseScaling, DC.data.filledimage.r, DC.data.filledimage.g, + DC.data.filledimage.b, SDL_Screens[DC.data.filledimage.destBuffer], + DC.data.filledimage.BlendNumerator, DC.data.filledimage.BlendDenominator); break; } case TFB_DRAWCOMMANDTYPE_LINE: { - SDL_Rect r; - int x1, y1, x2, y2; - Uint32 color; - PutPixelFn screen_plot; - - screen_plot = putpixel_for (SDL_Screen); - color = SDL_MapRGB (SDL_Screen->format, DC.data.line.r, DC.data.line.g, DC.data.line.b); - - x1 = DC.data.line.x1; - x2 = DC.data.line.x2; - y1 = DC.data.line.y1; - y2 = DC.data.line.y2; - - SDL_GetClipRect(SDL_Screen, &r); - - /* Danger, Will Robinson! This code - looks VERY suspicious. It will end - up changing the slope of the - line! */ - - if (x1 < r.x) - x1 = r.x; - else if (x1 > r.x + r.w) - x1 = r.x + r.w; - - if (x2 < r.x) - x2 = r.x; - else if (x2 > r.x + r.w) - x2 = r.x + r.w; - - if (y1 < r.y) - y1 = r.y; - else if (y1 > r.y + r.h) - y1 = r.y + r.h; - - if (y2 < r.y) - y2 = r.y; - else if (y2 > r.y + r.h) - y2 = r.y + r.h; - - SDL_LockSurface (SDL_Screens[DC.data.line.destBuffer]); - line (x1, y1, x2, y2, color, screen_plot, SDL_Screens[DC.data.line.destBuffer]); - SDL_UnlockSurface (SDL_Screens[DC.data.line.destBuffer]); - + TFB_DrawCanvas_Line (DC.data.line.x1, DC.data.line.y1, + DC.data.line.x2, DC.data.line.y2, + DC.data.line.r, DC.data.line.g, DC.data.line.b, + SDL_Screens[DC.data.line.destBuffer]); break; } case TFB_DRAWCOMMANDTYPE_RECTANGLE: { - SDL_Rect r; - r.x = DC.data.rect.x; - r.y = DC.data.rect.y; - r.w = DC.data.rect.w; - r.h = DC.data.rect.h; + TFB_DrawCanvas_Rect (&DC.data.rect.rect, DC.data.rect.r, + DC.data.rect.g, DC.data.rect.b, + SDL_Screens[DC.data.rect.destBuffer]); - SDL_FillRect(SDL_Screens[DC.data.rect.destBuffer], &r, SDL_MapRGB(SDL_Screen->format, DC.data.rect.r, DC.data.rect.g, DC.data.rect.b)); break; } case TFB_DRAWCOMMANDTYPE_SCISSORENABLE: @@ -698,11 +611,11 @@ TFB_FlushGraphics () // Only call from main thread!! r.w = DC.data.scissor.w; r.h = DC.data.scissor.h; - SDL_SetClipRect(SDL_Screen, &r); + SDL_SetClipRect(SDL_Screens[0], &r); break; } case TFB_DRAWCOMMANDTYPE_SCISSORDISABLE: - SDL_SetClipRect(SDL_Screen, NULL); + SDL_SetClipRect(SDL_Screens[0], NULL); break; case TFB_DRAWCOMMANDTYPE_COPYTOIMAGE: { diff --git a/sc2/src/sc2code/libs/graphics/tfb_draw.c b/sc2/src/sc2code/libs/graphics/tfb_draw.c index ca7e2a6c0..38325f5ef 100644 --- a/sc2/src/sc2code/libs/graphics/tfb_draw.c +++ b/sc2/src/sc2code/libs/graphics/tfb_draw.c @@ -35,10 +35,7 @@ TFB_DrawScreen_Rect (PRECT rect, int r, int g, int b, SCREEN dest) } DC.Type = TFB_DRAWCOMMANDTYPE_RECTANGLE; - DC.data.rect.x = rect->corner.x; - DC.data.rect.y = rect->corner.y; - DC.data.rect.w = rect->extent.width; - DC.data.rect.h = rect->extent.height; + DC.data.rect.rect = *rect; DC.data.rect.r = r; DC.data.rect.g = g; DC.data.rect.b = b; @@ -220,8 +217,6 @@ TFB_DrawScreen_WaitForSignal (void) WaitForSignal (); } -#if 0 - /* These don't compile until I write the DrawCanvas commands. */ void @@ -246,18 +241,16 @@ void TFB_DrawImage_Image (TFB_Image *img, int x, int y, BOOLEAN scaled, TFB_Palette *palette, TFB_Image *target) { LockMutex (target->mutex); - TFB_DrawCanvas_Image (img, x, y, scaled, target->NormalImg); + TFB_DrawCanvas_Image (img, x, y, scaled, palette, target->NormalImg, 4, 4); target->dirty = TRUE; UnlockMutex (target->mutex); } void -TFB_DrawImage_FilledImage (TFB_Image *img, int x, int y, BOOLEAN scaled, TFB_Palette *palette, TFB_Image *target) +TFB_DrawImage_FilledImage (TFB_Image *img, int x, int y, BOOLEAN scaled, int r, int g, int b, TFB_Image *target) { LockMutex (target->mutex); - TFB_DrawCanvas_FilledImage (img, x, y, scaled, target->NormalImg); + TFB_DrawCanvas_FilledImage (img, x, y, scaled, r, g, b, target->NormalImg, 4, 4); target->dirty = TRUE; UnlockMutex (target->mutex); } - -#endif diff --git a/sc2/src/sc2code/libs/graphics/tfb_draw.h b/sc2/src/sc2code/libs/graphics/tfb_draw.h index 8c4816fa4..59636ca2d 100644 --- a/sc2/src/sc2code/libs/graphics/tfb_draw.h +++ b/sc2/src/sc2code/libs/graphics/tfb_draw.h @@ -19,7 +19,6 @@ #ifndef TFB_DRAW_H #define TFB_DRAW_H -#include "SDL.h" #include "libs/threadlib.h" typedef void *TFB_Canvas; @@ -45,8 +44,8 @@ typedef struct tfb_palette typedef struct tfb_image { - SDL_Surface *NormalImg; - SDL_Surface *ScaledImg; + TFB_Canvas NormalImg; + TFB_Canvas ScaledImg; TFB_Palette *Palette; int colormap_index; int scale; @@ -68,18 +67,14 @@ void TFB_DrawScreen_WaitForSignal (void); void TFB_DrawScreen_SetPalette (int index, int r, int g, int b); void TFB_FlushPaletteCache (void); -#if 0 -// These don't compile until I implement the DrawCanvas commands. void TFB_DrawImage_Line (int x1, int y1, int x2, int y2, int r, int g, int b, TFB_Image *dest); void TFB_DrawImage_Rect (PRECT rect, int r, int g, int b, TFB_Image *image); void TFB_DrawImage_Image (TFB_Image *img, int x, int y, BOOLEAN scaled, TFB_Palette *palette, TFB_Image *target); -void TFB_DrawImage_FilledImage (TFB_Image *img, int x, int y, BOOLEAN scaled, TFB_Palette *palette, TFB_Image *target); +void TFB_DrawImage_FilledImage (TFB_Image *img, int x, int y, BOOLEAN scaled, int r, int g, int b, TFB_Image *target); -/* These routines are properly defined in the driver-specific libraries. */ void TFB_DrawCanvas_Line (int x1, int y1, int x2, int y2, int r, int g, int b, TFB_Canvas dest); void TFB_DrawCanvas_Rect (PRECT rect, int r, int g, int b, TFB_Canvas image); -void TFB_DrawCanvas_Image (TFB_Image *img, int x, int y, BOOLEAN scaled, TFB_Palette *palette, TFB_Canvas target); -void TFB_DrawCanvas_FilledImage (TFB_Image *img, int x, int y, BOOLEAN scaled, TFB_Palette *palette, TFB_Canvas target); -#endif +void TFB_DrawCanvas_Image (TFB_Image *img, int x, int y, BOOLEAN scaled, TFB_Palette *palette, TFB_Canvas target, int bn, int bd); +void TFB_DrawCanvas_FilledImage (TFB_Image *img, int x, int y, BOOLEAN scaled, int r, int g, int b, TFB_Canvas target, int bn, int bd); #endif