You're here: Snippet Directory » C/C++ (495)
Language:

MapGen 0.1 : Totally Random Module

Language: English
Programming Language: C
Published by: leckyt [not registered]
Last Update: 5/15/2006
Views: 132


Description

The first part of an eventual package for generating and manipulating height fields of variable width and height.This Snippet populates a random heightfield using standard ANSI C functionality.

Code

1 /* 2 * TotallyRandom : A Totally Random Heightfield Generator 3 * 4 * Copyright (C) 2001 Guy W. Lecky-Thompson 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 19 * 20 * Contact Info : Guy_W@Lecky-Thompson.net | www.Lecky-Thompson.net 21 * 22 */ 23 24 /* 25 Version Information 26 Future authors, please add update information here. 27 28 o --------------------------+-----------------------+------------------ o 29 | Author EMail |Date (dd-mm-yyyy) |Comments | 30 +----------------------------+-----------------------+--------------------+ 31 | Guy_W@Lecky-Thompson.net |9-10-2001 |Initial release 0.1 | 32 o --------------------------+-----------------------+------------------ o 33 */ 34 35 // Standard libraries 36 #include <stdio.h> // Input and output 37 #include <stdlib.h> // Memory and other standard ANSI functions 38 #include <time.h> // Date and time (for seeding random number generator) 39 40 // One cell in the heightfield 41 struct sCell 42 { 43 int nValue; 44 }; 45 46 // The grid for the heightfield 47 struct sGrid 48 { 49 int nWidth; // The width of the grid 50 int nHeight; // The height of the grid 51 int nMin; // Minimum cell value 52 int nMax; // Maximum cell value 53 struct sCell * oCellSpace; // The grid itself 54 }; 55 56 // License splash screen. 57 void ShowSplash() 58 { 59 printf("MapGen (c) 2001 Guy W. Lecky-Thompson\n\n"); 60 printf("Software made available under the GPL, see source code for details.\n\n"); 61 printf("Totally Random Module\n\n"); 62 } 63 64 // In case usage unclear 65 void ShowUsage() 66 { 67 printf(" Usage : MapGen <grid_width> <grid_height> [<min> <max>]\n\n"); 68 } 69 70 // Populate the grid with random values 71 void RandomPopulateGrid(struct sGrid * oGridObject) 72 { 73 int x; 74 int y; 75 76 // Seed the random number generator 77 srand(time(NULL)); 78 79 for (x = 0; x < oGridObject->nWidth; x++) 80 { 81 for (y = 0; y < oGridObject->nHeight; y++) 82 { 83 // Formula used to calculate offset into array : column + (width * row) 84 oGridObject->oCellSpace[x + (oGridObject->nWidth * y)].nValue = 85 (rand() % ((oGridObject->nMax + 1) - oGridObject->nMin)) + oGridObject->nMin; 86 } 87 } 88 89 } 90 91 // Show the grid on the screen 92 void DumpGridToScreen(struct sGrid * oGridObject) 93 { 94 int x; 95 int y; 96 97 printf("\n"); 98 99 for (x = 0; x < oGridObject->nWidth; x++) 100 { 101 for (y = 0; y < oGridObject->nHeight; y++) 102 { 103 // Formula used to calculate offset into array : column + (width * row) 104 printf("%1d",oGridObject->oCellSpace[x + (oGridObject->nWidth * y)].nValue); 105 } 106 printf("\n"); 107 } 108 } 109 110 // Put grid into a file for future use 111 void DumpGridToFile(struct sGrid * oGridObject, char * szFileName) 112 { 113 int x; 114 int y; 115 FILE * hFile; 116 117 printf("\nOpening file %s...",szFileName); 118 119 // Opening new file in text mode 120 hFile = fopen(szFileName,"wt"); 121 if (hFile == NULL) 122 { 123 printf(" ...Failed.\n"); 124 return; 125 } 126 else 127 { 128 printf(" ...Ok.\n"); 129 } 130 131 // Place the grid parameters in the file 132 fprintf(hFile,"%d,%d,%d,%d\n", 133 oGridObject->nWidth,oGridObject->nHeight,oGridObject->nMin,oGridObject->nMax); 134 135 // Write out the grid 136 for (x = 0; x < oGridObject->nWidth; x++) 137 { 138 for (y = 0; y < oGridObject->nHeight; y++) 139 { 140 // Formula used to calculate offset into array : column + (width * row) 141 fprintf(hFile,"%1d",oGridObject->oCellSpace[x + (oGridObject->nWidth * y)].nValue); 142 } 143 // End of line. Not strictly necessary, but adds readability. 144 fprintf(hFile,"\n"); 145 } 146 147 // Close the file 148 fclose(hFile); 149 150 } 151 152 // Harness for above functionality. Just included here for info. 153 // Future Snippets/Packages will replace this. 154 155 void main(int argc, char ** argv) 156 { 157 struct sGrid oGrid; 158 struct sCell oCell; // Used for calculating size of array to create 159 long lSpaceNeeded; // The space we need for our grid 160 char szFileName[255]; 161 162 ShowSplash(); 163 if (argc < 3) 164 { 165 ShowUsage(); 166 return; 167 } 168 169 // Set the width and height of the oGrid object 170 oGrid.nWidth = atoi(argv[1]); 171 oGrid.nHeight = atoi(argv[2]); 172 173 // Set the min and max heights of the oGrid object, if required 174 if (argc == 5) 175 { 176 oGrid.nMin = atoi(argv[3]); 177 oGrid.nMax = atoi(argv[4]); 178 } 179 else 180 { 181 oGrid.nMin = 0; 182 oGrid.nMax = 100; 183 } 184 185 // Do some *really* basic checking 186 if ((oGrid.nWidth <= 0) || (oGrid.nHeight <= 0)) 187 { 188 printf("Error : Width and Height must be greater than zero\n"); 189 return; 190 } 191 192 // Spit out some kind of status 193 printf("Building oGrid of %d by %d, height values between %d and %d\n", 194 oGrid.nWidth, oGrid.nHeight, oGrid.nMin, oGrid.nMax); 195 196 // Assign the memory needed 197 lSpaceNeeded = sizeof(oCell)*(oGrid.nWidth*oGrid.nHeight); 198 oGrid.oCellSpace = malloc(lSpaceNeeded); 199 200 // Check the memory 201 if (oGrid.oCellSpace != NULL) 202 { 203 printf(" Memory allocated Ok. [%ld]\n",lSpaceNeeded); 204 } 205 else 206 { 207 printf("Error: Failed to allocate memory. [%ld]\n",lSpaceNeeded); 208 return; 209 } 210 211 // Populate the grid with random data 212 printf("Populating grid...\n"); 213 RandomPopulateGrid(&oGrid); 214 215 // Grab the output filename 216 szFileName[0] = '\0'; 217 printf("\nEnter Filename >"); 218 scanf("%s",szFileName); 219 if (szFileName[0] == '\0') 220 { 221 DumpGridToScreen(&oGrid); 222 } 223 else 224 { 225 DumpGridToFile(&oGrid, szFileName); 226 } 227 228 // Free the memory 229 if (oGrid.oCellSpace != NULL) 230 { 231 printf("Freeing memory.\n"); 232 free(oGrid.oCellSpace); 233 } 234 235 }

No comments avaiable

Add a comment

Name *  

Email (won't be displayed) *    

Website  

Comment *  

Sicherheitscode Security Code *    

RSS