dellwood
July 29th, 2009, 12:24 AM
Hi,
One of my SDL functions in "Engine.cpp" is spitting out an error that I can't seem to pinpoint. Here's the code.
Engine.h
#ifndef ENGINE_H
#define ENGINE_H
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;
class Init {
private:
int win_width, win_height, win_bpp; //used for setting up the window
string win_caption; //the text that goes in the window caption
int win_time; //the time the window stays open ( for dev purposes only )
string file, handle; //the filename and image handle strings
SDL_Surface *nativeImage; // used for loading image
SDL_Surface *optimizedImage; // used for optimizing the image
string filename; // filename for loadImage
int xCoord, yCoord; // placeSurface offsets
SDL_Surface *source;
SDL_Surface *destination; //for surface blitting
public:
SDL_Surface *screen; // needs to be used outside of function
Init();
~Init();
void Load( int win_width, int win_height, int win_bpp, string win_caption, int win_time );
SDL_Surface *loadImage( string filename );
void placeSurface( int xCoord, int yCoord, SDL_Surface *source, SDL_Surface* destination );
void Quit();
};
#endif // ENGINE_H
Engine.cpp
#include "Engine.h"
/* Constructor */
Init::Init() {
int win_width = 640;
int win_height = 480;
int win_bpp = 32;
string win_caption = "Default Caption";
int win_time = 3;
SDL_Surface *screen = NULL;
string filename = " ";
string handle = " ";
SDL_Surface *nativeImage = NULL;
SDL_Surface *optimizedImage = NULL;
int xCoord = 0;
int yCoord = 0;
SDL_Surface *source = NULL;
SDL_Surface *destination = NULL;
}
/* Destructor */
Init::~Init() {
}
/* Class Functions */
void Init::Load( int win_width, int win_height, int win_bpp, string win_caption, int win_time ) {
/* Initialize Subsystems */
if( SDL_Init( SDL_INIT_EVERYTHING < 0 ) ) {
cerr << "Failed to Initialize subsystems: ", SDL_GetError();
}
/* Set up screen */
screen = SDL_SetVideoMode( win_width, win_height, win_bpp, SDL_SWSURFACE );
/* Make sure screen initialized */
if( screen == NULL ) {
cerr << "Failed to load screen: ", SDL_GetError();
}
/* Set Caption */
SDL_WM_SetCaption( win_caption.c_str(), NULL );
/* Convert win_time from seconds to milliseconds */
win_time *= 1000;
/* Wait win_time seconds */
SDL_Delay( win_time );
}
SDL_Surface *loadImage( string filename ) {
/* Load the image */
SDL_Surface *nativeImage = IMG_Load( filename.c_str() );
if( nativeImage != NULL ) {
/* Optimize the image */
SDL_Surface *optimizedImage = SDL_DisplayFormat( nativeImage );
}
else {
cerr << "Error loading image: " << SDL_GetError() << endl;
}
return optimizedImage;
}
void placeSuface( int xCoord, int yCoord, SDL_Surface *source, SDL_Surface *destination ) {
/* Create a rectangle */
SDL_Rect surface;
/* Set the */
surface.x = xCoord;
surface.y = yCoord;
/* Blit the surface to the screen */
SDL_BlitSurface( source, NULL, destination, &surface);
}
void Init::Quit() {
/* Quit the program (more to be added here later */
SDL_Quit();
}
#include <iostream>
#include "Engine.h"
int main( int argc, char** args ) {
SDL_Surface * image;
/* Create class objects */
Init main_init;
/* Load the object functions */
main_init.Load( 640, 480, 32, "A Test Program", 3 );
image = main_init.loadImage( "image.png" );
main_init.placeSurface( 0, 0, image, main_init.screen );
main_init.Quit();
return 0;
}
And last but not least, the error:
g++ -o Main main.cpp Engine.cpp -lSDL -lSDL_image ~/Programs/C++/syi-engine
Engine.cpp: In function ‘SDL_Surface* loadImage(std::string)’:
Engine.cpp:68: error: ‘optimizedImage’ was not declared in this scope
It seems to think I haven't declared "optimized image," even though I did in the header as well as in the constructor...right ? :D I have been mulling this over for 30 + minutes without success:-k.
Thanks :).
-Josh
One of my SDL functions in "Engine.cpp" is spitting out an error that I can't seem to pinpoint. Here's the code.
Engine.h
#ifndef ENGINE_H
#define ENGINE_H
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;
class Init {
private:
int win_width, win_height, win_bpp; //used for setting up the window
string win_caption; //the text that goes in the window caption
int win_time; //the time the window stays open ( for dev purposes only )
string file, handle; //the filename and image handle strings
SDL_Surface *nativeImage; // used for loading image
SDL_Surface *optimizedImage; // used for optimizing the image
string filename; // filename for loadImage
int xCoord, yCoord; // placeSurface offsets
SDL_Surface *source;
SDL_Surface *destination; //for surface blitting
public:
SDL_Surface *screen; // needs to be used outside of function
Init();
~Init();
void Load( int win_width, int win_height, int win_bpp, string win_caption, int win_time );
SDL_Surface *loadImage( string filename );
void placeSurface( int xCoord, int yCoord, SDL_Surface *source, SDL_Surface* destination );
void Quit();
};
#endif // ENGINE_H
Engine.cpp
#include "Engine.h"
/* Constructor */
Init::Init() {
int win_width = 640;
int win_height = 480;
int win_bpp = 32;
string win_caption = "Default Caption";
int win_time = 3;
SDL_Surface *screen = NULL;
string filename = " ";
string handle = " ";
SDL_Surface *nativeImage = NULL;
SDL_Surface *optimizedImage = NULL;
int xCoord = 0;
int yCoord = 0;
SDL_Surface *source = NULL;
SDL_Surface *destination = NULL;
}
/* Destructor */
Init::~Init() {
}
/* Class Functions */
void Init::Load( int win_width, int win_height, int win_bpp, string win_caption, int win_time ) {
/* Initialize Subsystems */
if( SDL_Init( SDL_INIT_EVERYTHING < 0 ) ) {
cerr << "Failed to Initialize subsystems: ", SDL_GetError();
}
/* Set up screen */
screen = SDL_SetVideoMode( win_width, win_height, win_bpp, SDL_SWSURFACE );
/* Make sure screen initialized */
if( screen == NULL ) {
cerr << "Failed to load screen: ", SDL_GetError();
}
/* Set Caption */
SDL_WM_SetCaption( win_caption.c_str(), NULL );
/* Convert win_time from seconds to milliseconds */
win_time *= 1000;
/* Wait win_time seconds */
SDL_Delay( win_time );
}
SDL_Surface *loadImage( string filename ) {
/* Load the image */
SDL_Surface *nativeImage = IMG_Load( filename.c_str() );
if( nativeImage != NULL ) {
/* Optimize the image */
SDL_Surface *optimizedImage = SDL_DisplayFormat( nativeImage );
}
else {
cerr << "Error loading image: " << SDL_GetError() << endl;
}
return optimizedImage;
}
void placeSuface( int xCoord, int yCoord, SDL_Surface *source, SDL_Surface *destination ) {
/* Create a rectangle */
SDL_Rect surface;
/* Set the */
surface.x = xCoord;
surface.y = yCoord;
/* Blit the surface to the screen */
SDL_BlitSurface( source, NULL, destination, &surface);
}
void Init::Quit() {
/* Quit the program (more to be added here later */
SDL_Quit();
}
#include <iostream>
#include "Engine.h"
int main( int argc, char** args ) {
SDL_Surface * image;
/* Create class objects */
Init main_init;
/* Load the object functions */
main_init.Load( 640, 480, 32, "A Test Program", 3 );
image = main_init.loadImage( "image.png" );
main_init.placeSurface( 0, 0, image, main_init.screen );
main_init.Quit();
return 0;
}
And last but not least, the error:
g++ -o Main main.cpp Engine.cpp -lSDL -lSDL_image ~/Programs/C++/syi-engine
Engine.cpp: In function ‘SDL_Surface* loadImage(std::string)’:
Engine.cpp:68: error: ‘optimizedImage’ was not declared in this scope
It seems to think I haven't declared "optimized image," even though I did in the header as well as in the constructor...right ? :D I have been mulling this over for 30 + minutes without success:-k.
Thanks :).
-Josh