Advertisement

[Windows programming help] Can some of you guys help me out with this?

Started by June 12, 2017 08:56 AM
0 comments, last by GibbonThatCodes 7 years, 5 months ago

#include <Windows.h>
#include <windef.h>
#include <winuser.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define APPTITLE "Game Loop"

//function prototypes
LRESULT CALLBACK WinProc(HWND, UINT, WPARAM, LPARAM);
ATOM MyRegisterClass(HINSTANCE);
BOOL InitInstance(HINSTANCE, int);
void DrawBitmap(HDC, char*, int, int);
void GameInit();
void GameRun();
void GameEnd();

//Local Variables
HWND globalHwnd;
HDC globalHdc;

//Event callback function
LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	globalHwnd = hWnd;
	globalHdc = GetDC(hWnd);

	switch (message)
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	}
	return DefWindowProc(hWnd, message, wParam, lParam);
}

//helper function to set up windows properties
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	// create struucture
	WNDCLASSEX wc;
	wc.cbSize = sizeof(WNDCLASSEX);

	//fill the structure with information
	wc.style			= CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc		= (WNDPROC)WinProc;
	wc.cbClsExtra		= 0;
	wc.cbWndExtra		= 0;
	wc.hInstance		= hInstance;
	wc.hIcon			= NULL;
	wc.hCursor			= LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground	= (HBRUSH)GetStockObject(WHITE_BRUSH);
	wc.lpszMenuName		= NULL;
	wc.lpszClassName	= APPTITLE;
	wc.hIconSm			= NULL;

	//set up the window with the class info
	return RegisterClassEx(&wc);
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
	HWND hWnd;

	//create New window
	hWnd = CreateWindow(
		APPTITLE,
		APPTITLE,
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		1080,
		720,
		NULL,
		NULL,
		hInstance,
		NULL);

	//if error
	if (!hWnd)
		return FALSE;

	//display the window
	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);

	return TRUE;
}

//Entry point for a windows program
int WINAPI WinMain(HINSTANCE hInstance,
	HINSTANCE hPrevInstance,
	LPSTR lpCmdLine,
	int    nCmdShow)
{
	int done = 0;
	MSG msg;

	MyRegisterClass(hInstance);

	if (!InitInstance(hInstance, nCmdShow))
	{
		return FALSE;
	}

	GameInit();

	// main message loop
	while (!done)
	{
		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			//look for quit message
			if (msg.message == WM_QUIT)
			{
				done = 1;
			}

			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}

		//process the Gameloop
		GameRun();
	}

	//cleanup
	GameEnd();

	return msg.wParam;
}

void GameInit()
{
	//Initialize the game
	//load bitmaps, mesh, textures, sounds etc

	//initialize the random number generator
	srand(time(NULL));
}

void GameRun()
{
	//this is called once every frame
	//don't include yourn own loop here

	int x = 0, y = 0;
	RECT rect;
	GetClientRect(globalHwnd, &rect);

	if (rect.right > 0)
	{
		x = rand() % (rect.right - rect.left);
		y = rand() % (rect.bottom - rect.top);
		DrawBitmap(globalHdc, "c.bmp", x, y);
	}
}

void GameEnd()
{

}

void DrawBitmap(HDC hdcDest, char *filename, int x, int y)
{
	HBITMAP image;
	BITMAP bm;
	HDC hdcMem;

	image = LoadImage(0, "c.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);

	GetObject(image, sizeof(BITMAP), &bm);

	hdcMem = CreateCompatibleDC(globalHdc);
	SelectObject(hdcMem, image);

	BitBlt(
		globalHdc,
		x, y,
		bm.bmWidth,
		bm.bmHeight,
		hdcMem,
		0, 0,
		SRCCOPY);

	DeleteDC(hdcMem);
	DeleteObject((HBITMAP)image);
}

Severity Code Description Project File Line Suppression State

Warning C4244 'argument': conversion from 'time_t' to 'unsigned int', possible loss of data MyDirectX c:\users\conta\documents\one\personalprojects\mydirectx book\main.cpp 140
Error C2440 '=': cannot convert from 'HANDLE' to 'HBITMAP' MyDirectX c:\users\conta\documents\one\personalprojects\mydirectx book\main.cpp 171
Error (active) a value of type "HANDLE" cannot be assigned to an entity of type "HBITMAP" MyDirectX c:\Users\conta\Documents\ONE\PersonalProjects\MyDirectX Book\main.cpp 171

I Just wrote this code practicing game loops and basically copied everything the reference book said but there's a red underline on the equals sign and the errors are as follows. Can somebody help?

Well, the LoadImage function isnt returning the HBITMAP type that the image variable is ... you can just cast the return to HBITMAP though with a lovely (HBITMAP) placed in front of LoadImage

This topic is closed to new replies.

Advertisement