The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
Problem with my small game engine...
Christian Leger
Guest

I suggest you learn to use gdb or another debugger to find what the problem is. Seg faults are almost always trying to access an invalid pointer or a memory location that the program does not own. You can also use more logging (such as output to the console) and verify the values of your variables, to confirm they are what you think they should be. Good luck.




On Thu, Dec 24, 2015 at 9:15 AM, MaBunny wrote:
Quote:
Hey guys so i started making a game engine with sdl,and till now i have two files inmy project,one header file and one source file.The problem is that when i try to draw a .bmp picture to the screen it gives a "segmentation Fault".
Here's my header file named game.h:-



Code:

#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED

using namespace std;

#include

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 my main file:-




Code:

#include "game.h"
#include

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




So to see where im going wrong ive included couts in major functions.All goes well till the one in void render(),where the cout doesnt print to the screen.
Can you help guys?Actually I dont know quite well how to use SDL_Error() function.Can you tell me where and how to include it for diagnostic purposes?
Thnx!!


_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Problem with my small game engine...
brightening-eyes


Joined: 18 Jun 2015
Posts: 21
Location: tehran, iran
hi,
you seem to copy a null pointer (SDL_Render) which you get a segfault
cout doesn't print anything because your application is a gui
application, not a console one
please do not use any namespaces in your headers, refer them by using ::



۱۳۹۴-۱۰-۲۸ ۱:۱۶ +۰۳:۳۰ گرینویچ, Christian Leger:
Quote:
I suggest you learn to use gdb or another debugger to find what the problem
is. Seg faults are almost always trying to access an invalid pointer or a
memory location that the program does not own. You can also use more
logging (such as output to the console) and verify the values of your
variables, to confirm they are what you think they should be. Good luck.

On Thu, Dec 24, 2015 at 9:15 AM, MaBunny wrote:

Quote:
Hey guys so i started making a game engine with sdl,and till now i have
two files inmy project,one header file and one source file.The problem is
that when i try to draw a .bmp picture to the screen it gives a
"segmentation Fault".
Here's my header file named game.h:-



Code:

#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED

using namespace std;

#include

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 my main file:-




Code:

#include "game.h"
#include

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



So to see where im going wrong ive included couts in major functions.All
goes well till the one in void render(),where the cout doesnt print to
the
screen.
Can you help guys?Actually I dont know quite well how to use SDL_Error()
function.Can you tell me where and how to include it for diagnostic
purposes?
Thnx!! [image: Very Happy] [image: Very Happy] [image: Smile]

_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org



_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org