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

Loading…
Cancel
Save