Thread: game window rejected painting !

  1. #1
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    Exclamation game window rejected painting !

    Hi everyone,

    just a simple windows question. i'm making my first windows game with C and everything seems to be ok, but when i run the game i found the game window rejected paiting anything after several seconds, even though i removed all code handles drawing objects, is there anyone know what happened and gimme a solution ? thanks !!!
    Never end on learning~

  2. #2
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Yes. I know exactly what happened because all of your code magically appeared on my computer with [code][/code] tags.
    Last edited by Brad0407; 03-26-2007 at 06:08 AM.
    Don't quote me on that... ...seriously

  3. #3
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    well i dont show any piece of code here because they are not only 20 lines or something, if you like i can post it later. i just wanna ask if anyone suffered this weired thing before, everything works perfect but several seconds later what you can c only is a white screen, nothing will be drawn anymore, any help ?
    Never end on learning~

  4. #4
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Any coder has suffered symptoms like that. However, the cause could be anything from an infinite loop to a memory leak. That is why we ask that you post your code. At least post the code for the function that does the drawing, any functions it calls, and the function that called it.
    Don't quote me on that... ...seriously

  5. #5
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    oki here they comes:

    // framework.c
    Code:
    /////////////////
    //// Headers ////
    /////////////////
    #include <windows.h>
    #include "const.h"
    
    
    ///////////////////
    //// Variables ////
    ///////////////////
    WNDCLASSEX wc;
    HWND hwnd;
    MSG msg;
    
    
    /////////////////////////////
    //// Function Prototypes ////
    /////////////////////////////
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
    extern void initGame(HWND hWnd);
    extern void updateGame();
    extern void drawGame();
    extern void keyPressedGame();
    extern void clearGame();
    
    
    ////////////////////
    //// Main Entry ////
    ////////////////////
    int WINAPI WinMain(HINSTANCE hInstance, 
    				   HINSTANCE hPrevInstance,
    				   LPSTR lpCmdLine,
    				   int nCmdShow)
    {
    	// Window class~
    	wc.cbClsExtra					=	0;
    	wc.cbSize						=	sizeof(WNDCLASSEX);
    	wc.cbWndExtra					=	0;
    	wc.hbrBackground				=	(HBRUSH) (COLOR_WINDOW + 1);
    	wc.hCursor						=	NULL;
    	wc.hIcon						=	NULL;
    	wc.hIconSm						=	NULL;
    	wc.hInstance					=	hInstance;
    	wc.lpfnWndProc					=	WndProc;
    	wc.lpszClassName				=	APP_CLASS_NAME;
    	wc.lpszMenuName					=	NULL;
    	wc.style						=	CS_HREDRAW | CS_VREDRAW;
    
    	// Register the window~
    	if (!RegisterClassEx(&wc))
    	{
    		MessageBox(NULL, "Can not register this class !", NULL, MB_OK);
    		return FALSE;
    	}
    
    	// Create a window~
    	hwnd							=	CreateWindow(
    												APP_CLASS_NAME, 
    												GAME_NAME, 
    												WS_OVERLAPPEDWINDOW, 
    												WIN_ROOT_X, 
    												WIN_ROOT_Y, 
    												WIN_ROOT_W, 
    												WIN_ROOT_H, 
    												NULL, 
    												NULL, 
    												hInstance, 
    												NULL
    											);
    
    	// Show error message if creating window failed~
    	if (!hwnd)
    	{
    		MessageBox(NULL, ERROR_STR_CAN_NOT_CREATE_WINDOW, NULL, MB_OK);
    		return FALSE;
    	}
    
    	// Show window state~
    	ShowWindow(hwnd, nCmdShow);
    
    	// Sends a WM_PAINT message to WndProc~
    	UpdateWindow(hwnd);
    
    	// Initialize game resources~
    	initGame(hwnd);
    
    	// Main loop~
    //	ZeroMemory(&msg, sizeof(msg));
    	while (msg.message != WM_QUIT)
    	{
    		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    		{
    			TranslateMessage(&msg);
    			DispatchMessage(&msg);
    		}
    
    		// Game loop~
    		updateGame();
    		drawGame();
    
    		// Sleep a while~
    		Sleep(GAME_SLEEP_FRAMES);
    	}
    
    	// Release game resources~
    	clearGame();
    
    	return FALSE;
    }
    
    
    /////////////////////////
    //// Message Handler ////
    /////////////////////////
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	switch (message)
    	{
    		case WM_PAINT:
    			//
    			break;
    		case WM_KEYDOWN:
    			keyPressedGame(wParam);
    			break;
    		case WM_DESTROY:
    			PostQuitMessage(0);
    			break;
    		default:
    			return DefWindowProc(hWnd, message, wParam, lParam);
    	}
    
    	return 0;
    }
    // game.c
    Code:
    #include <windows.h>
    #include "const.h"
    
    
    ///////////////////
    //// Variables ////
    ///////////////////
    extern HWND hwnd;
    //HDC hdc;
    //HDC hdcMem;
    //HBITMAP hbmp;
    int iMainState;
    int iGameState;
    int iShapeType;
    int iShapeState;
    int iShapeRow;
    int iShapeCol;
    int iShapeMoveTick;
    int iMapMinX;
    int iMapMaxX;
    int iMapMinY;
    int iMapMaxY;
    
    int map[MAP_ROW][MAP_COL];
    
    int shapesRow[SHAPE_TYPE_NUM][SHAPE_STATE_NUM][SHAPE_RECT_NUM] = 
    {
    	{{0, 0, 1, 1},		{0, 0, 1, 1},	{0, 0, 1, 1},	{0, 0, 1, 1}}, 
    	{{0, 0, 0, 0},		{0, -1, 1, 2},	{0, 0, 0, 0},	{0, -1, 1, 2}}, 
    	{{0, -1, 0, 0},		{0, -1, 0, 1},	{0, 0, 1, 0},	{0, -1, 0, 1}}, 
    	{{0, -1, -1, 0},	{0, -1, 0, 1},	{0, -1, -1, 0},	{0, -1, 0, 1}}, 
    	{{0, 0, -1, -1},	{0, -1, 0, 1},	{0, 0, -1, -1},	{0, -1, 0, 1}}, 
    	{{0, -1, 0, 0},		{0, -1, 1, 1},	{0, 0, 0, 1},	{0, -1, -1, 1}}, 
    	{{0, 0, -1, 0},		{0, -1, -1, 1},	{0, 0, 0, 1},	{0, -1, 1, 1}}
    };
    
    int shapesCol[SHAPE_TYPE_NUM][SHAPE_STATE_NUM][SHAPE_RECT_NUM] = 
    {
    	{{0, 1, 0, 1},		{0, 1, 0, 1},	{0, 1, 0, 1},	{0, 1, 0, 1}}, 
    	{{0, -1, 1, 2},		{0, 0, 0, 0},	{0, -1, 1, 2},	{0, 0, 0, 0}}, 
    	{{0, 0, -1, 1},		{0, 0, -1, 0},	{0, -1, 0, 1},	{0, 0, 1, 0}}, 
    	{{0, -1, 0, 1},		{0, 0, -1, -1},	{0, -1, 0, 1},	{0, 0, -1, -1}}, 
    	{{0, -1, 0, 1},		{0, -1, -1, 0},	{0, -1, 0, 1},	{0, -1, -1, 0}}, 
    	{{0, -1, -1, 1},	{0, 0, -1, 0},	{0, -1, 1, 1},	{0, 0, 1, 0}}, 
    	{{0, -1, 1, 1},		{0, -1, 0, 0},	{0, -1, 1, -1},	{0, 0, 1, 0}}
    };
    
    
    /////////////////////////////
    //// Function Prototypes ////
    /////////////////////////////
    void initGame(HWND hWnd);
    void updateGame();
    void drawGame();
    void keyPressedGame();
    void clearGame();
    
    void initGameMain();
    void updateGameMain();
    void drawGameMain(HDC hdc);
    
    void drawRect(HDC hdc, int type, int x, int y);
    void drawMapBG(HDC hdc);
    void drawRects(HDC hdc);
    int createShape();
    void moveLeft();
    void moveRight();
    void moveDown();
    void dropDown();
    void rotate(int dir);
    void concrete();
    void check();
    
    
    //
    void initGame(HWND hWnd)
    {
    //	hwnd								=	hWnd;
    //	// Get current Device Context~
    //	hdc									=	GetDC(hwnd);
    //	// Create a DC in memory~
    //	hdcMem								=	CreateCompatibleDC(hdc);
    //	// Get a bitmap object based on current DC~
    //	hbmp								=	CreateCompatibleBitmap(hdc, WIN_ROOT_W, WIN_ROOT_H);
    //	// Bind memory DC with that bitmap~
    //	SelectObject(hdcMem, hbmp);
    //	iMainState							=	MAIN_STATE_DEMO;
    //temp
    	iMainState							=	MAIN_STATE_GAMEMAIN;
    //
    	iGameState							=	GAME_STATE_CREATE;
    
    	iShapeType							=	SHAPE_TYPE_EMPTY;
    	iShapeState							=	0;
    	iShapeRow							=	SHAPE_INIT_ROW;
    	iShapeCol							=	SHAPE_INIT_COL;
    	iShapeMoveTick						=	0;
    
    	iMapMinX							=	MAP_X;
    	iMapMaxX							=	MAP_X + MAP_COL * (RECT_W + RECT_SPACE);
    	iMapMinY							=	MAP_Y;
    	iMapMaxY							=	MAP_Y + MAP_ROW * (RECT_H + RECT_SPACE);
    }
    
    void updateGame()
    {
    	switch (iMainState)
    	{
    		case MAIN_STATE_DEMO:
    			//
    			break;
    		case MAIN_STATE_COVER:
    			//
    			break;
    		case MAIN_STATE_OPTION:
    			//
    			break;
    		case MAIN_STATE_CREDIT:
    			//
    			break;
    		case MAIN_STATE_GAMEMAIN:
    			updateGameMain();
    			break;
    		case MAIN_STATE_GAMEOVER:
    			//
    			break;
    		case MAIN_STATE_GAMEFINISHED:
    			//
    			break;
    		case MAIN_STATE_STAGEOVER:
    			//
    			break;
    		case MAIN_STATE_STAGEFINISHED:
    			//
    			break;
    	}
    }
    
    void drawGame()
    {
    	// Get current Device Context~
    	HDC hdc = GetDC(hwnd);
    //	// Create a DC in memory~
    //	HDC hdcMem = CreateCompatibleDC(hdc);
    //	// Get a bitmap object based on current DC~
    //	HBITMAP hbmp = CreateCompatibleBitmap(hdc, WIN_ROOT_W, WIN_ROOT_H);
    //	// Bind memory DC with that bitmap~
    //	SelectObject(hdcMem, hbmp);
    
    	switch (iMainState)
    	{
    		case MAIN_STATE_DEMO:
    			//
    			break;
    		case MAIN_STATE_COVER:
    			//
    			break;
    		case MAIN_STATE_OPTION:
    			//
    			break;
    		case MAIN_STATE_CREDIT:
    			//
    			break;
    		case MAIN_STATE_GAMEMAIN:
    //			drawGameMain(hdcMem);
    			drawGameMain(hdc);
    			break;
    		case MAIN_STATE_GAMEOVER:
    			//
    			break;
    		case MAIN_STATE_GAMEFINISHED:
    			//
    			break;
    		case MAIN_STATE_STAGEOVER:
    			//
    			break;
    		case MAIN_STATE_STAGEFINISHED:
    			//
    			break;
    	}
    
    	// Copy a bitmap from memory to current DC~
    //	BitBlt(hdc, 0, 0, WIN_ROOT_W, WIN_ROOT_H, hdcMem, 0, 0, SRCCOPY);
    
    	// Release objects we don't use anymore~
    //	ReleaseDC(hwnd, hdcMem);
    	ReleaseDC(hwnd, hdc);
    //	DeleteDC(hdcMem);
    //	DeleteObject(hbmp);
    }
    
    void keyPressedGame(int key)
    {
    	switch (key)
    	{
    		case 0x57: // W: Up
    			dropDown();
    			break;
    		case 0x53: // S: Down
    			moveDown();
    			break;
    		case 0x41: // A: Left
    			moveLeft();
    			break;
    		case 0x44: // D: Right
    			moveRight();
    			break;
    		case 0x4A: // J: Rotate Counter Clockwise~
    			rotate(DIR_LEFT);
    			break;
    		case 0x4B: // K: Rotate Clockwise~
    			rotate(DIR_RIGHT);
    			break;
    	}
    }
    
    void clearGame()
    {
    	//
    }
    
    
    /////////////////////////
    //// State Game Main ////
    /////////////////////////
    void initGameMain()
    {
    	//
    }
    
    void updateGameMain()
    {
    	switch (iGameState)
    	{
    		case GAME_STATE_CREATE:
    			iShapeType					=	createShape();
    			iShapeState					=	0;
    			iShapeRow					=	SHAPE_INIT_ROW;
    			iShapeCol					=	SHAPE_INIT_COL;
    
    			if (iShapeType != SHAPE_TYPE_EMPTY)
    				iGameState				=	GAME_STATE_UPDATE;
    			else
    				iMainState				=	MAIN_STATE_GAMEOVER;
    
    			break;
    		case GAME_STATE_UPDATE:
    			if (++iShapeMoveTick > SHAPE_MOVE_TIME)
    				moveDown();
    
    			break;
    	}
    }
    
    void drawGameMain(HDC hdc)
    {
    	drawMapBG(hdc);
    	drawRects(hdc);
    }
    
    
    /////////////
    //// ETC ////
    /////////////
    void drawMapBG(HDC hdc)
    {
    	RECT rect;
    
    	// Border~
    	rect.left							=	iMapMinX - (MAP_BD_SIZE << 1);
    	rect.right							=	iMapMaxX + (MAP_BD_SIZE << 1);
    	rect.top							=	iMapMinY - (MAP_BD_SIZE << 1);
    	rect.bottom							=	iMapMaxY + (MAP_BD_SIZE << 1);
    
    	FillRect(hdc, &rect, CreateSolidBrush(MAP_COLOR_BD));
    
    	// BG~
    	rect.left							=	iMapMinX - MAP_BD_SIZE;
    	rect.right							=	iMapMaxX + MAP_BD_SIZE;
    	rect.top							=	iMapMinY - MAP_BD_SIZE;
    	rect.bottom							=	iMapMaxY + MAP_BD_SIZE;
    
    	FillRect(hdc, &rect, CreateSolidBrush(MAP_COLOR_BG));
    }
    
    void drawRects(HDC hdc)
    {
    	int r								=	0;
    	int c								=	0;
    	int i								=	0;
    
    	for (r=0; r<MAP_ROW; ++r)
    	{
    		for (c=0; c<MAP_COL; ++c)
    		{
    			if (map[r][c] != SHAPE_TYPE_EMPTY)
    				drawRect(hdc, map[r][c], MAP_X+c*(RECT_W+RECT_SPACE), MAP_Y+r*(RECT_H+RECT_SPACE));
    		}
    	}
    
    	for (i=0; i<SHAPE_RECT_NUM; ++i)
    	{
    		r								=	iShapeRow + shapesRow[iShapeType-1][iShapeState][i];
    		c								=	iShapeCol + shapesCol[iShapeType-1][iShapeState][i];
    
    		drawRect(hdc, iShapeType, MAP_X+c*(RECT_W+RECT_SPACE), MAP_Y+r*(RECT_H+RECT_SPACE));
    	}
    }
    
    void drawRect(HDC hdc, int type, int x, int y)
    {
    	RECT rect;
    
    	rect.left							=	x;
    	rect.right							=	x + 1 + RECT_W;
    	rect.top							=	y;
    	rect.bottom							=	y + 1 + RECT_H;
    
    	switch (type)
    	{
    		case SHAPE_TYPE_1:
    			FillRect(hdc, &rect, CreateSolidBrush(SHAPE_COLOR_1));
    			break;
    		case SHAPE_TYPE_2:
    			FillRect(hdc, &rect, CreateSolidBrush(SHAPE_COLOR_2));
    			break;
    		case SHAPE_TYPE_3:
    			FillRect(hdc, &rect, CreateSolidBrush(SHAPE_COLOR_3));
    			break;
    		case SHAPE_TYPE_4:
    			FillRect(hdc, &rect, CreateSolidBrush(SHAPE_COLOR_4));
    			break;
    		case SHAPE_TYPE_5:
    			FillRect(hdc, &rect, CreateSolidBrush(SHAPE_COLOR_5));
    			break;
    		case SHAPE_TYPE_6:
    			FillRect(hdc, &rect, CreateSolidBrush(SHAPE_COLOR_6));
    			break;
    		case SHAPE_TYPE_7:
    			FillRect(hdc, &rect, CreateSolidBrush(SHAPE_COLOR_7));
    			break;
    		case SHAPE_TYPE_DISABLED:
    			FillRect(hdc, &rect, CreateSolidBrush(SHAPE_COLOR_DISABLED));
    			break;
    	}
    }
    
    int createShape()
    {
    	int type, i, row, col, result;
    
    	srand(GetTickCount());
    	type = (rand() &#37; SHAPE_TYPE_NUM) + 1;
    
    	for (i=0; i<SHAPE_STATE_NUM; ++i)
    	{
    		row								=	SHAPE_INIT_ROW + shapesRow[type-1][0][i];
    		col								=	SHAPE_INIT_COL + shapesCol[type-1][0][i];
    
    		if (map[row][col] != SHAPE_TYPE_EMPTY)
    			result						=	0;
    		else
    			result						=	type;
    	}
    
    	return result;
    }
    
    void moveLeft()
    {
    	int r, c, i;
    	int iEmpty							=	0;
    
    	for (i=0; i<SHAPE_RECT_NUM; ++i)
    	{
    		r								=	iShapeRow + shapesRow[iShapeType-1][iShapeState][i];
    		c								=	iShapeCol + shapesCol[iShapeType-1][iShapeState][i];
    
    		if (c > 0 && map[r][c-1] == SHAPE_TYPE_EMPTY)
    			++iEmpty;
    		else
    			break;
    	}
    
    	if (iEmpty >= SHAPE_RECT_NUM)
    		--iShapeCol;
    }
    
    void moveRight()
    {
    	int r, c, i;
    	int iEmpty							=	0;
    
    	for (i=0; i<SHAPE_RECT_NUM; ++i)
    	{
    		r								=	iShapeRow + shapesRow[iShapeType-1][iShapeState][i];
    		c								=	iShapeCol + shapesCol[iShapeType-1][iShapeState][i];
    
    		if (c < MAP_COL - 1 && map[r][c+1] == SHAPE_TYPE_EMPTY)
    			++iEmpty;
    		else
    			break;
    	}
    
    	if (iEmpty >= SHAPE_RECT_NUM)
    		++iShapeCol;
    }
    
    void moveDown()
    {
    	int r, c, i;
    	int iEmpty							=	0;
    
    	for (i=0; i<SHAPE_RECT_NUM; ++i)
    	{
    		r								=	iShapeRow + shapesRow[iShapeType-1][iShapeState][i];
    		c								=	iShapeCol + shapesCol[iShapeType-1][iShapeState][i];
    
    		if (r < MAP_ROW - 1 && map[r+1][c] == SHAPE_TYPE_EMPTY)
    			++iEmpty;
    		else
    			break;
    	}
    
    	if (iEmpty >= SHAPE_RECT_NUM)
    	{
    		++iShapeRow;
    		iShapeMoveTick					=	0;
    	}
    	else
    	{
    		concrete();
    		check();
    		iGameState						=	GAME_STATE_CREATE;
    	}
    }
    
    void dropDown()
    {
    //temp
    	//
    }
    
    void rotate(int dir)
    {
    	int r, c, i;
    	int iEmpty							=	0;
    	int iShapeNextState					=	iShapeState;
    
    	switch (dir)
    	{
    		case DIR_LEFT:
    			iShapeNextState				=	iShapeState < SHAPE_RECT_NUM - 1 ? iShapeState + 1 : 0;
    			break;
    		case DIR_RIGHT:
    			iShapeNextState				=	iShapeState > 0 ? iShapeState - 1 : SHAPE_RECT_NUM - 1;
    			break;
    	}
    
    	for (i=0; i<SHAPE_RECT_NUM; ++i)
    	{
    		r								=	iShapeRow + shapesRow[iShapeType-1][iShapeNextState][i];
    		c								=	iShapeCol + shapesCol[iShapeType-1][iShapeNextState][i];
    
    		if (r >= 0 && 
    			r < MAP_ROW && 
    			c >= 0 &&
    			c < MAP_COL && 
    			map[r][c+1] == SHAPE_TYPE_EMPTY
    			)
    			++iEmpty;
    	}
    
    	if (iEmpty >= SHAPE_RECT_NUM)
    		iShapeState						=	iShapeNextState;
    }
    
    void concrete()
    {
    	int r, c, i;
    
    	for (i=0; i<SHAPE_RECT_NUM; ++i)
    	{
    		r								=	iShapeRow + shapesRow[iShapeType-1][iShapeState][i];
    		c								=	iShapeCol + shapesCol[iShapeType-1][iShapeState][i];
    
    		if (map[r][c] == SHAPE_TYPE_EMPTY)
    			map[r][c]					=	SHAPE_TYPE_DISABLED;
    	}
    }
    
    void check()
    {
    	int r, c, i, j, iCell;
    
    	for (r=0; r<MAP_ROW; ++r)
    	{
    		iCell							=	0;
    
    		for (c=0; c<MAP_COL; ++c)
    		{
    			if (map[r][c] == SHAPE_TYPE_DISABLED)
    				++iCell;
    			else
    				break;
    		}
    
    		if (iCell >= MAP_COL)
    		{
    			for (i=r; i>=0; --i)
    			{
    				for (j=0; j<MAP_COL; ++j)
    					map[i][j]			=	i != 0 ? map[i-1][j] : SHAPE_TYPE_EMPTY;
    			}
    		}
    	}
    }
    it is a simple tetris game. any help ?
    Never end on learning~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C or C++
    By AcerN30 in forum Game Programming
    Replies: 41
    Last Post: 05-30-2008, 06:57 PM
  2. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 02:39 PM
  3. no errors but still errors
    By Megatron in forum Windows Programming
    Replies: 7
    Last Post: 01-12-2003, 11:21 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Invoking MSWord
    By Donn in forum C Programming
    Replies: 21
    Last Post: 09-08-2001, 04:08 PM