Little problem in my small SDL game engine...

Hey guys so I have learned the basics of C++ and have decided to start making small 2d games as a hobby.I have decided to go with SDL2 as it is under zlib license.So I am trying to develop a sample game engine(2d) and have written a small game engine containing 2 source files,one header n other the main body.
Here's my hearder file named game.h:-

<#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED

using namespace std;

#include<SDL2/SDL.h>

class Game
{
private:
SDL_Window *w;
SDL_Renderer *r;
SDL_Texture *t;
SDL_Rect *sr;
SDL_Rect *dr;
bool isRun;
public:
Game()
{
this->w=NULL;
this->r=NULL;
this->isRun=false;
}
~Game()
{
this->clean();
delete this;
}
bool init(const char*,int,int,int,int,int);
void render();
void draw();
void update();
void handle_events();
void clean();
bool isRunning()
{return isRun;}
};
bool Game:: init(const char *name,int xpos,int ypos,int width,int height,int flags)
{
if(SDL_Init(SDL_INIT_EVERYTHING) >= 0)
{
w=SDL_CreateWindow(name,xpos,ypos,width,height,flags);
if(w!=0)
{
r=SDL_CreateRenderer(w,-1,0);
if(r!=0)
{
SDL_SetRenderDrawColor(r,255,255,255,255);
this->draw();
}
else
return false;

}
else
return false;
}
else
return false;

isRun=true;
return true;
}

void Game:: render()
{
SDL_RenderClear(r);
SDL_RenderCopy(r,t,sr,dr);
cout<<"Done copying!!!\n";
SDL_RenderPresent(r);
}

void Game:: clean()
{
SDL_DestroyWindow(w);
SDL_DestroyRenderer(r);
SDL_Quit();
}

void Game::handle_events()
{
SDL_Event event;
if(SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
isRun = false;
break;
default:
break;
}
}
}

void Game::draw()
{
SDL_Surface *temp=SDL_LoadBMP("Assets/Hello_SDL.bmp");
cout<<"Image loaded!!!\n";
t=SDL_CreateTextureFromSurface(r,temp);
cout<<"Texture created!!!\n";
SDL_FreeSurface(temp);
SDL_QueryTexture(t,NULL,NULL,&sr->w,&sr->h);
cout<<"Done quering!!!\n";
dr->x=sr->x=0;
dr->y=sr->y=0;
dr->w=sr->w;
dr->h=sr->h;

}


#endif // GAME_H_INCLUDED
>

And here's the main body:-

< #include "game.h"
#include<SDL2/SDL.h>

using namespace std;

bool Game:: init(const char *name,int xpos,int ypos,int width,int height,int flags)
{
if(SDL_Init(SDL_INIT_EVERYTHING) >= 0)
{
w=SDL_CreateWindow(name,xpos,ypos,width,height,flags);
if(w!=0)
{
r=SDL_CreateRenderer(w,-1,0);
if(r!=0)
{
SDL_SetRenderDrawColor(r,255,255,255,255);
}
else
return false;

}
else
return false;
}
else
return false;

isRun=true;
return true;
}
>

When i run the code,it gives the normal messages as it should(I have included messages to check if everything works or not) till the one in void render().That's when the messages stop and it prints "Segmentation Fault".
Can anyone explain to me where I am wrong?
And if you guys can then pls correct my code wherever Im wrong,pls.
Thank you!!!
SDL_QueryTexture(t,NULL,NULL,&sr->w,&sr->h);


Here, you dereference sr which points to some random place in memory. Don't do that. Immediatly after that you dereference dr which suffers from the same problem.

(I don't see what this has to do with Unix/Linux programming.)
Last edited on
Topic archived. No new replies allowed.