#include "pch.hpp"
//
//==========================================================================//
// File:	 Surface.cpp													//
// Project:  GameOS					                                        //
// Contents: DirectDraw Surface object										//
//--------------------------------------------------------------------------//
// Date      Who    Modification                                            //
// 11/15/97  JTR    Created													//
// 12/23/97  JTR    Documentation, Additions and Debugging 					//
// 01/05/98  JTR    Fixed blit clip bug					 					//
// 01/07/98  JTR    Added TGA loader, added setpixel	 					//
// 04/13/98  JTR	Added 8888RGB surface for 32bit desktops				//
// 04/23/98  JTR	Fixed bug in nativeformat for 24bit surfaces			//
//																			//
//--------------------------------------------------------------------------//
// Copyright (C) 1997-1998, Virtual World Entertainment, Inc.               //
// All Rights reserved worldwide                                            //
// This unpublished sourcecode is PROPRIETARY and CONFIDENTIAL              //
//==========================================================================//
//
#include "Font3D_DBCS_Surface.hpp"
#include "DirectX.hpp"
#include "DXRasterizer.hpp"


extern DDSURFACEDESC2 TextureFormat_Solid;		// Normally 565 or 555

extern void gos_ChangeHeapSize( HGOSHEAP Heap, int Change, bool SystemAllocation=0 );

extern HGOSHEAP DefaultHeap;
extern HGOSHEAP Heap_Font;

// Constructor for a surface. A width, height, and format can be specified.
// As it is constructed, the surface's values are set to defaults.
DBCSSurface::DBCSSurface(DWORD width, DWORD height)
{
	gosASSERT(width > 0 && height > 0);
	m_width = width;
	m_height = height;
	m_offsety = 0;

//
// Setup format of system memory surface.
//
	DDSURFACEDESC2 ddsd;
	memset(&ddsd,0, sizeof(ddsd));
	ddsd.dwSize = sizeof(ddsd);
	ddsd.ddpfPixelFormat.dwFlags |= DDPF_RGB | DDPF_ALPHAPIXELS;
	ddsd.ddpfPixelFormat.dwRGBBitCount = 32;
	ddsd.ddpfPixelFormat.dwRBitMask = 0x00ff0000;
	ddsd.ddpfPixelFormat.dwGBitMask = 0x0000ff00;
	ddsd.ddpfPixelFormat.dwBBitMask = 0x000000ff;
	ddsd.ddpfPixelFormat.dwRGBAlphaBitMask = 0xff000000;
	ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
	ddsd.dwFlags			= DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
	ddsd.ddsCaps.dwCaps		= DDSCAPS_SYSTEMMEMORY | DDSCAPS_TEXTURE;
	ddsd.dwWidth			= width;
	ddsd.dwHeight			= height;

//
// Create system memory texture surface
//
	wCreateSurface( CurrentDDobject, &ddsd, &m_lpdds7, NULL );
//
// Calculate memory useage
//
	gos_ChangeHeapSize( Heap_Font, m_width*m_height*4, 1 );
	m_lpDynData = NULL;
}	

// Deconstructor. Removes the surface from the surface list and releases
// associated memory.
DBCSSurface::~DBCSSurface()
{
	if (m_lpdds7)
	{
		gos_ChangeHeapSize( Heap_Font, -m_width*m_height*4, 1 );
		wRelease(m_lpdds7);
		m_lpdds7 = NULL;
	}
}


// Get the surface data pointer with error checking (make sure it's locked).
void* DBCSSurface::GetDataPointer()
{
	DDSURFACEDESC2 ddsd;
	ddsd.dwSize = sizeof(ddsd);
	wGetSurfaceDesc(m_lpdds7, &ddsd);
	return ddsd.lpSurface;
}


// This function performs the work of the similar gos_ call.
void DBCSSurface::Clear(DWORD color)
{
	DWORD * lp = (DWORD *) m_lpDynData;
	DWORD SurfaceSize=m_height * m_width;
	_asm{
		push edi
		push ecx
		mov edi,lp
		mov ecx,SurfaceSize
		mov eax,color
		rep stosd
		pop ecx
		pop edi
	}
}


