]> git.lizzy.rs Git - shadowclad.git/commitdiff
Add TGA image parsing; Start work on level image parsing
authoroutfrost <kotlet.bahn@gmail.com>
Sat, 13 Jan 2018 20:03:24 +0000 (21:03 +0100)
committeroutfrost <kotlet.bahn@gmail.com>
Sat, 13 Jan 2018 20:03:24 +0000 (21:03 +0100)
Makefile
level.c [new file with mode: 0644]
level.h [new file with mode: 0644]
tga.c [new file with mode: 0644]
tga.h [new file with mode: 0644]

index 652969cc2d649d776319a2c27d381a42d9f8214b..2e38020f24e1c14170cbeed3480b78e17fd97c45 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,7 @@
 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)
@@ -19,3 +20,9 @@ out/glut_janitor.o : glut_janitor.c
 
 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)
diff --git a/level.c b/level.c
new file mode 100644 (file)
index 0000000..0969646
--- /dev/null
+++ b/level.c
@@ -0,0 +1,18 @@
+#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;
+}
diff --git a/level.h b/level.h
new file mode 100644 (file)
index 0000000..22e4ad4
--- /dev/null
+++ b/level.h
@@ -0,0 +1,16 @@
+#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
diff --git a/tga.c b/tga.c
new file mode 100644 (file)
index 0000000..cbc8bba
--- /dev/null
+++ b/tga.c
@@ -0,0 +1,67 @@
+#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;
+}
diff --git a/tga.h b/tga.h
new file mode 100644 (file)
index 0000000..c79ec76
--- /dev/null
+++ b/tga.h
@@ -0,0 +1,32 @@
+#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