380 lines
11 KiB
C++
380 lines
11 KiB
C++
#define _WIN32_WINNT 0x0500
|
|
#include <windows.h>
|
|
|
|
|
|
#include <windows.h>
|
|
#include <stdio.h>
|
|
#include <vector>
|
|
#include <psapi.h>
|
|
|
|
#pragma comment(lib, "psapi.lib")
|
|
|
|
#include "SCENE.h"
|
|
|
|
#ifdef ONLINE_MODE
|
|
#include "account.h"
|
|
extern CCamCamAccount Account;
|
|
#endif
|
|
|
|
|
|
|
|
u32 Dump_Blocks_In_All_Heaps ()
|
|
{
|
|
u32 RetValue = 0;
|
|
PROCESS_MEMORY_COUNTERS ppsmemCounters;
|
|
if (GetProcessMemoryInfo(GetCurrentProcess(),&ppsmemCounters,sizeof(PROCESS_MEMORY_COUNTERS)))
|
|
return ppsmemCounters.WorkingSetSize;
|
|
else
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL GetWindowClientRect(HWND wnd, RECT *clientRect)
|
|
{
|
|
POINT tmpPoint;
|
|
GetClientRect(wnd, clientRect);
|
|
|
|
tmpPoint.x = clientRect->left;
|
|
tmpPoint.y = clientRect->top;
|
|
ClientToScreen(wnd, &tmpPoint);
|
|
clientRect->left = tmpPoint.x;
|
|
clientRect->top = tmpPoint.y;
|
|
|
|
tmpPoint.x = clientRect->right;
|
|
tmpPoint.y = clientRect->bottom;
|
|
ClientToScreen(wnd, &tmpPoint);
|
|
clientRect->right = tmpPoint.x;
|
|
clientRect->bottom = tmpPoint.y;
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
void WORLD::Flip_DIB(bool clearDI)
|
|
{
|
|
HDC HwndHDC;
|
|
START_RASTER(DIB);
|
|
// Copy and Keep it SQUARE
|
|
if (FrameBufferResult.GetBase())
|
|
{
|
|
RECT WR, CR;
|
|
//HDC hdcScreen = CreateDC("DISPLAY",NULL,NULL,NULL);
|
|
HDC hdcScreen = GetDC(OwnerHwnd);
|
|
GetWindowRect(OwnerHwnd,&WR);
|
|
GetWindowClientRect(OwnerHwnd,&CR);
|
|
|
|
if (hdcCompatible == NULL) hdcCompatible = CreateCompatibleDC(hdcScreen);
|
|
if (hbmScreen == NULL) hbmScreen = CreateCompatibleBitmap(hdcScreen, GetDeviceCaps(hdcScreen, HORZRES), GetDeviceCaps(hdcScreen, VERTRES));
|
|
|
|
HGDIOBJ OldObject = SelectObject(hdcCompatible, hbmScreen);
|
|
if (hbmScreen && OldObject)
|
|
{
|
|
BITMAPINFOHEADER bi;
|
|
memset(&bi, 0,sizeof(BITMAPINFOHEADER));
|
|
bi.biSize = sizeof(BITMAPINFOHEADER);
|
|
bi.biWidth = XSize;
|
|
bi.biHeight = YSize;
|
|
bi.biPlanes = 1;
|
|
bi.biBitCount = 32;
|
|
bi.biCompression = BI_RGB;
|
|
|
|
SetDIBits ( hdcCompatible, hbmScreen, 0,FrameBufferResult.SY, FrameBufferResult.GetBase(),(BITMAPINFO*)&bi, DIB_RGB_COLORS);
|
|
if (LayederWindow)
|
|
{
|
|
POINT dstPt = {WR.left, WR.top};
|
|
SIZE dstSz = {WR.right-WR.left, WR.bottom-WR.top};
|
|
POINT srcPt = {0,0};
|
|
BLENDFUNCTION BF;
|
|
BF.BlendOp = AC_SRC_OVER;
|
|
BF.BlendFlags = 0;
|
|
BF.SourceConstantAlpha = 255;
|
|
BF.AlphaFormat = AC_SRC_ALPHA ;
|
|
|
|
UpdateLayeredWindow(OwnerHwnd, NULL, &dstPt, &dstSz, hdcCompatible, &srcPt, 0, &BF, ULW_ALPHA);
|
|
} else {
|
|
BitBlt(hdcScreen, 0,0, XSize,YSize,hdcCompatible,0,0, SRCCOPY);
|
|
}
|
|
SelectObject(hdcCompatible, hbmScreen);
|
|
};
|
|
SelectObject(hdcCompatible,OldObject);
|
|
ReleaseDC(OwnerHwnd,hdcScreen);
|
|
}
|
|
STOP_RASTER(DIB);
|
|
if (clearDI)
|
|
DI->Flip(0);
|
|
}
|
|
|
|
int UpdateFrameBuffer(DrawInterface *DI, MAP *FrameBuffer, MAP *layerPtr, u32 mask, int lineMode, bool forceRefresh)
|
|
{
|
|
static MAP tileMap;
|
|
static MAP layerSaveMap;
|
|
static int tileMapInit = 0;
|
|
static int line = 0;
|
|
static int update = 0;
|
|
|
|
int tileX, tileY;
|
|
|
|
tileX = FrameBuffer->SX / layerPtr->SX + ((FrameBuffer->SX % layerPtr->SX) ? 1 : 0);
|
|
tileY = FrameBuffer->SY / layerPtr->SY + ((FrameBuffer->SY % layerPtr->SY) ? 1 : 0);
|
|
|
|
tileMap.SetSize(FrameBuffer->SX, tileY);
|
|
layerSaveMap.SetSize(layerPtr->SX, layerPtr->SY);
|
|
|
|
|
|
if (!tileMapInit || forceRefresh)
|
|
{
|
|
DI->ReadFrameBuffer(0,0,FrameBuffer->SX,FrameBuffer->SY,FrameBuffer);
|
|
memcpy(layerSaveMap.GetBase(), layerPtr->GetBase(), layerPtr->SX*layerPtr->SY*sizeof(int));
|
|
|
|
tileMapInit = 1;
|
|
return true;
|
|
}
|
|
|
|
int result = false;
|
|
int *lBuffer = layerPtr->GetBase();
|
|
int *lBufferSave= layerSaveMap.GetBase();
|
|
int lX0, lX1;
|
|
|
|
line++;
|
|
if (line >= layerSaveMap.SY) {
|
|
line = 0;
|
|
if (update > 0)
|
|
{
|
|
update--;
|
|
result = true;
|
|
}
|
|
}
|
|
|
|
if (lineMode == 1)
|
|
memset(layerSaveMap.GetBase() + line*layerSaveMap.PITCH, 0xff, layerSaveMap.PITCH*sizeof(int));
|
|
|
|
for (int lY = 0; lY<layerPtr->SY; lY++)
|
|
{
|
|
int tileYBegin = lY*tileY;
|
|
int tileYToCopy = tileY;
|
|
if (tileYBegin + tileYToCopy > FrameBuffer->SY) tileYToCopy = FrameBuffer->SY - tileYBegin;
|
|
|
|
if (tileYToCopy > 0)
|
|
{
|
|
if (lineMode == 2 && update > 0 && lY == line)
|
|
{
|
|
MAP TmpMap = *FrameBuffer;
|
|
TmpMap.staticBase = 1;
|
|
TmpMap.MoveBase(tileYBegin*FrameBuffer->PITCH);
|
|
DI->ReadFrameBuffer(0,tileYBegin,FrameBuffer->PITCH, tileYToCopy, &TmpMap);
|
|
}
|
|
|
|
for (int lX = 0; lX<layerPtr->SX; lX++, lBuffer++, lBufferSave++)
|
|
{
|
|
lX0 = lX;
|
|
for (;lX < layerPtr->SX && (*lBuffer & mask) != (*lBufferSave & mask); lX++, lBuffer++, lBufferSave++);
|
|
if (lX == layerPtr->SX) { lX--; lBuffer--; lBufferSave--; }
|
|
if (lX0 != lX)
|
|
{
|
|
result = true;
|
|
update = 2;
|
|
do
|
|
{
|
|
lX1 = lX;
|
|
lX++; lBuffer++; lBufferSave++;
|
|
for (;lX < layerPtr->SX && *lBuffer != *lBufferSave; lX++, lBuffer++, lBufferSave++);
|
|
} while (lX1 != lX - 1);
|
|
lX--; lBuffer--; lBufferSave--;
|
|
|
|
int tileXBegin = lX0*tileX;
|
|
int tileXToCopy = tileX*(lX-lX0);
|
|
if (tileXBegin + tileXToCopy > FrameBuffer->SX) tileXToCopy = FrameBuffer->SX - tileXBegin;
|
|
|
|
if (tileXToCopy > 0)
|
|
{
|
|
DI->ReadFrameBuffer(tileXBegin,tileYBegin,tileXToCopy,tileYToCopy,&tileMap);
|
|
int *fBufferPtr = FrameBuffer->GetBase() + tileXBegin + tileYBegin*FrameBuffer->PITCH;
|
|
int *tBufferPtr = tileMap.GetBase();
|
|
for (int y=0; y<tileYToCopy; y++, tBufferPtr += tileXToCopy, fBufferPtr += FrameBuffer->PITCH)
|
|
memcpy(fBufferPtr, tBufferPtr, tileXToCopy*sizeof(int));
|
|
}
|
|
}
|
|
}
|
|
lBuffer += layerPtr->PITCH - layerPtr->SX;
|
|
lBufferSave += layerSaveMap.PITCH - layerSaveMap.SX;
|
|
}
|
|
}
|
|
if (result)
|
|
memcpy(layerSaveMap.GetBase(), layerPtr->GetBase(), layerPtr->SX*layerPtr->SY*sizeof(int));
|
|
return result;
|
|
}
|
|
|
|
|
|
|
|
HRGN GetRgnFromImage(MAP *imgPtr, int component, int threshhold, float scale);
|
|
void WORLD::RenderFlip()
|
|
{
|
|
//SkipFunctions |= SKIP_NOFPS;
|
|
if (SkipFunctions & SKIP_NOFPS)
|
|
{
|
|
static u32 BigDT;
|
|
static float faDT = 16.0f;
|
|
char TTTTTT[100];
|
|
BigDT = timeGetTime() - BigDT;
|
|
if (BigDT > 1000) BigDT = 1000;
|
|
faDT = faDT * 0.99f + (float)(BigDT) * 0.01f;
|
|
sprintf(TTTTTT,"%d",(u32)(1000.0f / faDT));
|
|
DI->DrawText((unsigned char*)TTTTTT,10,YSize-30,1.5f,1.5f,0xff00ffff);
|
|
BigDT = timeGetTime();
|
|
if (DI->GetDriverType() == 1)
|
|
DI->DrawText((unsigned char*)"D3D",10,YSize-30-16,1.f,1.f,0xff00ffff);
|
|
else
|
|
DI->DrawText((unsigned char*)"OPENGL",10,YSize-30-16,1.f,1.f,0xff00ffff);
|
|
sprintf(TTTTTT,"fps camera :%d",(u32)(1000.0 / (double)(trackMiliseconds_x_16>>8)));
|
|
DI->DrawText((unsigned char*)TTTTTT,10,YSize-30-32,1.f,1.f,0xff00ffff);
|
|
|
|
static u32 DisplayCounter = 0;
|
|
static u32 MemoireMedia = 0;
|
|
static u32 MemoireDriver = 0;
|
|
static u32 MemoireSmallScene = 0;
|
|
static u32 SmallSceneNumber = 0;
|
|
static u32 MemoireLua = 0;
|
|
static u32 HeapMemory = 0;
|
|
static u32 MemoryBuffer = 0;
|
|
|
|
if ((DisplayCounter & 0x7f) == 0)
|
|
{
|
|
MemoireMedia = MemoireDriver = MemoireSmallScene = SmallSceneNumber = MemoireLua = 0;
|
|
for (int Counter = 0 ; Counter < MAX_INPMED ; Counter++)
|
|
{
|
|
if (ID_MEDIA[Counter])
|
|
{
|
|
MemoireMedia += ID_MEDIA[Counter]->Image.PITCH * ID_MEDIA[Counter]->Image.SY * 4;
|
|
if (ID_MEDIA[Counter]->ulAssociatedTextureIndex != -1)
|
|
{
|
|
if ((DI->GetDriverType()==1) || (DI->AllRef[ID_MEDIA[Counter]->ulAssociatedTextureIndex].OGLRef != -2))
|
|
MemoireDriver += GetUpperPO2(ID_MEDIA[Counter]->Image.PITCH) * GetUpperPO2(ID_MEDIA[Counter]->Image.SY) * 4;
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
LUA_API int lua_gc (lua_State *L, int what, int data) ;
|
|
MemoireLua = lua_gc(luaScriptTable[currentScript], LUA_GCCOUNT,0);
|
|
|
|
MemoireSmallScene = SmallSceneNumber = 0;
|
|
for (int Counter = 0 ; Counter < MAX_SmallSCenes ; Counter++)
|
|
{
|
|
if (pSmallScenes[Counter])
|
|
{
|
|
SmallSceneNumber++;
|
|
MemoireSmallScene += GetSmScSize(pSmallScenes[Counter]);
|
|
}
|
|
}
|
|
HeapMemory = Dump_Blocks_In_All_Heaps();
|
|
#ifndef USER_MODE
|
|
#ifdef ONLINE_MODE
|
|
MemoryBuffer = Account.getMemoryUsage();
|
|
#endif
|
|
#endif
|
|
}
|
|
|
|
extern u32 MediaSize ;
|
|
sprintf(TTTTTT,"M=%.2fM (%.3f)",(float)(MemoireMedia>>10)/1000.0f,(float)(MediaSize >>10)/1000.0f);
|
|
DI->DrawText((unsigned char*)TTTTTT,10,YSize-30-32-16,1.f,1.f,0xff00ffff);
|
|
sprintf(TTTTTT,"Driver = %.2fMo",(float)(MemoireDriver>>10)/1000.0f);
|
|
DI->DrawText((unsigned char*)TTTTTT,10,YSize-30-32-32,1.f,1.f,0xff00ffff);
|
|
sprintf(TTTTTT,"Lua = %.2fKo",(float)(MemoireLua)/1000.0f);
|
|
DI->DrawText((unsigned char*)TTTTTT,10,YSize-30-32-32-16,1.f,1.f,0xff00ffff);
|
|
sprintf(TTTTTT,"SMC = %.2fKo(#%d)",(float)(MemoireSmallScene>>10)/1000.0f,SmallSceneNumber);
|
|
DI->DrawText((unsigned char*)TTTTTT,10,YSize-30-32-32-32,1.f,1.f,0xff00ffff);//*/
|
|
sprintf(TTTTTT,"Buffer = %.2fKo",(float)(MemoryBuffer)/1000.0f);
|
|
DI->DrawText((unsigned char*)TTTTTT,10,YSize-30-32-32-32-16,1.f,1.f,0xff00ffff);//*/
|
|
sprintf(TTTTTT,"Total = %.2fKo",(float)(HeapMemory>>10)/1000.0f);
|
|
DI->DrawText((unsigned char*)TTTTTT,10,YSize-30-32-32-32-32,1.f,1.f,0xff00ffff);//*/
|
|
|
|
DisplayCounter ++;
|
|
}
|
|
|
|
START_RASTER(ReadPixels);
|
|
|
|
if (ulMustUpdateFrameBufferResult || UseGDI)
|
|
{
|
|
if (ulFullRefresh)
|
|
SetWindowRgn(OwnerHwnd, NULL, true);
|
|
|
|
if (FrameBufferMediaDiff && ID_MEDIA[FrameBufferMediaDiff-1])
|
|
UpdateFrameBuffer(DI, &FrameBufferResult, &ID_MEDIA[FrameBufferMediaDiff-1]->Image, 0xFFFFFFFF, 1, ulFullRefresh);
|
|
else
|
|
DI->ReadFrameBuffer(0,0,XSize,YSize,&FrameBufferResult);
|
|
} else if (RegionThreshold && FrameBufferMediaDiff && ID_MEDIA[FrameBufferMediaDiff-1])
|
|
{
|
|
if (UpdateFrameBuffer(DI, &FrameBufferResult, &ID_MEDIA[FrameBufferMediaDiff-1]->Image, 0xFF000000, 2, ulFullRefresh))
|
|
SetWindowRgn(OwnerHwnd, GetRgnFromImage(&FrameBufferResult, 3, RegionThreshold, 1.0f), true);
|
|
}
|
|
ulFullRefresh = false;
|
|
|
|
STOP_RASTER(ReadPixels);
|
|
|
|
if (AVIMediaToSave == 0)
|
|
{
|
|
SaveInFile_Copy(&FrameBufferResult);
|
|
} else if (AVIMediaToSave > 0 && ID_MEDIA[AVIMediaToSave-1])
|
|
{
|
|
SaveInFile_Copy(&ID_MEDIA[AVIMediaToSave-1]->Image);
|
|
}
|
|
|
|
/* 60 hz limitation */
|
|
if (!(SkipFunctions & SKIP_NO_60hz_LIMIT))
|
|
{
|
|
static s32 LasTime = 0;
|
|
LasTime = timeGetTime() - LasTime;
|
|
LasTime = 16 - LasTime;
|
|
timeBeginPeriod(1);
|
|
if ((LasTime >= 0) && (LasTime < 100)) Sleep(LasTime);
|
|
timeEndPeriod(1);
|
|
LasTime = timeGetTime();
|
|
}
|
|
|
|
|
|
if (UseGDI)
|
|
Flip_DIB();
|
|
else
|
|
{
|
|
if (hbmScreen) DeleteObject(hbmScreen);
|
|
if (hdcCompatible) DeleteDC(hdcCompatible);
|
|
hbmScreen = NULL;
|
|
hdcCompatible = NULL;
|
|
DI->Flip(1);
|
|
}
|
|
|
|
};
|
|
|
|
void WORLD::ChangeResolution(u32 NewSizeX,u32 NewSizeY)
|
|
{
|
|
if ((XSize == NewSizeX) && (YSize == NewSizeY))
|
|
return;
|
|
|
|
if (hbmScreen) DeleteObject(hbmScreen);
|
|
if (hdcCompatible) DeleteDC(hdcCompatible);
|
|
hbmScreen = NULL;
|
|
hdcCompatible = NULL;
|
|
|
|
{
|
|
DI->SetResolution(NewSizeX,NewSizeY);
|
|
XSize = NewSizeX;
|
|
YSize = NewSizeY;
|
|
FrameBufferResult.SetSize(NewSizeX, NewSizeY);
|
|
}
|
|
}
|
|
|
|
void WORLD::ReadFrameBuffer()
|
|
{
|
|
DI->ReadFrameBuffer(0,0,XSize,YSize,&FrameBufferResult);
|
|
}
|
|
|
|
void WORLD::ChangeGDIMode(u32 useGDI)
|
|
{
|
|
if (useGDI == UseGDI)
|
|
return;
|
|
|
|
UseGDI = useGDI;
|
|
DI->ChangeMode(OwnerHwnd, UseGDI);
|
|
}
|