Author Topic: Drawing directly to a console  (Read 1705 times)

0 Members and 1 Guest are viewing this topic.

Offline rain_storm

  • Here comes the Rain
  • DBF Aficionado
  • ******
  • Posts: 3088
  • Karma: 182
  • Rain never hurt nobody
    • View Profile
    • org_100h
Drawing directly to a console
« on: October 20, 2011 »
Here's a neat trick I found that allows you to draw directly to a console window (Only tested under XP). This gives you access to a window using only kernel32 and gdi32 no need to link to user32 at all. The trick is to set the console title to a unique string and then get the HWND to the console using FindWindow. I'm interested in seeing if this works on windows7 because win7 requires you to pump the message queue which adds extra function calls.


Code: [Select]
#pragma comment(lib, "kernel32.lib")
#pragma comment(lib, "gdi32.lib")
#undef UNICODE
#undef _UNICODE
#include <windows.h>

const int resx = 256;
const int resy = 256;
int   pixel[resx*resy];
int   t;
HWND  hwnd;
HDC   hdc;
RECT  rc;

void mainCRTStartup(void)
{
static BITMAPINFO bmi = { sizeof(BITMAPINFOHEADER),0,0,1,32 };
SetConsoleTitle("io");
hwnd = FindWindow(NULL, "io");
if (!hwnd) return;
hdc = GetDC(hwnd);
if (!hdc) return;
static PIXELFORMATDESCRIPTOR pfd = { sizeof(PIXELFORMATDESCRIPTOR),
1,PFD_DRAW_TO_WINDOW|PFD_SUPPORT_GDI|PFD_DOUBLEBUFFER,PFD_TYPE_RGBA,
32,0,0,0,0,0,0,0,0,0,0,0,0,0,16,0,0,PFD_MAIN_PLANE,0,0,0,0 };
static int fmt = ChoosePixelFormat(hdc,&pfd);
if (!fmt) return;
if (!SetPixelFormat(hdc,fmt,&pfd)) return;
while (!GetAsyncKeyState(VK_ESCAPE)) {
for (int y = 0; y < resy; y++) {
for (int x = 0; x < resx; x++) {
pixel[x+y*resx] = (x^y)+t;
}
}
GetClientRect(hwnd, &rc);
bmi.bmiHeader.biWidth  = resx;
bmi.bmiHeader.biHeight = resy;
StretchDIBits(hdc,rc.right/2-resx/2,rc.bottom/2-resy/2,resx,resy,
0,0,resx,resy,pixel,&bmi,0,SRCCOPY);
SwapBuffers(hdc);
t++;
}
ReleaseDC(hwnd,hdc);
DestroyWindow(hwnd);
ExitProcess(0);
return;
}

-edit my bad FindWindow requires user32 thought NODEFAULTLIBS was suppose to exclude it apparently it doesn't
« Last Edit: October 20, 2011 by rain_storm »

Challenge Trophies Won:

Offline Raizor

  • Founder Member
  • Pentium
  • ********
  • Posts: 1150
  • Karma: 174
    • View Profile
    • Raizor's Dev Blog
Re: Drawing directly to a console
« Reply #1 on: October 20, 2011 »
I've tested it here on win7 x64. It fails 9 times out of 10 and I just get a quick flash of the console window before it exits (no drawing). When it does work, I see blue square patterns drawn in the console window.

Not sure why it's failing so often, maybe a timing issue with grabbing the handle before the it's set up??
raizor

Challenge Trophies Won:

Offline rain_storm

  • Here comes the Rain
  • DBF Aficionado
  • ******
  • Posts: 3088
  • Karma: 182
  • Rain never hurt nobody
    • View Profile
    • org_100h
Re: Drawing directly to a console
« Reply #2 on: October 20, 2011 »
Very strange. Also this doesn't seem to work when I try it in FASM. I wonder why that is

Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17378
  • Karma: 497
  • evil/good
    • View Profile
    • My Homepage
Re: Drawing directly to a console
« Reply #3 on: October 23, 2011 »
I get almost the same result as Raizor, it works about 1 time out of every 4 times it runs, it's really col when it works though!
Shockwave ^ Codigos
Challenge Trophies Won: