"Game" crashes when instantiating object

Started by
10 comments, last by _Zac_ 12 years, 6 months ago

#ifndef _GAME_H_
#define _GAME_H_

#include "State.h"
#include "CEvent.h"
#include "GSurface.h"
#include <iostream>

class Game:public CEvent
{
private:
bool Running;
SDL_Surface* SurfDisplay;
State* CurrentState;
GSurface* temp;
SDL_Surface* temp2;

public:
Game();
~Game();
int onExecute();
bool onInit(); //inits display surface
void onEvent(SDL_Event* event); //Handles button presses
void onLoop(); //Game Loop
void onRender(); //Graphics
void onCleanup(); //Cleans up entire game
void onExit(); //Sets Running to false
void OnKeyDown(SDLKey sym, SDLMod mod, Uint16 unicode);
};

Game::Game()
{
SDL_Init(SDL_INIT_EVERYTHING);
Running=true;
SurfDisplay=0;
CurrentState=0;
temp=0;
//temp=new GSurface("image0","./image.png");
temp2=0;

}

bool Game::onInit()
{
//if(SDL_Init(SDL_INIT_EVERYTHING)<0){return false;}
SurfDisplay=SDL_SetVideoMode(640,480,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
if(SurfDisplay==NULL)
{
return false;
}
temp=new GSurface("image0","./image.png");
temp2=CSurface::OnLoad("./image.png");
return true;
}

void Game::onRender()
{
if(temp->drawSurface(SurfDisplay,32,32)==false)
{
Running=false;
}
//CSurface::OnDraw(SurfDisplay,temp2,0,0);

SDL_Flip(SurfDisplay);
}


The previous problem I was having has been fixed, but now I have a new problem, though it still concerns strings. The function OnLoad() takes in a const* char and returns a pointer to an SDL_Surface. The variable File is a std::string equal to "./image.png". I put in File.c_str() but it fails to load. When I put in "./image.png" instead it works. I wish to know what I am doing wrong and what I can do to solve this.

#ifndef _GSURFACE_H_
#define _GSURFACE_H_

#include "CSurface.h"
#include <iostream>

class GSurface
{
public:
std::string File;
private:
SDL_Surface* Surface;
std::string Name;
//std::string File;
bool Loaded;
Uint8 R,G,B,A;
int Width,Height;
int iWidth,iHeight;
int ImageMax;

public:
GSurface(std::string n,std::string location);
GSurface(std::string n,std::string location,int iw,int ih);
GSurface(std::string n,std::string location,int iw,int ih,Uint8 red,Uint8

green,Uint8 blue,Uint8 alpha);
//GSurface(char* n,char* location,Uint32 RGBA);
~GSurface();
bool drawSurface(SDL_Surface* surfDest,int x,int y,int ix,int iy,int w,int h);
bool drawSurface(SDL_Surface* surfDest,int x,int y,int image);
bool drawSurface(SDL_Surface* surfDest,int x,int y);
bool setAlpha(Uint8 a); //Sets alpha of surface
void setColorKey(Uint8 r,Uint8 g,Uint8 b,Uint8 a); //Sets color key and alpha
void setImage(int w,int h);
//bool setColorKey(Uint32 rgba);
std::string getName();
void setName(std::string n);
void free(); //calls unload()

private:
bool load();
void unload();
void checkLoaded();
};

GSurface::GSurface(std::string n,std::string location)
{
GSurface(n,location,NULL,NULL,NULL,NULL,NULL,NULL);
}
GSurface::GSurface(std::string n,std::string location,int iw,int ih)
{
GSurface(n,location,iw,ih,NULL,NULL,NULL,NULL);
}
GSurface::GSurface(std::string n,std::string location,int iw,int ih,Uint8

red,Uint8 green,Uint8 blue,Uint8 alpha)
{
Name=n;
File=location;
R=red;
G=green;
B=blue;
A=alpha;
iWidth=iw;
iHeight=ih;

Surface=NULL;
Loaded=false;
Width=Height=ImageMax=0;
}
GSurface::~GSurface()
{
unload();
Surface=NULL;
}
bool GSurface::load()
{
if(Loaded==true)
{
return true;
}
//Surface=CSurface::OnLoad(File.c_str()); NOTE: Does Not Work
Surface=CSurface::OnLoad("./image.png"); //But if replaced with this it does

work
if(Surface==NULL)
{
return false;
}
Loaded=true;
if(Width==0)
{
Width=Surface->w;
Height=Surface->h;
}
if(R!=NULL&&G!=NULL&&B!=NULL&&A!=NULL)
{
CSurface::OnColorKey(Surface,R,G,B);
}
return true;
}
void GSurface::unload()
{
SDL_FreeSurface(Surface);
Surface=NULL;
}
bool GSurface::drawSurface(SDL_Surface* surfDest,int x,int y)
{
checkLoaded();if(!Loaded){return false;}

return CSurface::OnDraw(surfDest,Surface,x,y);
}
bool GSurface::drawSurface(SDL_Surface* surfDest,int x,int y,int ix,int iy,int

w,int h)
{
checkLoaded();if(!Loaded){return false;}

return CSurface::OnDraw(surfDest,Surface,x,y,ix,iy,w,h);
}
bool GSurface::drawSurface(SDL_Surface* surfDest,int x,int y,int image)
{
checkLoaded();if(!Loaded){return false;}

int ix,iy,fw;

while(image>ImageMax)
{
image-=ImageMax;
}

fw=(int)(Width/iWidth);
ix=(image%fw)*iWidth;
iy=(image/fw)*iHeight;

return CSurface::OnDraw(surfDest,Surface,x,y,ix,iy,iWidth,iHeight);
}
void GSurface::setColorKey(Uint8 r,Uint8 g,Uint8 b,Uint8 a)
{
R=r;
G=g;
B=b;
A=a;
if(Loaded==true)
{
CSurface::OnColorKey(Surface,R,G,B);
}
}
void GSurface::setImage(int w,int h)
{
iWidth=w;
iHeight=h;
}
void GSurface::checkLoaded()
{
if(!Loaded&&load())
{
Loaded=true;
}
}
std::string GSurface::getName()
{
return Name;
}
void GSurface::setName(std::string n)
{
Name=n;
}
void GSurface::free()
{
unload();
}

#endif



SDL_Surface* CSurface::OnLoad(const char* File)
{
SDL_Surface* Surf_Temp=NULL;
SDL_Surface* Surf_Return=NULL;

if((Surf_Temp=IMG_Load(File))==NULL)
{
return NULL;
}
Surf_Return=SDL_DisplayFormat(Surf_Temp);
SDL_FreeSurface(Surf_Temp);
return Surf_Return;
}
Advertisement
Do you ever allocate the destination strings before you copy to them? The C strings need to be allocated first, and have no constructor.
You should also try debugging to point out exactly where in your GSurface constructor this fault is happening. If it's the first line, then obviously (points to above post).
I think Ectara has already found the problem. To make string handling a lot easier you can use std::string instead of char*.

I also noticed you never called SDL_Init. SDL_Init should be called before you call SDL_SetVideoMode (or any other SDL functions).
As Ectara said, you have to allocate your strings. The line where you say "strcpy(Name, n);" is causing the crash. Your variable "Name" is just a pointer, not an actual data buffer. strcpy is trying to put the contents pointed to by n into the location pointed to by Name, but since Name has never been allocated, it doesn't point to any valid region of memory (it probably points to memory address 0, but it could point to anything, since it's uninitialized). This is the source of your crash.
Use std::strings, like others are recommending. You'll save yourself pain, suffering, confusion, and more pain. wink.gif
Thank you so much guys. I really should've know that about strcpy. I'm switching to std::string's and just to have an easier time with this.
Alright, I'm having another problem.

File is a std::string equal to "./image.png"

I use a function that takes a filename as a const char* and loads an image.

OnLoad(File.c_str()) fails to load the file

OnLoad("./image.png") works.

How do I convert File into a const char* that would work with this? (No I cannot change the function to take in a std::string instead)

Tell me if you need more information, I'm updating the OP to include code I believe is relevant.


Alright, I'm having another problem.

File is a std::string equal to "./image.png"

I use a function that takes a filename as a const char* and loads an image.

OnLoad(File.c_str()) fails to load the file

OnLoad("./image.png") works.

How do I convert File into a const char* that would work with this? (No I cannot change the function to take in a std::string instead)

Tell me if you need more information, I'm updating the OP to include code I believe is relevant.




Have you confirmed that File.c_str() returns a valid string? Try outputting it to a console or a file.
I edited the OP stating that I fixed it by using File.assign() instead of File=. Though I guess if you want to clarify:

std::string File

function(std::string n){File=n;}

The function is then called likeso:

function("name");

Why would File=n not work yet File.assign(n) does?

This topic is closed to new replies.

Advertisement