vulkan/main.cpp
//-----------------------------------------------------------------------------
// [SECTION] general setup functions implementation
//-----------------------------------------------------------------------------
[SECTION] general setup functions implementation
[ SECTION ] general setup functions implementation
static void
create_window()
{
#ifdef _WIN32
    WNDCLASSEXW winClass = {};
    winClass.cbSize = sizeof(WNDCLASSEXW);
    winClass.style = CS_HREDRAW | CS_VREDRAW;
    winClass.lpfnWndProc = &WndProc;
    winClass.hInstance = ::GetModuleHandle(nullptr);
    winClass.hIcon = ::LoadIconW(0, IDI_APPLICATION);
    winClass.hCursor = ::LoadCursorW(0, IDC_ARROW);
    winClass.lpszClassName = L"MyWindowClass";
    winClass.hIconSm = ::LoadIconW(0, IDI_APPLICATION);
    if (!::RegisterClassExW(&winClass)) 
    {
        ::MessageBoxA(0, "RegisterClassEx failed", "Fatal Error", MB_OK);
        assert(false);
        return;
    }
    RECT initialRect = { 0, 0, g_width, g_height };
    ::AdjustWindowRectEx(&initialRect, WS_OVERLAPPEDWINDOW, FALSE, WS_EX_OVERLAPPEDWINDOW);
    LONG initialWidth = initialRect.right - initialRect.left;
    LONG initialHeight = initialRect.bottom - initialRect.top;
    g_hwnd = ::CreateWindowExW(WS_EX_OVERLAPPEDWINDOW,
        winClass.lpszClassName,
        L"Vulkan",
        WS_OVERLAPPEDWINDOW | WS_VISIBLE,
        CW_USEDEFAULT, CW_USEDEFAULT,
        initialWidth,
        initialHeight,
        0, 0, ::GetModuleHandle(nullptr), 0);
    if (!g_hwnd) 
    {
        ::MessageBoxA(0, "CreateWindowEx failed", "Fatal Error", MB_OK);
        assert(false);
        return;
    }
#elif defined(__APPLE__)
#else
    g_display = XOpenDisplay(nullptr);           
    XAutoRepeatOff(g_display);                   
    g_connection = XGetXCBConnection(g_display); 
    if(xcb_connection_has_error(g_connection))
    {
        assert(false && "Failed to connect to X server via XCB.");
        return;
    }
    const xcb_setup_t* setup = xcb_get_setup(g_connection);
    xcb_screen_iterator_t it = xcb_setup_roots_iterator(setup);
    int screen_p = 0;
    for (int s = screen_p; s > 0; s--) 
    {
        xcb_screen_next(&it);
    }
    g_screen = it.data;
    g_window = xcb_generate_id(g_connection);
    unsigned int event_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
    unsigned int  event_values = XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE |
                    XCB_EVENT_MASK_KEY_PRESS | XCB_EVENT_MASK_KEY_RELEASE |
                    XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_POINTER_MOTION |
                    XCB_EVENT_MASK_STRUCTURE_NOTIFY;
    unsigned int  value_list[] = {g_screen->black_pixel, event_values};
    xcb_create_window(
        g_connection,
        XCB_COPY_FROM_PARENT,  
        g_window,
        g_screen->root,        
        200,                              
        200,                              
        g_width,                          
        g_height,                         
        0,                              
        XCB_WINDOW_CLASS_INPUT_OUTPUT,  
        g_screen->root_visual,
        event_mask,
        value_list);
    xcb_change_property(
        g_connection,
        XCB_PROP_MODE_REPLACE,
        g_window,
        XCB_ATOM_WM_NAME,
        XCB_ATOM_STRING,
        8,  
        strlen("Vulkan"),
        "Vulkan");
    xcb_intern_atom_cookie_t wm_delete_cookie = xcb_intern_atom(
        g_connection,
        0,
        strlen("WM_DELETE_WINDOW"),
        "WM_DELETE_WINDOW");
    xcb_intern_atom_cookie_t wm_protocols_cookie = xcb_intern_atom(
        g_connection,
        0,
        strlen("WM_PROTOCOLS"),
        "WM_PROTOCOLS");
    xcb_intern_atom_reply_t* wm_delete_reply = xcb_intern_atom_reply(
        g_connection,
        wm_delete_cookie,
        NULL);
    xcb_intern_atom_reply_t* wm_protocols_reply = xcb_intern_atom_reply(
        g_connection,
        wm_protocols_cookie,
        NULL);
    g_wm_delete_win = wm_delete_reply->atom;
    g_wm_protocols = wm_protocols_reply->atom;
    xcb_change_property(
        g_connection,
        XCB_PROP_MODE_REPLACE,
        g_window,
        wm_protocols_reply->atom,
        4,
        32,
        1,
        &wm_delete_reply->atom);
    xcb_map_window(g_connection, g_window);
    int stream_result = xcb_flush(g_connection);
    if (stream_result <= 0) 
    {
        assert(false && "An error occurred when flusing the stream");
        return;
    }
#endif
}
static void create_window ( ) { #ifdef _WIN32 WNDCLASSEXW winClass = { } ; winClass . cbSize = sizeof ( WNDCLASSEXW ) ; winClass . style = CS_HREDRAW | CS_VREDRAW ; winClass . lpfnWndProc = & WndProc ; winClass . hInstance = :: GetModuleHandle ( nullptr ) ; winClass . hIcon = :: LoadIconW ( 0 , IDI_APPLICATION ) ; winClass . hCursor = :: LoadCursorW ( 0 , IDC_ARROW ) ; winClass . lpszClassName = L" " ; winClass . hIconSm = :: LoadIconW ( 0 , IDI_APPLICATION ) ; if ( ! :: RegisterClassExW ( & winClass ) ) { :: MessageBoxA ( 0 , " " , " " , MB_OK ) ; assert ( false ) ; return ; } RECT initialRect = { 0 , 0 , g_width , g_height } ; :: AdjustWindowRectEx ( & initialRect , WS_OVERLAPPEDWINDOW , FALSE , WS_EX_OVERLAPPEDWINDOW ) ; LONG initialWidth = initialRect . right - initialRect . left ; LONG initialHeight = initialRect . bottom - initialRect . top ; g_hwnd = :: CreateWindowExW ( WS_EX_OVERLAPPEDWINDOW , winClass . lpszClassName , L" " , WS_OVERLAPPEDWINDOW | WS_VISIBLE , CW_USEDEFAULT , CW_USEDEFAULT , initialWidth , initialHeight , 0 , 0 , :: GetModuleHandle ( nullptr ) , 0 ) ; if ( ! g_hwnd ) { :: MessageBoxA ( 0 , " " , " " , MB_OK ) ; assert ( false ) ; return ; } #elif defined ( __APPLE__ ) #else g_display = XOpenDisplay ( nullptr ) ; XAutoRepeatOff ( g_display ) ; g_connection = XGetXCBConnection ( g_display ) ; if ( xcb_connection_has_error ( g_connection ) ) { assert ( false && " " ) ; return ; } const xcb_setup_t * setup = xcb_get_setup ( g_connection ) ; xcb_screen_iterator_t it = xcb_setup_roots_iterator ( setup ) ; int screen_p = 0 ; for ( int s = screen_p ; s > 0 ; s -- ) { xcb_screen_next ( & it ) ; } g_screen = it . data ; g_window = xcb_generate_id ( g_connection ) ; unsigned int event_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK ; unsigned int event_values = XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_KEY_PRESS | XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_POINTER_MOTION | XCB_EVENT_MASK_STRUCTURE_NOTIFY ; unsigned int value_list [ ] = { g_screen -> black_pixel , event_values } ; xcb_create_window ( g_connection , XCB_COPY_FROM_PARENT , g_window , g_screen -> root , 200 , 200 , g_width , g_height , 0 , XCB_WINDOW_CLASS_INPUT_OUTPUT , g_screen -> root_visual , event_mask , value_list ) ; xcb_change_property ( g_connection , XCB_PROP_MODE_REPLACE , g_window , XCB_ATOM_WM_NAME , XCB_ATOM_STRING , 8 , strlen ( " " ) , " " ) ; xcb_intern_atom_cookie_t wm_delete_cookie = xcb_intern_atom ( g_connection , 0 , strlen ( " " ) , " " ) ; xcb_intern_atom_cookie_t wm_protocols_cookie = xcb_intern_atom ( g_connection , 0 , strlen ( " " ) , " " ) ; xcb_intern_atom_reply_t * wm_delete_reply = xcb_intern_atom_reply ( g_connection , wm_delete_cookie , NULL ) ; xcb_intern_atom_reply_t * wm_protocols_reply = xcb_intern_atom_reply ( g_connection , wm_protocols_cookie , NULL ) ; g_wm_delete_win = wm_delete_reply -> atom ; g_wm_protocols = wm_protocols_reply -> atom ; xcb_change_property ( g_connection , XCB_PROP_MODE_REPLACE , g_window , wm_protocols_reply -> atom , 4 , 32 , 1 , & wm_delete_reply -> atom ) ; xcb_map_window ( g_connection , g_window ) ; int stream_result = xcb_flush ( g_connection ) ; if ( stream_result <= 0 ) { assert ( false && " " ) ; return ; } #endif }
[SECTION] general setup functions implementation
[ SECTION ] general setup functions implementation
// connect to x // turn off key repeats   // retrieve connection from display // get data from x server // Loop through screens using iterator // After screens have been looped through, assign it. // Allocate a XID for the window to be created. // Register event types. // XCB_CW_BACK_PIXEL = filling then window bg with a single colour // XCB_CW_EVENT_MASK is required. // Listen for keyboard and mouse buttons // Values to be sent over XCB (bg colour, events) // Create the window // depth // parent //x //y //width //height // No border //class // Change the title // data should be viewed 8 bits at a time // Tell the server to notify when the window manager // attempts to destroy the window. // Map the window to the screen // Flush the stream