See what's going on with flipcode!




This section of the archives stores flipcode's complete Developer Toolbox collection, featuring a variety of mini-articles and source code contributions from our readers.

 

  Small Footprint OpenGL Init Code
  Submitted by



I have written some init code for OpenGL in C, which only takes up 1.5kb of uncompressed code. The code works somewhat like GLUT where you specify an InitIntro() function that takes care of your initializing of whatever data you might need and later it loops a RunIntro() function in which you may apply your own effect code. You may gain additional space by translating the above code into assembly but that would also ruin the readability alot (I have an MASM32 version for my own use).

Download Associated File: minigl.c (5,072 bytes)

/************************************************************************************************
 * Small footprint OpenGL init code made by Rasmus Christian Kaae (kaae@daimi.au.dk)            *
 * Made in January 2002 - Please credit me if you use this!                                     *
 *                                                                                              *
 * Usage:                                                                                       *
 *        Make a main.c file containing two functions :                                         *
 *                                                                                              *
 *        void InitIntro()                                                                      *
 *        { /* todo: init stuff */ }                                                            *
 *        void RunIntro()                                                                       *
 *        {                                                                                     *
 *             glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);                              *
 *             /* todo: display something nice */                                               *
 *        }                                                                                     *
 *                                                                                              *
 *        Compile your DEBUG project as normal and set your RELEASE version to no-defaultlibs.  *
 *        This OpenGL init code will leave a small footprint of about 1.5kb uncompressed code.  *                            
 *        Tested with MS Visual Studio 6.0.                                                     *
 ************************************************************************************************/

#define WIN32_LEAN_AND_MEAN #include <windows.h> #include <gl/gl.h> #include <gl/glu.h>

/************************************************************************************************/

// out-comment this to get windowed rendering #define FULLSCREEN

/************************************************************************************************/

// implement these in your main.c file extern void InitIntro(); extern void RunIntro();

/************************************************************************************************/

#ifdef _DEBUG int WINAPI WinMain(HINSTANCE i, HINSTANCE p, LPSTR cmd, int n) #else int WinMainCRTStartup() #endif { PIXELFORMATDESCRIPTOR pfd; HWND hwnd; HDC hdc; int pixelFormat; #ifdef FULLSCREEN DEVMODE dmScreenSettings; // Device Mode #endif #ifndef _DEBUG HINSTANCE i = GetModuleHandle(NULL); #endif

/* init the pixelformat descriptor */ RtlZeroMemory(&pfd,sizeof(PIXELFORMATDESCRIPTOR)); // Clear the struct pfd.nSize=sizeof(PIXELFORMATDESCRIPTOR); // Size pfd.nVersion=1; // Version pfd.dwFlags=PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER; // Selected flags pfd.iPixelType=PFD_TYPE_RGBA; // Pixelformat pfd.cColorBits=32; // Pixel depth pfd.cDepthBits=16; // Zbuffer depth pfd.iLayerType=PFD_MAIN_PLANE; // Place the pixelformat on the main plane

#ifdef FULLSCREEN /* init the resolution change settings */ RtlZeroMemory(&dmScreenSettings,sizeof(dmScreenSettings)); // Clear the struct dmScreenSettings.dmSize=sizeof(dmScreenSettings); // Size Of The Devmode Structure dmScreenSettings.dmPelsWidth = 640; // Selected Screen Width dmScreenSettings.dmPelsHeight = 480; // Selected Screen Height dmScreenSettings.dmBitsPerPel = 32; // Selected Bits Per Pixel dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT; // Notify which fields we have used if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL) return 0; #endif

/* setup the window handle */ hwnd=CreateWindowEx(WS_EX_TOOLWINDOW, "STATIC", "blipblop", WS_POPUP|WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN, 0, 0, 640, 480, 0, 0, i, 0);

/* select the pixel format for the current window */ hdc = GetDC(hwnd); pixelFormat = ChoosePixelFormat(hdc, &pfd); SetPixelFormat(hdc, pixelFormat, &pfd); wglMakeCurrent( hdc, wglCreateContex(hdc) );

/* cursor be gone */ SetCursor(0);

/* init the actual intro */ InitIntro();

/* loop until escape is pressed all normal windows messages are ignored */ while (!GetAsyncKeyState(VK_ESCAPE)) { RunIntro(); // nifty effects here SwapBuffers(hdc); // display the cool stuff } return 0; // exit to OS }

The zip file viewer built into the Developer Toolbox made use of the zlib library, as well as the zlibdll source additions.

 

Copyright 1999-2008 (C) FLIPCODE.COM and/or the original content author(s). All rights reserved.
Please read our Terms, Conditions, and Privacy information.