#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "gdriver.h"
#include "gevents.h"
#include "gmain.h"
#define WIN_WIDTH 640u
#define WIN_HEIGHT 450u
typedef struct _Translator
{
char key;
GEvent event_class;
int event_info;
} Translator;
#define NUM_Translators 20
static const Translator trans[NUM_Translators] =
{
{ (char)27, event_Quit, 0 },
{ 'q', event_Quit, 0 },
{ 'x', event_Rotate_Glyph, -1 },
{ 'c', event_Rotate_Glyph, 1 },
{ 'v', event_Rotate_Glyph, -16 },
{ 'b', event_Rotate_Glyph, 16 },
{ '{', event_Change_Glyph, -10000 },
{ '}', event_Change_Glyph, 10000 },
{ '(', event_Change_Glyph, -1000 },
{ ')', event_Change_Glyph, 1000 },
{ '9', event_Change_Glyph, -100 },
{ '0', event_Change_Glyph, 100 },
{ 'i', event_Change_Glyph, -10 },
{ 'o', event_Change_Glyph, 10 },
{ 'k', event_Change_Glyph, -1 },
{ 'l', event_Change_Glyph, 1 },
{ '+', event_Scale_Glyph, 10 },
{ '-', event_Scale_Glyph, -10 },
{ 'u', event_Scale_Glyph, 1 },
{ 'j', event_Scale_Glyph, -1 }
};
static HWND hwndGraphic;
static LPBITMAPINFO pbmi;
static HBITMAP hbm;
static TEvent ourevent = { event_Quit, 0 };
static int eventToProcess = 0;
extern char Header[];
int Driver_Restore_Mode()
{
if ( hwndGraphic ) {
DestroyWindow ( hwndGraphic );
PostMessage( hwndGraphic, WM_QUIT, 0, 0 );
}
if ( pbmi ) free ( pbmi );
return 1;
}
static BOOL RegisterTheClass ( void );
static BOOL CreateTheWindow ( int width, int height );
int Driver_Set_Graphics ( int mode )
{
int i;
static RGBQUAD gray_scale[5] = {
{ 0xFF, 0xFF, 0xFF, 0 },
{ 0xC0, 0xC0, 0xC0, 0 },
{ 0x80, 0x80, 0x80, 0 },
{ 0x40, 0x40, 0x40, 0 },
{ 0, 0, 0, 0 } };
if( ! RegisterTheClass() ) return 0;
if ( (pbmi = malloc ( sizeof ( BITMAPINFO ) + sizeof ( RGBQUAD ) * 256 ) )
== NULL )
return 0;
memset( pbmi, 0, sizeof ( BITMAPINFO ) + sizeof ( RGBQUAD ) * 256 );
switch ( mode )
{
case Graphics_Mode_Mono:
pbmi->bmiHeader.biBitCount = 1;
pbmi->bmiColors[0] = gray_scale[0];
pbmi->bmiColors[1] = gray_scale[4];
vio_ScanLineWidth = WIN_WIDTH / 8;
vio_Width = WIN_WIDTH;
vio_Height = WIN_HEIGHT;
break;
case Graphics_Mode_Gray:
pbmi->bmiHeader.biBitCount = 8;
pbmi->bmiHeader.biClrUsed = 5;
memcpy ( &pbmi->bmiColors[0], gray_scale, sizeof gray_scale );
vio_ScanLineWidth = WIN_WIDTH;
vio_Width = WIN_WIDTH;
vio_Height = WIN_HEIGHT;
for ( i = 0; i < 5; ++i )
gray_palette[i] = i;
break;
default:
free ( pbmi );
return 0;
}
pbmi->bmiHeader.biSize = sizeof ( BITMAPINFOHEADER );
pbmi->bmiHeader.biWidth = vio_Width;
pbmi->bmiHeader.biHeight = vio_Height;
pbmi->bmiHeader.biPlanes = 1;
if( ! CreateTheWindow(vio_Width, vio_Height) )
{
free ( pbmi );
return 0;
}
return 1;
}
int Driver_Display_Bitmap ( char* buffer, int lines, int cols )
{
HDC hDC;
if ( cols * 8 != pbmi->bmiHeader.biWidth * pbmi->bmiHeader.biBitCount )
pbmi->bmiHeader.biWidth = cols * 8 / pbmi->bmiHeader.biBitCount;
hDC = GetDC ( hwndGraphic );
SetDIBits ( hDC, hbm, 0, lines, buffer, pbmi, DIB_RGB_COLORS );
ReleaseDC ( hwndGraphic, hDC );
ShowWindow( hwndGraphic, SW_SHOW );
InvalidateRect ( hwndGraphic, NULL, FALSE );
UpdateWindow ( hwndGraphic );
return 1;
}
void Get_Event( TEvent* event )
{
MSG msg;
if ( hwndGraphic )
{
SetWindowText ( hwndGraphic, Header );
}
do {
while ( PeekMessage( &msg, 0, 0, 0, PM_REMOVE ) ) {
TranslateMessage ( &msg );
DispatchMessage ( &msg );
}
if ( ! eventToProcess )
WaitMessage();
} while ( ! eventToProcess );
event->what = ourevent.what;
event->info = ourevent.info;
eventToProcess = 0;
return;
}
LRESULT CALLBACK Message_Process( HWND, UINT, WPARAM, LPARAM );
static
BOOL RegisterTheClass ( void )
{
WNDCLASS ourClass = {
0,
Message_Process,
0,
0,
0,
0,
0,
0,
NULL,
"FreeTypeTestGraphicDriver"
};
ourClass.hInstance = GetModuleHandle( NULL );
ourClass.hIcon = LoadIcon(0, IDI_APPLICATION);
ourClass.hCursor = LoadCursor(0, IDC_ARROW);
ourClass.hbrBackground= GetStockObject(BLACK_BRUSH);
return RegisterClass(&ourClass) != 0;
}
static
BOOL CreateTheWindow ( int width, int height )
{
if ( ! (hwndGraphic = CreateWindow(
"FreeTypeTestGraphicDriver",
"FreeType Test Graphic Driver",
WS_OVERLAPPED | WS_SYSMENU,
CW_USEDEFAULT,
CW_USEDEFAULT,
width + 2*GetSystemMetrics(SM_CXBORDER),
height+ GetSystemMetrics(SM_CYBORDER)
+ GetSystemMetrics(SM_CYCAPTION),
HWND_DESKTOP,
0,
GetModuleHandle( NULL ),
NULL))
)
return 0;
return 1;
}
LRESULT CALLBACK Message_Process( HWND handle, UINT mess,
WPARAM wParam, LPARAM lParam )
{
switch( mess )
{
case WM_DESTROY:
ourevent.what = event_Quit;
ourevent.info = 0;
eventToProcess = 1;
hwndGraphic = 0;
PostQuitMessage ( 0 );
DeleteObject ( hbm );
break;
case WM_CREATE:
{
HDC hDC;
hDC = GetDC ( handle );
hbm = CreateDIBitmap (
hDC,
&pbmi->bmiHeader,
0,
NULL,
pbmi,
DIB_RGB_COLORS);
ReleaseDC ( handle, hDC );
break;
}
case WM_PAINT:
{
HDC hDC, memDC;
HANDLE oldbm;
PAINTSTRUCT ps;
hDC = BeginPaint ( handle, &ps );
memDC = CreateCompatibleDC(hDC);
oldbm = SelectObject(memDC, hbm);
BitBlt ( hDC, 0, 0, vio_Width, vio_Height, memDC, 0, 0, SRCCOPY);
ReleaseDC ( handle, hDC );
SelectObject ( memDC, oldbm );
DeleteObject ( memDC );
EndPaint ( handle, &ps );
}
case WM_KEYDOWN:
switch ( wParam )
{
case VK_ESCAPE:
ourevent.what = event_Quit;
ourevent.info = 0;
eventToProcess = 1;
break;
case VK_F1:
break;
}
break;
case WM_CHAR:
{
char c = wParam ;
int i;
for ( i = 0; i < NUM_Translators; i++ )
{
if ( c == trans[i].key )
{
ourevent.what = trans[i].event_class;
ourevent.info = trans[i].event_info;
eventToProcess = 1;
return 0;
}
}
ourevent.what = event_Keyboard;
ourevent.info = (int)c;
eventToProcess = 1;
}
break;
default:
return DefWindowProc( handle, mess, wParam, lParam );
}
return 0;
}