|
| 1 | +#ifdef MONOCLE_LINUX |
| 2 | + |
| 3 | +#include "LinuxPlatform.h" |
| 4 | + |
| 5 | +#include<X11/X.h> |
| 6 | +#include<X11/extensions/Xrandr.h> |
| 7 | + |
| 8 | +#include <stdio.h> |
| 9 | +#include <time.h> |
| 10 | +#include <unistd.h> |
| 11 | + |
| 12 | +#include "../Debug.h" |
| 13 | +#include "../Core.h" |
| 14 | +#include "../Graphics.h" |
| 15 | + |
| 16 | +// opengl/windows init code baesd on http://nehe.gamedev.net |
| 17 | +// keyboard code based on SDL http://www.libsdl.org/ |
| 18 | + |
| 19 | +#ifndef VK_0 |
| 20 | +#define VK_0 '0' |
| 21 | +#define VK_1 '1' |
| 22 | +#define VK_2 '2' |
| 23 | +#define VK_3 '3' |
| 24 | +#define VK_4 '4' |
| 25 | +#define VK_5 '5' |
| 26 | +#define VK_6 '6' |
| 27 | +#define VK_7 '7' |
| 28 | +#define VK_8 '8' |
| 29 | +#define VK_9 '9' |
| 30 | +#define VK_A 'A' |
| 31 | +#define VK_B 'B' |
| 32 | +#define VK_C 'C' |
| 33 | +#define VK_D 'D' |
| 34 | +#define VK_E 'E' |
| 35 | +#define VK_F 'F' |
| 36 | +#define VK_G 'G' |
| 37 | +#define VK_H 'H' |
| 38 | +#define VK_I 'I' |
| 39 | +#define VK_J 'J' |
| 40 | +#define VK_K 'K' |
| 41 | +#define VK_L 'L' |
| 42 | +#define VK_M 'M' |
| 43 | +#define VK_N 'N' |
| 44 | +#define VK_O 'O' |
| 45 | +#define VK_P 'P' |
| 46 | +#define VK_Q 'Q' |
| 47 | +#define VK_R 'R' |
| 48 | +#define VK_S 'S' |
| 49 | +#define VK_T 'T' |
| 50 | +#define VK_U 'U' |
| 51 | +#define VK_V 'V' |
| 52 | +#define VK_W 'W' |
| 53 | +#define VK_X 'X' |
| 54 | +#define VK_Y 'Y' |
| 55 | +#define VK_Z 'Z' |
| 56 | +#endif /* VK_0 */ |
| 57 | + |
| 58 | +#define VK_SEMICOLON 0xBA |
| 59 | +#define VK_EQUALS 0xBB |
| 60 | +#define VK_COMMA 0xBC |
| 61 | +#define VK_MINUS 0xBD |
| 62 | +#define VK_PERIOD 0xBE |
| 63 | +#define VK_SLASH 0xBF |
| 64 | +#define VK_GRAVE 0xC0 |
| 65 | +#define VK_LBRACKET 0xDB |
| 66 | +#define VK_BACKSLASH 0xDC |
| 67 | +#define VK_RBRACKET 0xDD |
| 68 | +#define VK_APOSTROPHE 0xDE |
| 69 | +#define VK_BACKTICK 0xDF |
| 70 | +#define VK_OEM_102 0xE2 |
| 71 | + |
| 72 | +#define REPEATED_KEYMASK (1<<30) |
| 73 | +#define EXTENDED_KEYMASK (1<<24) |
| 74 | + |
| 75 | +namespace Monocle |
| 76 | +{ |
| 77 | + LinuxPlatform *LinuxPlatform::instance = NULL; |
| 78 | + |
| 79 | + //TODO: cleanup code, replace message boxes |
| 80 | + bool LinuxPlatform::CreatePlatformWindow(const char* title, int width, int height, int bits, bool fullscreenflag) |
| 81 | + { |
| 82 | + const char *windowName = "Monocle Engine\0"; |
| 83 | + Window hRootWindow; |
| 84 | + GLint att[] = { GLX_RGBA, GLX_DEPTH_SIZE, bits, GLX_DOUBLEBUFFER, None }; |
| 85 | + XVisualInfo *vi; |
| 86 | + Colormap cmap; |
| 87 | + XSetWindowAttributes swa; |
| 88 | + |
| 89 | + fullscreen=fullscreenflag; // Set The Global Fullscreen Flag |
| 90 | + |
| 91 | + hDisplay = XOpenDisplay(NULL); |
| 92 | + if (hDisplay == NULL) { |
| 93 | + fprintf(stderr, "Cannot open display\n"); |
| 94 | + return(false); |
| 95 | + } |
| 96 | + |
| 97 | + hRootWindow = DefaultRootWindow(hDisplay); |
| 98 | + hScreen = DefaultScreen(hDisplay); |
| 99 | + |
| 100 | + vi = glXChooseVisual(hDisplay, 0, att); |
| 101 | + |
| 102 | + if(vi == NULL) { |
| 103 | + fprintf(stderr, "No visual was found.\n"); |
| 104 | + return(false); |
| 105 | + } |
| 106 | + cmap = XCreateColormap(hDisplay, hRootWindow, vi->visual, AllocNone); |
| 107 | + |
| 108 | + swa.colormap = cmap; |
| 109 | + swa.event_mask = ExposureMask | KeyPressMask; |
| 110 | + |
| 111 | + hWindow = XCreateWindow( |
| 112 | + hDisplay, // display |
| 113 | + hRootWindow, // parent |
| 114 | + 0, 0, width, height, // rect |
| 115 | + 1, // border width |
| 116 | + bits, // depth |
| 117 | + InputOutput, // class |
| 118 | + vi->visual, // visual |
| 119 | + CWColormap | CWEventMask, // value mask |
| 120 | + &swa); |
| 121 | + XMapWindow(hDisplay, hWindow); |
| 122 | + XStoreName(hDisplay, hWindow, title); |
| 123 | + |
| 124 | + GC = glXCreateContext(hDisplay, vi, NULL, GL_TRUE); |
| 125 | + glXMakeCurrent(hDisplay, hWindow, GC); |
| 126 | + glEnable(GL_DEPTH_TEST); |
| 127 | + |
| 128 | + // TODO: IMPLEMENT fullscreen |
| 129 | + |
| 130 | + CenterWindow(); |
| 131 | + |
| 132 | + return true; |
| 133 | + } |
| 134 | + |
| 135 | + void LinuxPlatform::GetDesktopSize(int *width, int *height) |
| 136 | + { |
| 137 | + *width = 800; |
| 138 | + *height = 600; |
| 139 | + // TODO: IMPLEMENT |
| 140 | + } |
| 141 | + |
| 142 | + void LinuxPlatform::CenterWindow() |
| 143 | + { |
| 144 | + // TODO: IMPLEMENT |
| 145 | + } |
| 146 | + |
| 147 | + void LinuxPlatform::KillPlatformWindow() |
| 148 | + { |
| 149 | + glXMakeCurrent(hDisplay, None, NULL); |
| 150 | + glXDestroyContext(hDisplay, GC); |
| 151 | + XDestroyWindow(hDisplay, hWindow); |
| 152 | + XCloseDisplay(hDisplay); |
| 153 | + } |
| 154 | + |
| 155 | + Platform *Platform::instance = NULL; |
| 156 | + |
| 157 | + bool Platform::keys[KEY_MAX]; |
| 158 | + bool Platform::mouseButtons[3]; |
| 159 | + Vector2 Platform::mousePosition; |
| 160 | + |
| 161 | + Platform::Platform() |
| 162 | + { |
| 163 | + LinuxPlatform::instance = new LinuxPlatform(); |
| 164 | + instance = this; |
| 165 | + LinuxPlatform::instance->platform = this; |
| 166 | + |
| 167 | + for (int i = 0; i < KEY_MAX; i++) |
| 168 | + { |
| 169 | + keys[i] = false; |
| 170 | + localKeymap[i] = KEY_UNDEFINED; |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + void Platform::Init() |
| 175 | + { |
| 176 | + Init(800, 600, 24, false); |
| 177 | + } |
| 178 | + |
| 179 | + void Platform::Init(int w, int h, int bits, bool fullscreen) |
| 180 | + { |
| 181 | + LinuxPlatform::instance->CreatePlatformWindow("Monocle Powered", w, h, bits, fullscreen); |
| 182 | + width = w; |
| 183 | + height = h; |
| 184 | + } |
| 185 | + |
| 186 | + void Platform::Update() |
| 187 | + { |
| 188 | + XEvent event; |
| 189 | + |
| 190 | + while (XPending(LinuxPlatform::instance->hDisplay)) { |
| 191 | + XNextEvent(LinuxPlatform::instance->hDisplay, &event); |
| 192 | + switch(event.type) { |
| 193 | + case Expose: |
| 194 | + glViewport(0, 0, width, height); |
| 195 | + ShowBuffer(); |
| 196 | + break; |
| 197 | + case KeyPress: |
| 198 | + break; |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + void Platform::SetMouseButton(int button, bool on) |
| 204 | + { |
| 205 | + mouseButtons[button] = on; |
| 206 | + } |
| 207 | + |
| 208 | + long Platform::GetMilliseconds() |
| 209 | + { |
| 210 | + return (long)time(0); |
| 211 | + } |
| 212 | + |
| 213 | + bool Platform::IsKeyPressed(KeyCode keyCode) |
| 214 | + { |
| 215 | + return instance->keys[(int)keyCode]; |
| 216 | + } |
| 217 | + |
| 218 | + void Platform::ShowBuffer() |
| 219 | + { |
| 220 | + glXSwapBuffers(LinuxPlatform::instance->hDisplay, LinuxPlatform::instance->hWindow); |
| 221 | + } |
| 222 | + |
| 223 | + int Platform::GetWidth() |
| 224 | + { |
| 225 | + return instance->width; |
| 226 | + } |
| 227 | + |
| 228 | + int Platform::GetHeight() |
| 229 | + { |
| 230 | + return instance->height; |
| 231 | + } |
| 232 | + |
| 233 | + void Platform::WindowSizeChanged(int w, int h) |
| 234 | + { |
| 235 | + instance->width = w; |
| 236 | + instance->height = h; |
| 237 | + Graphics::Resize(w, h); |
| 238 | + } |
| 239 | + |
| 240 | + void Platform::SetLocalKey(int key, bool on) |
| 241 | + { |
| 242 | + } |
| 243 | +} |
| 244 | + |
| 245 | +#endif |
0 commit comments