• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
AbelCorver

WGL: High Memory Usage and Slow Startup

0 posts in this topic

Hey guys, I just started writing my very first OpenGL app. I need to use WGL, so that I have full control over window creation. I managed to implement this functionality, so that I have a borderless window and everything seems to work fine when no drawing takes place. When I don't draw anything (and don't swap buffers) the app uses around 7 MB RAM. I guess that's normal, since a very minimal GLUT app uses that amount of memory too. However, as soon as I start drawing things, the memory usages rises to almost 25 - 30 MB! I don't think a simple blue rectangle takes that much space. I'm probably doing something wrong here... :D The 'CWindow.h' Header File:
#ifndef __CWINDOW_H__
#define __CWINDOW_H__

#include <Windows.h>
#include "IWindow.h"

#pragma comment(lib,"opengl32.lib")
#pragma comment(lib,"glu32.lib")

// =============================================================
// Name: CWindow
// Desc: Implements a window on Microsoft Windows (r).
// =============================================================

class CWindow : public IWindow {
public:
	// ---------------------------------------------------------
	// Public Methods
	// ---------------------------------------------------------

	// constructor / destructor
	CWindow( HINSTANCE hAppInstance );
	~CWindow();
	
	// ---------------------------------------------------------
	// Public Methods Inherited from IWindow
	// ---------------------------------------------------------
	
	bool InitWindow( unsigned int Width , unsigned int Height );
	bool SetAsRenderTarget();

	HDC GetGLDC(){ return m_hRenderTargetDC; }

private:
	// ---------------------------------------------------------
	// Private Member Variables
	// ---------------------------------------------------------

	// OpenGL / WGL variables (shared by all windows)
	static HWND	m_hRenderTargetWnd;
	static HDC m_hRenderTargetDC;
	static HGLRC m_hRenderTargetRC;

	// the handle to the window
	HWND m_hWnd;

	// the handle to the application instance
	HINSTANCE m_hAppInstance;
	
	// ---------------------------------------------------------
	// Private Methods
	// ---------------------------------------------------------
	
	// release this window as a render target ( e.g. release DC and GLRC )
	static bool ReleaseAsRenderTarget();

	// window procedures
	static LRESULT CALLBACK StaticWindowProcedure( HWND hWnd , UINT Msg , WPARAM wParam , LPARAM lParam );
	LRESULT CALLBACK WindowProcedure( HWND hWnd , UINT Msg , WPARAM wParam , LPARAM lParam );
};

#endif


The 'CWindow.cpp' Source File:
#include "CWindow.h"

#ifndef __CWINDOW_DEFSTATICS__
#define __CWINDOW_DEFSTATICS__

// define static variables
HWND	CWindow::m_hRenderTargetWnd	= NULL ;
HDC		CWindow::m_hRenderTargetDC	= NULL ;
HGLRC	CWindow::m_hRenderTargetRC	= NULL ;

#endif

// ---------------------------------------------------------
// Constructor and Destructor
// ---------------------------------------------------------

CWindow::CWindow( HINSTANCE hAppInstance )
{
	// save application instance handle
	m_hAppInstance = hAppInstance;

	// set other variables to null
	m_hWnd = NULL;
}

CWindow::~CWindow()
{
	// release as render target
	ReleaseAsRenderTarget();

	// clean-up
	if( m_hWnd != NULL ) DestroyWindow( m_hWnd );
	m_hWnd = NULL;

	// unregister window class
	UnregisterClass( "CWindowWin32" , m_hAppInstance );
}

bool CWindow::ReleaseAsRenderTarget() 
{
	bool bError = false;

	// Do we have a rendering context for OpenGL
	if( m_hRenderTargetRC) {

		// 'unassociate' DC with OpenGL RC
		bError |= !wglMakeCurrent( NULL , NULL ); 

		// delete render context
		bError |= !wglDeleteContext( m_hRenderTargetRC);
		m_hRenderTargetRC= NULL;
	}

	// Has device context?
	if( m_hRenderTargetDC ) {

		// delete DC
		bError |= !ReleaseDC( m_hRenderTargetWnd , m_hRenderTargetDC );
		m_hRenderTargetDC = NULL;
	}

	// Finish !!
	return !bError;
}

// ---------------------------------------------------------
// Name: CreateWindow (Public)
// Desc: Create a window.
// ---------------------------------------------------------

bool CWindow::InitWindow( unsigned int Width , unsigned int Height ) 
{
	// create window class
	WNDCLASS wc = {0};
	
	// save appplication instance handle
	wc.hInstance = m_hAppInstance;

	// icon & cursor information
	wc.hIcon	= LoadIcon	( m_hAppInstance , IDI_APPLICATION	);
	wc.hCursor	= LoadCursor( m_hAppInstance , IDC_ARROW		);

	// set background to black
	wc.hbrBackground = (HBRUSH) GetStockObject( BLACK_BRUSH );

	// set window style (just a window without ANY borders)
	DWORD WndStyle = WS_POPUP | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
	wc.style = CS_BYTEALIGNCLIENT | CS_HREDRAW | CS_VREDRAW /* | CS_OWNDC */ ;

	// set classname
	wc.lpszClassName = "CWindowWin32";

	// set window procedure
	wc.lpfnWndProc = StaticWindowProcedure;

	// register it!
	RegisterClass( &wc );

	// create the window
	if( m_hWnd != NULL ) DestroyWindow( m_hWnd );
	m_hWnd = CreateWindow( "CWindowWin32" , "" , WndStyle , 10 , 10 , Width , Height , 0 , 0 , m_hAppInstance , this ); 
	if( m_hWnd == NULL ) return false;

	// show the world!
	ShowWindow( m_hWnd , SW_SHOW );
	UpdateWindow( m_hWnd );

	// Success !!
	return true;
}

// ---------------------------------------------------------
// Name: SetAsRenderTarget
// Desc: Sets this window as the render target. Future calls 
//       to OpenGL will use this window as drawing canvas.
// ---------------------------------------------------------

bool CWindow::SetAsRenderTarget() 
{
	// declare static variables ( do the initialization process only once ) ...
	static PIXELFORMATDESCRIPTOR PixelFormat = {0};
	static int iPixelFmt = 0;

	// validate...
	// if( !m_bDrawable ) goto Failed;

	// release if necessary
	ReleaseAsRenderTarget();

	// save window handle
	m_hRenderTargetWnd = m_hWnd;

	// get window DC
	m_hRenderTargetDC = NULL;
	m_hRenderTargetDC = GetDC( m_hWnd );
	if( m_hRenderTargetDC == NULL ) goto Failed;

	// set desired pixel format
	if( PixelFormat.nSize == 0 ) {
		// set size of this structure
		PixelFormat.nSize = sizeof( PIXELFORMATDESCRIPTOR );
		
		// set version number
		PixelFormat.nVersion = 1;

		// set settings
		PixelFormat.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;

		// set pixel format
		PixelFormat.iPixelType = PFD_TYPE_RGBA;

		// set pixel depth
		PixelFormat.cColorBits = 32;

		// set Z-buffer depth
		PixelFormat.cDepthBits = 16;

		// set main drawing layer
		PixelFormat.iLayerType = PFD_MAIN_PLANE;
		
		// find pixel format
		iPixelFmt = ChoosePixelFormat( m_hRenderTargetDC , &PixelFormat );
	}

	// check pixel format
	if( !iPixelFmt ) goto Failed;

	// now that we HAVE the pixel format, SET it also
	if( !SetPixelFormat( m_hRenderTargetDC , iPixelFmt , &PixelFormat ) ) goto Failed;

	// create a rendering context
	m_hRenderTargetRC= NULL;
	m_hRenderTargetRC= wglCreateContext( m_hRenderTargetDC );
	if( m_hRenderTargetRC== NULL ) goto Failed;

	// select this context
	if( !wglMakeCurrent( m_hRenderTargetDC , m_hRenderTargetRC) ) goto Failed;

	// Success !!
	return true;

// Shit! How did we reach this point!!
Failed:
	// Clean up the mess ...
	ReleaseAsRenderTarget();

	// Failure ...
	return false;
}

// ---------------------------------------------------------
// Name: StaticWindowProcedure (Static)(Private)
// Desc: Gateway for ALL window messages for this window.
// ---------------------------------------------------------

LRESULT CALLBACK CWindow::StaticWindowProcedure( HWND hWnd , UINT Msg , WPARAM wParam , LPARAM lParam ) 
{
	if( Msg == WM_CREATE ) {
		// store pointer to window class in window structure
		if( lParam != 0 ) {
			// fetch pointer
			CWindow* pWnd = (CWindow*) lParam;
			// store...
			SetWindowLongPtr( hWnd , GWLP_USERDATA , (LONG_PTR) pWnd );
		}
	}

	// get pointer back...
	CWindow* pWnd = (CWindow*) GetWindowLongPtr( hWnd , GWLP_USERDATA );
	if( pWnd != 0 ) {
		// call local window procedure
		return pWnd->WindowProcedure( hWnd , Msg , wParam , lParam );
	}

	// do the standard stuff...
	return DefWindowProc( hWnd , Msg , wParam , lParam );
}

// ---------------------------------------------------------
// Name: WindowProcedure (Private)
// Desc: Handles messages for this window object.
// ---------------------------------------------------------

LRESULT CALLBACK CWindow::WindowProcedure( HWND hWnd , UINT Msg , WPARAM wParam , LPARAM lParam ) 
{
	// Handle Messages
	switch( Msg ) {

		// -------------------------------------------------
		// Handle Keys
		// -------------------------------------------------

		case WM_KEYDOWN:
			switch( LOWORD(wParam) ) {
				case VK_ESCAPE: PostQuitMessage(0);
			} return 0;
	}

	// do the standard stuff...
	return DefWindowProc( hWnd , Msg , wParam , lParam );
}


The 'Main.cpp' File (starts my app and uses the CWindow class):
#include "CWindow.h"
#include "gl/gl.h"
#include "gl/glu.h"

void InitOpenGL()
{	
	// set background color to black
	glClearColor( 0.0 , 0.0 , 0.0 , 1.0 );

	// initialize view
	glMatrixMode( GL_PROJECTION );
	glLoadIdentity();
	glOrtho( 0.0 , 1.0 , 1.0 , 0.0 , -1.0 , 1.0 );
}

void RenderScene( CWindow* pWnd ) 
{
	// clear buffer
	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

	// draw blue rectangle
	glColor3f( 0.0f , 0.0f , 1.0f );
	glBegin( GL_POLYGON );
		glVertex3f( 0.10f , 0.10f , 0.0f );
		glVertex3f( 0.60f , 0.10f , 0.0f );
		glVertex3f( 0.60f , 0.40f , 0.0f );
		glVertex3f( 0.10f , 0.40f , 0.0f );
	glEnd();

	// flush operations
	glFlush();

	// swap buffers
	SwapBuffers( pWnd->GetGLDC() );
}

int WINAPI WinMain( HINSTANCE hInst , 
				    HINSTANCE hPrevInst , 
					LPSTR lpCmdLine , 
					int showCmd )
{
	// create window
	CWindow* pWnd = new CWindow( hInst );
	pWnd->InitWindow( 800 , 700 );

	// init message variable
	MSG Msg; ZeroMemory( &Msg , sizeof( MSG ) );
			
	// Set Render Target			
	pWnd->SetAsRenderTarget();
	InitOpenGL();
	
	// enter game loop
	while( true ) {
		if( PeekMessage( &Msg , 0 , 0 , 0 , PM_REMOVE ) ) {
			if( Msg.message == WM_QUIT ) break;
			TranslateMessage( &Msg );
			DispatchMessage( &Msg );
		} else {
			// Render The Scene
			RenderScene( pWnd );
			// Sleep...
			Sleep(10);
		}
	}

	// Clean-up
	delete pWnd; 
	pWnd = 0;

	// Success !!
	return 0;
}


Thanks in advance! Regards, Abel.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0