这是我程序的主要功能。此代码有效,但是如果我将包含许多If
和LineTo()
调用的2 MoveToEx()
个语句放在For
循环中,那么屏幕上就不会显示图形输出。我尝试将GetDC()
和ReleaseDC()
调用放在For
循环内,但没有做任何更改。请帮忙。抱歉,格式化是如此混乱。它通过3个不同的文本编辑器和一个Web浏览器。
// includes
#include <stdafx.h>
#include <windows.h>
#include <windowsx.h>
#include <string>
#include <vector>
// namespaces
using std::string;
using std::vector;
// defines
#define STARTX 1280/2
#define STARTY 720/2
#define CLIENT_WIDTH 1280
#define CLIENT_HEIGHT 720
#define SEGMENT_LENGTH 5
#define SEGMENT_WIDTH 5
#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
// globals
string szinput("sl");
int iterations=2; int direction=1;
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance,
LPSTR lpcmdline, int ncmdshow)
{
HWND hwnd;
MSG msg;
WNDCLASSEX wc;
HDC hdc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize=sizeof(WNDCLASSEX);
wc.style=CS_HREDRAW|CS_VREDRAW;
wc.lpfnWndProc=WindowProc;
wc.hInstance=hinstance;
wc.hCursor=LoadCursor(NULL, IDC_ARROW);
wc.lpszClassName=L"Fractals";
if(!RegisterClassEx(&wc))
{
return(0);
}
if(!(hwnd=CreateWindow(L"Fractals", L"Fractals",
WS_OVERLAPPEDWINDOW|WS_VISIBLE, 0, 0, CLIENT_WIDTH,
CLIENT_HEIGHT, NULL, NULL, hinstance, NULL)))
{
return(0);
}
// main game loop
HPEN pen=CreatePen(PS_SOLID, SEGMENT_WIDTH, RGB(0, 255, 0));
POINT point;
//int center=CLIENT_WIDTH/CLIENT_HEIGHT;
int offsetx=10, offsety=10;
int x=STARTX, y=STARTY;
int flag=0;
int temp;
//while(TRUE)
//{
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if(msg.message==WM_QUIT)
{
//break;
}
hdc=GetDC(hwnd);
SelectObject(hdc, pen);
// place the pen on the starting point specified by user
//if(!flag)
//{
//SelectObject(hdc, pen);
MoveToEx(hdc, STARTX, STARTY, &point);
x=STARTX;
y=STARTY;
//flag=1;
//}
vector<int> result=fngetParse();
//for(int i=iterations; i<iterations; i++)
//{
//hdc=GetDC(hwnd);
// if(!flag)
// {
// SelectObject(hdc, pen);
// MoveToEx(hdc, STARTX, STARTY, &point);
// x=STARTX;
// y=STARTY;
// flag=1;
// }
// SelectObject(hdc, pen);
temp=result.at(0);
if(temp==0)
{
if(direction==1) // flat
{
x+=10; y+=0;
LineTo(hdc, x, y);
direction=1;
MoveToEx(hdc, x, y, &point);
} else if(direction==2)
{
x+=10; y-=10;
LineTo(hdc, x, y);
direction=2;
MoveToEx(hdc, x, y, &point);
} else if(direction==3)
{
x+=0; y-=10;
LineTo(hdc, x, y);
direction=3;
MoveToEx(hdc, x, y, &point);
} else if(direction==4)
{
x-=10; y+=10;
LineTo(hdc, x, y);
direction=4;
MoveToEx(hdc, x, y, &point);
}
}
////////////////
temp=result.at(1);
if(temp==1)
{
if(direction==1) // if 1, change to 2
{
x+=10; y-=10;
LineTo(hdc, x, y);
direction=2;
MoveToEx(hdc, x, y, &point);
} else if(direction==2) // if 2, chnage to 3
{
x+=0; y-=10;
LineTo(hdc, x, y);
direction=3;
} else if(direction==3) // if 3, change to 4
{
x-=0; y-=10;
LineTo(hdc, x, y);
direction=4;
MoveToEx(hdc, x, y, &point);
} else if(direction==4) // if 4 change to 1
{
x-=0; y+=10;
LineTo(hdc, x, y);
direction=1;
MoveToEx(hdc, x, y, &point);
}
}
//ReleaseDC(hwnd, hdc);
//}
/*
// draw a straight line in the left direction
//x+=25; y+=0;
x+=10; y+=0;
LineTo(hdc, x, y);
//SelectObject(hdc, pen);
MoveToEx(hdc, x, y, &point);
//SelectObject(hdc, pen);
//x=point.x; y=point.y;
x+=10; y-=10;
LineTo(hdc, x, y);
//SelectObject(hdc, pen);
MoveToEx(hdc, x, y, &point);
//x+=25; y+=0;
//LineTo(hdc, x, y);
//SelectObject(hdc, pen);
x+=10; y+=10;
LineTo(hdc, x, y);
MoveToEx(hdc, x, y, &point);
x-=10; y+=10;
LineTo(hdc, x, y);
MoveToEx(hdc, x, y, &point);
// draw line at 45 degree angle
//SelectObject(hdc, pen);
//x+=25*cos(PI/2); y+=25*cos(PI/2);
//x+=50; y-=20;
//LineTo(hdc, x, y);
//SelectObject(hdc, pen);
//MoveToEx(hdc, x, y, &point);
//
//vector<int> result=fngetParse();
//for(int i=0; i<result.size(); i++) {
// if(result.at(i)==0)
// {
// LineTo(hdc, x, y);
// }
// else if(result.at(i)==1)
// {
// LineTo(hdc, x+10*cos(PI/2), y+10*cos(PI/2));
// }
// MoveToEx(hdc, x, y, &point);
// x=+10; y+=10;
//LineTo(hdc, 700, 400);
//}
*/
if(KEY_DOWN(VK_ESCAPE))
{
PostQuitMessage(0);
}
ReleaseDC(hwnd, hdc);
Sleep(3000);
//}
return(msg.wParam);
}
vector<int> fnparseLSys(string& str)
{
vector<int> result;
for(int i=0; i<str.length(); i++)
{
if(str.at(i)=='s')
{
result.push_back(0);
} else if(str.at(i)=='l')
{
result.push_back(1);
}
}
return result;
}
vector<int> fngetParse(void)
{
vector<int> result;
result=fnparseLSys(szinput);
return result;
}