Sign in to follow this  
phil67rpg

breakout lives

Recommended Posts

I am trying to print out the number of lives left to the screen,
void drawBitmapText(char *string,float x,float y,float z) 
{  
char *c;
glPushAttrib(GL_CURRENT_BIT);
glClear(GL_COLOR_BUFFER_BIT);
brick();
paddle();
ball();
glColor3f(1.0f,1.0f,1.0f);
glRasterPos3f(x, y, z);
for (c=string; *c != '\0'; c++) 
{
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, *c);
}
glPopAttrib();
}

void lives()
{
	num--;
	strng=itoa(num,buffer_two,10);
	drawBitmapText(strng,0.0f,2.0f,0.0f);	
}

void Timer(int value)
{
	if(x > windowWidth) 
	{
	xstep = -xstep;
	}

	if( x < -windowWidth)
	{
	xstep=-xstep;
	}

	if(y > windowHeight)
	{
	ystep=-ystep;
	}

	if(y < -windowHeight)
	{
	x=0.0f;
	y=0.0f;
	lives();
	ball();
	}

Share this post


Link to post
Share on other sites
well I am trying to print to the screen the number of lives left in my game. I am using the lives() function to print out to the screen but I don't get anything. When the ball goes off the bottom of the screen I want the lives counter to decrement by one. Let me know if you need any more code or information.

Share this post


Link to post
Share on other sites
I'm not particularly familiar with OpenGL, but here are some good questions to consider. Are you sure (0, 2, 0) is actually within the window? Is your window background white (you're printing white text)? Does the current raster position refer to the bottom left or top left of a character? Try outputting the text at (100, 100, 0) and see what happens.

Share this post


Link to post
Share on other sites
try setting all your matrices to identity matrices right before you draw the text. You might be translating the text without your knowing.

EDIT: Also, it appears as if glColor and glRasterPos are not OpenGL 4 compliant. They may have been deprecated because I can't seem to find them here.

EDIT2: Lighting and texturing may also effect the color of your text because it seems when lighting is on, a lighting equation is used to compute a new primary color for the rasterized pixel. Edited by incertia

Share this post


Link to post
Share on other sites
I am using glut 3.7.6 which works with glColor and glRasterPos. Actually this piece of code works.
void brick_collision()
{
	GLint n=0;
	
	if(bricks[2][4]==true)
	{
	n+=100;
	str=itoa(n,buffer,10);
	drawBitmapText(str,4.0f,2.0f,0.0f);
	}

	if(bricks[2][3]==true)
	{
	n+=100;
	str=itoa(n,buffer,10);
	drawBitmapText(str,4.0f,2.0f,0.0f);	
	}

	if(bricks[2][2]==true)
	{
	n+=100;
	str=itoa(n,buffer,10);
	drawBitmapText(str,4.0f,2.0f,0.0f);	
	}

	if(bricks[2][1]==true)
	{
	n+=100;
	str=itoa(n,buffer,10);
	drawBitmapText(str,4.0f,2.0f,0.0f);	
	}

	if(bricks[2][0]==true)
	{
	n+=100;
	str=itoa(n,buffer,10);
	drawBitmapText(str,4.0f,2.0f,0.0f);	
	}

Share this post


Link to post
Share on other sites

Can you explain why you are calling:

 

brick();

paddle();

ball();

 

inside your drawBitmapText function.

 

 

Also can you explain what brick_collision() does?


 

Though my guess is that you are clearing the screen every frame, however you are only drawing the "num" for one frame when the "y" leaves the screen.

Share this post


Link to post
Share on other sites

In addition to JTippetts post, it seems that there are some vague naming conventions. Now these may seem okay to you however, people trying to contribute in assisting you (like myself) will have a hard time navigating the "what does this function do?" minefield.

 

The method lives() to me should be renamed to something along the line of 'handle lives', 'do lives' etc. At its basic level it should check to see if some lives altering event occurred, such as the ball going off screen or you make contact with a power-up that increases them. Then you increment/decrement 'num' (should also be renamed, paddle_lives would suffice) by the appropriate amount. This would then be included in the portion of the loop JTippetts describes as:

 

Detect, handle, respond to and resolve collisions and other object interactions

 

DrawBitmapText would then occur when you Draw the UI Elements. Can I ask, what do the 3 methods brick(), paddle(), ball() do? Are these the same as lives in that they handle the logic for those objects? Does this mean that in your code you effectively repeat their actions more than once per loop? Or, do you remove them for where they were and put them in the new method you think they should be? Either way, it will start to become messy, un-organised and can actually do more damage when problems like this occur.

 

Remove these methods from you DrawText function, have the function do what it says on the tin, and perform any action related to drawing text. Keep it simple.

 

Back on topic, I have to agree with stannic. What probably happens is that, when the ball goes off screen, the lives gets drawn for a split-second and then disappears. If you remove the DrawText from lives and move it to the correct position in the loop, untying it from any sort of 'if' clause, it should show the text constantly. I assume that it works for the Bricks code you posted because 'brick[N][N] == true' refers to the brick existing?

 

Regards,

 

Stitchs.

Share this post


Link to post
Share on other sites

I still can't believe phil, with all his struggles to get normal coding principles down, insists on using OpenGL directly with it's 3D interface.  Instead, he could be using one of the many multimedia API's that make drawing, writing text, etc SO EASY (like SFML or SDL) in 2D.

 

He's just stubborn, and doesn't listen to advise.  Hard to help someone when they do that

Share this post


Link to post
Share on other sites

Here is a short article on Naming Conventions, it doesn't take too long to read and should be enough information for you to make effective changes:

 

http://www.cprogramming.com/tutorial/style_naming_conventions.html

 

Regarding responsibility, you should really take a step back from your code, and probably this project. It might take too long to modify with different naming conventions and changing all the responsibilities however, what you can do is take away the lessons you've learnt into a new project. One where you can start from the ground up, get your names correct, decide on the responsibilities of each class and method. Ask yourself as you code, using the current problem as an example; "I have named this method XXX (DrawText) however, I am processing YYY (brick logic) in here, is it correct?" Of course the two will link in a small way, how can you display text about the number of bricks on screen, if you don't examine the list of active bricks to see which are alive? But is DrawText, a general helper function probably used multiple times, specific to brick logic?

 

This is a very specific example, but if you generalise as I did with XXX and YYY, then it is applicable to most areas. I would like to point you to an article on this very site about the subject of pre-visualisation, which touches on Responsibilities in a light way that should be easy to understand:

 

http://www.gamedev.net/page/resources/_/technical/general-programming/pre-visualization-is-important-r2969

 

Read these two articles in conjunction with what I have given you, and you should be off to a better start with your next project. Aim to resolve this issue, try and get the text to display, then write down what you did wrong, how you fixed it, and you can use this to identify early issues in your next game.

 

A final thought; you have been working on Breakout for a long time, and I admire the perseverance, but it might be time to start a fresh. Build your game design with Responsibility and good Naming conventions, and you have an easier time when it comes to coding.

 

Feel free to ask for any more information,

 

Regards,

 

Stitchs.

 

P.S. I agree with BeerNutts here, make life easier on yourself and use SFML or Allegro instead for your next project. It's a bit of a minefield getting into programming without using a hardware accelerated graphics library such as OpenGL. It's a good step-up once you have the principles of an API such as SFML down, but right now, focus on something simpler. There are a lot of videos on these sorts of libraries such as this YouTube channel: http://www.youtube.com/user/CodingMadeEasy?feature=watch

Edited by stitchs

Share this post


Link to post
Share on other sites
*sigh* I promised myself I wouldn't respond to any more phil threads, but I'm an old softie...

phil, i think part of your problem might be that you are still using GLUT. GLUT, in my experience, is okay for quick one-off prototypes and small demos, but is not really suitable for actual games. The problem with GLUT is that it takes control of the main loop away from you, forcing you to act through callbacks to get anything done. This isn't really appropriate for the main loop structure that games look for. There are other libraries that are far more appropriate for game development. SDL, SFML, GLFW, Allegro, etc... I highly recommend you switch to one of them, and make your life a little bit easier. Once you are working with a library where you control the actual loop, then I recommend the article Fix Your Timestep! It offers an excellent introduction to the way a main loop is structured, and offers justification for switching to a fixed timestep loop. Read it, study it, ask specific questions about it and maybe we can help you finally make some progress.

But please, for the love of God, abandon what you are doing right now. Toss out your lives() method, throw away your drawBitmapText() method, get rid of it all and start clean. It's just not working, man. Let it go. You're just running around in the same old circles, asking the same old questions, and you haven't made any meaningful changes to your code from what it was months and months ago.

Share this post


Link to post
Share on other sites

I would recommend Pong. Key reasons being:

 

  1. It is not dissimilar to the project you just worked on, meaning you can take away the concepts you used to move the ball, move the paddle etc.
  2. You can learn your new API in relative safety; you're not having to learn entirely new game mechanics as well as all the functionality of said API.
  3. It is different enough that it feels fresh.

 

Good luck!

 

Stitchs.

Share this post


Link to post
Share on other sites

well I really want to finish this game I am so close.

No you're not. Your code still exhibits major flaws that stand in the way of it ever being a finished and bug-free game. Trying to "finish" it as it stands would require a major overhaul anyway, so I'd say you shouldn't waste any more time on it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this