Reputation: 2338
Sprite1 *test = new Sprite1(450, 450, "enemy.bmp", *screen);
test->DrawJon();
SDL_Delay(1000);
test->MoveJon(20,20);
I am getting a runtime error on line #2. It says access violation at 0x0
Sprite1 is a class I have defined and DrawJon() and MoneJon() in the class. This syntax is ok with the compiler but fails in runtime.
Sprite1.cpp
#include "Sprite1.h"
Sprite1::Sprite1(int posX, int posY, std::string imagePath, SDL_Surface screen) : PosX(posX), PosY(posY), ImagePath(imagePath), Screen(screen)
{
void DrawSprite1Jon( int x, int y, SDL_Surface *sprite, SDL_Surface *screen );
void DrawJon();
void MoveJon(int xDist, int yDist);
}
void Sprite1::DrawSprite1Jon( int x, int y, SDL_Surface *sprite, SDL_Surface *screen )
{
//Make a temporary rectangle to hold the offsets
SDL_Rect offset;
//Give the offsets to the rectangle
offset.x = x;
offset.y = y;
//Blit the surface
SDL_BlitSurface( sprite, NULL, screen, &offset );
SDL_UpdateRect(screen, 0, 0, 0, 0);
}
void Sprite1::DrawJon()
{
#pragma region Char to String Conversion
string ImagePath;
char * writable = new char[ImagePath.size() + 1];
copy(ImagePath.begin(), ImagePath.end(), writable);
writable[ImagePath.size()] = '\0';
#pragma endregion
temp = SDL_LoadBMP(writable);
sprite = SDL_DisplayFormat(temp);
SDL_FreeSurface(temp);
// free the string after using it
delete[] writable;
DrawSprite1Jon(PosX, PosY, sprite, screen);
}
Sprite1.h
#include <string>
#include <SDL.h>
#include "Functions.h"
using namespace std;
class Sprite1
{
private:
int PosX;
int PosY;
int xDist;
int yDist;
string ImagePath;
SDL_Surface Screen;
SDL_Surface *temp, *sprite, *screen;
public:
Sprite1(int PosX, int PosY, string ImagePath, SDL_Surface Screen );
void DrawSprite1Jon( int x, int y, SDL_Surface *sprite, SDL_Surface *screen);
void DrawJon();
void MoveJon(int xDist, int yDist);
};
upon further investigation, it is this line
DrawSprite1Jon(PosX, PosY, sprite, screen);
That is failing in the DrawJon()
Upvotes: 0
Views: 128
Reputation: 2338
There were some problems with my implementation but in the end I had to build in release mode rather than debug mode.
Upvotes: 0
Reputation: 62906
At least this piece of your code is broken:
string ImagePath;
char * writable = new char[ImagePath.size() + 1];
copy(ImagePath.begin(), ImagePath.end(), writable);
writable[ImagePath.size()] = '\0';
You are creating local ImagePath variable, not using the class member variable. The local variable shadows the member variable. Remove the local variable (first line in above snippet).
Also, you can probably (I'm not very familiar with SDL) do the loading simply like this:
temp = SDL_LoadBMP(ImagePath.c_str());
Then, just guessing, but image loading might be failing, and that function returns NULL pointer. So check return value and then check the error (either there is some SDL error function you can call, or you need to check standard errno global variable.
Further suggestion: turn on compiler warnings (for gcc: -W -Wall) and learn to understand (copying the warning to google is a good start) and then fix the warnings. Most of the time they are real bugs (hence the warning!) and even when they are not, fixing the warning will make your code better.
Upvotes: 1
Reputation: 12554
I is apparent that you've just began to program in C++, maybe you have a javascript background, therefore you've tried to put function declarations into the constructor. It is a wrong idea here. (#pragma
's usually have some explicit meaning, you also misuse them here. see eg. #pragma GCC poison
)
I see a lot of confusion in this code.
I suggest, you grab a great quality beginner C++ book, before going ahead with this piece of code. At this point I see no reason trying to hammer-out something reasonable from this code.
Upvotes: 0