SDL OpenGL
Posted: Sun Mar 07, 2010 4:39 pm
I'm making an OpenGL SDL app, and it compiles and runs. The screen clears to white like I want, but it doesn't draw the triangle and the program stops responding... I don't have any clue what would be wrong, I'm a noob to this so any help what so ever is appreciated!
Includes.h
Constants.h
SDLinit.h just holds the prototype for the function
SDLinit.cpp
OGLinit.h contains prototype
OGLinit.cpp
DrawStuff.h holds prototype
DrawStuff.cpp
Main.cpp
Includes.h
Code: Select all
#include <stdio.h>
#include <SDL.h>
#include <SDL_opengl.h>
#include "Constants.h"
Code: Select all
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
SDLinit.cpp
Code: Select all
#include "Includes.h"
#include "OGLinit.h"
#include "SDLinit.h"
//Sets up window and stuff
bool initVideo()
{
//init SDL
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
{
//if init didn't go well, report the error and return false
printf("SDLinit Fail: %s\n", SDL_GetError());
return false;
}
//if you exit, quit SDL
atexit(SDL_Quit);
//get some info about the player's computer
const SDL_VideoInfo *info = SDL_GetVideoInfo();
if (!info)
{
//I don't really need this, it shouldn't happen, but ya never know
printf("Video Query Failed: %s\n", SDL_GetError());
return false;
}
//get bpp
int bpp = info->vfmt->BitsPerPixel;
//Set up bits for color and some other stuff
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
if (SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, bpp, SDL_OPENGL | SDL_SWSURFACE) == 0)
{
printf("VideoMode Fail: %s\n", SDL_GetError());
return false;
}
//it all worked, now return oglsetup
return OGLSetup(SCREEN_WIDTH, SCREEN_HEIGHT);
}
OGLinit.cpp
Code: Select all
#include "Includes.h"
#include "SDLinit.h"
#include "OGLinit.h"
//does opengl stuff
bool OGLSetup(int width, int height)
{
//enable ogl stuff
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
//clear the screen with a nice white color
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
//set viewport to the screen width and height
glViewport(0, 0, width, height);
//start setting the camera perspective
glMatrixMode(GL_PROJECTION);
//reset the camera
glLoadIdentity();
//set perspective
gluPerspective(45.0, (double) width / (double) height, 1.0, 200.0);
return true;
}
DrawStuff.cpp
Code: Select all
#include "Includes.h"
void Draw()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//glPushMatrix();
glColor3f(0.0f, 0.0f, 1.0f);
glBegin(GL_TRIANGLES);
glVertex3f(0.0f, 0.0f, 4.0f);
glVertex3f(1.0f, 0.0f, 4.0f);
glVertex3f(1.0f, 1.0f, 4.0f);
glEnd();
//glPopMatrix();
SDL_GL_SwapBuffers();
}
Code: Select all
#include "Includes.h"
#include "SDLinit.h"
#include "OGLinit.h"
#include "DrawStuff.h"
int main(int argc, char *arv[])
{
if (!initVideo())
{
printf("SDL/OGL Setup Fail");
return 1;
}
SDL_Event event;
bool done = false;
while (!done)
{
if (event.type == SDL_KEYDOWN)
{
done = true;
}
Draw();
}
return 0;
}