#include <time.h>
#include <pthread.h>
#include <rtl_ipc.h>
#include <semaphore.h>
#include <rtl_barrier.h>
#include <rtl_conf.h>
#include <rtl_printf.h>
#include <unistd.h>
#include "examples.h"
#include <stdio.h>
#include <math.h>


#define MWINCLUDECOLORS

#include <windows.h>
#include <GL/gl.h>
#include <GL/nglx.h>

#if YABIRO_EXAMPLE 
double L1=30.0;
double L2=90.0;
double L3=30.0;
double R1=30.0;
double R2=50.0;
double R3=30.0;
double CL1=90.0;
double CL2=90.0;
double CR1=90.0;
double CR2=90.0;
extern MWIMAGEHDR image_logosartl;
HWND hwndLeft=NULL;
HWND hwndRight=NULL;
HWND hwndCenter=NULL;
void tkSwapBuffers(void) {
    nglXSwapBuffers(hwndCenter);
};

pthread_mutex_t guimutex;

 // Robot 3D OpenGL Engine
#include "mech.c"

pthread_t thread14[10];
#ifdef CONFIG_RTL_GUI
LRESULT CALLBACK wprocleft(HWND,UINT,WPARAM,LPARAM);
LRESULT CALLBACK wprocright(HWND,UINT,WPARAM,LPARAM);
LRESULT CALLBACK wproccenter(HWND,UINT,WPARAM,LPARAM);
static NGLXContext cx;

void *task15(void *arg);
pthread_t thread15;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                   PSTR szCmdLine, int iCmdShow)
{       pthread_attr_t attr;
        struct sched_param sched_param;
 
        static char szAppNameLeft[]="Left Leg";
        static char szAppNameRight[]="Right Leg";
        static char szAppNameCenter[]="Center Robot";
	static int aux;
       MSG msg;
        WNDCLASS wndclass;

        wndclass.style          = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
        wndclass.lpfnWndProc    = (WNDPROC)wprocleft;
        wndclass.cbClsExtra     =0;
        wndclass.cbWndExtra     =0;
        wndclass.hInstance      =0;
        wndclass.hIcon          =0;
        wndclass.hCursor        =0;
        wndclass.hbrBackground  =(HBRUSH)GetStockObject(LTGRAY_BRUSH);
        wndclass.lpszMenuName   =NULL;
        wndclass.lpszClassName  = szAppNameLeft;
        RegisterClass(&wndclass);
        wndclass.lpszClassName  = szAppNameRight;
        wndclass.lpfnWndProc    = (WNDPROC)wprocright;
        RegisterClass(&wndclass);
        wndclass.style          = CS_DBLCLKS;
        wndclass.hbrBackground  =(HBRUSH)GetStockObject(BLACK_BRUSH);
        wndclass.lpszClassName  = szAppNameCenter;
        wndclass.lpfnWndProc    = (WNDPROC)wproccenter;
	RegisterClass(&wndclass);
	
        hwndLeft=CreateWindowEx(0L,
                          szAppNameLeft,
                          "Left Leg",
                          WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                          CW_USEDEFAULT,
                          CW_USEDEFAULT,
                          160,
                          160,
                          NULL,
                          NULL,
                          NULL,
                          NULL);
       hwndRight=CreateWindowEx(0L,
                          szAppNameRight,
                          "Right Left",
                          WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                          CW_USEDEFAULT,
                          CW_USEDEFAULT,
                          160,
                          160,
                          NULL,
                          NULL,
                          NULL,
                          NULL);
        hwndCenter=CreateWindowEx(0L,
                          szAppNameCenter,
                          "3D View",
                          WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                          CW_USEDEFAULT,
                          CW_USEDEFAULT,
                          300,
                          300,
                          NULL,
                          NULL,
                          NULL,
                          NULL);
	cx = nglXCreateContext(NULL,0);
	nglXMakeCurrent(hwndCenter,cx);
	initGLView();
        pthread_attr_init (&attr);
        sched_param.sched_priority = 3;
        pthread_attr_setfp_np(&attr, 1);
        pthread_attr_setschedparam (&attr, &sched_param);
        pthread_create(&thread15,&attr,task15,0);
        MwSetDesktopWallpaper(&image_logosartl); 
	pthread_mutex_lock(&guimutex);
	ShowWindow(hwndLeft,iCmdShow);
        ShowWindow(hwndRight,iCmdShow);
        ShowWindow(hwndCenter,iCmdShow);
        UpdateWindow(hwndLeft);
        UpdateWindow(hwndRight);
        UpdateWindow(hwndCenter);
	pthread_mutex_unlock(&guimutex);
        
        while (GetMessage(&msg,NULL,0,0)) {
		pthread_mutex_lock(&guimutex);
                TranslateMessage(&msg);
                DispatchMessage(&msg);
		pthread_mutex_unlock(&guimutex);
        }      
        return msg.wParam;
}       

LRESULT CALLBACK wprocleft(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{       
        HDC hdc;
        PAINTSTRUCT ps;
        RECT rect;
        int x;
	int y;
	int x1;
	int y1;
	int x2;
	int y2;
	
        switch (iMsg) {
        case WM_CREATE:
                break;
        case WM_PAINT:
        /*case WM_MOUSEFIRST:*/
                hdc=BeginPaint(hwnd,&ps);
                GetClientRect(hwnd,&rect);
		FillRect(hdc,&rect,GetStockObject(LTGRAY_BRUSH));
                MoveToEx(hdc,70,1,NULL);
		LineTo(hdc,90,1);
		MoveToEx(hdc,70,3,NULL);
		LineTo(hdc,90,3);
		MoveToEx(hdc,80,3,NULL);
		x=80-((int) (cos(L1*M_PI/180)*40.0));
		y=3+ ((int) (sin(L1*M_PI/180)*40.0));
		LineTo(hdc,x,y);
		MoveToEx(hdc,82,3,NULL);
		LineTo(hdc,x+2,y);
		MoveToEx(hdc,x,y,NULL);
           	x1=x+((int)(cos((L2-L1)*M_PI/180)*40.0));
           	y1=y+((int)(sin((L2-L1)*M_PI/180)*40.0));
		LineTo(hdc,x1,y1);
		MoveToEx(hdc,x+2,y,NULL);
		LineTo(hdc,x1+2,y1);
		MoveToEx(hdc,x1,y1,NULL);
	      	x2=x1-((int)(cos((L3-L2+L1)*M_PI/180)*20.0));
           	y2=y1+((int)(sin((L3-L2+L1)*M_PI/180)*20.0));
		LineTo(hdc,x2,y2);
		EndPaint(hwnd,&ps);
                break;
        case WM_DESTROY:
                PostQuitMessage(0);
                break;
        default:
                return DefWindowProc(hwnd,iMsg,wParam,lParam);
        }      
        return (0);
}

LRESULT CALLBACK wprocright(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{       
        HDC hdc;
        PAINTSTRUCT ps;
        RECT rect;
        int x;
	int y;
	int x1;
	int y1;
	int x2;
	int y2;
	
        switch (iMsg) {
        case WM_CREATE:
                break;
        case WM_PAINT:
        /*case WM_MOUSEFIRST:*/
                hdc=BeginPaint(hwnd,&ps);
                GetClientRect(hwnd,&rect);
		FillRect(hdc,&rect,GetStockObject(LTGRAY_BRUSH));
	        MoveToEx(hdc,70,1,NULL);
		LineTo(hdc,90,1);
		MoveToEx(hdc,70,3,NULL);
		LineTo(hdc,90,3);
		MoveToEx(hdc,80,3,NULL);
		x=80-((int) (cos(R1*M_PI/180)*40.0));
		y=3+ ((int) (sin(R1*M_PI/180)*40.0));
		LineTo(hdc,x,y);
		MoveToEx(hdc,82,3,NULL);
		LineTo(hdc,x+2,y);
		MoveToEx(hdc,x,y,NULL);
           	x1=x+((int)(cos((R2-R1)*M_PI/180)*40.0));
           	y1=y+((int)(sin((R2-R1)*M_PI/180)*40.0));
		LineTo(hdc,x1,y1);
		MoveToEx(hdc,x+2,y,NULL);
		LineTo(hdc,x1+2,y1);
		MoveToEx(hdc,x1,y1,NULL);
	      	x2=x1-((int)(cos((R3-R2+R1)*M_PI/180)*20.0));
           	y2=y1+((int)(sin((R3-R2+R1)*M_PI/180)*20.0));
	        LineTo(hdc,x2,y2);
		EndPaint(hwnd,&ps);
                break;
        case WM_DESTROY:
                PostQuitMessage(0);
                break;
        default:
                return DefWindowProc(hwnd,iMsg,wParam,lParam);
        }      
        return (0);
}

LRESULT CALLBACK wproccenter(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{       
        HDC hdc;
        PAINTSTRUCT ps;
        RECT rect;
        
        switch (iMsg) {
        case WM_CREATE:
                break;
        case WM_PAINT:
        /*case WM_MOUSEFIRST:*/
                hdc=BeginPaint(hwnd,&ps);
                GetClientRect(hwnd,&rect);
		draw();
		EndPaint(hwnd,&ps);
                break;
        case WM_DESTROY:
                PostQuitMessage(0);
                break;
        default:
                return DefWindowProc(hwnd,iMsg,wParam,lParam);
        }      
        return (0);
}
#endif //CONFIG_RTL_GUI

void *task15(void *arg) 
{
  pthread_make_periodic_np (pthread_self(), gethrtime(),300000000);
  while (1)
  {
	pthread_wait_np();

#ifdef CONFIG_RTL_GUI
	pthread_mutex_lock(&guimutex);
        if (hwndLeft!=NULL) SendMessage(hwndLeft,WM_PAINT,0,0);
        if (hwndRight!=NULL) SendMessage(hwndRight,WM_PAINT,0,0);
	if (hwndCenter!=NULL) draw();
	pthread_mutex_unlock(&guimutex);
#endif
  };
};

int can_init_module(void);
int rri_init_module(void);
int robot_init_module(void);


void *task14(void *arg)
{
  pthread_mutex_init (&guimutex, 0);
  
  rri_init_module(); 
  can_init_module();
  robot_init_module();
#ifdef CONFIG_RTL_GUI  
  main(0,NULL);
#endif
  
  while (1) {
  };
  return 0;
};









#endif
