// 謎の螺旋？？？.cpp : アプリケーションのエントリ ポイントを定義します。
//

//#include "stdafx.h"
//#include "謎の螺旋？？？.h"

#include <vector>
#define WIN32_LEAN_AND_MEAN 
#include <windows.h>
#include <tchar.h>

#define MAX_LOADSTRING 100

//////////////////////

#define IDS_APP_TITLE			103

#define IDR_MAINFRAME			128
#define IDD_MY_DIALOG	102
#define IDD_ABOUTBOX			103
#define IDM_ABOUT				104
#define IDM_EXIT				105
#define IDI_MY			107
#define IDI_SMALL				108
#define IDC_MY			109
#define IDC_MYICON				2
#ifndef IDC_STATIC
#define IDC_STATIC				-1
#endif

//////////////////////

// グローバル変数:
HINSTANCE hInst;								// 現在のインターフェイス
TCHAR szTitle[MAX_LOADSTRING];					// タイトル バーのテキスト
TCHAR szWindowClass[MAX_LOADSTRING];			// メイン ウィンドウ クラス名

// このコード モジュールに含まれる関数の宣言を転送します:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);




bool MoveTo(HDC hdc, POINT To, LPPOINT LastPoint){
	::MoveToEx(hdc, To.x, To.y, LastPoint);
	return true;
}
bool LineTo(HDC hdc, POINT To, HPEN Pen){

	HPEN Old = (HPEN)SelectObject(hdc, Pen);
	::LineTo(hdc, To.x, To.y);
	SelectObject(hdc, Old);

	return 0;
}
bool DrawText(HDC hdc, LPTSTR Text, int TextCount, LPRECT Rect, UINT Format, COLORREF COLOR, HFONT Font){
	HFONT OldFont = (HFONT)SelectObject(hdc, Font);
	COLORREF Color = SetTextColor(hdc, COLOR);

	::DrawText(hdc, Text, TextCount, Rect, Format);

	SelectObject(hdc, OldFont);
	SetTextColor(hdc, Color);

	return true;
}

std::vector<bool> Prime;

bool MakePrime(std::size_t N){//追記型エラトステネスの篩
	std::size_t Start = Prime.size();
	std::size_t End = N;
	bool F = false;

	if (Prime.size() == 0){
		Prime.push_back(false);
		Prime.push_back(false);
		Prime.push_back(true);
	}
	Start = Prime.size();
	for (std::size_t i = Start; i < End; i++){
		F = true;
		for (std::size_t j = 1; j < i; j++){
			if (Prime[j] == false)continue;
			if (i%j == 0){
				F = false;
				break;

			}
		}
		Prime.push_back(F);
	}
	return true;
}

int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPTSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

 	// TODO: ここにコードを挿入してください。
	MSG msg;
	HACCEL hAccelTable;

	// グローバル文字列を初期化しています。
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_MY, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// アプリケーションの初期化を実行します:
	if (!InitInstance (hInstance, nCmdShow))
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY));

	// メイン メッセージ ループ:
	while (GetMessage(&msg, NULL, 0, 0))
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return (int) msg.wParam;
}



//
//  関数: MyRegisterClass()
//
//  目的: ウィンドウ クラスを登録します。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MY));
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= MAKEINTRESOURCE(IDC_MY);
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

	return RegisterClassEx(&wcex);
}

//
//   関数: InitInstance(HINSTANCE, int)
//
//   目的: インスタンス ハンドルを保存して、メイン ウィンドウを作成します。
//
//   コメント:
//
//        この関数で、グローバル変数でインスタンス ハンドルを保存し、
//        メイン プログラム ウィンドウを作成および表示します。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // グローバル変数にインスタンス処理を格納します。

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  関数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  目的:    メイン ウィンドウのメッセージを処理します。
//
//  WM_COMMAND	- アプリケーション メニューの処理
//  WM_PAINT	- メイン ウィンドウの描画
//  WM_DESTROY	- 中止メッセージを表示して戻る
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;
	
	static const int TW = 16;
	static const int TH = 16;

	RECT rt;
	RECT Prog = { 0, };
	int Dir = 0;
	int Count = 0;

	GetClientRect(hWnd, &rt);

	int CW = rt.right - rt.left;
	int CH = rt.bottom - rt.top;
	int CX = CW / 2;
	int CY = CH / 2;

	POINT Pos = { CX, CY };
	int L = 0;
	int M = 1;
	TCHAR Str[1024] = _T("");

	POINT D[4] = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };

	switch (message)
	{
	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		// 選択されたメニューの解析:
		switch (wmId)
		{
		case IDM_ABOUT:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
			break;
		case IDM_EXIT:
			DestroyWindow(hWnd);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		MoveTo(hdc, { CX, CY }, NULL);

		// TODO: 描画コードをここに追加してください...
		Prog.right = 1;
		L = 1;
		while (true){
			Dir++;
			Dir %= 4;
			if(Count == 0)goto Label;

			if (Dir == 0){
				Prog.bottom = Prog.top + 2;
				L = Prog.bottom;
			}
			if (Dir == 1){
				Prog.right = Prog.left + 2;
				L = Prog.right;
			}
			if (Dir == 2){
				Prog.top = Prog.bottom + 2;
				L = Prog.top;
			}
			if (Dir == 3){
				Prog.left = Prog.right + 2;
				L = Prog.left;
			}

			Label:


			for (int i = 0; i < L; i++){
				Pos.x = Pos.x + (D[Dir].x*TW);
				Pos.y = Pos.y + (D[Dir].y*TH);

				LineTo(hdc, { Pos.x, Pos.y }, (HPEN)GetStockObject(BLACK_PEN));
				Count++;
				MakePrime(Count+1);
				if (Prime[Count] == true){
					wsprintf(Str, _T("%d"),Count);
					int Len = lstrlen(Str);
					RECT SR{Pos.x-64,Pos.y,Pos.x+64,Pos.y+16};
					DrawText(hdc, Str, Len, &SR, DT_CENTER);
				}
				//if (Count == 1024) goto End;
				if (Pos.x > CW) goto End;
				if (Pos.x < 0) goto End;
				if (Pos.y > CH) goto End;
				if (Pos.y < 0)goto End;
			}

		}
		End:
		EndPaint(hWnd, &ps);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	case WM_MOUSEMOVE:
		InvalidateRect(hWnd, NULL, FALSE);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

// バージョン情報ボックスのメッセージ ハンドラーです。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
	case WM_INITDIALOG:
		return (INT_PTR)TRUE;

	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
		{
			EndDialog(hDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;
		}
		break;
	}
	return (INT_PTR)FALSE;
}
