File: gl_wrap.cpp

package info (click to toggle)
glest 3.2.2-2
  • links: PTS, VCS
  • area: contrib
  • in suites: squeeze
  • size: 2,800 kB
  • ctags: 6,581
  • sloc: cpp: 32,575; sh: 8,341; makefile: 63
file content (164 lines) | stat: -rw-r--r-- 4,440 bytes parent folder | download
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
// ==============================================================
//	This file is part of Glest Shared Library (www.glest.org)
//
//	Copyright (C) 2001-2008 Martio Figueroa
//
//	You can redistribute this code and/or modify it under 
//	the terms of the GNU General Public License as published 
//	by the Free Software Foundation; either version 2 of the 
//	License, or (at your option) any later version
// ==============================================================

#include "gl_wrap.h"

#include <cassert>

#include <windows.h>

#include "opengl.h"
#include "leak_dumper.h"

using namespace Shared::Graphics::Gl;

namespace Shared{ namespace Platform{

// =====================================================
//	class PlatformContextGl
// =====================================================

void PlatformContextGl::init(int colorBits, int depthBits, int stencilBits){
	
	int iFormat;
	PIXELFORMATDESCRIPTOR pfd;
	BOOL err;

	//Set8087CW($133F);
	dch = GetDC(GetActiveWindow());
	assert(dch!=NULL);

	ZeroMemory(&pfd, sizeof(pfd));
	pfd.nSize= sizeof(PIXELFORMATDESCRIPTOR);
	pfd.nVersion= 1;
	pfd.dwFlags= PFD_GENERIC_ACCELERATED | PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
	pfd.iPixelType= PFD_TYPE_RGBA;
	pfd.cColorBits= colorBits;
	pfd.cDepthBits= depthBits;
	pfd.iLayerType= PFD_MAIN_PLANE;
	pfd.cStencilBits= stencilBits; 

	iFormat= ChoosePixelFormat(dch, &pfd);
	assert(iFormat!=0);

	err= SetPixelFormat(dch, iFormat, &pfd);
	assert(err);    

	glch= wglCreateContext(dch);
	if(glch==NULL){
		throw runtime_error("Error initing OpenGL device context");
	}

	makeCurrent();
}

void PlatformContextGl::end(){
	int makeCurrentError= wglDeleteContext(glch);
	assert(makeCurrentError);
}

void PlatformContextGl::makeCurrent(){
	int makeCurrentError= wglMakeCurrent(dch, glch);
	assert(makeCurrentError);
}

void PlatformContextGl::swapBuffers(){
	int swapErr= SwapBuffers(dch);
	assert(swapErr);
}

// ======================================
//	Global Fcs  
// ======================================

void createGlFontBitmaps(uint32 &base, const string &type, int size, int width, int charCount, FontMetrics &metrics){
	HFONT font= CreateFont(
		size, 0, 0, 0, width, 0, FALSE, FALSE, ANSI_CHARSET,
		OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, 
		DEFAULT_PITCH, type.c_str());

	assert(font!=NULL);

	HDC dc= wglGetCurrentDC();
	SelectObject(dc, font);
	BOOL err= wglUseFontBitmaps(dc, 0, charCount, base);
		
	FIXED one;
	one.value= 1;
	one.fract= 0;

	FIXED zero;
	zero.value= 0;
	zero.fract= 0;

	MAT2 mat2;
	mat2.eM11= one;
	mat2.eM12= zero;
	mat2.eM21= zero;
	mat2.eM22= one;

	//metrics
	GLYPHMETRICS glyphMetrics;
	int errorCode= GetGlyphOutline(dc, 'a', GGO_METRICS, &glyphMetrics, 0, NULL, &mat2);
	if(errorCode!=GDI_ERROR){
		metrics.setHeight(static_cast<float>(glyphMetrics.gmBlackBoxY));
	}
	for(int i=0; i<charCount; ++i){
		int errorCode= GetGlyphOutline(dc, i, GGO_METRICS, &glyphMetrics, 0, NULL, &mat2);
		if(errorCode!=GDI_ERROR){
			metrics.setWidth(i, static_cast<float>(glyphMetrics.gmCellIncX));
		}
	}

	DeleteObject(font);

	assert(err);
}

void createGlFontOutlines(uint32 &base, const string &type, int width, float depth, int charCount, FontMetrics &metrics){
	HFONT font= CreateFont(
		10, 0, 0, 0, width, 0, FALSE, FALSE, ANSI_CHARSET,
		OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, 
		DEFAULT_PITCH, type.c_str());

	assert(font!=NULL);

	GLYPHMETRICSFLOAT *glyphMetrics= new GLYPHMETRICSFLOAT[charCount];

	HDC dc= wglGetCurrentDC();
	SelectObject(dc, font);
	BOOL err= wglUseFontOutlines(dc, 0, charCount, base, 1000, depth, WGL_FONT_POLYGONS, glyphMetrics);
		
	//load metrics
	metrics.setHeight(glyphMetrics['a'].gmfBlackBoxY);
	for(int i=0; i<charCount; ++i){
		metrics.setWidth(i, glyphMetrics[i].gmfCellIncX);
	}

	DeleteObject(font);
	delete [] glyphMetrics;

	assert(err);
}

const char *getPlatformExtensions(const PlatformContextGl *pcgl){
	typedef const char* (WINAPI * PROCTYPE) (HDC hdc);
	PROCTYPE proc= reinterpret_cast<PROCTYPE>(getGlProcAddress("wglGetExtensionsStringARB"));
	return proc==NULL? "": proc(pcgl->getHandle());
}

PROC getGlProcAddress(const char *procName){
	PROC proc= wglGetProcAddress(procName);
	assert(proc!=NULL);
	return proc;
}

}}//end namespace