📄 graphics.cpp
字号:
SetPixel(hdc[0], x, y, PALETTEINDEX(c+BG));
}
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT messg,
WPARAM wParam, LPARAM lParam)
{
int i;
static bool palette_changed = false;
switch (messg) {
case WM_PAINT:
if (hdc[0] == 0) {
hdc[0] = BeginPaint(hWnd, &ps);
SelectPalette(hdc[0], hPalette, FALSE);
RealizePalette(hdc[0]);
hdc[1] = CreateCompatibleDC(hdc[0]);
SelectPalette(hdc[1], hPalette, FALSE);
hdc[2] = CreateCompatibleDC(hdc[0]);
SelectPalette(hdc[2], hPalette, FALSE);
hdc[3] = CreateCompatibleDC(hdc[0]);
SelectPalette(hdc[3], hPalette, FALSE);
screen_width = GetDeviceCaps(hdc[0], HORZRES);
screen_height = GetDeviceCaps(hdc[0], VERTRES);
hBitmap[active_page] =
CreateCompatibleBitmap(hdc[0], screen_width, screen_height);
SelectObject(hdc[1], hBitmap[active_page]);
SetTextColor(hdc[0], PALETTEINDEX(text_color+BG));
SetTextColor(hdc[1], PALETTEINDEX(text_color+BG));
SetBkColor(hdc[0], PALETTEINDEX(BG));
SetBkColor(hdc[1], PALETTEINDEX(BG));
SelectObject(hdc[0], hBrush[fill_settings.pattern]);
SelectObject(hdc[1], hBrush[fill_settings.pattern]);
RECT scr;
scr.left = -view_settings.left;
scr.top = -view_settings.top;
scr.right = screen_width-view_settings.left-1;
scr.bottom = screen_height-view_settings.top-1;
FillRect(hdc[1], &scr, hBackgroundBrush);
}
if (hRgn != NULL) {
SelectClipRgn(hdc[0], NULL);
}
if (visual_page != active_page) {
SelectObject(hdc[1], hBitmap[visual_page]);
}
BitBlt(hdc[0], -view_settings.left,
-view_settings.top, window_width, window_height,
hdc[1], -view_settings.left, -view_settings.top,
SRCCOPY);
if (hRgn != NULL) {
SelectClipRgn(hdc[0], hRgn);
}
if (visual_page != active_page) {
SelectObject(hdc[1], hBitmap[active_page]);
}
ValidateRect(hWnd, NULL);
break;
case WM_SETFOCUS:
if (palette_changed) {
HPALETTE new_palette = CreatePalette(pPalette);
SelectPalette(hdc[0], new_palette, FALSE);
RealizePalette(hdc[0]);
SelectPalette(hdc[1], new_palette, FALSE);
SelectPalette(hdc[2], new_palette, FALSE);
SelectPalette(hdc[3], new_palette, FALSE);
DeleteObject(hPalette);
hPalette = new_palette;
palette_changed = false;
}
break;
case WM_PALETTECHANGED:
RealizePalette(hdc[0]);
UpdateColors(hdc[0]);
palette_changed = true;
break;
case WM_DESTROY:
EndPaint(hWnd, &ps);
hdc[0] = 0;
DeleteObject(hdc[1]);
DeleteObject(hdc[2]);
DeleteObject(hdc[3]);
if (hPutimageBitmap) {
DeleteObject(hPutimageBitmap);
hPutimageBitmap = NULL;
}
for (i = 0; i < MAX_PAGES; i++) {
if (hBitmap[i] != NULL) {
DeleteObject(hBitmap[i]);
hBitmap[i] = 0;
}
}
DeleteObject(hPalette);
hPalette = 0;
PostQuitMessage(0);
break;
case WM_SIZE:
window_width = LOWORD(lParam);
window_height = HIWORD(lParam);
break;
case WM_TIMER:
KillTimer(hWnd, TIMER_ID);
timeout_expired = true;
break;
case WM_CHAR:
kbd_queue.put((TCHAR) wParam);
break;
// Handle some mouse events, too (1-Oct-2000, Matthew Weathers, Erik Habbestad)
case WM_LBUTTONDOWN:
iClickedMouseX = LOWORD(lParam);
iClickedMouseY = HIWORD(lParam);
bMouseDown = true;
iWhichMouseButton = LEFT_BUTTON;
break;
case WM_LBUTTONUP:
iClickedMouseX = LOWORD(lParam);
iClickedMouseY = HIWORD(lParam);
bMouseUp = true;
iWhichMouseButton = LEFT_BUTTON;
break;
case WM_RBUTTONDOWN:
iClickedMouseX = LOWORD(lParam);
iClickedMouseY = HIWORD(lParam);
bMouseDown = true;
iWhichMouseButton = RIGHT_BUTTON;
break;
case WM_RBUTTONUP:
iClickedMouseX = LOWORD(lParam);
iClickedMouseY = HIWORD(lParam);
bMouseUp = true;
iWhichMouseButton = RIGHT_BUTTON;
break;
case WM_MOUSEMOVE:
iCurrentMouseX = LOWORD(lParam);
iCurrentMouseY = HIWORD(lParam);
break;
default:
return DefWindowProc(hWnd, messg, wParam, lParam);
}
return 0;
}
void closegraph()
{
DestroyWindow(hWnd);
while(handle_input(true));
}
static void detect_mode(int* gd, int* gm)
{
switch (*gd) {
case CGA:
window_height = 200;
switch (*gm) {
case CGAC0:
case CGAC1:
case CGAC2:
case CGAC3:
window_width = 320;
break;
case CGAHI:
window_width = 640;
break;
default:
window_width = 320;
break;
}
break;
case MCGA:
window_height = 200;
switch (*gm) {
case MCGAC0:
case MCGAC1:
case MCGAC2:
case MCGAC3:
window_width = 320;
break;
case MCGAMED:
window_width = 640;
break;
case MCGAHI:
window_width = 640;
window_height = 480;
break;
default:
window_width = 320;
break;
}
break;
case EGA:
window_width = 640;
switch (*gm) {
case EGALO:
window_height = 200;
break;
case EGAHI:
window_height = 350;
break;
default:
window_height = 350;
break;
}
break;
case EGA64:
window_width = 640;
switch (*gm) {
case EGA64LO:
window_height = 200;
break;
case EGA64HI:
window_height = 350;
break;
default:
window_height = 350;
break;
}
break;
case EGAMONO:
window_width = 640;
window_height = 350;
break;
case HERCMONO:
window_width = 720;
window_height = 348;
break;
case ATT400:
window_height = 200;
switch (*gm) {
case ATT400C0:
case ATT400C1:
case ATT400C2:
case ATT400C3:
window_width = 320;
break;
case ATT400MED:
window_width = 640;
break;
case ATT400HI:
window_width = 640;
window_height = 400;
break;
default:
window_width = 320;
break;
}
break;
default:
case DETECT:
*gd = VGA;
*gm = bgiemu_default_mode;
case VGA:
window_width = 900; //Default WIDTH
switch (*gm) {
case VGALO:
window_height = 200;
break;
case VGAMED:
window_height = 350;
break;
case VGAHI:
window_height = 480;
break;
default:
window_height = 640; //DEFAULT HEIGHT
break;
}
break;
case PC3270:
window_width = 720;
window_height = 350;
break;
case IBM8514:
switch (*gm) {
case IBM8514LO:
window_width = 640;
window_height = 480;
break;
case IBM8514HI:
window_width = 1024;
window_height = 768;
break;
default:
window_width = 1024;
window_height = 768;
break;
}
break;
}
}
static void set_defaults()
{
color = text_color = WHITE;
bkcolor = 0;
line_settings.thickness = 1;
line_settings.linestyle = SOLID_LINE;
line_settings.upattern = ~0;
fill_settings.pattern = SOLID_FILL;
fill_settings.color = WHITE;
write_mode = COPY_PUT;
text_settings.direction = HORIZ_DIR;
text_settings.font = DEFAULT_FONT;
text_settings.charsize = 1;
text_settings.horiz = LEFT_TEXT;
text_settings.vert = TOP_TEXT;
text_align_mode = ALIGN_NOT_SET;
active_page = visual_page = 0;
view_settings.left = 0;
view_settings.top = 0;
view_settings.right = window_width-1;
view_settings.bottom = window_height-1;
aspect_ratio_x = aspect_ratio_y = 10000;
}
void initgraph(int* device, int* mode, char const* /*pathtodriver*/,
int size_width, int size_height)
{
int index;
static WNDCLASS wcApp;
gdi_error_code = grOk;
if (wcApp.lpszClassName == NULL) {
wcApp.lpszClassName = "BGIlibrary";
wcApp.hInstance = 0;
wcApp.lpfnWndProc = WndProc;
wcApp.hCursor = LoadCursor(NULL, IDC_ARROW);
wcApp.hIcon = 0;
wcApp.lpszMenuName = 0;
wcApp.hbrBackground = (HBRUSH__ *) GetStockObject(BLACK_BRUSH);
wcApp.style = CS_SAVEBITS;
wcApp.cbClsExtra = 0;
wcApp.cbWndExtra = 0;
if (!RegisterClass(&wcApp)) {
gdi_error_code = GetLastError();
return;
}
pPalette = (NPLOGPALETTE)LocalAlloc(LMEM_FIXED,
sizeof(LOGPALETTE)+sizeof(PALETTEENTRY)*PALETTE_SIZE);
pPalette->palVersion = 0x300;
pPalette->palNumEntries = PALETTE_SIZE;
memset(pPalette->palPalEntry, 0, sizeof(PALETTEENTRY)*PALETTE_SIZE);
for (index = 0; index < BG; index++) {
pPalette->palPalEntry[index].peFlags = PC_EXPLICIT;
pPalette->palPalEntry[index].peRed = index;
pPalette->palPalEntry[PALETTE_SIZE-BG+index].peFlags = PC_EXPLICIT;
pPalette->palPalEntry[PALETTE_SIZE-BG+index].peRed =
PALETTE_SIZE-BG+index;
}
hBackgroundBrush = CreateSolidBrush(PALETTEINDEX(BG));
hBrush[EMPTY_FILL] = (HBRUSH__*) GetStockObject(NULL_BRUSH);
hBrush[SOLID_FILL] =
CreatePatternBrush(CreateBitmap(8, 8, 1, 1, SolidBrushBitmap));
hBrush[LINE_FILL] =
CreatePatternBrush(CreateBitmap(8, 8, 1, 1, LineBrushBitmap));
hBrush[LTSLASH_FILL] =
CreatePatternBrush(CreateBitmap(8, 8, 1, 1, LtslashBrushBitmap));
hBrush[SLASH_FILL] =
CreatePatternBrush(CreateBitmap(8, 8, 1, 1, SlashBrushBitmap));
hBrush[BKSLASH_FILL] =
CreatePatternBrush(CreateBitmap(8, 8, 1, 1, BkslashBrushBitmap));
hBrush[LTBKSLASH_FILL] =
CreatePatternBrush(CreateBitmap(8, 8, 1, 1, LtbkslashBrushBitmap));
hBrush[HATCH_FILL] =
CreatePatternBrush(CreateBitmap(8, 8, 1, 1, HatchBrushBitmap));
hBrush[XHATCH_FILL] =
CreatePatternBrush(CreateBitmap(8, 8, 1, 1, XhatchBrushBitmap));
hBrush[INTERLEAVE_FILL] =
CreatePatternBrush(CreateBitmap(8, 8, 1, 1, InterleaveBrushBitmap));
hBrush[WIDE_DOT_FILL] =
CreatePatternBrush(CreateBitmap(8, 8, 1, 1, WidedotBrushBitmap));
hBrush[CLOSE_DOT_FILL] =
CreatePatternBrush(CreateBitmap(8, 8, 1, 1, ClosedotBrushBitmap));
hBrush[USER_FILL] =
CreatePatternBrush(CreateBitmap(8, 8, 1, 1, SolidBrushBitmap));
}
memcpy(BGIpalette, BGIcolor, sizeof BGIpalette);
current_palette.size = MAXCOLORS+1;
for (index = 10; index <= MAXCOLORS; index++) {
pPalette->palPalEntry[index] = BGIcolor[0];
}
for (index = 0; index <= MAXCOLORS; index++) {
current_palette.colors[index] = index;
pPalette->palPalEntry[index+BG] = BGIcolor[index];
}
hPalette = CreatePalette(pPalette);
detect_mode(device, mode);
set_defaults();
if (size_width) window_width=size_width;
if (size_height) window_height=size_height;
hWnd = CreateWindow("BGIlibrary", "Windows BGI",
WS_OVERLAPPEDWINDOW,
0, 0, window_width+BORDER_WIDTH,
window_height+BORDER_HEIGHT,
(HWND)NULL, (HMENU)NULL,
0, NULL);
if (hWnd == NULL) {
gdi_error_code = GetLastError();
return;
}
ShowWindow(hWnd, *mode == VGAMAX ? SW_SHOWMAXIMIZED : SW_SHOWNORMAL);
UpdateWindow(hWnd);
}
void graphdefaults()
{
set_defaults();
for (int i = 0; i <= MAXCOLORS; i++) {
current_palette.colors[i] = i;
BGIpalette[i] = BGIcolor[i];
}
SetPaletteEntries(hPalette, BG, MAXCOLORS+1, BGIpalette);
RealizePalette(hdc[0]);
SetTextColor(hdc[0], PALETTEINDEX(text_color+BG));
SetTextColor(hdc[1], PALETTEINDEX(text_color+BG));
SetBkColor(hdc[0], PALETTEINDEX(BG));
SetBkColor(hdc[1], PALETTEINDEX(BG));
SelectClipRgn(hdc[0], NULL);
SelectClipRgn(hdc[1], NULL);
SetViewportOrgEx(hdc[0], 0, 0, NULL);
SetViewportOrgEx(hdc[1], 0, 0, NULL);
SelectObject(hdc[0], hBrush[fill_settings.pattern]);
SelectObject(hdc[1], hBrush[fill_settings.pattern]);
moveto(0,0);
}
void restorecrtmode() {}
/////////////////////////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -