Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Displaying Arrays  (Read 1658 times)

0 Members and 1 Guest are viewing this topic.

Jalfor

  • Jr. Member
  • **
  • Posts: 62
    • View Profile
Displaying Arrays
« on: July 02, 2011, 09:13:45 am »
How would I go about displaying an array of shapes. I am trying to make a game of snake and I need to create a grid which can be individually color changeable for obvious reasons. Here is the code with all the irrelevant bits cut out.

 
Code: [Select]

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <iostream>

int main()
{
int count = 0;
        int count1 = 0;
sf::Shape grid[10][10];

int x1 = 120;
int y1 = 100;
int x2 = 100;
int y2 = 80;

    sf::RenderWindow App(sf::VideoMode(800, 600), "Snake");

    while (App.IsOpened())
{
       
App.Clear();

for(int xdot = 0; xdot < 10; xdot++)
for(int ydot = 0; ydot < 10; ydot++)
{
grid[xdot][ydot] = sf::Shape::Rectangle(x1 + xdot * 22, y1 + ydot * 22, x2 + xdot * 22, y2 + 22, sf::Color::Red);
}

       
App.Draw(grid);    //That's really what I want to do though it's giving me a syntax error
App.Display();
    }

    return EXIT_SUCCESS;
}
If a picture tells 1000 words, then a YouTube video clearly tells 1000000 making a YouTube video obviously a larger work than the Lord Of The Rings, or some other massive novel. Thus are the laws of YouTube videos.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Displaying Arrays
« Reply #1 on: July 02, 2011, 09:19:57 am »
You need to initialize the array only once, not every frame (i.e. before the main loop). Inside the loop, you can draw every shape:
Code: [Select]
for(int xdot = 0; xdot < 10; xdot++)
     for(int ydot = 0; ydot < 10; ydot++)
     {
        App.Draw(grid[xdot][ydot]);
     }
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Jalfor

  • Jr. Member
  • **
  • Posts: 62
    • View Profile
Displaying Arrays
« Reply #2 on: July 02, 2011, 09:41:10 am »
Thanks :)
If a picture tells 1000 words, then a YouTube video clearly tells 1000000 making a YouTube video obviously a larger work than the Lord Of The Rings, or some other massive novel. Thus are the laws of YouTube videos.