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 (122 lines) | stat: -rw-r--r-- 3,573 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
//This file is part of Glest Shared Library (www.glest.org)
//Copyright (C) 2005 Matthias Braun <matze@braunis.de>

//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 <iostream>
#include <sstream>
#include <stdexcept>
#include <cassert>

#include <SDL.h>
#ifdef X11_AVAILABLE
#include <GL/glx.h>
#endif

#include "opengl.h"
#include "sdl_private.h"
#include "leak_dumper.h"
#include "noimpl.h"

using namespace Shared::Graphics::Gl;

namespace Shared{ namespace Platform{
	
// ======================================
//	class PlatformContextGl  
// ======================================

void PlatformContextGl::init(int colorBits, int depthBits, int stencilBits) {

	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
	SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 1);
	SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 1);
	SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 1);
	SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, stencilBits);
	SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, depthBits);
	int flags = SDL_OPENGL;
	if(Private::shouldBeFullscreen)
		flags |= SDL_FULLSCREEN;

	int resW = Private::ScreenWidth;
	int resH = Private::ScreenHeight;
	SDL_Surface* screen = SDL_SetVideoMode(resW, resH, colorBits, flags);
	if(screen == 0) {
		std::ostringstream msg;
		msg << "Couldn't set video mode "                                    	
			<< resW << "x" << resH << " (" << colorBits 
			<< "bpp " << stencilBits << " stencil "
			<< depthBits << " depth-buffer). SDL Error is: " << SDL_GetError();
		throw std::runtime_error(msg.str());
	}
}

void PlatformContextGl::end() {
}

void PlatformContextGl::makeCurrent() {
}

void PlatformContextGl::swapBuffers() {
	SDL_GL_SwapBuffers();
}

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

void createGlFontBitmaps(uint32 &base, const string &type, int size, int width,
						 int charCount, FontMetrics &metrics) {
#ifdef X11_AVAILABLE
	Display* display = glXGetCurrentDisplay();
	if(display == 0) {
		throw std::runtime_error("Couldn't create font: display is 0");
	}
	XFontStruct* fontInfo = XLoadQueryFont(display, type.c_str());
	if(!fontInfo) {
		throw std::runtime_error("Font not found.");
	}

	// we need the height of 'a' which sould ~ be half ascent+descent
	metrics.setHeight(static_cast<float> 
			(fontInfo->ascent + fontInfo->descent) / 2);
	for(unsigned int i = 0; i < static_cast<unsigned int> (charCount); ++i) {
		if(i < fontInfo->min_char_or_byte2 ||
				i > fontInfo->max_char_or_byte2) {
			metrics.setWidth(i, static_cast<float>(6));
		} else {
			int p = i - fontInfo->min_char_or_byte2;
			metrics.setWidth(i, static_cast<float> (
						fontInfo->per_char[p].rbearing 
						- fontInfo->per_char[p].lbearing));
		}
	}

	glXUseXFont(fontInfo->fid, 0, charCount, base);
	XFreeFont(display, fontInfo);
#else
    // we badly need a solution portable to more than just glx
	NOIMPL;
#endif
}

void createGlFontOutlines(uint32 &base, const string &type, int width,
						  float depth, int charCount, FontMetrics &metrics) {
	NOIMPL;
}

const char *getPlatformExtensions(const PlatformContextGl *pcgl) {
	return "";
}

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

}}//end namespace