• Advertisement
Sign in to follow this  

Unity Trying to compile Greven's tetris clone

This topic is 4432 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

As the title says. His thread seems to be closed so I cannot reply in it. Thus I turn here. I think my error message is fairly basic, but I'm new to Windows programming so I haven't got a clue what it means. This is my main.cpp:
/* FBS = Falling Block Game */

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdlib.h>
#include "bitmapobject.h"

#define WINDOWCLASS "FallingBlockGame"
#define WINDOWTITLE "Falling Block Game"

const int TILESIZE = 16;				// Size of one tile
const int MAPWIDTH = 10;				// 
const int MAPHEIGHT = 30;				//
const int GREYAREA = 8;					// Side bar

enum color {NODRAW,BLACK,GREY,BLUE,RED,GREEN,YELLOW,WHITE,STEEL,PURPLE};

bool GameInit();						// Game initialization function
void GameLoop();						// Where the game actually takes place
void GameDone();						// Clean up! 
void DrawTile(int x, int y, int tile);	// Coordinates & tile type
void DrawMap();							// Draw the whole map.. render function, basically
void NewBlock();						// Create a new block!
void RotateBlock();						// Rotate a block.. if you can!
void Move(int x, int y);				// Coordinates to move.
int CollisionTest(int nx, int ny);		// Test collision of blocks
void RemoveRow(int row);				// Remove a row.. that would be the 'x'.
void NewGame();							// Make a new game!

HINSTANCE hInstMain=NULL;				// Main app handle
HWND hWndMain=NULL;						// Main window handle

int Map[MAPWIDTH][MAPHEIGHT+1];			//the game map!

struct Piece {
	int size[4][4];
	int x;
	int y;
};

Piece sPrePiece;						// Preview piece.
Piece sPiece;							// The 's' prefixes indicate this is a 'structure'

DWORD start_time;						// Used in timing
bool GAMESTARTED = false;				// Used by NewBlock()

BitMapObject bmoMap;					// Map for the program
BitMapObject bmoBlocks;					// Block images

LRESULT CALLBACK TheWindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
	//which message did we get?
	switch(uMsg) {
		case WM_KEYDOWN: {
			if(wParam==VK_ESCAPE) { //check for escape key
				DestroyWindow(hWndMain);
				return(0);//handled message
			}
			else if(wParam==VK_DOWN) { //check for down arrow key			
				Move(0,1);
				return(0);//handled message
			}
			else if(wParam==VK_UP) { //check for up arrow key			
				RotateBlock();
				return(0);//handled message
			}
			else if(wParam==VK_LEFT) { //check for left arrow key			
				Move(-1,0);
				return(0);//handled message
			}
			else if(wParam==VK_RIGHT) { //check for right arrow key			
				Move(1,0);
				return(0);//handled message
			}
		} break;
		case WM_DESTROY: { //the window is being destroyed
			//tell the application we are quitting
			PostQuitMessage(0);
			//handled message, so return 0
			return(0);
		} break;
		case WM_PAINT: { //the window needs repainting		
			//a variable needed for painting information
			PAINTSTRUCT ps;			
			//start painting
			HDC hdc=BeginPaint(hwnd,&ps);
			//redraw the map
			BitBlt(hdc,0,0,TILESIZE*MAPWIDTH+TILESIZE*GREY,TILESIZE*MAPHEIGHT,bmoMap,0,0,SRCCOPY);
			//end painting
			EndPaint(hwnd,&ps);
					
			//handled message, so return 0
			return(0);
		}break;
	}

	//pass along any other message to default message handler
	return(DefWindowProc(hwnd,uMsg,wParam,lParam));
}

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
	//assign instance to global variable
	hInstMain=hInstance;

	//create window class
	WNDCLASSEX wcx;

	//set the size of the structure
	wcx.cbSize=sizeof(WNDCLASSEX);

	//class style
	wcx.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;

	//window procedure
	wcx.lpfnWndProc=TheWindowProc;

	//class extra
	wcx.cbClsExtra=0;

	//window extra
	wcx.cbWndExtra=0;

	//application handle
	wcx.hInstance=hInstMain;

	//icon
	wcx.hIcon=LoadIcon(NULL,IDI_APPLICATION);

	//cursor
	wcx.hCursor=LoadCursor(NULL,IDC_ARROW);

	//background color
	wcx.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);

	//menu
	wcx.lpszMenuName=NULL;

	//class name
	wcx.lpszClassName=WINDOWCLASS;

	//small icon
	wcx.hIconSm=NULL;

	//register the window class, return 0 if not successful
	if(!RegisterClassEx(&wcx)) return(0);

	//create main window
	hWndMain=CreateWindowEx(0,WINDOWCLASS,WINDOWTITLE, WS_BORDER | WS_SYSMENU | WS_CAPTION| WS_VISIBLE,0,0,320,240,NULL,NULL,hInstMain,NULL);

	//error check
	if(!hWndMain) return(0);

	//if program initialization failed, then return with 0
	if(!GameInit()) return(0);

	//message structure
	MSG msg;

	//message pump
	for( ; ; )	
	{
		//look for a message
		if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
		{
			//there is a message

			//check that we arent quitting
			if(msg.message==WM_QUIT) break;
			
			//translate message
			TranslateMessage(&msg);

			//dispatch message
			DispatchMessage(&msg);
		}

		//run main game loop
		GameLoop();
		
	}
	
	//clean up program data
	GameDone();

	//return the wparam from the WM_QUIT message
	return(msg.wParam);
}

bool GameInit()
{
	//set the client area size
	RECT rcTemp;
	SetRect(&rcTemp,0,0,MAPWIDTH*TILESIZE+TILESIZE*GREY,MAPHEIGHT*TILESIZE);//160x480 client area
	AdjustWindowRect(&rcTemp,WS_BORDER | WS_SYSMENU | WS_CAPTION| WS_VISIBLE,FALSE);//adjust the window size based on desired client area
	SetWindowPos(hWndMain,NULL,0,0,rcTemp.right-rcTemp.left,rcTemp.bottom-rcTemp.top,SWP_NOMOVE);//set the window width and height

	//create map image
	HDC hdc=GetDC(hWndMain);
	bmoMap.Create(hdc,MAPWIDTH*TILESIZE+TILESIZE*GREY,MAPHEIGHT*TILESIZE);
	FillRect(bmoMap,&rcTemp,(HBRUSH)GetStockObject(BLACK_BRUSH));
	ReleaseDC(hWndMain,hdc);

	bmoBlocks.Load(NULL,"blocks.bmp");
	NewGame();

	return(true);//return success
}

void GameDone()
{
	//clean up code goes here
}

void GameLoop()
{
	if( (GetTickCount() - start_time) > 1000)
	{
		Move(0,1);
		start_time=GetTickCount();
	}

}

void NewGame()
{
	start_time=GetTickCount();
	GAMESTARTED=false;
	color black = BLACK;
	color grey = GREY;

	//start out the map
	for(int x = 0;x < MAPWIDTH;x++)
	{
		for(int y = 0;y < MAPHEIGHT+1;y++)
		{
			if(y == MAPHEIGHT) //makes Y-collision easier.
				Map[x][y] = grey;
			else
				Map[x][y] = black;
		}
	}
	NewBlock();	
	DrawMap();
}

void DrawTile(int x,int y,int tile)//put a tile
{
	//mask first
	BitBlt(bmoMap,x*TILESIZE,y*TILESIZE,TILESIZE,TILESIZE,bmoBlocks,tile*TILESIZE,TILESIZE,SRCAND);
	//then image
	BitBlt(bmoMap,x*TILESIZE,y*TILESIZE,TILESIZE,TILESIZE,bmoBlocks,tile*TILESIZE,0,SRCPAINT);
}

void DrawMap()//draw screen
{
	int xmy, ymx;
	color grey = GREY, nodraw = NODRAW;

	//place the toolbar
	for(xmy=MAPWIDTH; xmy< MAPWIDTH+GREYAREA; xmy++)
		for(ymx=0; ymx< MAPHEIGHT; ymx++)
			DrawTile(xmy, ymx, grey);

	//draw preview block
	for(xmy=0; xmy<4; xmy++)
		for(ymx=0; ymx<4; ymx++)
			if(sPrePiece.size[xmy][ymx] != nodraw)
				DrawTile(sPrePiece.x+xmy, sPrePiece.y+ymx, sPrePiece.size[xmy][ymx]);
	
	//draw the map
	//loop through the positions
	for(xmy=0;xmy< MAPWIDTH;xmy++)
		for(ymx=0;ymx< MAPHEIGHT;ymx++)
				DrawTile(xmy,ymx,Map[xmy][ymx]);

	//draw moving block
	for(xmy=0; xmy<4; xmy++)
		for(ymx=0; ymx<4; ymx++)
			if(sPiece.size[xmy][ymx] != nodraw)
				DrawTile(sPiece.x+xmy, sPiece.y+ymx, sPiece.size[xmy][ymx]);

	//invalidate the window rect
	InvalidateRect(hWndMain,NULL,FALSE);
}

void NewBlock()
{
	int newblock;
	int i,j;
	color nodraw = NODRAW, col;
	//  0   1   2   3   4    5   6    
	//   X                             These
	//   X   XX   X  XX   XX  XX   XX  are
	//   X   XX  XXX  XX XX    X   X   block
	//   X                     X   X   types

	//begin game! make generate a block and then one in preview.

	srand(GetTickCount());

	//initialize the piece to all blank.
	for(i=0; i<4; i++)
		for(j=0; j<4; j++)
			sPiece.size[ i ][j]=nodraw;

	sPiece.x=MAPWIDTH/2-2;
	sPiece.y=-1;

	//let's see if the game's started yet
	if(GAMESTARTED == false)
	{
		//guess not..  
		//Generate a piece right off.
		//From now on, use previous preview block.
		GAMESTARTED=true;

		newblock=rand()%7;

		switch (newblock)
		{
		case 0: //Tower!
			{
				col = RED;
				sPiece.size[1][0]=col;
				sPiece.size[1][1]=col;
				sPiece.size[1][2]=col;
				sPiece.size[1][3]=col;
				sPiece.y=0;
			}break;
		case 1: //Box!
			{
				col = BLUE;
				sPiece.size[1][1]=col;
				sPiece.size[1][2]=col;
				sPiece.size[2][1]=col;
				sPiece.size[2][2]=col;
			}break;
		case 2: //Pyramid!
			{
				col = STEEL;
				sPiece.size[1][1]=col;
				sPiece.size[0][2]=col;
				sPiece.size[1][2]=col;
				sPiece.size[2][2]=col;
			}break;
		case 3://Left Leaner
			{
				col = YELLOW;
				sPiece.size[0][1]=col;
				sPiece.size[1][1]=col;
				sPiece.size[1][2]=col;
				sPiece.size[2][2]=col;
			}break;
		case 4://Right Leaner
			{
				col = GREEN;
				sPiece.size[2][1]=col;
				sPiece.size[1][1]=col;
				sPiece.size[1][2]=col;
				sPiece.size[0][2]=col;
			}break;
		case 5://Left Knight
			{
				col = WHITE;
				sPiece.size[1][1]=col;
				sPiece.size[2][1]=col;
				sPiece.size[2][2]=col;
				sPiece.size[2][3]=col;
			}break;
		case 6://Right Knight
			{
				col = PURPLE;
				sPiece.size[2][1]=col;
				sPiece.size[1][1]=col;
				sPiece.size[1][2]=col;
				sPiece.size[1][3]=col;
			}break;
		}
	}
	else
	{
		for(i=0; i<4; i++)
			for(j=0; j<4; j++)
				sPiece.size[ i ][j]=sPrePiece.size[ i ][j];

	}

	newblock=rand()%7;

	for(i=0; i<4; i++)
		for(j=0; j<4; j++)
			sPrePiece.size[ i ][j]=nodraw;

	sPrePiece.x=MAPWIDTH+GREY/4;
	sPrePiece.y=GREY/4;

	switch (newblock)
	{
		case 0: //Tower!
			{
				col = RED;
				sPrePiece.size[1][0]=col;
				sPrePiece.size[1][1]=col;
				sPrePiece.size[1][2]=col;
				sPrePiece.size[1][3]=col;
			}break;
		case 1: //Box!
			{
				col = BLUE;
				sPrePiece.size[1][1]=col;
				sPrePiece.size[1][2]=col;
				sPrePiece.size[2][1]=col;
				sPrePiece.size[2][2]=col;
			}break;
		case 2: //Pyramid!
			{
				col = STEEL;
				sPrePiece.size[1][1]=col;
				sPrePiece.size[0][2]=col;
				sPrePiece.size[1][2]=col;
				sPrePiece.size[2][2]=col;
			}break;
		case 3://Left Leaner
			{
				col = YELLOW;
				sPrePiece.size[0][1]=col;
				sPrePiece.size[1][1]=col;
				sPrePiece.size[1][2]=col;
				sPrePiece.size[2][2]=col;
			}break;
		case 4://Right Leaner
			{
				col = GREEN;
				sPrePiece.size[2][1]=col;
				sPrePiece.size[1][1]=col;
				sPrePiece.size[1][2]=col;
				sPrePiece.size[0][2]=col;
			}break;
		case 5://Left Knight
			{
				col = WHITE;
				sPrePiece.size[1][1]=col;
				sPrePiece.size[2][1]=col;
				sPrePiece.size[2][2]=col;
				sPrePiece.size[2][3]=col;
			}break;
		case 6://Right Knight
			{
				col = PURPLE;
				sPrePiece.size[2][1]=col;
				sPrePiece.size[1][1]=col;
				sPrePiece.size[1][2]=col;
				sPrePiece.size[1][3]=col;
			}break;
	}

	DrawMap();
}

void RotateBlock()
{
	int i, j, temp[4][4];
	color black = BLACK, nodraw = NODRAW;

	//copy &rotate the piece to the temporary array
	for(i=0; i<4; i++)
		for(j=0; j<4; j++)
			temp[3-j][ i ]=sPiece.size[ i ][j];

	//check collision of the temporary array with map borders
	for(i=0; i<4; i++)
		for(j=0; j<4; j++)
			if(temp[ i ][j] != nodraw)
				if(sPiece.x + i < 0 || sPiece.x + i > MAPWIDTH - 1 ||
					sPiece.y + j < 0 || sPiece.y + j > MAPHEIGHT - 1)
					return;

	//check collision of the temporary array with the blocks on the map
	for(int x=0; x< MAPWIDTH; x++)
		for(int y=0; y< MAPHEIGHT; y++)
			if(x >= sPiece.x && x < sPiece.x + 4)
				if(y >= sPiece.y && y < sPiece.y +4)
					if(Map[x][y] != black)
						if(temp[x - sPiece.x][y - sPiece.y] != nodraw)
							return;

	//end collision check

	//successful!  copy the rotated temporary array to the original piece
	for(i=0; i<4; i++)
		for(j=0; j<4; j++)
			sPiece.size[ i ][j]=temp[ i ][j];
	
	DrawMap();

	return;
}

void Move(int x, int y)
{
	color nodraw = NODRAW, black = BLACK;
	if(CollisionTest(x, y))
	{
		if(y == 1)
		{
			if(sPiece.y<1)
			{
				//you lose!  new game.
				NewGame();
			}
			else
			{
				bool killblock=false;
				int i,j;
				//new block time! add this one to the list!
				for(i=0; i<4; i++)
					for(j=0; j<4; j++)
						if(sPiece.size[ i ][j] != nodraw)
							Map[sPiece.x+i][sPiece.y+j] = sPiece.size[ i ][j];

				//check for cleared row!
				for(j=0; j< MAPHEIGHT; j++)
				{
					bool filled=true;
					for(i=0; i< MAPWIDTH; i++)
						if(Map[ i ][j] == black)
							filled=false;

					if(filled)
					{
						RemoveRow(j);
						killblock=true;
					}
				}

				if(killblock)
				{
					for(i=0; i<4; i++)
						for(j=0; j<4; j++)
							sPiece.size[ i ][j]=nodraw;
				}
				NewBlock();
			}
		}

	}
	else
	{
		sPiece.x+=x;
		sPiece.y+=y;
	}

	DrawMap();
}

int CollisionTest(int nx, int ny)
{
	color nodraw = NODRAW, black = BLACK;
	int newx=sPiece.x+nx;
	int newy=sPiece.y+ny;

	int i,j,x,y;

	for(i=0; i< 4; i++)
		for(j=0; j< 4; j++)
			if(sPiece.size[ i ][j] != nodraw)
				if(newx + i < 0 || newx + i > MAPWIDTH - 1 ||
					newy + j < 0 || newy + j > MAPHEIGHT - 1)
					return 1;

	for(x=0; x< MAPWIDTH; x++)
		for(y=0; y< MAPHEIGHT; y++)
			if(x >= newx && x < newx + 4)
				if(y >= newy && y < newy +4)
					if(Map[x][y] != black)
						if(sPiece.size[x - newx][y - newy] != nodraw)
							return 1;
	return 0;
}

void RemoveRow(int row)
{
	int x,y;
	int counter=0;

	for(x=0; x< MAPWIDTH; x++)
		for(y=row; y>0; y--)
			Map[x][y]=Map[x][y-1];

}


and I get these errors and warning:
------ Build started: Project: FBS, Configuration: Debug Win32 ------
Compiling...
main.cpp
c:\documents and settings\magnus olsson\mina dokument\egna projekt\fbs\fbs\fbs\main.cpp(140) : error C2440: '=' : cannot convert from 'const char [17]' to 'LPCWSTR'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
c:\documents and settings\magnus olsson\mina dokument\egna projekt\fbs\fbs\fbs\main.cpp(149) : error C2664: 'CreateWindowExW' : cannot convert parameter 2 from 'const char [17]' to 'LPCWSTR'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
c:\documents and settings\magnus olsson\mina dokument\egna projekt\fbs\fbs\fbs\main.cpp(187) : warning C4244: 'return' : conversion from 'WPARAM' to 'int', possible loss of data
c:\documents and settings\magnus olsson\mina dokument\egna projekt\fbs\fbs\fbs\main.cpp(204) : error C2664: 'BitMapObject::Load' : cannot convert parameter 2 from 'const char [11]' to 'LPCTSTR'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Build log was saved at "file://c:\Documents and Settings\Magnus Olsson\Mina dokument\Egna projekt\FBS\FBS\FBS\Debug\BuildLog.htm"
FBS - 3 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I'm compiling on MS Visual Studio 8 Express Edition (if that helps). [Edited by - MdaG on January 2, 2006 6:14:45 PM]

Share this post


Link to post
Share on other sites
Advertisement
You seem to be in unicode mode. Check out the project settings and see if you can change to a normal multibyte character set instead.

Share this post


Link to post
Share on other sites
I did and it built without a problem, thanks! :)
One question though: How did you know that that was the problem?

Share this post


Link to post
Share on other sites
Quote:
Original post by MdaG
1) How did you know that that was the problem?
The end of the Win32 functions had a trailing W (as opposed to A), which stands for the Wide character set (it's also where the W in LPCWSTR comes from). And you were just sending regular 8-bit characters.

Normally you wouldn't write these manually, indeed the source invokes CreateWindowEx rather than CreateWindowExW, instead the short names are automatically expanded (through a bit of macro magic) to the correct versions.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Where in the settings do I turn on MBCS? I have looked and search google but I cannot find it.

Help please!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By bryandalo
      Good day,

      I just wanted to share our casual game that is available for android.

      Description: Fight your way from the ravenous plant monster for survival through flips. The rules are simple, drag and release your phone screen. Improve your skills and show it to your friends with the games quirky ranks. Select an array of characters using the orb you acquire throughout the game.

      Download: https://play.google.com/store/apps/details?id=com.HellmodeGames.FlipEscape&hl=en
       
      Trailer: 
       
    • By Manuel Berger
      Hello fellow devs!
      Once again I started working on an 2D adventure game and right now I'm doing the character-movement/animation. I'm not a big math guy and I was happy about my solution, but soon I realized that it's flawed.
      My player has 5 walking-animations, mirrored for the left side: up, upright, right, downright, down. With the atan2 function I get the angle between player and destination. To get an index from 0 to 4, I divide PI by 5 and see how many times it goes into the player-destination angle.

      In Pseudo-Code:
      angle = atan2(destination.x - player.x, destination.y - player.y) //swapped y and x to get mirrored angle around the y axis
      index = (int) (angle / (PI / 5));
      PlayAnimation(index); //0 = up, 1 = up_right, 2 = right, 3 = down_right, 4 = down

      Besides the fact that when angle is equal to PI it produces an index of 5, this works like a charm. Or at least I thought so at first. When I tested it, I realized that the up and down animation is playing more often than the others, which is pretty logical, since they have double the angle.

      What I'm trying to achieve is something like this, but with equal angles, so that up and down has the same range as all other directions.

      I can't get my head around it. Any suggestions? Is the whole approach doomed?

      Thank you in advance for any input!
       
    • By khawk
      Watch the latest from Unity.
       
    • By GytisDev
      Hello,
      without going into any details I am looking for any articles or blogs or advice about city building and RTS games in general. I tried to search for these on my own, but would like to see your input also. I want to make a very simple version of a game like Banished or Kingdoms and Castles,  where I would be able to place like two types of buildings, make farms and cut trees for resources while controlling a single worker. I have some problem understanding how these games works in the back-end: how various data can be stored about the map and objects, how grids works, implementing work system (like a little cube (human) walks to a tree and cuts it) and so on. I am also pretty confident in my programming capabilities for such a game. Sorry if I make any mistakes, English is not my native language.
      Thank you in advance.
    • By Ovicior
      Hey,
      So I'm currently working on a rogue-like top-down game that features melee combat. Getting basic weapon stats like power, weight, and range is not a problem. I am, however, having a problem with coming up with a flexible and dynamic system to allow me to quickly create unique effects for the weapons. I want to essentially create a sort of API that is called when appropriate and gives whatever information is necessary (For example, I could opt to use methods called OnPlayerHit() or IfPlayerBleeding() to implement behavior for each weapon). The issue is, I've never actually made a system as flexible as this.
      My current idea is to make a base abstract weapon class, and then have calls to all the methods when appropriate in there (OnPlayerHit() would be called whenever the player's health is subtracted from, for example). This would involve creating a sub-class for every weapon type and overriding each method to make sure the behavior works appropriately. This does not feel very efficient or clean at all. I was thinking of using interfaces to allow for the implementation of whatever "event" is needed (such as having an interface for OnPlayerAttack(), which would force the creation of a method that is called whenever the player attacks something).
       
      Here's a couple unique weapon ideas I have:
      Explosion sword: Create explosion in attack direction.
      Cold sword: Chance to freeze enemies when they are hit.
      Electric sword: On attack, electricity chains damage to nearby enemies.
       
      I'm basically trying to create a sort of API that'll allow me to easily inherit from a base weapon class and add additional behaviors somehow. One thing to know is that I'm on Unity, and swapping the weapon object's weapon component whenever the weapon changes is not at all a good idea. I need some way to contain all this varying data in one Unity component that can contain a Weapon field to hold all this data. Any ideas?
       
      I'm currently considering having a WeaponController class that can contain a Weapon class, which calls all the methods I use to create unique effects in the weapon (Such as OnPlayerAttack()) when appropriate.
  • Advertisement