home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / MacWT 0.04 / wt / texture.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-31  |  3.4 KB  |  142 lines  |  [TEXT/MMCC]

  1. /*
  2. **  wt -- a 3d game engine
  3. **
  4. **  Copyright (C) 1994 by Chris Laurel
  5. **  email:  claurel@mr.net
  6. **  snail mail:  Chris Laurel, 5700 W Lake St #208,  St. Louis Park, MN  55416
  7. **
  8. **  This program is free software; you can redistribute it and/or modify
  9. **  it under the terms of the GNU General Public License as published by
  10. **  the Free Software Foundation; either version 2 of the License, or
  11. **  (at your option) any later version.
  12. **
  13. **  This program is distributed in the hope that it will be useful,
  14. **  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. **  GNU General Public License for more details.
  17. **
  18. **  You should have received a copy of the GNU General Public License
  19. **  along with this program; if not, write to the Free Software
  20. **  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22.  
  23. /* GIF87a code from Thomas Hamren (d0hampe@dtek.chalmers.se) */
  24.  
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include "wt.h"
  29. #include "wtmem.h"
  30. #include "error.h"
  31. #include "texture.h"
  32.  
  33.  
  34. static TexFormat check_texture_file(FILE *fp, char *filename);
  35. static int log2(int x);
  36.  
  37.  
  38. Texture *new_texture(int width, int height)
  39. {
  40.      Texture *t;
  41.  
  42.      if (height <= 0 || width <= 0)
  43.       fatal_error("new_texture:  bad texture dimensions");
  44.  
  45.      t = wtmalloc(sizeof(Texture));
  46.      t->texels = wtmalloc(height * width);
  47.  
  48.      t->width = width;
  49.      t->height = height;
  50.      t->log2width = log2(width);
  51.      
  52.      return t;
  53. }
  54.  
  55.  
  56. Texture *read_texture_file(char *filename)
  57. {
  58.      FILE *fp;
  59.      Texture *t;
  60.      int height, width;
  61.  
  62.  
  63.      if (!(fp = fopen(filename, "rb")))
  64.       fatal_error("Cannot open texture file %s.", filename);
  65.  
  66.      switch (check_texture_file(fp, filename)) {
  67.     case WTTEX:
  68.       break;
  69.  
  70.     case GIF87a:
  71.       return LoadGIF(fp, filename);
  72.       break;
  73.  
  74.     default:
  75.       fatal_error("%s is not a texture file (bad magic number).",
  76.               filename);
  77.       break;
  78.      }
  79.  
  80.      if (fscanf(fp, "%d %d\n", &width, &height) != 2)
  81.       fatal_error("Texture file %s has a bad header.", filename);
  82.  
  83.      /* The height and width should be powers of two for efficient
  84.      **   texture mapping.  Here, we enforce this.
  85.      */
  86.      if (log2(width) == -1 || log2(height) == -1)
  87.       fatal_error("Dimensions texture %s are not powers of two.", 
  88.               filename);
  89.      t = new_texture(width, height);
  90.  
  91.      if (fread(t->texels, 1, t->height * t->width, fp) !=
  92.      t->height * t->width)
  93.       fatal_error("Error reading texture file %s.", filename);
  94.  
  95.      fclose(fp);
  96.  
  97.      HostFixupPixelData(t->texels, t->height * t->width);
  98.  
  99.      return t;
  100. }
  101.  
  102.  
  103. /* in what format are the texturefile */
  104. static TexFormat check_texture_file( FILE *fp, char *filename )
  105. {
  106.      char magic[TEXTURE_MAGIC_LENGTH];
  107.  
  108.      if (fread(magic, 1, TEXTURE_MAGIC_LENGTH, fp) != TEXTURE_MAGIC_LENGTH)
  109.       fatal_error("Error reading texture file %s.", filename);
  110.  
  111.      if (strncmp(magic, TEXTURE_MAGIC, TEXTURE_MAGIC_LENGTH) == 0)
  112.       return WTTEX;
  113.  
  114.      if (strncmp(magic, GIF_MAGIC, TEXTURE_MAGIC_LENGTH) == 0)
  115.       return GIF87a;
  116.  
  117.      return Unknown;
  118. }
  119.  
  120.  
  121. /* Return the log base 2 of the argument, or -1 if the argument is not
  122. **   an integer power of 2.
  123. */
  124. static int log2(int x)
  125. {
  126.      int i;
  127.      unsigned int n;
  128.  
  129.      if (x <= 0)
  130.       return -1;
  131.      else
  132.       n = (unsigned int) x;
  133.  
  134.      
  135.      for (i = 0; (n & 0x1) == 0; i++, n >>= 1) ;
  136.  
  137.      if (n == 1)
  138.       return i;
  139.      else
  140.       return -1;
  141. }
  142.