I don't know where to begin

I will keep this as short as I can. I'm trying to make a simple game to demonstrate AI. I am using a directX framework from PlanetChili, and this is my first time with such a project. I created my own class for Ship, which would be used primarily (or at least initially) for the player's ship. I wanted to have the object itself have a function to draw the ship on the window. I attempted to use the D3Dgraphics class used by the rest of the program, but the program crashes. I cut out all of the irrelevant stuff, like variable and unrelated function declaration, but if you want to have the entire project I would be happy to supply it.

Forgive me if I violated any of your protocols here; I am new to the site and unfamiliar with standard etiquette. Below is the important parts of the code, and I will paste the error message at the end of the post.

Thank you for your time and consideration.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//Ship.h
#pragma once

#include "D3DGraphics.h"
#include "Keyboard.h"
#include "Game.h"
#include <vector>
#define SIZE 25
#define VMAX 10
#define PI 3.14159265

class Ship{
private:
	D3DGraphics gfx;
public:
	Ship::Ship(HWND hWnd); //Default Constructor
};

1
2
3
4
5
6
7
8
9
10
//Ship.cpp
#include "Ship.h"
#include "Game.h"
#include <math.h>

//Constructor
Ship::Ship(HWND hWnd)
:	gfx ( hWnd )
{
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//Game.h
#pragma once

#include "D3DGraphics.h"
#include "Keyboard.h"
#include "Ship.h"

class Game
{
public:
	Game( HWND hWnd,const KeyboardServer& kServer );
	void Go();
	//Member functions
	//Draw a line
	void Game::drawLine(int x1, int y1, int x2, int y2, int red, int green, int blue); //Draw a line between two points
	void Game::drawCircle(int x0, int y0, int radius, int red, int green, int blue); //Draw a circle

private:
	void ComposeFrame();

private:
	D3DGraphics gfx;
	KeyboardClient kbd;
        Ship psp;
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//Game.cpp
#include "Game.h"
#include "Ship.h"
#include <math.h>

Game::Game( HWND hWnd,const KeyboardServer& kServer )
:	gfx ( hWnd ),
	kbd( kServer )
{
	Ship psp(HWND hWnd); //Trying to declare the object
}

void Game::Go()
{
	gfx.BeginFrame();
	ComposeFrame();
	gfx.EndFrame();
}

void Game::ComposeFrame()
{
	psp.getAlpha();
}


>[pathname]\game.cpp(91): error C2065: 'psp' : undeclared identifier
>[pathname]\game.cpp(91): error C2228: left of '.getAlpha' must have class/struct/union
Last edited on
[code]"Please use code tags"[/code]

1
2
3
{
   Ship psp(HWND hWnd);
} //here `psp' ceases to exist 
check out `scope'
I just noticed that the last section of code is not Game.h, but Game.cpp. I have Game.h listed twice- I shall correct that and the lack of formal editing after this post.

I added this portion to Game.h to add the Ship object to the Game class as seen below. I removed this line for testing, and forgot to put it back when I posted the code. I will also put this back in my edit above.

1
2
3
4
5
6
//From Game.h
private:
	D3DGraphics gfx;
	KeyboardClient kbd;
	Ship psp;
};


My error is now something different:

1
2
3
game.h(43): error C2146: syntax error : missing ';' before identifier 'psp'
game.h(43): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
game.h(43): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Last edited on
Thank you. I read through point "** 4) The "right way" to include **". I am still unsure exactly what this means- or rather how it pertains to my code. I am believing that my issue has to do with forward declaration, but I'm not certain how to apply it.

To be honest, I'm not entirely certain which class is causing me a problem- I've managed to tangle three classes all in one in this example.

I had a thought when I took a moment to get some coffee that I could keep the draw functions strictly to the Game object and pass necessary information from the Ship object into Game for drawing purposes. I don't believe it's ideal, but in the end, what do I know what's best?
> do nothing if: A makes no references at all to B
`Ship.h' should not include `Game.h'
Oh I see! Thank you, I didn't even consider that. I included Game.h early because I mistakenly thought the graphics object was defined there. When I looked closer, I realized otherwise and never removed the include.

I did correct this issue, but it did nothing to solve the original problem. I believe I fixed my issue by including the SHIP.H in GAME.H, which it didn't appear to be in before. Now it appears that I can use my Ship object in Game.cpp, but something else is amiss.

The program compiles, but when I use the ship object in game.cpp to draw the ship, at the very least it should draw a circle around the default position of the player, but the screen remains black. If I move the drawCircle function to game.cpp/game.h, it runs without issue, but here it does not.
Last edited on
I believe my issue may be because I have two D3Dgraphics objects- One in Game.h and one in Ship.h. Perhaps calling the draw function in Game.cpp with the Ship object is causing the drawing to be done in another D3D environment, or something?

This is the code I implemented for drawing the circle.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//IN SHIP.CPP
//Draw a bounding circle
void Ship::drawBound(){
	int x1=SIZE, y1=0, deltaX=1-(SIZE<<1), deltaY=0, error=0, red=0, green=255, blue=255;
	while(x>=y){
		gfx.PutPixel(x1 + x, y1 + y, red, green, blue);
		gfx.PutPixel(y1 + x, x1 + y, red, green, blue);
		gfx.PutPixel(-x1 + x, y1 + y, red, green, blue);
		gfx.PutPixel(-y1 + x, x1 + y, red, green, blue);
		gfx.PutPixel(-x1 + x, -y1 + y, red, green, blue);
		gfx.PutPixel(-y1 + x, -x1 + y, red, green, blue);
		gfx.PutPixel(x1 + x, -y1 + y, red, green, blue);
		gfx.PutPixel(y1 + x, -x1 + y, red, green, blue);
		y++;
		error+=deltaY;
		deltaY+=2;
		if(((error << 1)+deltaX)>0){
			x--;
			error+=deltaX;
			deltaX+=2;
		}
	}
}


The default X and Y values are 400 and 300 respectively, which are initialized in the constructor. I have the Ship object successfully created in Game.cpp.

Below is the Ship object calling its drawShip function in the frame drawing function of the Game.cpp.
1
2
3
4
void Game::ComposeFrame()
{
	psp.drawShip();
}




I believe my problem is from here at the top of Game.cpp, because I have to give an HWND object called hWnd to both the D3D graphics object and to the Ship object. Any suggestions? Actually, more likely than that, what about this code in Game::Go()- I have nothing like this in the Ship object.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "Game.h"
#include <math.h>

Game::Game( HWND hWnd,const KeyboardServer& kServer )
:	gfx ( hWnd ),
	psp(hWnd),
	kbd( kServer )
{}

void Game::Go()
{
	gfx.BeginFrame();
	ComposeFrame();
	gfx.EndFrame();
}


I fixed my own problem by reorganizing the program such that all drawing takes place in Game.cpp, and the ship object is able to pass the necessary information into Game to perform drawing operations.
Last edited on
Topic archived. No new replies allowed.