Jump to content
  • Advertisement
Sign in to follow this  

Create Moving Quad

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

Hey,

I would like to move a quad, while pressing the left or right key. But the quad ist just flashing, but not moving.
Here is my code. Can anyone help? By the way, I just need 2D.

#include <stdio.h>
#include "stdafx.h"
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <gl/glut.h>

/* Defines */

#define HEIGHT 500
#define WIDTH 500

/*Variabeln Defintionen*/

float moveX=0;
float moveY=0;

/*Funktionen Defintionen*/

void drawScene(void);
void Steuerung(int key, int x, int y);
void Keypress(unsigned char key, int x ,int y);
void timer(int value);

/* Hauptprogramm */

int _tmain(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(HEIGHT, WIDTH);
glutCreateWindow("The Game");
glutDisplayFunc(drawScene);
glutSpecialFunc(Steuerung);
glutKeyboardFunc(Keypress);
glutTimerFunc(10, timer, 10);
glutMainLoop();
return 1;
}

void timer(int value){
glutPostRedisplay();
glutTimerFunc(value, timer, value);
}

void drawScene(void) {

glClear(GL_DOUBLE |GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glTranslatef(moveX,-1.0,0.0);
glBegin(GL_QUADS);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(0.1f, 0.0f, 0.0f);
glVertex3f(0.1f, 0.1f, 0.0f);
glVertex3f(0.0f, 0.1f, 0.0f);
glEnd();
glFlush();
glutSwapBuffers();

}

void Steuerung(int key, int x, int y){
switch(key) {
case GLUT_KEY_LEFT:
moveY=0;
moveX-=0.1;
break;
case GLUT_KEY_RIGHT:
moveY=0;
moveX+=0.1;
break;
}
}

void Keypress(unsigned char key, int x, int y)
{
switch (key) {
case 27: // Escape
exit(0);
}
}

Share this post


Link to post
Share on other sites
Advertisement
glLoadIdentiy() before translate, otherwise its translating by .1, then .2, then .3 (as movex gets bigger). glLoadIdentity() resets the previous translations so that you just get the one that is what move_x currently is.

Share this post


Link to post
Share on other sites
Thanks. Now I can see the motion, but the Quad is getting bigger, but not really changing the position. For example we have a quad 10 * 10, and after pressing two times left, it's 30*10.

What's the problem?

Share this post


Link to post
Share on other sites
You obviously translate on the wrong axis. Try glTranslate(0,-1,moveX). Seems like x-axis pointing towards the camera.

Share this post


Link to post
Share on other sites

Thanks. Now I can see the motion, but the Quad is getting bigger, but not really changing the position. For example we have a quad 10 * 10, and after pressing two times left, it's 30*10.

What's the problem?

Sounds like you're not clearing the screen before you do the update. Make sure that you defined a glClearColor. From the looks of the code, I don't think you need to be calling GLFlush() because it will happen when you swap the buffers. Also, not sure why you have GL_DOUBLE as a parameter to glClear. GL_DOUBLE has to do with double precision floating point types.

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!