Advertisement

Please help with some code

Started by January 06, 2001 01:04 AM
1 comment, last by caffeineaddict 24 years ago
OK I wrote some code to load Bitmaps and everything in the Windows GDI but now that I have bitmaps on the screen I want to put them together on the screen to resemble shapes, I am making a Tetris clone by the way, and I am trying to use an array and my friend says I should use timers to get them on there instead of a for loop, if you can help, please look at the code and reply. thanks a lot. BTW we are using Dev-C++ 4 if that helps any, it sure doesnt help us though. #include #pragma hdrstop static char g_szClassName[] = "MyWindowClass"; static HINSTANCE g_hInst = NULL; const UINT idTimer1 = 1; UINT nTimerDelay = 100; HBITMAP hbmBall, hbmMask; BITMAP bm; int ballX, ballY; int shape1[5][5] = {{0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 1, 1, 0, 0}, {0, 1, 1, 0, 0}}; int shape2[5][5] = {{0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 1, 1, 1, 0}, {0, 0, 1, 0, 0}}; int shape3[5][5] = {{0, 0, 0, 0, 0}, {0, 0, 1, 0, 0}, {0, 0, 1, 0, 0}, {0, 0, 1, 0, 0}, {0, 0, 1, 0, 0}}; int shape4[5][5] = {{0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 1, 1, 1, 0}, {0, 1, 0, 0, 0}}; int shape5[5][5] = {{0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 1, 1, 1, 0}, {0, 0, 0, 1, 0}}; int shape6[5][5] = {{0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 1, 0, 0, 0}, {0, 1, 1, 0, 0}, {0, 0, 1, 0, 0}}; int shape7[5][5] = {{0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 1, 0}, {0, 0, 1, 1, 0}, {0, 0, 1, 0, 0}}; void EraseBall(HDC hdc){ RECT rc; rc.left = ballX; rc.top = ballY; rc.right = ballX + bm.bmWidth; rc.bottom = ballY + bm.bmHeight; FillRect(hdc, &rc, (HBRUSH) GetStockObject(WHITE_BRUSH)); } void DrawBall(HDC hdc){ HDC hdcMemory; hdcMemory = CreateCompatibleDC(hdc); SelectObject(hdcMemory, hbmMask); BitBlt(hdc, ballX, ballY, bm.bmWidth, bm.bmHeight, hdcMemory, 0, 0, SRCAND); SelectObject(hdcMemory, hbmBall); BitBlt(hdc, ballX, ballY, bm.bmWidth, bm.bmHeight, hdcMemory, 0, 0, SRCPAINT); DeleteDC(hdcMemory); } void UpdateBall(HWND hwnd){ HDC hdcWindow; hdcWindow = GetDC(hwnd); RECT rc; GetClientRect(hwnd, &rc); int r; int c; for(r=1; r>=100; r=+20){ for (c=1; c>=100; c=+20){ if (shape1[r][c] == 1){ ballX=r; ballY=c; DrawBall(hdcWindow); } } } } LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam){ switch(Message){ case WM_CREATE: hbmBall = LoadBitmap(g_hInst, "BALLBMP"); hbmMask = LoadBitmap(g_hInst, "MASKBMP"); if(!hbmBall || !hbmMask){ MessageBox(hwnd, "Load of resources failed.", "Error", MB_OK | MB_ICONEXCLAMATION); return -1; } GetObject(hbmBall, sizeof(bm), &bm); SetTimer(hwnd, idTimer1, nTimerDelay, NULL); ballX = 0; ballY = 0; break; case WM_TIMER: if(hbmBall && hbmMask){ HDC hdcWindow; hdcWindow = GetDC(hwnd); EraseBall(hdcWindow); UpdateBall(hwnd); DrawBall(hdcWindow); ReleaseDC(hwnd, hdcWindow); } break; case WM_PAINT: if(hbmBall && hbmMask){ PAINTSTRUCT ps; HDC hdcWindow; hdcWindow = BeginPaint(hwnd, &ps); DrawBall(hdcWindow); EndPaint(hwnd, &ps); } break; case WM_CLOSE: DestroyWindow(hwnd); break; case WM_DESTROY: KillTimer(hwnd, idTimer1); DeleteObject(hbmBall); DeleteObject(hbmMask); PostQuitMessage(0); break; default: return DefWindowProc(hwnd, Message, wParam, lParam); } return 0; } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){ WNDCLASSEX WndClass; HWND hwnd; MSG Msg; g_hInst = hInstance; WndClass.cbSize = sizeof(WNDCLASSEX); WndClass.style = 0; WndClass.lpfnWndProc = WndProc; WndClass.cbClsExtra = 0; WndClass.cbWndExtra = 0; WndClass.hInstance = g_hInst; WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); WndClass.hCursor = LoadCursor(NULL, IDC_ARROW); WndClass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); WndClass.lpszMenuName = NULL; WndClass.lpszClassName = g_szClassName; WndClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION); if(!RegisterClassEx(&WndClass)){ MessageBox(0, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL); return 0; } hwnd = CreateWindowEx( WS_EX_CLIENTEDGE, g_szClassName, "A Bitmap Program", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 320, 240, NULL, NULL, g_hInst, NULL); if(hwnd == NULL){ MessageBox(0, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL); return 0; } ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); while(GetMessage(&Msg, NULL, 0, 0)){ TranslateMessage(&Msg); DispatchMessage(&Msg); } return Msg.wParam; }
You can use either. Timers are simplier because you have a fixed frame rate assuming you can keep up with the timer. A loop is going to push frames as fast as you can which means the frame rate will most likely vary so things don''t move fixed distances each frame. That means you have to do your updates based upon how long it has been since the last update. If you use a loop you have peek for messages since otherwise you won''t return until a message is in the cue. The loop would be an outer loop around your message loop so that you continue to process messages like mouse clicks and key presses. So you peek for a message and if there are any you process them until there aren''t any more then update your frame.

Most games use a frame loop rather than a timer. If that wasn''t true then people wouldn''t talk about how much higher a frame rate they got when they got a new computer or video card because everyone over the minimum needed to keep up with the timer would get the same frame rate. The distinction your friend misses is the differance between realtime and normal applications. Games are realtime. While that means many things the most important is that you are not trying to play nice with other applications competing for the CPU. Your main concern is being responsive to the user. Timers are generally better for applications that run all the time, but sleep most of the time like an application that periodically downloads stock quotes. Tetris isn''t real demanding so a timer might work fine, but the GDI isn''t exactly fast either.
Keys to success: Ability, ambition and opportunity.
Advertisement
Thanks for the response but that really didnt help answer my question much, I wanted to know how to get that code to work, all that code does right now is put a bitmap on the screen, I want to know how to get it to load a bitmap in the position of the ones in the array, I also had an idea that could do this where it did something like

case(1)
{
int shape1[5][5] =
{{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 1, 1, 1, 0},
{0, 1, 1, 1, 0},
{0, 1, 1, 1, 0}};
}
case(2)
{
int shape2[5][5] =
{{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 1, 1, 1},
{0, 0, 0, 1, 0}};
}
etc. etc. for the rest of the shapes, would something like that work and if not how could i use the code and make the shapes appear on the screen in the code i gave in my first post

This topic is closed to new replies.

Advertisement