Segmetation fault noob doubt #4124
-
Hi there, im new using ray lib and i wanna simulate the rubik cube, im on an early stage on the project and some hazarous segmentation fault appear. This is the code (3 files and a CMAKEFILE.txt) //
// rubik.hpp
// Created by daniel on 30/06/24.
//
#ifndef RUBIK_H
#define RUBIK_H
#include <utility>
#include "raylib.h"
#include "raymath.h"
#include <array>
#include <vector>
typedef Vector3 Scale;
class Cube {
public:
Cube() = delete;
static Cube new_cube(const Scale &v_size, const Vector3 ¢er, const Color &faceColor);
[[nodiscard]] const Scale &size() const { return size_; }
[[nodiscard]] const Vector3 &position() const { return center_; }
[[nodiscard]] const Mesh &mesh() const { return mesh_; }
[[nodiscard]] const Model &model() const { return model_; }
[[nodiscard]] const Color &color() const { return face_color_; }
private:
Cube(const Scale &size, const Vector3 ¢er, const Color &faceColor, const Mesh &mesh, const Model &model) :
size_(size), center_(center), face_color_(faceColor), mesh_(mesh), orientacion_(MatrixIdentity()),
model_(model){};
Scale size_;
Vector3 center_;
Color face_color_;
Matrix orientacion_;
Mesh mesh_;
Model model_;
static Mesh genMesh(const Scale &scale);
static Model genModel(const Mesh &mesh, const Vector3 ¢er);
};
class Rubik {
public:
Rubik() = delete;
static Rubik GenerateCube(float size);
void draw() const;
std::vector<std::array<Cube, 7>> piezas_;
explicit Rubik(std::vector<std::array<Cube, 7>> piezas) : piezas_(std::move(piezas)){};
private:
};
Cube generateCube(float size);
#endif // RUBIK_H //
// Created by daniel on 30/06/24.
//
#include "rubik.hpp"
#include <array>
#include <vector>
#include "raymath.h"
auto Cube::new_cube(const Scale &v_size, const Vector3 ¢er, const Color &faceColor) -> Cube {
// fmt::print("Generating Rubik\n");
const auto mesh = genMesh(v_size);
// fmt::print("Generating Rubik\n");
const auto model = genModel(mesh, center);
return Cube(v_size, center, faceColor, mesh,model);
}
auto Cube::genMesh(const Scale &scale) -> Mesh {
return GenMeshCube(scale.x, scale.y, scale.z);
}
auto Cube::genModel(const Mesh &mesh, const Vector3 ¢er) -> Model {
auto model = LoadModelFromMesh(mesh);
model.transform = MatrixTranslate(center.x, center.y, center.z);
return model;
}
auto Rubik::GenerateCube(float size) -> Rubik {
auto colors = std::array<Color, 6>{WHITE, BLUE, ORANGE, RED, YELLOW, GREEN};
float offset = size - 0.7f;
const Scale size_x = {size * 0.9f, size * 0.9f, size * 0.1f};
const Scale size_y = {size * 0.9f, size * 0.1f, size * 0.9f};
const Scale size_z = {size * 0.1f, size * 0.9f, size * 0.9f};
std::vector<std::array<Cube, 7>> piezas;
piezas = {};
for (const auto x: {0, 1, 2})
for (const auto y: {0, 1, 2})
for (const auto z: {0, 1, 2}) {
auto face_colors = std::array<Color, 6>{
(z != 2) ? colors[0] : BLACK, (z != 0) ? colors[1] : BLACK, (x != 2) ? colors[2] : BLACK,
(x != 0) ? colors[3] : BLACK, (y != 2) ? colors[4] : BLACK, (y != 0) ? colors[5] : BLACK,
};
Vector3 center_position = {static_cast<float>(x - 1) * offset, static_cast<float>(y - 1) * offset,
static_cast<float>(z - 1) * offset};
auto center = Cube::new_cube({size, size, size}, center_position, BLACK);
// Front Face
Vector3 front_position = {center_position.x, center_position.y, center_position.z + size / 2};
auto front = Cube::new_cube(size_z, front_position, face_colors[0]);
// Back Face
Vector3 back_position = {center_position.x, center_position.y, center_position.z - size / 2};
auto back = Cube::new_cube(size_z, back_position, face_colors[1]);
// Right Face
Vector3 right_position = {center_position.x + size / 2, center_position.y, center_position.z};
auto right = Cube::new_cube(size_x, right_position, face_colors[2]);
// Left Face
Vector3 left_position = {center_position.x - size / 2, center_position.y, center_position.z};
auto left = Cube::new_cube(size_x, left_position, face_colors[3]);
// Top Face
Vector3 top_position = {center_position.x, center_position.y + size / 2, center_position.z};
auto top = Cube::new_cube(size_y, top_position, face_colors[4]);
// Bottom Face
Vector3 bot_position = {center_position.x, center_position.y - size / 2, center_position.z};
auto bot = Cube::new_cube(size_y, bot_position, face_colors[5]);
piezas.emplace_back < std::array<Cube, 7>>({ center, front, back, right, left, top, bot });
}
return Rubik(piezas);
}
auto Rubik::draw() const -> void {
for (const auto &pieza: piezas_) {
for (const auto &cara: pieza) {
DrawModel(cara.model(), cara.position(), 2.0f, cara.color());
}
}
}
Cube generateCube(float size) {
float x = 0,y = 0,z=0;
auto pieza = Cube::new_cube({size,size,size},{x,y,z},BLACK);
return pieza;
}
// main.cpp
#include <cstdint>
#include <memory>
// #include "fmt/core.h"
#include "raylib.h"
#include "rubik.hpp"
namespace ConfigData {
static constexpr int16_t w_width = 800;
static constexpr int16_t w_height = 600;
static constexpr int8_t fps = 60;
static const auto mainCamera = std::make_shared<Camera>(
Camera{Vector3{0.0f, 10.0f, 10.0f}, Vector3{0.0f, 0.0f, 0.0f}, Vector3{0.0f, 1.0f, 0.0f}, 45.0f, 0});
static constexpr std::string windowName = "RubikSimulator";
} // namespace ConfigData
namespace assets {
static std::shared_ptr<Rubik> cube;
static std::shared_ptr<Cube> cube1;
}
void gameLoop() {
while (!WindowShouldClose()) {
UpdateCamera(ConfigData::mainCamera.get(), CameraMode::CAMERA_ORBITAL);
BeginDrawing();
ClearBackground(RAYWHITE);
BeginMode3D(*ConfigData::mainCamera);
DrawGrid(20, 1.0);
// for (const auto &pieza: assets::cube1.piezas_) {
// for (const auto &cara: pieza) {
// DrawModel(cara.model(), cara.position(), 2.0f, cara.color());
// }
// }
DrawModel(assets::cube1->model(),assets::cube1->position(),2,assets::cube1->color());
// assets::cube1;
EndMode3D();
EndDrawing();
}
}
auto main() -> int {
assets::cube=std::make_shared<Rubik>(Rubik::GenerateCube(3));
assets::cube1=std::make_shared<Cube>(generateCube(2));
InitWindow(ConfigData::w_width, ConfigData::w_height, ConfigData::windowName.c_str());
SetTargetFPS(ConfigData::fps);
gameLoop();
CloseWindow();
return 0;
} and the raylib is instaled by cmake function FetchContent by this command: include(FetchContent)
set(FETCHCONTENT_QUIET FALSE)
set(BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) # don't build the supplied examples
set(BUILD_GAMES OFF CACHE BOOL "" FORCE) # don't build the supplied example games
FetchContent_Declare(
raylib
GIT_REPOSITORY "https://github.com/raysan5/raylib.git"
GIT_TAG "master"
GIT_PROGRESS TRUE
)
FetchContent_MakeAvailable(raylib) The function that blowsUp is the one that calls raylib`s function GenMeshCube, it seems like the segfault is generated when u create some buffer in opengl, but i dont think is a lib bug. Any help is welcomed. Thanks in advance and sorry for any miss-spell word |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
sry for the entry, everithing works now, i cant call any function of the opengl api before i init the window, lesson learned |
Beta Was this translation helpful? Give feedback.
sry for the entry, everithing works now, i cant call any function of the opengl api before i init the window, lesson learned