// TestSDL2.cpp : Defines the entry point for the console application.
//

#include <stdio.h>
#include <SDL.h>
#include <SDL_syswm.h>

#pragma comment(lib, "SDL2.lib")
#pragma comment(lib, "SDL2Main.lib")

#include <Windows.h>

#include "Chart.h"
#if defined(_UNICODE) || defined(UNICODE)
#	pragma comment(lib,"CChartu.lib")
#else
#	pragma comment(lib,"CChart.lib")
#endif
using namespace NsCChart;

#include <math.h>

CChart chart;

HWND MyGetHWND(SDL_Window *window)
{
	if(!window)return NULL;

	SDL_SysWMinfo sys;
	SDL_VERSION(&sys.version);
	if(!SDL_GetWindowWMInfo(window, &sys))return NULL;

	return sys.info.win.window;
}

HDC MyGetHDC(SDL_Window *window)
{
	if(!window)return NULL;

	SDL_SysWMinfo sys;
	SDL_VERSION(&sys.version);
	if(!SDL_GetWindowWMInfo(window, &sys))return NULL;

	return sys.info.win.hdc;
}

int	MyDraw(SDL_Window *window)
{
//	int nRet = 0;
//	nRet = SDL_UpdateWindowSurface(window);

	HWND hWnd = MyGetHWND(window);
	if(!hWnd)return 0;

	RECT rect;
	GetClientRect(hWnd, &rect);
	//rect.left = (rect.left + rect.right)/2;
	//rect.top = (rect.top + rect.bottom)/2;
	//chart.OnDraw(hWnd, rect);

	long w = rect.right-rect.left;
	long h = rect.bottom-rect.top;
	SDL_Rect rects[3];
	rects[0].x = rect.left;
	rects[0].y = rect.top;
	rects[0].w = w/2;
	rects[0].h = h/2;

	rects[1].x = rect.left+w/2;
	rects[1].y = rect.top;
	rects[1].w = w/2;
	rects[1].h = h/2;

	rects[2].x = rect.left;
	rects[2].y = rect.top+h/2;
	rects[2].w = w/2;
	rects[2].h = h/2;

	int nRet = 0;
	nRet = SDL_UpdateWindowSurfaceRects(window, rects, 3);
	rect.left = (rect.left + rect.right)/2;
	rect.top = (rect.top + rect.bottom)/2;

	HDC hDC = MyGetHDC(window);
	MyMemDC memdc(hDC, 0, &rect);
	memdc.SetAutoUpdate(false);
	chart.OnDraw(memdc, rect);
	memdc.Update();

	return nRet;
}

int main(int argc, char* argv[])
{
	SDL_Surface *hello = NULL;
	SDL_Surface *screen = NULL;
	SDL_Init(SDL_INIT_EVERYTHING);
	SDL_Window* window = NULL;
	window = SDL_CreateWindow("SDL CChart", 
		SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 
		640, 480, SDL_WINDOW_SHOWN);

	if (window == NULL)
	{
		fprintf(stderr, "Window could not be created: %s\n", SDL_GetError());
		return 1;
	}
	screen = SDL_GetWindowSurface(window);
	if(screen == NULL)
	{
		fprintf(stderr, "Could not get surface: %s\n", SDL_GetError());
		return 1;
	}

	hello = SDL_LoadBMP("hello.BMP");
	SDL_BlitSurface(hello, NULL, screen, NULL);

	double pi = 4.0*atan(1.0);
	for(int i=0; i<720; ++i)
	{
		chart.AddPoint2D(i, 1.2*sin(i*2.0*pi/360));
	}
	chart.SetTitle(_T("在SDL2.0界面中使用CChart"));
	chart.SetBkgndColor(RGB(10, 10, 10));
	chart.SetTitleColor(RGB(245, 245, 245));
	//chart.SetEraseBkgnd(false);
	MyDraw(window);

	bool bQuit = false;
	SDL_Event event;
	HDC hDC = MyGetHDC(window);
	WPARAM wParam;
	LPARAM lParam;
	while(!bQuit)
	{
		while( SDL_PollEvent( &event ) )  
		{  
			switch(event.type)
			{
			case SDL_KEYDOWN:       //如果是键盘按下事件  
				if(event.key.keysym.sym == SDLK_ESCAPE) //如果按的是ESC键  
				{  
					bQuit = true;                       //退出循环  
				}  
				break;
			case SDL_MOUSEBUTTONDOWN:
				if(event.button.button == SDL_BUTTON_LEFT)
				{
					if(event.button.clicks == 1)
					{
						wParam = event.key.keysym.sym;
						lParam = event.button.x + (event.button.y<<16);
						chart.OnEvent(hDC, WM_LBUTTONDOWN, wParam, lParam); 
					}
					else
					{
						wParam = event.key.keysym.sym;
						lParam = event.button.x + (event.button.y<<16);
						chart.OnEvent(hDC, WM_LBUTTONDBLCLK, wParam, lParam); 
					}
				}
				else if(event.button.button == SDL_BUTTON_RIGHT)
				{
					wParam = event.key.keysym.sym;
					lParam = event.button.x + (event.button.y<<16);
					chart.OnEvent(hDC, WM_CONTEXTMENU, wParam, lParam); 
				}
				break;
			case SDL_MOUSEBUTTONUP:
				if(event.button.button == SDL_BUTTON_LEFT)
				{
					wParam = event.key.keysym.sym;
					lParam = event.button.x + (event.button.y<<16);
					chart.OnEvent(hDC, WM_LBUTTONUP, wParam, lParam); 
				}
				break;
			case SDL_MOUSEMOTION:
				if(true)
				{
					wParam = event.key.keysym.sym;
					lParam = event.button.x + (event.button.y<<16);
					chart.OnEvent(hDC, WM_MOUSEMOVE, wParam, lParam); 
				}
				break;
			case SDL_QUIT:  
				bQuit = true;
				break;  
			default:  
				break;  
			}  
		}
		if( MyDraw(window) == -1 )  
		{  
			break;
		}  
  	}

	SDL_FreeSurface(hello);
	SDL_Quit();
	return 0;
}
