Kukatz 3D  0.1
Török Attila szakdolgozata
projects/Kukatz 3D/src/grid.cpp
00001 /*
00002  * grid.cpp - Kukatz 3D
00003  * Copyright (c) 2011 - TÖRÖK Attila (torokati44@gmail.com)
00004  *
00005  * This software is provided 'as-is', without any express or implied
00006  * warranty. In no event will the authors be held liable for any damages
00007  * arising from the use of this software.
00008  *
00009  * Permission is granted to anyone to use this software for any purpose,
00010  * including commercial applications, and to alter it and redistribute it
00011  * freely, subject to the following restrictions:
00012  *
00013  *       1. The origin of this software must not be misrepresented; you must not
00014  *                      claim that you wrote the original software. If you use this software
00015  *                      in a product, an acknowledgment in the product documentation would be
00016  *                      appreciated but is not required.
00017  *
00018  *       2. Altered source versions must be plainly marked as such, and must not be
00019  *                      misrepresented as being the original software.
00020  *
00021  *       3. This notice may not be removed or altered from any source
00022  *                      distribution.
00023  */
00024 
00025 #include "grid.hpp"
00026 
00027 #include <cstring>
00028 
00029 Grid::Grid(unsigned int x_s, unsigned int y_s, unsigned int z_s):
00030         x_size(x_s), y_size(y_s), z_size(z_s)
00031 {
00032         cells = new GridCell[x_s * y_s * z_s];
00033         memset(cells, 0, x_s * y_s * z_s * sizeof(GridCell));
00034         memset(&invalid_cell, 0, sizeof(GridCell));
00035         invalid_cell.type = GridCell::INVALID;
00036 }
00037 
00038 GridCell& Grid::operator()(unsigned int x, unsigned int y, unsigned int z)
00039 {
00040         if ((x < x_size) && (y < y_size) && (z < z_size))
00041         {
00042                 return cells[x * y_size * z_size + y * z_size + z];
00043         }
00044         else
00045         {
00046                 memset(&invalid_cell, 0, sizeof(GridCell));
00047                 invalid_cell.type = GridCell::INVALID;
00048                 
00049                 return invalid_cell;
00050         }
00051 }
00052 
00053 GridCell& Grid::operator()(const sf::Vector3i& v)
00054 {
00055         if (
00056                         ((v.x < (int)x_size) && (v.y < (int)y_size) && (v.z < (int)z_size)) &&
00057                         ((v.x >= 0) && (v.y >= 0) && (v.z >= 0))
00058                 )
00059         {
00060                 return cells[v.x * y_size * z_size + v.y * z_size + v.z];
00061         }
00062         else
00063         {
00064                 memset(&invalid_cell, 0, sizeof(GridCell));
00065                 invalid_cell.type = GridCell::INVALID;
00066                 
00067                 return invalid_cell;
00068         }
00069 }
00070 
00071 Grid::~Grid()
00072 {
00073         delete[] cells;
00074 }
 Összes Osztályok