breakout game

Started by
19 comments, last by phil67rpg 11 years, 4 months ago
I am working on a breakout game using texture mapping and OpenGL. I have the paddle working but when I move it the bricks disappear. The problem is probably related to the buffer swapping command.

void drawScene() {
glClear(GL_COLOR_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glEnable(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
for(float i=-5.0f;i<=3.0f;i+=2.0f)
{
//draw bricks
glBegin(GL_QUADS);
glTexCoord2f(-1.0f, 0.0f);
glVertex3f(i, 5.0f, 0.0f);
glTexCoord2f(-1.0f, 1.0f);
glVertex3f(i+2.0f, 5.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(i+2.0f, 4.5f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(i, 4.5f, 0.0f);
glEnd();
}
for(float i=-5.0f;i<=3.0f;i+=2.0f)
{
//draw bricks
glBegin(GL_QUADS);
glTexCoord2f(-1.0f, 0.0f);
glVertex3f(i, 4.5f, 0.0f);
glTexCoord2f(-1.0f, 1.0f);
glVertex3f(i+2.0f, 4.5f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(i+2.0f, 4.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(i, 4.0f, 0.0f);
glEnd();
}
for(float i=-5.0f;i<=3.0f;i+=2.0f)
{
//draw bricks
glBegin(GL_QUADS);
glTexCoord2f(-1.0f, 0.0f);
glVertex3f(i, 4.0f, 0.0f);
glTexCoord2f(-1.0f, 1.0f);
glVertex3f(i+2.0f, 4.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(i+2.0f, 3.5f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(i, 3.5f, 0.0f);
glEnd();
}
//draw paddle
glBegin(GL_QUADS);
glTexCoord2f(-1.0f, 0.0f);
glVertex3f(-1.0f, -4.5f, 0.0f);
glTexCoord2f(-1.0f, 1.0f);
glVertex3f(1.0f, -4.5f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(1.0f, -5.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0f, -5.0f, 0.0f);
glEnd();
glDisable(GL_TEXTURE_2D);

glutSwapBuffers();
}
void paddle_left()
{
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

//draw paddle
glBegin(GL_QUADS);
glTexCoord2f(-1.0f, 0.0f);
glVertex3f(-1.0f+i, -4.5f, 0.0f);
glTexCoord2f(-1.0f, 1.0f);
glVertex3f(1.0f+i, -4.5f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(1.0f+i, -5.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0+i, -5.0f, 0.0f);
glEnd();
i-=0.05f;
glDisable(GL_TEXTURE_2D);
glutSwapBuffers();
}

hopefully this is not too much.
Advertisement
Why would you expect anything else if you clear the buffer, draw only the paddle and then display only the paddle?

How does this even compile if 'i' isn't declared in paddle_left (and if it's global... can you tell why naming a global variable "i" is just incredibly bad and confusing?)

And how is this ever going to work if the next time you draw your scene you will always draw the paddle in exactly the same position, no matter what?

How will you ever "remove" a brick if drawing them is hard coded like that?

Why is that loop copy pasted for every row of bricks and how will you ever be able to have any other number of rows in a level?

You need to take a very big step back and realize that the stuff drawn on the screen isn't the "game", but takes the current state of the game and visualizes it. AI players (or psychics) should be able to play without needing ANY kind of graphics of drawing. In one nice Google friendly acronym: "MVC".

A buffer in OpenGL isn't a stage where you put stuff and then move it around, it's a ton of pixels that need to be rendered again for every frame (unless you really know what you're doing and invest a lot of head ache to optimize something that absolutely doesn't need to be optimized).
f@dzhttp://festini.device-zero.de
I have worked on this a lot, can I get another hint on what to do.
If you don't understand what he is saying, then you should ask a specific question about his post.

Why would you expect anything else if you clear the buffer, draw only the paddle and then display only the paddle?

can you please clarify what you mean by only drawing the paddle and the bricks.
I assume you call paddle_left whenever the paddle is supposed to move left? That's why your bricks disappear. As soon as paddle_left is called, the buffer is cleared and only the paddle is drawn. Anything else that was drawn (perhaps during a previous call to draw_scene) is wiped when glClear is called inside paddle_left.

What you are doing here is just... no. There should be one place, and one place only where things are drawn. The proper place is not inside whatever logic you use to make paddles move. You might want to take a break and do some research on basic game loops: how they're structured, what pieces go where, etc... Mixing logic and rendering like you are doing here is just not going to cut it.

Typically, a loop proceeds through a set of tasks in a pre-defined order:

1) Call Time() to get the start of frame time

2) Process any waiting input events, and hand the results off to interested parties. Here you might call functionality to move your paddle, for instance by adding or subtracting a value from its position. Only that, though; no drawing, no buffer clearing. Just input handling.

3) Update logic. How this is done varies; some setups will update logic on a fixed time step (every, say, 1/60th of a second) while others will compare the current Time() against the Time() of the last logic update, and use the difference as a step length. In this stage, only logic is performed: things are moved, decisions are made. No drawing. None whatsoever. Doesn't belong here, either.

4) Update collisions. This process might be interleaved with your physics step, depending on how you are doing your physics. At any rate, here you account for the things that happen when a collision occurs. Break blocks, bounce balls, check for ball out of bounds and decrement ball counter, and so on. Again, no rendering. These stages of the loop don't care what goes on the screen. As Trienco indicated, they should do what they do even if nothing is ever drawn on the screen. Any drawing code in these sections should actually generate compile errors, because there is absolutely no reason these modules should even know what OpenGL is. That's how you avoid problems.

5) Render. Aha! Here we go, now that everything has moved to its new location, we can draw the screen. This part of the loop doesn't care about logic or physics or things moving or input being handled. If you are doing any of that here, you are doing the wrong thing. The only thing this part of the loop cares about is drawing the stuff it's told to draw on the screen. There should be exactly one call to glClear() and one call (after drawing is done) to swap buffers. Any more and you are just going to eradicate work already done. Clear the buffer, draw your game, draw your UI, swap buffers.

6) Repeat.

There are many variations to the loop, but that is the basic gist of it. You might want to read the usual suspects when it comes to game loops (Gaffer's fixed time step article, the Bullet physics canonical game loop, etc...) in order to really understand the processes and how the different sections need to be separated. Make any number of throwaway applications as you need to get this figured out. Without this basic understanding, you are just doomed to repeat your past failures and cause yourself more frustration.
wow that is a lot to digest, thanks for all the help, I will do some research on game loops.

assume you call paddle_left whenever the paddle is supposed to move left? That's why your bricks disappear. As soon as paddle_left is called, the buffer is cleared and only the paddle is drawn. Anything else that was drawn (perhaps during a previous call to draw_scene) is wiped when glClear is called inside paddle_left.

I am still working on this problem.
well according to tippets I should put all my rendering inside the drawscene function. I should do no drawing inside the paddle left function. My question is what should I put in my paddle left function.
well tippets is right on, thanks a lot I finally figured it out.

This topic is closed to new replies.

Advertisement