27 #include <gdk/gdkkeysyms.h>
34 #include <gdk/gdkwin32.h>
50 static Display *display;
53 static HDC devicecontext;
61 static void init_opengl_common(
void)
66 glEnable(GL_TEXTURE_2D);
67 glShadeModel(GL_SMOOTH);
71 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
73 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
87 glMatrixMode(GL_PROJECTION);
92 glMatrixMode(GL_MODELVIEW);
97 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &texSize);
98 LOG(
LOG_INFO,
"gtk-v2::opengl_common",
"Maximum texture size is %d\n", texSize);
111 int attrListDbl[] = { GLX_RGBA, GLX_DOUBLEBUFFER,
119 XSetWindowAttributes attr;
122 display = GDK_WINDOW_XDISPLAY(gtk_widget_get_window(drawingarea));
123 vi = glXChooseVisual(display,
124 gdk_x11_get_default_screen (), attrListDbl);
126 width = drawingarea->allocation.width;
127 height = drawingarea->allocation.height;
136 DefaultVisual(display, gdk_x11_get_default_screen ());
137 attr.colormap= XCreateColormap(display, GDK_WINDOW_XID(gtk_widget_get_window(drawingarea)),
138 vi->visual, AllocNone);
140 window = XCreateWindow(display, GDK_WINDOW_XID(gtk_widget_get_window(drawingarea)),
147 XMapWindow(display,window);
150 LOG(
LOG_WARNING,
"gtk-v2::init_glx_opengl",
"Could not get double buffered screen!\n");
153 ctx = glXCreateContext(display, vi, 0, GL_TRUE);
155 if (!glXMakeCurrent(display, window, ctx)) {
156 LOG(
LOG_ERROR,
"gtk-v2::init_glx_opengl",
"Could not set opengl context!\n");
159 if (glXIsDirect(display, ctx)) {
160 LOG(
LOG_INFO,
"gtk-v2::init_glx_opengl",
"Direct rendering is available!\n");
162 LOG(
LOG_INFO,
"gtk-v2::init_glx_opengl",
"Direct rendering is not available!\n");
174 void init_wgl_opengl(GtkWidget* drawingarea)
179 PIXELFORMATDESCRIPTOR pfd = {
180 sizeof(PIXELFORMATDESCRIPTOR),
200 width = drawingarea->allocation.width;
201 height = drawingarea->allocation.height;
203 dctx = GetDC(GDK_WINDOW_HWND(gtk_widget_get_window(drawingarea)));
204 devicecontext = dctx;
207 pixelformat = ChoosePixelFormat(dctx, &pfd);
208 SetPixelFormat(dctx, pixelformat, &pfd);
210 glctx = wglCreateContext(dctx);
211 wglMakeCurrent(dctx, glctx);
227 init_wgl_opengl(drawingarea);
229 init_opengl_common();
249 #define DEFAULT_DARKNESS 0
270 static void opengl_light_space(
int x,
int y,
int mx,
int my)
290 glColor4ub(0, 0, 0, map_darkness[x*2][y*2]);
293 glColor4ub(0, 0, 0, map_darkness[x*2 + 1][y*2]);
296 glColor4ub(0, 0, 0, map_darkness[x*2 + 1][y*2 + 1]);
299 glColor4ub(0, 0, 0, map_darkness[x*2][y*2+1]);
307 glColor4ub(0, 0, 0, map_darkness[x*2+1][y*2]);
310 glColor4ub(0, 0, 0, map_darkness[x*2+2][y*2]);
313 glColor4ub(0, 0, 0, map_darkness[x*2+2][y*2+1]);
316 glColor4ub(0, 0, 0, map_darkness[x*2+1][y*2+1]);
324 glColor4ub(0, 0, 0, map_darkness[x*2][y*2+1]);
327 glColor4ub(0, 0, 0, map_darkness[x*2+1][y*2+1]);
330 glColor4ub(0, 0, 0, map_darkness[x*2+1][y*2+2]);
333 glColor4ub(0, 0, 0, map_darkness[x*2][y*2+2]);
341 glColor4ub(0, 0, 0, map_darkness[x*2+1][y*2+1]);
344 glColor4ub(0, 0, 0, map_darkness[x*2+2][y*2+1]);
347 glColor4ub(0, 0, 0, map_darkness[x*2+2][y*2+2]);
350 glColor4ub(0, 0, 0, map_darkness[x*2+1][y*2+2]);
360 static int dx[8]= {0,1,1,1,0,-1,-1,-1};
361 static int dy[8]= {-1,-1,0,1,1,1,0,-1};
363 static int bweights[8]= {2,0,4,0,8,0,1,0};
364 static int cweights[8]= {0,2,0,4,0,8,0,1};
365 static int bc_exclude[8]= {
380 #define TEXTURE_OFFSET 1.0/16.0
393 static void drawsmooth_opengl (
int x,
int y,
int mx,
int my,
int layer)
395 int partdone[8]= {0,0,0,0,0,0,0,0}, slevels[8], sfaces[8], i,
396 weight,weightC, emx,emy, smoothface, dosmooth, lowest, havesmooth;
399 for (i=0; i<8; i++) {
426 while (havesmooth < dosmooth) {
428 for (i=0; i<8; i++) {
429 if ( (slevels[i]>0) && (!partdone[i]) && ((lowest<0) || (slevels[i]<slevels[lowest]))) {
441 for (i=0; i<8; i++) {
442 if (slevels[i]==slevels[lowest] && sfaces[i] == sfaces[lowest]) {
444 weight=weight+bweights[i];
445 weightC&=~bc_exclude[i];
449 weightC&=~cweights[i];
453 smoothface=sfaces[lowest];
470 glBindTexture(GL_TEXTURE_2D,
pixmaps[smoothface]->fog_texture);
472 glBindTexture(GL_TEXTURE_2D,
pixmaps[smoothface]->map_texture);
483 glTexCoord2f(TEXTURE_OFFSET * weight, 0.0f);
486 glTexCoord2f(TEXTURE_OFFSET * (weight+1), 0.0f);
489 glTexCoord2f(TEXTURE_OFFSET * (weight+1), 0.5f);
492 glTexCoord2f(TEXTURE_OFFSET * weight, 0.5f);
500 glTexCoord2f(TEXTURE_OFFSET * weight, 0.5f);
503 glTexCoord2f(TEXTURE_OFFSET * (weight+1), 0.5f);
506 glTexCoord2f(TEXTURE_OFFSET * (weight+1), 1.0f);
509 glTexCoord2f(TEXTURE_OFFSET * weight, 1.0f);
521 static void draw_smoothing(
int layer)
532 drawsmooth_opengl(x, y, mx, my, layer);
554 int mx,my, layer,x,y, d1, d2, d3, num_dark, got_smooth, face, t1, t2;
555 glClear(GL_COLOR_BUFFER_BIT);
561 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
573 for (layer=0; layer<=
MAXLAYERS; layer++) {
581 glBindTexture(GL_TEXTURE_2D, 0);
612 opengl_light_space(x, y, mx, my);
641 map_darkness[x*2 + 1][y*2 + 1] = DEFAULT_DARKNESS;
644 d1 = DEFAULT_DARKNESS;
645 d2 = DEFAULT_DARKNESS;
646 d3 = DEFAULT_DARKNESS;
654 if (x>0 && y>0 &&
mapdata_cell(mx-1, my-1)->have_darkness) {
670 map_darkness[x*2][y*2] = (d1 + d2 +d3 + map_darkness[x*2 + 1][y*2 + 1]) / num_dark;
673 map_darkness[x*2][y*2 + 1] = (d1 + map_darkness[x*2 + 1][y*2 + 1]) / 2;
675 map_darkness[x*2][y*2 + 1] = map_darkness[x*2 + 1][y*2 + 1];
679 map_darkness[x*2 +1 ][y*2] = (d3 + map_darkness[x*2 + 1][y*2 + 1]) / 2;
681 map_darkness[x*2 + 1][y*2] = map_darkness[x*2 + 1][y*2 + 1];
689 map_darkness[x*2][y*2] = MAX( MAX(d1, d2), MAX(d3, map_darkness[x*2 + 1][y*2 + 1]));
690 map_darkness[x*2][y*2 + 1] = MAX(d1, map_darkness[x*2 + 1][y*2 + 1]);
691 map_darkness[x*2 + 1][y*2] = MAX(d3, map_darkness[x*2 + 1][y*2 + 1]);
719 glBindTexture(GL_TEXTURE_2D,
pixmaps[
mapdata_cell(mx, my)->heads[layer].face]->fog_texture);
721 glBindTexture(GL_TEXTURE_2D,
pixmaps[
mapdata_cell(mx, my)->heads[layer].face]->map_texture);
726 glTexCoord2f(0.0f, 0.0f);
727 glVertex3i(nx, ny, 0);
729 glTexCoord2f(1.0f, 0.0f);
732 glTexCoord2f(1.0f, 1.0f);
735 glTexCoord2f(0.0f, 1.0f);
747 if (
pixmaps[face]->map_texture) {
770 glBindTexture(GL_TEXTURE_2D,
pixmaps[face]->fog_texture);
772 glBindTexture(GL_TEXTURE_2D,
pixmaps[face]->map_texture);
777 glTexCoord2f(0.0f, 0.0f);
778 glVertex3i(nx, ny, 0);
780 glTexCoord2f(1.0f, 0.0f);
783 glTexCoord2f(1.0f, 1.0f);
786 glTexCoord2f(0.0f, 1.0f);
807 draw_smoothing(layer);
812 glXSwapBuffers(display, window);
814 SwapBuffers(devicecontext);
827 static guint8 *newdata;
829 int nwidth, nheight, numshifts, i;
830 guint8 *data_to_use = data, *l;
839 for (nwidth = pi->
map_width, numshifts=0; nwidth >1; nwidth >>=1, numshifts++) ;
840 nwidth <<= numshifts;
845 for (nheight = pi->
map_height, numshifts=0; nheight >1; nheight >>=1, numshifts++) ;
846 nheight <<= numshifts;
871 if (nwidth * nheight * 4 > size) {
872 size = nwidth * nheight * 4;
873 newdata = g_realloc(newdata, size);
875 if (newdata == NULL) {
877 "Could not allocate memory: %s", strerror(errno));
887 memset(newdata, 0, (nheight - pi->
map_height) * nwidth * 4);
888 datastart = newdata + (nheight - pi->
map_height) * nwidth * 4;
894 memset(datastart + y * nwidth * 4, 0, (nwidth - pi->
map_width) * 4);
895 memcpy(datastart + y * nwidth * 4 + (nwidth - pi->
map_width) * 4,
898 data_to_use = newdata;
903 glGenTextures(1, &pi->map_texture);
904 glBindTexture(GL_TEXTURE_2D, pi->map_texture);
905 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
906 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
909 0, GL_RGBA, GL_UNSIGNED_BYTE, data_to_use);
919 newdata = g_realloc(newdata, size);
923 if (data_to_use != newdata) {
928 l = (guint8 *) (newdata + i*4);
931 p = (guint32*) newdata + i;
932 *p = g | (g << 8) | (g << 16) | (*(l + 3) << 24);
935 glGenTextures(1, &pi->fog_texture);
936 glBindTexture(GL_TEXTURE_2D, pi->fog_texture);
937 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
938 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
941 0, GL_RGBA, GL_UNSIGNED_BYTE, newdata);
950 if (pi->map_texture) {
951 glDeleteTextures(1, &pi->map_texture);
954 if (pi->fog_texture) {
955 glDeleteTextures(1, &pi->fog_texture);
960 #include "../../pixmaps/question.111"
971 GLubyte question[question_height][question_width][4];
972 int xb, x, y, offset=0;
978 for (y=0; y<question_height; y++) {
979 for (xb=0; xb<question_width/8; xb++) {
980 for (x=0; x<8; x++) {
981 if (question_bits[offset] & (1 << x)) {
982 question[y][xb * 8 + x][0] = 255;
983 question[y][xb * 8 + x][1] = 255;
984 question[y][xb * 8 + x][2] = 255;
985 question[y][xb * 8 + x][3] = 255;
987 question[y][xb * 8 + x][0] = 0;
988 question[y][xb * 8 + x][1] = 0;
989 question[y][xb * 8 + x][2] = 0;
990 question[y][xb * 8 + x][3] = 0;
997 glGenTextures(1, &
pixmaps[0]->map_texture);
998 glBindTexture(GL_TEXTURE_2D,
pixmaps[0]->map_texture);
999 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1000 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1002 glTexImage2D(GL_TEXTURE_2D, 0, 4, question_width, question_height,
1003 0, GL_RGBA, GL_UNSIGNED_BYTE, &question[0][0][0]);
1005 glGenTextures(1, &
pixmaps[0]->fog_texture);
1006 glBindTexture(GL_TEXTURE_2D,
pixmaps[0]->fog_texture);
1007 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1008 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1010 glTexImage2D(GL_TEXTURE_2D, 0, 4, question_width, question_height,
1011 0, GL_RGBA, GL_UNSIGNED_BYTE, &question[0][0][0]);