summaryrefslogtreecommitdiffstats
path: root/menu_demo/bitmap.c
blob: edb981d8044bab97239ddd071011e22908eb68b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#ifdef G_OS_WIN32
  #define WIN32_LEAN_AND_MEAN 1
  #include <windows.h>
  #define GL_BGR 0x80E0
#endif

#include <GL/gl.h>
#include <GL/glu.h>
#include <stdio.h>
#include <stdlib.h>

#include "bitmap.h"

#define BITMAP_FILESIZE 0x02
#define BITMAP_OFFSET 0x0a
#define BITMAP_HEADERSIZE 0x0e
#define BITMAP_WIDTH 0x12
#define BITMAP_HEIGHT 0x16



struct ImageStruct BitmapLoad(char *filename)
{
  struct ImageStruct l_sImage;
  GLuint texture;
  FILE *bitmap;

  // try to open the file
  bitmap = fopen(filename, "r");

  // does the bitmap exist?
  if (bitmap > 0)
  {
    unsigned int dataoffset, filesize;
    GLsizei width, height;
    unsigned char *imagedata;

    fseek(bitmap, BITMAP_FILESIZE, SEEK_SET);
    fread(&filesize, 4, 1, bitmap);
    fseek(bitmap, BITMAP_OFFSET, SEEK_SET);
    fread(&dataoffset, 4, 1, bitmap);
    fseek(bitmap, BITMAP_WIDTH, SEEK_SET);
    fread(&width, 4, 1, bitmap);
    fread(&height, 4, 1, bitmap);

    imagedata = (unsigned char *)malloc((size_t)(filesize -dataoffset));

    fseek(bitmap, dataoffset, SEEK_SET);
    fread(imagedata, (size_t)(filesize -dataoffset), 1, bitmap);

    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);
    gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, width, height, GL_BGR, GL_UNSIGNED_BYTE, imagedata);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    l_sImage.m_iImageId = texture;
    l_sImage.m_iWidth   = width;
    l_sImage.m_iHeight  = height;

    free(imagedata);
    fclose(bitmap);

    return l_sImage;
  }
  else
  {
    // couldn't find bitmap
    exit(0);
  }
}