🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

breakout in opengl and c++

Started by
55 comments, last by pbivens67 2 years, 11 months ago
Advertisement

Can you please post your code on GitHub?

well since my program is not very long, here is my code

#include <iostream>

using namespace std;

void CalcBrickCoords(float &br_x, float &br_y, float &br_w, float &br_h, const int boardX, const int boardY);
void DrawBrick(float br_x, float br_y, float br_w, float br_h, const bool black);

float brickWidth = 35.0f, brickHeight = 15.0f;
bool blackBrickType = false;
float scaleX = 35.0f, scaleY = 15.0f;
float offsetX = -135.0f, offsetY = 100.0f;
int boardHeight = 3, boardWidth = 8;
int test_coll_brick_x, test_coll_brick_y;

class Bricks
{
public:
	float ball_x;
	float ball_y;
	float ball_width;
	float ball_height;
	float brick_x;
	float brick_y;
	float brick_width;
	float brick_height;
	bool collision(float, float, float, float, float, float, float, float);
};

bool Bricks::collision(float ball_x, float ball_y, float ball_width, float ball_height, float brick_x, float brick_y, float brick_width, float brick_height)
{
	if (ball_x<brick_x + brick_width && ball_x + ball_width>brick_x&&ball_y<brick_y + brick_height && ball_y + ball_height>brick_y)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

int main()
{
	Bricks coll;

	float b_x=0.0f, b_y=0.0f, b_w=5.0f, b_h=5.0f;
	float br_x, br_y, br_w, br_h;

	CalcBrickCoords(br_x, br_y, br_w, br_h, test_coll_brick_x, test_coll_brick_y);

	if (coll.collision(b_x, b_y, b_w, b_h, br_x, br_y, br_w, br_h) == 1)
	{
		DrawBrick(br_x, br_y, br_w, br_h, blackBrickType);
	}
	return 0;
}

void CalcBrickCoords(float &br_x, float &br_y, float &br_w, float &br_h, const int boardX, const int boardY)
{
	br_x = float(boardX) * scaleX + offsetX;
	br_y = float(boardY) * scaleY + offsetY;
	br_w = brickWidth;
	br_h = brickHeight;
}

int board[24] = { 0,0,1,1,1,1,0,0,
				 0,1,2,2,2,2,1,0,
				 1,2,2,3,3,2,2,1 };

void DrawBoard(int *board)
{
	for (int y = 0; y < boardHeight; y++)
		for (int x = 0; x < boardWidth; x++)
		{
			int brickType = board[x + y * boardWidth];
			if (brickType == 0) // skip if no brick in that grid cell
				continue;

			float br_x, br_y, br_w, br_h;
			CalcBrickCoords(br_x, br_y, br_w, br_h, x, y);

			DrawBrick(br_x, br_y, br_w, br_h, brickType);
		}
}
 

Where’s the rest of it?

You really should consider using GitHub.

taby said:
Where’s the rest of it?

Likely that's all.

So, Phil, as said before, you need to build up a gameloop / render loop, which can render stuff each frame.
IIRC you used GLUT in the past? So you did something like rendering a rotating cube already before?
Or you did not any 3D rendering yet?

To compile the code as is, you could add a implementation simply by adding braces:

void DrawBrick(float br_x, float br_y, float br_w, float br_h, const bool black)
{
}

It should compile then, but running it not much would happen.
You need to create a Window, OpenGL context, OS Message Processing… all that application stuff.
GLUT makes this easy, but if you did not use it yet, we might want to discuss some other options. (GLUT is dead)

taby said:
Where’s the rest of it?

Likely that's all. Learning how to use github is a challenge on it's own. Maybe more a distraction than benefit for now.

So, Phil, as said before, you need to build up a gameloop / render loop, which can render stuff each frame.
IIRC you used GLUT in the past? So you did something like rendering a rotating cube already before?
Or you did not any 3D rendering yet?

To compile the code as is, you could add a implementation simply by adding braces:

void DrawBrick(float br_x, float br_y, float br_w, float br_h, const bool black)
{
}

It should compile then, but running it not much would happen.
You need to create a Window, OpenGL context, OS Message Processing… all that application framework stuff.
GLUT makes this easy, but if you did not use it yet, we might want to discuss some other options. (GLUT is dead)

well I put in the braces and it worked

This topic is closed to new replies.

Advertisement