SFML community forums

Help => General => Topic started by: Idiot22 on June 08, 2017, 06:32:56 am

Title: Help on how to make an array of rectangles for game of life program
Post by: Idiot22 on June 08, 2017, 06:32:56 am
So I have to code Conway's game of life using QT and SFML. I have a text version of the game and have to add graphics to it with SFML. The way my professor said we should do it is by drawing multiple rectangles (with an array) on the screen, like a grid, with each one being connected to the neighborhood array (the first rectangle is connected to (array)[0][0] and determines whether to print or not depending on if the cell is alive or dead). HOw can I do this?
Title: Re: Help on how to make an array of rectangles for game of life program
Post by: Laurent on June 08, 2017, 07:50:55 am
We're not going to do your homework ;) So what part of the project do you need help with? Have you already tried something?
Title: Re: Help on how to make an array of rectangles for game of life program
Post by: Idiot22 on June 08, 2017, 05:03:09 pm
This what I have. When I try to run it all I get is a blank window.

#include "game.h"
#include <gol_functions.h>
#include <iostream>
using namespace std;

Game::Game(){
    window.create(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "Game of Life");

    window.setFramerateLimit(60);

    int x=10;
    int y=10;

    initialize(world);

    for (int i=0; i<MAX; i++)
    {
        for (int j=0; j<COL; j++)
        {
             int n;
             n=count_neighbors(world, rowpos, colpos);
             if (n<2 || n>3){
                 box[i][j].setFillColor(sf::Color::Black);
             }
             if (n==3)
             {
             box[i][j].setFillColor(sf::Color::Red);
             }
             box[i][j].setSize(sf::Vector2f(20, 20));
             box[i][j].setPosition(sf::Vector2f(x+=25, y+=25));
        }
    }

}

void Game::Draw(){
    //Look at the data and based on the data, draw shapes on window object.
   // window.draw(box);
    for (unsigned int i = 0; i <MAX ; i++)
        {
        for (int j=0; j<COL; j++)
        {
            window.draw(box[i][j]);
        }
        }
}


void Game::update()
{
    play=true;
        do
        {
            next_gen(world, rowpos, colpos);
        }while(play);
}
void Game::render(){
       window.clear();
       Draw();
       window.display();
}



void Game::processEvents()
{
   sf::Event event;
   while (window.pollEvent(event))//or waitEvent
       {
       // check the type of the event...
           switch (event.type)
           {
               // window closed
               // "close requested" event: we close the window
               case sf::Event::Closed:
                   window.close();
                   break;

               // key pressed
               case sf::Event::KeyPressed:
                   switch(sf::Event::KeyPressed)
                   {
                        case 'p':
                        case 'P':
                           if (play)
                           {
                               play=false;
                           }
                           if (!play)
                           {
                               play=true;
                           }
                           break;
                        case 's':
                        case 'S':
                           save_neighborhood(world);
                           break;
                        case 'R':
                        case 'r':
                           initialize(world);
                           break;
                        default:
                           break;
                   }

                   break;

                   default:
                       break;
           }
       }
}
void Game::run()
{
   while (window.isOpen())
   {
       processEvents();
       update();
       render(); //clear/draw/display
   }
}
 
Title: Re: Help on how to make an array of rectangles for game of life program
Post by: Elias Daler on June 09, 2017, 01:43:23 am
My guess:

do
{
    next_gen(world, rowpos, colpos);
}while(play);

Are you sure this loop stops at some point? Because it seems to me like it doesn't. And you get stuck in update()
Title: Re: Help on how to make an array of rectangles for game of life program
Post by: Ruskointhehizzy on June 24, 2017, 08:23:43 am
if(n<2||n>3)
n is an int and then you intialize it - it like looks like you go through both for loops and set the table black, because it will always go into that if statement. Is this what you intend to do?