• 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

Does the Windows 7 interface (GDI?) utilize the framebuffer?

5 posts in this topic

Right now, I'm working on a method for quickly grabbing the average screen color and spewing it out to my arduino to control my room's RGB strips.



I've been using mostly device contexts like GetDC(NULL) and then using StretchBlt to "stretch" it down to one pixel. But that seems like a ludicrous number of calls in total. I see about 15 fps and 6% cpu usage. (When grabbing one pixel, it's probably 100+ fps with <1% usage.)





But, I've heard there's a way to directly access the GPU's framebuffer. In which case, if GDI uses the framebuffer, I could grab the frame and have the GPU do the averaging massively parallel via CUDA, then send the returned color directly to the Arduino.


But that would depend strictly on if the windows GUI uses the framebuffer. (And perhaps how much initialization it would take to access it.)



Share this post

Link to post
Share on other sites

It must occupy the GPU's frambuffer at some point, but I'd wager its not exposed in any way to you, aside from the GDI readback method you're already doing.


It'd probably be easier/better/faster to not down-sample literally the entire desktop to derive your RGB color. You could sample 1 pixel out of every 4x4 pixel square across the screen and cut your work to 1/16th of the StretchBlt method, and still come up with a color that's highly representative of your screen's average color. Certain degenerate cases could produce an undesirable effect with this scheme, but that could be solved by retaining a few frames worth of the RGB color and blending them together smoothly, just to avoid any potential harshness.


Share this post

Link to post
Share on other sites

"You could sample 1 pixel out of every 4x4 pixel square across the screen and cut your work to 1/16th of the StretchBlt method"


I figured that much. I was working on a method, actually, for creating a pixel density function so that the samples are more dense near the edges of the screen.


But I figured stuff like BitBlt and StretchBlt would grab a whole chunk of data while it's open, THEN extract what it needs.


I figure the only method I would really have for grabbing individual pixels is GetPixel(). But since it would re-access the device context each time, I figured that would be like shutting off the engine in your car every time you make a new turn.


I like your idea of keeping a few frames. It would also be interesting to dither the samples in a sense, like take a random pixel out of each 4x4 block, and keep the previous two or so frames.



Edit: And one more quick thing, if possible. How would I go about creating a buffer HDC? I tried creating one, and all I really got was a mess of digits that remained static. So as it is, I'm writing the average color to a pixel, and writing that pixel at 0,0 on my screen, then accessing the whole screen again just to grab that pixel.

Edited by Dimension10

Share this post

Link to post
Share on other sites
int main()
	// Establish Arduino as a color controller on COM7.

	cColorController *Arduino;				
	Arduino = new cColorController("COM7");
	Arduino->setAdjust(	1,		//Red Multiplier
				0.8,		//Green Multiplier
				0.4,		//Blue Multiplier
				2.0		//Gamma

	// Create a Device Context handle to grab some pixels.

	HDC hdcScreen = GetDC(NULL);
	COLORREF CurrentColor;
	SetBrushOrgEx(hdcScreen, 0, 0, NULL);
	int ScreenWPix = GetDeviceCaps(hdcScreen,HORZRES);
	int ScreenHPix = GetDeviceCaps(hdcScreen,VERTRES);

	// Create a Buffer device to send data to. Todo: Buffer not working.
	HDC hdcBuffer = CreateCompatibleDC(NULL);
	ZeroMemory( &hdcInfo.bmiHeader , sizeof(BITMAPINFOHEADER));
	hdcInfo.bmiHeader.biBitCount	= 24;
	hdcInfo.bmiHeader.biPlanes	=  1;
	hdcInfo.bmiHeader.biHeight	=  1;
	hdcInfo.bmiHeader.biWidth	=  1;
	hdcInfo.bmiHeader.biSizeImage   =  0;
	hdcInfo.bmiHeader.biSize	= sizeof(BITMAPINFOHEADER);
	hdcInfo.bmiHeader.biClrUsed	=  0;
	hdcInfo.bmiHeader.biClrImportant=  0;
	VOID *pvBits;
	HBITMAP bmpBuffer = CreateDIBSection(hdcBuffer,
	SelectObject(hdcBuffer, bmpBuffer);


		//Stretch the screen down to a single pixel.
		StretchBlt(hdcScreen, 0, 0, 1, 1,
			hdcScreen, 0, 0, ScreenWPix, ScreenHPix,
		//Get this pixel's color, rather than call GetPixel for r, g, and b.
		CurrentColor = GetPixel(hdcScreen, 0, 0);

		Arduino->setColor(GetRValue(CurrentColor), //Send this color to the Arduino
				GetBValue(CurrentColor)); //Todo: Have Arduino read COLORREF


	ReleaseDC(NULL, hdcScreen);
	return 1;

Share this post

Link to post
Share on other sites

The fastest possible way to get a working average screen colour is:


 - Create a render target texture with mipmap autogeneration do this once only).

 - Read the screen contents into the texture.

 - Let mipmaps autogenerate (you shouldn't have to do anything for this; it just happens).

 - Sample from the smallest (1x1) miplevel (copy this off to another texture if you need to, since you may not be able to read back from a mipmap-autogen texture).


All of this should run on the GPU, with the exception of the readback from the 1x1 miplevel to CPU-side, and is capable of running at several hundred FPS on reasonable hardware.  The readback will actually be your primary bottleneck even though it's just a single pixel, owing to the required CPU/GPU synchronization.


Share this post

Link to post
Share on other sites

That's a good idea, perfect for my usage. though I worry about initializing devices and windows. How much initialization is generally required before I can create a texture and sample it down to 1 pixel?


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