medfall

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 50425cab424edd07fb2d9707882947d771d86c0d
parent f10c81fd72aa8774e33d7113e62890d45c5f9f54
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Sat Aug  1 14:30:03 +0200

Add preprocessor. Currently only splits images into tiles

Diffstat:
.gitignore | 1+
Makefile | 21+++++++++++++++------
pp.cc | 60++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 76 insertions(+), 6 deletions(-)
diff --git a/.gitignore b/.gitignore @@ -1,5 +1,6 @@ bsp hm +pp *.o ttvfs *.bsp diff --git a/Makefile b/Makefile @@ -1,4 +1,4 @@ -all: bsp hm +all: bsp hm pp BSPSRCS = bsp.cc bsp_renderer.cc gl.cc BSPOBJS := $(patsubst %.cc,%.o,$(BSPSRCS)) @@ -6,6 +6,9 @@ BSPOBJS := $(patsubst %.cc,%.o,$(BSPSRCS)) HMSRCS = hm.cc gl.cc stb_image.cc stb_perlin.cc HMOBJS := $(patsubst %.cc,%.o,$(HMSRCS)) +PPSRCS = pp.cc stb_image.cc stb_image_write.cc +PPOBJS := $(patsubst %.cc,%.o,$(PPSRCS)) + WARNINGS = -Wall -Wextra -Wno-unused-parameter -Wno-unused-function -Wno-write-strings CXXFLAGS += -std=c++11 -O2 $(WARNINGS) -ggdb3 -DGL_GLEXT_PROTOTYPES LDFLAGS += -lm -lGL -lGLEW -lglfw @@ -15,16 +18,22 @@ picky: WARNINGS += -Wunused-parameter -Wunused-function -Wwrite-strings picky: all hm: $(HMOBJS) - $(CXX) $(HMOBJS) $(LDFLAGS) -o hm + $(CXX) $^ $(LDFLAGS) -o $@ bsp: $(BSPOBJS) - $(CXX) $(BSPOBJS) $(LDFLAGS) -o bsp + $(CXX) $^ $(LDFLAGS) -o $@ + +pp: $(PPOBJS) + $(CXX) $^ $(LDFLAGS) -o $@ clean: - rm -f bsp hm $(BSPOBJS) $(HMOBJS) + rm -f bsp hm $(BSPOBJS) $(HMOBJS) $(PPOBJS) stb_image.o: stb_image.cc - $(CXX) $(CXXFLAGS) -c -o stb_image.o stb_image.cc -DSTB_IMAGE_IMPLEMENTATION + $(CXX) $(CXXFLAGS) -c -o $@ $^ -DSTB_IMAGE_IMPLEMENTATION + +stb_image_write.o: stb_image_write.cc + $(CXX) $(CXXFLAGS) -c -o $@ $^ -DSTB_IMAGE_WRITE_IMPLEMENTATION stb_perlin.o: stb_perlin.cc - $(CXX) $(CXXFLAGS) -c -o stb_perlin.o stb_perlin.cc -DSTB_PERLIN_IMPLEMENTATION + $(CXX) $(CXXFLAGS) -c -o $@ $^ -DSTB_PERLIN_IMPLEMENTATION diff --git a/pp.cc b/pp.cc @@ -0,0 +1,60 @@ +#include <string> +#include <iostream> +#include <fstream> + +#include <stdio.h> +#include <sys/stat.h> + +#include "int.h" +#include "stb_image.h" +#include "stb_image_write.h" + +const int TILE_SIZE = 128; + +int w, h; +u8 * pixels; + +void write_tile( const std::string & dir, const int tx, const int ty ) { + u8 tile[ TILE_SIZE * TILE_SIZE ]; + + for( int y = 0; y < TILE_SIZE; y++ ) { + for( int x = 0; x < TILE_SIZE; x++ ) { + const int gy = ty * TILE_SIZE + y; + const int gx = tx * TILE_SIZE + x; + + if( gy >= h || gx >= w ) { + tile[ y * TILE_SIZE + x ] = 0; + } + else { + tile[ y * TILE_SIZE + x ] = pixels[ ( ty * TILE_SIZE + y ) * w + tx * TILE_SIZE + x ]; + } + } + } + + const std::string file = dir + "/" + std::to_string( tx ) + "_" + std::to_string( ty ) + ".tga"; + printf( "writing tile %s\n", file.c_str() ); + stbi_write_tga( file.c_str(), TILE_SIZE, TILE_SIZE, 1, tile ); +} + +int main( int argc, char ** argv ) { + const std::string path = argc == 2 ? argv[ 1 ] : "mountains512.png"; + pixels = stbi_load( path.c_str(), &w, &h, nullptr, 1 ); + + const std::string dir = path + ".parts"; + + mkdir( dir.c_str(), 0755 ); + + for( int ty = 0; ty < h / TILE_SIZE; ty++ ) { + for( int tx = 0; tx < w / TILE_SIZE; tx++ ) { + write_tile( dir, tx, ty ); + } + } + + std::ofstream dims( dir + "/dims.txt" ); + dims << std::to_string( w ) + " " + std::to_string( h ); + dims.close(); + + stbi_image_free( pixels ); + + return 0; +}