Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include "main.h"
- //read a world file
- int readFile(FILE *file){
- int a;
- if((a = getc(file)) == EOF){
- return 0;
- }
- //convert ASCII codes
- switch(a){
- case 48:
- return 0;
- case 49:
- return 1;
- case 50:
- return 2;
- case 51:
- return 3;
- case 52:
- return 4;
- case 53:
- return 5;
- case 54:
- return 6;
- case 55:
- return 7;
- case 56:
- return 8;
- case 57:
- return 9;
- }
- return 0;
- }
- //initialize a world structure
- struct world readWorld(char *filename){
- struct world w;
- FILE *file;
- int a,b;
- file = fopen(filename, "r");
- w.rows = readFile(file);
- w.cols = readFile(file);
- printf("%d",w.rows);
- w.tile = malloc(w.rows*sizeof(int*));
- for(a=0;a<w.rows;a++)
- w.tile[a] = malloc(w.cols*sizeof(int*));
- for(a=0;a<w.rows;a++)
- for(b=0;b<w.cols;b++){
- w.tile[a][b] = readFile(file);
- }
- fclose(file);
- for(a=0;a<w.rows;a++)
- for(b=0;b<w.cols;b++)
- printf("%d",w.tile[a][b]);
- return w;
- }
- //free allocated world memory
- void freeWorld(struct world w){
- int a;
- for(a=0;a<w.rows;a++)
- free(w.tile[a]);
- free(w.tile);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement