Browse Source

Clicking adds a pixel

main
neauoire 5 years ago
parent
commit
9e1ce2e86f
  1. 64
      nasu.c

64
nasu.c

@ -14,14 +14,9 @@
#define SZ (HOR * VER * 16)
typedef struct Brush {
int x, y;
int px, py;
int mode;
int size;
int color;
int down;
int edit;
int erase;
int x, y, px, py;
int mode, size, color;
int down, edit, erase;
} Brush;
char *modes[] = {
@ -34,7 +29,6 @@ char *modes[] = {
"exes",
"fixe"};
unsigned char chrbuf[SZ];
int colors[] = {color1, color2, color3, color4, color0};
int WIDTH = 8 * HOR + PAD * 2;
int HEIGHT = 8 * VER + PAD * 2;
@ -43,7 +37,8 @@ int GUIDES = 0;
SDL_Window *gWindow = NULL;
SDL_Renderer *gRenderer = NULL;
SDL_Texture *gTexture = NULL;
uint32_t *pixels;
unsigned char chrbuf[SZ];
Uint32 *pixels;
/* helpers */
@ -200,23 +195,30 @@ line(int ax, int ay, int bx, int by, int color)
/* draw */
void
draw(uint32_t *dst)
drawtile(Uint32 *dst, int x, int y, int id)
{
int b, i, j, id = 0;
for(b = 0; b < SZ; b += 16)
for(i = 0; i < 8; i++)
for(j = 7; j >= 0; j--) {
int ch1 = chrbuf[b + i];
int ch2 = chrbuf[b + i + 8];
int color = ((ch1 >> j) & 0x1) + (((ch2 >> j) & 0x1) << 1);
int ti = id / 64;
int odd = (ti + (ti / HOR + 2)) % 2 == 0;
int px = (ti / (HOR * VER)) * (8 * HOR) + (ti % HOR) * 8 + (id % 8);
int py = ((ti / HOR) * 8) + ((id % 64) / 8);
dst[(py + PAD) * WIDTH + (px + PAD)] = colors[GUIDES && odd && color == 0 ? 4 : color];
id++;
}
SDL_UpdateTexture(gTexture, NULL, dst, WIDTH * sizeof(uint32_t));
int v, h, offset = id * 16, sz = WIDTH * HEIGHT;
for(v = 0; v < 8; v++)
for(h = 0; h < 8; h++) {
int px = (x * 8) + (8 - h);
int py = (y * 8) + v;
int ch1 = chrbuf[offset + v];
int ch2 = chrbuf[offset + v + 8];
int clr = ((ch1 >> h) & 0x1) + (((ch2 >> h) & 0x1) << 1);
int key = (py + PAD) * WIDTH + (px + PAD);
if(key >= 0 && key <= sz - 1)
dst[key] = colors[clr];
}
}
void
draw(Uint32 *dst)
{
int x, y;
for(y = 0; y < VER; ++y)
for(x = 0; x < HOR; ++x)
drawtile(dst, x, y, x + y * HOR);
SDL_UpdateTexture(gTexture, NULL, dst, WIDTH * sizeof(Uint32));
SDL_RenderClear(gRenderer);
SDL_RenderCopy(gRenderer, gTexture, NULL, NULL);
SDL_RenderPresent(gRenderer);
@ -329,8 +331,9 @@ domouse(SDL_Event *event, Brush *b)
update(b);
break;
case SDL_MOUSEBUTTONDOWN:
if(event->button.button == SDL_BUTTON_LEFT)
if(event->button.button == SDL_BUTTON_LEFT) {
b->down = 1;
}
if(event->button.button == SDL_BUTTON_RIGHT)
b->erase = 1;
if(event->button.button == SDL_BUTTON_MIDDLE) {
@ -344,6 +347,11 @@ domouse(SDL_Event *event, Brush *b)
}
b->px = (event->motion.x - (PAD * ZOOM)) / ZOOM;
b->py = (event->motion.y - (PAD * ZOOM)) / ZOOM;
if(b->down) {
putchr(b->px, b->py, b->color);
draw(pixels);
}
break;
case SDL_MOUSEMOTION:
if(b->down) {
b->x = (event->motion.x - (PAD * ZOOM)) / ZOOM;
@ -420,7 +428,7 @@ init(void)
HEIGHT);
if(gTexture == NULL)
return error("Texture", SDL_GetError());
pixels = (uint32_t *)malloc(WIDTH * HEIGHT * sizeof(uint32_t));
pixels = (Uint32 *)malloc(WIDTH * HEIGHT * sizeof(Uint32));
if(pixels == NULL)
return error("Pixels", "Failed to allocate memory");
for(i = 0; i < HEIGHT; i++)

Loading…
Cancel
Save