Advertisement Jump to content
Sign in to follow this  

grabbing the windows device context

This topic is 5015 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I don't know if this is the right place to do this but I need to find a way to grab the device context in a windows applications. The reason is I just started using SDL to create my opengl window instead of NEHE's way of registering the class. But I need the device context to create fonts that I will be able to use. So I need to grab the device constext but the only way that I can find is if you have your window handle. Here is how I set up opengl right now.
#include <cstdlib> // Atexit
#include <SDL.h> // main SDL header
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>

const int screen_width = 640;	// Screen properties
const int screen_height = 480;

// Function prototypes
bool InitVideo();
bool SetupGL(int width,int height);
void Render();	// Draw function

bool InitVideo()
	// Load SDL
	if(SDL_Init(SDL_INIT_VIDEO) < 0)
		// Failed
		MessageBox(NULL,"Failed to initialized video.","ERROR",0);
		return false;
	atexit(SDL_Quit);	// Run quit on exit
	// Now set up for opengl
	const SDL_VideoInfo *info = SDL_GetVideoInfo();
		// Should not happen
		MessageBox(NULL,"Video query failed.","ERROR",0);
		return false;
	int bpp = info->vfmt->BitsPerPixel;	// Get bpp
	// Set up bits
    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
  	// Set up color depth
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
	// Set up double buffering
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, true);
    // Now set it to let opengl do drawing
    if (SDL_SetVideoMode(screen_width, screen_height, bpp, SDL_OPENGL | SDL_SWSURFACE | SDL_FULLSCREEN) == 0) 
    	MessageBox(NULL,"Unable to set video mode.","ERROR",0);
	    return false;
  	SetupGL(screen_width,screen_height);	//Setup the opengl window
  	return true;

bool SetupGL(int width,int height)
	glViewport(0,0,(GLsizei)(screen_width),(GLsizei)(screen_height));	//Set up viewport
	glMatrixMode(GL_PROJECTION);	//Switch to projection to set up view
	glLoadIdentity();	//Reset it
	glMatrixMode(GL_MODELVIEW);	//switch to modelview
	glLoadIdentity();	//Reset it
	glShadeModel(GL_SMOOTH);			//Allow for smooth shading
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);						// Black Background									// Enable Depth Testing
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);			// Enable Alpha Blending (disable alpha testing)
	glEnable(GL_BLEND);											// Enable Blending       (disable alpha testing)									// Enable Texture Mapping
	return true;

int main(int argc, char *argv[])
	if (InitVideo() == false) { return 1; }
	int done = 0;	//Whether the loop is running
	while(done == 0)	//While done is false
		SDL_Event event;	//Used to handle event
		while(SDL_PollEvent(&event))	//Poll events till none left
			if(event.type == SDL_QUIT)  	//Quit message
				done = 1;  
			if ( event.type == SDL_KEYDOWN )
        		if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }
      		Render();	// Draw the screen
	return 0;

	return 0;

void Render()
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear the buffers
	SDL_GL_SwapBuffers(); // Swap the buffers

Is it possible to grab the device context to build my fonts? Thanks again, vbuser.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!