Windows screen Saver OpenGL template

Source: Internet
Author: User
Tags clear screen

Visual Studio 2013 Screen saver opengl templates

ScreenSaver.cpp

#define Vc_extralean#include <windows.h> #include <commctrl.h> #include <scrnsave.h> #include < time.h> #include <math.h> #include <string> #include <time.h> #include "resource.h" #include &LT;GL \glu.h> #include <gl\GL.h> #pragma comment (lib, "Opengl32.lib") #pragma comment (lib, "Glu32.lib") #pragma Comment (lib, "Scrnsavw.lib") #pragma comment (lib, "Comctl32.lib") bool Setupopengl (); void Killgl (); void Drawglscene (); HDCHDC = NULL; HGLRCHRC = NULL; Hwndhwnd = NULL; Hinstancehinstance;int Utimer; LONG WINAPI Screensaverproc (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM LPARAM) {//Handle screen Saver message switch (message) {case Wm_cre ATE://Create a screen saver form message First Run HWND = hwnd; Setupopengl ();//Set Timer (interval is 16 milliseconds, in order to refresh 60 times per minute (1000/60 = +) Utimer = SetTimer (hwnd, 1, +, NULL); return 0;case WM_ERASEBKGND: Clear screen protection background//OpenGL does not use return 0;case wm_timer://timer message drawglscene (); return 0;case wm_destroy://Destroy KillTimer (hwnd, Utimer); KILLGL (); PostQuitMessage (0); return 0;} Return Defscreensaverproc (hwnd, message, wparam, lparam);} BOOL WINAPI Screensaverconfiguredialog (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM LPARAM) {return true;} BOOL WINAPI registerdialogclasses (HANDLE hmodule) {return true;} BOOL Setupopengl () {gluint pixelformat;int width;int height;width = GetDeviceCaps (GetDC (NULL), horzres); height = GetDeviceCaps (GetDC (NULL), vertres); Rectwindowrect; Windowrect.left = (long) 0; Windowrect.right = (long) width; Windowrect.top = (long) 0; Windowrect.bottom = (long) height;//set OpenGL context Staticpixelformatdescriptor PFD ={sizeof (pixelformatdescriptor), 1, Pfd_draw_to_window | Pfd_support_opengl | pfd_doublebuffer,pfd_type_rgba,24,0, 0, 0, 0, 0, 0,0,0,0,0, 0, 0, 0,16, 0,0,pfd_main_plane,0,0, 0, 0};if (!) ( HDC = GetDC (hWnd))) {KILLGL (); MessageBox (NULL, Text ("Can ' t Create A GL Device Context."), Text ("ERROR"), MB_OK | mb_iconexclamation); return FALSE;} if (! ( PixelFormat = Choosepixelformat (HDC, &AMP;PFD))) {KILLGL (); MessageBox (NULL, Text ("Can ' t Find A suitable PixelFormat."), Text ("ERROR"), MB_OK | MB_iconexclamation); return FALSE;} if (! Setpixelformat (HDC, PixelFormat, &AMP;PFD)) {KILLGL (); MessageBox (NULL, Text ("Can ' t Set the PixelFormat."), Text ("ERROR"), MB_OK | mb_iconexclamation); return FALSE;} if (! ( HRC = Wglcreatecontext (HDC))) {KILLGL (); MessageBox (NULL, Text ("Can ' t Create A GL Rendering Context."), Text ("ERROR"), MB_OK | mb_iconexclamation); return FALSE;} if (!wglmakecurrent (HDC, HRC)) {KILLGL (); MessageBox (NULL, Text ("Can ' t Activate the GL Rendering Context."), Text ("ERROR"), MB_OK | mb_iconexclamation); return FALSE;} Perspective Matrix if (height = = 0) {height = 1;} Glviewport (0, 0, width, height); Glmatrixmode (gl_projection); glloadidentity (); Gluperspective (45.0f, (glfloat) Width/ (glfloat) height, 0.1f, 100.0f); Glmatrixmode (Gl_modelview); glloadidentity ();//Initialize OpenGL some of the parameters Glshademodel (gl_smooth ); Glclearcolor (0.0f, 0.0f, 0.0f, 0.5f), Glcleardepth (1.0f), glenable (gl_depth_test); Gldepthfunc (gl_lequal); Glhint ( Gl_perspective_correction_hint, gl_nicest); return TRUE;} void Killgl (glvoid) {ChangedisplaysettiNGS (NULL, 0);//If the Switch back to the Desktopif (HRC) {//Does We have A Rendering Context? 

Wglmakecurrent (null, NULL);//Release the DC and RC Contextswgldeletecontext (HRC);//Delete the RCHRC = null;//Set RC to Null}releasedc (HWnd, HDC);//Release the Dc}void drawglscene () {Glclear (Gl_color_buffer_bit | Gl_depth_buffer_bit); glloadidentity (); Glclear (Gl_color_buffer_bit | Gl_depth_buffer_bit); glloadidentity (); Gltranslatef ( -1.5f, 0.0f, -6.0f); Glbegin (gl_triangles); glvertex3f (0.0f, 1.0f, 0.0f); glvertex3f ( -1.0f, -1.0f, 0.0f), glvertex3f (1.0f, -1.0f, 0.0f), Glend (); Gltranslatef (3.0f, 0.0f, 0.0f); Glbegin (Gl_quads), glvertex3f ( -1.0f, 1.0f, 0.0f), glvertex3f (1.0f, 1.0f, 0.0f), glvertex3f (1.0f, -1.0f, 0.0f); glvertex3f ( -1.0f, -1.0f, 0.0f); Glend (); Swapbuffers (HDC);}


Template code hosted in Https://github.com/tiancode/windows_opengl_screensaver_template


Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

Windows screen Saver OpenGL template

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.