win_vid.c
来自「经典的老游戏digger的源代码.提起digger相信很多人会回忆起曾经为了它挑」· C语言 代码 · 共 1,254 行 · 第 1/3 页
C
1,254 行
/* Digger Remastered
Copyright (c) Andrew Jenner 1998-2004 */
#include "def.h"
#include "win_dig.h"
#include "hardware.h"
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include "win_vid.h"
#include "resource.h"
#include "sprite.h"
struct FULLSCREEN_VID_MODE_INFO* supported_video_modes=NULL; // list of supported video modes (modes that are 640+ x 400+)
struct FULLSCREEN_VID_MODE_INFO preferred_video_mode; // the vid mode to use when running fullscreen
#define NUM_SPRITES_TO_CACHE 150
int use_direct_draw=0;
HBITMAP title_bitmap[2]={(HBITMAP) NULL, (HBITMAP) NULL};
RGBQUAD vga16_pal1_rgbq[] = {{0,0,0,0}, /* palette1, normal intensity */
{128,0,0,0},
{0,128,0,0},
{128,128,0,0},
{0,0,128,0},
{128,0,128,0},
{0,64,128,0},
{128,128,128,0},
{64,64,64,0},
{255,0,0,0},
{0,255,0,0},
{255,255,0,0},
{0,0,255,0},
{255,0,255,0},
{0,255,255,0},
{255,255,255,0}};
RGBQUAD vga16_pal1i_rgbq[] = {{0,0,0,0}, /* palette1, high intensity */
{255,0,0,0},
{0,255,0,0},
{255,255,0,0},
{0,0,255,0},
{255,0,255,0},
{0,128,255,0},
{192,192,192,0},
{128,128,128,0},
{255,128,128,0},
{128,255,128,0},
{255,255,128,0},
{128,128,255,0},
{255,128,255,0},
{128,255,255,0},
{255,255,255,0}};
RGBQUAD vga16_pal2_rgbq[] = {{0,0,0,0}, /* palette2, normal intensity */
{0,128,0,0},
{0,0,128,0},
{0,64,128,0},
{128,0,0,0},
{128,128,0,0},
{128,0,128,0},
{128,128,128,0},
{64,64,64,0},
{0,255,0,0},
{0,0,255,0},
{0,255,255,0},
{255,0,0,0},
{255,255,0,0},
{255,0,255,0},
{255,255,255,0}};
RGBQUAD vga16_pal2i_rgbq[] = {{0,0,0,0}, /* palette2, high intensity */
{0,255,0,0},
{0,0,255,0},
{0,128,255,0},
{255,0,0,0},
{255,255,0,0},
{255,0,255,0},
{192,192,192,0},
{128,128,128,0},
{128,255,128,0},
{128,128,255,0},
{128,255,255,0},
{255,128,128,0},
{255,255,128,0},
{255,128,255,0},
{255,255,255,0}};
RGBQUAD cga16_pal1_rgbq[] = {{0,0,0,0}, /* palette1, normal intensity */
{0,168,0,0},
{0,0,168,0},
{0,84,168,0},
{0,0,128,0},
{128,0,128,0},
{0,64,128,0},
{128,128,128,0},
{64,64,64,0},
{255,0,0,0},
{0,255,0,0},
{255,255,0,0},
{0,0,255,0},
{255,0,255,0},
{0,255,255,0},
{255,255,255,0}};
RGBQUAD cga16_pal1i_rgbq[] = {{0,0,0,0}, /* palette1, high intensity */
{85,255,85,0},
{85,85,255,0},
{85,255,255,0},
{0,0,255,0},
{255,0,255,0},
{0,128,255,0},
{192,192,192,0},
{128,128,128,0},
{255,128,128,0},
{128,255,128,0},
{255,255,128,0},
{128,128,255,0},
{255,128,255,0},
{128,255,255,0},
{255,255,255,0}};
RGBQUAD cga16_pal2_rgbq[] = {{0,0,0,0}, /* palette2, normal intensity */
{0,128,0,0},
{128,0,128,0},
{160,160,160,0},
{160,160,160,0},
{128,128,0,0},
{128,0,128,0},
{128,128,128,0},
{64,64,64,0},
{0,255,0,0},
{0,0,255,0},
{0,255,255,0},
{255,0,0,0},
{255,255,0,0},
{255,0,255,0},
{255,255,255,0}};
RGBQUAD cga16_pal2i_rgbq[] = {{0,0,0,0}, /* palette2, high intensity */
{0,255,0,0},
{0,0,255,0},
{160,160,160,0},
{255,0,0,0},
{255,255,0,0},
{255,0,255,0},
{192,192,192,0},
{128,128,128,0},
{128,255,128,0},
{128,128,255,0},
{128,255,255,0},
{255,128,128,0},
{255,255,128,0},
{255,128,255,0},
{255,255,255,0}};
RGBQUAD *windowed_palette[4]; /* Used in Windowed mode. These palettes are applied to the 'back_bitmap' */
HPALETTE desktop_palette=(HPALETTE) NULL; /* Used in Windowed mode, but is only if Windows is set to a color resolution which supports palettes. {ie. 256 colors} ) */
bool palettized_desktop=FALSE;
LPDIRECTDRAWPALETTE fullscreen_palette[4] = {NULL, NULL, NULL, NULL}; /* Used in Full Screen mode. These palettes are applied to the DirectDraw primary surface. */
LPDIRECTDRAW g_pDD = NULL; /* DirectDraw object */
LPDIRECTDRAWSURFACE g_pDDSPrimary = NULL; /* DirectDraw primary surface */
//LPDIRECTDRAWSURFACE g_pDDSBack = NULL; /* DirectDraw back surface */
RECT g_rcWindow; /* Saves the window size & pos.*/
RECT g_rcViewport; /* Pos. & size to blt from */
RECT g_rcScreen; /* Screen pos. for blt */
BOOL g_bActive = FALSE;
BOOL g_bReady = FALSE; /* App is ready for updates */
BOOL g_bWindowed = TRUE; /* App is in windowed mode */
RECT rc_draw_area;
RECT rc_640x400;
bool use_async_screen_updates;
enum video_mode_enum video_mode= VIDEO_MODE_VGA_16;
int cur_intensity;
int cur_palette;
HDC back_dc;
HBITMAP back_bitmap;
char *back_bitmap_bits;
HBITMAP old_bitmap;
bool use_640x480_fullscreen;
Uint3 *sprite_data=NULL;
Uint3 *sprite_array[200];
HRESULT blit_to_window(void);
void vgaclear(void);
void destroy_palettes();
void init_palettes();
void init_graphics()
{
HDC window_dc;
window_dc = GetDC(hWnd);
init_directdraw();
create_back_buffer(window_dc);
ReleaseDC(hWnd, window_dc);
init_surfaces();
}
void graphicsoff(void) {};
void gretrace(void)
{
};
/********************************************************/
/* Functions for displaying the VGA data */
/********************************************************/
void vgainit(void)
{
video_mode=VIDEO_MODE_VGA_16;
windowed_palette[0] = vga16_pal1_rgbq;
windowed_palette[1] = vga16_pal1i_rgbq;
windowed_palette[2] = vga16_pal2_rgbq;
windowed_palette[3] = vga16_pal2i_rgbq;
destroy_palettes();
init_palettes();
}
void vgaclear(void)
{
memset(back_bitmap_bits, 0, 256000l);
blit_to_window();
};
void vgapal(Sint4 pal)
{
cur_palette=pal;
if (g_bWindowed || preferred_video_mode.bpp > 8)
{
SetDIBColorTable(back_dc,0,16, windowed_palette[cur_palette*2 + cur_intensity]);
if (use_async_screen_updates)
InvalidateRect(hWnd, NULL, FALSE);
else
blit_to_window();
}
else
{
IDirectDrawSurface_SetPalette(g_pDDSPrimary, fullscreen_palette[cur_palette*2 + cur_intensity]);
SetDIBColorTable(back_dc,0,16, windowed_palette[cur_palette*2 + cur_intensity]);
}
};
void vgainten(Sint4 inten)
{
cur_intensity=inten;
if (g_bWindowed || preferred_video_mode.bpp > 8)
{
SetDIBColorTable(back_dc,0,16, windowed_palette[cur_palette*2 + cur_intensity]);
if (use_async_screen_updates)
InvalidateRect(hWnd, NULL, FALSE);
else
blit_to_window();
}
else
{
IDirectDrawSurface_SetPalette(g_pDDSPrimary, fullscreen_palette[cur_palette*2 + cur_intensity]);
SetDIBColorTable(back_dc,0,16, windowed_palette[cur_palette*2 + cur_intensity]);
}
};
Sint4 vgagetpix(Sint4 x,Sint4 y)
{
Uint4 xi,yi;
Sint4 rval;
rval=0;
if (x>319||y>199)
{
return 0xff;
}
for (yi=0;yi<2;yi++)
for (xi=0;xi<8;xi++)
if (back_bitmap_bits[(Uint5) ((y*2l+yi)*640l + x*2l + xi)])
rval |= 0x80 >> xi;
rval &= 0xee;
return rval;
};
void vgawrite(Sint4 x,Sint4 y,Sint4 ch,Sint4 c)
{
Uint5 yi;
Uint5 xi;
int color;
ch-=32;
if (ch<0x5f)
{
if (ascii2vga[ch])
{
for (yi=0;yi<24;yi++)
{
for (xi=0;xi<24;xi++)
{
if (xi&0x1)
color=(ascii2vga[ch][yi*12+(xi>>1)] & 0x0F);
else
color=(ascii2vga[ch][yi*12+(xi>>1)] >> 4);
if (color==10)
if (c==2)
color=12;
else
{
if (c==3)
color=14;
}
else
if (color==12)
if (c==1)
color=2;
else
if (c==2)
color=4;
else
if (c==3)
color=6;
back_bitmap_bits[(Uint5) ((y*2+yi)*640l + x*2l+xi)]=color;
}
}
}
else /* draw a space (needed when reloading and displaying high scores when user switches to/from normal/gauntlet mode, etc ) */
for (yi=0;yi<24;yi++)
memset(&back_bitmap_bits[(y*2+yi)*640 + x*2] , 0, 24);
}
blit_rect_to_window(x*2, y*2, 24, 24);
}
void cgawrite(Sint4 x,Sint4 y,Sint4 ch,Sint4 c)
{
Uint5 yi;
Uint5 xi;
int color;
ch-=32;
if (ch<0x5f)
{
if (ascii2cga[ch])
{
for (yi=0;yi<12;yi++)
{
for (xi=0;xi<12;xi++)
{
if (xi&0x1)
color=(ascii2cga[ch][yi*6+(xi>>1)] & 0x0F);
else
color=(ascii2cga[ch][yi*6+(xi>>1)] >> 4);
color=color&c;
farmemset(back_bitmap_bits + ((y*2+yi*2)*640l + x*2+xi*2), color,2);
farmemset(back_bitmap_bits + ((y*2+yi*2+1)*640l + x*2+xi*2), color,2);
}
}
}
else /* draw a space (needed when reloading and displaying high scores when user switches to/from normal/gauntlet mode, etc ) */
for (yi=0;yi<24;yi++)
memset(&back_bitmap_bits[(y*2+yi)*640 + x*2] , 0, 24);
}
blit_rect_to_window(x*2, y*2, 24, 24);
}
void vgatitle(void)
{
display_title_bitmap(0);
}
void cgatitle(void)
{
display_title_bitmap(1);
}
void display_title_bitmap(int idx)
{
HDC temp_dc;
HBITMAP old_bitmap;
GdiFlush();
temp_dc = CreateCompatibleDC(back_dc);
if (palettized_desktop)
{
SelectPalette(temp_dc, desktop_palette, FALSE);
RealizePalette(temp_dc);
}
old_bitmap=SelectObject(temp_dc, title_bitmap[idx]);
BitBlt(back_dc, 0, 0, 640, 400, temp_dc, 0, 0, SRCCOPY);
GdiFlush();
SelectObject(temp_dc, old_bitmap);
DeleteDC(temp_dc);
blit_to_window();
}
/* blit entire backbuffer to the window */
HRESULT blit_to_window()
{
HRESULT hresult;
DDSURFACEDESC ddsd;
int x,y;
RECT nodraw;
POINT pt;
ddsd.dwSize=sizeof(ddsd);
hresult=DD_OK;
if (!g_bWindowed && !g_bActive)
return hresult;
if (g_bWindowed || preferred_video_mode.bpp > 8)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?