compileargs = -Wall -Wextra -Wpedantic
linkargs = -lGL -lglut
-objects = out/main.o out/debugutil.o out/glut_janitor.o out/render.o
+objects = out/main.o out/debugutil.o out/glut_janitor.o out/render.o \
+ out/tga.o out/level.o
shadowclad : $(objects)
gcc -o out/shadowclad $(objects) $(linkargs)
out/render.o : render.c render.h typedefs.h
gcc -c -o out/render.o render.c $(compileargs)
+
+out/tga.o : tga.c tga.h
+ gcc -c -o out/tga.o tga.c $(compileargs)
+
+out/level.o : level.c level.h tga.h
+ gcc -c -o out/level.o level.c $(compileargs)
--- /dev/null
+#include <GL/gl.h>
+#include <stdlib.h>
+
+#include "level.h"
+#include "tga.h"
+
+TGAimage* level_image = NULL;
+
+Block get_block(GLushort x, GLushort y) {
+ if (level_image == NULL) {
+ return BLOCK_EMPTY;
+ }
+ return ((Block*) (*level_image).bytes)[x * (*level_image).header.image_width + y];
+}
+
+void set_image(TGAimage* image) {
+ level_image = image;
+}
--- /dev/null
+#ifndef LEVEL_H
+#define LEVEL_H
+
+#include <GL/gl.h>
+
+#include "tga.h"
+
+typedef GLuint Block;
+
+const Block BLOCK_EMPTY = 0;
+const Block BLOCK_WALL01 = 1;
+
+Block get_block(GLushort x, GLushort y);
+void set_image(TGAimage* image);
+
+#endif
--- /dev/null
+#include <GL/gl.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "tga.h"
+
+TGAimage* read_tga(const char* path) {
+ FILE* tga_file = fopen(path, "rb");
+ if (tga_file == NULL) {
+ return NULL;
+ }
+
+ TGAheader header;
+
+ if (fread(&header, sizeof(TGAheader), 1, tga_file) != 1) {
+ fclose(tga_file);
+ return NULL;
+ }
+
+ GLenum image_format;
+ GLint image_components;
+
+ if (header.image_bpp == 32) {
+ image_format = GL_BGRA_EXT;
+ image_components = GL_RGBA8;
+ }
+ else if (header.image_bpp == 24) {
+ image_format = GL_BGR_EXT;
+ image_components = GL_RGB8;
+ }
+ else if (header.image_bpp == 8) {
+ image_format = GL_LUMINANCE;
+ image_components = GL_LUMINANCE8;
+ }
+ else {
+ fclose(tga_file);
+ return NULL;
+ }
+
+ unsigned long image_size = header.image_width * header.image_height * (header.image_bpp >> 3);
+
+ GLbyte* bytes = malloc(image_size * sizeof(GLbyte));
+ if (bytes == NULL) {
+ fclose(tga_file);
+ return NULL;
+ }
+
+ if (fread(bytes, image_size, 1, tga_file) != 1) {
+ free(bytes);
+ fclose(tga_file);
+ return NULL;
+ }
+
+ fclose(tga_file);
+
+ TGAimage* image = malloc(sizeof(TGAimage));
+ if (image == NULL) {
+ return NULL;
+ }
+
+ (*image).header = header;
+ (*image).image_format = image_format;
+ (*image).image_components = image_components;
+ (*image).bytes = bytes;
+
+ return image;
+}
--- /dev/null
+#ifndef TGA_H
+#define TGA_H
+
+#include <GL/gl.h>
+
+#pragma pack(push, 1)
+typedef struct {
+ GLubyte id_length;
+ GLbyte color_map_type;
+ GLbyte image_type;
+ GLushort color_map_start;
+ GLushort color_map_length;
+ GLubyte color_map_bpp;
+ GLushort x_origin;
+ GLushort y_origin;
+ GLushort image_width;
+ GLushort image_height;
+ GLubyte image_bpp;
+ GLbyte image_descriptor;
+} TGAheader;
+#pragma pack(pop)
+
+typedef struct {
+ TGAheader header;
+ GLenum image_format;
+ GLint image_components;
+ GLbyte* bytes;
+} TGAimage;
+
+TGAimage* read_tga(const char* path);
+
+#endif