ARGS() { //int pdata; HDC hDC; HGLRC gc; THIS_CLASS(); //printf( "DOING USE\n" ); gc = (HGLRC)GET_INT_FIELD("renderContext"); //pdata = (int) GET_INT_FIELD("pData"); if( gc == 0 ) { printf( "gc = 0\n" ); return 0; } hDC = (HDC)GET_INT_FIELD("display"); //printf( "USE - hDC: %d\n", hDC); if( !hDC ) { printf( "use - hdc 0" ); return 0; } if( wglGetCurrentContext() == NULL ) { printf( "ERROR WITH CONTEXT" ); wglMakeCurrent( NULL, NULL ); wglDeleteContext( (HGLRC)GET_INT_FIELD("renderContext") ); SetDCPixelFormat(hDC); // Create palette if needed GetOpenGLPalette(hDC); gc = wglCreateContext( hDC ); wglMakeCurrent( hDC, gc ); SET_INT_FIELD("renderContext", (long)gc ); return 0; } if( !gc ) return 0; if( !wglMakeCurrent( hDC, gc ) ) { wglMakeCurrent( NULL, NULL ); printf( "Error in wglMakeCurrent: %d", GetLastError() ); return 0; } return 1; }
// Window procedure, handles all messages for this program LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { static HGLRC hRC; // Permenant Rendering context static HDC hDC; // Private GDI Device context switch (message) { // Window creation, setup for OpenGL case WM_CREATE: SetTimer(hWnd, 1002, Tools::refreshFreq, NULL); // Store the device context hDC = GetDC(hWnd); // Select the pixel format SetDCPixelFormat(hDC); // Create palette if needed hPalette = GetOpenGLPalette(hDC); // Create the rendering context and make it current hRC = wglCreateContext(hDC); wglMakeCurrent(hDC, hRC); SetupRC(); nazwa = "Moon.bmp"; const char* bitmapFileName; bitmapFileName =nazwa.c_str(); glGenTextures(1, &texture); // tworzy obiekt tekstury // 3aduje pierwszy obraz tekstury: bitmapData = LoadBitmapFile((char*)bitmapFileName, &bitmapInfoHeader); glBindTexture(GL_TEXTURE_2D, texture); // aktywuje obiekt tekstury glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); // tworzy obraz tekstury glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth, bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapData); //if (bitmapData) //free(bitmapData); /* GLuint texture; int width, height; unsigned char* image; // Load textures glGenTextures(1, &texture); image = LoadBitmapFile("Bitmapy//Moon.bmp",&bitmapInfoHeader); // select our current texture glBindTexture(GL_TEXTURE_2D, texture); // select modulate to mix texture with color for shading glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth, bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapData); */ // 3aduje drugi obraz tekstury: /* bitmapData = LoadBitmapFile("Bitmapy\\crate.bmp", &bitmapInfoHeader); glBindTexture(GL_TEXTURE_2D, texture[1]); // aktywuje obiekt tekstury glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); // tworzy obraz tekstury glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth, bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapData); if (bitmapData) free(bitmapData); */ // ustalenie sposobu mieszania tekstury z t3em //glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); break; // Window is being destroyed, cleanup case WM_DESTROY: // Deselect the current rendering context and delete it wglMakeCurrent(hDC, NULL); wglDeleteContext(hRC); // Delete the palette if it was created if (hPalette != NULL) DeleteObject(hPalette); // Tell the application to terminate after the window // is gone. PostQuitMessage(0); break; // Window is resized. case WM_SIZE: // Call our function which modifies the clipping // volume and viewport ChangeSize(LOWORD(lParam), HIWORD(lParam)); break; // The painting function. This message sent by Windows // whenever the screen needs updating. case WM_PAINT: { } case WM_TIMER: { // Call OpenGL drawing code position = world->getPosition(); v = world->getVelocity(); fuel = 1000 * world->getFuel(); velocity = v.y; velocityx = v.x / 2; velocityz = v.z / 2; height = position.y / 20; score = world->getHighscore(); if (score > highscore) { highscore = score; } RenderScene(); SwapBuffers(hDC); if (spisSet) { world->control[0] = true; } else { world->control[0] = false; } if (s) { world->control[3] = true; } else { world->control[3] = false; } if (a) { world->control[2] = true; } else { world->control[2] = false; } if (d) { world->control[1] = true; } else { world->control[1] = false; } if (w) { world->control[4] = true; } else { world->control[4] = false; } // Validate the newly painted client area ValidateRect(hWnd, NULL); } // Windows is telling the application that it may modify // the system palette. This message in essance asks the // application for a new palette. case WM_QUERYNEWPALETTE: // If the palette was created. if (hPalette) { int nRet; // Selects the palette into the current device context SelectPalette(hDC, hPalette, FALSE); // Map entries from the currently selected palette to // the system palette. The return value is the number // of palette entries modified. nRet = RealizePalette(hDC); // Repaint, forces remap of palette in current window InvalidateRect(hWnd, NULL, FALSE); return nRet; } break; // This window may set the palette, even though it is not the // currently active window. case WM_PALETTECHANGED: // Don't do anything if the palette does not exist, or if // this is the window that changed the palette. if ((hPalette != NULL) && ((HWND)wParam != hWnd)) { // Select the palette into the device context SelectPalette(hDC, hPalette, FALSE); // Map entries to system palette RealizePalette(hDC); // Remap the current colors to the newly realized palette UpdateColors(hDC); return 0; } break; // Key press, check for arrow keys to do cube rotation. case WM_KEYDOWN: { if (wParam == VK_UP) xRot -= 5.0f; if (wParam == VK_DOWN) xRot += 5.0f; if (wParam == VK_LEFT) yRot -= 5.0f; if (wParam == VK_RIGHT) yRot += 5.0f; if (wParam == VK_SPACE) spisSet = true; if (wParam == 0x41) a = true; if (wParam == 0x44) d = true; if (wParam == 0x57) w = true; if (wParam == 0x53) s = true; if (wParam == 0x52) world->tryRefuel(); xRot = GLfloat((const int)xRot % 360); yRot = GLfloat((const int)yRot % 360); InvalidateRect(hWnd, NULL, FALSE); } break; case WM_KEYUP: { if (wParam == VK_SPACE) spisSet = false; if (wParam == 0x41) a = false; if (wParam == 0x44) d = false; if (wParam == 0x57) w = false; if (wParam == 0x53) s = false; } case WM_MOUSEMOVE: { int posX = GET_X_LPARAM(lParam); int posY = GET_Y_LPARAM(lParam); float xAngle = 0.2f; float yAngle = 0.2f; int angle = 1.0f; if (GetKeyState(VK_LBUTTON) & 0x80) { /* if (posX > mousePosX) world->changeCamera(0.0f, angle, 0.0f); if (posX < mousePosX) world->changeCamera(0.0f, -angle, 0.0f); if (posY > mousePosY) world->changeCamera(angle, 0.0f, 0.0f); if (posY < mousePosY) world->changeCamera(-angle, 0.0f, 0.0f);*/ world->changeCamera((posY - mousePosY)*yAngle, (posX - mousePosX)*xAngle, 0.0f); } mousePosX = posX; mousePosY = posY; } break; case WM_MOUSEWHEEL: { int x = GET_Y_LPARAM(wParam); if (x > 0) world->changeCamera(0.0f, 0.0f, -20.0f); if (x < 0) world->changeCamera(0.0f, 0.0f, 20.0f); } break; // A menu command case WM_COMMAND: { switch (LOWORD(wParam)) { // Exit the program case ID_FILE_EXIT: DestroyWindow(hWnd); break; // Display the about box case ID_HELP_ABOUT: DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG_ABOUT), hWnd, (DLGPROC)AboutDlgProc); break; } } break; default: // Passes it on if unproccessed return (DefWindowProc(hWnd, message, wParam, lParam)); } return (0L); }
// Window procedure, handles all messages for this program LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { static HGLRC hRC; // Permenant Rendering context static HDC hDC; // Private GDI Device context switch (message) { // Window creation, setup for OpenGL case WM_CREATE: // Store the device context hDC = GetDC(hWnd); // Select the pixel format SetDCPixelFormat(hDC); // Create the rendering context and make it current hRC = wglCreateContext(hDC); wglMakeCurrent(hDC, hRC); // Create the palette hPalette = GetOpenGLPalette(hDC); SetupRC(hDC); break; // Window is being destroyed, cleanup case WM_DESTROY: // Kill the timer that we created KillTimer(hWnd,101); glDeleteLists(nFontList, 128); // Deselect the current rendering context and delete it wglMakeCurrent(hDC,NULL); wglDeleteContext(hRC); // Delete the palette if(hPalette != NULL) DeleteObject(hPalette); // Tell the application to terminate after the window // is gone. PostQuitMessage(0); break; // Window is resized. case WM_SIZE: // Call our function which modifies the clipping // volume and viewport ChangeSize(LOWORD(lParam), HIWORD(lParam)); break; // The painting function. This message sent by Windows // whenever the screen needs updating. case WM_PAINT: { // Call OpenGL drawing code RenderScene(); // Call function to swap the buffers SwapBuffers(hDC); ValidateRect(hWnd,NULL); } break; // Windows is telling the application that it may modify // the system palette. This message in essance asks the // application for a new palette. case WM_QUERYNEWPALETTE: // If the palette was created. if(hPalette) { int nRet; // Selects the palette into the current device context SelectPalette(hDC, hPalette, FALSE); // Map entries from the currently selected palette to // the system palette. The return value is the number // of palette entries modified. nRet = RealizePalette(hDC); // Repaint, forces remap of palette in current window InvalidateRect(hWnd,NULL,FALSE); return nRet; } break; // This window may set the palette, even though it is not the // currently active window. case WM_PALETTECHANGED: // Don't do anything if the palette does not exist, or if // this is the window that changed the palette. if((hPalette != NULL) && ((HWND)wParam != hWnd)) { // Select the palette into the device context SelectPalette(hDC,hPalette,FALSE); // Map entries to system palette RealizePalette(hDC); // Remap the current colors to the newly realized palette UpdateColors(hDC); return 0; } break; default: // Passes it on if unproccessed return (DefWindowProc(hWnd, message, wParam, lParam)); } return (0L); }
HGLRC LIBAPIENTRY get_GC( HDC * hDC, GLCapabilities *glCaps, HGLRC shareWith, int offScreenRenderer, int width, int height, HBITMAP *pix, int verbose) { const char * text=0; HDC hDCOrig = 0; // Color Palette handle HPALETTE hPalette = NULL; HGLRC tempRC=0; if( *hDC == 0 && !offScreenRenderer) printf( "get_GC: Error, HDC is zero\n"); // Select the pixel format if(offScreenRenderer) { hDCOrig = *hDC; *hDC = CreateCompatibleDC(hDCOrig); // setupDIB(*hDC, pix, width, height); setupDIB(hDCOrig, *hDC, pix, width, height); /* SetDCPixelFormat(hDCOffScr, doubleBuffer, stereo, stencilBits, offScreenRenderer); */ /* setupPalette(hDC); USE MY PROC */ } SetDCPixelFormat(*hDC, glCaps, offScreenRenderer, verbose); // Create palette if needed hPalette = GetOpenGLPalette(*hDC); tempRC = disp__wglCreateContext( *hDC ); if(verbose) { fprintf(stderr,"\n\nPIXELFORMAT OF GL-Context SETTINGS:\n"); text=GetTextualPixelFormatByHDC(*hDC); fprintf(stderr,text); } /* check if the context could be created */ if( tempRC == NULL ) { fprintf(stderr, "getGC context could NOT be created \n"); return( 0 ); } /* associated the context with the X window */ if( disp__wglMakeCurrent( *hDC, tempRC ) == FALSE) { fprintf(stderr,"wglMakeCurrent(%p,%p) failed on new context!!!\n", *hDC,tempRC); fprintf(stderr,"Error code = %d\n",(int)GetLastError()); disp__wglMakeCurrent(NULL, NULL); disp__wglDeleteContext( tempRC ); return( 0 ); } if(shareWith!=NULL && disp__wglShareLists(shareWith, tempRC)==FALSE) { fprintf(stderr,"\nERROR: Could not share lists between the new and the given GLContext (Win32Native)!\n"); fprintf(stderr,"Error code = %d\n",(int)GetLastError()); disp__wglMakeCurrent(NULL, NULL); disp__wglDeleteContext( tempRC ); return( 0 ); } if(verbose) printf( "HGLRC (glContext) created: %p\n", tempRC ); return tempRC; }