Creating a cross platform compatible window that is maximized upon startup

import sys 
import os 
sys.path.append(os.getcwd())

from OpenGL.GL import *
import glfw 
from glfw.GLFW import *
from glfw import _GLFWwindow as GLFWwindow
import glm 

from includes.GLFW.init_glfw import init_glfw
from includes.GL.GLArcball import GLArcball 
from includes.GL.GLTextImageGroup import GLTextImageGroup

global view_state
view_state =  {
    'eye': glm.vec3( 0, 0, 2000.0 ),
    'target': glm.vec3( 0,0,0 ),
    'position': glm.vec3( 0,0,0 ),
    'up': glm.vec3( 0,1,0 ),
    'near':  -500000,
    'far': 50000,
    'window_width': None,
    'window_height': None,
    'lookAt': glm.mat4(1.0),
    'world': glm.mat4(1.0)
}

global input_state
input_state = {
    'shift_left': {
        'isDown': False
    },
    'mouse_right': {
        'isDown': False
    },
    'mouse_X_prev': None,
    'mouse_Y_prev': None,
    'mouse_X_cur': None,
    'mouse_Y_cur': None
}

global camera 
camera = GLArcball( view_state['eye'], view_state['position'], view_state['up'])

def mouse_callback(window, xpos, ypos):
    if (input_state['shift_left']['isDown'] == True and
        input_state['mouse_right']['isDown'] == True):

        input_state['mouse_X_cur'] = xpos 
        input_state['mouse_Y_cur'] = ypos 

        cur_mouse_ndc = camera.mouse_to_ndc(xpos, 
                                            ypos, 
                                            view_state['window_width'], 
                                            view_state['window_height'])

        if input_state['mouse_X_prev'] != None:
            prev_mouse_ndc = camera.mouse_to_ndc(input_state['mouse_X_prev'], 
                                                 input_state['mouse_Y_prev'], 
                                                 view_state['window_width'], 
                                                 view_state['window_height'])
            camera.rotate(prev_mouse_ndc, cur_mouse_ndc)
        input_state['mouse_X_prev'] = xpos
        input_state['mouse_Y_prev'] = ypos
    elif (input_state['shift_left']['isDown'] == False and
        input_state['mouse_right']['isDown'] == True):

        input_state['mouse_X_cur'] = xpos 
        input_state['mouse_Y_cur'] = ypos 

        cur_mouse_ndc = camera.mouse_to_ndc(xpos, 
                                            ypos, 
                                            view_state['window_width'], 
                                            view_state['window_height'])

        if input_state['mouse_X_prev'] != None:
            prev_mouse_ndc = camera.mouse_to_ndc(input_state['mouse_X_prev'], 
                                                 input_state['mouse_Y_prev'], 
                                                 view_state['window_width'], 
                                                 view_state['window_height'])
            
            delta = prev_mouse_ndc - cur_mouse_ndc 
            delta.x = -delta.x
            camera.pan(delta)
        input_state['mouse_X_prev'] = xpos
        input_state['mouse_Y_prev'] = ypos

def mouse_button_callback(window, button, action, mods):
    if button == glfw.MOUSE_BUTTON_RIGHT:
        if action == glfw.PRESS:
            input_state['mouse_right']['isDown'] = True
        elif action == glfw.RELEASE:
            input_state['mouse_right']['isDown'] = False
            input_state['mouse_X_prev'] = None
            input_state['mouse_Y_prev'] = None
            input_state['mouse_X_cur'] = None
            input_state['mouse_Y_cur'] = None

def key_callback(window, key, scancode, action, mods):
    if key == glfw.KEY_LEFT_SHIFT:
        if action == glfw.PRESS:
            input_state['shift_left']['isDown'] = True
        elif action == glfw.RELEASE:
            input_state['shift_left']['isDown'] = False

def scrollwheel_callback(window, xdelta, ydelta):
    scale_factor = 1.0
    if(ydelta==1.0):
        scale_factor=1.1
        view_state['world'] = glm.scale( view_state['world'], glm.vec3(scale_factor, scale_factor, scale_factor))
    else:
        scale_factor=0.9
        view_state['world'] = glm.scale( view_state['world'], glm.vec3(scale_factor, scale_factor, scale_factor))


def framebuffer_size_callback(window: GLFWwindow, width: int, height: int) -> None:
    glViewport(0, 0, width, height) 

def main():
    win_width, win_height, window = init_glfw("LearnOpenGL")
    view_state['window_width'] = win_width 
    view_state['window_height'] = win_height
    view_state['lookAt'] = glm.lookAt(view_state['eye'], glm.vec3(0, 0, 0), glm.vec3(0, 1, 0))
    view_state['ortho'] = glm.ortho(-(view_state['window_width'] / 2), view_state['window_width'] / 2, 
                                         -(view_state['window_height'] / 2), view_state['window_height'] / 2, view_state['near'], view_state['far']) 
    glfwSetScrollCallback(window, scrollwheel_callback)
    glfwSetWindowAspectRatio(window, int(win_width), int(win_height))
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback) 
    glfwSetKeyCallback(window, key_callback)  
    glfwSetMouseButtonCallback(window, mouse_button_callback) 
    glfwSetCursorPosCallback(window, mouse_callback)
    glEnable(GL_BLEND)
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
    glViewport(0, 0, win_width, win_height)
    init_objects()
    while not glfwWindowShouldClose(window):
        render(window)
    glfwTerminate()
    print('done...')

def init_objects():
    # Create example OpenGL objects for drawing....
    pass 



def render(window):
    while not glfwWindowShouldClose(window):
        world = view_state['world']
        view = camera.transform()
        projection = view_state['ortho']
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        #Draw the example OpenGL objects...
        glfwSwapBuffers(window)
        glfwPollEvents()
        glClearColor(0.2, 0.3, 0.3, 1.0)

if __name__ == "__main__":
    main()
import glfw 
from glfw.GLFW import *
from OpenGL.GL import *
import platform 

def init_glfw(title):
    if not glfw.init():
        raise Exception('glfw can not be initialized')

    glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 4)
    glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 6)
    glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE)  # For macOS
    glfwWindowHint(GLFW_RED_BITS, 8)
    glfwWindowHint(GLFW_GREEN_BITS, 8)
    glfwWindowHint(GLFW_BLUE_BITS, 8)
    glfwWindowHint(GLFW_ALPHA_BITS, 8)
    
    if platform.system() == "Darwin":
        glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, GL_TRUE)
    
    window = glfw.create_window(640, 480, title, None, None)
    if not window:
        glfw.terminate()
        raise Exception('glfw window can not be created')
    
    glfw.make_context_current(window)
    glfw.maximize_window(window)
    width, height = glfw.get_window_size(window)
    
    return width, height, window

This type of windowing system which works fine and does produce a maximized winodw on Microsoft Window 11, doesn’t work at all on Ubuntu 24. So far I am using the code on an 8’th gen Intel i7 CPU with a GTX1060 (mobile).

When the code is run on Ubuntu the window appears to be locked up for keyboard or mouse input, the window doesn’t yet appear to be maximized and attempting to grab the title bar with the mouse to move the window or double clicking the title bar of the window produces no effect at all.

Why is this happening? What can be done to create a more cross compatible system where things work the same way on Microsoft Windows and Ubuntu? Can a GLFW be produced in the maximized state upon startup in such a way that the code works without modification on Microsoft Windows and also Ubuntu 24?

Can a GLFW be produced in the maximized state upon startup …?

Have you tried:

glfwWindowHint( GLFW_MAXIMIZED, GLFW_TRUE );

As for the other issues, are you using the latest GLFW, and what Windowing system are you using on Ubuntu (Wayland or X11)?

It sounds like you might be using Wayland, and although Wayland is improving I still have found some issues with it. You may want to add the following code prior to glfwInit() to select X11:

	if (glfwPlatformSupported(GLFW_PLATFORM_X11))
	{
		glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_X11);
	}
1 Like

I can confirm it that the difficulty was that Wayland was selected by default on my Ubuntu 24 system and that by setting X11 to the default windowing system, that the difficulty I had went away.