initial commit
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sean Barrett · committed by Sean Barrett · parent df29024 · Browse files · 5 files·+1401 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+1401 added−3 removed
- tests:+118 added−3 removed
- other:+1283 added−0 removed
Provenance
- Commit
88c99e4- Tree
d8db6af- Author
Sean Barrett <sean2@nothings.org>- Committer
Sean Barrett <sean2@nothings.org>- Parent
df29024
5 files changed+1401 added−3 removed
added:tests/herringbone_generator.c+8 added
1+#define STB_HERRINGBONE_WANG_TILE_IMPLEMENTATION2+#include "stb_herringbone_wang_tile.h"3+4+int main(int argc, char **argv)5+{6+ return 0;7+}8+added:tests/herringbone.dsp+95 added
1+# Microsoft Developer Studio Project File - Name="herringbone" - Package Owner=<4>2+# Microsoft Developer Studio Generated Build File, Format Version 6.003+# ** DO NOT EDIT **4+5+# TARGTYPE "Win32 (x86) Console Application" 0x01036+7+CFG=herringbone - Win32 Debug8+!MESSAGE This is not a valid makefile. To build this project using NMAKE,9+!MESSAGE use the Export Makefile command and run10+!MESSAGE 11+!MESSAGE NMAKE /f "herringbone.mak".12+!MESSAGE 13+!MESSAGE You can specify a configuration when running NMAKE14+!MESSAGE by defining the macro CFG on the command line. For example:15+!MESSAGE 16+!MESSAGE NMAKE /f "herringbone.mak" CFG="herringbone - Win32 Debug"17+!MESSAGE 18+!MESSAGE Possible choices for configuration are:19+!MESSAGE 20+!MESSAGE "herringbone - Win32 Release" (based on "Win32 (x86) Console Application")21+!MESSAGE "herringbone - Win32 Debug" (based on "Win32 (x86) Console Application")22+!MESSAGE 23+24+# Begin Project25+# PROP AllowPerConfigDependencies 026+# PROP Scc_ProjName ""27+# PROP Scc_LocalPath ""28+CPP=cl.exe29+RSC=rc.exe30+31+!IF "$(CFG)" == "herringbone - Win32 Release"32+33+# PROP BASE Use_MFC 034+# PROP BASE Use_Debug_Libraries 035+# PROP BASE Output_Dir "Release"36+# PROP BASE Intermediate_Dir "Release"37+# PROP BASE Target_Dir ""38+# PROP Use_MFC 039+# PROP Use_Debug_Libraries 040+# PROP Output_Dir "Release"41+# PROP Intermediate_Dir "Release"42+# PROP Ignore_Export_Lib 043+# PROP Target_Dir ""44+# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c45+# ADD CPP /nologo /W3 /GX /O2 /I ".." /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c46+# ADD BASE RSC /l 0x409 /d "NDEBUG"47+# ADD RSC /l 0x409 /d "NDEBUG"48+BSC32=bscmake.exe49+# ADD BASE BSC32 /nologo50+# ADD BSC32 /nologo51+LINK32=link.exe52+# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I38653+# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I38654+55+!ELSEIF "$(CFG)" == "herringbone - Win32 Debug"56+57+# PROP BASE Use_MFC 058+# PROP BASE Use_Debug_Libraries 159+# PROP BASE Output_Dir "herringbone___Win32_Debug"60+# PROP BASE Intermediate_Dir "herringbone___Win32_Debug"61+# PROP BASE Target_Dir ""62+# PROP Use_MFC 063+# PROP Use_Debug_Libraries 164+# PROP Output_Dir "Debug"65+# PROP Intermediate_Dir "Debug"66+# PROP Ignore_Export_Lib 067+# PROP Target_Dir ""68+# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c69+# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I ".." /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /FD /GZ /c70+# SUBTRACT CPP /YX71+# ADD BASE RSC /l 0x409 /d "_DEBUG"72+# ADD RSC /l 0x409 /d "_DEBUG"73+BSC32=bscmake.exe74+# ADD BASE BSC32 /nologo75+# ADD BSC32 /nologo76+LINK32=link.exe77+# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept78+# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept79+80+!ENDIF 81+82+# Begin Target83+84+# Name "herringbone - Win32 Release"85+# Name "herringbone - Win32 Debug"86+# Begin Source File87+88+SOURCE=.\herringbone_generator.c89+# End Source File90+# Begin Source File91+92+SOURCE=..\stb_herringbone_wang_tile.h93+# End Source File94+# End Target95+# End Projectmodified:tests/stb.dsw+13 added-1 removed
1 unmodified line2 # WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!3 4 ###############################################################################5 6-Project: "c_lexer_test"=.\c_lexer_test\c_lexer_test.dsp - Package Owner=<4>6+Project: "c_lexer_test"=.\c_lexer_test.dsp - Package Owner=<4>7 8 Package=<5>9 {{{10 }}}3 unmodified lines14 }}}15 16 ###############################################################################17 18+Project: "herringbone"=.\herringbone.dsp - Package Owner=<4>19+20+Package=<5>21+{{{22+}}}23+24+Package=<4>25+{{{26+}}}27+28+###############################################################################29+30 Project: "image_test"=.\image_test.dsp - Package Owner=<4>31 32 Package=<5>33 {{{80 unmodified linesmodified:tests/test_cpp_compilation.cpp+2 added-2 removed
3 unmodified lines4 #define STB_DXT_IMPLEMENATION5 #define STB_C_LEXER_IMPLEMENTATIOn6 #define STB_DIVIDE_IMPLEMENTATION7 #define STB_IMAGE_IMPLEMENTATION8+#define STB_HERRINGBONE_WANG_TILE_IMPLEMENTATION9 10 #include "stb_truetype.h"11 #include "stb_image_write.h"12 #include "stb_perlin.h"13 #include "stb_dxt.h"14 #include "stb_c_lexer.h"15 #include "stb_divide.h"16 #include "stb_image.h"16-17-17+#include "stb_herringbone_wang_tile.h"added:stb_herringbone_wang_tile.h+1283 added
1+/* stbhw-v0.01 - public domain - http://nothings.org/stb/stb_herringbone_wang_tile.h2+ Herringbone Wang Tile Generator - Sean Barrett 20143+4+ This file is in the public domain. In case that declaration is ineffective,5+ you are also granted a license to use and modify it without restriction.6+7+== WHAT IT IS ===========================8+9+ This library is an SDK for Herringbone Wang Tile generation:10+11+ http://nothings.org/gamedev/herringbone12+13+ The core design is that you use this library offline to generate a14+ "template" of the tiles you'll create. You then edit those tiles, then15+ load the created tile image file back into this library and use it at16+ runtime to generate "maps".17+ 18+ You cannot load arbitrary tile image files with this library; it is19+ only designed to load image files made from the template it created.20+ It stores a binary description of the tile sizes & constraints in a21+ few pixels, and uses those to recover the rules, rather than trying22+ to parse the tiles themselves.23+ 24+ You *can* use this library to generate from arbitrary tile sets, but25+ only by loading the tile set and specifying the constraints explicitly26+ yourself.27+28+== COMPILING ============================29+30+ 1. #define STB_HERRINGBONE_WANG_TILE_IMPLEMENTATION before including this31+ header file in *one* source file to create the implementation32+ in that source file.33+34+ 2. optionally #define STB_HBWANG_RAND() to be a random number35+ generator. if you don't define it, it will use rand(),36+ and you need to seed srand() yourself.37+38+ 3. optionally #define STB_HBWANG_ASSERT(x), otherwise39+ it will use assert()40+41+ 4. optionally #define STB_HBWANG_STATIC to force all symbols to be42+ static instead of public, so they are only accesible43+ in the source file that creates the implementation44+45+ 5. optionally #define STB_HBWANG_NO_REPITITION_REDUCTION to disable46+ the code that tries to reduce having the same tile appear47+ adjacent to itself in wang-corner-tile mode (e.g. imagine48+ if you were doing something where 90% of things should be49+ the same grass tile, you need to disable this system)50+51+ 6. optionally define STB_HBWANG_MAX_X and STB_HBWANG_MAX_Y52+ to be the max dimensions of the generated map in multiples53+ of the wang tile's short side's length (e.g. if you54+ have 20x10 wang tiles, so short_side_len=10, and you55+ have MAX_X is 17, then the largest map you can generate56+ is 170 pixels wide). The defaults are 100x100. This57+ is used to define static arrays which affect memory58+ usage.59+60+== USING ================================61+62+ To use the map generator, you need a tileset. You can download63+ some sample tilesets from http://nothings.org/gamedev/herringbone64+65+ Then see the "sample application" below.66+67+ You can also use this file to generate templates for68+ tilesets which you then hand-edit to create the data.69+70+71+== MEMORY MANAGEMENT ====================72+73+ The tileset loader allocates memory with malloc(). The map74+ generator does no memory allocation, so e.g. you can load75+ tilesets at startup and never free them and never do any76+ further allocation.77+78+79+== SAMPLE APPLICATION ===================80+81+#include <stdlib.h>82+#include <stdio.h>83+#include <time.h>84+85+#define STB_IMAGE_IMPLEMENTATION86+#include "stb_image.h" // http://nothings.org/stb_image.c87+88+#define STB_IMAGE_WRITE_IMPLEMENTATION89+#include "stb_image_write.h" // http://nothings.org/stb/stb_image_write.h90+91+#define STB_HBWANG_IMPLEMENTATION92+#include "stb_hbwang.h"93+94+int main(int argc, char **argv)95+{96+ unsigned char *data;97+ int xs,ys, w,h;98+ stbhw_tileset ts;99+100+ if (argc != 4) {101+ fprintf(stderr, "Usage: mapgen {tile-file} {xsize} {ysize}\n"102+ "generates file named 'test_map.png'\n");103+ exit(1);104+ }105+ data = stbi_load(argv[1], &w, &h, NULL, 3);106+ xs = atoi(argv[2]);107+ ys = atoi(argv[3]);108+ if (data == NULL) {109+ fprintf(stderr, "Error opening or parsing '%s' as an image file\n", argv[1]);110+ exit(1);111+ }112+ if (xs < 1 || xs > 1000) {113+ fprintf(stderr, "xsize invalid or out of range\n");114+ exit(1);115+ }116+ if (ys < 1 || ys > 1000) {117+ fprintf(stderr, "ysize invalid or out of range\n");118+ exit(1);119+ }120+121+ stbhw_build_tileset_from_image(&ts, data, w*3, w, h);122+ free(data);123+124+ // allocate a buffer to create the final image to125+ data = malloc(3 * xs * ys);126+127+ srand(time(NULL));128+ stbhw_generate_image(&ts, NULL, data, xs*3, xs, ys);129+130+ stbi_write_png("test_map.png", xs, ys, 3, data, xs*3);131+132+ stbhw_free_tileset(&ts);133+ free(data);134+135+ return 0;136+}137+138+*/139+140+//////////////////////////////////////////////////////////////////////////////141+// //142+// HEADER FILE SECTION //143+// //144+145+#ifndef INCLUDE_STB_HWANG_H146+#define INCLUDE_STB_HWANG_H147+148+#ifdef STB_HBWANG_STATIC149+#define STBHW_EXTERN static150+#else151+#ifdef __cplusplus152+#define STBHW_EXTERN extern "C"153+#else154+#define STBHW_EXTERN extern155+#endif156+#endif157+158+typedef struct stbhw_tileset stbhw_tileset;159+160+// returns description of last error produced by any function (not thread-safe)161+STBHW_EXTERN char *stbhw_get_last_error(void);162+163+// build a tileset from an image that conforms to a template created by this164+// library. (you allocate storage for stbhw_tileset and function fills it out;165+// memory for individual tiles are malloc()ed).166+// returns non-zero on success, 0 on error167+STBHW_EXTERN int stbhw_build_tileset_from_image(stbhw_tileset *ts,168+ unsigned char *pixels, int stride_in_bytes, int w, int h);169+170+// free a tileset built by stbhw_build_tileset_from_image171+STBHW_EXTERN void stbhw_free_tileset(stbhw_tileset *ts);172+173+// generate a map that is w * h pixels (3-bytes each)174+// returns non-zero on success, 0 on error175+// not thread-safe (uses a global data structure to avoid memory management)176+// weighting can be NULL, or it 177+STBHW_EXTERN int stbhw_generate_image(stbhw_tileset *ts, int **weighting,178+ unsigned char *pixels, int stride_in_bytes, int w, int h);179+180+//////////////////////////////////////181+//182+// TILESET DATA STRUCTURE183+//184+// if you use the image-to-tileset system from this file, you185+// don't need to worry about these data structures. but if you186+// want to build/load a tileset yourself, you'll need to fill187+// these out.188+189+typedef struct190+{191+ // the edge or vertex constraints, according to diagram below192+ signed char a,b,c,d,e,f;193+194+ // The herringbone wang tile data; it is a bitmap which is either195+ // w=2*short_sidelen,h=short_sidelen, or w=short_sidelen,h=2*short_sidelen.196+ // it is always RGB, stored row-major, with no padding between rows.197+ // (allocate stbhw_tile structure to be large enough for the pixel data)198+ unsigned char pixels[1];199+} stbhw_tile;200+201+struct stbhw_tileset202+{203+ int is_corner;204+ int num_color[6]; // number of colors for each of 6 edge types or 4 corner types205+ int short_side_len;206+ stbhw_tile **h_tiles;207+ stbhw_tile **v_tiles;208+ int num_h_tiles, max_h_tiles;209+ int num_v_tiles, max_v_tiles;210+};211+212+/////////////// TEMPLATE GENERATOR //////////////////////////213+214+// when requesting a template, you fill out this data215+typedef struct216+{217+ int is_corner; // using corner colors or edge colors?218+ int short_side_len; // rectangles is 2n x n, n = short_side_len219+ int num_color[6]; // see below diagram for meaning of the index to this;220+ // 6 values if edge (!is_corner), 4 values if is_corner221+ // legal numbers: 1..8 if edge, 1..4 if is_corner222+ int num_vary_x; // additional number of variations along x axis in the template223+ int num_vary_y; // additional number of variations along y axis in the template224+ int corner_type_color_template[4][4];225+ // if corner_type_color_template[s][t] is non-zero, then any226+ // corner of type s generated as color t will get a little227+ // corner sample markup in the template image data228+229+} stbhw_config;230+231+// computes the size needed for the template image232+STBHW_EXTERN void stbhw_get_template_size(stbhw_config *c, int *w, int *h);233+234+// generates a template image, assuming data is 3*w*h bytes long, RGB format235+STBHW_EXTERN void stbhw_make_template(stbhw_config *c, unsigned char *data, int w, int h);236+237+#endif//INCLUDE_STB_HWANG_H238+239+240+// TILE CONSTRAINT TYPES241+//242+// there are 4 "types" of corners and 6 types of edges.243+// you can configure the tileset to have different numbers244+// of colors for each type of color or edge.245+// 246+// corner types:247+//248+// 0---*---1---*---2---*---3249+// | | |250+// * * *251+// | | |252+// 1---*---2---*---3 0---*---1---*---2253+// | | |254+// * * *255+// | | |256+// 0---*---1---*---2---*---3257+//258+//259+// edge types:260+//261+// *---2---*---3---* *---0---*262+// | | | |263+// 1 4 5 1264+// | | | |265+// *---0---*---2---* * *266+// | |267+// 4 5268+// | |269+// *---3---*270+//271+// TILE CONSTRAINTS272+//273+// each corner/edge has a color; this shows the name274+// of the variable containing the color275+//276+// corner constraints:277+//278+// a---*---d279+// | |280+// * *281+// | |282+// a---*---b---*---c b e283+// | | | |284+// * * * *285+// | | | |286+// d---*---e---*---f c---*---f287+//288+//289+// edge constraints:290+//291+// *---a---*---b---* *---a---*292+// | | | |293+// c d b c294+// | | | |295+// *---e---*---f---* * *296+// | |297+// d e298+// | |299+// *---f---*300+//301+302+303+//////////////////////////////////////////////////////////////////////////////304+// //305+// IMPLEMENTATION SECTION //306+// //307+308+#ifdef STB_HERRINGBONE_WANG_TILE_IMPLEMENTATION309+310+311+#include <string.h> // memcpy312+#include <stdlib.h> // malloc313+314+#ifndef STB_HBWANG_RAND315+#include <stdlib.h>316+#define STB_HBWANG_RAND() (rand() >> 4)317+#endif318+319+#ifndef STB_HBWANG_ASSERT320+#include <assert.h>321+#define STB_HBWANG_ASSERT(x) assert(x)322+#endif323+324+// map size325+#ifndef STB_HBWANG_MAX_X326+#define STB_HBWANG_MAX_X 100327+#endif328+329+#ifndef STB_HBWANG_MAX_Y330+#define STB_HBWANG_MAX_Y 100331+#endif332+333+// global variables for color assignments334+// @MEMORY change these to just store last two/three rows335+// and keep them on the stack336+static signed char c_color[STB_HBWANG_MAX_Y+6][STB_HBWANG_MAX_X+6];337+static signed char v_color[STB_HBWANG_MAX_Y+6][STB_HBWANG_MAX_X+5];338+static signed char h_color[STB_HBWANG_MAX_Y+5][STB_HBWANG_MAX_X+6];339+340+static char *stbhw_error;341+STBHW_EXTERN char *stbhw_get_last_error(void)342+{343+ return stbhw_error;344+}345+346+347+348+349+/////////////////////////////////////////////////////////////350+//351+// SHARED TEMPLATE-DESCRIPTION CODE352+//353+// Used by both template generator and tileset parser; by354+// using the same code, they are locked in sync and we don't355+// need to try to do more sophisticated parsing of edge color356+// markup or something.357+358+typedef void stbhw__process_rect(struct stbhw__process *p, int xpos, int ypos,359+ int a, int b, int c, int d, int e, int f);360+361+typedef struct stbhw__process362+{363+ stbhw_tileset *ts;364+ stbhw_config *c;365+ stbhw__process_rect *process_h_rect;366+ stbhw__process_rect *process_v_rect;367+ unsigned char *data;368+ int stride,w,h;369+} stbhw__process;370+371+static void stbhw__process_h_row(stbhw__process *p,372+ int xpos, int ypos,373+ int a0, int a1,374+ int b0, int b1,375+ int c0, int c1,376+ int d0, int d1,377+ int e0, int e1,378+ int f0, int f1,379+ int variants)380+{381+ int a,b,c,d,e,f,v;382+383+ for (v=0; v < variants; ++v)384+ for (f=f0; f <= f1; ++f)385+ for (e=e0; e <= e1; ++e)386+ for (d=d0; d <= d1; ++d)387+ for (c=c0; c <= c1; ++c)388+ for (b=b0; b <= b1; ++b)389+ for (a=a0; a <= a1; ++a) {390+ p->process_h_rect(p, xpos, ypos, a,b,c,d,e,f);391+ xpos += 2*p->c->short_side_len + 3;392+ }393+}394+395+static void stbhw__process_v_row(stbhw__process *p,396+ int xpos, int ypos,397+ int a0, int a1,398+ int b0, int b1,399+ int c0, int c1,400+ int d0, int d1,401+ int e0, int e1,402+ int f0, int f1,403+ int variants)404+{405+ int a,b,c,d,e,f,v;406+407+ for (v=0; v < variants; ++v)408+ for (f=f0; f <= f1; ++f)409+ for (e=e0; e <= e1; ++e)410+ for (d=d0; d <= d1; ++d)411+ for (c=c0; c <= c1; ++c)412+ for (b=b0; b <= b1; ++b)413+ for (a=a0; a <= a1; ++a) {414+ p->process_v_rect(p, xpos, ypos, a,b,c,d,e,f);415+ xpos += p->c->short_side_len+3;416+ }417+}418+419+static void stbhw__get_template_info(stbhw_config *c, int *w, int *h, int *h_count, int *v_count)420+{421+ int size_x,size_y;422+ int horz_count,vert_count;423+424+ if (c->is_corner) {425+ int horz_w = c->num_color[1] * c->num_color[2] * c->num_color[3] * c->num_vary_x;426+ int horz_h = c->num_color[0] * c->num_color[1] * c->num_color[2] * c->num_vary_y;427+428+ int vert_w = c->num_color[0] * c->num_color[3] * c->num_color[2] * c->num_vary_y;429+ int vert_h = c->num_color[1] * c->num_color[0] * c->num_color[3] * c->num_vary_x;430+431+ int horz_x = horz_w * (2*c->short_side_len + 3);432+ int horz_y = horz_h * ( c->short_side_len + 3);433+434+ int vert_x = vert_w * ( c->short_side_len + 3);435+ int vert_y = vert_h * (2*c->short_side_len + 3);436+437+ horz_count = horz_w * horz_h;438+ vert_count = vert_w * vert_h;439+440+ size_x = horz_x > vert_x ? horz_x : vert_x;441+ size_y = 2 + horz_y + 2 + vert_y;442+ } else {443+ // @TODO: non-corner-tile444+ int horz_w = c->num_color[0] * c->num_color[1] * c->num_color[2] * c->num_vary_x;445+ int horz_h = c->num_color[3] * c->num_color[4] * c->num_color[2] * c->num_vary_y;446+447+ int vert_w = c->num_color[0] * c->num_color[5] * c->num_color[2] * c->num_vary_y;448+ int vert_h = c->num_color[3] * c->num_color[4] * c->num_color[5] * c->num_vary_x;449+450+ int horz_x = horz_w * (2*c->short_side_len + 3);451+ int horz_y = horz_h * ( c->short_side_len + 3);452+453+ int vert_x = vert_w * ( c->short_side_len + 3);454+ int vert_y = vert_h * (2*c->short_side_len + 3);455+456+ horz_count = horz_w * horz_h;457+ vert_count = vert_w * vert_h;458+459+ size_x = horz_x > vert_x ? horz_x : vert_x;460+ size_y = 2 + horz_y + 2 + vert_y;461+ }462+ if (w) *w = size_x;463+ if (h) *h = size_y;464+ if (h_count) *h_count = horz_count;465+ if (v_count) *v_count = vert_count;466+}467+468+STBHW_EXTERN void stbhw_get_template_size(stbhw_config *c, int *w, int *h)469+{470+ stbhw__get_template_info(c, w, h, NULL, NULL);471+}472+473+static int stbhw__process_image(stbhw__process *p)474+{475+ int i,j,k,q, ypos;476+ int size_x, size_y;477+ stbhw_config *c = p->c;478+479+ stbhw__get_template_info(c, &size_x, &size_y, NULL, NULL);480+481+ if (p->w < size_x || p->h < size_y) {482+ stbhw_error = "image too small for configuration";483+ return 0;484+ }485+486+ if (c->is_corner) {487+ ypos = 2;488+ for (k=0; k < c->num_color[2]; ++k) {489+ for (j=0; j < c->num_color[1]; ++j) {490+ for (i=0; i < c->num_color[0]; ++i) {491+ for (q=0; q < c->num_vary_y; ++q) {492+ stbhw__process_h_row(p, 0,ypos,493+ 0,c->num_color[1]-1, 0,c->num_color[2]-1, 0,c->num_color[3]-1,494+ i,i, j,j, k,k,495+ c->num_vary_x);496+ ypos += c->short_side_len + 3;497+ }498+ }499+ }500+ }501+ ypos += 2;502+ for (k=0; k < c->num_color[3]; ++k) {503+ for (j=0; j < c->num_color[0]; ++j) {504+ for (i=0; i < c->num_color[1]; ++i) {505+ for (q=0; q < c->num_vary_x; ++q) {506+ stbhw__process_v_row(p, 0,ypos, 507+ 0,c->num_color[0]-1, 0,c->num_color[3]-1, 0,c->num_color[2]-1,508+ i,i, j,j, k,k,509+ c->num_vary_y);510+ ypos += (c->short_side_len*2) + 3;511+ }512+ }513+ }514+ }515+ assert(ypos == size_y);516+ } else {517+ ypos = 2;518+ for (k=0; k < c->num_color[3]; ++k) {519+ for (j=0; j < c->num_color[4]; ++j) {520+ for (i=0; i < c->num_color[2]; ++i) {521+ for (q=0; q < c->num_vary_y; ++q) {522+ stbhw__process_h_row(p, 0,ypos,523+ 0,c->num_color[2]-1, k,k,524+ 0,c->num_color[1]-1, j,j,525+ 0,c->num_color[0]-1, i,i,526+ c->num_vary_x);527+ ypos += c->short_side_len + 3;528+ }529+ }530+ }531+ }532+ ypos += 2;533+ for (k=0; k < c->num_color[3]; ++k) {534+ for (j=0; j < c->num_color[4]; ++j) {535+ for (i=0; i < c->num_color[5]; ++i) {536+ for (q=0; q < c->num_vary_x; ++q) {537+ stbhw__process_v_row(p, 0,ypos,538+ 0,c->num_color[0]-1, i,i,539+ 0,c->num_color[1]-1, j,j,540+ 0,c->num_color[5]-1, k,k,541+ c->num_vary_y);542+ ypos += (c->short_side_len*2) + 3;543+ }544+ }545+ }546+ }547+ assert(ypos == size_y);548+ }549+ return 1;550+}551+552+553+/////////////////////////////////////////////////////////////554+//555+// MAP GENERATOR556+//557+558+static void stbhw__draw_pixel(unsigned char *output, int stride, int x, int y, unsigned char c[3])559+{560+ memcpy(output + y*stride + x*3, c, 3);561+}562+563+static void stbhw__draw_h_tile(unsigned char *output, int stride, int xmax, int ymax, int x, int y, stbhw_tile *h, int sz)564+{565+ int i,j;566+ for (j=0; j < sz; ++j)567+ if (y+j >= 0 && y+j < ymax)568+ for (i=0; i < sz*2; ++i)569+ if (x+i >= 0 && x+i < xmax)570+ stbhw__draw_pixel(output,stride, x+i,y+j, &h->pixels[(j*sz*2 + i)*3]);571+}572+573+static void stbhw__draw_v_tile(unsigned char *output, int stride, int xmax, int ymax, int x, int y, stbhw_tile *h, int sz)574+{575+ int i,j;576+ for (j=0; j < sz*2; ++j)577+ if (y+j >= 0 && y+j < ymax)578+ for (i=0; i < sz; ++i)579+ if (x+i >= 0 && x+i < xmax)580+ stbhw__draw_pixel(output,stride, x+i,y+j, &h->pixels[(j*sz + i)*3]);581+}582+583+584+// randomly choose a tile that fits constraints for a given spot, and update the constraints585+static stbhw_tile * stbhw__choose_tile(stbhw_tile **list, int numlist,586+ signed char *a, signed char *b, signed char *c,587+ signed char *d, signed char *e, signed char *f,588+ int **weighting)589+{590+ int i,n,m = 1<<30,pass;591+ for (pass=0; pass < 2; ++pass) {592+ n=0;593+ // pass #1:594+ // count number of variants that match this partial set of constraints595+ // pass #2:596+ // stop on randomly selected match597+ for (i=0; i < numlist; ++i) {598+ stbhw_tile *h = list[i];599+ if ((*a < 0 || *a == h->a) &&600+ (*b < 0 || *b == h->b) &&601+ (*c < 0 || *c == h->c) &&602+ (*d < 0 || *d == h->d) &&603+ (*e < 0 || *e == h->e) &&604+ (*f < 0 || *f == h->f)) {605+ if (weighting)606+ n += weighting[0][i];607+ else608+ n += 1;609+ if (n > m) {610+ // use list[i]611+ // update constraints to reflect what we placed612+ *a = h->a;613+ *b = h->b;614+ *c = h->c;615+ *d = h->d;616+ *e = h->e;617+ *f = h->f;618+ return h;619+ }620+ }621+ }622+ if (n == 0) {623+ stbhw_error = "couldn't find tile matching constraints";624+ return NULL;625+ }626+ m = STB_HBWANG_RAND() % n;627+ }628+ STB_HBWANG_ASSERT(0);629+ return NULL;630+}631+632+static int stbhw__match(int x, int y)633+{634+ return c_color[y][x] == c_color[y+1][x+1];635+}636+637+static int stbhw__weighted(int num_options, int *weights)638+{639+ int k, total, choice;640+ total = 0;641+ for (k=0; k < num_options; ++k)642+ total += weights[k];643+ choice = STB_HBWANG_RAND() % total;644+ total = 0;645+ for (k=0; k < num_options; ++k) {646+ total += weights[k];647+ if (choice < total)648+ break;649+ }650+ STB_HBWANG_ASSERT(k < num_options);651+ return k;652+}653+654+static int stbhw__change_color(int old_color, int num_options, int *weights)655+{656+ if (weights) {657+ int k, total, choice;658+ total = 0;659+ for (k=0; k < num_options; ++k)660+ if (k != old_color)661+ total += weights[k];662+ choice = STB_HBWANG_RAND() % total;663+ total = 0;664+ for (k=0; k < num_options; ++k) {665+ if (k != old_color) {666+ total += weights[k];667+ if (choice < total)668+ break;669+ }670+ }671+ STB_HBWANG_ASSERT(k < num_options);672+ return k;673+ } else {674+ int offset = 1+STB_HBWANG_RAND() % (num_options-1);675+ return (old_color+offset) % num_options;676+ }677+}678+679+680+681+// generate a map that is w * h pixels (3-bytes each)682+// returns 1 on success, 0 on error 683+STBHW_EXTERN int stbhw_generate_image(stbhw_tileset *ts, int **weighting, unsigned char *output, int stride, int w, int h)684+{685+ int sidelen = ts->short_side_len;686+ int xmax = (w / sidelen) + 6;687+ int ymax = (h / sidelen) + 6;688+ if (xmax > STB_HBWANG_MAX_X+6 || ymax > STB_HBWANG_MAX_Y+6) {689+ stbhw_error = "increase STB_HBWANG_MAX_X/Y";690+ return 0;691+ }692+693+ if (ts->is_corner) {694+ int i,j, ypos;695+ int *cc = ts->num_color;696+697+ for (j=0; j < ymax; ++j) {698+ for (i=0; i < xmax; ++i) {699+ int p = (i-j+1)&3; // corner type700+ if (weighting==NULL || weighting[p]==0 || cc[p] == 1)701+ c_color[j][i] = STB_HBWANG_RAND() % cc[p];702+ else703+ c_color[j][i] = stbhw__weighted(cc[p], weighting[p]);704+ }705+ }706+ #ifndef STB_HBWANG_NO_REPITITION_REDUCTION707+ // now go back through and make sure we don't have adjancent 3x2 vertices that are identical,708+ // to avoid really obvious repetition (which happens easily with extreme weights)709+ for (j=0; j < ymax-3; ++j) {710+ for (i=0; i < xmax-3; ++i) {711+ int p = (i-j+1) & 3; // corner type712+ STB_HBWANG_ASSERT(i+3 < STB_HBWANG_MAX_X+6);713+ STB_HBWANG_ASSERT(j+3 < STB_HBWANG_MAX_Y+6);714+ if (stbhw__match(i,j) && stbhw__match(i,j+1) && stbhw__match(i,j+2)715+ && stbhw__match(i+1,j) && stbhw__match(i+1,j+1) && stbhw__match(i+1,j+2)) {716+ int p = ((i+1)-(j+1)+1) & 3;717+ if (cc[p] > 1)718+ c_color[j+1][i+1] = stbhw__change_color(c_color[j+1][i+1], cc[p], weighting ? weighting[p] : NULL);719+ }720+ if (stbhw__match(i,j) && stbhw__match(i+1,j) && stbhw__match(i+2,j)721+ && stbhw__match(i,j+1) && stbhw__match(i+1,j+1) && stbhw__match(i+2,j+1)) {722+ int p = ((i+2)-(j+1)+1) & 3;723+ if (cc[p] > 1)724+ c_color[j+1][i+2] = stbhw__change_color(c_color[j+1][i+2], cc[p], weighting ? weighting[p] : NULL);725+ }726+ }727+ }728+ #endif729+730+ ypos = -1 * sidelen;731+ for (j = -1; ypos < h; ++j) {732+ // a general herringbone row consists of:733+ // horizontal left block, the bottom of a previous vertical, the top of a new vertical734+ int phase = (j & 3);735+ // displace horizontally according to pattern736+ if (phase == 0) {737+ i = 0;738+ } else {739+ i = phase-4;740+ }741+ for (i;; i += 4) {742+ int xpos = i * sidelen;743+ if (xpos >= w)744+ break;745+ // horizontal left-block746+ if (xpos + sidelen*2 >= 0 && ypos >= 0) {747+ stbhw_tile *t = stbhw__choose_tile(748+ ts->h_tiles, ts->num_h_tiles,749+ &c_color[j+2][i+2], &c_color[j+2][i+3], &c_color[j+2][i+4],750+ &c_color[j+3][i+2], &c_color[j+3][i+3], &c_color[j+3][i+4],751+ weighting752+ );753+ if (t == NULL)754+ return 0;755+ stbhw__draw_h_tile(output,stride,w,h, xpos, ypos, t, sidelen);756+ }757+ xpos += sidelen * 2;758+ // now we're at the end of a previous vertical one759+ xpos += sidelen;760+ // now we're at the start of a new vertical one761+ if (xpos < w) {762+ stbhw_tile *t = stbhw__choose_tile(763+ ts->v_tiles, ts->num_v_tiles,764+ &c_color[j+2][i+5], &c_color[j+3][i+5], &c_color[j+4][i+5],765+ &c_color[j+2][i+6], &c_color[j+3][i+6], &c_color[j+4][i+6],766+ weighting767+ );768+ if (t == NULL)769+ return 0;770+ stbhw__draw_v_tile(output,stride,w,h, xpos, ypos, t, sidelen);771+ }772+ }773+ ypos += sidelen;774+ }775+ } else {776+ // @TODO edge-color repetition reduction777+ int i,j, ypos;778+ memset(v_color, -1, sizeof(v_color));779+ memset(h_color, -1, sizeof(h_color));780+781+ ypos = -1 * sidelen;782+ for (j = -1; ypos<h; ++j) {783+ // a general herringbone row consists of:784+ // horizontal left block, the bottom of a previous vertical, the top of a new vertical785+ int phase = (j & 3);786+ // displace horizontally according to pattern787+ if (phase == 0) {788+ i = 0;789+ } else {790+ i = phase-4;791+ }792+ for (i;; i += 4) {793+ int xpos = i * sidelen;794+ if (xpos >= w)795+ break;796+ // horizontal left-block797+ if (xpos + sidelen*2 >= 0 && ypos >= 0) {798+ stbhw_tile *t = stbhw__choose_tile(799+ ts->h_tiles, ts->num_h_tiles,800+ &h_color[j+2][i+2], &h_color[j+2][i+3],801+ &v_color[j+2][i+2], &v_color[j+2][i+4],802+ &h_color[j+3][i+2], &h_color[j+3][i+3],803+ weighting804+ );805+ if (t == NULL) return 0;806+ stbhw__draw_h_tile(output,stride,w,h, xpos, ypos, t, sidelen);807+ }808+ xpos += sidelen * 2;809+ // now we're at the end of a previous vertical one810+ xpos += sidelen;811+ // now we're at the start of a new vertical one812+ if (xpos < w) {813+ stbhw_tile *t = stbhw__choose_tile(814+ ts->v_tiles, ts->num_v_tiles,815+ &h_color[j+2][i+5],816+ &v_color[j+2][i+5], &v_color[j+2][i+6],817+ &v_color[j+3][i+5], &v_color[j+3][i+6],818+ &h_color[j+4][i+5],819+ weighting820+ );821+ if (t == NULL) return 0;822+ stbhw__draw_v_tile(output,stride,w,h, xpos, ypos, t, sidelen);823+ }824+ }825+ ypos += sidelen;826+ }827+ }828+ return 1;829+}830+831+static void stbhw__parse_h_rect(stbhw__process *p, int xpos, int ypos,832+ int a, int b, int c, int d, int e, int f)833+{834+ int len = p->c->short_side_len;835+ stbhw_tile *h = (stbhw_tile *) malloc(sizeof(*h)-1 + 3 * (len*2) * len);836+ int i,j;837+ ++xpos;838+ ++ypos;839+ h->a = a, h->b = b, h->c = c, h->d = d, h->e = e, h->f = f;840+ for (j=0; j < len; ++j)841+ for (i=0; i < len*2; ++i)842+ memcpy(h->pixels + j*(3*len*2) + i*3, p->data+(ypos+j)*p->stride+(xpos+i)*3, 3);843+ STB_HBWANG_ASSERT(p->ts->num_h_tiles < p->ts->max_h_tiles);844+ p->ts->h_tiles[p->ts->num_h_tiles++] = h;845+}846+847+static void stbhw__parse_v_rect(stbhw__process *p, int xpos, int ypos,848+ int a, int b, int c, int d, int e, int f)849+{850+ int len = p->c->short_side_len;851+ stbhw_tile *h = (stbhw_tile *) malloc(sizeof(*h)-1 + 3 * (len*2) * len);852+ int i,j;853+ ++xpos;854+ ++ypos;855+ h->a = a, h->b = b, h->c = c, h->d = d, h->e = e, h->f = f;856+ for (j=0; j < len*2; ++j)857+ for (i=0; i < len; ++i)858+ memcpy(h->pixels + j*(3*len) + i*3, p->data+(ypos+j)*p->stride+(xpos+i)*3, 3);859+ STB_HBWANG_ASSERT(p->ts->num_v_tiles < p->ts->max_v_tiles);860+ p->ts->v_tiles[p->ts->num_v_tiles++] = h;861+}862+863+STBHW_EXTERN int stbhw_build_tileset_from_image(stbhw_tileset *ts, unsigned char *data, int stride, int w, int h)864+{865+ int i, h_count, v_count;866+ unsigned char header[9];867+ stbhw_config c = { 0 };868+ stbhw__process p = { 0 };869+870+ // extract binary header871+872+ // remove encoding that makes it more visually obvious it encodes actual data873+ for (i=0; i < 9; ++i)874+ header[i] = data[w*3 - 1 - i] ^ (i*55);875+876+ // extract header info877+ if (header[7] == 0xc0) {878+ // corner-type879+ c.is_corner = 1;880+ for (i=0; i < 4; ++i)881+ c.num_color[i] = header[i];882+ c.num_vary_x = header[4];883+ c.num_vary_y = header[5];884+ c.short_side_len = header[6];885+ } else {886+ c.is_corner = 0;887+ // edge-type888+ for (i=0; i < 6; ++i)889+ c.num_color[i] = header[i];890+ c.num_vary_x = header[6];891+ c.num_vary_y = header[7];892+ c.short_side_len = header[8];893+ }894+895+ if (c.num_vary_x < 0 || c.num_vary_x > 64 || c.num_vary_y < 0 || c.num_vary_y > 64)896+ return 0;897+ if (c.short_side_len == 0)898+ return 0;899+900+ stbhw__get_template_info(&c, NULL, NULL, &h_count, &v_count);901+902+ ts->is_corner = c.is_corner;903+ ts->short_side_len = c.short_side_len;904+ memcpy(ts->num_color, c.num_color, sizeof(ts->num_color));905+906+ ts->max_h_tiles = h_count;907+ ts->max_v_tiles = v_count;908+909+ ts->num_h_tiles = ts->num_v_tiles = 0;910+911+ ts->h_tiles = (stbhw_tile **) malloc(sizeof(*ts->h_tiles) * h_count);912+ ts->v_tiles = (stbhw_tile **) malloc(sizeof(*ts->v_tiles) * v_count);913+914+ p.ts = ts;915+ p.data = data;916+ p.stride = stride;917+ p.process_h_rect = stbhw__parse_h_rect;918+ p.process_v_rect = stbhw__parse_v_rect;919+ p.w = w;920+ p.h = h;921+922+ // load all the tiles out of the image923+ return stbhw__process_image(&p);924+}925+926+STBHW_EXTERN void stbhw_free_tileset(stbhw_tileset *ts)927+{928+ int i;929+ for (i=0; i < ts->num_h_tiles; ++i)930+ free(ts->h_tiles[i]);931+ for (i=0; i < ts->num_v_tiles; ++i)932+ free(ts->v_tiles[i]);933+ free(ts->h_tiles);934+ free(ts->v_tiles);935+ ts->h_tiles = NULL;936+ ts->v_tiles = NULL;937+ ts->num_h_tiles = ts->max_h_tiles = 0;938+ ts->num_v_tiles = ts->max_v_tiles = 0;939+}940+941+//////////////////////////////////////////////////////////////////////////////942+//943+// GENERATOR944+//945+//946+947+948+// shared code949+950+static void stbhw__set_pixel(unsigned char *data, int stride, int xpos, int ypos, unsigned char color[3])951+{952+ memcpy(data + ypos*stride + xpos*3, color, 3);953+}954+955+static void stbhw__stbhw__set_pixel_whiten(unsigned char *data, int stride, int xpos, int ypos, unsigned char color[3])956+{957+ unsigned char c2[3];958+ int i;959+ for (i=0; i < 3; ++i)960+ c2[i] = (color[i]*2 + 255)/3;961+ memcpy(data + ypos*stride + xpos*3, c2, 3);962+}963+964+965+static unsigned char stbhw__black[3] = { 0,0,0 };966+967+// each edge set gets its own unique color variants968+// used http://phrogz.net/css/distinct-colors.html to generate this set,969+// but it's not very good and needs to be revised970+971+static unsigned char stbhw__color[7][8][3] =972+{973+ { {255,51,51} , {143,143,29}, {0,199,199}, {159,119,199}, {0,149,199} , {143, 0,143}, {255,128,0}, {64,255,0}, },974+ { {235,255,30 }, {255,0,255}, {199,139,119}, {29,143, 57}, {143,0,71} , { 0,143,143}, {0,99,199}, {143,71,0}, },975+ { {0,149,199} , {143, 0,143}, {255,128,0}, {64,255,0}, {255,191,0} , {51,255,153}, {0,0,143}, {199,119,159},},976+ { {143,0,71} , { 0,143,143}, {0,99,199}, {143,71,0}, {255,190,153}, { 0,255,255}, {128,0,255}, {255,51,102},},977+ { {255,191,0} , {51,255,153}, {0,0,143}, {199,119,159}, {255,51,51} , {143,143,29}, {0,199,199}, {159,119,199},},978+ { {255,190,153}, { 0,255,255}, {128,0,255}, {255,51,102}, {235,255,30 }, {255,0,255}, {199,139,119}, {29,143, 57}, },979+980+ { {40,40,40 }, { 90,90,90 }, { 150,150,150 }, { 200,200,200 },981+ { 255,90,90 }, { 160,160,80}, { 50,150,150 }, { 200,50,200 } },982+};983+984+static void stbhw__draw_hline(unsigned char *data, int stride, int xpos, int ypos, int color, int len, int slot)985+{986+ int i;987+ int j = len * 6 / 16;988+ int k = len * 10 / 16;989+ for (i=0; i < len; ++i)990+ stbhw__set_pixel(data, stride, xpos+i, ypos, stbhw__black);991+ if (k-j < 2) {992+ j = len/2 - 1;993+ k = j+2;994+ if (len & 1)995+ ++k;996+ }997+ for (i=j; i < k; ++i)998+ stbhw__stbhw__set_pixel_whiten(data, stride, xpos+i, ypos, stbhw__color[slot][color]);999+}1000+1001+static void stbhw__draw_vline(unsigned char *data, int stride, int xpos, int ypos, int color, int len, int slot)1002+{1003+ int i;1004+ int j = len * 6 / 16;1005+ int k = len * 10 / 16;1006+ for (i=0; i < len; ++i)1007+ stbhw__set_pixel(data, stride, xpos, ypos+i, stbhw__black);1008+ if (k-j < 2) {1009+ j = len/2 - 1;1010+ k = j+2;1011+ if (len & 1)1012+ ++k;1013+ }1014+ for (i=j; i < k; ++i)1015+ stbhw__stbhw__set_pixel_whiten(data, stride, xpos, ypos+i, stbhw__color[slot][color]);1016+}1017+1018+// 0--*--1--*--2--*--31019+// | | |1020+// * * *1021+// | | |1022+// 1--*--2--*--3 0--*--1--*--21023+// | | |1024+// * * *1025+// | | |1026+// 0--*--1--*--2--*--31027+//1028+// variables while enumerating (no correspondence between corners1029+// of the types is implied by these variables)1030+//1031+// a-----b-----c a-----d1032+// | | | |1033+// | | | |1034+// | | | |1035+// d-----e-----f b e1036+// | |1037+// | |1038+// | |1039+// c-----f1040+//1041+1042+unsigned char stbhw__corner_colors[4][4][3] =1043+{1044+ { { 255,0,0 }, { 200,200,200 }, { 100,100,200 }, { 255,200,150 }, },1045+ { { 0,0,255 }, { 255,255,0 }, { 100,200,100 }, { 150,255,200 }, },1046+ { { 255,0,255 }, { 80,80,80 }, { 200,100,100 }, { 200,150,255 }, },1047+ { { 0,255,255 }, { 0,255,0 }, { 200,120,200 }, { 255,200,200 }, },1048+};1049+1050+int stbhw__corner_colors_to_edge_color[4][4] =1051+{1052+ // 0 1 2 31053+ { 0, 1, 4, 9, }, // 01054+ { 2, 3, 5, 10, }, // 11055+ { 6, 7, 8, 11, }, // 21056+ { 12, 13, 14, 15, }, // 31057+};1058+1059+#define stbhw__c2e stbhw__corner_colors_to_edge_color1060+1061+static void stbhw__draw_clipped_corner(unsigned char *data, int stride, int xpos, int ypos, int w, int h, int x, int y)1062+{1063+ static unsigned char template_color[3] = { 167,204,204 };1064+ int i,j;1065+ for (j = -2; j <= 1; ++j) {1066+ for (i = -2; i <= 1; ++i) {1067+ if ((i == -2 || i == 1) && (j == -2 || j == 1))1068+ continue;1069+ else {1070+ if (x+i < 1 || x+i > w) continue;1071+ if (y+j < 1 || y+j > h) continue;1072+ stbhw__set_pixel(data, stride, xpos+x+i, ypos+y+j, template_color);1073+1074+ }1075+ }1076+ }1077+}1078+1079+static void stbhw__edge_process_h_rect(stbhw__process *p, int xpos, int ypos,1080+ int a, int b, int c, int d, int e, int f)1081+{1082+ int len = p->c->short_side_len;1083+ stbhw__draw_hline(p->data, p->stride, xpos+1 , ypos , a, len, 2);1084+ stbhw__draw_hline(p->data, p->stride, xpos+ len+1 , ypos , b, len, 3);1085+ stbhw__draw_vline(p->data, p->stride, xpos , ypos+1 , c, len, 1);1086+ stbhw__draw_vline(p->data, p->stride, xpos+2*len+1 , ypos+1 , d, len, 4);1087+ stbhw__draw_hline(p->data, p->stride, xpos+1 , ypos + len+1, e, len, 0);1088+ stbhw__draw_hline(p->data, p->stride, xpos + len+1 , ypos + len+1, f, len, 2);1089+}1090+1091+static void stbhw__edge_process_v_rect(stbhw__process *p, int xpos, int ypos,1092+ int a, int b, int c, int d, int e, int f)1093+{1094+ int len = p->c->short_side_len;1095+ stbhw__draw_hline(p->data, p->stride, xpos+1 , ypos , a, len, 0);1096+ stbhw__draw_vline(p->data, p->stride, xpos , ypos+1 , b, len, 5);1097+ stbhw__draw_vline(p->data, p->stride, xpos + len+1, ypos+1 , c, len, 1);1098+ stbhw__draw_vline(p->data, p->stride, xpos , ypos + len+1, d, len, 4);1099+ stbhw__draw_vline(p->data, p->stride, xpos + len+1, ypos + len+1, e, len, 5);1100+ stbhw__draw_hline(p->data, p->stride, xpos+1 , ypos + 2*len+1, f, len, 3);1101+}1102+1103+static void stbhw__corner_process_h_rect(stbhw__process *p, int xpos, int ypos,1104+ int a, int b, int c, int d, int e, int f)1105+{1106+ int len = p->c->short_side_len;1107+1108+ stbhw__draw_hline(p->data, p->stride, xpos+1 , ypos , stbhw__c2e[a][b], len, 2);1109+ stbhw__draw_hline(p->data, p->stride, xpos+ len+1 , ypos , stbhw__c2e[b][c], len, 3);1110+ stbhw__draw_vline(p->data, p->stride, xpos , ypos+1 , stbhw__c2e[a][d], len, 1);1111+ stbhw__draw_vline(p->data, p->stride, xpos+2*len+1 , ypos+1 , stbhw__c2e[c][f], len, 4);1112+ stbhw__draw_hline(p->data, p->stride, xpos+1 , ypos + len+1, stbhw__c2e[d][e], len, 0);1113+ stbhw__draw_hline(p->data, p->stride, xpos + len+1 , ypos + len+1, stbhw__c2e[e][f], len, 2);1114+1115+ if (p->c->corner_type_color_template[1][a]) stbhw__draw_clipped_corner(p->data,p->stride, xpos,ypos, len*2,len, 1,1);1116+ if (p->c->corner_type_color_template[2][b]) stbhw__draw_clipped_corner(p->data,p->stride, xpos,ypos, len*2,len, len+1,1);1117+ if (p->c->corner_type_color_template[3][c]) stbhw__draw_clipped_corner(p->data,p->stride, xpos,ypos, len*2,len, len*2+1,1);1118+1119+ if (p->c->corner_type_color_template[0][d]) stbhw__draw_clipped_corner(p->data,p->stride, xpos,ypos, len*2,len, 1,len+1);1120+ if (p->c->corner_type_color_template[1][e]) stbhw__draw_clipped_corner(p->data,p->stride, xpos,ypos, len*2,len, len+1,len+1);1121+ if (p->c->corner_type_color_template[2][f]) stbhw__draw_clipped_corner(p->data,p->stride, xpos,ypos, len*2,len, len*2+1,len+1);1122+1123+ stbhw__set_pixel(p->data, p->stride, xpos , ypos, stbhw__corner_colors[1][a]);1124+ stbhw__set_pixel(p->data, p->stride, xpos+len , ypos, stbhw__corner_colors[2][b]);1125+ stbhw__set_pixel(p->data, p->stride, xpos+2*len+1, ypos, stbhw__corner_colors[3][c]);1126+ stbhw__set_pixel(p->data, p->stride, xpos , ypos+len+1, stbhw__corner_colors[0][d]);1127+ stbhw__set_pixel(p->data, p->stride, xpos+len , ypos+len+1, stbhw__corner_colors[1][e]);1128+ stbhw__set_pixel(p->data, p->stride, xpos+2*len+1, ypos+len+1, stbhw__corner_colors[2][f]);1129+}1130+1131+static void stbhw__corner_process_v_rect(stbhw__process *p, int xpos, int ypos,1132+ int a, int b, int c, int d, int e, int f)1133+{1134+ int len = p->c->short_side_len;1135+1136+ stbhw__draw_hline(p->data, p->stride, xpos+1 , ypos , stbhw__c2e[a][d], len, 0);1137+ stbhw__draw_vline(p->data, p->stride, xpos , ypos+1 , stbhw__c2e[a][b], len, 5);1138+ stbhw__draw_vline(p->data, p->stride, xpos + len+1, ypos+1 , stbhw__c2e[d][e], len, 1);1139+ stbhw__draw_vline(p->data, p->stride, xpos , ypos + len+1, stbhw__c2e[b][c], len, 4);1140+ stbhw__draw_vline(p->data, p->stride, xpos + len+1, ypos + len+1, stbhw__c2e[e][f], len, 5);1141+ stbhw__draw_hline(p->data, p->stride, xpos+1 , ypos + 2*len+1, stbhw__c2e[c][f], len, 3);1142+1143+ if (p->c->corner_type_color_template[0][a]) stbhw__draw_clipped_corner(p->data,p->stride, xpos,ypos, len,len*2, 1,1);1144+ if (p->c->corner_type_color_template[3][b]) stbhw__draw_clipped_corner(p->data,p->stride, xpos,ypos, len,len*2, 1,len+1);1145+ if (p->c->corner_type_color_template[2][c]) stbhw__draw_clipped_corner(p->data,p->stride, xpos,ypos, len,len*2, 1,len*2+1);1146+1147+ if (p->c->corner_type_color_template[1][d]) stbhw__draw_clipped_corner(p->data,p->stride, xpos,ypos, len,len*2, len+1,1);1148+ if (p->c->corner_type_color_template[0][e]) stbhw__draw_clipped_corner(p->data,p->stride, xpos,ypos, len,len*2, len+1,len+1);1149+ if (p->c->corner_type_color_template[3][f]) stbhw__draw_clipped_corner(p->data,p->stride, xpos,ypos, len,len*2, len+1,len*2+1);1150+1151+ stbhw__set_pixel(p->data, p->stride, xpos , ypos , stbhw__corner_colors[0][a]);1152+ stbhw__set_pixel(p->data, p->stride, xpos , ypos+len , stbhw__corner_colors[3][b]);1153+ stbhw__set_pixel(p->data, p->stride, xpos , ypos+2*len+1, stbhw__corner_colors[2][c]);1154+ stbhw__set_pixel(p->data, p->stride, xpos+len+1, ypos , stbhw__corner_colors[1][d]);1155+ stbhw__set_pixel(p->data, p->stride, xpos+len+1, ypos+len , stbhw__corner_colors[0][e]);1156+ stbhw__set_pixel(p->data, p->stride, xpos+len+1, ypos+2*len+1, stbhw__corner_colors[3][f]);1157+}1158+1159+#endif // STB_HBWANG_IMPLEMENTATION1160+1161+#ifndef INCLUDE_STB_IMAGE_WRITE_H1162+#define STB_IMAGE_WRITE_IMPLEMENTATION1163+#include "stb_image_write.h"1164+#endif1165+1166+int corner_color_count[4] = { 2,2,2,2 };1167+1168+// whether each color for each type of corner should get the corner template1169+int corner_type_color_template[4][4] =1170+{1171+ { 0,0,0,0 },1172+ { 0,0,0,0 },1173+ { 0,0,0,0 },1174+ { 0,0,0,0 },1175+};1176+1177+// number of duplicates with the exact same edge colors1178+int num_variants_x = 1;1179+int num_variants_y = 1;1180+1181+// total number of items in complete set:1182+//1183+// horizontal items:1184+// ec[1] * ec[2] * ec[3] * ec[0] * ec[1] * ec[2] * num_variants_x * num_variants_y1185+//1186+// vertical items:1187+// ec[0] * ce[1] * ce[3] * ce[0] * ec[2] * ec[3] * num_variants_x * num_variants_y1188+1189+// number of tiles along the short side1190+int short_side = 9;1191+1192+int my_main(int argc, char **argv)1193+{1194+ stbhw_config c;1195+ stbhw__process p;1196+ int i;1197+1198+ if (argc != 2) {1199+ fprintf(stderr, "Usage: gentemplate {filename}\n"); 1200+ return 1;1201+ }1202+ p.data = NULL;1203+ p.h = p.w = p.stride = 0;1204+ p.process_h_rect = stbhw__corner_process_h_rect;1205+ p.process_v_rect = stbhw__corner_process_v_rect;1206+ //p.ts = 0;1207+ p.c = & c;1208+1209+ c.is_corner = 1;1210+ c.short_side_len = short_side;1211+ c.num_vary_x = num_variants_x;1212+ c.num_vary_y = num_variants_y;1213+ memcpy(c.num_color, corner_color_count, sizeof(corner_color_count));1214+1215+ stbhw__process_image(&p);1216+1217+ // write out binary information in first line of image1218+ for (i=0; i < 4; ++i)1219+ p.data[p.w*3-1-i] = c.num_color[i];1220+ p.data[p.w*3-1-i] = num_variants_x;1221+ p.data[p.w*3-2-i] = num_variants_y;1222+ p.data[p.w*3-3-i] = short_side;1223+ p.data[p.w*3-4-i] = 0xc0;1224+1225+ // make it more obvious it encodes actual data1226+ for (i=0; i < 9; ++i)1227+ p.data[p.w*3 - 1 - i] ^= i*55;1228+1229+ stbi_write_png(argv[1], p.w, p.h, 3, p.data, p.w*3);1230+ return 0;1231+}1232+1233+1234+/*1235+// number of colors for each edge based on above diagram:1236+int edge_colors[6] = { 1,1,1,1,1,1, };1237+1238+// number of duplicates with the exact same edge colors1239+int num_variants_x = 4;1240+int num_variants_y = 4;1241+1242+// total number of items in complete set:1243+//1244+// horizontal items:1245+// ec[0] * ec[1] * ec[2] * ec[3] * ec[4] * ec[2] * num_variants_x * num_variants_y1246+//1247+// vertical items:1248+// ec[0] * ce[1] * ce[5] * ce[3] * ec[4] * ec[5] * num_variants_x * num_variants_y1249+1250+// number of tiles along the short side1251+int short_side = 12;1252+1253+int main(int argc, char **argv)1254+{1255+ int w,h,i;1256+ unsigned char *data;1257+1258+ if (argc != 2) {1259+ fprintf(stderr, "Usage: gentemplate {filename}\n"); 1260+ return 1;1261+ }1262+1263+ memcpy(ec, edge_colors, sizeof(edge_colors));1264+ num_vx = num_variants_x;1265+ num_vy = num_variants_y;1266+1267+ data = process_image(NULL, &w, &h, short_side);1268+1269+ // write out binary information in first line of image1270+ for (i=0; i < 6; ++i)1271+ data[w*3-1-i] = ec[i];1272+ data[w*3-1-i] = num_variants_x;1273+ data[w*3-2-i] = num_variants_y;1274+ data[w*3-3-i] = short_side;1275+1276+ // make it more obvious it encodes actual data1277+ for (i=0; i < 9; ++i)1278+ data[w*3 - 1 - i] ^= i*55;1279+1280+ stbi_write_png(argv[1], w, h, 3, data, w*3);1281+ return 0;1282+}1283+*/