Wolfos

Untitled

Mar 21st, 2014
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.15 KB | None | 0 0
  1. //
  2. //  Wolfmap streaming prototype
  3. //
  4.  
  5. #include "stdafx.h"
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <malloc.h>
  9. #include <Windows.h>
  10.  
  11. typedef struct
  12. {
  13.     int width;
  14.     int height;
  15.     int layers;
  16.     int* data;
  17.     int cacheSize;
  18.     //The position where the cache starts
  19.     int startX;
  20.     int startY;
  21. }Map;
  22.  
  23. Map* NewMap(int w, int h, int l)
  24. {
  25.     Map* map = (Map*)malloc(sizeof(Map));
  26.     map->width = w;
  27.     map->height = h;
  28.     map->layers = l;
  29.  
  30.     int i;
  31.  
  32.     map->data = (int*)calloc(w*h*l, sizeof(int));
  33.  
  34.     for (i = 0; i<w*h*l; i++)
  35.     {
  36.         map->data[i] = i;
  37.     }
  38.  
  39.     return map;
  40. }
  41.  
  42. void WriteMap(Map* map)
  43. {
  44.     FILE *f = fopen("map.WolfMap", "wb");
  45.    
  46.     fwrite(&map->width, sizeof(int), 1, f);
  47.     fwrite(&map->height, sizeof(int), 1, f);
  48.     fwrite(&map->layers, sizeof(int), 1, f);
  49.     fwrite(map->data, sizeof(int), map->width*map->height*map->layers, f);
  50.  
  51.     fclose(f);
  52. }
  53.  
  54. Map* LoadMap(int cacheSize, int startAtX, int startAtY)
  55. {
  56.     Map* map = (Map*)malloc(sizeof(Map));
  57.  
  58.     FILE *f = fopen("map.WolfMap", "rb");
  59.  
  60.     int w, h, l;
  61.  
  62.     fread(&w, sizeof(int), 1, f);
  63.     fread(&h, sizeof(int), 1, f);
  64.     fread(&l, sizeof(int), 1, f);
  65.  
  66.     map->layers = l;
  67.  
  68.     if (startAtX<0)
  69.     {
  70.         startAtX = 0;
  71.     }
  72.     if (startAtY<0)
  73.     {
  74.         startAtY = 0;
  75.     }
  76.  
  77.     if (startAtX + cacheSize > w)
  78.     {
  79.         map->width = w-startAtX;
  80.     }
  81.     else
  82.     {
  83.         map->width = cacheSize;
  84.     }
  85.     if (startAtY + cacheSize > h)
  86.     {
  87.         map->height = h-startAtY;
  88.     }
  89.     else
  90.     {
  91.         map->height = cacheSize;
  92.     }
  93.    
  94.     map->startX = startAtX;
  95.     map->startY = startAtY;
  96.     map->cacheSize = cacheSize;
  97.  
  98.     map->data = (int*)calloc(map->width*map->height*map->layers, sizeof(int));
  99.  
  100.     //Move to the correct position in the file
  101.     fseek(f, startAtX*sizeof(int), SEEK_CUR);
  102.     fseek(f, startAtY*w*sizeof(int), SEEK_CUR);
  103.  
  104.     int realX = 0, realY = 1;
  105.     for (int i = 0; i < map->width*map->height*map->layers; i++)
  106.     {
  107.         fread(&map->data[i], sizeof(int), 1, f);
  108.         realX++;
  109.         if (realX >= map->width)
  110.         {
  111.             fseek(f, (w - (startAtX + realX))*sizeof(int), SEEK_CUR);
  112.             realY++;
  113.             realX = 0;
  114.         }
  115.         //Move to the next layer
  116.         if (realY > map->height)
  117.         {
  118.             fseek(f, startAtX*sizeof(int), SEEK_CUR);
  119.             fseek(f, startAtY*w*sizeof(int), SEEK_CUR);
  120.             realY = 0;
  121.         }
  122.     }
  123.  
  124.     fclose(f);
  125.  
  126.     return map;
  127. }
  128.  
  129. ///
  130. /// Returns a pointer to the integer from the map you ask for, if this part is not loaded we stream it
  131. ///
  132. int Get(int x, int y, int l, Map* map)
  133. {
  134.     int pos = (x - map->startX)+((y - map->startY)*map->width);
  135.     if (pos < 0)
  136.     {
  137.         map = LoadMap(map->cacheSize, x-map->cacheSize/2, y-map->cacheSize/2);
  138.         pos = (x - map->startX) + ((y - map->startY)*map->width);
  139.     }
  140.     else if (pos>map->width*map->height)
  141.     {
  142.         map = LoadMap(map->cacheSize, x - map->cacheSize / 2, y - map->cacheSize / 2);
  143.         pos = (x - map->startX) + ((y - map->startY)*map->width);
  144.     }
  145.    
  146.     //Move to correct layer
  147.     pos += (map->width*map->height)*l;
  148.  
  149.     int val = map->data[pos];
  150.  
  151.     return val;
  152. }
  153.  
  154. int _tmain(int argc, _TCHAR* argv[])
  155. {
  156.     int w = 6;
  157.     int h = 6;
  158.     int l = 3;
  159.  
  160.     //Map* map = NewMap(w, h, l);
  161.  
  162.     //WriteMap(map);
  163.  
  164.     Map* map = LoadMap(100, 0, 1);
  165.  
  166.     printf("%d", Get(5, 0, 1, map));
  167.  
  168.     //printf("%d\n", map->data[5]);
  169.     Sleep(2000);
  170.     return 0;
  171. }
Advertisement
Add Comment
Please, Sign In to add comment