Segmentation fault while trying to draw an array in raylib

43 Views Asked by At

I have a custom struct Object, with an initializing CreateObjects function to create an array with the given properties. The goal is to be able to draw multiple of these objects with the same texture onto different coordinates of the screen. I have done something similar before and the implementation seems nearly identical, but I keep getting a segmentation fault in the drawing loop of the DrawObjects function

object.c:

#include <malloc.h>
#include <stdio.h>
#include "raylib/include/raylib.h"
#include "object.h"

Object* CreateObjects(Texture2D texture, int numberOfObjects, int width, int height, double Xpos[], double Ypos[]){

        Object* objects = malloc(sizeof(Object) * numberOfObjects);

        for(int i=0; i < numberOfObjects; i++){
                objects[i].texture = texture;
                objects[i].rect.width = width;
                objects[i].rect.height = height;
                objects[i].rect.x = 0;
                objects[i].rect.y = 0;
                objects[i].position.x = Xpos[i];
                objects[i].position.y = Ypos[i];
        }
}

void DrawObjects(Object* objects, int numberOfObjects){

        for(int i=0; i < numberOfObjects; i++){
        DrawTextureRec(objects[i].texture, objects[i].rect, objects[i].position, WHITE);

        }
}

test.c, Initializing objects:

double objXPos[1] = {600-32};
        double objYPos[1] = {900-64};
        Texture2D barrelTexture = LoadTexture("sprites/barrel.png");
        Object* barrels = CreateObjects(barrelTexture, 1, 64, 64, objXPos, objYPos);

Drawing objects:

BeginDrawing();
                ClearBackground(BLACK);
                DrawTexture(background, 0, 0, RAYWHITE);
                DrawEntity(entity);
                DrawObjects(barrels, 1);

                if(bullet->isActive){
                        DrawBullet(bullet);
                        UpdateBullet(bullet);
                        CheckEntityCollision(bullet, entity);
                }

                EndDrawing();

(DrawBullet and DrawEntity are both working fine, I believe it's something within the for loop of DrawObjects that is causing the segmentation fault, but I have not been able to discover why)

0

There are 0 best solutions below