threading patch
git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@29 8092fc87-c524-0410-9efc-e669fe64eaf9
This commit is contained in:
@@ -44,6 +44,7 @@ enum
|
|||||||
// flags for TFB_InitGraphics
|
// flags for TFB_InitGraphics
|
||||||
#define TFB_GFXFLAGS_FULLSCREEN (1<<0)
|
#define TFB_GFXFLAGS_FULLSCREEN (1<<0)
|
||||||
#define TFB_GFXFLAGS_BILINEAR_FILTERING (1<<1)
|
#define TFB_GFXFLAGS_BILINEAR_FILTERING (1<<1)
|
||||||
|
#define TFB_GFXFLAGS_SHOWFPS (1<<2)
|
||||||
|
|
||||||
int TFB_InitGraphics (int driver, int flags, int width, int height, int bpp);
|
int TFB_InitGraphics (int driver, int flags, int width, int height, int bpp);
|
||||||
int TFB_CreateGamePlayThread ();
|
int TFB_CreateGamePlayThread ();
|
||||||
@@ -106,7 +107,7 @@ typedef struct tfb_drawcommandqueue
|
|||||||
{
|
{
|
||||||
int Front;
|
int Front;
|
||||||
int Back;
|
int Back;
|
||||||
int Size;
|
volatile int Size;
|
||||||
} TFB_DrawCommandQueue;
|
} TFB_DrawCommandQueue;
|
||||||
|
|
||||||
TFB_DrawCommandQueue *TFB_DrawCommandQueue_Create ();
|
TFB_DrawCommandQueue *TFB_DrawCommandQueue_Create ();
|
||||||
|
|||||||
@@ -39,6 +39,9 @@ blt (PRECT pClipRect, PRIMITIVEPTR PrimPtr)
|
|||||||
|
|
||||||
img = (TFB_Image *) ((BYTE *) SrcFramePtr + SrcFramePtr->DataOffs);
|
img = (TFB_Image *) ((BYTE *) SrcFramePtr + SrcFramePtr->DataOffs);
|
||||||
|
|
||||||
|
if (SDL_mutexP(img->mutex))
|
||||||
|
printf("blt(): couldn't lock img->mutex\n");
|
||||||
|
|
||||||
if (TYPE_GET (_CurFramePtr->TypeIndexAndFlags) == SCREEN_DRAWABLE)
|
if (TYPE_GET (_CurFramePtr->TypeIndexAndFlags) == SCREEN_DRAWABLE)
|
||||||
{
|
{
|
||||||
TFB_DrawCommand DC_on_stack;
|
TFB_DrawCommand DC_on_stack;
|
||||||
@@ -46,8 +49,6 @@ blt (PRECT pClipRect, PRIMITIVEPTR PrimPtr)
|
|||||||
|
|
||||||
// DrawCommand = HMalloc (sizeof (TFB_DrawCommand));
|
// DrawCommand = HMalloc (sizeof (TFB_DrawCommand));
|
||||||
|
|
||||||
if (DrawCommand)
|
|
||||||
{
|
|
||||||
DrawCommand->Type = TFB_DRAWCOMMANDTYPE_IMAGE;
|
DrawCommand->Type = TFB_DRAWCOMMANDTYPE_IMAGE;
|
||||||
DrawCommand->x = pClipRect->corner.x -
|
DrawCommand->x = pClipRect->corner.x -
|
||||||
GetFrameHotX (_CurFramePtr);
|
GetFrameHotX (_CurFramePtr);
|
||||||
@@ -85,8 +86,6 @@ blt (PRECT pClipRect, PRIMITIVEPTR PrimPtr)
|
|||||||
// much memory and is perhaps too slow to process on slower systems.
|
// much memory and is perhaps too slow to process on slower systems.
|
||||||
// -Mika
|
// -Mika
|
||||||
|
|
||||||
// TODO: does this need mutexes?
|
|
||||||
|
|
||||||
SDL_Surface *new_surf;
|
SDL_Surface *new_surf;
|
||||||
|
|
||||||
img->scale = gscale;
|
img->scale = gscale;
|
||||||
@@ -132,7 +131,6 @@ blt (PRECT pClipRect, PRIMITIVEPTR PrimPtr)
|
|||||||
|
|
||||||
TFB_EnqueueDrawCommand(DrawCommand);
|
TFB_EnqueueDrawCommand(DrawCommand);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
SDL_Rect SDL_SrcRect, SDL_DstRect;
|
SDL_Rect SDL_SrcRect, SDL_DstRect;
|
||||||
@@ -141,6 +139,9 @@ blt (PRECT pClipRect, PRIMITIVEPTR PrimPtr)
|
|||||||
dst_img = ((TFB_Image *) ((BYTE *) _CurFramePtr +
|
dst_img = ((TFB_Image *) ((BYTE *) _CurFramePtr +
|
||||||
_CurFramePtr->DataOffs));
|
_CurFramePtr->DataOffs));
|
||||||
|
|
||||||
|
if (SDL_mutexP(dst_img->mutex))
|
||||||
|
printf("blt(): couldn't lock dst_img->mutex\n");
|
||||||
|
|
||||||
SDL_SrcRect.x = (short) img->SurfaceSDL->clip_rect.x;
|
SDL_SrcRect.x = (short) img->SurfaceSDL->clip_rect.x;
|
||||||
SDL_SrcRect.y = (short) img->SurfaceSDL->clip_rect.y;
|
SDL_SrcRect.y = (short) img->SurfaceSDL->clip_rect.y;
|
||||||
SDL_SrcRect.w = (short) img->SurfaceSDL->clip_rect.w;
|
SDL_SrcRect.w = (short) img->SurfaceSDL->clip_rect.w;
|
||||||
@@ -159,7 +160,13 @@ blt (PRECT pClipRect, PRIMITIVEPTR PrimPtr)
|
|||||||
);
|
);
|
||||||
|
|
||||||
dst_img->dirty = TRUE;
|
dst_img->dirty = TRUE;
|
||||||
|
|
||||||
|
if (SDL_mutexV(dst_img->mutex))
|
||||||
|
printf("blt(): couldn't unlock dst_img->mutex\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (SDL_mutexV(img->mutex))
|
||||||
|
printf("blt(): couldn't unlock img->mutex\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@@ -212,6 +219,9 @@ fillrect_blt (PRECT pClipRect, PRIMITIVEPTR PrimPtr)
|
|||||||
img = ((TFB_Image *) ((BYTE *) _CurFramePtr +
|
img = ((TFB_Image *) ((BYTE *) _CurFramePtr +
|
||||||
_CurFramePtr->DataOffs));
|
_CurFramePtr->DataOffs));
|
||||||
|
|
||||||
|
if (SDL_mutexP(img->mutex))
|
||||||
|
printf("fillrect_blt(): couldn't lock img->mutex\n");
|
||||||
|
|
||||||
SDLRect.x = (short) (pClipRect->corner.x -
|
SDLRect.x = (short) (pClipRect->corner.x -
|
||||||
GetFrameHotX (_CurFramePtr));
|
GetFrameHotX (_CurFramePtr));
|
||||||
SDLRect.y = (short) (pClipRect->corner.y -
|
SDLRect.y = (short) (pClipRect->corner.y -
|
||||||
@@ -223,6 +233,8 @@ fillrect_blt (PRECT pClipRect, PRIMITIVEPTR PrimPtr)
|
|||||||
|
|
||||||
img->dirty = TRUE;
|
img->dirty = TRUE;
|
||||||
|
|
||||||
|
if (SDL_mutexV(img->mutex))
|
||||||
|
printf("fillrect_blt(): couldn't unlock img->mutex\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -293,6 +293,12 @@ SleepTask (DWORD wake_time)
|
|||||||
{
|
{
|
||||||
DWORD t;
|
DWORD t;
|
||||||
|
|
||||||
|
if (wake_time == 0)
|
||||||
|
{
|
||||||
|
SDL_Delay (5);
|
||||||
|
return (GetTimeCounter ());
|
||||||
|
}
|
||||||
|
|
||||||
t = GetTimeCounter ();
|
t = GetTimeCounter ();
|
||||||
if (wake_time <= t)
|
if (wake_time <= t)
|
||||||
SDL_Delay (0);
|
SDL_Delay (0);
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ SDL_Surface *SDL_Video;
|
|||||||
SDL_Surface *SDL_Screen;
|
SDL_Surface *SDL_Screen;
|
||||||
SDL_Surface *ExtraScreen;
|
SDL_Surface *ExtraScreen;
|
||||||
|
|
||||||
|
BOOLEAN ShowFPS = FALSE;
|
||||||
volatile int continuity_break;
|
volatile int continuity_break;
|
||||||
|
|
||||||
|
|
||||||
@@ -54,6 +55,9 @@ TFB_InitGraphics (int driver, int flags, int width, int height, int bpp)
|
|||||||
SDL_EnableUNICODE (1);
|
SDL_EnableUNICODE (1);
|
||||||
SDL_WM_SetCaption (TFB_WINDOW_CAPTION, NULL);
|
SDL_WM_SetCaption (TFB_WINDOW_CAPTION, NULL);
|
||||||
|
|
||||||
|
if (flags & TFB_GFXFLAGS_SHOWFPS)
|
||||||
|
ShowFPS = TRUE;
|
||||||
|
|
||||||
//if (flags & TFB_GFXFLAGS_FULLSCREEN)
|
//if (flags & TFB_GFXFLAGS_FULLSCREEN)
|
||||||
// SDL_ShowCursor (SDL_DISABLE);
|
// SDL_ShowCursor (SDL_DISABLE);
|
||||||
|
|
||||||
@@ -132,6 +136,7 @@ TFB_LoadImage (SDL_Surface *img)
|
|||||||
float y_scale = (float)ScreenHeightActual / ScreenHeight;
|
float y_scale = (float)ScreenHeightActual / ScreenHeight;
|
||||||
|
|
||||||
myImage = (TFB_Image*) HMalloc (sizeof (TFB_Image));
|
myImage = (TFB_Image*) HMalloc (sizeof (TFB_Image));
|
||||||
|
myImage->mutex = SDL_CreateMutex();
|
||||||
myImage->dirty = FALSE;
|
myImage->dirty = FALSE;
|
||||||
myImage->ScaledImg = NULL;
|
myImage->ScaledImg = NULL;
|
||||||
|
|
||||||
@@ -238,33 +243,56 @@ TFB_DisplayFormatAlpha (SDL_Surface *surface)
|
|||||||
return SDL_DisplayFormatAlpha (surface);
|
return SDL_DisplayFormatAlpha (surface);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
TFB_ComputeFPS ()
|
||||||
|
{
|
||||||
|
static Uint32 last_time = 0, fps_counter = 0;
|
||||||
|
Uint32 current_time, delta_time;
|
||||||
|
|
||||||
|
current_time = SDL_GetTicks ();
|
||||||
|
delta_time = current_time - last_time;
|
||||||
|
last_time = current_time;
|
||||||
|
|
||||||
|
fps_counter += delta_time;
|
||||||
|
if (fps_counter > 1000)
|
||||||
|
{
|
||||||
|
fps_counter = 0;
|
||||||
|
printf ("fps %.2f\n",1.0 / (delta_time / 1000.0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
TFB_FlushGraphics () // Only call from main thread!!
|
TFB_FlushGraphics () // Only call from main thread!!
|
||||||
{
|
{
|
||||||
int semval;
|
int semval;
|
||||||
Uint32 this_flush;
|
|
||||||
static Uint32 last_flush = 0;
|
|
||||||
|
|
||||||
this_flush = SDL_GetTicks ();
|
|
||||||
if (this_flush - last_flush < 1000 / 100)
|
|
||||||
return;
|
|
||||||
|
|
||||||
semval = SDL_SemTryWait (GraphicsSem);
|
|
||||||
if (semval != 0 && !continuity_break)
|
|
||||||
return;
|
|
||||||
|
|
||||||
continuity_break = 0;
|
|
||||||
if (DrawCommandQueue == 0 || DrawCommandQueue->Size == 0)
|
if (DrawCommandQueue == 0 || DrawCommandQueue->Size == 0)
|
||||||
{
|
{
|
||||||
if (semval == 0)
|
SDL_Delay(5);
|
||||||
SDL_SemPost (GraphicsSem);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
last_flush = this_flush;
|
if (!continuity_break) {
|
||||||
|
// TODO: a more optimal way of getting the lock on GraphicsSem..
|
||||||
|
// cannot currently use SDL_SemWait because it would break
|
||||||
|
// the usage of continuity_break
|
||||||
|
|
||||||
|
semval = SDL_SemWaitTimeout (GraphicsSem, 100);
|
||||||
|
if (semval != 0 && !continuity_break)
|
||||||
|
return;
|
||||||
|
continuity_break = 0;
|
||||||
|
|
||||||
if (semval == 0)
|
if (semval == 0)
|
||||||
SDL_SemPost (GraphicsSem);
|
SDL_SemPost (GraphicsSem);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
continuity_break = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ShowFPS)
|
||||||
|
TFB_ComputeFPS ();
|
||||||
|
|
||||||
|
|
||||||
while (DrawCommandQueue->Size > 0)
|
while (DrawCommandQueue->Size > 0)
|
||||||
{
|
{
|
||||||
@@ -280,6 +308,9 @@ TFB_FlushGraphics () // Only call from main thread!!
|
|||||||
}
|
}
|
||||||
|
|
||||||
DC_image = (TFB_Image*) DC->image;
|
DC_image = (TFB_Image*) DC->image;
|
||||||
|
if (DC_image)
|
||||||
|
if (SDL_mutexP(DC_image->mutex))
|
||||||
|
printf("TFB_FlushGraphics(): couldn't lock DC_image->mutex\n");
|
||||||
|
|
||||||
switch (DC->Type)
|
switch (DC->Type)
|
||||||
{
|
{
|
||||||
@@ -413,10 +444,19 @@ TFB_FlushGraphics () // Only call from main thread!!
|
|||||||
SDL_FreeSurface (DC_image->ScaledImg);
|
SDL_FreeSurface (DC_image->ScaledImg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (SDL_mutexV(DC_image->mutex))
|
||||||
|
printf("TFB_FlushGraphics(): couldn't unlock DC_image->mutex (DELETEIMAGE)\n");
|
||||||
|
SDL_DestroyMutex (DC_image->mutex);
|
||||||
|
|
||||||
HFree (DC_image);
|
HFree (DC_image);
|
||||||
|
DC_image = 0;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (DC_image)
|
||||||
|
if (SDL_mutexV(DC_image->mutex))
|
||||||
|
printf("TFB_FlushGraphics(): couldn't unlock DC_image->mutex\n");
|
||||||
|
|
||||||
TFB_DeallocateDrawCommand (DC);
|
TFB_DeallocateDrawCommand (DC);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ extern SDL_Surface *SDL_Screen;
|
|||||||
extern SDL_Surface *ExtraScreen;
|
extern SDL_Surface *ExtraScreen;
|
||||||
|
|
||||||
extern volatile int continuity_break;
|
extern volatile int continuity_break;
|
||||||
|
extern BOOLEAN ShowFPS;
|
||||||
|
|
||||||
void ScreenOrigin (FRAME Display, COORD sx, COORD sy);
|
void ScreenOrigin (FRAME Display, COORD sx, COORD sy);
|
||||||
void LoadDisplay (PDISPLAY_INTERFACE *pDisplay);
|
void LoadDisplay (PDISPLAY_INTERFACE *pDisplay);
|
||||||
@@ -45,8 +46,9 @@ typedef struct tfb_image
|
|||||||
SDL_Surface *ScaledImg;
|
SDL_Surface *ScaledImg;
|
||||||
int scale; // used with ScaledImg
|
int scale; // used with ScaledImg
|
||||||
BOOLEAN dirty; // true if DrawableImg needs to be created again
|
BOOLEAN dirty; // true if DrawableImg needs to be created again
|
||||||
|
SDL_mutex *mutex;
|
||||||
UBYTE pad[sizeof(TFB_ImageStruct)-sizeof(SDL_Surface*)-sizeof(SDL_Surface*)-
|
UBYTE pad[sizeof(TFB_ImageStruct)-sizeof(SDL_Surface*)-sizeof(SDL_Surface*)-
|
||||||
sizeof(SDL_Surface*)-sizeof(int)-sizeof(BOOLEAN)];
|
sizeof(SDL_Surface*)-sizeof(int)-sizeof(BOOLEAN)-sizeof(SDL_mutex*)];
|
||||||
} TFB_Image;
|
} TFB_Image;
|
||||||
|
|
||||||
TFB_Image *TFB_LoadImage (SDL_Surface *img);
|
TFB_Image *TFB_LoadImage (SDL_Surface *img);
|
||||||
|
|||||||
@@ -89,7 +89,8 @@ TFB_InitInput (int driver, int flags)
|
|||||||
void
|
void
|
||||||
ProcessKeyboardEvent(const SDL_Event *Event)
|
ProcessKeyboardEvent(const SDL_Event *Event)
|
||||||
{
|
{
|
||||||
if(Event->key.keysym.sym == SDLK_BACKQUOTE)
|
if(Event->key.keysym.sym == SDLK_BACKQUOTE ||
|
||||||
|
Event->key.keysym.sym == SDLK_WORLD_7)
|
||||||
{
|
{
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-3
@@ -49,17 +49,20 @@ main (int argc, char *argv[])
|
|||||||
else if (!strcmp(argv[i],"-opengl"))
|
else if (!strcmp(argv[i],"-opengl"))
|
||||||
{
|
{
|
||||||
gfxdriver = TFB_GFXDRIVER_SDL_OPENGL;
|
gfxdriver = TFB_GFXDRIVER_SDL_OPENGL;
|
||||||
gfxflags |= TFB_GFXFLAGS_BILINEAR_FILTERING;
|
|
||||||
}
|
}
|
||||||
else if (!strcmp(argv[i],"-linear"))
|
else if (!strcmp(argv[i],"-bilinear"))
|
||||||
{
|
{
|
||||||
gfxflags &= ~TFB_GFXFLAGS_BILINEAR_FILTERING;
|
gfxflags |= TFB_GFXFLAGS_BILINEAR_FILTERING;
|
||||||
}
|
}
|
||||||
else if (!strcmp(argv[i],"-frequency"))
|
else if (!strcmp(argv[i],"-frequency"))
|
||||||
{
|
{
|
||||||
i++;
|
i++;
|
||||||
sscanf(argv[i],"%d",&frequency);
|
sscanf(argv[i],"%d",&frequency);
|
||||||
}
|
}
|
||||||
|
else if (!strcmp(argv[i],"-fps"))
|
||||||
|
{
|
||||||
|
gfxflags |= TFB_GFXFLAGS_SHOWFPS;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TFB_InitGraphics (gfxdriver, gfxflags, width, height, bpp);
|
TFB_InitGraphics (gfxdriver, gfxflags, width, height, bpp);
|
||||||
|
|||||||
Reference in New Issue
Block a user