Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Wolfmap streaming prototype
- //
- #include "stdafx.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <malloc.h>
- #include <Windows.h>
- typedef struct
- {
- int width;
- int height;
- int layers;
- int* data;
- int cacheSize;
- //The position where the cache starts
- int startX;
- int startY;
- }Map;
- Map* NewMap(int w, int h, int l)
- {
- Map* map = (Map*)malloc(sizeof(Map));
- map->width = w;
- map->height = h;
- map->layers = l;
- int i;
- map->data = (int*)calloc(w*h*l, sizeof(int));
- for (i = 0; i<w*h*l; i++)
- {
- map->data[i] = i;
- }
- return map;
- }
- void WriteMap(Map* map)
- {
- FILE *f = fopen("map.WolfMap", "wb");
- fwrite(&map->width, sizeof(int), 1, f);
- fwrite(&map->height, sizeof(int), 1, f);
- fwrite(&map->layers, sizeof(int), 1, f);
- fwrite(map->data, sizeof(int), map->width*map->height*map->layers, f);
- fclose(f);
- }
- Map* LoadMap(int cacheSize, int startAtX, int startAtY)
- {
- Map* map = (Map*)malloc(sizeof(Map));
- FILE *f = fopen("map.WolfMap", "rb");
- int w, h, l;
- fread(&w, sizeof(int), 1, f);
- fread(&h, sizeof(int), 1, f);
- fread(&l, sizeof(int), 1, f);
- map->layers = l;
- if (startAtX<0)
- {
- startAtX = 0;
- }
- if (startAtY<0)
- {
- startAtY = 0;
- }
- if (startAtX + cacheSize > w)
- {
- map->width = w-startAtX;
- }
- else
- {
- map->width = cacheSize;
- }
- if (startAtY + cacheSize > h)
- {
- map->height = h-startAtY;
- }
- else
- {
- map->height = cacheSize;
- }
- map->startX = startAtX;
- map->startY = startAtY;
- map->cacheSize = cacheSize;
- map->data = (int*)calloc(map->width*map->height*map->layers, sizeof(int));
- //Move to the correct position in the file
- fseek(f, startAtX*sizeof(int), SEEK_CUR);
- fseek(f, startAtY*w*sizeof(int), SEEK_CUR);
- int realX = 0, realY = 1;
- for (int i = 0; i < map->width*map->height*map->layers; i++)
- {
- fread(&map->data[i], sizeof(int), 1, f);
- realX++;
- if (realX >= map->width)
- {
- fseek(f, (w - (startAtX + realX))*sizeof(int), SEEK_CUR);
- realY++;
- realX = 0;
- }
- //Move to the next layer
- if (realY > map->height)
- {
- fseek(f, startAtX*sizeof(int), SEEK_CUR);
- fseek(f, startAtY*w*sizeof(int), SEEK_CUR);
- realY = 0;
- }
- }
- fclose(f);
- return map;
- }
- ///
- /// Returns a pointer to the integer from the map you ask for, if this part is not loaded we stream it
- ///
- int Get(int x, int y, int l, Map* map)
- {
- int pos = (x - map->startX)+((y - map->startY)*map->width);
- if (pos < 0)
- {
- map = LoadMap(map->cacheSize, x-map->cacheSize/2, y-map->cacheSize/2);
- pos = (x - map->startX) + ((y - map->startY)*map->width);
- }
- else if (pos>map->width*map->height)
- {
- map = LoadMap(map->cacheSize, x - map->cacheSize / 2, y - map->cacheSize / 2);
- pos = (x - map->startX) + ((y - map->startY)*map->width);
- }
- //Move to correct layer
- pos += (map->width*map->height)*l;
- int val = map->data[pos];
- return val;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- int w = 6;
- int h = 6;
- int l = 3;
- //Map* map = NewMap(w, h, l);
- //WriteMap(map);
- Map* map = LoadMap(100, 0, 1);
- printf("%d", Get(5, 0, 1, map));
- //printf("%d\n", map->data[5]);
- Sleep(2000);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment