PDA

View Full Version : Move a basic shape



sapkota
01-21-2016, 03:18 PM
Hi, I'm very new to GLFW and openGL and I just wanted to know how to move a basic shape using the up,down,left,right arrow keys.
Every time I execute the code it just makes the box bigger with each press and I'm left as to wonder why.
Thanks for your time and help in advance :D.

My Code:


//Prebuilt libraries & headers
#include <glew.h>
#include <glfw3.h>
#include <iostream>

//Custom headers
#include "createWindow.h"

using namespace std;

//Variables
float X1 = -0.05;
float Y1 = 0.1;
float X2 = 0.05;
float Y2 = 0.0;

//Protoypes
static void error_callback(int error, const char* description)
{
fputs(description, stderr);
}
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
//Close program
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
//Move left
else if (key == GLFW_KEY_LEFT && action == GLFW_PRESS)
X1 += -0.05;
//Move up
else if (key == GLFW_KEY_UP && action == GLFW_PRESS)
Y1 += 0.1;
//Move right
else if (key == GLFW_KEY_RIGHT && action == GLFW_PRESS)
X2 += 0.05;
//Move down
else if (key == GLFW_KEY_DOWN && action == GLFW_PRESS)
Y2 -= 0.1;
}

//Primary function
void createWindow::windowCreate() {
//Create a 854x480 window
GLFWwindow* window;
//Error
glfwSetErrorCallback(error_callback);
if (!glfwInit())
exit(EXIT_FAILURE);
//Create Window
window = glfwCreateWindow(854, 480, "Pong", NULL, NULL);
//Crash if window has not been created
if (!window)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
//Declarations
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
glfwSetKeyCallback(window, key_callback);
//Run while window is not closed
while (!glfwWindowShouldClose(window))
{
int width, height;
glfwGetFramebufferSize(window, &width, &height);
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRectf(X1, Y1, X2, Y2);
glfwSwapBuffers(window);
glfwPollEvents();
}
//If window is closed
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
};

sapkota
01-21-2016, 04:32 PM
How do I mark this thread as solved?

Khronos_webmaster
01-22-2016, 06:35 AM
We don't have an "Answered" or "Resolved" switch. Posting your solution though, might help others. Then we'd see it was resolved.