aboutsummaryrefslogtreecommitdiffstats
path: root/CNativeCode/OpenGL_Win32_JDirect.c
blob: a97b7c542b9178aeacdf9a1bd9cf4b485d830dfb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 * Original Author: Leo Chan -- 1995
 *
 * Adam King 1997
 *		Ported to Win32 from X
 *
 * Ron Cemer 1999
 *      Ported to MS Java VM, which supports JDirect instead of JNI
 *
 * This file takes care of the C implementation of finding the correct
 * Win32 window, assigning an OpenGL graphics context to it, storing that
 * graphics context in the java class data structure.
 *
 * also contains the use() and swap() functions for double buffering
 *
 * September 12, 1997	- Adam King
 *		- Added support for rendering directly into a Canvas ( BIG rewrite )
 *
 * Currently, only the windows special overlapping window procs are
 * implemented here !
 *
 * The other procs are implemented with JNI !
 */

#include "OpenGL_misc.h"

#define CLASS_NAME "GL4JavaOglRenderingWindowClass"
#define WINDOW_NAME "GL4JavaOglRenderingWindow"

#define MAX_WINDOWS 1024

static int verbose = 0;
static LONG classregistered = 0;

static LRESULT CALLBACK CallWndProc(int nCode,WPARAM wParam,LPARAM lParam);
static LRESULT CALLBACK CallWndRetProc(int nCode,WPARAM wParam,LPARAM lParam);

BOOL APIENTRY DllMain
	(HANDLE hModule, 
     DWORD ul_reason_for_call, 
     LPVOID lpReserved)
	{
    switch (ul_reason_for_call)
        {
        case DLL_PROCESS_ATTACH:
                /* Initialize various things the first time
                   that we attach this DLL to a process. */
            if (InterlockedIncrement(&classregistered) == 1)
		        {
		        WNDCLASS wndclass;
		        LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

		        wndclass.style         = CS_GLOBALCLASS | CS_SAVEBITS;
		        wndclass.lpfnWndProc   = (WNDPROC)WindowProc;
		        wndclass.cbClsExtra    = 0;
		        wndclass.cbWndExtra    = 0;
		        wndclass.hInstance     = hModule;
		        wndclass.hIcon         = LoadIcon(NULL,IDI_APPLICATION);
		        wndclass.hCursor       = LoadCursor(NULL,IDC_ARROW);
		        wndclass.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);
		        wndclass.lpszMenuName  = NULL;
		        wndclass.lpszClassName = CLASS_NAME;
		        RegisterClass(&wndclass);
                }
            else
                InterlockedDecrement(&classregistered);
            break;
        case DLL_PROCESS_DETACH:
            break;
        case DLL_THREAD_ATTACH:
            break;
        case DLL_THREAD_DETACH:
            break;
        }
    return TRUE;
	}	/* DllMain() */

// Select the pixel format for a given device context

static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
	{
    return DefWindowProc(hwnd,uMsg,wParam,lParam);
	}	/* WindowProc() */

__declspec(dllexport) void OGLWindowMsgPumpJDirect(void)
    {
	MSG msg;

	while (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
		{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
		}
    }   /* OGLWindowMsgPumpJDirect() */

__declspec(dllexport) void moveOGLWindowNativeJDirect(long _hdc, int x, int y, int width, int height)
	{
	HWND hwnd, hwndParent;
	HDC hdc = (HDC)((PointerHolder)_hdc);
    RECT rect;
    int parentx = 0, parenty = 0;
    int insetsleft = 0, insetstop = 0;

	if (hdc != NULL)
		{
		if ((hwnd = WindowFromDC(hdc)) != NULL)
            {
            if ((hwndParent = GetParent(hwnd)) != NULL)
                {
                GetWindowRect(hwndParent,&rect);
                parentx = rect.left;
                parenty = rect.top;
                }
            else
                parentx = parenty = 0;
            MoveWindow(hwnd,x-parentx,y-parenty,width,height,TRUE);
            GetWindowRect(hwnd,&rect);
            insetsleft = rect.left-x;
            insetstop = rect.top-y;
            if ( (insetsleft) || (insetstop) )
                MoveWindow(hwnd,x-(parentx+insetsleft),y-(parenty+insetstop),width,height,TRUE);
            }
		}
	}	/* moveOGLWindowNativeJDirect() */

__declspec(dllexport) long createOGLWindowNativeJDirect(long hwndParent, int x, int y, int width, int height)
	{
	HWND hwnd;
    HDC hdc;

	if ((hwnd = CreateWindow
		(CLASS_NAME,
		 WINDOW_NAME,
		 WS_CHILD | WS_VISIBLE | WS_DISABLED,
		 x,
		 y,
		 width,
		 height,
		 (HWND)((PointerHolder)hwndParent),
		 NULL,
		 NULL,
		 NULL)) == NULL)
		{
        fprintf(stderr,"createOGLWindowNativeJDirect(): CreateWindow failed!\n");
		fprintf(stderr,"GetLastError() returns: %d\n",(int)GetLastError());
		return(0);
		}
    hdc = GetDC(hwnd);
    moveOGLWindowNativeJDirect((long)((PointerHolder)hdc), x, y, width, height);
    return (long)((PointerHolder)hdc);
	}	/* createOGLWindowNativeJDirect() */

__declspec(dllexport) void destroyOGLWindowNativeJDirect(long _hdc)
	{
	HWND hwnd;
	HDC hdc = (HDC)((PointerHolder)_hdc);

	if (hdc != NULL)
		{
		if ((hwnd = WindowFromDC(hdc)) != NULL)
			{
			ReleaseDC(hwnd,hdc);
            DestroyWindow(hwnd);
			}
		}
	}	/* destroyOGLWindowNativeJDirect() */