void Object::moveSnap(int xSnap, int ySnap) { int x = staticCasti(mPosition.x) % xSnap; int y = staticCasti(mPosition.y) % ySnap; if (x >= xSnap / 2) { mPosition.x += (xSnap - x); } else { mPosition.x -= x; } if (y >= ySnap / 2) { mPosition.y += (ySnap - y); } else { mPosition.y -= y; } }
int GLContext::evaluateFormat(uint32 bpp, const ContextSettings &settings, int colorBits, int depthBits, int stencilBits, int aa) { return std::abs(staticCasti(bpp - colorBits)) + std::abs(staticCasti(settings.depthBits - depthBits)) + std::abs(staticCasti(settings.stencilBits - stencilBits)) + std::abs(staticCasti(settings.aaLevel - aa)); }
void GLContextWin32::createContext(GLContextWin32 *shared, uint32 bpp, const ContextSettings &settings) { mSettings = settings; int bestFormat = 0; if (mSettings.aaLevel > 0) { PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB = reinterpretCast(PFNWGLCHOOSEPIXELFORMATARBPROC, wglGetProcAddress("wglChoosePixelFormatARB")); if (wglChoosePixelFormatARB) { int intAttributes[] = { WGL_DRAW_TO_WINDOW_ARB, GL_TRUE, WGL_SUPPORT_OPENGL_ARB, GL_TRUE, WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB, WGL_DOUBLE_BUFFER_ARB, GL_TRUE, WGL_SAMPLE_BUFFERS_ARB, (mSettings.aaLevel ? GL_TRUE : GL_FALSE), WGL_SAMPLES_ARB, staticCasti(mSettings.aaLevel), 0, 0 }; int formats[128]; UINT nbFormats; float floatAttributes[] = {0, 0}; bool isValid = wglChoosePixelFormatARB(mDeviceContext, intAttributes, floatAttributes, sizeof(formats) / sizeof(*formats), formats, &nbFormats) != 0; while ((!isValid || (nbFormats == 0)) && mSettings.aaLevel > 0) { --mSettings.aaLevel; intAttributes[11] = mSettings.aaLevel; isValid = wglChoosePixelFormatARB(mDeviceContext, intAttributes, floatAttributes, sizeof(formats) / sizeof(*formats), formats, &nbFormats) != 0; } if (isValid && nbFormats != 0) { int bestScore = 0xFFFF; for (UINT i = 0; i < nbFormats; ++i) { PIXELFORMATDESCRIPTOR attributes; attributes.nSize = sizeof(attributes); attributes.nVersion = 1; DescribePixelFormat(mDeviceContext, formats[i], sizeof(attributes), &attributes); int color = attributes.cRedBits + attributes.cGreenBits + attributes.cBlueBits + attributes.cAlphaBits; int score = evaluateFormat(bpp, mSettings, color, attributes.cDepthBits, attributes.cStencilBits, mSettings.aaLevel); if (score < bestScore) { bestScore = score; bestFormat = formats[i]; } } } } else { std::cerr << "Could not set aa.\n"; mSettings.aaLevel = 0; } } if (bestFormat == 0) { PIXELFORMATDESCRIPTOR descriptor; ZeroMemory(&descriptor, sizeof(descriptor)); descriptor.nSize = sizeof(descriptor); descriptor.nVersion = 1; descriptor.iLayerType = PFD_MAIN_PLANE; descriptor.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; descriptor.iPixelType = PFD_TYPE_RGBA; descriptor.cColorBits = staticCast(BYTE, bpp); descriptor.cDepthBits = staticCast(BYTE, mSettings.depthBits); descriptor.cStencilBits = staticCast(BYTE, mSettings.stencilBits); descriptor.cAlphaBits = bpp == 32 ? 8 : 0; bestFormat = ChoosePixelFormat(mDeviceContext, &descriptor); if (bestFormat == 0) { std::cerr << "Failed to find a suitable pixel format for device context, cannot create OpenGL context.\n"; return; } } PIXELFORMATDESCRIPTOR actualFormat; actualFormat.nSize = sizeof(actualFormat); actualFormat.nVersion = 1; DescribePixelFormat(mDeviceContext, bestFormat, sizeof(actualFormat), &actualFormat); mSettings.depthBits = actualFormat.cDepthBits; mSettings.stencilBits = actualFormat.cStencilBits; if (!SetPixelFormat(mDeviceContext, bestFormat, &actualFormat)) { std::cerr << "Failed to set pixel format for the devicecontext, cannot create opengl context.\n"; return; } HGLRC sharedContext = shared ? shared->mContext : null; while (!mContext && (mSettings.majorVersion >= 3)) { PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB = reinterpretCast(PFNWGLCREATECONTEXTATTRIBSARBPROC, wglGetProcAddress("wglCreateContextAttribsARB")); if (wglCreateContextAttribsARB) { int attributes[] = { WGL_CONTEXT_MAJOR_VERSION_ARB, staticCasti(mSettings.majorVersion), WGL_CONTEXT_MINOR_VERSION_ARB, staticCasti(mSettings.minorVersion), WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB, 0, 0 }; mContext = wglCreateContextAttribsARB(mDeviceContext, sharedContext, attributes); } else { std::cerr << "Could not find \"wglCreateContextAttribsARB\".\n"; } if (!mContext) { if (mSettings.minorVersion > 0) { --mSettings.minorVersion; } else { --mSettings.majorVersion; mSettings.minorVersion = 9; } } } //no opengl >= 3 if (!mContext) { mSettings.majorVersion = 2; mSettings.minorVersion = 0; mContext = wglCreateContext(mDeviceContext); if (!mContext) { std::cerr << "Failed to create an opengl context for this window.\n"; return; } if (sharedContext) { static Mutex mutex; Lock lock(mutex); if (!wglShareLists(sharedContext, mContext)) { std::cerr << "Failed to share the opengl context.\n"; } } } }
bool Object::placeSnapped(const Vector2i &snap) const { return (staticCasti(mPosition.x) % snap.x == 0 && staticCasti(mPosition.y) % snap.y == 0); }
bool Object::placeSnapped(int xSnap, int ySnap) const { return (staticCasti(mPosition.x) % xSnap == 0 && staticCasti(mPosition.y) % ySnap == 0); }