December 30th, 2010, 11:56 AM
#1
Window won't repaint
Hi, for School work we got to do a retarded game and I'm nearly finished with the gameplay then it's on to improving the code. But the problem is with the CheckStatus function. If I put it in the end of WM_KEYDOWN it repaints fine but if i WM_PAINT the window won't repaint. Basicly the CheckStatus function checks if the cat ate all the mice it gives you the choice of a new game or exit and if a(non moving) dog ate the cat the same choice.
So what's wrong ?
Attached Files
December 30th, 2010, 10:53 PM
#2
Re: Window won't repaint
Try using this command:
Code:
InvalidateRect(hwndDlg, NULL, TRUE);
It forces a redraw - it should be placed in one of your button cases or anywhere an action takes place. - I didnt look at your code because I wont download it. You should post it using [ code ] tags:
http://www.codeguru.com/forum/misc.php?do=bbcode#code
December 31st, 2010, 07:51 AM
#3
Re: Window won't repaint
Maybe I haven't made myself clear enough. The program works fine create calls start start initilizes all the variables and animal positions and then the board paints. And everything works ok you hit a dog or eat all the mice the game ends and gives you 2 options. 1st play again and 2nd exit. The program works fine if I put the CheckStatus function in WM_KEYDOWN if the user chooses to play again the board re paints but if I put the function in WM_PAINT it doesn't.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#define WIDTH 1378
#define HEIGHT 820
#define WM_START 0x1337
#define IDM_FILE_NEW 1
#define IDM_FILE_REPLAY 2
#define IDM_FILE_DIFFICULTY 3
#define IDM_FILE_OPTIONS 4
#define IDM_FILE_QUIT 5
HINSTANCE Instance;
int GameDevelopment = 1;
int Misek;
int Resizing = 0;
int Tezavnost = 2;
int Vertical = 0;
struct Cat{
int X, Y;
int prevX, prevY;
}Cat;
struct Mice{
char status;
int X, Y;
}Mouse[50];
struct Dogs{
int X, Y;
}Dog[200];
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void LoadAndBlitCat(HDC hWinDC);
void LoadAndBlitMice(HDC hWinDC);
void LoadAndBlitDogs(HDC hWinDC);
void InitializeAnimals(HWND hwnd);
void CheckStatus(HWND hwnd);
void AddMenus(HWND hwnd);
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow )
{
MSG msg ;
WNDCLASS wc = {0};
wc.lpszClassName = TEXT( "Windows" );
wc.hInstance = hInstance ;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpfnWndProc = WndProc ;
wc.hCursor = LoadCursor(0,IDC_ARROW);
RegisterClass(&wc);
CreateWindow(wc.lpszClassName, TEXT("Windows"),
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_VISIBLE,
GetSystemMetrics(SM_CXSCREEN)/9,
GetSystemMetrics(SM_CYSCREEN)/12,
WIDTH, HEIGHT, 0, 0, hInstance, 0);
while( GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam ){
HDC hdc;
PAINTSTRUCT ps;
switch(msg)
{
case WM_START:{
Misek = Tezavnost*5;
GameDevelopment = 1;
InitializeAnimals(hwnd);
InvalidateRect(hwnd, NULL, TRUE);
break;
}
case WM_CREATE:{
AddMenus(hwnd);
SendMessage(hwnd, WM_START, 0, 0);
break;
}
case WM_KEYDOWN:{
if(!Vertical){
Cat.prevX = Cat.X;
Cat.prevY = Cat.Y;
}
if(wParam == 0x57){
if(Cat.Y-34 <= 0)
Beep(500,500);
else
Cat.Y -= 34;
}
if(wParam == 0x41){
if(Cat.X-34 <= 0)
Beep(500,500);
else
Cat.X -= 34;
}
if(wParam == 0x53){
if(Cat.Y+34 >= HEIGHT-150)
Beep(500,500);
else
Cat.Y += 34;
}
if(wParam == 0x44){
if(Cat.X+34 >= WIDTH-34)
Beep(500,500);
else
Cat.X += 34;
}
Vertical = 1;
CheckStatus(hwnd);
InvalidateRect(hwnd, NULL, FALSE);
break;
}
case WM_PAINT:{
Vertical = 0;
hdc = BeginPaint(hwnd, &ps);
HBRUSH hFillBrush = GetSysColorBrush(COLOR_3DFACE);
HBRUSH hBrushOld = (HBRUSH)SelectObject(hdc, hFillBrush);
HPEN hLinePen = CreatePen(PS_NULL, 0, RGB(0, 0, 0));
HPEN hPenOld = (HPEN)SelectObject(hdc, hLinePen);
Rectangle(hdc, Cat.prevX, Cat.prevY, Cat.prevX+34, Cat.prevY+34);
hLinePen = CreatePen(PS_SOLID, 2, RGB(0, 0, 0));
hPenOld = (HPEN)SelectObject(hdc, hLinePen);
if(GameDevelopment){
for(int i = 1;i<=WIDTH;i=i+34){
MoveToEx(hdc, i, 0, NULL);
LineTo(hdc, i, HEIGHT-139);
}
for(int i = 1;i<=HEIGHT-139;i=i+34){
MoveToEx(hdc, 0, i, NULL);
LineTo(hdc, WIDTH-17, i);
}
if(GameDevelopment == 1 || Resizing == 1){
LoadAndBlitMice(hdc);
Resizing = 0;
}
LoadAndBlitCat(hdc);
if(GameDevelopment == 1 || Resizing == 1){
LoadAndBlitDogs(hdc);
Resizing = 0;
}
}
SelectObject(hdc, hPenOld);
DeleteObject(hLinePen);
SelectObject(hdc, hBrushOld);
DeleteObject(hFillBrush);
EndPaint(hwnd, &ps);
Sleep(100);
break;
}
case WM_SIZE:{
Resizing = 1;
break;
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
void AddMenus(HWND hwnd){
HMENU hMenubar;
HMENU hMenu;
hMenubar = CreateMenu();
hMenu = CreateMenu();
AppendMenu(hMenu, MF_STRING, IDM_FILE_NEW, TEXT("&New"));
AppendMenu(hMenu, MF_STRING, IDM_FILE_REPLAY, TEXT("&Replay"));
AppendMenu(hMenu, MF_STRING, IDM_FILE_DIFFICULTY, TEXT("&Difficutly"));
AppendMenu(hMenu, MF_STRING, IDM_FILE_OPTIONS, TEXT("&Options"));
AppendMenu(hMenu, MF_SEPARATOR, 0, NULL);
AppendMenu(hMenu, MF_STRING, IDM_FILE_QUIT, TEXT("&Quit"));
AppendMenu(hMenubar, MF_POPUP, (UINT_PTR)hMenu, TEXT("&File"));
SetMenu(hwnd, hMenubar);
}
void LoadAndBlitCat(HDC hWinDC){
HBITMAP hBitmap;
BITMAP qBitmap;
HDC hLocalDC;
hBitmap = (HBITMAP)LoadImage(NULL, TEXT("Cat.bmp"), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_DEFAULTSIZE | LR_LOADFROMFILE);
hLocalDC = CreateCompatibleDC(hWinDC);
GetObject((HGDIOBJ) hBitmap, sizeof(BITMAP), (LPVOID) &qBitmap);
HBITMAP hOldBmp = (HBITMAP) SelectObject(hLocalDC, hBitmap);
BitBlt(hWinDC, Cat.X, Cat.Y, qBitmap.bmWidth, qBitmap.bmHeight, hLocalDC, 0, 0, SRCCOPY);
SelectObject(hLocalDC, hOldBmp);
DeleteDC(hLocalDC);
DeleteObject(hBitmap);
}
void LoadAndBlitMice(HDC hWinDC){
HBITMAP hBitmap;
BITMAP qBitmap;
HDC hLocalDC;
hBitmap = (HBITMAP)LoadImage(NULL, TEXT("Mouse.bmp"), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_DEFAULTSIZE | LR_LOADFROMFILE);
hLocalDC = CreateCompatibleDC(hWinDC);
GetObject((HGDIOBJ) hBitmap, sizeof(BITMAP), (LPVOID) &qBitmap);
HBITMAP hOldBmp = (HBITMAP) SelectObject(hLocalDC, hBitmap);
for(int i=0;i<Tezavnost*5;i++)
if(Mouse[i].status != 'D')
BitBlt(hWinDC, Mouse[i].X, Mouse[i].Y, qBitmap.bmWidth, qBitmap.bmHeight, hLocalDC, 0, 0, SRCCOPY);
SelectObject(hLocalDC, hOldBmp);
DeleteDC(hLocalDC);
DeleteObject(hBitmap);
}
void LoadAndBlitDogs(HDC hWinDC){
HBITMAP hBitmap;
BITMAP qBitmap;
HDC hLocalDC;
hBitmap = (HBITMAP)LoadImage(NULL, TEXT("Batman.bmp"), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_DEFAULTSIZE | LR_LOADFROMFILE);
hLocalDC = CreateCompatibleDC(hWinDC);
GetObject((HGDIOBJ) hBitmap, sizeof(BITMAP), (LPVOID) &qBitmap);
HBITMAP hOldBmp = (HBITMAP) SelectObject(hLocalDC, hBitmap);
for(int i=0;i<Tezavnost*50;i++)
BitBlt(hWinDC, Dog[i].X, Dog[i].Y, qBitmap.bmWidth, qBitmap.bmHeight, hLocalDC, 0, 0, SRCCOPY);
SelectObject(hLocalDC, hOldBmp);
DeleteDC(hLocalDC);
DeleteObject(hBitmap);
}
void InitializeAnimals(HWND hwnd){
int i, j, si;
Cat.X =2;
Cat.Y=2;
srand(time(NULL));
for(i=0;i<Tezavnost*5;i++)
Mouse[i].status = 'A';
for(i=0;i<Tezavnost*5;i++){//Misi XY
do{
si=0;
Mouse[i].X = (rand()%40*34)+2;
Mouse[i].Y = (rand()%20*34)+2;
for(j=0;i>j;j++)
if((Mouse[i].Y == Mouse[j].Y && Mouse[i].X == Mouse[i].X) || (Mouse[i].Y == Cat.Y && Mouse[i].X == Cat.X))
si=1;
}while(si);
}
for(i=0;i<Tezavnost*50;i++){//Psi XY
do{
si=0;
Dog[i].X = (rand()%40*34)+2;
Dog[i].Y = (rand()%20*34)+2;
for(j=0;i>j;j++)
if((Dog[i].X == Cat.X && Dog[i].Y == Cat.Y) || (Dog[i].X == Dog[j].X && Dog[i].Y == Dog[j].Y) || (Dog[i].X == Mouse[j].X && Dog[i].Y == Mouse[j].Y))
si=1;
if(Dog[i].X == Mouse[j].X && Dog[i].Y == Mouse[j].Y)
si=1;
}while(si);
}
}
void CheckStatus(HWND hwnd){
int i;
for(i=0;i<Tezavnost*5;i++){
if(Mouse[i].Y == Cat.Y && Mouse[i].X == Cat.X){
Mouse[i].status = 'D';
if(--Misek == 0){
if(MessageBox(hwnd, TEXT("YOU WIN"), TEXT("MessageBox"), MB_YESNO) == IDYES)
SendMessage(hwnd, WM_START, 0, 0);
else
SendMessage(hwnd, WM_CLOSE, 0, 0);
}
i=1000;
}
}
for(i=0;i<Tezavnost*50;i++){
if(Dog[i].Y == Cat.Y && Dog[i].X == Cat.X){
if(MessageBox(hwnd, TEXT("YOU LOST"), TEXT("MessageBox"), MB_YESNO) == IDYES)
SendMessage(hwnd, WM_START, 0, 0);
else
SendMessage(hwnd, WM_CLOSE, 0, 0);
i=1000;
}
}
}
Last edited by Sevko; December 31st, 2010 at 08:15 AM .
January 1st, 2011, 10:21 AM
#4
Re: Window won't repaint
I can't say I have read everything in your code but why use SendMessage instead of PostMessage?. SendMessage calls WndProc and that shouldn't be necessary.
Where in WM_PAINT do you place the call to CheckStatus? If you call CheckStatus before EndPaint I guess that the window is not invalidated anymore (since you use SendMessage). If you minimize and then restore the window I guess it works alright? Anyway, in my mind WM_PAINT should only do paint stuff so having the CheckStatus call elsewhere is fine.
Last edited by S_M_A; January 1st, 2011 at 10:35 AM .
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
Click Here to Expand Forum to Full Width