winapi - GetLastError() returns ERROR_INVALID_HANDLE / 6 after calling SwapBuffers(HDC) -
whenever attempt call swapbuffers(), getlasterror() returns 6 / error_invalid_handle. while have attempted fix rewriting code in different ways, attempting find different origins of errors , looking @ doing wrong. but, haven't come single conclusion on inducing or can resolve this.
i have recognized when call opengl functions such gluseprogram() , glvertexattribpointer(), glgeterror() returns 1282 / gl_invalid_operation.
window::window(string title, int xpos, int ypos, int width, int height, string icon_path) { this->title = title; this->width = width; this->height = height; if (this->height == 0) this->height = 1; if (!create(xpos, ypos)) { cout << "window creation failure!" << endl; exit(exit_failure); } handle hicon = loadimage(0, icon_path.c_str(), image_icon, 0, 0, lr_defaultsize | lr_loadfromfile); sendmessage(hwnd, wm_seticon, icon_big, (lparam) hicon); glclearcolor(0.0f, 0.0f, 0.0f, 1.0f); } window::~window() { wglmakecurrent(hdc, 0); wgldeletecontext(hrc); releasedc(hwnd, hdc); } bool window::create(int xpos, int ypos) { wndclass wndclass; dword dwexstyle = ws_ex_appwindow | ws_ex_windowedge; dword dwstyle = ws_overlapped | ws_caption | ws_sysmenu | ws_thickframe | ws_minimizebox; this->hinstance = getmodulehandle(nullptr); if (!this->hinstance) return false; wndclass.style = cs_hredraw | cs_vredraw | cs_owndc; wndclass.lpfnwndproc = (wndproc) wndproc; wndclass.cbclsextra = 0; wndclass.cbwndextra = 0; wndclass.hinstance = this->hinstance; wndclass.hicon = loadicon(nullptr, idi_winlogo); wndclass.hcursor = loadcursor(nullptr, idc_arrow); wndclass.hbrbackground = nullptr; wndclass.lpszmenuname = nullptr; wndclass.lpszclassname = this->title.c_str(); if (!registerclass(&wndclass)) return false; this->hwnd = createwindowex(dwexstyle, this->title.c_str(), this->title.c_str(), dwstyle, xpos, ypos, this->width, this->height, nullptr, nullptr, this->hinstance, nullptr); if (!this->hwnd) return false; if (!this->createcontext()) return false; showwindow(this->hwnd, sw_show); updatewindow(this->hwnd); return true; } bool window::createcontext() { this->hdc = getdc(hwnd); if (!this->hdc) return false; pixelformatdescriptor pfd; memset(&pfd, 0, sizeof(pixelformatdescriptor)); pfd.nsize = sizeof(pixelformatdescriptor); pfd.dwflags = pfd_doublebuffer | pfd_support_opengl | pfd_draw_to_window; pfd.ipixeltype = pfd_type_rgba; pfd.ccolorbits = 32; pfd.cdepthbits = 32; pfd.ilayertype = pfd_main_plane; int npixelformat = choosepixelformat(this->hdc, &pfd); if (!npixelformat) return false; bool bresult = setpixelformat(this->hdc, npixelformat, &pfd); if (!bresult) return false; hglrc tempopenglcontext = wglcreatecontext(this->hdc); wglmakecurrent(this->hdc, tempopenglcontext); glenum err = glewinit(); if (glew_ok != err) return false; int attributes[] = { wgl_context_major_version_arb, 3, wgl_context_minor_version_arb, 2, wgl_context_flags_arb, wgl_context_forward_compatible_bit_arb, 0 }; if (wglewissupported("wgl_arb_create_context") == true) { this->hrc = wglcreatecontextattribsarb(this->hdc, null, attributes); wglmakecurrent(nullptr, nullptr); wgldeletecontext(tempopenglcontext); wglmakecurrent(this->hdc, this->hrc); if (!this->hrc) return false; } else { this->hrc = tempopenglcontext; } int glversion[2] = {-1, -1}; glgetintegerv(gl_major_version, &glversion[0]); glgetintegerv(gl_minor_version, &glversion[1]); cout << "opengl running on context version : " << glversion[0] << ", " << glversion[1] << endl; return true; } int window::gameloop(core *core) { msg msg; while (core->isrunning()) { if (peekmessage(&msg, null, 0, 0, pm_remove)) { if (msg.message == wm_quit) { core->setrunning(false); } else { translatemessage(&msg); dispatchmessage(&msg); } } else { glclear(gl_color_buffer_bit | gl_depth_buffer_bit); core->run(); swapbuffers(this->hdc); } } core::destroy(); return (int)(msg.wparam); } code game state(where call gl functions):
#include "state.h" using namespace lumen::core_engine; glfloat data[9] = { -0.5f, -0.5f, 0.0f, 0.5f, -0.5f, 0.0f, 0.0f, 0.5f, 0.0f }; example::example() { this->colourshader = new shader("./shaders/vertex/basiccolour.vert", "./shaders/vertex/basiccolour.vert"); this->colourvertexarray = new vertexarray(new vertexbuffer(data, sizeof(data), gl_triangles, 3, sizeof(glfloat) * 3, this->colourshader)); cout << "'example' state initialized!" << endl; } example::~example() { } void example::update() { glenum errgl = glgeterror(); if (glew_no_error != errgl) cout << errgl << endl; dword errwin = getlasterror(); if (no_error != errwin) cout << errwin << endl; } void example::render() { gluseprogram((colourvertexarray->getvertexbufferobject()->getshader())->getprogram()); (colourvertexarray->getvertexbufferobject()->getshader())->setfloat4((colourvertexarray->getvertexbufferobject()->getshader())->getuniform("colour"), 1.0f, 0.0f, 0.0f, 1.0f); (colourvertexarray)->addvertexbufferobjecttarget((colourvertexarray->getvertexbufferobject()->getshader())->getattribute("position")); (colourvertexarray)->render(); }
you calling getlasterror irrespective of whether or not previous api call failed. should call getlasterror when documentation indicates should. typically when return value of function called indicates failure.
it possible api call succeed, , getlasterror return non-zero value. seems happening here.
Comments
Post a Comment