@@ 15,11 15,6 @@ WITH REGARD TO THIS SOFTWARE.
#define HOR 32
#define VER 16
#define PAD 8
-#define color1 0x000000
-#define color2 0x72DEC2
-#define color3 0xFFFFFF
-#define color4 0x444444
-#define color0 0x111111
#define SZ (HOR * VER * 16)
@@ 29,7 24,7 @@ typedef struct Brush {
int down, erase;
} Brush;
-int colors[] = {color1, color2, color3, color4, color0};
+int theme[] = {0x000000, 0x72DEC2, 0xFFFFFF, 0x444444, 0x111111};
int WIDTH = 8 * HOR + PAD * 2;
int HEIGHT = 8 * VER + PAD * 2;
int FPS = 30, GUIDES = 1, ZOOM = 2;
@@ 66,6 61,25 @@ spos(char *s, char *ss)
return -1;
}
+unsigned char
+chex(char c)
+{
+ if(c >= 'a' && c <= 'f')
+ return 10 + c - 'a';
+ if(c >= 'A' && c <= 'F')
+ return 10 + c - 'A';
+ return (c - '0') & 0xF;
+}
+
+int
+shex(char *s, int len)
+{
+ int i, n = 0;
+ for(i = 0; i < len; ++i)
+ n |= (chex(s[i]) << ((len - i - 1) * 4));
+ return n;
+}
+
int
getclr(int r, int g, int b)
{
@@ 200,6 214,15 @@ line(int ax, int ay, int bx, int by, int color)
/* draw */
void
+clear(Uint32 *dst)
+{
+ int i, j;
+ for(i = 0; i < HEIGHT; i++)
+ for(j = 0; j < WIDTH; j++)
+ dst[i * WIDTH + j] = theme[0];
+}
+
+void
drawchr(Uint32 *dst, int x, int y, int id)
{
int v, h, offset = id * 16;
@@ 212,14 235,15 @@ drawchr(Uint32 *dst, int x, int y, int id)
int clr = ((ch1 >> h) & 0x1) + (((ch2 >> h) & 0x1) << 1);
int key = (py + PAD) * WIDTH + (px + PAD);
int guides = GUIDES && !clr && (x + y) % 2;
- dst[key] = colors[guides ? 4 : clr];
+ dst[key] = theme[guides ? 4 : clr];
}
}
void
-draw(Uint32 *dst)
+redraw(Uint32 *dst)
{
int x, y;
+ clear(dst);
for(y = 0; y < VER; ++y)
for(x = 0; x < HOR; ++x)
drawchr(dst, x, y, x + y * HOR);
@@ 241,10 265,10 @@ error(char *msg, const char *err)
void
modzoom(int mod)
{
- if((mod > 0 && ZOOM < 4) || (mod < 0 && ZOOM > 1))
+ if((mod > 0 && ZOOM < 4) || (mod < 0 && ZOOM > 1)) {
ZOOM += mod;
- SDL_SetWindowSize(gWindow, WIDTH * ZOOM, HEIGHT * ZOOM);
- draw(pixels);
+ SDL_SetWindowSize(gWindow, WIDTH * ZOOM, HEIGHT * ZOOM);
+ }
}
void
@@ 274,7 298,7 @@ void
toggleguide(void)
{
GUIDES = !GUIDES;
- draw(pixels);
+ redraw(pixels);
printf("%s Guides\n", GUIDES ? "Show" : "Hide");
}
@@ 282,7 306,7 @@ void
destroy(void)
{
newchr();
- draw(pixels);
+ redraw(pixels);
puts("Destroy");
}
@@ 301,7 325,7 @@ renderbmp(void)
{
SDL_Surface *surface = SDL_GetWindowSurface(gWindow);
GUIDES = 0;
- draw(pixels);
+ redraw(pixels);
SDL_RenderReadPixels(gRenderer,
NULL,
SDL_PIXELFORMAT_ARGB8888,
@@ 342,6 366,26 @@ loadbmp(char *path)
}
void
+loadtheme(FILE *f)
+{
+ int id = 0;
+ char line[256];
+ if(!f)
+ return;
+ while(fgets(line, 256, f)) {
+ int i = 0;
+ while(line[i++]) {
+ if(line[i] != '#')
+ continue;
+ if(id == 0 || id > 4)
+ theme[id > 4 ? id - 4 : 0] = shex(line + i + 1, 6);
+ id++;
+ }
+ }
+ fclose(f);
+}
+
+void
quit(void)
{
free(pixels);
@@ 376,14 420,14 @@ domouse(SDL_Event *event, Brush *b)
b->x = (event->motion.x - (PAD * ZOOM)) / ZOOM;
b->y = (event->motion.y - (PAD * ZOOM)) / ZOOM;
line(b->px - 1, b->py, b->x, b->y, b->erase ? 0 : b->color);
- draw(pixels);
+ redraw(pixels);
}
}
b->px = (event->motion.x - (PAD * ZOOM)) / ZOOM;
b->py = (event->motion.y - (PAD * ZOOM)) / ZOOM;
if(b->down) {
putchr(b->px - 1, b->py, b->erase ? 0 : b->color);
- draw(pixels);
+ redraw(pixels);
}
break;
case SDL_MOUSEMOTION:
@@ 394,7 438,7 @@ domouse(SDL_Event *event, Brush *b)
line(b->px - 1, b->py, b->x - 1, b->y, b->erase ? 0 : b->color);
else
fill(b->x - 1, b->y, b->mode, b->size, b->erase ? 0 : b->color);
- draw(pixels);
+ redraw(pixels);
b->px = b->x;
b->py = b->y;
}
@@ 434,7 478,6 @@ dokey(SDL_Event *event, Brush *b)
int
init(void)
{
- int i, j;
if(SDL_Init(SDL_INIT_VIDEO) < 0)
return error("Init", SDL_GetError());
gWindow = SDL_CreateWindow("Nasu",
@@ 458,9 501,7 @@ init(void)
pixels = (Uint32 *)malloc(WIDTH * HEIGHT * sizeof(Uint32));
if(pixels == NULL)
return error("Pixels", "Failed to allocate memory");
- for(i = 0; i < HEIGHT; i++)
- for(j = 0; j < WIDTH; j++)
- pixels[i * WIDTH + j] = color1;
+ clear(pixels);
return 1;
}
@@ 478,6 519,8 @@ main(int argc, char **argv)
if(!init())
return error("Init", "Failure");
+ loadtheme(fopen("theme.svg", "r"));
+
if(argc > 1 && spos(argv[1], ".bmp") > -1)
loadbmp(argv[1]);
else if(argc > 1 && spos(argv[1], ".chr") > -1)
@@ 485,7 528,7 @@ main(int argc, char **argv)
else
newchr();
- draw(pixels);
+ redraw(pixels);
while(1) {
int tick = SDL_GetTicks();
@@ 504,7 547,7 @@ main(int argc, char **argv)
dokey(&event, &brush);
else if(event.type == SDL_WINDOWEVENT)
if(event.window.event == SDL_WINDOWEVENT_EXPOSED)
- draw(pixels);
+ redraw(pixels);
}
}
quit();