c++ game engine error

accessor func not working..

Page 1 of 1

7 Replies - 2015 Views - Last Post: 23 August 2009 - 12:11 PM Rate Topic: -----

#1 DustyD  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 75
  • Joined: 19-August 09

c++ game engine error

Posted 23 August 2009 - 10:22 AM

Hi all,

I've recently started a game engine project using SDL,fmod and opengl..

I have a small problem with an accessor function that's supposed to return an SDL_Surface*...
But when I compile, it says an lvalue is required as left hand of operand..

This is my code:

OG.h (All classes should include this header..)
/***********************************************************************************************************************
**  OG.h created by Louis Fry 04/08/09
**
**
**
***********************************************************************************************************************/




#ifndef __OG_BASE_H__
#define __OG_BASE_H__





//#include ".h"
//#include ".h" 


// Sound manipulation library
#include <fmod.h>
//#include <fmod.hpp>


// Graphics library
#ifdef __APPLE__
#  include <GLUT/glut.h>
#else
#  include <GL/glut.h>
#endif



// Window management library
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>



// usefull headers
#include <cmath>
#include <vector>
#include <string>
#include <cstdlib>
#include <unistd.h>
#include <iostream>
#include <cstdio>


// OG specific Headers 

#include "OGTypes.h"
#include "OGEngine.h"


#endif



This is the types file:

OGTypes.h
#ifndef __OG_TYPES_H__
#define __OG_TYPES_H__




typedef char OGchar;
typedef char* OGcstring;

typedef int OGint;
typedef short OGshort;
typedef double OGdouble;
typedef long OGlong;
typedef float OGfloat;

typedef bool OGbool;
typedef void* OGpointer;


typedef unsigned char OGuchar;
typedef unsigned int OGuint;
typedef unsigned short OGushort;
typedef unsigned long OGulong;

// Optionally

typedef long int OGlint;
typedef unsigned long int OGulint;

typedef long long int OGint64;
typedef unsigned long long int OGuint64;



#endif



This defines the engine class OGEngine.h
#ifndef __OG_ENGINE_H__
#define __OG_ENGINE_H__

#include "OGTypes.h"

struct OGEngine
{  
   /* Pointer to this structure */
   static OGEngine* engine; // engine->status()...;
   
   /* Set graphics resolution */
   OGuint w_res;
   OGuint h_res;
   
   /* Set graphics depth */
   OGuint depth;
   
   /* Set flags to pass to sdl video mode */
   OGuint flags;
   
   /* Surface the graphics lay on */
   SDL_Surface * OG_Main_Frame;
  
   /* Default Constructor */
   OGEngine(OGuint,OGuint,OGuint,SDL_Surface *);
   
   /* Destructor set to virtual to enable use anywhere in derived class(OGsystem) */
   virtual ~OGEngine();
   
   
   // ACCESSORS
   inline OGEngine* OGeng_get_engine(){ return engine; };
   
   inline SDL_Surface * OGeng_get_window_surface(){ return OG_Main_Frame; };
   
   inline OGuint OGeng_get_width_res(){ return w_res; };
   
   inline OGuint OGeng_get_height_res(){ return h_res; };
   
   inline OGuint OGeng_get_pixel_depth(){ return depth; };
   
   inline OGuint OGeng_set_flags(OGuint f1,OGuint f2){ flags = f1; flags |= f2; return flags; };
   
   // END ACCESSORS
   
   
   /* Initialize the engine (window with sdl set with opengl attributes) */
   static void OGInit();
   
   /* Create the OpenGL context and setup the viewport **
   ** All drawing functions will be using opengl.	  */
   static void OGInitDisplay();
   
   /* Quit the gl context and application */
   static void OGQuit();
   
};


#endif /*__OG_ENGINE_H__*/




This is OGEngine.cpp:
#include "OG.h"

/* Default constructor */
OGEngine::OGEngine(OGuint w,OGuint h,OGuint d,SDL_Surface * surface)
{ 
   w_res = w,
   h_res = h,
   depth = d,
   OG_Main_Frame = surface;
}



/* Destructor */
OGEngine::~OGEngine()
{
   w_res = 0, 
   h_res = 0,
   depth = 0;
}




/* Initialise sdl video mode with Opengl attributes */
void OGEngine::OGInit()
{
   // Start sdl with complete opengl settings
   if(SDL_Init(SDL_INIT_VIDEO) != 0)
   {
	  printf("\nSDL Init failed on error: %s",SDL_GetError());
	  return;
   }
   
   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_ALPHA_SIZE, 8);
   SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
   SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
   SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
   
}





/* Setup an Opengl rendering context on sdl surface */
void OGEngine::OGInitDisplay()
{
   /* Create the window with engine's properties */
   engine->OGeng_get_window_surface() = SDL_SetVideoMode( engine->OGeng_get_width_res(),
														  engine->OGeng_get_height_res(),
														  engine->OGeng_get_pixel_depth(),
														  engine->OGeng_set_flags(SDL_OPENGL,SDL_FULLSCREEN) );
}



/* Quit opengl and sdl */
void OGEngine::OGQuit()
{
   //if(OG_Main_Frame) SDL_FreeSurface(OG_Main_Frame);
   //~OGEngine();
   SDL_Quit();
}




And finnaly this is the test program:

#include "OG.h"




int main()
{
   SDL_Surface * screen;
   OGEngine new_engine(1024,768,32,screen);
   
   OGEngine::OGInit();
   OGEngine::OGInitDisplay();
   return 0;
}




I am using gPlusPlus in Linux Debian Lenny

This is my command:

gPlusPlus -o t OGEngine.cpp OGtest.cpp `sdl-config --libs --cflags`

and this is the error:

OGEngine.cpp: In static member function ‘static void OGEngine::OGInitDisplay()’:
OGEngine.cpp:65: error: lvalue required as left operand of assignment


This line seems to not be returning anything
engine->OGeng_get_window_surface()



Can anyone tell me wassup with this?? Could it be my usage of SDL??

Is This A Good Question/Topic? 0
  • +

Replies To: c++ game engine error

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: c++ game engine error

Posted 23 August 2009 - 10:38 AM

You're returning a pointer and you are trying to set the location of that pointer to something. Dereference it.


Example using primitives:

#include <iostream>
using namespace std;

//global for example
int* thePtr;

int* function()		{return thePtr;};


int main()
{
	int x;
	thePtr = &x;
	*(function()) = 8; 


	cout << *thePtr << endl;
	return 0;
}



However, you're trying to get the pointer to set it. since it's global, why not just set it manually? You'll use the accessor function later on down the road once the pointer has actually been assigned something.


Take the above code example. If I try to run this:

#include <iostream>
using namespace std;

//global for example
int* thePtr;

int* function()		{return thePtr;};


int main()
{
	int x;
	//thePtr = &x;
	*(function()) = 8; 


	cout << *thePtr << endl;
	return 0;
}



I'll get a seg fault since I'm trying to access a null pointer (0x00000000). Point being, the pointer has to point to something before assignment.

This post has been edited by KYA: 23 August 2009 - 10:39 AM

Was This Post Helpful? 1
  • +
  • -

#3 DustyD  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 75
  • Joined: 19-August 09

Re: c++ game engine error

Posted 23 August 2009 - 10:53 AM

Cool thankyou, got rid of that but I have new errors.


This is how I changed the problem:

In OGEngine.cpp:

void OGEngine::OGInitDisplay()
{
   /* Create the window with engine's properties */
   engine->OG_Main_Frame = SDL_SetVideoMode( engine->OGeng_get_width_res(),
																					engine->OGeng_get_height_res(),
																					engine->OGeng_get_pixel_depth(),
																					engine->OGeng_set_flags(SDL_OPENGL,SDL_FULLSCREEN) );
}



This works, but now my usage of
(In struct OGEngine)
static OGEngine* engine;



is incorrect:

I get these errors now at linker stage:

/tmp/ccMaQFzP.o: In function `OGEngine::OGInitDisplay()':
OGEngine.cpp:(.text+0xc9): undefined reference to `OGEngine::engine'
OGEngine.cpp:(.text+0xce): undefined reference to `OGEngine::engine'
OGEngine.cpp:(.text+0xed): undefined reference to `OGEngine::engine'
OGEngine.cpp:(.text+0xfd): undefined reference to `OGEngine::engine'
OGEngine.cpp:(.text+0x10c): undefined reference to `OGEngine::engine'
collect2: ld returned 1 exit status


I understand that I havn't specified which OGEngine the engine pointer belongs to, but the OGEngine struct is defined in the test, I don't know how to tell the pointer that it points to an engine that will be created later...

Thanks KYA for you help..
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: c++ game engine error

Posted 23 August 2009 - 10:58 AM

If I'm reading that correctly (assuming no other code changed), you need to declare that pointer 'extern' in that cpp file (so it can access it):

extern static OGEngine* engine;



Alternatively, include the header that houses the struct that has this pointer.


On a side note, why is OGEngine.cpp not including its own header (OGEngine.h)?
Was This Post Helpful? 0
  • +
  • -

#5 DustyD  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 75
  • Joined: 19-August 09

Re: c++ game engine error

Posted 23 August 2009 - 11:11 AM

ok, I've never used extern before, now I see how it can be used..

Now it's throwing an error I've never seen before:

OGEngine.cpp:13: error: conflicting specifiers in declaration of ‘engine’


OGEngine.h is included in OG.h, so I included OG.h in OGEngine.cpp..
Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: c++ game engine error

Posted 23 August 2009 - 11:28 AM

Oh ok. I missed that. Probably because I would have had OGEngine include OG.h since OG includes the actual libraries, but it is ultimately irrelevant.

Try extern and see how that works out for you.

The error arises from the double inclusion of the header.
Was This Post Helpful? 0
  • +
  • -

#7 DustyD  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 75
  • Joined: 19-August 09

Re: c++ game engine error

Posted 23 August 2009 - 12:08 PM

Yea thats what it is.

Time to make a bloc diagram of what I want to achieve with my headers, because it's gonna get confusing.. to make sure I'm not including the same file multiple times.

Is this an other possibility? ->

Compile the OGEngine class to object code first, and the compile the test linked with the OGEngine class.

Would this avoid the muliple header error? because they were compiled seperately but linked at the same time?

This post has been edited by DustyD: 23 August 2009 - 12:19 PM

Was This Post Helpful? 0
  • +
  • -

#8 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: c++ game engine error

Posted 23 August 2009 - 12:11 PM

What I like to do:

Library header
Class headers
cpp files

So, in this case:

OG.h //top level, doesn't include anything
OGEngine.h //inclues OG.h
OGEngine.cpp //includes OGEngine.h

Same pattern for any other same named headers/cpps

Main.cpp generally includes the main header (OG.h) and any other headers it needs framework/reference for. :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1