Jump to content
  • Advertisement
Sign in to follow this  
qhash

OpenGL [n00b] moving objects in OpenGL , Tetris

This topic is 5073 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

hi I am new here and OpenGL is also new for me ;) I already can initialize, draw , resize windows , make perspective in OpenGL , but i dont know how to move objects. This is a big problem , beacouse i am writing tetris. Could someone send me a simple code showing how to make moving 2D cubes , adding new ones to the scene , in order to make my game working. I searched the net and didnt find anything useful for me :(

Share this post


Link to post
Share on other sites
Advertisement
look up matrix transformations, glTranslatef() is what you need, as for handling your blocks, youll need to devise your own way to hang on to your current blocks and keep adding one (for now a std::list will serve you well)

hope that helps a bit
-Dan

Share this post


Link to post
Share on other sites
big thx for help , especially that link to nehe page

and if someone has his own sourcecode of tetris i will of sharing it , possibly will help me a lot
[ not whole , simply a way that moving blocks is implemented ]

Share this post


Link to post
Share on other sites
hehe the date of this file is 3/7/2000, its 4 years and a day old, man i was a crap programmeur then


#include "main.h"

float currentTick, lastTick, totalTime;

float fps;

// this sets up the various shapes of the bricks

void brick_sort::setup_areas(int *places)
{ for (int rot=0;rot<4;rot++)
{ for (int i=0;i<8;i++)
{ if (i%2)
area[i/2][rot].y=places[i+rot*8];
else
area[i/2][rot].x=places[i+rot*8];
}
}
}

Game::Game()
{
}

int areas[] = {0,0,1,0,0,1,1,1, 0,0,1,0,0,1,1,1, 0,0,1,0,0,1,1,1, 0,0,1,0,0,1,1,1, // SQUARE
0,0,-1,0,0,1,1,0, 0,0,-1,0,0,1,0,-1, 0,0,-1,0,0,-1,1,0, 0,0,0,1,0,-1,1,0, // TSHAPE
0,0,-1,0,0,1,1,1, 0,0,0,1,1,0,1,-1, 0,0,-1,0,0,1,1,1, 0,0,0,1,1,0,1,-1, // ZSHAPELEFT
0,0,-1,1,0,1,1,0, 0,0,0,-1,1,0,1,1, 0,0,-1,1,0,1,1,0, 0,0,0,-1,1,0,1,1, // ZSHAPERIGHT
0,0,-1,0,1,0,2,0, 0,0,0,-1,0,-2,0,1, 0,0,-1,0,1,0,2,0, 0,0,0,-1,0,-2,0,1, // LONGONE
0,0,0,1,0,-1,1,-1, 0,0,-1,0,1,0,1,1, 0,0,-1,1,0,1,0,-1, 0,0,-1,0,1,0,-1,-1, // LLEFT
0,0,0,1,-1,-1,0,-1, 0,0,-1,0,1,0,1,-1, 0,0,0,1,1,1,0,-1, 0,0,-1,1,-1,0,1,0};// LRIGHT

float colours[] = {1,0,0, 1,1,0, 0,1,0, 1,0,1, 0,1,1, 0,0,1, 1,0.75,0};

void Game::initGame(void)
{
for (int i=0;i<NUM_OF_DIFFERENT_BRICKS;i++)
{ Btype.setup_areas(areas+32*i);
memcpy(Btype.colour,colours+i*3,sizeof(float)*3);
}

next_brick.rot = R0;
next_brick.pos.x = BOARDWIDTH/2;
next_brick.pos.y = BOARDHEIGHT;
next_brick.variety = brick_variety(rand()%NUM_OF_DIFFERENT_BRICKS);

addBrick();
score=0;
next_level(0);
}

void Game::next_level(int lvl)
{ level=lvl;
rows_left = level+3;
brick_falling_time = 800-level*80;
if (brick_falling_time < 25)
brick_falling_time=25;

for (int x=0;x<BOARDWIDTH;x++)
{ for (int y=0;y<BOARDHEIGHT;y++)
{ board[x][y].whats_here = NONE;
}
}

totalTime = glutGet(GLUT_ELAPSED_TIME);
srand(totalTime);
time_for_the_brick_to_fall = totalTime + brick_falling_time;

}

void Game::addBrick(void)
{
current_brick = next_brick;
next_brick.pos.x = BOARDWIDTH+6;
next_brick.pos.y = BOARDHEIGHT-8;

next_brick.rot = R0;
next_brick.variety = brick_variety(rand()%NUM_OF_DIFFERENT_BRICKS);
current_brick.pos.x = BOARDWIDTH/2;
current_brick.pos.y = BOARDHEIGHT;

if (!is_brick_able_to_be_here(current_brick))
gameover=true;
}

// tests to see if the brick ain't hitting the walls or another brick

bool Game::is_brick_able_to_be_here(brick b)
{ for (int i=0;i<4;i++)
{ int xx = Btype[b.variety].area[b.rot].x + b.pos.x;
int yy = Btype[b.variety].area[b.rot].y + b.pos.y;
if (xx<0 || xx>=BOARDWIDTH || yy<0 )
return false;

for (int x=0;x<BOARDWIDTH;x++)
{ for (int y=0;y<BOARDHEIGHT;y++)
{ if (board[x][y].whats_here && x==xx && y==yy)
return false;
}
}
}
return true;
}

// clean this up zed
int how_many_rows;

void Game::remove_rows(int startrow)
{
for (int j=startrow;j<BOARDHEIGHT;j++)
{ bool full=true;
for (int i=0;i<BOARDWIDTH;i++)
{ if (board[j].whats_here == NONE)
full=false;
}
if (full) // this row needs to be removed and all the others moved down
{ for (int jj=j;jj<BOARDHEIGHT-1;jj++)
{ for (int ii=0;ii<BOARDWIDTH;ii++)
{ board[ii][jj]=board[ii][jj+1];
}
}
how_many_rows++;
rows_left--;
if (rows_left<=0)
next_level(++level);
remove_rows(j);
}
}


}

void Game::add_brick_to_the_board(void)
{ int i;

brick b = current_brick;
for (i=0;i<4;i++)
{ int x = Btype[b.variety].area[b.rot].x + b.pos.x;
int y = Btype[b.variety].area[b.rot].y + b.pos.y;
board[x][y].whats_here = BLOCK;
memcpy(board[x][y].colour,Btype[b.variety].colour,sizeof(float)*3);

}
// see if we've completed a row
// should only check where we've put the last brick
score++; // 1 point per brick
how_many_rows=0;
remove_rows(0);

switch (how_many_rows)
{ case 0: break;
case 1: score+=25; break;
case 2: score+=100; break;
case 3: score+=250; break;
case 4: score+=500; break;
}
glColor4f(1,1,1,1);
char str[80];
glScissor(BRICKSIZE*BOARDWIDTH+32,BRICKSIZE*BOARDHEIGHT-64,320,96);
glClear( GL_COLOR_BUFFER_BIT);
glEnable(GL_BLEND); // any part of the texture black we won't draw
glBindTexture(GL_TEXTURE_2D, hud_Texture);
sprintf(str,"score %d",score);
drawText(str,BRICKSIZE*BOARDWIDTH+32,BRICKSIZE*BOARDHEIGHT,24);
sprintf(str,"rows left %d",rows_left);
drawText(str,BRICKSIZE*BOARDWIDTH+32,BRICKSIZE*BOARDHEIGHT-32,24);
sprintf(str,"level %d",level);
drawText(str,BRICKSIZE*BOARDWIDTH+32,BRICKSIZE*BOARDHEIGHT-64,24);
glDisable(GL_BLEND);

glBindTexture(GL_TEXTURE_2D, grafix_Texture);
glScissor(BRICKSIZE*BOARDWIDTH+128,BRICKSIZE*BOARDHEIGHT-196,BRICKSIZE*5,BRICKSIZE*4);
glClear( GL_COLOR_BUFFER_BIT);
addBrick();
drawBrick(next_brick);

glScissor(BRICKSIZE,BRICKSIZE,BRICKSIZE*BOARDWIDTH,BRICKSIZE*BOARDHEIGHT+BRICKSIZE*2);
}

void Game::drawBrick(brick b)
{ int x=b.pos.x;
int y=b.pos.y;

for (int i=0;i<4;i++)
{ int xx = Btype[b.variety].area[b.rot].x*BRICKSIZE;
int yy = Btype[b.variety].area[b.rot].y*BRICKSIZE;
glColor3fv(Btype[b.variety].colour);
drawSomeit((x+1)*BRICKSIZE + xx, (y+1)*BRICKSIZE + yy , 0, 0, 0.5, BRICKSIZE, BRICKSIZE);

}
}

// bollux function remove

void Game::moveBrick(int x,int y)
{ if (y)
{ current_brick.pos.y+=y;
}
if (x)
{ current_brick.pos.x+=x;
}
}


void Game::mainLoop(void)
{
// NOTE cause we are drawing everything 2d we don't need to clear the depth buffer cause there ain't one
glClear( GL_COLOR_BUFFER_BIT);

// we use this to keep the game running at the same speed on an athlon as a 486
float thisTick = glutGet(GLUT_ELAPSED_TIME);
currentTick = thisTick-lastTick;
totalTime+=currentTick;
lastTick = thisTick;

static int loopnum=0;
static float beginTick=thisTick;
loopnum++;
if (loopnum>=100)
{
fps = (1.0/((thisTick-beginTick)/loopnum))*1000.;
loopnum=0;
beginTick = thisTick;

char str[80];
sprintf(str,"Tetris %.d FPS",int(fps));
glutSetWindowTitle(str);
}

if (totalTime>time_for_the_brick_to_fall)
{ moveBrick(0,-1);
if (!is_brick_able_to_be_here(current_brick))
{ moveBrick(0,1); // move it back up
add_brick_to_the_board();

}
time_for_the_brick_to_fall = totalTime + brick_falling_time;
}

renderScreen();

}

#define BACKGROUNDSIZE 128

void Game::drawBackground(void)
{ glBindTexture(GL_TEXTURE_2D, grafix_Texture);
glColor3f(1,1,1);
for (int x=0;x<SCREENWIDTH;x+=BACKGROUNDSIZE)
{ for (int y=0;y<SCREENHEIGHT;y+=BACKGROUNDSIZE)
{
drawSomeit(x, y , 0.5, 0.0, 0.5, BACKGROUNDSIZE, BACKGROUNDSIZE);
}
}
}

static int first_time=0;

void Game::renderScreen(void)
{
glColor3f(1,1,1);
glBindTexture(GL_TEXTURE_2D, bottom_Texture);
// draw the background of eddie and richie twice
drawSomeit(BRICKSIZE, BRICKSIZE , 0, 0, 1, BRICKSIZE*BOARDWIDTH, BRICKSIZE*(BOARDHEIGHT+2)/2);
drawSomeit(BRICKSIZE, BRICKSIZE+BRICKSIZE*(BOARDHEIGHT+2)/2 , 0, 0, 1, BRICKSIZE*BOARDWIDTH, BRICKSIZE*(BOARDHEIGHT+2)/2);

glBindTexture(GL_TEXTURE_2D, grafix_Texture);
// draw all the bricks
for (int x=0;x<BOARDWIDTH;x++)
{ for (int y=0;y<BOARDHEIGHT;y++)
{ if (board[x][y].whats_here)
{ glColor3fv(board[x][y].colour);
drawSomeit((x+1)*BRICKSIZE, (y+1)*BRICKSIZE, 0, 0, 0.5, BRICKSIZE, BRICKSIZE);
}
}
}

// draw the players brick
drawBrick(current_brick);

// we're finished drawing int the back buffer so put the back buffer in the front buffer?
glutSwapBuffers();

}


Share this post


Link to post
Share on other sites
again thanks , now with tutorials and this code i think my skills will increase fast

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!