not 5 cell not rendering

50 views Asked by At

I have been trying to implement Conway's Game of Life in C. The CLI version of my code (quoted below) is working in the terminal but the 2-D animation rendering with Raylib is not working, why?

In the Raylib version when I register 5 blocks vertically it only registers the last repeating animation and does not render the full animation.

(The working code:)

#include <stdio.h>
#include <unistd.h>
int main()
{
    int mat[20][20];
    int buffer[20][20];
    int up, down, left, right;
    int life;
    int data;
    int x, y;
    int generation;
    int k, i, j;
    // default perm mat
    for (i = 0; i < 20; ++i)
    {
        for (j = 0; j < 20; ++j)
        {
            mat[i][j] = 0;
        }
    }
    // default temp mat
    for (i = 0; i < 20; i++)
    {
        for (j = 0; j < 20; j++)
        {
            buffer[i][j] = 0;
        }
    }
    printf("Enter number of live cell to add:");
    scanf("%d", &data);
    for (int i = 0; i < data; i++)
    {
        printf("enter position (format:row,column)");
        scanf("%d,%d", &x, &y);
        mat[x][y] = 1;
    }
    printf("Enter the loops for rendering eg( 15 for 15 generation rendering):");
    scanf("%d", &generation);
    for (k = 0; k < generation; k++)
    {
        sleep(1);
        system("@cls||clear");
        for (i = 0; i < 20; i++)
        {
            for (j = 0; j < 20; j++)
            {
                right = (j + 1) % 20;       //   19->0 20->1
                left = ((j - 1) + 20) % 20; // 0->19 1->20
                up = ((i - 1) + 20) % 20;   // 0->19 1->20
                down = (i + 1) % 20;        // 20-->1 19-->0
                // add 8 dir
                life = mat[down][j] + mat[i][left] + mat[up][left] + mat[up][j] + mat[up][right] + mat[i][right] + mat[down][right] + mat[down][left];
                if (mat[i][j] == 1 && (life < 2 || life > 3))
                {
                    buffer[i][j] = 0;
                }
                else if (mat[i][j] == 1 && (life == 2 || life == 3))
                {
                    buffer[i][j] = 1;
                }
                else if (mat[i][j] == 0 && life == 3)
                {
                    buffer[i][j] = 1;
                }
            }
        }
        for (i = 0; i < 20; i++)
        {
            for (j = 0; j < 20; j++)
            {
                mat[i][j] = buffer[i][j];
            }
        }
        for (i = 0; i < 20; i++)
        {
            for (j = 0; j < 20; j++)
            {
                if (mat[i][j] == 0)
                {
                    printf("\t");
                }
                else
                {
                    printf("#\t");
                }
            }
            printf("\n");
        }
    }
    return 0;
}

The Raylib implementation which does not work:

#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include "raylib.h"
#include "raymath.h"

#define COLS 20
#define ROWS 20

const int screenWidth = 600;
const int screenHeight = 600;

int cellWidth = screenWidth / COLS;
int cellHeight = screenHeight / ROWS;
int mat[20][20];
mat[20][20] =
    {
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

int temp[20][20];
int k, i, j;
int N, s, w, e;
int alive;
int x, y;

int main()
{
    SetTargetFPS(1);
    srand(time(0));
    InitWindow(screenWidth, screenHeight, "trial render");

    while (!WindowShouldClose())
    {
        BeginDrawing();
                       for (int i = 0; i < 20; i++)
        {
            for (int j = 0; j < 20; j++)
            {
                
                DrawRectangleLines(i * cellWidth, j * cellHeight,cellWidth,cellHeight, BLACK);
            }
        }


        for (i = 0; i < 20; i++)
        {
            for (j = 0; j < 20; j++)
            {
                temp[i][j] = 0;
            }
        }

        for (k = 0; k < 15; k++)
        {
            ClearBackground(WHITE);

            for (i = 0; i < 20; i++)
            {
                for (j = 0; j < 20; j++)
                {
                    int n;
                    e = (j + 1) % 20;
                    w = (j - 1 + 20) % 20;
                    n = (i - 1 + 20) % 20;
                    s = (i + 1) % 20;

                    alive = mat[s][j] + mat[i][w] + mat[n][w] + mat[n][j] + mat[n][e] + mat[i][e] + mat[s][e] + mat[s][w];

                    if (mat[i][j] == 1 && (alive < 2 || alive > 3))
                    {
                        temp[i][j] = 0;
                    }
                    else if (mat[i][j] == 1 && (alive == 2 || alive == 3))
                    {
                        temp[i][j] = 1;
                    }
                    else if (mat[i][j] == 0 && alive == 3)
                    {
                        temp[i][j] = 1;
                    }
                }
            }

            for (i = 0; i < 20; i++)
            {
                for (j = 0; j < 20; j++)
                {
                    mat[i][j] = temp[i][j];
                }
            }

     for (int i = 0; i < 20; i++)
        {
            for (int j = 0; j < 20; j++)
            {
                
                 if (mat[i][j] == 0)
                    {
                        DrawRectangle(i * cellWidth, j * cellHeight, cellWidth, cellHeight, WHITE);
                    }
                    else{
                         DrawRectangle(i * cellWidth, j * cellHeight, cellWidth, cellHeight, BLACK);
                    }
            }
                 
        }
           
        }
        EndDrawing();
    }

    CloseWindow();

    return 0;
}

0

There are 0 answers