I'm a new developer in Pocket PC and I wanna develop some GAPI games. I been trying to write directly to the screen buffer with my own BltBlt function but encounter alot of problems.
Recently I found of a new way of drawing to screen througnt the help of the Microsoft's DIB and BltBlt. It goes like the
My sprite --(Using Windows Bltblt)--> DIB --(Using memcpy or my own BltBlt)--> Framebuffer
The good thing of this method is the I can use the Windows Bltblt to handle the loading, resizing, transparent, etc. I sure this method is slower but how much? Currently, I using memcpy for normal sprite and pixel copy for transparent and alpha sprite. If u have a better method of posting sprite to screen, please advise. Codes and example will be nice. Thanx.
ello,
well, it's hard for me to say exactly how much slower it is, but I guess quite a lot. Especially the Pixel copy for transparent and alpha blended sprites is surelly really slow. The bitblt/memcpy might be not so bad...
They way to make it right (fast) is to use GAPI and do all your graphic functions yourself (or use a lib...) You might want to check easyce (
www.pocketmatrix.com/phantom/ - go to tech page for full source download) , it is a great lib with all the initialization for GAPI and some simple drawing functions included.
Good luck!
To put it simply, if you can use GAPI, then use it. For instance, Quake would be completely and utterly unplayable with the wind bitblt function. The 720 actually accelorates it, and it still is way to slow. There are several reasons why (ok maybe only one or two), but all you need to know is that comparing bitblt to GAPI is like comparing a moped (SP) to a formula 1 racing car.
Rob,
I'm curious to know how much slower Quake ran by using BitBlt rather than a custom GAPI blit routine. From what Dan has mentioned before, the majority of Quake's time is spent doing floating point calculations rather than blits. I would think that it should only be doing one blit per frame - from the back buffer to display RAM.
GAPI is way faster. I am just writing a general purpose gfx library to simplify bliting etc. for people who are starting pocket pc game programming. So far I have:
LoadImage/BitBlit/MaskBlit/TextOut/TransparentBlit
If you want help with gapi blitting please just email me!
Simon (and others),
Saying something is "way faster" doesn't carry much weight. If you're going to provide a library of routines for people to use, that has as its primary value to be faster than the available blitting routines provided by GDI, you should post profiling results between the two.
Simon:why do you code it? There's EasyCE already, you might rather want to add something to EasyCE so we have a more complete library.
EasyCE is a framework, I just wanted a library of functions for stuff like masked/alpha blitting. Since I am going to release the source code with no restrictions it would be easy to add into EasyCE if anyone wanted to do that.
I think Bitbank once tested the speed of BitBlt on PPC and found that the routine ran around 30ms. That's 3fps, not nearly fast enough for anything but board games.
Er, I can believe GDI BitBlt is 30ms, which is 33 fps surely. 1000/30 == 33 ish.
Of course you wouldnt get 33 fps because the game would have to be doing other things too.
Maybe it is less though, the only way to be sure it to time it yourself

You can easily get 10ms for a fullscreen copy with a pixel by pixel copy in c++, and about half that with some optimization. Obviously to get exact figures it needs proper timing trials, but there is certainly no doubt that GAPI is faster than GDI by a large margin.
BTW: I was talking about an iPaq 36xx for the rough timings in the above post. The speed would of course vary wildly depending on processor speed.
I had another thought

One other major factor would be the pixel format. A 256 col screen should blit twice as fast as a 4096/65000 col screen.
Anyway, I might run some speed tests on the GDI tomorrow if I feel like it. I suspect the real penalty for games with the GDI will actually NOT be the BitBlt function, but things like DrawText and the shape drawing functions which have loads of extra options you wouldnt normally need, but must take their toll on the speed.
Simon:well, if you can't get the backbuffer->frontbuffer copy in under 10ms you're having a poor start to get 24fps which is your tft screen refresh rate(as far as I know, might differ from device to device)
I dont really see why. If, for instance, I wanted 24 fps, that would give 40ms per frame. If the pixel per pixel copy with format conversion takes 10ms, and a full screen optimised blit takes 5ms, that gives quite a bit of time to do everything needed.
It doesnt really matter in the end, as my newest gfx routines are working fast enough that the game I wanted them for needs to be slowed down to make it playable.
well, it's still a great loss to give away 25% of PPC power to make a backbuffer->frontbuffer copy. And more advance games will gladly accept some additional cpu time. Blitting something to backbuffer is used only in the simplest games. Once a lot of sprites appears and you use effects like alpha-blending or even more complicated it all starts to work slow and every ms is precious
Hi guys! I have juz finished the code but there is something very strange. My GAPI is very slow. I can only hit a framerate of 49FPS even when I only do a memcopy of a blank screen. I hope that someone out there can alighten me. If u want to see my code, I will be more that happy to send. Thanks alot.
What are you doing in your main loop? Are you Sleeping at all? Are you building a debug or release build?
Dan East
Well I used peekmessage(). I'm currently using a Debug build but I have also tried the Release Build. The following is the code of my main file.
#include "stdafx.h"
#include <commctrl.h>
#include <aygshell.h>
#include "XSprite.h"
#include "XConsole.h"
//Menu Bar Height
#define MENU_HEIGHT 26
#define MAX_LOADSTRING 100
#define APP_TITLE TEXT("XConsole")
#define WINDOW_CLASS TEXT("MainWindow")
// Global Variables:
HINSTANCE hInst; // The current instance
HWND hwndCB; // The command bar handle
// Foward declarations of functions included in this code module:
ATOM MyRegisterClass (HINSTANCE hInstance, LPTSTR szWindowClass);
BOOL InitInstance (HINSTANCE, int);
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
//Global variables for XConsoles
DWORD presentTickValue, startTickValue, nextSecondTickValue;
int fps = 0, frameCounter = 0;
HWND XConsolehWnd;
HDC XConsoleBackBufferHDC;
HBITMAP XConsoleBackBufferHANDLER;
BITMAP XConsoleBackBufferBITMAP;
void* pXConsoleOffScreen;
int sizeofBackBuffer;
GXDisplayProperties g_gxdp;
void* pFrameBuffer;
XSprite *testSprite = new XSprite(TEXT("..\\Test.bmp"));
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
// Look for running instance of application
if (FindWindow(WINDOW_CLASS, APP_TITLE)){
SetForegroundWindow(FindWindow(WINDOW_CLASS, APP_TITLE));
return FALSE;
}
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
// Main message loop:
BOOL endLoop = FALSE;
while (!endLoop)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if(msg.message == WM_QUIT)
endLoop = TRUE;
// Dispatch the message
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
presentTickValue = GetTickCount();
if(nextSecondTickValue < presentTickValue)
{
fps = frameCounter;
frameCounter = 0;
nextSecondTickValue = presentTickValue + 1000;
}
else
frameCounter++;
testSprite->draw(0, 0);
updateConsole();
}
}
return msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
{
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(WINDOW_CLASS));
wc.hCursor = 0;
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = szWindowClass;
return RegisterClass(&wc);
}
//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
// Initialize global strings
MyRegisterClass(hInstance, WINDOW_CLASS);
XConsolehWnd = hWnd = CreateWindow(WINDOW_CLASS, APP_TITLE, WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
SetForegroundWindow ((HWND)(((DWORD)hWnd) | 0x01));
return FALSE;
}
//When the main window is created using CW_USEDEFAULT the height of the menubar (if one
// is created is not taken into account). So we resize the window after creating it
// if a menubar is present
{
RECT rect;
GetClientRect(hWnd, &rect);
RECT rc;
GetWindowRect(hWnd, &rc);
rc.bottom -= MENU_HEIGHT;
if (hwndCB)
MoveWindow(hWnd, rc.left, rc.top, rc.right, rc.bottom, FALSE);
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
switch (message)
{
case WM_COMMAND:
break;
case WM_CREATE:
SHFullScreen(hWnd, SHFS_HIDETASKBAR);
HDC winDC;
winDC = GetDC(hWnd);
RECT rcMain;
rcMain.left = 0;
rcMain.top = 0;
rcMain.right = GetDeviceCaps(winDC, HORZRES);
rcMain.bottom = GetDeviceCaps(winDC, VERTRES);
MoveWindow(hWnd, rcMain.left, rcMain.top, rcMain.right, rcMain.bottom, TRUE);
ShowWindow(hWnd, SW_SHOW);
ReleaseDC(hWnd, winDC);
// Setting up the XConsole
intializeXConsole();
break;
case WM_LBUTTONDOWN:
SendMessage(hWnd,WM_CLOSE,0,0);
break;
case WM_PAINT:
BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
break;
case WM_HIBERNATE:
//Release any large memory resources here
break;
case WM_ACTIVATE:
break;
case WM_DESTROY:
// Freeing up all the resources used by XConsole
destroyXConsole();
DestroyWindow(hwndCB);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
void intializeXConsole(void)
{
// Initial the GAPI and get the framebuffer
GXOpenDisplay(XConsolehWnd, GX_FULLSCREEN);
g_gxdp = GXGetDisplayProperties();
// Creates the off screen buffer
HDC hScreenDC;
hScreenDC = GetDC(0);
XConsoleBackBufferHDC = CreateCompatibleDC(hScreenDC);
//Initialize the DIB header for the backbuffer
BYTE buffer[sizeof(BITMAPINFOHEADER) + 3 * sizeof(RGBQUAD)];
// Handy pointers
BITMAPINFO* pBMI = (BITMAPINFO*) buffer;
BITMAPINFOHEADER* pHeader = &pBMI->bmiHeader;
DWORD* pColors = (DWORD*)&pBMI->bmiColors;
// DIB Header
pHeader->biSize = sizeof(BITMAPINFOHEADER);
pHeader->biWidth = g_gxdp.cxWidth;
pHeader->biHeight = -(long)g_gxdp.cyHeight;
pHeader->biPlanes = 1;
pHeader->biBitCount = (WORD)g_gxdp.cBPP;
pHeader->biCompression = BI_BITFIELDS;
pHeader->biSizeImage = sizeofBackBuffer
= (g_gxdp.cxWidth * g_gxdp.cyHeight * g_gxdp.cBPP) / 8;
pHeader->biXPelsPerMeter = 0;
pHeader->biYPelsPerMeter = 0;
pHeader->biClrUsed = 0;
pHeader->biClrImportant = 0;
// Color masks
if (g_gxdp.ffFormat & kfDirect555)
{
pColors[0] = 0x1F << 10;
pColors[1] = 0x1F << 5;
pColors[2] = 0x1F;
}
else if (g_gxdp.ffFormat & kfDirect565)
{
pColors[0] = 0x1F << 11;
pColors[1] = 0x3F << 5;
pColors[2] = 0x1F;
}
else if (g_gxdp.ffFormat & kfDirect888)
{
pColors[0] = 0x00FF0000;
pColors[1] = 0x0000FF00;
pColors[2] = 0x000000FF;
}
else if (g_gxdp.ffFormat & kfDirect444)
{
pColors[0] = 0x0F00;
pColors[1] = 0x00F0;
pColors[2] = 0x000F;
}
XConsoleBackBufferHANDLER = CreateDIBSection( hScreenDC, pBMI, DIB_RGB_COLORS, &pXConsoleOffScreen,
0, 0 );
GetObject(XConsoleBackBufferHANDLER, sizeof(BITMAP), &XConsoleBackBufferBITMAP);
SelectObject(XConsoleBackBufferHDC, XConsoleBackBufferHANDLER);
// Initialize the ticker
nextSecondTickValue = GetTickCount() + 1000;
ReleaseDC(0,hScreenDC);
pFrameBuffer = GXBeginDraw();
}
void destroyXConsole(void)
{
DeleteDC(XConsoleBackBufferHDC);
DeleteObject(XConsoleBackBufferHDC);
GXEndDraw();
GXCloseDisplay();
}
void updateConsole(void)
{
RECT rt;
GetClientRect(XConsolehWnd, &rt);
unsigned short strBuffer[10];
swprintf(strBuffer,L"FPS: %ld", fps);
// Draws to the back buffer
DrawText(XConsoleBackBufferHDC, strBuffer, _tcslen(strBuffer), &rt,
DT_SINGLELINE);
memcpy(pFrameBuffer, pXConsoleOffScreen, sizeofBackBuffer);
}
You're calling printf and using the Windows API to render a true-type font every frame. Those are two very expensive functions (relatively). If you take those calls out things should speed up significantly (for example, at least only call those routines once every second, use itoa instead of printf, and I would also recommend using a custom bitmap font you draw manually, like Jacco does in EasyCE). Also, there is a large difference in speed between debug and release builds. Release builds usually run at least twice as fast for me, be it a game, DEXplor ,etc. Also make sure you have the optimizations for the release build set for Maximize Speed.
Dan East