room101/WIN32/VGS.C

153 lines
4.3 KiB
C

#include "vgs.h"
#include <windows.h>
#include <mmsystem.h>
#include <ddraw.h>
#include <seafood/win32/dsutil.h>
static VGS vgs;
static DDSURFACEDESC ddsd;
static IDirectDraw* dd;
static IDirectDrawSurface* primary;
static IDirectDrawPalette* palette;
IDirectSound* directsound;
LRESULT CALLBACK
WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch (msg)
{
case WM_KEYDOWN:
switch (wparam)
{
case VK_ESCAPE:
PostQuitMessage(0);
break;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, msg, wparam, lparam);
}
int WINAPI
WinMain(HINSTANCE instance, HINSTANCE prev, PSTR cmdline, int cmdshow)
{
int i;
char* fb;
HWND hwnd;
MSG msg;
WNDCLASSEX wndclass;
freopen("stdout.txt", "w", stdout);
freopen("stderr.txt", "w", stderr);
wndclass.cbSize = sizeof(wndclass);
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = instance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = "MyApp";
wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassEx(&wndclass);
hwnd = CreateWindow(wndclass.lpszClassName,
"",
WS_POPUP | WS_VISIBLE,
0, 0,
GetSystemMetrics(SM_CXSCREEN),
GetSystemMetrics(SM_CYSCREEN),
NULL,
NULL,
instance,
NULL);
ShowWindow(hwnd, cmdshow);
UpdateWindow(hwnd);
ShowCursor(FALSE);
/*
#define DSSCL_NORMAL 1
#define DSSCL_PRIORITY 2
#define DSSCL_EXCLUSIVE 3
#define DSSCL_WRITEPRIMARY 4
*/
/*
** Create a DirectSound.
*/
assert(DirectSoundCreate(NULL, &directsound, NULL) == DS_OK);
assert(IDirectSound_SetCooperativeLevel(directsound, hwnd, DSSCL_EXCLUSIVE) == DS_OK);
/*
** Create a DirectDraw display.
*/
vgs.channel = ChannelCreate(320, 200, 1, 0, 0);
assert(DirectDrawCreate(NULL, &dd, NULL) == DD_OK);
assert(IDirectDraw_SetCooperativeLevel(dd, hwnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN) == DD_OK);
assert(IDirectDraw_SetDisplayMode(dd, vgs.channel->width, vgs.channel->height, 8) == DD_OK);
/*
** Create primary surface.
*/
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
assert(IDirectDraw_CreateSurface(dd, &ddsd, &primary, NULL) == DD_OK);
assert(IDirectDraw_CreatePalette(dd, DDPCAPS_8BIT, vgs.palette, &palette, NULL) == DD_OK);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
while (!PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
{
IDirectDrawPalette_SetEntries(palette, 0, 0, 256, vgs.palette);
IDirectDrawSurface_SetPalette(primary, palette);
vgs.keyboard[0x48] = GetAsyncKeyState(VK_UP) < 0;
vgs.keyboard[0x50] = GetAsyncKeyState(VK_DOWN) < 0;
vgs.keyboard[0x4B] = GetAsyncKeyState(VK_LEFT) < 0;
vgs.keyboard[0x4D] = GetAsyncKeyState(VK_RIGHT) < 0;
Refresh(&vgs);
/*
** Lock the primary surface.
*/
ddsd.dwSize = sizeof(ddsd);
if (IDirectDrawSurface_Lock(primary, NULL, &ddsd, 0, NULL) == DD_OK)
{
fb = ddsd.lpSurface;
for (i = 0; i < vgs.channel->height; i++)
{
memcpy(fb, vgs.channel->pixels.b8[i], vgs.channel->width);
fb += ddsd.lPitch;
}
IDirectDrawSurface_Unlock(primary, NULL);
}
}
}
return msg.wParam;
}