Game Programming




 
game programming
Game Programming :: Programming :: General Programming :: Code Examples :: [OpenGL] Making Basic Window - View Topic
Topic Rating: *****
Printable View
Andy
Administrator
*****

Posts: 40
Status: Offline
Gender: Male
Joined:  
pm

Reputation: 0%

[OpenGL] Making Basic Window (11th Jan 08 at 2:13am UTC)
So as we know all games and program are contained inside a window you might think to your self it is easy to make a window
well it is not as easy as you think below i have posted the source
code that allows me to make a window and render images ect

i would set this at a mideuam level you must know how to use OOP

main.cpp
Code:
 
  1. #define WIN32_LEAN_AND_MEAN
  2. #define WIN32_EXTRA_LEAN
  3.  
  4. #include <windows.h>
  5. #include <gl/gl.h>
  6. #include <gl/glu.h>
  7.  
  8. #include "CGfxOpenGL.h"
  9.  
  10. bool exiting = false;
  11. long windowWidth = 800;
  12. long windowHeight = 600;
  13. long windowBits = 32;
  14. bool fullscreen = false;
  15. HDC hDC;
  16.  
  17. CGfxOpenGL *g_glRender = NULL;
  18.  
  19. void SetupPixelFormat(HDC hDC)
  20. {
  21. int pixelFormat;
  22.  
  23. PIXELFORMATDESCRIPTOR pfd =
  24. {
  25. sizeof(PIXELFORMATDESCRIPTOR), // size
  26. 1, // version
  27. PFD_SUPPORT_OPENGL | // OpenGL window
  28. PFD_DRAW_TO_WINDOW | // render to window
  29. PFD_DOUBLEBUFFER, // support double-buffering
  30. PFD_TYPE_RGBA, // color type
  31. 32, // prefered color depth
  32. 0, 0, 0, 0, 0, 0, // color bits (ignored)
  33. 0, // no alpha buffer
  34. 0, // alpha bits (ignored)
  35. 0, // no accumulation buffer
  36. 0, 0, 0, 0, // accum bits (ignored)
  37. 16, // depth buffer
  38. 0, // no stencil buffer
  39. 0, // no auxiliary buffers
  40. PFD_MAIN_PLANE, // main layer
  41. 0, // reserved
  42. 0, 0, 0, // no layer, visible, damage masks
  43. };
  44.  
  45. pixelFormat = ChoosePixelFormat(hDC, &pfd);
  46. SetPixelFormat(hDC, pixelFormat, &pfd);
  47. }
  48.  
  49. LRESULT CALLBACK MainWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  50. {
  51. static HDC hDC;
  52. static HGLRC hRC;
  53. int height, width;
  54.  
  55. // dispatch messages
  56. switch (uMsg)
  57. {
  58. case WM_CREATE: // window creation
  59. hDC = GetDC(hWnd);
  60. SetupPixelFormat(hDC);
  61. //SetupPalette();
  62. hRC = wglCreateContext(hDC);
  63. wglMakeCurrent(hDC, hRC);
  64. break;
  65.  
  66. case WM_DESTROY: // window destroy
  67. case WM_QUIT:
  68. case WM_CLOSE: // windows is closing
  69.  
  70. // deselect rendering context and delete it
  71. wglMakeCurrent(hDC, NULL);
  72. wglDeleteContext(hRC);
  73.  
  74. // send WM_QUIT to message queue
  75. PostQuitMessage(0);
  76. break;
  77.  
  78. case WM_SIZE:
  79. height = HIWORD(lParam); // retrieve width and height
  80. width = LOWORD(lParam);
  81.  
  82. g_glRender->SetupProjection(width, height);
  83.  
  84. break;
  85.  
  86. case WM_ACTIVATEAPP: // activate app
  87. break;
  88.  
  89. case WM_PAINT: // paint
  90. PAINTSTRUCT ps;
  91. BeginPaint(hWnd, &ps);
  92. EndPaint(hWnd, &ps);
  93. break;
  94.  
  95. case WM_LBUTTONDOWN: // left mouse button
  96. break;
  97.  
  98. case WM_RBUTTONDOWN: // right mouse button
  99. break;
  100.  
  101. case WM_MOUSEMOVE: // mouse movement
  102. break;
  103.  
  104. case WM_LBUTTONUP: // left button release
  105. break;
  106.  
  107. case WM_RBUTTONUP: // right button release
  108. break;
  109.  
  110. case WM_KEYUP:
  111. break;
  112.  
  113. case WM_KEYDOWN:
  114. int fwKeys;
  115. LPARAM keyData;
  116. fwKeys = (int)wParam; // virtual-key code
  117. keyData = lParam; // key data
  118.  
  119. switch(fwKeys)
  120. {
  121. case VK_ESCAPE:
  122. PostQuitMessage(0);
  123. break;
  124. default:
  125. break;
  126. }
  127.  
  128. break;
  129.  
  130. default:
  131. break;
  132. }
  133. return DefWindowProc(hWnd, uMsg, wParam, lParam);
  134. }
  135.  
  136. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
  137. {
  138. WNDCLASSEX windowClass; // window class
  139. HWND hwnd; // window handle
  140. MSG msg; // message
  141. DWORD dwExStyle; // Window Extended Style
  142. DWORD dwStyle; // Window Style
  143. RECT windowRect;
  144.  
  145. g_glRender = new CGfxOpenGL;
  146.  
  147. windowRect.left=(long)0; // Set Left Value To 0
  148. windowRect.right=(long)windowWidth; // Set Right Value To Requested Width
  149. windowRect.top=(long)0; // Set Top Value To 0
  150. windowRect.bottom=(long)windowHeight; // Set Bottom Value To Requested Height
  151.  
  152. // fill out the window class structure
  153. windowClass.cbSize = sizeof(WNDCLASSEX);
  154. windowClass.style = CS_HREDRAW | CS_VREDRAW;
  155. windowClass.lpfnWndProc = MainWindowProc;
  156. windowClass.cbClsExtra = 0;
  157. windowClass.cbWndExtra = 0;
  158. windowClass.hInstance = hInstance;
  159. windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); // default icon
  160. windowClass.hCursor = LoadCursor(NULL, IDC_CROSS); // load cross
  161. windowClass.hbrBackground = NULL; // don't need background
  162. windowClass.lpszMenuName = NULL; // no menu
  163. windowClass.lpszClassName = "GLClass";
  164. windowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO); // windows logo small icon
  165.  
  166. // register the windows class
  167. if (!RegisterClassEx(&windowClass))
  168. return 0;
  169.  
  170. if (fullscreen) // fullscreen?
  171. {
  172. DEVMODE dmScreenSettings; // device mode
  173. memset(&;dmScreenSettings,0,sizeof(dmScreenSettings));
  174. dmScreenSettings.dmSize = sizeof(dmScreenSettings);
  175. dmScreenSettings.dmPelsWidth = windowWidth; // screen width
  176. dmScreenSettings.dmPelsHeight = windowHeight; // screen height
  177. dmScreenSettings.dmBitsPerPel = windowBits; // bits per pixel
  178. dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
  179.  
  180. //
  181. if (ChangeDisplaySettings(&;dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
  182. {
  183. // setting display mode failed, switch to windowed
  184. MessageBox(NULL, "Display mode failed", NULL, MB_OK);
  185. fullscreen = FALSE;
  186. }
  187. }
  188.  
  189. if (fullscreen) // Are We Still In Fullscreen Mode?
  190. {
  191. dwExStyle=WS_EX_APPWINDOW; // Window Extended Style
  192. dwStyle=WS_POPUP; // Windows Style
  193. ShowCursor(FALSE); // Hide Mouse Pointer
  194. }
  195. else
  196. {
  197. dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; // Window Extended Style
  198. dwStyle=WS_OVERLAPPEDWINDOW; // Windows Style
  199. }
  200.  
  201. AdjustWindowRectEx(&windowRect, dwStyle, FALSE, dwExStyle); // Adjust Window To True Requested Size
  202.  
  203. // class registered, so now create our window
  204. hwnd = CreateWindowEx(NULL, // extended style
  205. "GLClass", // class name
  206. "TESTING APPLICATION", // app name
  207. dwStyle | WS_CLIPCHILDREN |
  208. WS_CLIPSIBLINGS,
  209. 0, 0, // x,y coordinate
  210. windowRect.right - windowRect.left,
  211. windowRect.bottom - windowRect.top, // width, height
  212. NULL, // handle to parent
  213. NULL, // handle to menu
  214. hInstance, // application instance
  215. NULL); // no extra params
  216.  
  217. hDC = GetDC(hwnd);
  218.  
  219. // check if window creation failed (hwnd would equal NULL)
  220. if (!hwnd)
  221. return 0;
  222.  
  223. ShowWindow(hwnd, SW_SHOW); // display the window
  224. UpdateWindow(hwnd); // update the window
  225.  
  226. g_glRender->Init();
  227.  
  228. while (!exiting)
  229. {
  230. g_glRender->Prepare(0.0f);
  231. g_glRender->Render();
  232. SwapBuffers(hDC);
  233.  
  234. while (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
  235. {
  236. if (!GetMessage (&msg, NULL, 0, 0))
  237. {
  238. exiting = true;
  239. break;
  240. }
  241.  
  242. TranslateMessage (&msg);
  243. DispatchMessage (&msg);
  244. }
  245. }
  246.  
  247. delete g_glRender;
  248.  
  249. if (fullscreen)
  250. {
  251. ChangeDisplaySettings(NULL,0); // If So Switch Back To The Desktop
  252. ShowCursor(TRUE); // Show Mouse Pointer
  253. }
  254.  
  255. return (int)msg.wParam;
  256. }
  257.  
 


CGfxOpenGL.cpp
Code:
 
  1. #ifdef _WINDOWS
  2. #include <windows.h>
  3. #endif
  4.  
  5. #include <gl/gl.h>
  6. #include <gl/glu.h>
  7. #include <math.h>
  8. #include "CGfxOpenGL.h"
  9.  
  10. // disable implicit float-double casting
  11. #pragma warning(disable:4305)
  12.  
  13. CGfxOpenGL::CGfxOpenGL()
  14. {
  15. }
  16.  
  17. CGfxOpenGL::~CGfxOpenGL()
  18. {
  19. }
  20.  
  21. bool CGfxOpenGL::Init()
  22. {
  23. // clear to black background
  24. glClearColor(0.0, 0.0, 0.0, 0.0);
  25.  
  26.  
  27.  
  28. return true;
  29. }
  30.  
  31. bool CGfxOpenGL::Shutdown()
  32. {
  33. return true;
  34. }
  35.  
  36. void CGfxOpenGL::SetupProjection(int width, int height)
  37. {
  38. if (height == 0) // don't want a divide by zero
  39. {
  40. height = 1;
  41. }
  42.  
  43. glViewport(0, 0, width, height); // reset the viewport to new dimensions
  44. glMatrixMode(GL_PROJECTION); // set projection matrix current matrix
  45. glLoadIdentity(); // reset projection matrix
  46.  
  47. // calculate aspect ratio of window
  48. gluPerspective(52.0f,(GLfloat)width/(GLfloat)height,1.0f,1000.0f);
  49.  
  50.  
  51. glMatrixMode(GL_MODELVIEW); // set modelview matrix
  52. glLoadIdentity(); // reset modelview matrix
  53.  
  54. m_windowWidth = width;
  55. m_windowHeight = height;
  56. }
  57.  
  58. void CGfxOpenGL::Prepare(float dt)
  59. {
  60.  
  61. }
  62.  
  63. void CGfxOpenGL::Render()
  64. {
  65. // clear screen and depth buffer
  66. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  67. glLoadIdentity();
  68. gluLookAt(0.0, 10.0, 0.1, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  69.  
  70.  
  71. }
  72.  
 


CGfxOpenGL.h
Code:
 
  1. #ifndef __GL_COMPONENT
  2. #define __GL_COMPONENT
  3.  
  4. #define PI 3.14159
  5. #define TWO_PI PI*2.0
  6. #define HALF_PI PI/2.0
  7.  
  8. class CGfxOpenGL
  9. {
  10. private:
  11. int m_windowWidth;
  12. int m_windowHeight;
  13.  
  14.  
  15.  
  16. public:
  17. CGfxOpenGL();
  18. virtual ~CGfxOpenGL();
  19.  
  20. bool Init();
  21. bool Shutdown();
  22.  
  23. void SetupProjection(int width, int height);
  24.  
  25. void Prepare(float dt);
  26. void Render();
  27. };
  28.  
  29. #endif
  30.  
  31.  
 

 Printable View

All times are GMT+0 :: The current time is 9:12pm
Page generated in 0.7239 seconds
Purchase Ad-Free Credits
This Forum is Powered By vForums (v2.4)
Create a Forum for Free | Find Forums