-
GDI BitBlt() - no output [ORIGINAL SOURCE CODE]
Hi. Below is a sample code that outputs a random graph unto a resizeable window. It will work find using a Win32 Project (VC++ 6.0). My goal is to add the BitBlt() function in order to reduce flicker (works like a double buffer that flips the graphics drawn at the back buffer memory to the front buffer). I know that it should be placed within WM_PAINT, but how and where? Can someone help me on this? Thanks.
#include <windows.h>
#include "time.h"
#include "stdio.h"
// TIMER VARIABLES
#define TIMER_ID1 1
#define TIMER_RATE1 500
// ENTRY VALUES
#define ENTRY_MAX 250
#define VALUE_MAX 750
#define VALUE_INTERVAL 15
#define VALUE_X 5
int n[ENTRY_MAX] = {0}; // INITIALIZED BEFORE MESSAGE LOOP
// WINDOW BOUNDARIES
#define MARGIN_LEFT 100
#define MARGIN_RIGHT 20
#define MARGIN_TOP 50
#define MARGIN_BOTTOM 50
// SET THE HORIZONTAL GAP
#define SPACE_X (rect.right-MARGIN_LEFT-MARGIN_RIGHT)/ENTRY_MAX
// GLOBAL RECTANGLE
RECT rect;
void Randomize();
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
HINSTANCE g_hInst;
LPSTR lpszClass="Graph";
// ------------------------------------------------------------------------------------------
// WINDOWS REGISTRY
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance
,LPSTR lpszCmdParam,int nCmdShow)
{
HWND hWnd;
MSG Message;
WNDCLASS WndClass;
g_hInst=hInstance;
WndClass.cbClsExtra=0;
WndClass.cbWndExtra=0;
WndClass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
WndClass.hCursor=LoadCursor(NULL,IDC_ARROW);
WndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
WndClass.hInstance=hInstance;
WndClass.lpfnWndProc=(WNDPROC)WndProc;
WndClass.lpszClassName=lpszClass;
WndClass.lpszMenuName=NULL;
WndClass.style=CS_HREDRAW | CS_VREDRAW;
RegisterClass(&WndClass);
hWnd=CreateWindow(lpszClass,lpszClass,WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
NULL,(HMENU)NULL,hInstance,NULL);
ShowWindow(hWnd,nCmdShow);
// RANDOMIZE TIMER
srand(time(NULL));
// SET RANDOM VALUES
Randomize();
// SET THE TIMER
SetTimer(hWnd, TIMER_ID1, TIMER_RATE1, NULL);
while(GetMessage(&Message,0,0,0)) {
TranslateMessage(&Message);
DispatchMessage(&Message);
}
return Message.wParam;
}
// ------------------------------------------------------------------------------------------
// MESSAGE LOOP
LRESULT CALLBACK WndProc(HWND hWnd,UINT iMessage,WPARAM wParam,LPARAM lParam)
{
HDC hdc2;
PAINTSTRUCT ps;
HPEN Pen, OldPen;
HBRUSH Brush, OldBrush;
HFONT Font, OldFont;
LOGFONT lf;
switch(iMessage)
{
case WM_PAINT: {
hdc2=BeginPaint(hWnd,&ps);
// SET VIEW PORT
SetMapMode(hdc2,MM_ANISOTROPIC);
GetClientRect(hWnd,&rect);
SetWindowExtEx(hdc2,rect.right,rect.bottom,NULL);
SetViewportExtEx(hdc2,rect.right,rect.bottom,NULL);
// SET THE BRUSH COLOR
Brush = CreateSolidBrush(RGB(255,255,255));
OldBrush = (HBRUSH)SelectObject(hdc2,Brush);
// CLEAR THE RECT WITH SELECTED BRUSH
Rectangle(hdc2, 0,0,rect.right,rect.bottom);
// SET FONT AND OUTPUT VALUES
lf.lfWidth = 15;
lf.lfHeight = 15;
lf.lfEscapement = 0;
lf.lfOrientation = 0;
lf.lfWeight = 0;
lf.lfItalic = 0;
lf.lfUnderline = 0;
lf.lfStrikeOut = 0;
lf.lfCharSet = BALTIC_CHARSET;
lf.lfOutPrecision = 0;
lf.lfClipPrecision = 0;
lf.lfQuality = 0;
lf.lfPitchAndFamily = 0;
Font = CreateFontIndirect(&lf);
OldFont = (HFONT)SelectObject(hdc2,Font);
Pen=CreatePen(PS_SOLID, 1, RGB(255,0,0));
OldPen=(HPEN)SelectObject(hdc2, Pen);
char str[10];
double y=(double)rect.bottom-MARGIN_BOTTOM;
// OUTPUT VALUES AND HORIZONTAL GRID
for (int n=0; n<=VALUE_MAX; n+=VALUE_MAX/VALUE_INTERVAL) {
MoveToEx(hdc2,VALUE_X,y,NULL);
LineTo(hdc2,rect.right-MARGIN_RIGHT,y);
sprintf(str,"%d",n);
TextOut(hdc2,VALUE_X,y-(lf.lfHeight/2),str,strlen(str));
y-=(double)(rect.bottom-MARGIN_BOTTOM-MARGIN_TOP) /
VALUE_INTERVAL;
}
y = 0;
// DRAW VERTICAL LINES (DATA)
for (int i=0; i<ENTRY_MAX; i++) {
Pen=CreatePen(PS_SOLID, 1, RGB((255/VALUE_MAX)*n[i],
(255/VALUE_MAX)*n[i],(255/VALUE_MAX)*n[i]));
OldPen=(HPEN)SelectObject(hdc2, Pen);
MoveToEx(hdc2,(i*SPACE_X)+MARGIN_LEFT,rect.bottom-MARGIN_BOTTOM,
NULL);
y = ( (double)(rect.bottom-MARGIN_BOTTOM-MARGIN_TOP) /
VALUE_MAX ) * n[i];
y = (double)(rect.bottom-MARGIN_BOTTOM)-y;
LineTo(hdc2,(i*SPACE_X)+MARGIN_LEFT,y);
}
SelectObject(hdc2,OldPen);
DeleteObject(Pen);
SelectObject(hdc2,OldFont);
DeleteObject(Font);
SelectObject(hdc2,OldBrush);
DeleteObject(Brush);
DeleteObject(hdc2);
EndPaint(hWnd,&ps);
}
break;
case WM_TIMER: {
switch (wParam) {
case TIMER_ID1: {
Randomize();
InvalidateRect(hWnd, &rect, TRUE);
}
break;
}
}
break;
case WM_DESTROY: {
PostQuitMessage(0);
return 0;
}
}
return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}
// SET RANDOM VALUES
void Randomize()
{
for (int i=0; i<ENTRY_MAX; i++)
n[i] = rand()%(VALUE_MAX);
}
Last edited by watdg; 12-02-2007 at 11:10 PM.
Reason: Updated code
-
No resolution
You know what? I don't think there's a solution for this one. I'll just have to use DirectX for this although this program doesn't need further modification. It's good as it is. Unless, I'm designing a fast-paced game, the flickering shall be tolerated.
:WAVE:
Similar Threads
-
By Darkrider in forum C++
Replies: 1
Last Post: 04-24-2008, 06:03 AM
-
By Darkrider in forum Database
Replies: 0
Last Post: 11-28-2005, 12:56 PM
-
By jojo in forum VB Classic
Replies: 2
Last Post: 08-12-2005, 05:21 AM
-
Replies: 11
Last Post: 10-03-2001, 03:08 PM
-
By Mike in forum VB Classic
Replies: 0
Last Post: 02-09-2001, 10:49 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks