#include #include /* Copyright (c) 2020 Devine Lu Linvega Permission to use, copy, modify, and distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE. */ #define HOR 32 #define VER 16 #define PAD 2 #define SZ (HOR * VER * 16) typedef unsigned char Uint8; typedef struct { int unsaved; char name[256]; Uint8 data[SZ]; } Document; typedef struct { int w, h; } Size2d; typedef struct { int x, y; } Point2d; int WIDTH = 8 * HOR + 8 * PAD * 2; int HEIGHT = 8 * (VER + 2) + 8 * PAD * 2; int FPS = 30, GUIDES = 1, DOWN = 0, ZOOM = 2, COLORS = 1; Document doc; Size2d size; Point2d pickers[4]; Point2d *selected; Uint32 theme[] = { 0x000000, 0xFFFFFF, 0x72DEC2, 0x666666, 0x222222}; Uint8 icons[][8] = { {0x38, 0x7c, 0xfe, 0xfe, 0xfe, 0x7c, 0x38, 0x00}, /* color:full */ {0x00, 0x38, 0x7c, 0x7c, 0x7c, 0x38, 0x00, 0x00}, /* color:blank */ {0xfe, 0x82, 0x82, 0xfe, 0x82, 0x82, 0xfe, 0x00}, /* brush:1bit */ {0xfe, 0x92, 0x92, 0xfe, 0x92, 0x92, 0xfe, 0x00}, /* brush:2bit */ {0x18, 0x38, 0x18, 0x18, 0x18, 0x18, 0x7e, 0x00}, /* picker:1 */ {0x7c, 0xc6, 0x0e, 0x3c, 0x78, 0xe0, 0xfe, 0x00}, /* picker:2 */ {0x7e, 0x0c, 0x18, 0x3c, 0x06, 0xc6, 0x7c, 0x00}, /* picker:3 */ {0x1c, 0x3c, 0x6c, 0xcc, 0xfe, 0x0c, 0x0c, 0x00}, /* picker:4 */ {0xee, 0x92, 0x82, 0x54, 0x82, 0x92, 0xee, 0x00}, /* view:bigpixels */ {0x00, 0x00, 0x00, 0x82, 0x44, 0x38, 0x00, 0x00}, /* eye open */ {0x00, 0x38, 0x44, 0x92, 0x28, 0x10, 0x00, 0x00}, /* eye closed */ {0x10, 0x54, 0x28, 0xc6, 0x28, 0x54, 0x10, 0x00} /* unsaved */ }; SDL_Window *gWindow; SDL_Renderer *gRenderer; SDL_Texture *gTexture; Uint32 *pixels; SDL_Surface *image; #pragma mark - HELPERS int clamp(int val, int min, int max) { return (val >= min) ? (val <= max) ? val : max : min; } int distance(int ax, int ay, int bx, int by) { return (bx - ax) * (bx - ax) + (by - ay) * (by - ay); } int coloroffset(Uint32 a, Uint32 b) { Uint8 r0 = (a >> 16) & 0xFF, g0 = (a >> 8) & 0xFF, b0 = a & 0xFF; Uint8 r1 = (b >> 16) & 0xFF, g1 = (b >> 8) & 0xFF, b1 = b & 0xFF; return abs(r0 - r1) + abs(g0 - g1) + abs(b0 - b1); } char * scpy(char *src, char *dst, int len) { int i = 0; while((dst[i] = src[i]) && i < len - 2) i++; dst[i + 1] = '\0'; return dst; } #pragma mark - CHR HANDLERS int rowchr(int x, int y) { return (y % 8) + ((x / 8 + y / 8 * HOR) * 16); } int getchr(int x, int y) { int ch1, ch2, r = rowchr(x, y); if(r < 0 || r > SZ - 8) return 0; ch1 = (doc.data[r] >> (7 - x % 8)) & 1; ch2 = (doc.data[r + 8] >> (7 - x % 8)) & 1; if(ch1 && !ch2) return 1; if(!ch1 && ch2) return 2; if(ch1 && ch2) return 3; return 0; } void putchr(int x, int y, int color) { int row = rowchr(x, y), col = x % 8; if(x < 0 || x >= HOR * 8 || y < 0 || y >= VER * 8) return; if(color == 0 || color == 2) doc.data[row] &= ~(1UL << (7 - col)); else doc.data[row] |= 1UL << (7 - col); if(color == 0 || color == 1) doc.data[row + 8] &= ~(1UL << (7 - col)); else doc.data[row + 8] |= 1UL << (7 - col); doc.unsaved = 1; } #pragma mark - DRAW void clear(Uint32 *dst) { int v, h; for(v = 0; v < HEIGHT; v++) for(h = 0; h < WIDTH; h++) dst[v * WIDTH + h] = theme[0]; } void putpixel(Uint32 *dst, int x, int y, int color) { if(x >= 0 && x < WIDTH - 8 && y >= 0 && y < HEIGHT - 8) dst[(y + PAD * 8) * WIDTH + (x + PAD * 8)] = theme[color]; } void drawchr(Uint32 *dst, int x, int y, Uint8 *sprite) { int v, h; for(v = 0; v < 8; v++) for(h = 0; h < 8; h++) { int ch1 = ((sprite[v] >> h) & 0x1); int ch2 = (((sprite[v + 8] >> h) & 0x1) << 1); putpixel(dst, x + 7 - h, y + v, ch1 + ch2); } } void drawicn(Uint32 *dst, int x, int y, Uint8 *sprite, int fg, int bg) { int v, h; for(v = 0; v < 8; v++) for(h = 0; h < 8; h++) { int ch1 = (sprite[v] >> (7 - h)) & 0x1; putpixel(dst, x + h, y + v, ch1 ? fg : bg); } } void drawicna(Uint32 *dst, int x, int y, Uint8 *icon, int fg) { int v, h; for(v = 0; v < 8; v++) for(h = 0; h < 8; h++) if((icon[v] >> (7 - h)) & 0x1) putpixel(dst, x + h, y + v, fg); } void drawui(Uint32 *dst) { int bottom = VER * 8 + 8; drawicn(dst, 0, bottom, icons[COLORS ? 3 : 2], 1, 0); drawicn(dst, 2 * 8, bottom, icons[GUIDES ? 10 : 9], GUIDES ? 1 : 2, 0); drawicn(dst, (HOR - 1) * 8, bottom, icons[11], doc.unsaved ? 2 : 3, 0); /* save state */ } Uint32 getpixel(SDL_Surface *surface, int x, int y) { int bpp = surface->format->BytesPerPixel; Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp; switch(bpp) { case 1: return *p; case 2: return *(Uint16 *)p; case 3: if(SDL_BYTEORDER == SDL_BIG_ENDIAN) return p[0] << 16 | p[1] << 8 | p[2]; else return p[0] | p[1] << 8 | p[2] << 16; case 4: return *(Uint32 *)p; } return 0; } Uint32 getcolor(int x, int y) { SDL_Color rgb; SDL_GetRGB(getpixel(image, x, y), image->format, &rgb.r, &rgb.g, &rgb.b); return ((rgb.r & 0xFF) << 16) + ((rgb.g & 0xFF) << 8) + (rgb.b & 0xFF); } int getpattern(int palette, int x, int y) { if(palette == 3) return 3; if(palette == 2) return ((x + y) % 2) == 0 && ((y - x) % 2) == 0 ? 3 : 0; if(palette == 1) return ((x + y) % 4) == 0 && ((y - x) % 4) == 0 ? 3 : 0; return 0; } int getpalette(Uint32 clr) { Uint32 res = 0, dis = 999999; int i; for(i = 0; i < 4; ++i) { Uint32 d = coloroffset(clr, theme[i]); if(d < dis) { dis = d; res = i; } } return res; } void drawpicker(Uint32 *dst, int id) { drawicna(dst, pickers[id].x - 3, pickers[id].y - 3, icons[0], 4); drawicna(dst, pickers[id].x + 6, pickers[id].y - 3, icons[4 + id], 4); drawicna(dst, pickers[id].x - 3, pickers[id].y - 3, icons[1], id); } void redraw(Uint32 *dst) { int x, y; /* Update palette */ theme[0] = getcolor(pickers[0].x, pickers[0].y); theme[1] = getcolor(pickers[1].x, pickers[1].y); theme[2] = getcolor(pickers[2].x, pickers[2].y); theme[3] = getcolor(pickers[3].x, pickers[3].y); /* Draw */ clear(dst); drawui(dst); /* Draw images */ SDL_LockSurface(image); for(y = 0; y < image->h; ++y) { for(x = 0; x < image->w; ++x) { int palette = getpalette(getcolor(x, y)); putchr(x, y, COLORS ? palette : getpattern(palette, x, y)); } } SDL_UnlockSurface(image); for(y = 0; y < VER; ++y) for(x = 0; x < HOR; ++x) drawchr(dst, x * 8, y * 8, &doc.data[(x + y * HOR) * 16]); if(GUIDES) { /* draw pickers */ drawpicker(dst, 0); drawpicker(dst, 1); drawpicker(dst, 2); drawpicker(dst, 3); } SDL_UpdateTexture(gTexture, NULL, dst, WIDTH * sizeof(Uint32)); SDL_RenderClear(gRenderer); SDL_RenderCopy(gRenderer, gTexture, NULL, NULL); SDL_RenderPresent(gRenderer); } #pragma mark - OPTIONS int error(char *msg, const char *err) { printf("Error %s: %s\n", msg, err); return 0; } void savemode(int *i, int v) { *i = v; redraw(pixels); } void makedoc(Document *d, char *name) { int i; for(i = 0; i < SZ; ++i) d->data[i] = 0x00; d->unsaved = 0; scpy(name, d->name, 256); printf("Made: %s\n", d->name); redraw(pixels); } int savedoc(Document *d, char *name) { FILE *f = fopen(name, "w"); if(!fwrite(d->data, sizeof(d->data), 1, f)) return error("Save", "Failure"); d->unsaved = 0; scpy(name, d->name, 256); fclose(f); printf("0x%x 0x%x 0x%x 0x%x\n", theme[0], theme[1], theme[2], theme[3]); printf("Saved: %s\n", d->name); redraw(pixels); return 1; } int saveicn(Document *d, char *name) { int i; FILE *f = fopen(name, "w"); Uint8 data[SZ / 2]; for(i = 0; i < SZ / 2; ++i) data[i] = d->data[(i / 4) * 8 + (i % 8)]; if(!fwrite(data, sizeof(data), 1, f)) return error("Save", "Failure"); d->unsaved = 0; scpy(name, d->name, 256); fclose(f); printf("Saved(icn): %s\n", d->name); redraw(pixels); return 1; } void setpicker(Point2d *picker, int x, int y) { picker->x = clamp(x, 0, HOR * 8); picker->y = clamp(y, 0, VER * 8); redraw(pixels); } Point2d * selectpicker(int x, int y) { int i; for(i = 0; i < 4; ++i) if(distance(x, y, pickers[i].x, pickers[i].y) < 20) return &pickers[i]; return NULL; } int savebmp(void) { SDL_Surface *surface = SDL_GetWindowSurface(gWindow); SDL_RenderReadPixels(gRenderer, NULL, SDL_PIXELFORMAT_ARGB8888, surface->pixels, surface->pitch); if(SDL_SaveBMP(surface, "dito-render.bmp")) return error("Render", "Failed"); puts("Render: dito-render.bmp"); SDL_FreeSurface(surface); return 1; } void selectoption(int option) { switch(option) { case 0: savemode(&COLORS, !COLORS); break; case 2: savemode(&GUIDES, !GUIDES); break; case HOR - 1: COLORS ? savedoc(&doc, doc.name) : saveicn(&doc, doc.name); break; } } void quit(void) { free(pixels); SDL_DestroyTexture(gTexture); gTexture = NULL; SDL_DestroyRenderer(gRenderer); gRenderer = NULL; SDL_DestroyWindow(gWindow); gWindow = NULL; SDL_Quit(); exit(0); } #pragma mark - TRIGGERS void domouse(SDL_Event *event) { switch(event->type) { case SDL_MOUSEBUTTONUP: DOWN = 0; selected = NULL; break; case SDL_MOUSEBUTTONDOWN: if(event->motion.y / ZOOM / 8 - PAD == VER + 1) { selectoption(event->motion.x / ZOOM / 8 - PAD); return; } selected = selectpicker(event->motion.x / ZOOM - PAD * 8, event->motion.y / ZOOM - PAD * 8); DOWN = 1; break; case SDL_MOUSEMOTION: if(selected) setpicker(selected, event->motion.x / ZOOM - PAD * 8, event->motion.y / ZOOM - PAD * 8); break; } } void dokey(SDL_Event *event) { int shift = SDL_GetModState() & KMOD_LSHIFT || SDL_GetModState() & KMOD_RSHIFT; int ctrl = SDL_GetModState() & KMOD_LCTRL || SDL_GetModState() & KMOD_RCTRL; if(ctrl) { switch(event->key.keysym.sym) { /* Generic */ case SDLK_s: shift ? saveicn(&doc, doc.name) : savedoc(&doc, doc.name); break; case SDLK_h: savemode(&GUIDES, !GUIDES); break; } } else { switch(event->key.keysym.sym) { case SDLK_SPACE: savemode(&COLORS, !COLORS); break; case SDLK_1: selected = &pickers[0]; break; case SDLK_2: selected = &pickers[1]; break; case SDLK_3: selected = &pickers[2]; break; case SDLK_4: selected = &pickers[3]; break; } } } int init(void) { if(SDL_Init(SDL_INIT_VIDEO) < 0) return error("Init", SDL_GetError()); gWindow = SDL_CreateWindow("Dito", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH * ZOOM, HEIGHT * ZOOM, SDL_WINDOW_SHOWN); if(gWindow == NULL) return error("Window", SDL_GetError()); gRenderer = SDL_CreateRenderer(gWindow, -1, 0); if(gRenderer == NULL) return error("Renderer", SDL_GetError()); gTexture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, WIDTH, HEIGHT); if(gTexture == NULL) return error("Texture", SDL_GetError()); pixels = (Uint32 *)malloc(WIDTH * HEIGHT * sizeof(Uint32)); if(pixels == NULL) return error("Pixels", "Failed to allocate memory"); clear(pixels); return 1; } int main(int argc, char **argv) { int ticknext = 0; if(argc < 2) return error("Load", "Missing bmp image."); if(!init()) return error("Init", "Failure"); image = SDL_LoadBMP(argv[1]); if(!image) return error("Load", "Invalid bmp image."); setpicker(&pickers[0], 66, 16); setpicker(&pickers[1], 46, 49); setpicker(&pickers[2], 140, 10); setpicker(&pickers[3], 120, 40); makedoc(&doc, "untitled.chr"); while(1) { int tick = SDL_GetTicks(); SDL_Event event; if(tick < ticknext) SDL_Delay(ticknext - tick); ticknext = tick + (1000 / FPS); while(SDL_PollEvent(&event) != 0) { switch(event.type) { case SDL_QUIT: quit(); break; case SDL_MOUSEBUTTONUP: case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEMOTION: domouse(&event); break; case SDL_KEYDOWN: dokey(&event); break; case SDL_WINDOWEVENT: if(event.window.event == SDL_WINDOWEVENT_EXPOSED) redraw(pixels); break; } } } quit(); return 0; }