Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

Field.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004 Ivo Danihelka (ivo@danihelka.net)
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  */
00009 #include "Field.h"
00010 
00011 #include "V2.h"
00012 #include "Cube.h"
00013 #include "Rules.h"
00014 #include "ModelFactory.h"
00015 
00016 #include <string.h> // memset()
00017 
00018 //-----------------------------------------------------------------
00019 /**
00020  * Two dimensional array of Cube pointers.
00021  */
00022 Field::Field(int w, int h)
00023 {
00024     m_w = w;
00025     m_h = h;
00026 
00027     m_border = ModelFactory::createBorder();
00028     m_border->rules()->takeField(this);
00029 
00030     //NOTE: [y][x] indexes
00031     m_marks = new Cube**[m_h];
00032     for (int y = 0; y < m_h; ++y) {
00033         m_marks[y] = new Cube*[m_w];
00034         memset(m_marks[y], 0, sizeof(Cube *) * m_w);
00035     }
00036 }
00037 //-----------------------------------------------------------------
00038 Field::~Field()
00039 {
00040     for (int y = 0; y < m_h; ++y) {
00041         delete [] m_marks[y];
00042     }
00043     delete [] m_marks;
00044     delete m_border;
00045 }
00046 //-----------------------------------------------------------------
00047 /**
00048  * Get model which occupied this location.
00049  * Empty locations are NULL filled.
00050  * Locations out of field are filled with border object.
00051  */
00052 Cube *
00053 Field::getModel(const V2 &loc)
00054 {
00055     int x = loc.getX();
00056     int y = loc.getY();
00057 
00058     //NOTE: hack border everywhere in outher space
00059     Cube *result = m_border;
00060     if ((0 <= x && x < m_w) && (0 <= y && y < m_h)) {
00061         result = m_marks[y][x];
00062     }
00063     return result;
00064 }
00065 //-----------------------------------------------------------------
00066 /**
00067  * Mark this location as occupied by model.
00068  * Locations out of field will not be filled.
00069  */
00070 void
00071 Field::setModel(const V2 &loc, Cube *model)
00072 {
00073     int x = loc.getX();
00074     int y = loc.getY();
00075 
00076     if ((0 <= x && x < m_w) && (0 <= y && y < m_h)) {
00077         m_marks[y][x] = model;
00078     }
00079 }
00080 
00081 

Generated on Wed Jun 1 09:54:30 2005 for Fish Fillets - Next Generation by  doxygen 1.4.2