static void list_modes(GLFWmonitor* monitor) { int count, x, y, widthMM, heightMM, dpi, i; GLFWvidmode mode = glfwGetVideoMode(monitor); const GLFWvidmode* modes = glfwGetVideoModes(monitor, &count); glfwGetMonitorPos(monitor, &x, &y); glfwGetMonitorPhysicalSize(monitor, &widthMM, &heightMM); printf("Name: %s (%s)\n", glfwGetMonitorName(monitor), glfwGetPrimaryMonitor() == monitor ? "primary" : "secondary"); printf("Current mode: %s\n", format_mode(&mode)); printf("Virtual position: %i %i\n", x, y); dpi = (int) ((float) mode.width * 25.4f / (float) widthMM); printf("Physical size: %i x %i mm (%i dpi)\n", widthMM, heightMM, dpi); printf("Modes:\n"); for (i = 0; i < count; i++) { printf("%3u: %s", (unsigned int) i, format_mode(modes + i)); if (memcmp(&mode, modes + i, sizeof(GLFWvidmode)) == 0) printf(" (current mode)"); putchar('\n'); } }
static void monitor_callback(GLFWmonitor* monitor, int event) { if (event == GLFW_CONNECTED) { int x, y, widthMM, heightMM; const GLFWvidmode* mode = glfwGetVideoMode(monitor); glfwGetMonitorPos(monitor, &x, &y); glfwGetMonitorPhysicalSize(monitor, &widthMM, &heightMM); printf("%08x at %0.3f: Monitor %s (%ix%i at %ix%i, %ix%i mm) was connected\n", counter++, glfwGetTime(), glfwGetMonitorName(monitor), mode->width, mode->height, x, y, widthMM, heightMM); } else { printf("%08x at %0.3f: Monitor %s was disconnected\n", counter++, glfwGetTime(), glfwGetMonitorName(monitor)); } }
/// Dump a list of monitor info to Log and stdout. /// http://www.glfw.org/docs/3.0/monitor.html void PrintMonitorInfo() { int count; GLFWmonitor** monitors = glfwGetMonitors(&count); printf("Found %d monitors:\n", count); LOG_INFO("Found %d monitors:", count); for (int i=0; i<count; ++i) { GLFWmonitor* pMonitor = monitors[i]; if (pMonitor == NULL) continue; printf(" Monitor %d:\n", i); LOG_INFO(" Monitor %d:", i); /// Monitor name const char* pName = glfwGetMonitorName(pMonitor); printf(" Name: %s\n", pName); LOG_INFO(" Name: %s", pName); /// Monitor Physical Size int widthMM, heightMM; glfwGetMonitorPhysicalSize(pMonitor, &widthMM, &heightMM); //const double dpi = mode->width / (widthMM / 25.4); printf(" physical size: %d x %d mm\n"); LOG_INFO(" physical size: %d x %d mm"); /// Modes const GLFWvidmode* mode = glfwGetVideoMode(pMonitor); printf(" Current mode: %dx%d @ %dHz (RGB %d %d %d bits)\n", mode->width, mode->height, mode->refreshRate, mode->redBits, mode->greenBits, mode->blueBits); LOG_INFO(" Current mode: %dx%d @ %dHz (RGB %d %d %d bits)", mode->width, mode->height, mode->refreshRate, mode->redBits, mode->greenBits, mode->blueBits); #if 0 int modeCount; const GLFWvidmode* modes = glfwGetVideoModes(pMonitor, &modeCount); printf(" %d Modes:\n", modeCount); LOG_INFO(" %d Modes:", modeCount); for (int j=0; j<modeCount; ++j) { const GLFWvidmode& m = modes[j]; printf(" %dx%d @ %dHz (RGB %d %d %d bits)\n", m.width, m.height, m.refreshRate, m.redBits, m.greenBits, m.blueBits); LOG_INFO(" %dx%d @ %dHz (RGB %d %d %d bits)", m.width, m.height, m.refreshRate, m.redBits, m.greenBits, m.blueBits); } #endif } }
Vector<uint, 2> Monitor::getPhysicalSize() const { int x; int y; glfwGetMonitorPhysicalSize(monitor, &x, & y); return {static_cast<uint>(x), static_cast<uint>(y)}; }
// Obtain the primary monitors size void Properties::GetPrimaryMonitorSize() { GLFWmonitor* Monitor = glfwGetPrimaryMonitor(); if (Monitor) { glfwGetMonitorPhysicalSize(Monitor,&MonWidth,&MonHeight); } else { std::cout << "Error Obtaining Primary Monitor." << std::endl; exit(1); }; };
Screen::Screen(string& title, char flags) { if (!initializedGLFW) initGraphicContexCreation(); glfwGetMonitorPhysicalSize(glfwGetPrimaryMonitor(), &_width, &_height); createWindow(_width, _height, title, flags); setupGraphicFunctions(); }
JNIEXPORT jint JNICALL Java_com_badlogic_jglfw_Glfw_glfwGetMonitorPhysicalHeight(JNIEnv* env, jclass clazz, jlong monitor) { //@line:619 int width = 0; int height = 0; glfwGetMonitorPhysicalSize((GLFWmonitor*)monitor, &width, &height); return height; }
Monitor::Monitor(GLFWmonitor *monitor) { if(0 != monitor) { const char *nm = glfwGetMonitorName(monitor); name = nm; int width; int height; glfwGetMonitorPhysicalSize(monitor, &width, &height); size = glm::vec2(width, height); current = Mode(glfwGetVideoMode(monitor)); int modeCount; const GLFWvidmode *vidmods = glfwGetVideoModes(monitor, &modeCount); for(int i = 0; i < modeCount; ++i) { modes.push_back(Mode(vidmods + i)); } } descriptor = monitor; }
Vector3 Graphics::GetDisplayDPI(int monitor) const { int moncount=0; GLFWmonitor **monitors = glfwGetMonitors(&moncount); Vector3 result; if(monitor<moncount) { int widthMM, heightMM; glfwGetMonitorPhysicalSize(monitors[monitor], &widthMM, &heightMM); int diagonalMM = std::sqrt(widthMM * widthMM + heightMM*heightMM); const GLFWvidmode *mode = glfwGetVideoMode(monitors[monitor]); const float hdpi = mode->width / (widthMM / 25.4f); const float vdpi = mode->height / (heightMM / 25.4f); const float ddpi = std::sqrt(mode->width*mode->width + mode->height*mode->height) / (diagonalMM / 25.4f); result = {ddpi,hdpi,vdpi}; } return result; }
AlloyContext::AlloyContext(int width, int height, const std::string& title, const Theme& theme) : nvgContext(nullptr), window(nullptr), theme(theme) { threadId = std::this_thread::get_id(); if (glfwInit() != GL_TRUE) { throw std::runtime_error("Could not initialize GLFW."); } glfwSetErrorCallback( [](int error, const char* desc) { std::cout << "GLFW Error [" << error << "] " << desc << std::endl; }); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, 1); glfwWindowHint(GLFW_VISIBLE, 0); window = glfwCreateWindow(width, height, title.c_str(), NULL, NULL); if (!window) { glfwTerminate(); throw std::runtime_error("Could not create window."); } glfwMakeContextCurrent(window); glewExperimental = GL_TRUE; if (glewInit() != GLEW_OK) { throw std::runtime_error("Could not initialize GLEW."); } glGetError(); glDepthFunc(GL_LEQUAL); glEnable(GL_DEPTH_TEST); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glfwGetFramebufferSize(window, &width, &height); glViewport(0, 0, width, height); viewSize = int2(width, height); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, 1); glfwWindowHint(GLFW_VISIBLE, 0); offscreenWindow = glfwCreateWindow(width, height, "Offscreen", NULL, NULL); if (!offscreenWindow) { glfwTerminate(); throw std::runtime_error("Could not create window."); } glfwMakeContextCurrent(offscreenWindow); glewExperimental = GL_TRUE; if (glewInit() != GLEW_OK) { throw std::runtime_error("Could not initialize GLEW."); } glGetError(); glDepthFunc(GL_LEQUAL); glEnable(GL_DEPTH_TEST); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glViewport(0, 0, width, height); glfwMakeContextCurrent(window); nvgContext = nvgCreateGL3(NVG_ANTIALIAS | NVG_STENCIL_STROKES); const float2 TextureCoords[6] = { float2(1.0f, 0.0f), float2(0.0f, 0.0f), float2(0.0f, 1.0f), float2(0.0f, 1.0f), float2(1.0f, 1.0f), float2( 1.0f, 0.0f) }; const float3 PositionCoords[6] = { float3(1.0f, 1.0f, 0.0f), float3(0.0f, 1.0f, 0.0f), float3(0.0f, 0.0f, 0.0f), float3(0.0f, 0.0f, 0.0f), float3(1.0f, 0.0f, 0.0f), float3(1.0f, 1.0f, 0.0f) }; glGenVertexArrays(1, &vaoImageOnScreen.vao); glBindVertexArray(vaoImageOnScreen.vao); glGenBuffers(1, &vaoImageOnScreen.positionBuffer); glBindBuffer(GL_ARRAY_BUFFER, vaoImageOnScreen.positionBuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * 6, PositionCoords, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glGenBuffers(1, &vaoImageOnScreen.uvBuffer); glBindBuffer(GL_ARRAY_BUFFER, vaoImageOnScreen.uvBuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 2 * 6, TextureCoords, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); glfwHideWindow(offscreenWindow); glfwMakeContextCurrent(offscreenWindow); glGenVertexArrays(1, &vaoImageOffScreen.vao); glBindVertexArray(vaoImageOffScreen.vao); glGenBuffers(1, &vaoImageOffScreen.positionBuffer); glBindBuffer(GL_ARRAY_BUFFER, vaoImageOffScreen.positionBuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * 6, PositionCoords, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glGenBuffers(1, &vaoImageOffScreen.uvBuffer); glBindBuffer(GL_ARRAY_BUFFER, vaoImageOffScreen.uvBuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 2 * 6, TextureCoords, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); glfwMakeContextCurrent(window); int widthMM, heightMM; GLFWmonitor* monitor = glfwGetPrimaryMonitor(); if (monitor == nullptr) throw std::runtime_error("Could not find monitor."); const GLFWvidmode* mode = glfwGetVideoMode(monitor); if (mode == nullptr) throw std::runtime_error("Could not find video monitor."); glfwGetMonitorPhysicalSize(monitor, &widthMM, &heightMM); dpmm = double2(mode->width / (double) widthMM, mode->height / (double) heightMM); int winWidth, winHeight, fbWidth, fbHeight; glfwGetWindowSize(window, &winWidth, &winHeight); glfwGetFramebufferSize(window, &fbWidth, &fbHeight); // Calculate pixel ration for hi-dpi devices. screenSize = int2(winWidth,winHeight); viewSize = int2(fbWidth,fbHeight); pixelRatio = pixel((float) fbWidth / (float) winWidth); lastAnimateTime = std::chrono::steady_clock::now(); lastCursorTime = std::chrono::steady_clock::now(); lastUpdateTime = std::chrono::steady_clock::now(); cursor = &Cursor::Normal; }
int main() { GLFWwindow *window; char *userptr = "userptr"; glfwSetErrorCallback(errorcb); assert(glfwInit() == GL_TRUE); assert(!strcmp(glfwGetVersionString(), "3.2.1 JS WebGL Emscripten")); assert(glfwGetCurrentContext() == NULL); { int major, minor, rev; glfwGetVersion(&major, &minor, &rev); assert(major == 3); assert(minor == 2); assert(rev == 1); } { int count, x, y, w, h; GLFWmonitor **monitors = glfwGetMonitors(&count); assert(count == 1); for (int i = 0; i < count; ++i) { assert(monitors[i] != NULL); } assert(glfwGetPrimaryMonitor() != NULL); glfwGetMonitorPos(monitors[0], &x, &y); glfwGetMonitorPhysicalSize(monitors[0], &w, &h); assert(glfwGetMonitorName(monitors[0]) != NULL); glfwSetMonitorCallback(monitcb); // XXX: not implemented // assert(glfwGetVideoModes(monitors[0], &count) != NULL); // assert(glfwGetVideoMode(monitors[0]) != NULL); // glfwSetGamma(monitors[0], 1.0f); // assert(glfwGetGammaRamp(monitors[0]) != NULL); // glfwSetGammaRamp(monitors[0], ramp); } { int x, y, w, h; glfwDefaultWindowHints(); glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API); window = glfwCreateWindow(640, 480, "glfw3.c", NULL, NULL); assert(window != NULL); glfwSetWindowPosCallback(window, wposicb); glfwSetWindowSizeCallback(window, wsizecb); glfwSetWindowCloseCallback(window, wcloscb); glfwSetWindowRefreshCallback(window, wrfrscb); glfwSetWindowFocusCallback(window, wfocucb); glfwSetWindowIconifyCallback(window, wiconcb); glfwSetFramebufferSizeCallback(window, wfsizcb); assert(glfwWindowShouldClose(window) == 0); glfwSetWindowShouldClose(window, 1); assert(glfwWindowShouldClose(window) == 1); glfwSetWindowTitle(window, "test"); glfwSetWindowTitle(window, "glfw3.c"); // XXX: not implemented // glfwSetWindowPos(window, 1, 1); glfwGetWindowPos(window, &x, &y); // stub glfwGetWindowSize(window, &w, &h); assert(w == 640 && h == 480); glfwSetWindowSize(window, 1, 1); glfwGetWindowSize(window, &w, &h); assert(w == 1 && h == 1); glfwSetWindowSize(window, 640, 480); glfwGetFramebufferSize(window, &w, &h); // XXX: not implemented // glfwIconifyWindow(window); // glfwRestoreWindow(window); // glfwShowWindow(window); // glfwHideWindow(window); assert(glfwGetWindowMonitor(window) == NULL); glfwDestroyWindow(window); window = glfwCreateWindow(640, 480, "glfw3.c", glfwGetPrimaryMonitor(), NULL); assert(window != NULL); assert(glfwGetWindowMonitor(window) == glfwGetPrimaryMonitor()); glfwDestroyWindow(window); window = glfwCreateWindow(640, 480, "glfw3.c", NULL, NULL); assert(window != NULL); assert(glfwGetWindowAttrib(window, GLFW_CLIENT_API) == GLFW_OPENGL_ES_API); assert(glfwGetWindowUserPointer(window) == NULL); glfwSetWindowUserPointer(window, userptr); assert(glfwGetWindowUserPointer(window) == userptr); } { double x, y; glfwSetKeyCallback(window, wkeypcb); glfwSetCharCallback(window, wcharcb); glfwSetMouseButtonCallback(window, wmbutcb); glfwSetCursorPosCallback(window, wcurpcb); glfwSetCursorEnterCallback(window, wcurecb); glfwSetScrollCallback(window, wscrocb); // XXX: stub, events come immediatly // glfwPollEvents(); // glfwWaitEvents(); assert(glfwGetInputMode(window, GLFW_CURSOR) == GLFW_CURSOR_NORMAL); // XXX: not implemented // glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); glfwGetKey(window, GLFW_KEY_A); glfwGetMouseButton(window, 0); glfwGetCursorPos(window, &x, &y); // XXX: not implemented // glfwSetCursorPos(window, 0, 0); } { // XXX: not implemented // glfwJoystickPresent(joy); // glfwGetJoystickAxes(joy, &count); // glfwGetJoystickButtons(joy, &count); // glfwGetJoystickName(joy); } { // XXX: not implemented // glfwSetClipboardString(window, "string"); // glfwGetClipboardString(window); } { glfwGetTime(); glfwSetTime(0); } { glfwMakeContextCurrent(window); // stub assert(glfwGetCurrentContext() == window); glfwSwapBuffers(window); // stub glfwSwapInterval(0); // stub } { assert(glfwExtensionSupported("nonexistant") == 0); assert(glfwGetProcAddress("nonexistant") == NULL); } glfwTerminate(); #ifdef REPORT_RESULT REPORT_RESULT(1); #endif return 0; }
void call(GLFWmonitor* monitor, int* width, int* height) { glfwGetMonitorPhysicalSize(monitor, width, height); }
bool ctx_init(struct context *ctx, int w, int h, bool debug) { assert(! ctx->win); glfwSetErrorCallback(error_callback); if (! glfwInit()) { return false; } GLFWmonitor *monitor = glfwGetPrimaryMonitor(); const GLFWvidmode *mode = glfwGetVideoMode(monitor); int mw, mh; glfwGetMonitorPhysicalSize(monitor, &mw, &mh); ctx->dpi = mode->width / (mw / 25.4); ctx->hidpi = ctx->dpi > 180.0; ctx->vidmode = mode; if (ctx->hidpi) { w *= 2; h *= 2; } glfwWindowHint(GLFW_RESIZABLE, true); glfwWindowHint(GLFW_SRGB_CAPABLE, true); // Require OpenGL 3.3 or later glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); // Only support new core functionality glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, true); glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, true); glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_API); glfwWindowHint(GLFW_SAMPLES, 16); if ((ctx->win = glfwCreateWindow(w, h, "px", NULL, NULL)) == NULL) { return false; } glfwMakeContextCurrent(ctx->win); glfwSetWindowUserPointer(ctx->win, ctx); glfwSetInputMode(ctx->win, GLFW_CURSOR, GLFW_CURSOR_NORMAL); glfwSetInputMode(ctx->win, GLFW_STICKY_KEYS, true); glfwSetKeyCallback(ctx->win, key_callback); glfwSetMouseButtonCallback(ctx->win, mouse_button_callback); glfwSetCursorPosCallback(ctx->win, cursor_pos_callback); glfwSetCursorEnterCallback(ctx->win, focus_callback); glfwSetFramebufferSizeCallback(ctx->win, framebuffer_size_callback); glfwSetWindowPosCallback(ctx->win, window_pos_callback); glfwSetCharCallback(ctx->win, char_callback); glfwSetWindowAspectRatio(ctx->win, w, h); glfwGetFramebufferSize(ctx->win, &ctx->winw, &ctx->winh); ctx_win_center(ctx); gl_init(ctx->winw, ctx->winh, debug); int vw = ctx->winw, /* Virtual screen */ vh = ctx->winh; if (ctx->hidpi) { vw /= 2; vh /= 2; /* We can't create odd-sized framebuffer textures, so we make * the screen framebuffer even in case the virtual screen isn't. */ if (vw % 2 != 0) vw ++; if (vh % 2 != 0) vh ++; } infof("ctx", "real screen size: %dx%d", ctx->winw, ctx->winh); infof("ctx", "virtual screen size: %dx%d", vw, vh); ctx->lastframe = 0; ctx->frametime = 0; ctx->transforms = NULL; ctx->ortho = mat4ortho(ctx->winw, ctx->winh); ctx->font = malloc(sizeof(*ctx->font)); ctx->screen = framebuffer_screen(vw, vh, NULL); ctx->width = ctx->hidpi ? ctx->winw / 2 : ctx->winw; ctx->height = ctx->hidpi ? ctx->winh / 2 : ctx->winh; ctx_update_cursor_pos(ctx); ctx_blend_alpha(ctx); ctx_save(ctx); glfwSetTime(0); infof("ctx", "dpi = %f", ctx->dpi); return true; }
math::ivec CMonitor::getPhysicalSize() noexcept { std::int32_t width, height; glfwGetMonitorPhysicalSize(m_monitor, &width, &height); return math::ivec{width, height, 0, 0}; }