Ejemplo n.º 1
0
// ウインドウの位置を取得
Vec2i AppEnv::windowPosition() {
  int x_pos;
  int y_pos;
  glfwGetWindowPos(window_(), &x_pos, &y_pos);
    
  return Vec2i(x_pos, y_pos);
}
Ejemplo n.º 2
0
void key_callback( GLFWwindow* window, int key, int scancode, int action, int mods )
{
    if (action != GLFW_PRESS)
        return;

    if (key == GLFW_KEY_ESCAPE && mods == 0)
        glfwSetWindowShouldClose(window, GLFW_TRUE);
    if ((key == GLFW_KEY_ENTER && mods == GLFW_MOD_ALT) ||
        (key == GLFW_KEY_F11 && mods == GLFW_MOD_ALT))
    {
        if (glfwGetWindowMonitor(window))
        {
            glfwSetWindowMonitor(window, NULL,
                                 windowed_xpos, windowed_ypos,
                                 windowed_width, windowed_height, 0);
        }
        else
        {
            GLFWmonitor* monitor = glfwGetPrimaryMonitor();
            if (monitor)
            {
                const GLFWvidmode* mode = glfwGetVideoMode(monitor);
                glfwGetWindowPos(window, &windowed_xpos, &windowed_ypos);
                glfwGetWindowSize(window, &windowed_width, &windowed_height);
                glfwSetWindowMonitor(window, monitor, 0, 0, mode->width, mode->height, mode->refreshRate);
            }
        }
    }
}
Ejemplo n.º 3
0
// --------------------------------------------------------------------------------------------------------------------
void window::position(int* x, int* y) const
{
    if (is_open())
    {
        glfwGetWindowPos(_internal->_window, x, y);
    }
}
Ejemplo n.º 4
0
int main(int argc, char** argv)
{
    int x, y, width;
    GLuint texture;

    glfwSetErrorCallback(error_callback, NULL);

    if (!glfwInit())
        exit(EXIT_FAILURE);

    windows[0] = open_window("First", NULL, OFFSET, OFFSET);
    if (!windows[0])
    {
        glfwTerminate();
        exit(EXIT_FAILURE);
    }

    // This is the one and only time we create a texture
    // It is created inside the first context, created above
    // It will then be shared with the second context, created below
    texture = create_texture();

    glfwGetWindowPos(windows[0], &x, &y);
    glfwGetWindowSize(windows[0], &width, NULL);

    // Put the second window to the right of the first one
    windows[1] = open_window("Second", windows[0], x + width + OFFSET, y);
    if (!windows[1])
    {
        glfwTerminate();
        exit(EXIT_FAILURE);
    }

    // Set drawing color for both contexts
    glfwMakeContextCurrent(windows[0]);
    glColor3f(0.6f, 0.f, 0.6f);
    glfwMakeContextCurrent(windows[1]);
    glColor3f(0.6f, 0.6f, 0.f);

    glfwMakeContextCurrent(windows[0]);

    while (!glfwWindowShouldClose(windows[0]) &&
           !glfwWindowShouldClose(windows[1]))
    {
        glfwMakeContextCurrent(windows[0]);
        draw_quad(texture);

        glfwMakeContextCurrent(windows[1]);
        draw_quad(texture);

        glfwSwapBuffers(windows[0]);
        glfwSwapBuffers(windows[1]);

        glfwWaitEvents();
    }

    glfwTerminate();
    exit(EXIT_SUCCESS);
}
static int luaWindow_position(lua_State* L){
    int x, y;
    auto window = reinterpret_cast<GLFWwindow*>(lua_touserdata(L, lua_upvalueindex(1)));
    glfwGetWindowPos(window, &x, &y);
    lua_pushinteger(L, x);
    lua_pushinteger(L, y);
    return 2;
}
Ejemplo n.º 6
0
Archivo: tearing.c Proyecto: glfw/glfw
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
    if (action != GLFW_PRESS)
        return;

    switch (key)
    {
        case GLFW_KEY_UP:
        {
            if (swap_interval + 1 > swap_interval)
                set_swap_interval(window, swap_interval + 1);
            break;
        }

        case GLFW_KEY_DOWN:
        {
            if (swap_tear)
            {
                if (swap_interval - 1 < swap_interval)
                    set_swap_interval(window, swap_interval - 1);
            }
            else
            {
                if (swap_interval - 1 >= 0)
                    set_swap_interval(window, swap_interval - 1);
            }
            break;
        }

        case GLFW_KEY_ESCAPE:
            glfwSetWindowShouldClose(window, 1);
            break;

        case GLFW_KEY_F11:
        case GLFW_KEY_ENTER:
        {
            static int x, y, width, height;

            if (mods != GLFW_MOD_ALT)
                return;

            if (glfwGetWindowMonitor(window))
                glfwSetWindowMonitor(window, NULL, x, y, width, height, 0);
            else
            {
                GLFWmonitor* monitor = glfwGetPrimaryMonitor();
                const GLFWvidmode* mode = glfwGetVideoMode(monitor);
                glfwGetWindowPos(window, &x, &y);
                glfwGetWindowSize(window, &width, &height);
                glfwSetWindowMonitor(window, monitor,
                                     0, 0, mode->width, mode->height,
                                     mode->refreshRate);
            }

            break;
        }
    }
}
Ejemplo n.º 7
0
glm::ivec2 Window::position() const
{
    if (!m_window)
        return glm::ivec2();

    int x, y;
    glfwGetWindowPos(m_window, &x, &y);

    return glm::ivec2(x, y);
}
Ejemplo n.º 8
0
	bool CGlfwProxy::_createOpengl()
	{

		if (!glfwInit())
			return -1;

		m_glWnd = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
		if (!m_glWnd)
		{
			glfwTerminate();
			return -1;
		}

		glfwMakeContextCurrent(m_glWnd);

		PrintInfo();

		int w, h;
		glfwGetWindowSize(m_glWnd, &w, &h);

		int x, y;
		glfwGetWindowPos(m_glWnd, &x, &y);

		glfwSetWindowTitle(m_glWnd, "OK,this is a window.");

		HWND hdl = glfwGetWin32Window(m_glWnd);

		LONG style = ::GetWindowLong(hdl, GWL_STYLE);
		style &= (~WS_BORDER);
		style &= (~WS_CAPTION);
		style &= (~WS_DLGFRAME);
		style &= (~WS_SYSMENU);
		style &= (~WS_THICKFRAME);
		style &= (~WS_MINIMIZEBOX);
		style &= (~WS_MAXIMIZEBOX);
		::SetWindowLong(hdl, GWL_STYLE, style);

		glfwSetMouseButtonCallback(m_glWnd, MouseBtnFunc);
		glfwSetCursorPosCallback(m_glWnd, MouseMoveFunc);
		glfwSetScrollCallback(m_glWnd, MouseWheelFunc);
		glfwSetKeyCallback(m_glWnd, KeyFunc);

		//
		CFInput::InputEventLsnr_Debug lsnr;

		CFInput::CInputMngr::Ins().RegisterEventLsnr(CFInput::MouseDown, &lsnr);
		CFInput::CInputMngr::Ins().RegisterEventLsnr(CFInput::MouseUp, &lsnr);
		CFInput::CInputMngr::Ins().RegisterEventLsnr(CFInput::MouseMove, &lsnr);
		CFInput::CInputMngr::Ins().RegisterEventLsnr(CFInput::MouseWheel, &lsnr);
		CFInput::CInputMngr::Ins().RegisterEventLsnr(CFInput::KeyDown, &lsnr);
		CFInput::CInputMngr::Ins().RegisterEventLsnr(CFInput::KeyUp, &lsnr);
		//

		return true;
	}
Ejemplo n.º 9
0
void PlayerWin::relaunch()
{
    int x = 0;
    int y = 0;
	GLViewImpl* view = dynamic_cast<GLViewImpl*>(Director::getInstance()->getOpenGLView());
	glfwGetWindowPos(view->getWindow(), &x, &y);
    _project.setWindowOffset(Vec2(x, y));
    openNewPlayerWithProjectConfig(_project);

    quit();
}
Ejemplo n.º 10
0
Archivo: iconify.c Proyecto: 2php/glfw
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
    printf("%0.2f Key %s\n",
           glfwGetTime(),
           action == GLFW_PRESS ? "pressed" : "released");

    if (action != GLFW_PRESS)
        return;

    switch (key)
    {
        case GLFW_KEY_I:
            glfwIconifyWindow(window);
            break;
        case GLFW_KEY_M:
            glfwMaximizeWindow(window);
            break;
        case GLFW_KEY_R:
            glfwRestoreWindow(window);
            break;
        case GLFW_KEY_ESCAPE:
            glfwSetWindowShouldClose(window, GLFW_TRUE);
            break;
        case GLFW_KEY_F11:
        case GLFW_KEY_ENTER:
        {
            if (mods != GLFW_MOD_ALT)
                return;

            if (glfwGetWindowMonitor(window))
            {
                glfwSetWindowMonitor(window, NULL,
                                     windowed_xpos, windowed_ypos,
                                     windowed_width, windowed_height,
                                     0);
            }
            else
            {
                GLFWmonitor* monitor = glfwGetPrimaryMonitor();
                if (monitor)
                {
                    const GLFWvidmode* mode = glfwGetVideoMode(monitor);
                    glfwGetWindowPos(window, &windowed_xpos, &windowed_ypos);
                    glfwGetWindowSize(window, &windowed_width, &windowed_height);
                    glfwSetWindowMonitor(window, monitor,
                                         0, 0, mode->width, mode->height,
                                         mode->refreshRate);
                }
            }

            break;
        }
    }
}
Ejemplo n.º 11
0
JNIEXPORT jint JNICALL Java_com_badlogic_jglfw_Glfw_glfwGetWindowY(JNIEnv* env, jclass clazz, jlong window) {


//@line:751

		int x = 0;
		int y = 0;
		glfwGetWindowPos((GLFWwindow*)window, &x, &y);
		return y;
	

}
Ejemplo n.º 12
0
	// mouse
	bool BaseWindow::MouseEvent(double x, double y)
	{
        int sx = 0, sy = 0;
        int w = 0, h = 0;
        
        glfwGetWindowPos(m_window, &sx, &sy);
        glfwGetWindowSize(m_window, &w, &h);
        
        int ex = sx + w;
        int ey = sy + h;
        
        return x >= sx && x <= ex && y >= sy && y <= ey;
	}
Ejemplo n.º 13
0
Window::Window(RootObjectWeakPtr root)
       : BaseObject(root)
{
    _type = "window";

    ScenePtr scene = dynamic_pointer_cast<Scene>(root.lock());
    GlWindowPtr w = scene->getNewSharedWindow();
    if (w.get() == nullptr)
        return;

    _window = w;
    _isInitialized = setProjectionSurface();
    if (!_isInitialized)
        Log::get() << Log::WARNING << "Window::" << __FUNCTION__ << " - Error while creating the Window" << Log::endl;
    else
        Log::get() << Log::MESSAGE << "Window::" << __FUNCTION__ << " - Window created successfully" << Log::endl;

    _viewProjectionMatrix = glm::ortho(-1.f, 1.f, -1.f, 1.f);

    setEventsCallbacks();
    registerAttributes();
    showCursor(false);

    // Get the default window size and position
    glfwGetWindowPos(_window->get(), &_windowRect[0], &_windowRect[1]);
    glfwGetFramebufferSize(_window->get(), &_windowRect[2], &_windowRect[3]);

    // Create the render FBO
    glGetError();
    glGenFramebuffers(1, &_renderFbo);
    setupRenderFBO();

    glBindFramebuffer(GL_FRAMEBUFFER, _renderFbo);
    GLenum _status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
    if (_status != GL_FRAMEBUFFER_COMPLETE)
        Log::get() << Log::WARNING << "Window::" << __FUNCTION__ << " - Error while initializing render framebuffer object: " << _status << Log::endl;
    else
        Log::get() << Log::MESSAGE << "Window::" << __FUNCTION__ << " - Render framebuffer object successfully initialized" << Log::endl;
    glBindFramebuffer(GL_FRAMEBUFFER, 0);

    // And the read framebuffer
    setupReadFBO();
}
Ejemplo n.º 14
0
Window::Window(unsigned int width, unsigned int height, const char* title) {
  bool initStatus = glfwInit();
  ASSERT(initStatus, "could not initialize GLFW3");

  glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, true);
  glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  handle_ = glfwCreateWindow(width, height, title, NULL, NULL);
  if (!handle_) {
    glfwTerminate();
    ASSERT(false, "could not create a GLFW Window");
  }
  glfwMakeContextCurrent(handle_);

  glfwGetWindowPos(handle_, &position_.x, &position_.y);
  Vec2i size;
  glfwGetWindowSize(handle_, &size.x, &size.y);
  size_.x = static_cast<unsigned int>(size.x);
  size_.y = static_cast<unsigned int>(size.y);
}
Ejemplo n.º 15
0
	EApp::EApp(std::function<void(void)> startUp, int w, int h) : end(false), close([&]() {
		end = true;
	}), quit(s_quit) {
		if (eApp) {
			throw Exceptions::AppAlreadyConstructed();
		} else {
			eApp = this;
			msgQueue = EMsgQueue::create();
			glfwInit();
			handle = glfwCreateWindow(w, h, "", 0, 0);
			glfwSetWindowCloseCallback(handle, CloseCallback);
			glfwSetWindowSizeCallback(handle, ResizeCallback);
			glfwSetCursorPosCallback(handle, CursorPosCallback);
			coreThread = new std::thread([&](std::function<void(void)> startUp, int w, int h) {
				glfwMakeContextCurrent(handle);
				glfwSwapInterval(0);
				glewInit();

				glEnable(GL_SCISSOR_TEST);
				glEnable(GL_BLEND);
				glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

				glViewport(0, 0, w, h);
				glMatrixMode(GL_PROJECTION);
				glLoadIdentity();
				gluOrtho2D(0, w, 0, h);
				glTranslated(0, h, 0);
				glScaled(1, -1, 1);
				glMatrixMode(GL_MODELVIEW);

				int px, py;
				glfwGetWindowPos(handle, &px, &py);
				eRootWindow = new EMainWindow(px, py, w, h);
				s_quit.connect(loop.quit);
				startUp();
				loop.exec();
				close();
			}, startUp, w, h);
		}
	}
Ejemplo n.º 16
0
			Window::Window(int w, int h, const char *t, GLFWmonitor *m, GLFWwindow *s, const Hint &hint)
				:isEnable(false), X(x), Y(y), Width(this->w), Height(this->h), fbWidth(fbw), fbHeight(fbh), Title(title)
			{
				hint.apply();
				if(!(wnd = glfwCreateWindow(w, h, t, m, s)))
				{
					std::cerr << "Createting Window is failed.\n";
					std::cerr << "Window '" << t << "' is not enable.\n";
					return;
				}
				glfwSetWindowUserPointer(wnd, this);
				glfwSetWindowPosCallback(wnd, position);
				glfwSetWindowSizeCallback(wnd, size);
				glfwSetFramebufferSizeCallback(wnd, fbsize);
				isEnable = true;
				++glfw_window_count;

				glfwGetWindowPos      (wnd, &x, &y);
				glfwGetWindowSize     (wnd, &this->w, &this->h);
				glfwGetFramebufferSize(wnd, &fbw, &fbh);
				title = t;
			}
Ejemplo n.º 17
0
void widget_move(int16 x, int16 y)
{
	ui.screen.newmousepos.x = x;
	ui.screen.newmousepos.y = y;

	if(ui.moving){
		int windowx, windowy;

		glfwGetWindowPos(get_the_window(),&windowx, &windowy);
		glfwSetWindowPos(get_the_window(),x + windowx - ui.screen.mouseclick.x, y + windowy - ui.screen.mouseclick.y);
	}
	else{
		if(focused){
			if(focused->action & WIDGET_MOVING){
				int16 tempx = x - ui.screen.mousepos.x ;
				int16 tempy = y - ui.screen.mousepos.y ;

				if(focused->pos.x + tempx <= 0 || focused->pos.y + tempy <= 0)
					return;

				if(focused->pos.x + focused->width + tempx>= get_screen_width() +400|| focused->pos.y + focused->height + tempy >= get_screen_height() + 400)
					return;

				focused->pos.x = focused->pos.x + tempx ;

				focused->pos.y = focused->pos.y + tempy ;
				widget_position_update(focused);
			}

			if(widget_rect_contains(focused))
				widget_mouse_over(focused);
		}
		else{
			if(ui.root)
				widget_mouse_over(ui.root);
		}
	}
}
Ejemplo n.º 18
0
void Window::setupRenderFBO()
{
    glfwGetWindowPos(_window->get(), &_windowRect[0], &_windowRect[1]);
    glfwGetFramebufferSize(_window->get(), &_windowRect[2], &_windowRect[3]);

    glBindFramebuffer(GL_FRAMEBUFFER, _renderFbo);

    if (!_depthTexture)
    {
        _depthTexture = make_shared<Texture_Image>(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 512, 512, 0, GL_DEPTH_COMPONENT, GL_FLOAT, nullptr);
        glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, _depthTexture->getTexId(), 0);
    }
    else
    {
        _depthTexture->setAttribute("resizable", {1});
        _depthTexture->setAttribute("size", {_windowRect[2], _windowRect[3]});
        _depthTexture->setAttribute("resizable", {0});
    }

    if (!_colorTexture)
    {
        _colorTexture = make_shared<Texture_Image>();
        _colorTexture->setAttribute("filtering", {0});
        _colorTexture->reset(GL_TEXTURE_2D, 0, GL_SRGB8_ALPHA8, _windowRect[2], _windowRect[3], 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
        glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, _colorTexture->getTexId(), 0);
    }
    else
    {
        _colorTexture->setAttribute("resizable", {1});
        _colorTexture->setAttribute("size", {_windowRect[2], _windowRect[3]});
        _colorTexture->setAttribute("resizable", {0});
    }

    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
Ejemplo n.º 19
0
void Screen::fullscreen(bool full)
{

    if ( full )
    {
        batb->log << "batb->screen->fullscreen( true )" << std::endl;
        // save this before going fullscreen
        glfwGetWindowSize( glfw_window, &nonfullscreen_wth_, &nonfullscreen_hth_ );
        glfwGetWindowPos( glfw_window, &nonfullscreen_xpos_, &nonfullscreen_ypos_ );

        // full screen on
        glfw_monitor = glfwGetPrimaryMonitor();
        auto mode = glfwGetVideoMode( glfw_monitor );
        glfwSetWindowMonitor( glfw_window,  glfw_monitor, 0, 0, mode->width, mode->height, mode->refreshRate);
    }
    else
    {
        batb->log << "batb->screen->fullscreen( false )" << std::endl;

        // back to window with previos pos and size
        glfw_monitor = nullptr;
        glfwSetWindowMonitor( glfw_window, glfw_monitor, nonfullscreen_xpos_, nonfullscreen_ypos_, nonfullscreen_wth_, nonfullscreen_hth_, 0 );
    }
}
Ejemplo n.º 20
0
static void glfw_key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
    if (action == GLFW_PRESS) {
        if (key == GLFW_KEY_ESCAPE) {
            glfwSetWindowShouldClose(window, GLFW_TRUE);
        } else if (key == GLFW_KEY_F11 || key == GLFW_KEY_ENTER && mods == GLFW_MOD_ALT) {
            static int last_window_xpos, last_window_ypos;
            static int last_window_width, last_window_height;

            VK_CHECK(vkDeviceWaitIdle(vk.device));
            GLFWmonitor* monitor = glfwGetWindowMonitor(window);
            if (monitor == nullptr) {
                glfwGetWindowPos(window, &last_window_xpos, &last_window_ypos);
                last_window_width = window_width;
                last_window_height = window_height;

                monitor = glfwGetPrimaryMonitor();
                const GLFWvidmode* mode = glfwGetVideoMode(monitor);
                glfwSetWindowMonitor(window, monitor, 0, 0, mode->width, mode->height, mode->refreshRate);
            } else {
                glfwSetWindowMonitor(window, nullptr, last_window_xpos, last_window_ypos, last_window_width, last_window_height, 0);
            }
        }
    }
}
Ejemplo n.º 21
0
void VCInteropWindowGetPos( int* x, int* y )
{
	glfwGetWindowPos(VCWindow::Instance->GLFWWindowHandle, x, y);
}
Ejemplo n.º 22
0
glm::ivec2 kit::Window::getPosition()
{
  glm::ivec2 returner;
  glfwGetWindowPos(this->m_glfwHandle, &returner.x, &returner.y);
  return returner;
}
Ejemplo n.º 23
0
bool positionInWindow(GLFWwindow *window, double mouseX, double mouseY) {
    int windowX, windowY;
    glfwGetWindowPos(window, &windowX, &windowY);

    return (mouseX > 0 && mouseX < WINDOW_WIDTH) && (mouseY > 0 && mouseY < WINDOW_HEIGHT);
}
Ejemplo n.º 24
0
void Window::setPosition(const Vec2i& position) {
  glfwSetWindowPos(handle_, position.x, position.y);
  glfwGetWindowPos(handle_, &position_.x, &position_.y);
  return;
}
Ejemplo n.º 25
0
	vec2i CWindow::GetPosition() const
	{
		vec2i Position;
		glfwGetWindowPos(WindowHandle, &Position.X, &Position.Y);
		return Position;
	}
Ejemplo n.º 26
0
 void call(GLFWwindow* window, int* xpos, int* ypos) {
   return glfwGetWindowPos(window, xpos, ypos);
 }
Ejemplo n.º 27
0
int Window::GetPosY() const {
	int* x = 0;
   	int* y = 0;
	glfwGetWindowPos(window, x, y);
	return (*y);
}
Ejemplo n.º 28
0
int main(int argc, char** argv)
{
    int ch;
    GLFWwindow* windows[2];
    GLuint texture, program, vertex_buffer;
    GLint mvp_location, vpos_location, color_location, texture_location;

    srand((unsigned int) time(NULL));

    glfwSetErrorCallback(error_callback);

    if (!glfwInit())
        exit(EXIT_FAILURE);

    while ((ch = getopt(argc, argv, "d")) != -1)
    {
        switch (ch)
        {
            case 'd':
                glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GLFW_TRUE);
                break;
        }
    }

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);

    windows[0] = glfwCreateWindow(400, 400, "First", NULL, NULL);
    if (!windows[0])
    {
        glfwTerminate();
        exit(EXIT_FAILURE);
    }

    glfwSetKeyCallback(windows[0], key_callback);

    glfwMakeContextCurrent(windows[0]);

    // Only enable vsync for the first of the windows to be swapped to
    // avoid waiting out the interval for each window
    glfwSwapInterval(1);

    // The contexts are created with the same APIs so the function
    // pointers should be re-usable between them
    gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);

    if (GLAD_GL_KHR_debug)
    {
        glDebugMessageCallback(debug_callback, NULL);
        glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, NULL, GL_TRUE);
    }

    // Create the OpenGL objects inside the first context, created above
    // All objects will be shared with the second context, created below
    {
        int x, y;
        char pixels[16 * 16];
        GLuint vertex_shader, fragment_shader;

        glGenTextures(1, &texture);
        glBindTexture(GL_TEXTURE_2D, texture);

        for (y = 0;  y < 16;  y++)
        {
            for (x = 0;  x < 16;  x++)
                pixels[y * 16 + x] = rand() % 256;
        }

        glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 16, 16, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, pixels);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

        vertex_shader = glCreateShader(GL_VERTEX_SHADER);
        glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
        glCompileShader(vertex_shader);

        fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
        glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL);
        glCompileShader(fragment_shader);

        program = glCreateProgram();
        glAttachShader(program, vertex_shader);
        glAttachShader(program, fragment_shader);
        glLinkProgram(program);

        mvp_location = glGetUniformLocation(program, "MVP");
        color_location = glGetUniformLocation(program, "color");
        texture_location = glGetUniformLocation(program, "texture");
        vpos_location = glGetAttribLocation(program, "vPos");

        glGenBuffers(1, &vertex_buffer);
        glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
        glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    }

    glUseProgram(program);
    glUniform1i(texture_location, 0);

    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, texture);

    glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
    glEnableVertexAttribArray(vpos_location);
    glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE,
                          sizeof(vertices[0]), (void*) 0);

    windows[1] = glfwCreateWindow(400, 400, "Second", NULL, windows[0]);
    if (!windows[1])
    {
        glfwTerminate();
        exit(EXIT_FAILURE);
    }

    // Place the second window to the right of the first
    {
        int xpos, ypos, left, right, width;

        glfwGetWindowSize(windows[0], &width, NULL);
        glfwGetWindowFrameSize(windows[0], &left, NULL, &right, NULL);
        glfwGetWindowPos(windows[0], &xpos, &ypos);

        glfwSetWindowPos(windows[1], xpos + width + left + right, ypos);
    }

    glfwSetKeyCallback(windows[1], key_callback);

    glfwMakeContextCurrent(windows[1]);

    // While objects are shared, the global context state is not and will
    // need to be set up for each context

    glUseProgram(program);

    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, texture);

    glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
    glEnableVertexAttribArray(vpos_location);
    glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE,
                          sizeof(vertices[0]), (void*) 0);

    while (!glfwWindowShouldClose(windows[0]) &&
           !glfwWindowShouldClose(windows[1]))
    {
        int i;
        const vec3 colors[2] =
        {
            { 0.3f, 0.4f, 1.f },
            { 0.8f, 0.4f, 1.f }
        };

        for (i = 0;  i < 2;  i++)
        {
            int width, height;
            mat4x4 mvp;

            glfwGetFramebufferSize(windows[i], &width, &height);
            glfwMakeContextCurrent(windows[i]);

            glViewport(0, 0, width, height);

            mat4x4_ortho(mvp, 0.f, 1.f, 0.f, 1.f, 0.f, 1.f);
            glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp);
            glUniform3fv(color_location, 1, colors[i]);
            glDrawArrays(GL_TRIANGLE_FAN, 0, 4);

            glfwSwapBuffers(windows[i]);
        }

        glfwWaitEvents();
    }

    glfwTerminate();
    exit(EXIT_SUCCESS);
}
Ejemplo n.º 29
0
Vector2i Window::getPosition()
{
	int x = -1, y = -1;
	glfwGetWindowPos(s_window, &x, &y);
	return Vector2i(x, y);
}
Ejemplo n.º 30
0
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;
}