home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
DOS/V Power Report 1997 March
/
VPR9703A.ISO
/
VPR_DATA
/
DOGA
/
SOURCES
/
REND.LZH
/
REND
/
CRTWIN.C
< prev
next >
Wrap
C/C++ Source or Header
|
1996-07-16
|
17KB
|
677 lines
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#undef FLOAT
#include <windows.h>
#define crtprintf __crtprintf
#include "reader.h"
#undef crtprintf
#include "glib.h"
#include "rend.h"
#include "rend.rh"
char *program = "Do-GA C.G.A System Rendering Program" ;
static char *RendClass = "REND";
static char *RendTitle = "REND for Windows";
#define MESSAGES 5
#define MAXARGV 256
#define WIDTH 512
#define ID_COPY 2000
#define ID_SAVE 2001
static HWND HMainWindow = NULL;
static HWND HText[MESSAGES];
static int winx, winy;
static int borderx, bordery;
static int offsetx = 0, offsety = (16*MESSAGES);
static int maxy = 0;
#define WINX (winx+borderx+offsetx)
#define WINY (winy+bordery+offsety)
static HDC dc = NULL;
static HDC graphdc = NULL;
static HPALETTE hpal = NULL;
static BYTE *pBuffer;
static BITMAPINFO *pbmi;
static LOGPALETTE *plgpl;
static int headersize, imagesize;
static BYTE *err_r, *err_g, *err_b;
static BYTE *err_nr, *err_ng, *err_nb;
static enum {PAL8, RGB16, RGB24} crtmode = PAL8;
char profile[128];
static int posx = CW_USEDEFAULT, posy = 0;
void ReadProfile(HANDLE hInstance)
{
char *p;
GetModuleFileName(hInstance, profile, sizeof(profile));
if ((p = strrchr(profile, '.')) == NULL) {
p = profile + strlen(profile);
}
strcpy(p, ".ini");
posx = GetPrivateProfileInt("Rend", "PositionX", posx, profile);
posy = GetPrivateProfileInt("Rend", "PositionY", posy, profile);
}
void WriteProfile(void)
{
char buf[128];
#if 0
RECT rc;
GetWindowRect(HMainWindow, &rc);
sprintf(buf, "%d", rc.left);
WritePrivateProfileString("Rend", "PositionX", buf, profile);
sprintf(buf, "%d", rc.top);
WritePrivateProfileString("Rend", "PositionY", buf, profile);
#else
sprintf(buf, "%d", posx);
WritePrivateProfileString("Rend", "PositionX", buf, profile);
sprintf(buf, "%d", posy);
WritePrivateProfileString("Rend", "PositionY", buf, profile);
#endif
}
int CreateMessageWindow(HANDLE hInstance, int nCmdShow)
{
int i;
int sx, sy;
HMENU sysmenu;
sx = GetSystemMetrics(SM_CXSCREEN);
sy = GetSystemMetrics(SM_CYSCREEN);
winx = WIDTH;
winy = 0;
borderx = GetSystemMetrics(SM_CXFRAME) + GetSystemMetrics(SM_CXBORDER);
bordery = GetSystemMetrics(SM_CYFRAME) + GetSystemMetrics(SM_CYCAPTION) + 3;
if (posx + WINX > sx) posx = sx - WINX;
if (posy + WINY > sy) posy = sy - WINY;
if (posx < 0) posx = 0;
if (posy < 0) posy = 0;
HMainWindow = CreateWindow(RendClass, RendTitle,
(WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU |
WS_THICKFRAME | WS_MINIMIZEBOX),
posx, posy, WINX, WINY,
HWND_DESKTOP, NULL, hInstance, NULL);
if(HMainWindow==NULL) return FALSE;
sysmenu = GetSystemMenu(HMainWindow, FALSE);
InsertMenu(sysmenu, 0, MF_BYPOSITION|MF_STRING, ID_COPY, "コピー(&C)");
InsertMenu(sysmenu, 1, MF_BYPOSITION|MF_STRING, ID_SAVE, "名前を付けて保存(&A)");
InsertMenu(sysmenu, 2, MF_BYPOSITION|MF_SEPARATOR, -1, NULL);
for (i = 0; i < MESSAGES; ++i) {
HText[i] = CreateWindow("STATIC", "", WS_CHILD | WS_VISIBLE, 0, i*16, WIDTH, 16, HMainWindow, NULL, hInstance, NULL);
}
ShowWindow(HMainWindow, nCmdShow);
UpdateWindow(HMainWindow);
return TRUE;
}
LRESULT _export CALLBACK RendProc(
HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
WriteProfile();
break;
case WM_DESTROY:
if (dc != NULL) {
ReleaseDC(HMainWindow, dc);
dc = NULL;
}
if (hpal != NULL) {
DeleteObject(hpal);
hpal = NULL;
}
PostQuitMessage(0);
return 0;
case WM_MOVE:
{
WINDOWPLACEMENT wndpl;
memset(&wndpl, 0, sizeof(WINDOWPLACEMENT));
wndpl.length = sizeof(WINDOWPLACEMENT);
GetWindowPlacement(HMainWindow, &wndpl);
if (wndpl.showCmd == SW_NORMAL) {
posx = (int) LOWORD(lParam); /* 水平方向の位置 */
posy = (int) HIWORD(lParam); /* 垂直方向の位置 */
}
}
return 0;
case WM_ACTIVATE:
if (dc && crtmode == PAL8) {
RealizePalette(dc);
}
return 0;
case WM_PAINT:
if (dc != NULL && (graphdc == dc) && maxy > 0 && pBuffer != NULL
&& (crtmode != PAL8 || pbmi != NULL)) {
SetDIBitsToDevice(dc,
offsetx, offsety, winx, maxy,
0, winy - maxy,
0, winy, pBuffer, pbmi, DIB_RGB_COLORS);
}
break;
case WM_PALETTECHANGED:
if((HWND)wParam==hwnd) return 0L;
if (dc != NULL && graphdc == dc && crtmode == PAL8) {
RealizePalette(dc);
/* InvalidateRect(hwnd, NULL, FALSE);*/
}
break;
case WM_QUERYNEWPALETTE:
if (dc && graphdc == dc && crtmode == PAL8) {
RealizePalette(dc);
InvalidateRect(hwnd, NULL, FALSE);
}
return 0;
case WM_SYSCOMMAND:
case WM_COMMAND:
if (wParam == ID_COPY) {
HGLOBAL hmem;
BYTE *p;
int l;
hmem = GlobalAlloc(GMEM_MOVEABLE, headersize + imagesize);
p = GlobalLock(hmem);
memcpy(p, pbmi, headersize);
memcpy(p + headersize, pBuffer, imagesize);
GlobalUnlock(hmem);
OpenClipboard(hwnd);
EmptyClipboard();
SetClipboardData(CF_DIB, hmem);
CloseClipboard();
return 0;
} else if (wParam == ID_SAVE) {
char szFile[256];
OPENFILENAME ofn;
char chReplace;
int i;
memset(&ofn, 0, sizeof(OPENFILENAME));
ofn.lStructSize=sizeof(OPENFILENAME);
ofn.hwndOwner=hwnd;
ofn.lpstrFilter="*.BMP\0";
ofn.nFilterIndex=1;
ofn.lpstrFile=szFile;
szFile[0] = '\0';
ofn.nMaxFile=256;
ofn.lpstrFileTitle=NULL;
ofn.lpstrInitialDir=NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT;
ofn.lpstrDefExt = "BMP";
if (GetSaveFileName(&ofn)) {
FILE *fp;
if ((fp = fopen(szFile, "wb")) == NULL) {
MessageBox(hwnd, "ファイル出力に失敗しました",
RendTitle, MB_ICONEXCLAMATION | MB_OK);
} else {
BITMAPFILEHEADER bfh;
memset(&bfh, 0, sizeof(BITMAPFILEHEADER));
bfh.bfType = 'BM';
bfh.bfSize = (sizeof(BITMAPFILEHEADER)+headersize+imagesize+3)/4;
bfh.bfOffBits = sizeof(BITMAPFILEHEADER)+headersize;
fwrite(&bfh, 1, sizeof(BITMAPFILEHEADER), fp);
fwrite(pbmi, 1, headersize, fp);
fwrite(pBuffer, 1, imagesize, fp);
fclose(fp);
}
}
return 0;
}
break;
}
return DefWindowProc(
hwnd, msg, wParam, lParam);
}
int RegisterRendClass(HANDLE hInstance)
{
WNDCLASS wc;
wc.style=CS_GLOBALCLASS;
wc.lpfnWndProc=RendProc;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hInstance=hInstance;
wc.hIcon=LoadIcon(hInstance, MAKEINTRESOURCE(ID_REND));
wc.hCursor=NULL;
wc.hbrBackground=(HBRUSH)(COLOR_WINDOW);
wc.lpszMenuName=NULL;
wc.lpszClassName=RendClass;
return RegisterClass(&wc);
}
int PASCAL WinMain ( HANDLE hInstance, HANDLE hPrevInstance,
LPSTR lpszCmdParam, int nCmdShow )
{
extern int Main(int argc, char *argv[]);
int argc;
char *argv[MAXARGV], *param, *p;
param = malloc(strlen(lpszCmdParam)+1);
strcpy(param, lpszCmdParam);
argc = 1;
argv[0] = param;
for (p = param; *p; ++p) {
if (isspace(*p)) {
*p = '\0';
while (p[1] != '\0' && isspace(p[1])) {
p++;
}
if (p[1] == '\0') break;
argv[argc++] = p+1;
}
}
argv[argc] = NULL;
if(hPrevInstance==NULL) {
RegisterRendClass(hInstance);
}
ReadProfile(hInstance);
CreateMessageWindow(hInstance, nCmdShow);
Main(argc, argv);
if (nflag) {
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
WriteProfile();
return 0;
}
void check_winmessage(void)
{
MSG msg;
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
if (msg.message == WM_QUIT) {
exit(1);
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
static int status = 0;
void crtmessage(char *str)
{
int i;
if (offsety == 0) {
check_winmessage();
return;
}
if (strncmp(str, "Read", 4) == 0) {
SetWindowText(HText[0], str);
for (i = 1; i < MESSAGES; ++i) {
SetWindowText(HText[i], "");
}
status = 1;
} else {
if (status == MESSAGES) {
char buf[128];
for (i = 1; i < MESSAGES-1; ++i) {
GetWindowText(HText[i+1], buf, 128);
SetWindowText(HText[i], buf);
}
SetWindowText(HText[MESSAGES-1], str);
} else {
SetWindowText(HText[status++], str);
}
}
check_winmessage();
}
void palinit(void)
{
int i;
RGBQUAD *p;
PALETTEENTRY *pal;
dc = GetDC(HMainWindow);
if (graphdc == NULL) {
graphdc = dc;
}
pbmi = malloc(sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*256);
pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
pbmi->bmiHeader.biWidth = winx;
pbmi->bmiHeader.biHeight = winy;
pbmi->bmiHeader.biPlanes = 1;
pbmi->bmiHeader.biBitCount = 8;
pbmi->bmiHeader.biCompression = BI_RGB;
pbmi->bmiHeader.biSizeImage = 0;
pbmi->bmiHeader.biClrUsed = 0;
pbmi->bmiHeader.biClrImportant = 0;
if (GetDeviceCaps(dc, RASTERCAPS) & RC_PALETTE) {
crtmode = PAL8;
headersize = sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD)*256;
imagesize = ((winx+3)/4) * 4 * winy;
pBuffer = malloc(imagesize);
err_r = malloc((winx+2)*6);
err_g = err_r + (winx+2);
err_b = err_g + (winx+2);
err_nr = err_b + (winx+2);
err_ng = err_nr + (winx+2);
err_nb = err_ng + (winx+2);
plgpl = malloc(sizeof(LOGPALETTE) + sizeof(PALETTEENTRY)*256);
pbmi->bmiHeader.biBitCount = 8;
pbmi->bmiHeader.biClrUsed = 216;
plgpl->palVersion = 0x300;
plgpl->palNumEntries = 216;
p = pbmi->bmiColors;
pal = plgpl->palPalEntry;
#if 0
pal->peBlue = p->rgbBlue = 0;
pal->peGreen = p->rgbGreen = 0;
pal->peRed = p->rgbRed = 0;
pal->peFlags = p->rgbReserved = 0;
p++;
pal++;
#endif
for (i = 0; i < 6; ++i) {
int j;
for (j = 0; j < 6; ++j) {
int k;
for (k = 0; k < 6; ++k) {
pal->peBlue = p->rgbBlue = (BYTE)k*51;
pal->peGreen = p->rgbGreen = (BYTE)j*51;
pal->peRed = p->rgbRed = (BYTE)i*51;
pal->peFlags = p->rgbReserved = 0;
p++;
pal++;
}
}
}
hpal = CreatePalette(plgpl);
if (dc == graphdc) {
SelectPalette(dc, hpal, FALSE);
RealizePalette(dc);
}
} else if (GetDeviceCaps(dc, BITSPIXEL) < 24) {
crtmode = RGB16;
headersize = sizeof(BITMAPINFOHEADER);
imagesize = ((winx*3+3)/4)* 4 * winy;
pBuffer = malloc(imagesize);
pbmi->bmiHeader.biBitCount = 24;
} else {
crtmode = RGB24;
headersize = sizeof(BITMAPINFOHEADER);
imagesize = ((winx*3+3)/4)* 4 * winy;
pBuffer = malloc(imagesize);
pbmi->bmiHeader.biBitCount = 24;
}
}
void crtinit( line )
int line ;
{
extern int antiareas ; /* アンチエリアシング */
extern int XPixel, YPixel ; /* ピクセル数 */
int i;
int sx, sy;
RECT rc;
WINDOWPLACEMENT wndpl;
for (i = 0; i < MESSAGES; ++i) {
SetWindowText(HText[i], "");
}
status = 0;
sx = GetSystemMetrics(SM_CXSCREEN);
sy = GetSystemMetrics(SM_CYSCREEN);
GetWindowRect(HMainWindow, &rc);
memset(&wndpl, 0, sizeof(WINDOWPLACEMENT));
wndpl.length = sizeof(WINDOWPLACEMENT);
GetWindowPlacement(HMainWindow, &wndpl);
if (wndpl.showCmd == SW_NORMAL) {
posx = rc.left;
posy = rc.top;
}
if (posx + WINX > sx) posx = sx - WINX;
if (posy + WINY > sy) posy = sy - WINY;
if (posx < 0) posx = 0;
if (posy < 0) posy = 0;
winx = XPixel / antiareas;
winy = YPixel / antiareas;
if (graphdc == dc) {
if (nflag) {
offsety = 0;
for (i = 0; i < MESSAGES; ++i) {
ShowWindow(HText[i], SW_HIDE);
}
} else {
offsety = 16 * MESSAGES;
}
MoveWindow(HMainWindow, posx, posy, WINX, WINY, TRUE);
} else {
MoveWindow(HMainWindow, posx, posy, WINX, bordery + MESSAGES*16, TRUE);
}
maxy = 0;
palinit();
check_winmessage();
}
/* CRTのクリア */
void crtclr()
{
int i;
// HDC dc;
HGDIOBJ hgdiold;
// HPALETTE hpal;
// dc = GetDC(HMainWindow);
if (dc != NULL) {
if (dc == graphdc) {
hgdiold = SelectObject(dc,(HGDIOBJ)COLOR_WINDOW);
Rectangle(dc, 0, 0, winx, WINY);
SelectObject(dc, hgdiold);
if (crtmode == PAL8) RealizePalette(dc);
} else {
hgdiold = SelectObject(dc,(HGDIOBJ)COLOR_WINDOW);
Rectangle(dc, 0, 0, winx, 16*MESSAGES);
SelectObject(dc, hgdiold);
hgdiold = SelectObject(graphdc,(HGDIOBJ)COLOR_WINDOW);
Rectangle(graphdc, offsetx, offsety, winx, winy);
SelectObject(graphdc, hgdiold);
}
}
// DeleteObject(hpal);
// ReleaseDC(HMainWindow, dc);
for (i = 0; i < MESSAGES; ++i) {
SetWindowText(HText[i], "");
}
status = 0;
maxy = 0;
if (crtmode == PAL8) {
memset(err_r, 0, winx+2); memset(err_g, 0, winx+2); memset(err_b, 0, winx+2);
memset(err_nr, 0, winx+2); memset(err_ng, 0, winx+2); memset(err_nb, 0, winx+2);
}
check_winmessage();
}
void crtout24(framebuf, xlen, y)
ColorCode* framebuf ;
int xlen ;
int y ;
{
int x;
BYTE *p;
if (crtmode == PAL8) {
int er = 0, eg = 0, eb = 0, ee;
int r,g,b;
p = err_nr; err_nr = err_r; err_r = p;
p = err_ng; err_ng = err_g; err_g = p;
p = err_nb; err_nb = err_b; err_b = p;
memset(err_nr, 0, winx+2); memset(err_ng, 0, winx+2); memset(err_nb, 0, winx+2);
p = pBuffer + winx * (winy - 1 - y);
#define STEP 43
#define STEP 32
for (x = 0; x < xlen; ++x) {
ee = ((framebuf[x] >> 16) & 255) + er + err_r[x+1];
r = ee / STEP; ee = ee % STEP;
er = (ee * 3 + 4) / 8;
err_nr[x+1] += er;
err_nr[x+2] += ee - er*2;
ee = ((framebuf[x] >> 24) & 255) + eg + err_g[x+1];
g = ee / STEP; ee = ee % STEP;
eg = (ee * 3 + 4) / 8;
err_ng[x+1] += eg;
err_ng[x+2] += ee - eg*2;
ee = ((framebuf[x] >> 8) & 255) + eb + err_b[x+1];
b = ee / STEP; ee = ee % STEP;
eb = (eb * 3 + 4) / 8;
err_nb[x+1] += eb;
err_nb[x+2] += ee - eb*2;
if (--r < 0) r = 0;
if (r > 5) r = 5;
if (--g < 0) g = 0;
if (g > 5) g = 5;
if (--b < 0) b = 0;
if (b > 5) b = 5;
*p++ = (BYTE)(r * 36 + g * 6 + b);
}
p = pBuffer + winx * (winy - 1 - y);
#define DRAWUNIT 16
#if 1
if (y+1 == winy || ((y+1) % DRAWUNIT) == 0) {
int line;
line = (y % DRAWUNIT) + 1;
SetDIBitsToDevice(graphdc, offsetx, offsety + (y-line+1), winx, line, 0, 0,
0, line, p, pbmi, DIB_RGB_COLORS);
}
#else
SetDIBitsToDevice(graphdc, offsetx, offsety + y, winx, 1, 0, 0,
0, 1, p, pbmi, DIB_RGB_COLORS);
#endif
} else if (crtmode == RGB24) {
p = pBuffer + winx * (winy - 1 - y) * 3;
for (x = 0; x < xlen; ++x) {
*p++ = (framebuf[x] >> 8) & 255;
*p++ = (framebuf[x] >> 24) & 255;
*p++ = (framebuf[x] >> 16) & 255;
}
p = pBuffer + winx * (winy - 1 - y) * 3;
#if 1
if (y+1 == winy || ((y+1) % DRAWUNIT) == 0) {
int line;
line = (y % DRAWUNIT) + 1;
SetDIBitsToDevice(graphdc, offsetx, offsety + (y-line+1), winx, line, 0, 0,
0, line, p, pbmi, DIB_RGB_COLORS);
}
#else
SetDIBitsToDevice(graphdc, offsetx, offsety + y, winx, 1, 0, 0,
0, 1, p, pbmi, DIB_RGB_COLORS);
#endif
}
maxy = y+1;
check_winmessage();
}
/* CRT出力 */
void crtout( framebuf, xlen, y )
unsigned short *framebuf ;
int xlen ;
int y ;
{
if (crtmode == RGB16) {
int x;
BYTE *p;
p = pBuffer + winx * (winy - 1 - y) * 3;
for (x = 0; x < xlen; ++x) {
*p++ = (framebuf[x] & 0x003e) << 2;
*p++ = (framebuf[x] & 0xf800) >> 8;
*p++ = (framebuf[x] & 0x07c0) >> 3;
}
p = pBuffer + winx * (winy - 1 - y) * 3;
#if 1
if (y+1 == winy || ((y+1) % DRAWUNIT) == 0) {
int line;
line = (y % DRAWUNIT) + 1;
SetDIBitsToDevice(graphdc, offsetx, offsety + (y-line+1), winx, line, 0, 0,
0, line, p, pbmi, DIB_RGB_COLORS);
}
#else
SetDIBitsToDevice(graphdc, offsetx, offsety + y, winx, 1, 0, 0,
0, 1, p, pbmi, DIB_RGB_COLORS);
#endif
}
}
void crtline( x1, y1, x2, y2 )
int x1, y1, x2, y2 ;
{
// HDC dc;
x1 += offsetx;
x2 += offsetx;
y1 += offsety;
y2 += offsety;
// dc = GetDC(HMainWindow);
MoveToEx(graphdc, x1, y1, NULL);
LineTo(graphdc, x2, y2);
// ReleaseDC(HMainWindow, dc);
}
static char buf[1024];
int printwarning(const char *format, ...)
{
va_list arglist;
va_start(arglist, format);
vsprintf(buf, format, arglist);
va_end(arglist);
MessageBox(HMainWindow, buf, RendTitle, MB_ICONEXCLAMATION | MB_OK);
return 0;
}
int crtprintf(FILE * fp, const char * format, ...)
{
int l;
va_list arglist;
va_start(arglist, format);
l = vsprintf(buf, format, arglist);
va_end(arglist);
if (fp == stderr || fp == stdout) {
crtmessage(buf);
} else {
fputs(buf, fp);
}
return l;
}