The window that wont die

new topic     » topic index » view thread      » older message » newer message

This is a multi-part message in MIME format.
--------------030601000503000505010506


A couple of problems:
1)  When I click on the quit button -the 'x' in the corner- I don't receive
WM_QUIT (0x0012).  I get WM_NCLBUTTONDOWN and WM_NCLBUTTONUP and then 0x0118
which I can't find the meaning of.  Same for the minimize and resize buttons.

2)  When I try to change display modes from windowed to fullscreen, and vice
versa, the old window never gets killed.

Pressing the 'Esc' button closes the app down just fine.


--------------030601000503000505010506
Content-Type: text/plain;
 name="BaseOpenGL.exw"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="BaseOpenGL.exw"

include get.e
include dll.e
include machine.e
include msgbox.e

without warning

procedure not_found(sequence name)
    puts(1,'\n' & "Couldn't find " & name & '\n')
    while get_key() != 'n' do end while
    abort(1)
end procedure

function or_all(sequence s)
-- or together all elements of a sequence
    atom result

    result = 0
    for i = 1 to length(s) do
    result = or_bits(result, s[i])
    end for
    return result
end function

function link_c_func(atom dll, sequence name, sequence args, atom result)
-- dynamically link a C routine as a Euphoria function
    integer handle

    handle = define_c_func(dll, name, args, result)
    if handle = -1 then
    not_found(name)
    else
    return handle
    end if
end function

function link_c_proc(atom dll, sequence name, sequence args)
-- dynamically link a C routine as a Euphoria function
    integer handle

    handle = define_c_proc(dll, name, args)
    if handle = -1 then
    not_found(name)
    else
    return handle
    end if
end function

integer ChangeDisplaySettingsA, ShowCursor, ReleaseDC, UnregisterClassA,
GetModuleHandleA, LoadIconA,
LoadCursorA, RegisterClassA, AdjustWindowRectEx, CreateWindowExA,
        DestroyWindow, GetDC,
ChoosePixelFormat, SetPixelFormat, DescribePixelFormat, ShowWindow,
        SetForegroundWindow,
SetFocus, PostQuitMessage, DefWindowProcA, PeekMessageA,
        TranslateMessage, DispatchMessageA,
        GetLastError, FormatMessage

integer glViewport, glMatrixMode, glLoadIdentity, gluPerspective, glShadeModel,
glClearColor,
glClearDepth, glEnable, glDepthFunc, glHint, glClear, glTranslatef,
        glBegin, glEnd, glVertex3f,
        glColor3f, wglMakeCurrent, wglDeleteContext,
        wglCreateContext, wglSwapBuffers

procedure link_dll_routines()
    atom kernel32, user32, gdi32, gl, glu

    kernel32 = open_dll("kernel32.dll")
    if kernel32 = NULL then
        not_found("kernel32.dll")
    end if

    user32 = open_dll("user32.dll")
    if user32 = NULL then
        not_found("user32.dll")
    end if

    gdi32 = open_dll("gdi32.dll")
    if gdi32 = NULL then
        not_found("gdi32.dll")
    end if

    gl = open_dll("OpenGL32.dll")
    if gl = NULL then
        not_found("OpenGL32.dll")
    end if

    glu = open_dll("glu32.dll")
    if glu = NULL then
        not_found("glu32.dll")
    end if

ChangeDisplaySettingsA = link_c_func(user32, "ChangeDisplaySettingsA",
    {C_POINTER,C_DOUBLE}, C_LONG)
    ShowCursor = link_c_func(user32, "ShowCursor", {C_INT}, C_INT)
    ReleaseDC = link_c_func(user32, "ReleaseDC", {C_INT,C_INT}, C_INT)
UnregisterClassA = link_c_func(user32, "UnregisterClassA",
    {C_POINTER,C_INT}, C_INT)
GetModuleHandleA = link_c_func(kernel32, "GetModuleHandleA", {C_POINTER},
    C_INT)
    LoadIconA = link_c_func(user32, "LoadIconA", {C_INT,C_POINTER}, C_INT)
    LoadCursorA = link_c_func(user32, "LoadCursorA", {C_INT,C_POINTER}, C_INT)
    RegisterClassA = link_c_func(user32, "RegisterClassA", {C_POINTER}, C_INT)
AdjustWindowRectEx = link_c_func(user32, "AdjustWindowRectEx",
    {C_POINTER,C_DOUBLE,C_INT,C_DOUBLE}, C_INT)
CreateWindowExA = link_c_func(user32, "CreateWindowExA",
    {C_INT,C_POINTER,C_POINTER,C_INT,C_INT,C_INT,
C_INT,C_INT,C_INT,C_INT,C_INT,C_POINTER}, C_INT)
    DestroyWindow = link_c_func(user32, "GetDC", {C_INT}, C_INT)
    GetDC = link_c_func(user32, "GetDC", {C_INT}, C_INT)
ChoosePixelFormat = link_c_func(gdi32, "ChoosePixelFormat",
    {C_INT,C_POINTER}, C_INT)
SetPixelFormat = link_c_func(gdi32, "SetPixelFormat", {C_INT, C_INT,
    C_POINTER}, C_INT)
DescribePixelFormat = link_c_func(gdi32, "DescribePixelFormat", {C_INT,
    C_INT, C_UINT, C_POINTER}, C_INT)
    ShowWindow = link_c_func(user32, "ShowWindow", {C_INT,C_INT}, C_INT)
SetForegroundWindow = link_c_func(user32, "SetForegroundWindow", {C_INT},
    C_INT)
    SetFocus = link_c_func(user32, "SetFocus", {C_INT}, C_INT)
    PostQuitMessage = link_c_proc(user32, "PostQuitMessage", {C_INT})
DefWindowProcA = link_c_func(user32, "DefWindowProcA",
    {C_INT,C_INT,C_INT,C_INT}, C_INT)
PeekMessageA = link_c_func(user32, "PeekMessageA",
    {C_POINTER,C_INT,C_UINT,C_UINT,C_UINT},C_INT)
TranslateMessage = link_c_func(user32, "TranslateMessage", {C_POINTER},
    C_INT)
DispatchMessageA = link_c_func(user32, "DispatchMessageA", {C_POINTER},
    C_INT)
    GetLastError = link_c_func(kernel32, "GetLastError", {}, C_INT)
FormatMessage = link_c_func(kernel32,
    "FormatMessageA",{C_INT,C_POINTER,C_INT,C_INT,C_POINTER,C_INT,C_POINTER},C_INT)


    glViewport = link_c_proc(gl, "glViewport", {C_INT,C_INT,C_INT,C_INT})
    glMatrixMode = link_c_proc(gl, "glMatrixMode", {C_INT})
    glLoadIdentity = link_c_proc(gl, "glLoadIdentity", {})
gluPerspective = link_c_proc(glu, "gluPerspective",
    {C_FLOAT,C_FLOAT,C_FLOAT,C_FLOAT})
    glShadeModel = link_c_proc(gl, "glShadeModel", {C_INT})
glClearColor = link_c_proc(gl, "glClearColor",
    {C_FLOAT,C_FLOAT,C_FLOAT,C_FLOAT})
    glClearDepth = link_c_proc(gl, "glClearDepth", {C_FLOAT})
    glEnable = link_c_proc(gl, "glEnable", {C_INT})
    glDepthFunc = link_c_proc(gl, "glDepthFunc", {C_INT})
    glHint = link_c_proc(gl, "glHint", {C_INT,C_INT})
    glClear = link_c_proc(gl, "glClear", {C_INT})
    glTranslatef = link_c_proc(gl, "glTranslatef", {C_FLOAT,C_FLOAT,C_FLOAT})
    glBegin = link_c_proc(gl, "glBegin", {C_INT})
    glEnd = link_c_proc(gl, "glEnd", {})
    glVertex3f = link_c_proc(gl, "glVertex3f", {C_FLOAT,C_FLOAT,C_FLOAT})
    glColor3f = link_c_proc(gl, "glColor3f", {C_FLOAT,C_FLOAT,C_FLOAT})
    wglSwapBuffers = link_c_func(gl, "wglSwapBuffers", {C_INT}, C_INT)
    wglDeleteContext = link_c_func(gl,"wglDeleteContext", {C_INT}, C_INT)
    wglCreateContext = link_c_func(gl, "wglCreateContext", {C_INT}, C_INT)
    wglMakeCurrent = link_c_func(gl, "wglMakeCurrent", {C_INT,C_INT}, C_INT)

end procedure
link_dll_routines()

--look up the error code in the system error messages and display the
corresponding message
procedure ErrorMsg(integer error_num, integer funcnum)
    atom Error_message  --address of the returned error message buffer
    integer i

    Error_message = allocate(32)
    puts(1,'\n' & "System Error: ")
    i = 0
if not
    (c_func(FormatMessage,{#1100,NULL,error_num,0,Error_message,30,NULL})) then
    else
while peek(peek4u(Error_message) + i) != NULL do  --loop until the end
        of the string
            printf(1,"%s",peek(peek4u(Error_message) + i))
            i += 1
        end while
        printf(1,"  %d\n",funcnum)
    end if

end procedure

atom hRC, hDC, hWnd, hInstance
sequence keys keys = repeat(0,256)
constant TRUE = 1, FALSE = 0
integer active, fullscreen
active = TRUE
fullscreen = TRUE
hRC = NULL
hDC = NULL

constant GL_PROJECTION = #1701, GL_MODELVIEW = #1700
procedure ReSizeGLScene(integer width, integer height)
    if height = 0 then
        height = 1
    end if
    c_proc(glViewport,{0,0,width,height})
    c_proc(glMatrixMode,{GL_PROJECTION})
    c_proc(glLoadIdentity,{})
    c_proc(gluPerspective,{45.0,(width*1.0)/(height*1.0),0.1,100.0})
    c_proc(glMatrixMode,{GL_MODELVIEW})
    c_proc(glLoadIdentity,{})

end procedure

constant GL_SMOOTH = #1D01, GL_DEPTH_TEST = #0B71, GL_LEQUAL = #0203,
GL_PERSPECTIVE_CORRECTION_HINT = #0C50,
         GL_NICEST = #1102
function InitGL()

    c_proc(glShadeModel,{GL_SMOOTH})
    c_proc(glClearColor,{0.0,0.0,0.0,0.5})
    c_proc(glClearDepth,{1.0})
    c_proc(glEnable,{GL_DEPTH_TEST})
    c_proc(glDepthFunc,{GL_LEQUAL})
    c_proc(glHint,{GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST})

    return TRUE

end function

constant GL_COLOR_BUFFER_BIT = #00004000, GL_DEPTH_BUFFER_BIT = #00000100,
GL_TRIANGLES = #0004,
         GL_QUADS = #0007
function DrawGLScene()

    c_proc(glClear,{or_bits(GL_COLOR_BUFFER_BIT,GL_DEPTH_BUFFER_BIT)})
    c_proc(glLoadIdentity,{})

    c_proc(glTranslatef,{1.5,0.0,-6.0})
    c_proc(glBegin, {GL_TRIANGLES})
    c_proc(glColor3f, {1.0,0.0,0.0})
    c_proc(glVertex3f, {0.0,1.0,0.0})
    c_proc(glColor3f, {0.0,1.0,0.0})
    c_proc(glVertex3f, {-1.0,-1.0,0.0})
    c_proc(glColor3f, {0.0,0.0,1.0})
    c_proc(glVertex3f, {1.0,-1.0,0.0})
    c_proc(glEnd, {})

    c_proc(glTranslatef,{3.0,0.0,0.0})
    c_proc(glColor3f, {0.5,0.5,1.0})
    c_proc(glBegin, {GL_QUADS})
    c_proc(glVertex3f, {-1.0,1.0,0.0})
    c_proc(glVertex3f, {1.0,1.0,0.0})
    c_proc(glVertex3f, {1.0,-1.0,0.0})
    c_proc(glVertex3f, {-1.0,-1.0,0.0})
    c_proc(glEnd, {})

    return TRUE

end function

integer ClassName
procedure KillGLWindow()

    if fullscreen then
        if not c_func(ChangeDisplaySettingsA,{NULL,0}) then end if
        if not c_func(ShowCursor,{TRUE}) then end if
    end if

    if hRC then
        if not c_func(wglMakeCurrent,{NULL,NULL}) then
if message_box("Release of DC and RC failed","Shutdown
            Error",or_bits(MB_OK,MB_ICONINFORMATION)) then end if
        end if

        if not c_func(wglDeleteContext,{hRC}) then
if message_box("Release of Rendering Context failed","Shutdown
            Error",or_bits(MB_OK,MB_ICONINFORMATION)) then end if
        end if

        hRC = NULL
    end if

    if hDC and (not c_func(ReleaseDC,{hWnd,hDC})) then
if message_box("Release of Device Context failed","Shutdown
        Error",or_bits(MB_OK,MB_ICONINFORMATION)) then end if
        hDC = NULL
    end if

    if hWnd and (not c_func(DestroyWindow,{hWnd})) then
if message_box("Release window failed!","Shutdown Error",or_bits(MB_OK,
        MB_ICONINFORMATION)) then end if
        hWnd = NULL
    end if

    if c_func(UnregisterClassA,{ClassName,hInstance}) then
if message_box("Could not unregister class","Shutdown
        Error",or_bits(MB_OK,MB_ICONINFORMATION)) then end if
        hInstance = NULL
    end if

end procedure

constant WM_ACTIVATE = #0006, WM_SYSCOMMAND = #0112, SC_SCREENSAVE = #F140,
SC_MONITORPOWER = #F170, WM_CLOSE = #0010,
         WM_KEYDOWN = #0100, WM_KEYUP = #0101, WM_SIZE = #0005
function WndProc(atom hWnd, atom uMsg, atom wParam, atom lParam)

    if uMsg = WM_ACTIVATE then
        if not floor(wParam/#10000) then
            active = TRUE
        else
            active = FALSE
        end if
        return 0
    end if

    if  uMsg = WM_SYSCOMMAND then
        if wParam = SC_SCREENSAVE then end if
        if wParam = SC_MONITORPOWER then end if
        return 0
    end if

    if uMsg = WM_CLOSE then
        c_proc(PostQuitMessage,{0})
        return 0
    end if

    if uMsg = WM_KEYDOWN then
        keys[wParam] = TRUE
        return 0
    end if

    if uMsg = WM_KEYUP then
        keys[wParam] = FALSE
        return 0
    end if

    if uMsg = WM_SIZE then
        ReSizeGLScene(and_bits(lParam,#FFFF),floor(lParam/#10000))
        return 0
    end if

    return c_func(DefWindowProcA,{hWnd, uMsg, wParam, lParam})

end function

constant CS_HREDRAW = #0002, CS_VREDRAW = #0001, CS_OWNDC = #0020, IDI_WINLOGO =
32517, IDC_ARROW = 32512
function ClassRegistration()
    integer WndProcAddress, id, wc

    id = routine_id("WndProc")
    if id = -1 then
    puts(1, "routine_id failed!\n")
    abort(1)
    end if
    wc = allocate(40)
    WndProcAddress = call_back(id)
    hInstance = c_func(GetModuleHandleA,{NULL})
    ClassName = allocate_string("OpenGL")
    poke4(wc,or_all({CS_HREDRAW, CS_VREDRAW, CS_OWNDC}))
    poke4(wc+4,WndProcAddress)
    poke4(wc+8,0)
    poke4(wc+12,0)
    poke4(wc+16,hInstance)
    poke4(wc+20,c_func(LoadIconA,{NULL,IDI_WINLOGO}))
    poke4(wc+24,c_func(LoadCursorA,{NULL, IDC_ARROW}))
    poke4(wc+28,NULL)
    poke4(wc+32,NULL)
    poke4(wc+36,ClassName)

    if not c_func(RegisterClassA,{wc}) then
if message_box("Failed to register class","Error",
        or_bits(MB_OK,MB_ICONINFORMATION)) then end if
        return FALSE
    else
        return TRUE
    end if

end function

constant DM_BITSPERPEL = #00040000, DM_PELSWIDTH = #00080000, DM_PELSHEIGHT =
#00100000,
CDS_FULLSCREEN = #00000004, DISP_CHANGE_SUCCESSFUL = 0, WS_EX_APPWINDOW
         = #00040000,
WS_POPUP = #80000000, WS_EX_WINDOWEDGE = #00000100, WS_CLIPSIBLINGS =
         #04000000,
WS_OVERLAPPED = #00000000,WS_CAPTION = #00C00000, WS_SYSMENU =
         #00080000, WS_THICKFRAME = #00040000,
WS_MINIMIZEBOX = #00020000, WS_MAXIMIZEBOX = #00010000, WS_CLIPCHILDREN
         = #02000000,
PFD_DRAW_TO_WINDOW = #00000004, PFD_SUPPORT_OPENGL = #00000020,
         PFD_DOUBLEBUFFER = #40000000,
         PFD_TYPE_RGBA = 0, SW_SHOW = 5,
WS_OVERLAPPEDWINDOW =
         or_all({WS_OVERLAPPED,WS_CAPTION,WS_SYSMENU,WS_THICKFRAME,WS_MINIMIZEBOX,WS_MAXIMIZEBOX})
function CreateGLWindow(atom title, integer width, integer height, integer bits,
integer fullscreenflag)
    atom PixelFormat, wc, dmScreenSettings, pfd, WindowRect, dwExStyle, dwStyle
sequence s
    fullscreen = fullscreenflag

    if ClassRegistration() then end if

    if fullscreen then
        dmScreenSettings = allocate(156)
        mem_set(dmScreenSettings,0,156)
        s = int_to_bytes(156)
        poke(dmScreenSettings + 36,{s[1],s[2]})
poke4(dmScreenSettings +
        40,or_all({DM_BITSPERPEL,DM_PELSWIDTH,DM_PELSHEIGHT}))
        poke4(dmScreenSettings + 104, bits)
        poke4(dmScreenSettings + 108, width)
        poke4(dmScreenSettings + 112, height)

if c_func(ChangeDisplaySettingsA,{dmScreenSettings,CDS_FULLSCREEN}) !=
        DISP_CHANGE_SUCCESSFUL then
if message_box("The requested fullscreen mode is not supported
            by\nyour video card. " &
"Use windowed mode instead?","Error",
                           or_bits(MB_YESNO,MB_ICONEXCLAMATION)) = IDYES then
                fullscreen = FALSE
            else
if message_box("Program will now
                close","Error",or_bits(MB_OK,MB_ICONSTOP)) then end if
                return FALSE
            end if
        end if
    end if

    if fullscreen then
        dwExStyle = WS_EX_APPWINDOW
        dwStyle = WS_POPUP
        if c_func(ShowCursor,{FALSE}) then end if
    else
        dwExStyle = or_bits(WS_EX_APPWINDOW,WS_EX_WINDOWEDGE)
        dwStyle = WS_OVERLAPPEDWINDOW
    end if

    WindowRect = allocate(16)
    poke4(WindowRect,0)
    poke4(WindowRect + 4,width)
    poke4(WindowRect + 8, 0)
    poke4(WindowRect + 12, height)

if c_func(AdjustWindowRectEx,{WindowRect, dwStyle, FALSE, dwExStyle}) then
    end if
    hWnd = c_func(CreateWindowExA,{dwExStyle,
                                   ClassName,
                                   title,
or_all({WS_CLIPSIBLINGS,WS_CLIPCHILDREN,dwStyle}),
                                   0,
                                   0,
                                   peek4u(WindowRect + 4) - peek4u(WindowRect),
peek4u(WindowRect + 12) - peek4u(WindowRect +
                                   8),
                                   NULL,
                                   NULL,
                                   hInstance,
                                   NULL})

    if hWnd = NULL then
        KillGLWindow()
if message_box("Window creation
        error","Error",or_bits(MB_OK,MB_ICONEXCLAMATION)) then end if
        while not wait_key() do end while
        return FALSE
    end if

    pfd = allocate(40)
    mem_set(pfd,0,40)
    poke(pfd, 40)
    poke(pfd + 2, 1)
poke4(pfd + 4,
    or_all({PFD_DRAW_TO_WINDOW,PFD_SUPPORT_OPENGL,PFD_DOUBLEBUFFER}))
poke(pfd + 8, {PFD_TYPE_RGBA,
    and_bits(bits,#FF),0,0,0,0,0,0,0,0,0,0,0,0,0,16,0,0,0,0})
    poke4(pfd + 25, {0,0,0})

    hDC = c_func(GetDC,{hWnd})
    if not hDC then
        KillGLWindow()
if message_box("Can't create a GL device
        context","Error",or_bits(MB_OK,MB_ICONEXCLAMATION)) then end if
        return FALSE
    end if

    PixelFormat = c_func(ChoosePixelFormat,{hDC,pfd})
    if not PixelFormat then
        KillGLWindow()
if message_box("Can't find a suitable pixel
        format","Error",or_bits(MB_OK,MB_ICONEXCLAMATION)) then end if
        return FALSE
    end if

    if not (c_func(SetPixelFormat,{hDC,PixelFormat,pfd})) then
        KillGLWindow()
if message_box("Can't set the pixel
        format","Error",or_bits(MB_OK,MB_ICONEXCLAMATION)) then end if
        return FALSE
    end if

    if not c_func(DescribePixelFormat, {hDC,PixelFormat,40,pfd}) then
if message_box("Can't describe the pixel
        format","Error",or_bits(MB_OK,MB_ICONEXCLAMATION)) then end if
    end if

    hRC = c_func(wglCreateContext,{hDC})
    if not hRC then
        KillGLWindow()
if message_box("Can't create a GL rendering
        context","Error",or_bits(MB_OK,MB_ICONEXCLAMATION)) then end if
        return FALSE
    end if

    if not (c_func(wglMakeCurrent,{hDC,hRC})) then
        KillGLWindow()
if message_box("Can't activate the GL rendering
        context","Error",or_bits(MB_OK,MB_ICONEXCLAMATION)) then end if
        return FALSE
    end if

    if c_func(ShowWindow,{hWnd,SW_SHOW}) then end if
    if c_func(SetForegroundWindow,{hWnd}) then end if
    if c_func(SetFocus,{hWnd}) then end if

    ReSizeGLScene(width, height)

    if not InitGL() then
        KillGLWindow()
if message_box("Initialization
        failed","Error",or_bits(MB_OK,MB_ICONEXCLAMATION)) then end if
        return FALSE
    end if

    return TRUE

end function

constant PM_REMOVE = #0001, WM_QUIT = #0012, VK_ESCAPE = #1B, VK_F1 = #70
procedure WinMain()
    integer MSG, done, title, msg_message
    sequence s

    MSG = allocate(28)
    done = FALSE
    title = allocate_string("OpenGL")

if message_box("Would you like to run in fullscreen mode?","Start
    Fullscreen?",or_bits(MB_YESNO,MB_ICONQUESTION)) = IDNO then
        fullscreen = FALSE
    else
        fullscreen = TRUE
    end if


    if not CreateGLWindow(title,640,480,16,fullscreen) then
        --return 0
    end if

    while not done do
        if c_func(PeekMessageA,{MSG,NULL,0,0,PM_REMOVE}) then
            s = peek({MSG+4,2})
            msg_message = bytes_to_int({s[1],s[2],0,0})
            printf(1,"msg.message = 0x%04x\n",msg_message)
            if msg_message = WM_QUIT then
                done = TRUE
                if not wait_key() then end if
            else
                if c_func(TranslateMessage,{MSG}) then end if
                if c_func(DispatchMessageA,{MSG}) then end if
            end if
        else
            if active then
                if keys[VK_ESCAPE] then
                    done = TRUE
                else
                    if DrawGLScene() then end if
                    if c_func(wglSwapBuffers,{hDC}) then end if
                end if

                if keys[VK_F1] then
                    keys[VK_F1] = FALSE
                    KillGLWindow()
                    if fullscreen = 0 then
                        fullscreen = 1
                    else
                        fullscreen = 0
                    end if

                    if not CreateGLWindow(title,640,480,16,fullscreen) then
                        --return 0
                    end if
                end if
            end if
        end if
    end while

    KillGLWindow()
    --return peek4u(MSG + 8)

end procedure

WinMain()
--------------030601000503000505010506--

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu