I have just started learning OpenGL and I am attempting to make a game of pong.
I'm having trouble getting the paddle to appear on the screen and I can't fathom out why: I thought with this code it should appear in the top left corner and move when down and up keys are pressed.
main.cpp -
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
#include "classes.h"
#include "functions.h"
int main (int argc, char *argv[])
{
init_everything();
PlayerPaddle paddle;
bool quit = false;
while( quit == false )
{
while( SDL_PollEvent( &event ) )
{
paddle.handle_input();
if( event.type == SDL_QUIT )
{
quit = true;
}
}
glClear( GL_COLOR_BUFFER_BIT );
paddle.show();
SDL_GL_SwapBuffers();
}
SDL_Quit();
return 0;
}
classes.h -
#ifndef CLASSES_H
#define CLASSES_H
SDL_Event event;
// ******************* Beginning of PlayerPaddle class *******************
class PlayerPaddle
{
private:
int xloc;
int yloc;
int paddle_height;
int paddle_width;
public:
PlayerPaddle();
void show();
void handle_input();
};
PlayerPaddle::PlayerPaddle()
{
int xloc = 0;
int yloc = 0;
int paddle_height = 50;
int paddle_width = 15;
}
void PlayerPaddle::show()
{
glBegin( GL_QUADS );
glColor4f( 1.0, 1.0, 1.0, 1.0 );
glVertex3f( 0, yloc, 0 );
glVertex3f( paddle_width, yloc, 0 );
glVertex3f( paddle_width, paddle_height, 0 );
glVertex3f( 0, paddle_height, 0 );
glEnd();
glLoadIdentity();
}
void PlayerPaddle::handle_input()
{
if( event.type == SDL_KEYDOWN )
{
switch( event.key.keysym.sym )
{
case SDLK_UP:
yloc -= 10;
paddle_height -= 10;
break;
case SDLK_DOWN:
yloc += 10;
paddle_height += 10;
break;
}
}
if (yloc < 0)
{
yloc += 10;
paddle_height += 10;
}
if (yloc > 640)
{
yloc -= 10;
paddle_height -= 10;
}
}
// ******************* End of the PlayerPaddle class *******************
#endif
functions.h -
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
void init_GL()
{
glClearColor( 0, 0, 0, 0 );
glViewport(0, 0, 640, 480);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0, 640, 480, 0, 1, -1 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
}
void init_everything()
{
SDL_Init( SDL_INIT_EVERYTHING );
SDL_SetVideoMode( 640, 480, 32, SDL_OPENGL );
init_GL();
SDL_WM_SetCaption( "Pong by Michael Clover", NULL );
}
#endif
Give this a shot:
#include <SDL.h>
#include <SDL_opengl.h>
class PlayerPaddle
{
private:
int xloc;
int yloc;
int paddle_height;
int paddle_width;
public:
PlayerPaddle()
{
xloc = 0;
yloc = 0;
paddle_height = 50;
paddle_width = 15;
}
void show()
{
glPushMatrix();
glTranslatef( xloc, yloc, 0 );
glBegin( GL_QUADS );
glColor4f( 1.0, 1.0, 1.0, 1.0 );
glVertex2f( 0, 0 );
glVertex2f( paddle_width, 0 );
glVertex2f( paddle_width, paddle_height );
glVertex2f( 0, paddle_height );
glEnd();
glPopMatrix();
}
void handle_input( const SDL_Event& event )
{
if( event.type == SDL_KEYDOWN )
{
switch( event.key.keysym.sym )
{
case SDLK_UP: yloc += 10; break;
case SDLK_DOWN: yloc -= 10; break;
}
}
if (yloc < 0)
{
yloc = 0;
}
if (yloc > 640)
{
yloc = 640;
}
}
};
void init_GL()
{
glClearColor( 0, 0, 0, 0 );
glViewport(0, 0, 640, 480);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0, 640, 0, 480, -1, 1 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
}
void init_everything()
{
SDL_Init( SDL_INIT_EVERYTHING );
SDL_SetVideoMode( 640, 480, 32, SDL_OPENGL );
init_GL();
SDL_WM_SetCaption( "Pong by Michael Clover", NULL );
}
int main (int argc, char *argv[])
{
init_everything();
PlayerPaddle paddle;
bool quit = false;
while( quit == false )
{
SDL_Event event;
while( SDL_PollEvent( &event ) )
{
paddle.handle_input( event );
if( event.type == SDL_QUIT )
{
quit = true;
}
}
glClear( GL_COLOR_BUFFER_BIT );
paddle.show();
SDL_GL_SwapBuffers();
}
SDL_Quit();
return 0;
}
Most notably your constructor was borked. You were declaring and setting local variables instead of your member variables. Which resulted in uninitialized member variables, way off in la-la land (-80 thousand or so on my machine) and (obviously) nowhere near your viewport :)
I switched the glOrtho() call around to produce a standard Cartesian coordinate system, with (0,0) in the lower-left and (640,480) in the upper-right.
Related
I'm trying to just create a simple program that draws points where I click.
I declare a struct that holds the coordinates and the color of each point.
I declare a global vector< Point > that holds the list of points. The mouse input is well recorded and printed on the cout, but the points are not draw.
Any idea why I am missing in the initialization of GLUT?
#include <GL/freeglut_std.h>
#include <GL/gl.h>
#include <vector>
#include <iostream>
using namespace std;
struct Point
{
float x, y;
unsigned char r, g, b, a;
};
vector< Point > points;
void drawPoints()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-50, 50, -50, 50, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// draw
glColor3ub( 255, 255, 255 );
glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState( GL_COLOR_ARRAY );
glVertexPointer( 2, GL_FLOAT, sizeof(Point), &points[0].x );
glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(Point), &points[0].r );
glPointSize( 3.0 );
glDrawArrays( GL_POINTS, 0, points.size() );
glDisableClientState( GL_VERTEX_ARRAY );
glDisableClientState( GL_COLOR_ARRAY );
glFlush();
glutSwapBuffers();
}
void OnMouseClick(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)
{
//store the x,y value where the click happened
Point p = Point();
p.x = (float)x;
p.y = (float)y;
p.r = 255;
p.g = 255;
p.b = 255;
p.a = 255;
points.push_back(p);
for (vector<Point>::iterator it = points.begin(); it != points.end(); ++it)
{
cout << it->x << " " << it->y << " " << flush;
}
cout << endl;
glutPostRedisplay();
}
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("OpenGL - Drawing points");
glutDisplayFunc(drawPoints);
glutMouseFunc(OnMouseClick);
glutMainLoop();
return 0;
}
Coordinate system mismatch, either transform GLUT's mouse coordinates into your chosen glOrhto() coordinate system or change that call to match GLUT's mouse coordinate system.
Example of the second way:
#include <GL/glut.h>
#include <vector>
using namespace std;
struct Point
{
float x, y;
unsigned char r, g, b, a;
};
vector< Point > points;
void drawPoints()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
GLdouble w = glutGet( GLUT_WINDOW_WIDTH );
GLdouble h = glutGet( GLUT_WINDOW_HEIGHT );
glOrtho( 0, w, h, 0, -1, 1 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
// draw
if( !points.empty() )
{
glColor3ub( 255, 255, 255 );
glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState( GL_COLOR_ARRAY );
glVertexPointer( 2, GL_FLOAT, sizeof( Point ), &points[ 0 ].x );
glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof( Point ), &points[ 0 ].r );
glPointSize( 3.0 );
glDrawArrays( GL_POINTS, 0, points.size() );
glDisableClientState( GL_VERTEX_ARRAY );
glDisableClientState( GL_COLOR_ARRAY );
}
glutSwapBuffers();
}
void OnMouseClick( int button, int state, int x, int y )
{
if( button == GLUT_LEFT_BUTTON && state == GLUT_UP )
{
//store the x,y value where the click happened
Point p = Point();
p.x = (float)x;
p.y = (float)y;
p.r = 255;
p.g = 255;
p.b = 255;
p.a = 255;
points.push_back( p );
glutPostRedisplay();
}
}
int main( int argc, char **argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
glutInitWindowSize( 500, 500 );
glutInitWindowPosition( 100, 100 );
glutCreateWindow( "OpenGL - Drawing points" );
glutDisplayFunc( drawPoints );
glutMouseFunc( OnMouseClick );
glutMainLoop();
return 0;
}
I am having issues figuring out why the camera wont move! I think i setup the code correct. Any pointers?
When I launch it the triangle comes up but i cannot move the camera, most likely its my whole method behind the camera movement that is to blame due to inexperience. Could somebody point me in the right direction?
include "stdafx.h"
#include <GL/glut.h>
#include <GL/freeglut.h>
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
//THESE ARE MY VARIABLES
//===========
int x = 0; //|
int y = 0; //|
int z = 0; //|
//===========
//Camera Movement
void glViewport(GLint x, GLint y, GLsizei width, GLsizei height)
{
if (WM_KEYDOWN == VK_UP) {
std::cout << "User pressed UP!";
++z;
}
if (WM_KEYDOWN == VK_DOWN) {
std::cout << "User pressed DOWN!";
--z;
}
if (WM_KEYDOWN == VK_LEFT) {
std::cout << "User pressed LEFT!";
--x;
}
if (WM_KEYDOWN == VK_RIGHT) {
std::cout << "User pressed RIGHT!";
++x;
}
}
void display()
{
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_QUADS);
glColor3f(1.0, 0, 0);
glVertex3f(0.5, 0.5, 1);
glColor3f(0, 1.0, 0);
glVertex3f(0.5, 0, 1);
glColor3f(0, 0, 1.0);
glVertex3f(0, 0.5, 1);
glVertex3f(0.5, 0.5, 1);
glEnd();
glFlush();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(640, 480);
glutInitWindowPosition(10, 10);
glutCreateWindow("McDank");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
You need to register a keyboard callback with GLUT via glutKeyboardFunc() or glutSpecialFunc() to handle keystrokes.
Your glViewport() function is...bizarre. Not sure what you were trying to achieve here with comparing random Windows enums against other enums.
You need to actually use your x/y/z variables in your display function, perhaps in a glTranslate().
All together:
#include <GL/glut.h>
float x = 0;
float y = 0;
float z = 0;
void special( int key, int, int )
{
const float step = 0.1;
if( GLUT_KEY_LEFT == key )
x -= step;
if( GLUT_KEY_RIGHT == key )
x += step;
if( GLUT_KEY_UP == key )
y += step;
if( GLUT_KEY_DOWN == key )
y -= step;
glutPostRedisplay();
}
void display()
{
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( -2, 2, -2, 2, -1, 1 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glTranslatef( x, y, z );
glBegin( GL_TRIANGLES );
glColor3ub( 255, 0, 0 );
glVertex2f( -1, -1 );
glColor3ub( 0, 255, 0 );
glVertex2f( 1, -1 );
glColor3ub( 0, 0, 255 );
glVertex2f( 0, 1 );
glEnd();
glutSwapBuffers();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(600, 600);
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
glutCreateWindow("McDank");
glutSpecialFunc( special );
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
My complete code -
#include <SDL.h>
#include <SDL_opengl.h>
#include <GL\GLU.h>
#include <fstream>
using namespace std;
int index = 0;
int speed = 0;
int frames = 0;
int lasttime = 0;
int mousex = 0;
int mousey = 0;
bool postRedraw = true;
int screenWidth = 640;
int screenHeight = 480;
float screenAspect = screenWidth/screenHeight;
bool init();
bool initGL();
void handleKeys( unsigned char key, int x, int y );
void update();
void render();
void close();
void Initialize();
void handleEvents();
ofstream logFile;
SDL_Window* gWindow = NULL;
bool isRunning = true;
SDL_GLContext gContext;
float xpos = 0.0;
float ypos = 0.0;
float zpos = 0.0;
void Initialize()
{
gluPerspective(60.0f, screenAspect, 1.0, 400.0);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_NORMALIZE);
glTranslatef(0.0f, 0.0f, -5.0f);
glFrontFace(GL_CCW);
glEnable(GL_LIGHT0);
}
void render()
{
glTranslatef(xpos, ypos, zpos);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_TRIANGLES);
glVertex3f(-1.0f,0.0f,-1.0f);
glVertex3f(0.0f, 1.0f, -1.0f);
glVertex3f(0.0f, 2.0f, -2.0f);
glEnd();
postRedraw = true;
}
void handleEvents()
{
const Uint8* keystates = SDL_GetKeyboardState(NULL);
if(keystates[SDL_GetScancodeFromKey(SDLK_w)])
{
zpos += 0.01;
}
if(keystates[SDL_GetScancodeFromKey(SDLK_s)])
{
zpos -= 0.01;
} else {
}
}
bool init()
{
bool success = true;
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
logFile<<"SDL could not initialize! SDL Error: "<<SDL_GetError()<<endl;
success = false;
}
else
{
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 2 );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 1 );
gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, screenWidth, screenHeight, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE );
if( gWindow == NULL )
{
logFile<<"Window could not be created! SDL Error: "<<SDL_GetError()<<endl;
success = false;
}
else
{
gContext = SDL_GL_CreateContext( gWindow );
if( gContext == NULL )
{
logFile<<"OpenGL context could not be created! SDL Error: "<<SDL_GetError()<<endl;
success = false;
}
else
{
if( SDL_GL_SetSwapInterval( 1 ) < 0 )
{
logFile<<"Warning: Unable to set VSync! SDL Error: "<<SDL_GetError()<<endl;
}
if( !initGL() )
{
logFile<<"Unable to initialize OpenGL!"<<endl;
success = false;
}
}
}
}
logFile.open("log.txt");
if(logFile.is_open())
{
success = true;
} else {
success = false;
}
return success;
}
bool initGL()
{
bool success = true;
GLenum error = GL_NO_ERROR;
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
error = glGetError();
if( error != GL_NO_ERROR )
{
logFile<<"Error initializing OpenGL! "<<gluErrorString( error )<<endl;
success = false;
}
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
error = glGetError();
if( error != GL_NO_ERROR )
{
logFile<<"Error initializing OpenGL! "<<gluErrorString( error )<<endl;
success = false;
}
glClearColor( 0.f, 0.f, 0.f, 1.f );
error = glGetError();
if( error != GL_NO_ERROR )
{
logFile<<"Error initializing OpenGL! "<<gluErrorString( error );
success = false;
}
return success;
}
void close()
{
logFile.close();
SDL_DestroyWindow( gWindow );
gWindow = NULL;
SDL_Quit();
}
int main( int argc, char* args[] )
{
if( !init() )
{
logFile<<"Failed to initialize!"<<endl;
}
else
{
SDL_Event event;
Initialize();
lasttime = SDL_GetTicks();
while(isRunning)
{
while( SDL_PollEvent( &event ) != 0 )
{
if(event.type == SDL_QUIT)
{
isRunning = false;
} else if(event.type == SDL_MOUSEMOTION) {
SDL_GetMouseState(&mousex, &mousey);
} else if(event.type == SDL_MOUSEBUTTONDOWN) {
SDL_GetMouseState(&mousex, &mousey);
} else if(event.type == SDL_MOUSEBUTTONUP) {
SDL_GetMouseState(&mousex, &mousey);
}
}
handleEvents();
if(postRedraw)
{
postRedraw = false;
render();
SDL_GL_SwapWindow( gWindow );
}
if((SDL_GetTicks()-lasttime)>=1000)
{
lasttime = SDL_GetTicks();
speed = frames;
logFile<<speed<<endl;
frames = 0;
} else {
frames++;
}
}
}
close();
return 0;
}
The above code is just for testing, the minimalistic code where I have problem is -
void handleEvents()
{
const Uint8* keystates = SDL_GetKeyboardState(NULL);
if(keystates[SDL_GetSancodeFromKey(SDLK_w)]) {
zpos += 0.01;
}
if(keystates[SDL_GetScancodeFromKey(SDLK_s)])
{
zpos -= 0.01;
} else {
}
}
Expected:
the trangle drawn will move forword/ backward while w/s key is pressed.
Output:
the triangle keeps on moving in the desired direction even if the key was released.
You accumulate your matrix changes over time. glTranslatef modifies current matrix with translation. The next frame it modifies it again. And again, ....
You shoud reset matrix with glLoadIdentity() call. Be aware that it modifies currently selected matrix and will undo matrix changes you've made in Initialize (at least your glTranslatef here - perspective should go to GL_PROJECTION matrix, which you haven't done).
how can drawing like square or TRIANGLES when clicked on any position on screen show the shape this is my Attempt i do't know what doing
...........................................................................
.......................................................................................
#include <windows.h>
#include <gl/Gl.h>
#include <gl/glu.h>
#include <gl/glut.h>
GLsizei wh=500,ww=500;
GLfloat size=3.0;
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 mymose(int button,int state,int x,int y)
{
if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
drawsquare(x,y);
//if(button==GLUT_RIGHT_BUTTON_BUTTON && state==GLUT_UP)
// exit();
}
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);
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
glutInitWindowSize(ww,wh);
glutInitWindowPosition(100,100);
glutCreateWindow("GLUT");
glutMouseFunc(mymose);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
You need a vector to store the points in.
Add points in the mouse callback and request a redraw via glutPostRedisplay().
Then in display() you can spin over all the points in the vector and draw quads around them:
#include <GL/glut.h>
#include <vector>
struct Point
{
Point( float x, float y ) : x(x), y(y) {}
float x, y;
};
std::vector< Point > points;
void mouse( int button, int state, int x, int y )
{
if( button == GLUT_LEFT_BUTTON && state == GLUT_UP )
{
points.push_back( Point( x, y ) );
}
if( button == GLUT_RIGHT_BUTTON && state == GLUT_UP )
{
points.clear();
}
glutPostRedisplay();
}
void display()
{
glClearColor( 0, 0, 0, 1 );
glClear( GL_COLOR_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
int w = glutGet( GLUT_WINDOW_WIDTH );
int h = glutGet( GLUT_WINDOW_HEIGHT );
glOrtho( 0, w, h, 0, -1, 1 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glColor3ub( 255, 0, 0 );
glBegin( GL_QUADS );
for( size_t i = 0; i < points.size(); ++i )
{
const unsigned int SIZE = 20;
const Point& pt = points[ i ];
glVertex2i( pt.x - SIZE, pt.y - SIZE );
glVertex2i( pt.x + SIZE, pt.y - SIZE );
glVertex2i( pt.x + SIZE, pt.y + SIZE );
glVertex2i( pt.x - SIZE, pt.y + SIZE );
}
glEnd();
glutSwapBuffers();
}
int main( int argc, char **argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
glutInitWindowSize( 640, 480 );
glutCreateWindow( "GLUT" );
glutMouseFunc( mouse );
glutDisplayFunc( display );
glutMainLoop();
return 0;
}
I seem to be having the same trouble over and over again and cannot get my thick head around it. This program will not display a paddle as I want (and expected it) to do on screen :( -
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
SDL_Event event;
const int paddle_width = 20;
const int paddle_height = 80;
class PlayerPaddle
{
private:
int xloc;
int yloc;
int yvel;
public:
PlayerPaddle()
{
int xloc = 20;
int yloc = 200;
int yvel = 0;
}
void ShowPaddle()
{
glTranslatef(xloc,yloc,0);
glBegin(GL_QUADS);
glColor4f(1.0,1.0,1.0,1.0);
glVertex3f(0,0,0);
glVertex3f(20,0,0);
glVertex3f(20,80,0);
glVertex3f(0,80,0);
glEnd();
glLoadIdentity();
}
void MovePaddle()
{
yloc += yvel;
}
void Handle_Input()
{
if( event.type == SDL_KEYDOWN )
{
switch( event.key.keysym.sym )
{
case SDLK_UP: yvel -= paddle_height / 2; break;
case SDLK_DOWN: yvel += paddle_height / 2; break;
}
}
else if( event.type == SDL_KEYUP )
{
switch( event.key.keysym.sym )
{
case SDLK_UP: yvel += paddle_height / 2; break;
case SDLK_DOWN: yvel -= paddle_height / 2; break;
}
}
}
};
void init ()
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_SetVideoMode(640,480,32,SDL_OPENGL);
SDL_WM_SetCaption( "Pong", NULL );
glClearColor( 0, 0, 0, 0 );
glViewport(0, 0, 640, 480);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0, 640 , 480 , 0, 0, 1 );
glMatrixMode( GL_MODELVIEW );
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
}
int main (int argc, char **argv)
{
init();
PlayerPaddle ppaddle;
bool quit = false;
while (quit == false)
{
while (SDL_PollEvent (&event))
{
ppaddle.Handle_Input();
if (event.type == SDL_QUIT)
quit = true;
}
ppaddle.MovePaddle();
glClear(GL_COLOR_BUFFER_BIT);
ppaddle.ShowPaddle();
SDL_GL_SwapBuffers();
}
SDL_Quit();
return 0;
}
I just get a clear black screen until I quit the program which returns 0 as expected. I know the code:
ppaddle.MovePaddle();
glClear(GL_COLOR_BUFFER_BIT);
ppaddle.ShowPaddle();
SDL_GL_SwapBuffers();
iterates at least once as if I inserted a return 1; statement in there the program would start up and return 1 immediately.
At a minimum you screwed up your constructor. Again.
It should probably be this:
PlayerPaddle()
{
xloc = 20;
yloc = 200;
yvel = 0;
}
You may also want to toss in a SDL_Delay(100) at the end of your main loop to slow things down until you get some more sophisticated physics handling going.