|
MASA-Core
|
00001 /******************************************************************************* 00002 * 00003 * Copyright (c) 2010-2015 Edans Sandes 00004 * 00005 * This file is part of MASA-Core. 00006 * 00007 * MASA-Core is free software: you can redistribute it and/or modify 00008 * it under the terms of the GNU General Public License as published by 00009 * the Free Software Foundation, either version 3 of the License, or 00010 * (at your option) any later version. 00011 * 00012 * MASA-Core is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with MASA-Core. If not, see <http://www.gnu.org/licenses/>. 00019 * 00020 ******************************************************************************/ 00021 00022 #include "BlocksFile.hpp" 00023 00024 #include <stdio.h> 00025 #include <stdlib.h> 00026 00027 BlocksFile::BlocksFile(string filename) { 00028 this->filename = filename; 00029 this->file = NULL; 00030 this->initialized = false; 00031 this->buffer = NULL; 00032 } 00033 00034 BlocksFile::~BlocksFile() 00035 { 00036 close(); 00037 if (buffer != NULL) { 00038 delete buffer; 00039 buffer = NULL; 00040 } 00041 initialized = false; 00042 } 00043 00044 void BlocksFile::initialize(const Grid* grid) { 00045 file = fopen(filename.c_str(), "wb"); 00046 if (file == NULL) { 00047 fprintf(stderr, "Could not create block file: %s\n", filename.c_str()); 00048 exit(1); 00049 } 00050 this->width = grid->getGridWidth(); 00051 this->height = grid->getGridHeight(); 00052 fwrite(&height, sizeof(int), 1, file); 00053 fwrite(&width, sizeof(int), 1, file); 00054 initialized = true; 00055 } 00056 00057 void BlocksFile::setScore(int bx, int by, int score) { 00058 //if (bl < cutBlockX || bl >= cutBlockY) 00059 // w = 0x80000000; // MIN_INT 00060 if (by >= 0 && by < height && bx >= 0 && bx < width) { 00061 int pos = by * width + bx; 00062 fseek(file, (pos + 2) * sizeof(int), SEEK_SET); 00063 fwrite(&score, sizeof(int), 1, file); 00064 } 00065 } 00066 00067 void BlocksFile::close() { 00068 if (file != NULL) { 00069 fclose(file); 00070 file = NULL; 00071 initialized = false; 00072 } 00073 } 00074 00075 bool BlocksFile::isInitialized() { 00076 return initialized; 00077 } 00078 00079 int* BlocksFile::reduceData(int &bh, int &bw) { 00080 FILE* file = fopen(filename.c_str(), "rb"); 00081 if (file == NULL) { 00082 fprintf(stderr, "Could not open block file: %s\n", filename.c_str()); 00083 exit(1); 00084 } 00085 00086 int h; 00087 int w; 00088 fread(&h, sizeof(int), 1, file); 00089 fread(&w, sizeof(int), 1, file); 00090 00091 if (bh > h) { 00092 bh = h; 00093 } 00094 if (bw > w) { 00095 bw = w; 00096 } 00097 00098 if (buffer != NULL) { 00099 delete buffer; 00100 } 00101 buffer = new int[bh*bw]; 00102 for (int bi=0; bi<bh; bi++) { 00103 for (int bj=0; bj<bw; bj++) { 00104 buffer[bi*bw + bj] = 0x80000000; 00105 } 00106 } 00107 00108 00109 for (int i=0; i<h; i++) { 00110 for (int j=0; j<w; j++) { 00111 int score; 00112 fread(&score, sizeof(int), 1, file); 00113 00114 int bi = i*bh/h; 00115 int bj = j*bw/w; 00116 int index = bi*bw + bj; 00117 00118 if (buffer[index] < score) { 00119 buffer[index] = score; 00120 } 00121 } 00122 } 00123 fclose(file); 00124 return buffer; 00125 }
1.7.6.1