Go Back   Flash Flash Revolution > General Discussion > Critical Thinking > Homework & Help
Register FAQ Community Calendar Today's Posts Search

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Old 08-31-2015, 02:49 AM   #1
sickufully
Sir Krisk
FFR Veteran
 
sickufully's Avatar
 
Join Date: Dec 2007
Location: Vic, Australia
Posts: 930
Default [University - C++ Programming] Help With Assignment (Very Basic Stuff)

Alright so let me begin with saying that I've been studying C++ for about the past 6 weeks (only just started my course) and the assignment I've been slapped with is comparable to learning Ancient Latin in French (that's how I see it anyway). I literally don't even know where to start.

Since I'm not allowed to just ask for answers, I'll be asking for the next closest thing that won't get this thread locked. Any help or advice will aid me! And if you can give advice/help, please explain it like you we're talking to a dummy (cause I kinda feel that way atm). I'm also willing to talk on Skype or anything other chatting thingy cause I'm getting desperate at this stage. I spent 4 hours the other night trying to figure out the first question (which is 'What is the data type GUI_BUFFER?') and I'm pretty sure my answer is wrong.

Alright so here's the code that I've been supplied with. I will ask questions at the bottom of this post. Most of them will probably be simple to anyone with reasonable knowledge of C++. I guess I'll just post a few questions now and if people can help, I'll ask more questions that I get stuck on.

Also, sorry about the poor quality of the code layout, it's not as pretty as it looks in Visual Studio.

Code:
/******************************************************************************
* Program Description
* File: tilemap.cpp
* Application demonstrating the use of 2D arrays to represent tile-based game
* environments. Also demonstrates limited use of the Windows Console API.
******************************************************************************
*/


#include "tilemap.h"

// Declare map height and width parameters
const unsigned int MAP_HEIGHT = 20;
const unsigned int MAP_WIDTH = 30;

// Declare map elements using an enumeration
enum entity_labels	{
	EMPTY = 0,
	WALL
};
typedef entity_labels	ENTITY;

// Define an array of ASCII codes to use for visualising the map
const int TOKEN[2] = {
	32,		// EMPTY
	178		// WALL
};

// create type aliases for console and map array buffers
using GUI_BUFFER = CHAR_INFO[MAP_HEIGHT][MAP_WIDTH];
using MAP_BUFFER = ENTITY[MAP_HEIGHT][MAP_WIDTH];


//Declare application subroutines
void InitConsole(unsigned int, unsigned int);
void ClearConsole(HANDLE hStdOut);
WORD GetKey();
void DrawMap(MAP_BUFFER & rMap);



/** Function: main
  * Description:
  *		 Application entry point
  */
int main()
{

	/**************************************************************************
	  * Initialise the standard output console
	  */		
	HANDLE	hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
	if (hStdOut != INVALID_HANDLE_VALUE)
	{
		ClearConsole(hStdOut);

		// Set window title
		SetConsoleTitle(TEXT("Tile Map Demo"));

		// Set window size
		SMALL_RECT srWindowRect;
		srWindowRect.Left = 0;
		srWindowRect.Top = 0;
		srWindowRect.Bottom = srWindowRect.Top + MAP_HEIGHT;
		srWindowRect.Right = srWindowRect.Left + MAP_WIDTH;

		SetConsoleWindowInfo(hStdOut, true, &srWindowRect);

		// Set screen buffer size
		COORD cWindowSize = { MAP_WIDTH, MAP_HEIGHT };
		SetConsoleScreenBufferSize(hStdOut, cWindowSize);
	}
	/*************************************************************************/


	/*************************************************************************
	* Initialise the tile map with appropriate ENTITY values
	*/
	MAP_BUFFER		tileMap;
	
	for (unsigned int row = 0; row < MAP_HEIGHT; row++)
	{
		for (unsigned int col = 0; col < MAP_WIDTH; col++)
		{
			tileMap[row][col] = EMPTY;
		}
	}
	/*************************************************************************/



	/***************************************************************************
	* Execute the main application loop
	*/
	bool bExit = false;

	do
	{
		// Get user input (non-blocking) if it exists
		WORD wKeyCode = GetKey();

		// Process input to update application state
		switch (wKeyCode)
		{
			case VK_ESCAPE:
				bExit = true;
		};

		// Render the map state to the console
		DrawMap(tileMap);

	} while (!bExit);


	return 0;
}


/******************************************************************************
  *                        Define application subroutines                     *
  *****************************************************************************/

/** Function: ClearConsole
  * Inputs:
  *		hConsole : HANDLE	- handle to console to be cleared 
  * Description:
  *     Fill console internal buffer with blank characters
  */
void ClearConsole(HANDLE hConsole)
{
	CONSOLE_SCREEN_BUFFER_INFO csbiConsoleInfo;
	GetConsoleScreenBufferInfo(hConsole, &csbiConsoleInfo);
	SMALL_RECT srcWindow = csbiConsoleInfo.srWindow;
	DWORD dNumChars = (srcWindow.Right - srcWindow.Left + 1)*(srcWindow.Bottom - srcWindow.Top + 1);
	COORD cStart;
	cStart.X = srcWindow.Left;
	cStart.Y = srcWindow.Top;
	FillConsoleOutputCharacter(hConsole, ' ', dNumChars, cStart, &dNumChars);
}


/** Function: DrawMap
  * Inputs:
  *		rMap : MAP_BUFFER	- array of ENTITY map elements
  * Description:
  *		Use the ENTITY values in rMap to determine ASCII characters to be drawn to standard output console
  */
void	DrawMap(MAP_BUFFER & rMap)
{
	static GUI_BUFFER	rScreenBuffer = {};

	// Set the ScreenBuffer characters and attributes given the ENTITY values in rMap
	for (unsigned int row = 0; row < MAP_HEIGHT; row++)
	{
		for (unsigned int col = 0; col < MAP_WIDTH; col++)
		{
			ENTITY & rTile = rMap[row][col];
			rScreenBuffer[row][col].Char.AsciiChar = (char)TOKEN[rTile];
			switch (rTile)
			{
			case EMPTY:
				rScreenBuffer[row][col].Attributes = 0;
				break;

			case WALL:
				rScreenBuffer[row][col].Attributes = FOREGROUND_WHITE;
				break;
			};
		}
	}

	// Copy the ScreenBuffer content to the standard output console 
	HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
	if (hStdOut != INVALID_HANDLE_VALUE)
	{
		COORD pos = { 0, 0 };
		COORD size = { MAP_WIDTH, MAP_HEIGHT };

		SMALL_RECT srDrawRegion;
		srDrawRegion.Left = 0;
		srDrawRegion.Top = 0;
		srDrawRegion.Bottom = srDrawRegion.Top + MAP_HEIGHT;
		srDrawRegion.Right = srDrawRegion.Left + MAP_WIDTH;

		WriteConsoleOutput(hStdOut, (CHAR_INFO *)rScreenBuffer, size, pos, &srDrawRegion);
	}
}


/** Function: GetKey
  * Description:
  *		Check console event buffer for keydown events and return keycode of first event found
  *		Does not block - i.e., will return if no events in console event buffer
  */
WORD GetKey()
{
	bool bExit = false;
	WORD wKeyCode = 0;
	HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
	const unsigned int MAX_INPUTS = 128;
	if (hStdIn != INVALID_HANDLE_VALUE)
	{
		INPUT_RECORD irInBuf[MAX_INPUTS];
		DWORD dNumRead = 0;

		ReadConsoleInput(hStdIn, irInBuf, (DWORD)MAX_INPUTS, &dNumRead);
		for (DWORD i = 0; i < dNumRead; i++)
		{
			// Only consider events that are key-down events
			if (irInBuf[i].EventType == KEY_EVENT && irInBuf[i].Event.KeyEvent.bKeyDown)
			{
				// return the keycode of the first key-down event
				wKeyCode = irInBuf[i].Event.KeyEvent.wVirtualKeyCode;
				bExit = true;
				break;
			}
		}
	}
	return wKeyCode;
}
Questions
I'll try and break down to the best of my knowledge each question with what I think the answer is. If I'm close, please tell me and same if I'm completely wrong. You don't have to point out the answer, just point me in the right direction. Any help is appreciated. Willing to give credits to people who give decent help. Many thanks in advance.

Consider the first statement within the definition of the function DrawMap:
static GUI_BUFFER rScreenBuffer = {};

a) What is the data type GUI_BUFFER? Identify where this type is declared and determine the underlying data type. Explain the meaning of the declaration of this type in the context of the using keyword. How is this different to the use of the typedef keyword, as in the declaration of the ENTITY data type?

MY ANSWER - COMPLETED THIS QUESTION. The first 6 words are already confusing me. I thought the data type of GUI_BUFFER was static but I'm really second guessing myself now. I think the answer is void. As for where the type is declared, I'm guessing it's mentioned in the declaration near the top? No idea what the underlying data type is. The rest of the question is just a shamble to me.

b) What is the effect of the keyword static upon the scope and lifetime of the variable rScreenBuffer? Within which memory region would you expect to find this variable allocated? What performance benefit is obtained by making this buffer a static variable?

MY ANSWER - COMPLETED THIS QUESTION. I'm pretty sure static means that variable stays stored in the same location for the entire during of execution, but it's value can change. As for the rest of the question, memory region and performance benefits, I'm completely stumped.

c) What is the data type underlying the MAP_BUFFER array? What are the possible values that this data type can take (and hence the possible values stored in the map)?

MY ANSWER - I've written down so far that it's ENTITY[MAP_HEIGHT][MAP_WIDTH], only because it seems like a repeat of question one. Although I'm not sure of the possible values that this data type can take. I'm assuming that the data type is ints and because of this, they can only hold numerical values. Can someone clarify if I'm heading in the right direction?

d) Within the function DrawMap the MAP_BUFFER parameter is used to determine the values written into the GUI_BUFFER local variable. Discuss briefly the relationship between these data structures by explaining how the contents of an element of the MAP_BUFFER array determines the contents of the GUI_BUFFER array.

MY ANSWER - Don't have one yet.
__________________

Last edited by sickufully; 09-2-2015 at 02:42 AM..
sickufully is offline   Reply With Quote
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 05:55 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright FlashFlashRevolution