Original Post
So I've been trying to get an object oriented version of SDL (version 1.2.13) code up and working for the past few days. This is eventually going to be the graphics engine for a real-time parlor game. Currently, all my code compiles and follows the same logical steps that any other code I've written does. In fact, it even shows a window that is the correct size and with the correct image (when I give it one). The problem is that as soon as SDL draws to the screen once, it locks up and the window freezes. It cannot be closed, moved, and stays as a static image. The most annoying part of this all is that I get no compile errors and that when I run it in debug mode within Visual Studio 2008 and set some breakpoints before each SDL_Flip() routine, it appears to be updating fine in the code, but the window continues to lock up. I have no clue what's going on and I'm hoping someone here has tried the same and overcome this problem. A ZIP of the project files and a VS 2008 SLN file can be found here. The source files are also stored there separately if that is what you're interested in. I'll include the files in the post as well if you prefer to look at them that way. I appreciate any input anyone has on this problem. Thanks for taking your time to help me out! Matt Fielding [Edited by - Itzmattu on March 29, 2009 7:40:50 PM]
/*!
*
* \file Graphics.h
*
* \author Matt Fielding & Zori Babroudi
*
* \par email: ---
*
* \date 3/27/2009
*
* Header file for Graphics.cpp.
*
*/
#ifndef _GRAPHICS_H_
#define _GRAPHICS_H_
#include "SDL.h"
#include <string>
/*!
* \class Graphics Graphics.h
* \brief This is the main class for the Graphics engine.
*
* The Graphics class sets up SDL, the window, and has methods to draw
* the screen.
*/
namespace UMLGDG {
class Graphics
{
public:
// Graphics constructor.
Graphics();
// Graphics destructor.
~Graphics();
// Getters for private variables.
int getScreenWidth() const;
int getScreenHeight() const;
int getScreenBPP() const;
int getFPS() const;
std::string getConfigFile() const;
// Method to draw all current objects to the screen.
void draw();
protected:
// No protected members needed.
private:
// Config file for Graphics.
static const std::string CONFIG_FILE;
// Important variables used to setup window.
int SCREEN_WIDTH;
int SCREEN_HEIGHT;
int SCREEN_BPP;
int FRAMES_PER_SECOND;
// SDL_Surface used as main surface for window.
SDL_Surface *screen;
// Method to parse config file and save values.
bool configParse();
// Method to apply a surface to another surface (usually the screen).
void apply_surface( int x, int y, SDL_Surface *source, SDL_Surface *destination );
}; // End Graphics class.
} // End UMLGDG namespace.
#endif
/*!
*
* \file Graphics.cpp
*
* \author Matt Fielding & Zori Babroudi
*
* \par email: ---
*
* \date 3/27/2009
*
* This file implements the Graphics class.
*
* The job of the Graphics class is to setup the window using SDL, and
* draw whatever data is currently on the screen when called to do so.
*
*/
#include "Graphics.h"
#include "graphics_util.h"
#include "SDL.h"
#include <string>
#include <map>
#include <fstream>
#include <iostream>
#include <sstream>
const std::string UMLGDG::Graphics::CONFIG_FILE = "graphics.conf";
UMLGDG::Graphics::Graphics()
{
configParse();
SDL_Init( SDL_INIT_EVERYTHING );
SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
SDL_WM_SetCaption( "G.R.I.D.", NULL );
}
UMLGDG::Graphics::~Graphics()
{
// Quit SDL
SDL_Quit();
}
bool UMLGDG::Graphics::configParse()
{
std::string temp;
std::map<std::string, int> varMap;
std::ifstream infile;
infile.open(CONFIG_FILE.c_str(), std::fstream::in);
if(!infile)
{
return false;
}
while (!infile.eof())
{
std::string var;
int val;
getline(infile, temp);
std::istringstream buffer(temp);
buffer >> var >> val;
varMap[var] = val;
}
infile.close();
if (varMap.size() != 3)
{
return false;
}
SCREEN_HEIGHT = varMap["SCREEN_HEIGHT"];
SCREEN_WIDTH = varMap["SCREEN_WIDTH"];
SCREEN_BPP = varMap["SCREEN_BPP"];
return true;
} // End configParse() definition.
void UMLGDG::Graphics::apply_surface( int x, int y, SDL_Surface *source, SDL_Surface *destination )
{
// Make a temporary SDL_Rect to hold a rectangle.
SDL_Rect offset;
// Give the offsets to the rectangle.
offset.x = x;
offset.y = y;
// Blit the surface
SDL_BlitSurface( source, NULL, destination, &offset );
}
void UMLGDG::Graphics::draw()
{
return;
}
#include "SDL.h"
#include "Graphics.h"
int main(int argc, char *argv[])
{
UMLGDG::Graphics *gfxEngine = new UMLGDG::Graphics();
bool quit = false;
while( quot == false )
{
gfxEngine->draw();
}
return 0;
}