We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Fill() Mystery (Read 253 times)
Fill() Mystery
Feb 26th, 2008, 4:52pm
 
I'm trying to load a text file as an array of Strings, then use those Strings to identify the indeividual tiles in MapTiles. I've tried changing the fill() according to the identity of the tile, but fill() won't accept variables! Please help






color grassColor = color(0, 200, 0);
int tileSize = 20;
Tile grass = new Tile(tileSize, tileSize);
Map map1 = new Map(tileSize, tileSize, 20, 5, "map1");

void setup(){
size(800, 500);

}

void draw(){
 background(255);
 grass.create(0, 200, 0, 0, 0);
 map1.update();
 map1.create();
 //fill(255);
 //PFont fontA = loadFont("CourierNew36.vlw");
 //textFont(fontA, 36);
 //text(map1.MapTiles[0].Green, width/2, height/2);
 
}

class Tile{
String name;
float h;
float w;
float Red;
float Blue;
float Green;

 Tile(float dh, float dw){
   h = dh;
   w = dw;
 }
void create(float Red, float Green, float Blue, float dx, float dy){
 fill(Red, Green, Blue);
 rect(dx, dy, w, h);
 }
void identify(String dname, int dRed, int dGreen, int dBlue){
 Red = dRed;
 Green = dGreen;
 Blue = dBlue;
 name = dname;
 }
float Red(){
return Red;
}
float Green(){
return Green;
}
float Blue(){
return Blue;
}
void displayType(float dx, float dy){
 PFont fontA = loadFont("CourierNew36.vlw");
 textFont(fontA, 36);
 text(name, dx, dy);
}
}

class Map{
String name;
float tileW;
float tileH;
int mapW;
int mapH;
float w = tileW * mapW;
float h = tileH * mapH;
Tile[] MapTiles = new Tile[0];
String[] MapLoad;
String[] MapSave = new String[MapTiles.length];
   Map(float dtileW, float dtileH, int dmapW, int dmapH, String dname){
      tileW = dtileW;
      tileH = dtileH;
      mapH = dmapH;
      mapW = dmapW;
      name = dname;
   }
   
   void update(){
     MapTiles = (Tile[])expand(MapTiles, mapW * mapH);
     instantiate();
 //    MapSave = (String[])expand(MapSave, mapW * mapH);
   //  for(int i = 0; i < MapTiles.length; i++){
     //        MapSave[i] = "grass";
       // }
        //saveStrings( name + ".txt", MapSave);
        MapLoad = loadStrings(name + ".txt");
      for(int i = 0; i < MapLoad.length; i++){
              MapTiles[i].identify("grass", 0, 200, 0);      
      }
   }
       void create(){
      for(int i = 0; i < MapTiles.length; i++){
      for(int j = 0; j < mapW; j++){
      for(int k = 0; k < mapH; k++){
        if(i < MapLoad.length){
            if(MapLoad[i].equals("grass")){
        MapTiles[i].create(grass.Red(), grass.Green(), grass.Blue(), j*tileSize, tileSize*k);
            }
        }  
     }
   }  
 }
}
       void instantiate(){
         for(int i = 0; i < MapTiles.length; i++){
         MapTiles[i] = new Tile(tileSize, tileSize);
         }
   }
   int returnMapTilesLength(){
     return MapTiles.length;
   }
   int returnMapLoadLength(){
     return MapLoad.length;
   }
   int returnmapH(){
     return mapH;
   }    
   int returnmapW(){
     return mapW;
   }
}
Re: Fill() Mystery
Reply #1 - Feb 26th, 2008, 6:14pm
 
when fill() is given floats as arguments it's expecting values in the range 0-1. but you are giving it 200, casting from an int. which might confuse it. that said, when i replace the fill(Red, Green, Blue) with fill(.0, .7, .0) i still get black squares.

i think your program has bigger problems though - you're calling map1.create() on every iteration of the loop when it sounds more likely to be a setup task.

i also get 'outOfMemory' exceptions when i hit close. (which could be coincidence).

can you describe what it should be doing? with a map1.txt file containing two lines of grass i get a black rectangle in upper 1/5 of the screen...
Re: Fill() Mystery
Reply #2 - Feb 27th, 2008, 10:59am
 
ok, i had a better look at this last night and, well, i was wrong about the float values in fill().

this bit confuses me:

void create(){
 for(int i = 0; i < MapTiles.length; i++){
 for(int j = 0; j < mapW; j++){
 for(int k = 0; k < mapH; k++){
...

you are looping through all the tiles (i loop) and *for every tile* you are looping through *all* the tiles by j and k. why?

also, calling instantiate() iterates through all the tiles instantiating them. then you iterate over all the tiles identify()ing them. and then you iterate over all of them in create() (see above) modifying their value again. why not iterate over them once and set their values during the call to new Tile()?

i've written my own version (i was bored!) with above changes and a few more. there are some guesses as to what you are trying to do but... if that's going to be helpful then it's here:

http://www.koogy.clara.co.uk/processing/Grass/
Page Index Toggle Pages: 1