OpenGL in a class

Started by
4 comments, last by AmmoSponge 22 years, 6 months ago
Ive been trying for 4 months on and off to put an openGL renderer into a class for use with a win32 game. None of my implementations have worked, so I tried putting the windows class stuff in it too. Nothing seems to work, and I havent found anyone else in the gamedev forums or tutorials online about this topic. Does anyone have any ideas about implementation? Am I missing some major openGL limitation when combined with win32? I have my attempted source code available if anyone wants it.
Advertisement
ehm, isn''t there something wrong with your class code ... like a constructor you use everytime you try ...
why not post some simple class you tried to make in here, maybe someone will be able to tell you what you did wrong ...
Stefan
Actually I have started my own OGL class frame work maybe we can share ideas etc...
Heres all my source, for those tat are interested. It may not compile right because I started to modify it then gave up again.
For instance I started to make a client area struct in gamemain.
For what its worth...
  //winmain.h#ifndef WINMAIN_H#define WINMAIN_H#include "winmain.h"#include "gamemain.h"#endif//winmain.cpp#include <windows.h>#include "winmain.h"Gamemain Test01Game;LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd){	MSG msg;	Test01Game.Initialize(&hInstance, 640, 480, 16);	while(true)	{		if(PeekMessage( &msg, NULL, 0, 0, PM_REMOVE))		{			if(msg.message != WM_QUIT)			{				TranslateMessage( &msg);				DispatchMessage( &msg);			}			else			{				break;			}		}		else		{			Test01Game.Render();		}	}	return 1;}LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){	switch(uMsg)	{	case WM_CREATE:		return 0;	case WM_CLOSE:		PostQuitMessage(0);		return 0;	case WM_DESTROY:		return 0;	/*case WM_PAINT:		Test01Game.Render();		return 1;*/	case WM_KEYDOWN:		switch(wParam)		{		case VK_ESCAPE:			PostQuitMessage(0);			return 0;		}		return 0;	default:		return DefWindowProc(hWnd, uMsg, wParam, lParam);	}}//gamemain.h#ifndef GAMEMAIN_H#define GAMEMAIN_H#include <windows.h>#include <windowsx.h>#include <gl/gl.h>#include <gl/glu.h>struct WindowAttrib{	WNDCLASS Winclass;	DEVMODE ScreenSettings;	DWORD dwExStyle;	DWORD dwStyle;	RECT GameWindow;};class Gamemain{private:	HINSTANCE hGameInstance;	HWND hGameWnd;	HDC hGameDC;	HGLRC hGameRC;		int width;	int height;	int bpp;	PIXELFORMATDESCRIPTOR pfd;	int iFormat;		WindowAttrib ClientArea;public:	Gamemain();	~Gamemain();	void Initialize(HINSTANCE *hWinInstance, int wWidth, int wHeight, int wBpp);	void Render();	void Resize(int rWidth, int rHeight);};#endif//gamemain.cpp#include "gamemain.h"Gamemain::Gamemain(){	width = 640;	height = 480;	bpp = 16;	hGameInstance = NULL;	hGameWnd = NULL;	hGameDC = NULL;	hGameRC = NULL;}Gamemain::~Gamemain(){	wglDeleteContext(hGameRC);	hGameRC = NULL;	ReleaseDC(hGameWnd, hGameDC);	hGameDC = NULL;	wglMakeCurrent(NULL, NULL);}void Gamemain::Initialize(HINSTANCE *hWinInstance, int wWidth, int wHeight, int wBpp){	hGameInstance = *hWinInstance;	width = wWidth;	height = wHeight;	bpp = wBpp;		hGameWnd = CreateWindow( "GL", "Test01", 		WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,		0, 0, wWidth, wHeight,	NULL, NULL, hGameInstance, NULL );	hGameDC = GetDC(hGameWnd);		ClientArea.Winclass.style = CS_OWNDC;	ClientArea.Winclass.lpfnWndProc = WndProc;	ClientArea.Winclass.cbClsExtra = 0;	ClientArea.Winclass.cbWndExtra = 0;	ClientArea.Winclass.hInstance = hInstance;	ClientArea.Winclass.hIcon = LoadIcon( NULL, IDI_APPLICATION);	ClientArea.Winclass.hCursor = LoadCursor( NULL, IDC_ARROW);	ClientArea.Winclass.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH);	ClientArea.Winclass.lpszMenuName = NULL;	ClientArea.Winclass.lpszClassName = "GL";	RegisterClass( &ClientArea.Winclass);	// set the pixel format for the DC    ZeroMemory( &pfd, sizeof(pfd));    pfd.nSize = sizeof(pfd);    pfd.nVersion = 1;    pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;    pfd.iPixelType = PFD_TYPE_RGBA;    pfd.cColorBits = bpp;    pfd.cDepthBits = 16;    pfd.iLayerType = PFD_MAIN_PLANE;    iFormat = ChoosePixelFormat( hGameDC, &pfd);    SetPixelFormat( hGameDC, iFormat, &pfd);	hGameRC = wglCreateContext(hGameDC);	wglMakeCurrent(hGameDC, hGameRC);	glShadeModel(GL_SMOOTH);	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);	glClearDepth(1.0f);	glEnable(GL_DEPTH_TEST);	glDepthFunc(GL_LEQUAL);	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);}void Gamemain::Render(){	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	glLoadIdentity();	glTranslatef(0.0f, 0.0f, -6.0f);	glBegin(GL_TRIANGLES);	glVertex3f(-1.0f, -1.0f, -6.0f);	glVertex3f(0.0f, 1.0f, -6.0f);	glVertex3f(1.0f, -1.0f, -6.0f);	glEnd();	//SwapBuffers(hGameDC);	}void Gamemain::Resize(int rWidth, int rHeight){	if(rHeight == 0)		rHeight = 1;	glViewport(0, 0, rWidth, rHeight);	glMatrixMode(GL_PROJECTION);	glLoadIdentity();	gluPerspective(45.0f, rWidth/rHeight, 0.1f, 100.0f);	glMatrixMode(GL_MODELVIEW);	glLoadIdentity();}  


I''ve read your code quite quickly so I maybe wrong but your resize method seems to never be called and therefore your viewport is never set. You could try to add something like this in your WndProc function :

  switch(uMsg){case WM_SIZE:    Test01Game.Resize( LOWORD(lParam), HIWORD(lParam));    break;}   
I have done a tetris game with a rendering object and a seperate rendering thread. It will probably help you out with this. Nick it all ;o)

All my source and stuff is available on my website:

http://www.knownoffender.btinternet.co.uk

This topic is closed to new replies.

Advertisement