Segmentation Fault Error When Trying Something

So I thought maybe I would venture out a little from the lesson and try separating the code used to draw the map into its own header/source files just so I make sure I’m understanding how this is working. But when I created the files and called an instance in the main source file I’m getting an error on the header file constructor that just says “Segmentation Fault” with nothing else to help. I’m pretty sure I set the main function up correctly, here is the code:
#include “raylib.h”

#include “Level.h”

int main()

{

Level level;

level.createWindow();



while(!WindowShouldClose())

{

    level.drawLevel();

}

CloseWindow();

}

#include “raylib.h”

class Level

{

public:

    Level();

    void createWindow();

    void drawLevel();

private:

    int windowWidth{1920};

    int windowHeight{1080};

    Texture2D map{LoadTexture("maps/spellbook_base_level.png")};

    Vector2 mapPos{0.f, 0.f};

    float mapScale{6.f};

};

#include “Level.h”

Level::Level()

{

}

void Level::createWindow()

{

InitWindow(windowWidth, windowHeight, "V2");

}

void Level::drawLevel()

{

BeginDrawing();

ClearBackground(BLACK);

DrawTextureEx(map, mapPos, 0.f, mapScale, WHITE);

}

Everything looks ok but I don’t know why I’m getting that Segmentation Fault. Can someone help and maybe explain what I’m doing wrong?

Thanks!

Hello,
I am not getting an error when running your code, but I am stuck in an infinite “not responsive” state because the EndDrawing() command is missing (in this case at the end of the function Level::drawLevel()). Adding this creates a normal window for me.

Does that solve your error?

Hi There!

It’s highly recommended that you keep the BeginDrawing and EndDrawing function calls within the !WindowShouldClose() while loop.

The reason why you’re getting a Seg Fault is because you are trying to load a texture into memory before the GPU is ready to do so. I would remove your CreateWindow function from Level and just call InitWindow before declaring (and initializing) your Level.

Hi there,

This I think was the best solution, because removing the parameters for the texture allowed the code to execute without issue. But as soon as the DrawTexture was initialized it gave that error. Thanks for the info on this @Tuomo_T
So I’m assuming that since there is more going on behind-the-scenes for game engines/libraries, its probably not the best idea to create header files and source files for each section of what you would like your code to do? Only reason I’m asking is because we didn’t go over this error, but I’m sure there will be future instances of this where the debugger and compiler don’t help out too much with my error codes. For me, header files seemed pretty easy to understand and implement until I tried venturing out on my own.

In general, it’s ok to wrap code in your own classes so long as you know what order that code needs to be executed in.

No matter the library or engine, there will always be functions that have to be executed in a particular order to work, and compilers are not smart enough to tell you that.

With game development and programming in general, there will be lots of trial and error. So don’t feel discouraged if something doesn’t work the first time.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms