I Have a int main(); function and a int Game(); function where in the main function I have a window I use in both functions. I have defined the window in the global scope and when I try to run the Game(); function before GLFW loop in main function the window opens for a second and then closes. I then get
Error: The GLFW library is not initialised
printed from error_callback();
Here's My Code
#include <GLFW/glfw3.h>
#include <GL/freeglut.h>
#include <GL/GL.h>
#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;
void drawText(const char *text, int length, int x, int y);
void framebuffer_size_callback(GLFWwindow* wndow, int width, int height);
void DrawCube(GLfloat centerPosX, GLfloat centerPosY, GLfloat centerPosZ, GLfloat edgeLength);
void button(GLfloat red, GLfloat green, GLfloat blue, int x, int y, int width, int height);
void error_callback(int error, const char* description);
int Game();
int width = 860, height = 490;
GLFWwindow* window;
int main(int argc, char **argv) {
//GLTtext* text = gltCreateText();
//gltSetText(text, "ElectroCraft");
//GLFWwindow* window;
int width = 860, height = 490;
if (!glfwInit()) {
printf("failed to init glfw");
return -1;
}
glutInit(&argc, argv);
window = glfwCreateWindow(width, height, "ElectroCraft", NULL, NULL);
glfwMakeContextCurrent(window);
if (!window) {
printf("failed to start window");
glfwTerminate();
return -1;
}
Game();
glEnable(GL_DEPTH_TEST);
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, 0, 1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
string text;
glfwSetErrorCallback(error_callback);
while (!glfwWindowShouldClose(window)) {
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glClearColor(53.0f / 255.0f, 81.0f / 255.0f, 92.0f / 255.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_LINES);
glVertex2f(0, height-80);
glVertex2f(width, height - 80);
glEnd();
button(192.0f / 255.0f, 192.0f / 255.0f, 192.0f / 255.0f, width / 2 - 70, height / 2, 260, 50);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 1;
}
void error_callback(int error, const char* description) {
fprintf(stderr, "Error: %s\n", description);
}
int Game() {
//glfwMakeContextCurrent(window);
glEnable(GL_DEPTH_TEST);
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, 0, 1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
while (!glfwWindowShouldClose) {
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glClearColor(62.0f / 255.0f, 85.9f / 255.0f, 255.0 / 255.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 1;
}
This could be because I didn't initiate the GLFW library globally but I couldn't seem to do this as I got an error if a if statement was outside of a function
the window opens for a second and then closes
Of course, because glfwWindowShouldClose in not a function call in the function Game. glfwWindowShouldClose is a function pointer, so !glfwWindowShouldClose evaluates to false and the loop never runs:
while (!glfwWindowShouldClose) {
while (!glfwWindowShouldClose(window)) {
I then get Error: The GLFW library is not initialised
This is because GLFW is terminated by glfwTerminate() in the function Game. Delete glfwTerminate() from the function Game.
But note, once you've closed a window, you've to create a new window. An option would be to hide glfwHideWindow and show glfwShowWindow a window.
Related
OpenGL draws only the background, the yellow point does not appear. I want to draw it with glBegin and glEnd. The coordinates are variables, because I want to move that point later. Most of the code is just GLFW initialization the function that worries me is the
draw_player function since there the draw call is contained. The Fix I stumbled upon, to use GL_POINTS instead of GL_POINT (in glBegin as argument), does not help (I continue to use it though).
#include <GLFW/glfw3.h>
//#include <stdio.h>
//coordinates
int px, py;
//My not working function
void draw_player()
{
glColor3f(1.0f, 1.0f, 0);
glPointSize(64.0f);
glBegin(GL_POINTS);
glVertex2i(px, py);
glEnd();
}
int main(int argc, char* argv[])
{
GLFWwindow* window;
if (!glfwInit())
return -1;
window = glfwCreateWindow(910, 512, "Raycast", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glClearColor(0.1f, 0.1f, 0.5f, 1.0f);
//setting the coordinates
px = 100;
py = 10;
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
draw_player();
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
The coordinate (100, 10) is not in the window. You have not specified a projection matrix. Therefore, you must specify the coordinates of the point in the normalized device space (in the range [-1.0, 1.0]).
If you want to specify the coordinates in pixel units, you must define a suitable orthographic projection with glOrtho:
int main(int argc, char* argv[])
{
GLFWwindow* window;
if (!glfwInit())
return -1;
window = glfwCreateWindow(910, 512, "Raycast", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glMatrixMode(GL_PROJECTION);
glOrtho(0, 910.0, 512.0, 0.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
// [...]
}
I've checked every other related question on this site but none of the solutions have worked for me. I'm simply trying to follow my rectangle, which moves left and right with key presses in OpenGL. Here's my very simple program:
/*Begin useful backend functions/vars*/
/*************************************/
//Window size and refresh rate (60 fps)
int width = 500;
int height = 500;
int interval = 1000 / 60;
//Used to draw rectangles
void drawRect(float x, float y, float width, float height) {
glBegin(GL_QUADS);
glVertex2f(x, y);
glVertex2f(x + width, y);
glVertex2f(x + width, y + height);
glVertex2f(x, y + height);
glEnd();
}
/***********************************/
/*End useful backend functions/vars*/
/*Game vars*/
/***********/
//keycodes
#define keyA 0x41
#define keyD 0x44
//player
int playerWidth = 30;
int playerHeight = 50;
int playerSpeed = 3;
//player starting position
float playerX = width / 2;
float playerY = 25.0f;
/***************/
/*End game vars*/
/*Game specific functions*/
/*************************/
void keyboard() {
//Move player (and camera) on key presses
if (GetAsyncKeyState(keyA)) {
playerX -= playerSpeed;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(-playerSpeed,0,0);
}
if (GetAsyncKeyState(keyD)) {
playerX += playerSpeed;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(playerSpeed, 0, 0);
}
}
/********************/
/*End game functions*/
/*Draw and update for window*/
/****************************/
void draw() {
//Initialliy clear
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//Draw player
drawRect(playerX, playerY, playerWidth, playerHeight);
//Swap buffers to end
glutSwapBuffers();
}
void update(int value) {
// input handling
keyboard();
// Call update() again in 'interval' milliseconds
glutTimerFunc(interval, update, 0);
// Redisplay frame
glutPostRedisplay();
}
/*****************/
/*End draw/update*/
/*Main function*/
/***************/
int main(int argc, char** argv) {
// initialize opengl (via glut)
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(width, height);
glutCreateWindow("My Game");
// Register callback functions
glutDisplayFunc(draw);
glutTimerFunc(interval, update, 0);
// setup scene to be 2d
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0, height);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//set draw color to white
glColor3f(1.0f, 1.0f, 1.0f);
//start the whole thing
glutMainLoop();
return 0;
}
/*************/
/*End of main*/
The keyboard movement works perfectly, however:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(-playerSpeed,0,0);
inside my keyboard() function, does nothing. And if I try it with GL_PROJECTION, it turns my screen black.
First of all note, that drawing by glBegin/glEnd sequences and the fixed function matrix stack is deprecated since decades. See Fixed Function Pipeline and Legacy OpenGL.
Simplify things.
Add a the keyboard events for key up and down (glutKeyboardFunc / glutKeyboardUpFunc). This functions only modifies the speed of the player. The speed is set when a button is pressed and is set 0, when a button is release:
int playerSpeed = 0;
void keyboardDown(unsigned char key, int x, int y)
{
if (key == 'a')
playerSpeed -= 3;
else if (key == 'd')
playerSpeed = 3;
}
void keyboardUp( unsigned char key, int x, int y )
{
playerSpeed = 0;
}
The timer event just modifies the position of the player:
void update(int value)
{
playerX += playerSpeed;
glutTimerFunc(interval, update, 0);
glutPostRedisplay();
}
In draw the model matrix is set and the rectangle is drawn:
void draw()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(playerX, playerY, 0);
//Draw player
drawRect(0, 0, playerWidth, playerHeight);
//Swap buffers to end
glutSwapBuffers();
}
Set the callback functions in main:
int main(int argc, char** argv)
{
// ...
glutDisplayFunc(draw);
glutTimerFunc(interval, update, 0);
glutKeyboardFunc( keyboardDown );
glutKeyboardUpFunc( keyboardUp );
// ...
}
I have problem understanding some opengl stuff using GLFW and GLEW.
i have 3 files shown below:
main.cpp:
#include "gamewindow.h"
int main() {
GameWindow *gameWindow = new GameWindow(1024, 768, "FirstOpenGLGame");
/* Loop until the user closes the window */
while (gameWindow->getRunning()) {
/* Render here */
gameWindow->render();
gameWindow->update();
gameWindow->setRunning();
}
delete gameWindow;
glfwTerminate();
return 0;
}
This is where the problem is, gamewindow.cpp:
#include "gamewindow.h"
GameWindow::GameWindow(int width, int height, const char* title) : _running(true), _height(1024), _width(1024 * (16/9))
{
/* Initialize the library */
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(width, height, title, NULL, NULL);
if(!window) {
glfwTerminate();
exit(0);
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
if(!glewInit()){ // <-- problem is this
glfwTerminate();
exit(EXIT_FAILURE);
}
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
coordSettings();
}
void GameWindow::setRunning() {
_running = !glfwWindowShouldClose(window);
}
bool GameWindow::getRunning() {
return _running;
}
void GameWindow::render() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_QUADS);
glVertex2d(0.0f, 0.0f);
glVertex2d(100.0f, 0.0f);
glVertex2d(100.0f, 800.0f);
glVertex2d(0.0f, 800.0f);
glEnd();
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
void GameWindow::update() {
}
void GameWindow::coordSettings() {
glViewport( 0, 0, _width, _height );
glMatrixMode(GL_PROJECTION);
glOrtho(0.0, _width, 0.0, _height, 0.0, -1.0);
glMatrixMode(GL_MODELVIEW);
}
and last the header file gamewindow.h:
#ifndef GAMEWINDOW_H
#define GAMEWINDOW_H
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
class GameWindow
{
private:
GLFWwindow* window;
bool _running;
GLfloat _width;
GLfloat _height;
void coordSettings();
public:
GameWindow(int width, int height, const char* title);
void setRunning();
bool getRunning();
void render();
void update();
};
#endif // GAMEWINDOW_H
everything works fine, but then i try to call glewInit() (without really understanding is i need to, or when i need to) but then nothing works. the program starts, but there is no window with a quad in it, like before. why is this? how is GLEW even used, and do i need it?
I am trying to draw a simple square wherever I press the left mouse button using opengl/glut. My program runs perfectly except for the part where it does not draw the square where I click the left mouse button :). Can someone point out what I am doing wrong?
#include <stdlib.h>
#include <glut.h>
GLsizei WIDTH = 1300, HEIGHT = 700;
GLsizei MOUSEx, MOUSEy;
GLfloat SIDE=1;
GLfloat RED[3] = {1,0,0};
GLfloat GREEN[3] = {0,1,0};
GLfloat BLUE[3] = {0,0,1};
GLfloat WHITE[3] = {1,1,1};
GLfloat BLACK[3] = {0,0,0};
GLfloat YELLOW[3] = {1,1,0};
GLfloat CYAN[3] = {0,1,1};
GLfloat MAGENTA[3] = {1,0,1};
void drawSquare(int x, int y)
{
glColor3fv(YELLOW);
glBegin(GL_POLYGON);
glVertex3f(x+SIDE, y+SIDE,0);
glVertex3f(x-SIDE, y+SIDE,0);
glVertex3f(x-SIDE, y-SIDE,0);
glVertex3f(x+SIDE, y-SIDE,0);
glEnd();
glFlush();
}
void drawSquare1()
{
int x=0,y=0;
glColor3fv(BLUE);
glBegin(GL_POLYGON);
glVertex3f(x+SIDE, y+SIDE,0);
glVertex3f(x-SIDE, y+SIDE,0);
glVertex3f(x-SIDE, y-SIDE,0);
glVertex3f(x+SIDE, y-SIDE,0);
glEnd();
glFlush();
}
void display (void) {
glClearColor (0.0,0.0,0.0,1.0);
glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0,0,-5);
drawSquare1();
glFlush();
}
void reshape (int w, int h) {
glViewport (0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
glMatrixMode (GL_MODELVIEW);
WIDTH=w;
HEIGHT=h;
}
void setX(int x)
{
MOUSEx=x;
}
void setY(int y)
{
MOUSEy=y;
}
void mouse(int btn, int state, int x, int y)
{
if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
{
setX(x);
setY(y);
drawSquare(MOUSEx,HEIGHT-MOUSEy);
glutPostRedisplay();
}
if(btn==GLUT_RIGHT_BUTTON && state==GLUT_DOWN)
{
exit(1);
}
}
int main (int argc, char **argv) {
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (WIDTH, HEIGHT);
glutInitWindowPosition (10, 10);
glutCreateWindow ("New Window");
glutDisplayFunc (display);
glutReshapeFunc (reshape);
glutMouseFunc(mouse);
//glutMotionFunc(drawSquare);
glutMainLoop ();
return 0;
}
In short words: OpenGL is not a scene graph. That means that those drawing commands issued in the mouse handler don't create some kind of "persistency".
Instead clicking the mouse you should store the position in a list/array and draw the squares from the values in that list in the display function.
I am fresh to this Coding But I done it.
Here is My Code It will Work.
#include <GL/glut.h>
GLsizei MOUSEx=0, MOUSEy=0;
GLfloat SIDE=50;
GLfloat BLUE[3] = {0,0,1};
void drawSquare1()
{
glColor3fv(BLUE);
glBegin(GL_POLYGON);
glVertex3f(MOUSEx, MOUSEy,0);
glVertex3f(MOUSEx+SIDE, MOUSEy,0);
glVertex3f(MOUSEx+SIDE, MOUSEy+SIDE,0);
glVertex3f(MOUSEx, MOUSEy+SIDE,0);
glEnd();
glFlush();
}
void display(void)
{
glClearColor (0.0,0.0,0.0,1.0);
glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity();
drawSquare1();
glFlush();
}
void reshape(int w, int h)
{
glViewport(0,0,(GLsizei)w,(GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
glOrtho(0.0,1368,768,0,-1.0,1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void spindisplay(void)
{
glutPostRedisplay();
}
void setX(int x)
{
MOUSEx=x;
}
void setY(int y)
{
MOUSEy=y;
}
void mouse(int btn, int state, int x, int y)
{
if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
{
setX(x);
setY(y);
//drawSquare(MOUSEx,HEIGHT-MOUSEy);
glutPostRedisplay();
}
if(btn==GLUT_RIGHT_BUTTON && state==GLUT_DOWN)
{
exit(1); // To Exit the Program
}
}
int main(int argc, char **argv)
{ glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(1366,768);
glutInitWindowPosition(0,0);
glutCreateWindow("Moving squares");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutIdleFunc(spindisplay);
glutMainLoop();
}
Below is the sample code taken from OpenGL Projects. Try to execute this and I think the problem with your drawing squares in OpenGL would be solve easily. If any question kindly ask.
#include<stdlib.h>
#include<glut.h>
GLsizei wh=500,ww=500;
GLfloat size=3.0;
void display()
{
glFlush();
}
void myInit()
{
glViewport(0,0,ww,wh);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,(GLdouble)ww,0.0,(GLdouble)wh);
glMatrixMode(GL_MODELVIEW);
glClearColor(0.0,0.0,0.0,1.0);
glColor3f(1.0,0.0,0.0);
}
void myReshape(GLsizei w,GLsizei h)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,(GLdouble)w,0.0,(GLdouble)h);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0,0,w,h);
ww=w;
wh=h;
}
void drawSquare(int x,int y)
{
y=wh-y;
glBegin(GL_POLYGON);
glVertex2f(x+size,y+size);
glVertex2f(x-size,y+size);
glVertex2f(x-size,y-size);
glVertex2f(x+size,y-size);
glEnd();
glFlush();
}
void size_menu(int id)
{
switch(id)
{
case 2: size=size*2;
break;
case 3:if(size>1) size=size/2;
break;
}
glutPostRedisplay();
}
void top_menu(int id)
{
switch(id)
{
case 1:exit(0);break;
}
glutPostRedisplay();
}
void myMouse(int button,int state,int x,int y)
{
if(button==GLUT_LEFT_BUTTON&&state==GLUT_DOWN)
drawSquare(x,y);
if(button==GLUT_RIGHT_BUTTON&&state==GLUT_DOWN)
exit(0);
}
int main(int argc,char **argv)
{
int sub_menu;
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutCreateWindow("Hierarchical Menus to Draw Squares");
glutDisplayFunc(display);
myInit();
glutMouseFunc(myMouse);
sub_menu=glutCreateMenu(size_menu);
glutAddMenuEntry("Increase Square Size",2);
glutAddMenuEntry("Decrease Square Size",3);
glutCreateMenu(top_menu);
glutAddMenuEntry("Quit",1);
glutAddSubMenu("Resize",sub_menu);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutReshapeFunc(myReshape);
glClear(GL_COLOR_BUFFER_BIT);
glutMainLoop();
return 0;
}
I'm doing 2D drawing in a glut window, and I'm having trouble making the window resize properly.
My glutDisplayFunc and glutReshapeFunc are below. As it is, the drawing disappears when the window is resized. If I delete the glClear() from displayFunc(), new pixels don't "initialize" and there are clipping problems. How do I fix this?
glutDisplayFunc:
void displayFunc() {
glDisable( GL_DEPTH_TEST );
glClear( GL_COLOR_BUFFER_BIT );
glPointSize ( 3.0 );
glFlush();
}
glutReshapeFunc:
void windowReshapeFunc( GLint newWidth, GLint newHeight ) {
glViewport( 0, 0, newWidth, newHeight );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( 0, GLdouble (newWidth), 0, GLdouble (newHeight) );
windowWidth = newWidth;
windowHeight = newHeight;
}
I'd try adding a call to glutPostRedisplay() around the end of your reshape function.
This is basic template that I use...
#include <GL/glut.h>
#include <stdio.h>
#include <stdarg.h>
#define WINDOW_WIDTH 300
#define WINDOW_HEIGHT 300
// current Window width and height
int win_width, win_height;
void display()
{
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
// write your code here
//
//
glutSwapBuffers();
}
void on_resize(int w, int h)
{
win_width = w;
win_height = h;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-w / 2, w / 2, -h / 2, h / 2, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
display(); // refresh window.
}
void main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutCreateWindow("basic resize template");
glutDisplayFunc(display);
glutReshapeFunc(on_resize);
glutMainLoop();
}
You can not call gluOrtho2D twice with the same window, it will break the graphics and so nothing shows up. You would have to init window size and set Ortho to the size of the screen (current display resolution), then draw relative to the size of the window.
I guess your code does not draw everything on scene in display func, you see, if no events occcur ever you have to call display one time and in the first time it has your drawing. But your problem rises when there is an event which says the window is resized! try putting your drawing part in display function. Like so,
void displayFunc() {
glDisable( GL_DEPTH_TEST );
glClear( GL_COLOR_BUFFER_BIT );
glPointSize ( 3.0 );
glBegin(GL_POINTS);
{
//Blah blah blah some points here which you expect to draw
}
glEnd();
glFlush();
}
Please post the full code if this was not helpful.
You're not setting the matrix mode back to GL_MODELVIEW at the end of your reshape function.
void reshape(int width, int height) {
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, GLdouble (width), 0, GLdouble (height) );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
It's hard to say if there's something else in your display function without seeing more code. I hope that helps.