Jump to content

  • Log In with Google      Sign In   
  • Create Account

"Game" crashes when instantiating object


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 _Zac_   Members   -  Reputation: 138

Like
0Likes
Like

Posted 22 September 2011 - 04:53 PM

#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;
}


Sponsor:

#2 Ectara   Crossbones+   -  Reputation: 2967

Like
0Likes
Like

Posted 22 September 2011 - 05:57 PM

Do you ever allocate the destination strings before you copy to them? The C strings need to be allocated first, and have no constructor.

#3 Rodaxoleaux   Members   -  Reputation: 97

Like
0Likes
Like

Posted 22 September 2011 - 06:17 PM

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).

#4 Wooh   Members   -  Reputation: 623

Like
1Likes
Like

Posted 22 September 2011 - 06:26 PM

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).

#5 Samith   Members   -  Reputation: 2256

Like
0Likes
Like

Posted 22 September 2011 - 08:29 PM

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.

#6 Servant of the Lord   Crossbones+   -  Reputation: 19545

Like
1Likes
Like

Posted 22 September 2011 - 08:36 PM

Use std::strings, like others are recommending. You'll save yourself pain, suffering, confusion, and more pain. Posted Image
It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#7 _Zac_   Members   -  Reputation: 138

Like
0Likes
Like

Posted 22 September 2011 - 10:28 PM

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.

#8 _Zac_   Members   -  Reputation: 138

Like
0Likes
Like

Posted 22 September 2011 - 11:34 PM

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.



#9 Ectara   Crossbones+   -  Reputation: 2967

Like
0Likes
Like

Posted 23 September 2011 - 12:55 PM

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.

#10 _Zac_   Members   -  Reputation: 138

Like
0Likes
Like

Posted 23 September 2011 - 02:21 PM

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?



#11 Ectara   Crossbones+   -  Reputation: 2967

Like
0Likes
Like

Posted 23 September 2011 - 02:42 PM

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?



They look semantically the same, and references indicate such; std::string::assign() just offers more options and overloaded variants. Maybe an implementation detail? Try running it through a memory debugger.

And I was referring to your previous post. How is OnLoad() declared, and what is the returned string from File.c_str() when it breaks?

#12 _Zac_   Members   -  Reputation: 138

Like
0Likes
Like

Posted 23 September 2011 - 09:35 PM

Alright, just checked again, and it actually does not work. Must of grown delirious when programming late at night and assumed it was fixed.

Anyway, File.c_str() doesn't work, but just putting in "./image.png" does, even though File.c_str() should equal the same thing. Since SDL stops you from outputting to the console I'm going to work on writing the strings to files like Ectara said.

OnLoad() (which is a part of CSurface) is declared as a static function. You put in a char array (or c-string as I think they're called) and then it returns a pointer to a surface. What's odd is that it seems to still return a not null value when it fails to load the surface.

EDIT: Ok, it's working again. I just made a stupid error that gave me some weird results. I'm pretty strung out right now though, so I'll work on what I want for awhile before I post another question here. Thanks again everyone.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS