eobish
changeset 0:465ca72c9657
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 17 Jan 2015 18:37:28 +0200 |
parents | |
children | bc0996863c79 |
files | .hgignore Makefile doc/palette_allocation src/main.cc utils/Makefile utils/tileproc.c |
diffstat | 6 files changed, 320 insertions(+), 0 deletions(-) [+] |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/.hgignore Sat Jan 17 18:37:28 2015 +0200 1.3 @@ -0,0 +1,7 @@ 1.4 +\.o$ 1.5 +\.d$ 1.6 +\.swp$ 1.7 +^orig_data/ 1.8 +^data/ 1.9 +^eobish$ 1.10 +^utils/tileproc$
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/Makefile Sat Jan 17 18:37:28 2015 +0200 2.3 @@ -0,0 +1,19 @@ 2.4 +csrc = $(wildcard src/*.c) 2.5 +ccsrc = $(wildcard src/*.cc) 2.6 +obj = $(csrc:.c=.o) $(ccsrc:.cc=.o) 2.7 +bin = eobish 2.8 + 2.9 +CFLAGS = -pedantic -Wall -g `pkg-config --cflags sdl` 2.10 +CXXFLAGS = $(CFLAGS) 2.11 +LDFLAGS = `pkg-config --libs sdl` 2.12 + 2.13 +$(bin): $(obj) 2.14 + $(CXX) -o $@ $(obj) $(LDFLAGS) 2.15 + 2.16 +.PHONY: clean 2.17 +clean: 2.18 + rm -f $(obj) $(bin) 2.19 + 2.20 +.PHONY: data 2.21 +data: 2.22 + $(MAKE) -C orig_data
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/doc/palette_allocation Sat Jan 17 18:37:28 2015 +0200 3.3 @@ -0,0 +1,9 @@ 3.4 +Palette allocation (32 color blocks) 3.5 +------------------------------------ 3.6 + 0 - 31 : UI 3.7 + 32 - 63 : enemies dark 3.8 + 64 - 95 : enemies light (original) 3.9 + 96 - 159: effects 3.10 +160 - 191: dungeon dark 3.11 +192 - 223: dungeon mid 3.12 +224 - 255: dungeon light (original)
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/src/main.cc Sat Jan 17 18:37:28 2015 +0200 4.3 @@ -0,0 +1,81 @@ 4.4 +#include <stdio.h> 4.5 +#include <SDL/SDL.h> 4.6 + 4.7 +static void display(); 4.8 +static bool proc_event(SDL_Event *ev); 4.9 +static void set_pal_entry(int idx, int r, int g, int b); 4.10 + 4.11 +static SDL_Surface *fbsurf; 4.12 + 4.13 +int main(int argc, char **argv) 4.14 +{ 4.15 + SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER); 4.16 + 4.17 + if(!(fbsurf = SDL_SetVideoMode(320, 240, 8, SDL_SWSURFACE))) { 4.18 + fprintf(stderr, "failed to set video mode\n"); 4.19 + return 1; 4.20 + } 4.21 + set_pal_entry(1, 255, 0, 0); 4.22 + 4.23 + for(;;) { 4.24 + SDL_Event ev; 4.25 + while(SDL_PollEvent(&ev)) { 4.26 + if(!proc_event(&ev)) { 4.27 + goto done; 4.28 + } 4.29 + } 4.30 + 4.31 + display(); 4.32 + } 4.33 + 4.34 +done: 4.35 + SDL_Quit(); 4.36 + return 0; 4.37 +} 4.38 + 4.39 +void display() 4.40 +{ 4.41 + if(SDL_MUSTLOCK(fbsurf)) { 4.42 + SDL_LockSurface(fbsurf); 4.43 + } 4.44 + 4.45 + unsigned char *pixels = (unsigned char*)fbsurf->pixels; 4.46 + for(int i=0; i<fbsurf->w * fbsurf->h; i++) { 4.47 + *pixels++ = 1; 4.48 + } 4.49 + 4.50 + if(SDL_MUSTLOCK(fbsurf)) { 4.51 + SDL_UnlockSurface(fbsurf); 4.52 + } 4.53 + 4.54 + SDL_Flip(fbsurf); 4.55 +} 4.56 + 4.57 +static bool proc_event(SDL_Event *ev) 4.58 +{ 4.59 + switch(ev->type) { 4.60 + case SDL_KEYDOWN: 4.61 + if(ev->key.keysym.sym == SDLK_ESCAPE) { 4.62 + return false; 4.63 + } 4.64 + break; 4.65 + 4.66 + default: 4.67 + break; 4.68 + } 4.69 + return true; 4.70 +} 4.71 + 4.72 +static void set_pal_entry(int idx, int r, int g, int b) 4.73 +{ 4.74 + SDL_Color col; 4.75 + col.r = r; 4.76 + col.g = g; 4.77 + col.b = b; 4.78 + 4.79 + SDL_SetPalette(fbsurf, SDL_LOGPAL | SDL_PHYSPAL, &col, idx, 1); 4.80 + 4.81 + /*palette[idx].r = r; 4.82 + palette[idx].g = g; 4.83 + palette[idx].b = b;*/ 4.84 +}
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/utils/Makefile Sat Jan 17 18:37:28 2015 +0200 5.3 @@ -0,0 +1,8 @@ 5.4 +CFLAGS = -pedantic -Wall -g 5.5 + 5.6 +tileproc: tileproc.o 5.7 + $(CC) -o $@ $< -limago 5.8 + 5.9 +.PHONY: clean 5.10 +clean: 5.11 + rm -f *.o
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/utils/tileproc.c Sat Jan 17 18:37:28 2015 +0200 6.3 @@ -0,0 +1,196 @@ 6.4 +#include <stdio.h> 6.5 +#include <stdlib.h> 6.6 +#include <string.h> 6.7 +#include <math.h> 6.8 +#include <float.h> 6.9 +#include <errno.h> 6.10 +#ifndef WIN32 6.11 +#include <alloca.h> 6.12 +#else 6.13 +#include <malloc.h> 6.14 +#endif 6.15 +#include <imago2.h> 6.16 + 6.17 +struct palentry { 6.18 + float r, g, b; 6.19 + struct palentry *next; 6.20 +}; 6.21 + 6.22 +int load_palette(const char *fname); 6.23 +int proc_tile(const char *fname); 6.24 +float find_nearest(float r, float g, float b); 6.25 + 6.26 +struct palentry *palette; 6.27 +int palsize; 6.28 + 6.29 +int main(int argc, char **argv) 6.30 +{ 6.31 + int i; 6.32 + 6.33 + for(i=1; i<argc; i++) { 6.34 + if(argv[i][0] == '-') { 6.35 + switch(argv[i][1]) { 6.36 + case 'p': 6.37 + if(load_palette(argv[++i]) == -1) { 6.38 + return 1; 6.39 + } 6.40 + break; 6.41 + 6.42 + default: 6.43 + fprintf(stderr, "invalid option: %s\n", argv[i]); 6.44 + return 1; 6.45 + } 6.46 + } else { 6.47 + if(proc_tile(argv[i]) == -1) { 6.48 + return 1; 6.49 + } 6.50 + } 6.51 + } 6.52 + return 0; 6.53 +} 6.54 + 6.55 +int load_palette(const char *fname) 6.56 +{ 6.57 + FILE *fp; 6.58 + char buf[128]; 6.59 + struct palentry *newpal; 6.60 + struct palentry *head = 0, *tail; 6.61 + int nent = 0; 6.62 + 6.63 + if(!(fp = fopen(fname, "r"))) { 6.64 + fprintf(stderr, "failed to open palette file: %s: %s\n", fname, strerror(errno)); 6.65 + return -1; 6.66 + } 6.67 + 6.68 + while(fgets(buf, sizeof buf, fp)) { 6.69 + struct palentry *pent; 6.70 + char *endp, *line = buf; 6.71 + int r, g, b; 6.72 + 6.73 + if(!line || !*line) continue; 6.74 + 6.75 + if(*line == '#') { /* hex html-like values */ 6.76 + unsigned int val = strtol(line + 1, &endp, 16); 6.77 + if(endp == line) { 6.78 + fprintf(stderr, "unrecognized line \"%s\" in palette file: %s\n", line, fname); 6.79 + goto fail; 6.80 + } 6.81 + 6.82 + r = (val >> 16) & 0xff; 6.83 + g = (val >> 8) & 0xff; 6.84 + b = val & 0xff; 6.85 + } else { 6.86 + fprintf(stderr, "unrecognized line \"%s\" in palette file: %s\n", line, fname); 6.87 + goto fail; 6.88 + } 6.89 + 6.90 + if(!(pent = malloc(sizeof *pent))) { 6.91 + perror("failed to allocate palette entry"); 6.92 + goto fail; 6.93 + } 6.94 + pent->r = (float)r / 255.0; 6.95 + pent->g = (float)g / 255.0; 6.96 + pent->b = (float)b / 255.0; 6.97 + pent->next = 0; 6.98 + 6.99 + if(head) { 6.100 + tail->next = pent; 6.101 + tail = pent; 6.102 + } else { 6.103 + head = tail = pent; 6.104 + } 6.105 + nent++; 6.106 + } 6.107 + 6.108 + /* flatten */ 6.109 + if(!(newpal = malloc(nent * sizeof *newpal))) { 6.110 + fprintf(stderr, "failed to allocate %d palette entries: %s\n", nent, strerror(errno)); 6.111 + goto fail; 6.112 + } 6.113 + palette = newpal; 6.114 + palsize = nent; 6.115 + 6.116 + while(head) { 6.117 + struct palentry *ent = head; 6.118 + head = head->next; 6.119 + 6.120 + *newpal++ = *ent; 6.121 + free(ent); 6.122 + } 6.123 + 6.124 + printf("loaded palette: %s (%d colors)\n", fname, nent); 6.125 + 6.126 + fclose(fp); 6.127 + return 0; 6.128 + 6.129 +fail: 6.130 + fclose(fp); 6.131 + while(head) { 6.132 + void *tmp = head; 6.133 + head = head->next; 6.134 + free(tmp); 6.135 + } 6.136 + return -1; 6.137 +} 6.138 + 6.139 +int proc_tile(const char *fname) 6.140 +{ 6.141 + int i; 6.142 + int xsz, ysz; 6.143 + unsigned char *pixels, *pptr; 6.144 + char *outfname, *cptr; 6.145 + FILE *fp; 6.146 + const char *magic = "TILEIMAG"; 6.147 + 6.148 + outfname = alloca(strlen(fname) + 4); 6.149 + strcpy(outfname, fname); 6.150 + if((cptr = strrchr(outfname, '.'))) { 6.151 + *cptr = 0; 6.152 + } 6.153 + strcat(outfname, ".til"); 6.154 + 6.155 + printf("processing tile: %s -> %s\n", fname, outfname); 6.156 + 6.157 + if(!(pixels = img_load_pixels(fname, &xsz, &ysz, IMG_FMT_RGB24))) { 6.158 + fprintf(stderr, "failed to load source tile image: %s\n", fname); 6.159 + return -1; 6.160 + } 6.161 + if(!(fp = fopen(outfname, "wb"))) { 6.162 + fprintf(stderr, "failed to open output tile image for writing: %s: %s\n", outfname, strerror(errno)); 6.163 + img_free_pixels(pixels); 6.164 + return -1; 6.165 + } 6.166 + /* write header */ 6.167 + fwrite(magic, 1, 8, fp); 6.168 + fwrite(&xsz, sizeof xsz, 1, fp); 6.169 + fwrite(&ysz, sizeof ysz, 1, fp); 6.170 + 6.171 + pptr = pixels; 6.172 + for(i=0; i<xsz * ysz; i++) { 6.173 + int idx = find_nearest(pptr[0] / 255.0, pptr[1] / 255.0, pptr[2] / 255.0); 6.174 + fputc(idx, fp); 6.175 + pptr += 3; 6.176 + } 6.177 + img_free_pixels(pixels); 6.178 + fclose(fp); 6.179 + return 0; 6.180 +} 6.181 + 6.182 +float find_nearest(float r, float g, float b) 6.183 +{ 6.184 + int i, res; 6.185 + float mindist = FLT_MAX; 6.186 + 6.187 + for(i=0; i<palsize; i++) { 6.188 + float dr = r - palette[i].r; 6.189 + float dg = g - palette[i].g; 6.190 + float db = b - palette[i].b; 6.191 + float dist = dr * dr + dg * dg + db * db; 6.192 + 6.193 + if(dist < mindist) { 6.194 + mindist = dist; 6.195 + res = i; 6.196 + } 6.197 + } 6.198 + return res; 6.199 +}