Code:
//winshell.cpp
#include "screen.h"
#include "dirdraw.h"
#include "winshell.h"
DWORD WINAPI RedrawScreen (LPVOID lpvParm);
DWORD WINAPI UpdateProc (LPVOID lpvParm);
DWORD WINAPI HandleEvents (LPVOID lpvParm);
void InitLoops ( );
void CleanupLoops ( );
BOOL ActiveApp;// is the program active?
static HWND WindowHandle;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
HWND hwnd;
//DRAWING_MANAGERptr Draw;
DIRECT_DRAW_MANAGER *DDraw;
bool FullScreenFlag = /*false;*/true;
int SCREEN_WIDTH = 640, SCREEN_HEIGHT = 480;
InitLoops();
hPrevInstance = hPrevInstance;// avoid C++ warning
// here we instantiate the direct draw object
DDraw = new DIRECT_DRAW_MANAGER;
hwnd = DDraw->CreateMainWindow(SCREEN_WIDTH, SCREEN_HEIGHT,
"Generic Name", MessageLoop, hInstance,
FullScreenFlag);
//Draw = new DRAWING_MANAGER (SCREEN_WIDTH, SCREEN_HEIGHT,
// MessageLoop, hInstance, FullScreenFlag);
//hwnd = (HWND)Draw->GetWindowHandle();
//Draw->MousePointerPrepare();
//if (!hwnd) {delete Draw; return FALSE;}
if(!hwnd) { delete DDraw; return FALSE; }
while(1)
{
int t = PeekMessage(&msg, NULL, 0U, 0U, PM_NOREMOVE);
if (t)
{
if (!GetMessage (& msg, NULL, 0, 0))
return msg.wParam;
TranslateMessage (&msg);
DispatchMessage (&msg);
}
else if (ActiveApp)// this is important here
{
//RedrawScreen ((LPVOID) Draw);
RedrawScreen((LPVOID) DDraw);
}
else if (!ActiveApp)
WaitMessage();
}
//delete Draw;
delete DDraw;
CleanupLoops();
return 1;
}
//winmain
void* GetWindowHandle(void)
{
return (void*)WindowHandle;
}
void ErrorMessage (char* str)
{
MessageBox (WindowHandle, str, "Error", MB_OK);
DestroyWindow (WindowHandle);
}
// loops.cpp
#include "dirdraw.h"
#include "alphabet.h"
#include "graphic.h"
int frame = 0;
int time =0, timelast=0;
int count = 0;
ALPHABET_MANAGER alpha;
GRAPHIC *Graphic = NULL;
void Draw ( DIRECT_DRAW_MANAGER *DDraw );
void InitLoops ( )
{
int NoExternalScratchPad = 1;
int compress = 1;
Graphic = new GRAPHIC (NoExternalScratchPad);
STRINGC str = "Test.tga";
Graphic->Load(str, compress);
}
void CleanupLoops ( )
{
if(Graphic)
{
if(Graphic)
delete Graphic;
}
}
DWORD WINAPI RedrawScreen (LPVOID Parm)
{
DIRECT_DRAW_MANAGER *DDraw = (DIRECT_DRAW_MANAGER *) Parm;
if (DDraw == NULL) return 1;
time = timeGetTime();// get the current millisec
count++;
if (time-timelast>=1000)
{
frame = count;
timelast=time;// update time
count=0;
}
char buff[40];
itoa(frame, buff, 10);
while(DDraw->DrawFrame());
DDraw->ClearBackBuffer();
Draw(DDraw);
DDraw->PaintText(20, 20, buff);
return 0;
}
void Draw ( DIRECT_DRAW_MANAGER *DDraw )
{
SCREEN_STRUCT *screen = DDraw->GetScreen();
char buff[100];
long time1 = timeGetTime();
Graphic->DrawAt(screen, rand()%630+5, rand()%470+5);
long time2 = timeGetTime();
long result = time2-time1;
itoa(result, buff, 10);
alpha.PrintText(60, 60, screen, buff);
}
//one function from graphic.cpp
bool GRAPHIC:: Load (STRINGC& str, bool CompressFlag)// load by filename and convert to 16 bit compressed
{
flags.Flag.COMPRESSED = (U16)CompressFlag;// make sure we have this flag
if (!flags.Flag.NO_SCRATCH_PAD && ScratchPad == NULL) return false;
if (Image != NULL) {delete Image; Image = NULL;}
SetPath (str);
if (FileType == LLE) return LoadLLE();
if (FileType == TGA) return LoadTGA();
if (FileType != BMP) return false;
// bmp by default
BITMAPFILEHEADER Header;
BITMAPINFOHEADER Info;
//*************** assumes a BMP for now ***********************
FILE_MASTER file (str, FILE_MASTER:: READ);
if (file.Status() != FILE_MASTER::NONE) return false;
if (file.Read (&Header, sizeof (BITMAPFILEHEADER))) {return false;}
if (file.Read (&Info, sizeof (BITMAPINFOHEADER))) {return false; }
Height = (U16) Info.biHeight, Width = (U16) Info.biWidth;
if (Info.biBitCount == 16) return false;// we do not support 16 bit bitmaps
if (flags.Flag.NO_SCRATCH_PAD && ScratchPad) delete ScratchPad;
if (flags.Flag.NO_SCRATCH_PAD) ScratchPad = new U16 [2*Height*Width];
if (Info.biBitCount == 24) if (Load24Bit(&file)==false) return false;
if (Info.biBitCount == 8) if (Load8Bit(&file, Info.biClrUsed)==false) return false;
if (Info.biBitCount == 4) if (Load4Bit(&file, Info.biClrUsed)==false) return false;
if (Info.biBitCount != 1)
{
if (CompressFlag == true)
Image = LLECompression16Bit (ScratchPad, Width, Height, &NumPixels);
else
{
Image = CopyScratchPadIntoImage ();
NumPixels = Width*Height;
}
}
//***************
flags.Flag.LOADED = true;
if (flags.Flag.NO_SCRATCH_PAD && ScratchPad)
{
delete ScratchPad;
ScratchPad = NULL;
}
return true;
}