Browse Source

Minor cleanup

main
neauoire 5 years ago
parent
commit
76471025d9
  1. 3
      .gitignore
  2. 3
      README.md
  3. 103
      nasu.c

3
.gitignore vendored

@ -4,5 +4,4 @@
*gif~
*bmp~
nasu
nasu-export.chr
nasu-render.bmp
untitled.chr

3
README.md

@ -39,4 +39,5 @@ To resume working on a tileset:
### Paint
- `mouse1` Paint
- `mouse1+mouse2` Erase
- `mouse2` Line
- `mouse1+mouse3` Erase

103
nasu.c

@ -45,15 +45,15 @@ Uint32 theme[] = {
0x222222};
Uint8 icons[][8] = {
{0x38, 0x7c, 0xfe, 0xfe, 0xfe, 0x7c, 0x38, 0x00},
{0x38, 0x44, 0x82, 0x82, 0x82, 0x44, 0x38, 0x00},
{0x02, 0x02, 0x04, 0x38, 0x40, 0x80, 0x80, 0x00},
{0x88, 0x00, 0x22, 0x00, 0x88, 0x00, 0x22, 0x00},
{0xaa, 0x54, 0xaa, 0x54, 0xaa, 0x54, 0xaa, 0x00},
{0x38, 0x7c, 0xee, 0xd6, 0xee, 0x7c, 0x38, 0x00},
{0x44, 0xba, 0x44, 0x44, 0x44, 0xba, 0x44, 0x00},
{0xaa, 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa, 0x00},
{0xee, 0xaa, 0xee, 0x00, 0xee, 0xaa, 0xee, 0x00},
{0x38, 0x44, 0x82, 0x82, 0x82, 0x44, 0x38, 0x00}, /* color:blank */
{0x38, 0x7c, 0xfe, 0xfe, 0xfe, 0x7c, 0x38, 0x00}, /* color:full */
{0x02, 0x02, 0x04, 0x38, 0x40, 0x80, 0x80, 0x00}, /* brush:line */
{0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0x00}, /* brush:pattern0 */
{0xaa, 0x54, 0xaa, 0x54, 0xaa, 0x54, 0xaa, 0x00}, /* brush:pattern2 */
{0x88, 0x00, 0x22, 0x00, 0x88, 0x00, 0x22, 0x00}, /* brush:pattern4 */
{0x44, 0xba, 0x44, 0x44, 0x44, 0xba, 0x44, 0x00}, /* brush:cleanup */
{0xaa, 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa, 0x00}, /* view:grid */
{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 */
};
@ -65,6 +65,12 @@ Uint32 *pixels;
/* helpers */
int
clamp(int val, int min, int max)
{
return (val >= min) ? (val <= max) ? val : max : min;
}
char *
scpy(char *src, char *dst, int len)
{
@ -122,9 +128,7 @@ void
putchr(int x, int y, int color)
{
int row = rowchr(x, y), col = x % 8;
if(x < 0 || x >= HOR * 8)
return;
if(y < 0 || y >= VER * 8)
if(x < 0 || x >= HOR * 8 || y < 0 || y >= VER * 8)
return;
if(color == 0 || color == 2)
doc.data[row] &= ~(1UL << (7 - col));
@ -158,11 +162,11 @@ jagg(int x, int y)
int
patt(int x, int y, int mode)
{
if(mode == 1)
if(mode == 3)
return ((x + y) % 4) == 0 && ((y - x) % 4) == 0;
if(mode == 2)
return ((x + y) % 2) == 0 && ((y - x) % 2) == 0;
if(mode == 3)
if(mode == 1)
return 1;
return 0;
}
@ -263,7 +267,7 @@ drawicon(Uint32 *dst, int x, int y, Uint8 *icon, int fg, int bg)
void
drawui(Uint32 *dst)
{
int bottom = VER * 8 + 8;
int bottom = HEIGHT - 40;
drawicon(dst, 0, bottom, brush.color == 1 ? icons[1] : icons[0], 1, 0);
drawicon(dst, 8, bottom, brush.color == 2 ? icons[1] : icons[0], 2, 0);
drawicon(dst, 16, bottom, brush.color == 3 ? icons[1] : icons[0], 3, 0);
@ -312,37 +316,30 @@ setmode(int *i, int v)
}
void
setrange(int *i, int v, int min, int max)
{
*i = v < min ? min : v > max ? max
: v;
redraw(pixels);
}
void
newchr(void)
makedoc(Document *d, char *name)
{
int i;
for(i = 0; i < SZ; ++i)
doc.data[i] = 0x00;
scpy("untitled.chr", doc.name, 256);
printf("New: %s\n", doc.name);
d->data[i] = 0x00;
scpy(name, d->name, 256);
printf("Made: %s\n", d->name);
redraw(pixels);
}
int
savechr(void)
savedoc(Document *d, char *name)
{
FILE *f = fopen(doc.name, "w");
if(!fwrite(doc.data, sizeof(doc.data), 1, f))
return error("Export", "Failure");
FILE *f = fopen(name, "w");
if(!fwrite(d->data, sizeof(d->data), 1, f))
return error("Save", "Failure");
scpy(name, d->name, 256);
fclose(f);
printf("Save: %s\n", doc.name);
printf("Saved: %s\n", d->name);
return 1;
}
int
openchr(char *name)
opendoc(Document *d, char *name)
{
FILE *f = fopen(name, "r");
if(!f)
@ -357,7 +354,7 @@ openchr(char *name)
}
int
renderbmp(void)
savebmp(void)
{
SDL_Surface *surface = SDL_GetWindowSurface(gWindow);
SDL_RenderReadPixels(gRenderer,
@ -422,25 +419,21 @@ domouse(SDL_Event *event)
brush.down = 1;
if(event->button.button == SDL_BUTTON_RIGHT)
brush.erase = 1;
if(event->button.button == SDL_BUTTON_MIDDLE) {
brush.erase = 0;
if(brush.px != 0 && brush.py != 0) {
brush.x = screenpos(event->motion.x, brush.vx);
brush.y = screenpos(event->motion.y, brush.vy);
line(brush.px, brush.py, brush.x, brush.y, brush.erase ? 0 : brush.color);
redraw(pixels);
}
}
if(event->button.button == SDL_BUTTON_MIDDLE)
line(brush.px, brush.py, screenpos(event->motion.x, brush.vx), screenpos(event->motion.y, brush.vy), brush.erase ? 0 : brush.color);
brush.px = screenpos(event->motion.x, brush.vx);
brush.py = screenpos(event->motion.y, brush.vy);
if(!BIGPIXEL) {
brush.vx = (brush.px / 8) * 8;
brush.vy = (brush.py / 8) * 8;
brush.vx = clamp((brush.px / 8) * 8, 0, WIDTH - HOR - 2 * PAD * 8);
brush.vy = clamp((brush.py / 8) * 8, 0, HEIGHT - VER - 3 * PAD * 8);
}
if(brush.down && brush.mode == 0) {
putchr(brush.px, brush.py, brush.erase ? 0 : brush.color);
redraw(pixels);
if(brush.down) {
if(brush.mode == 0)
putchr(brush.px, brush.py, brush.erase ? 0 : brush.color);
else
fill(brush.px, brush.py, brush.mode, brush.size, brush.erase ? 0 : brush.color);
}
redraw(pixels);
break;
case SDL_MOUSEMOTION:
if(brush.down) {
@ -466,9 +459,9 @@ dokey(SDL_Event *event)
if(ctrl) {
switch(event->key.keysym.sym) {
/* Generic */
case SDLK_n: newchr(); break;
case SDLK_r: openchr(doc.name); break;
case SDLK_s: shift ? renderbmp() : savechr(); break;
case SDLK_n: makedoc(&doc, "untitled.chr"); break;
case SDLK_r: opendoc(&doc, doc.name); break;
case SDLK_s: shift ? savebmp() : savedoc(&doc, doc.name); break;
case SDLK_h: setmode(&GUIDES, !GUIDES); break;
}
} else {
@ -483,8 +476,8 @@ dokey(SDL_Event *event)
case SDLK_f: setmode(&brush.mode, 3); break;
case SDLK_g: setmode(&brush.mode, 4); break;
case SDLK_b: setmode(&BIGPIXEL, !BIGPIXEL); break;
case SDLK_z: setrange(&brush.size, brush.size - 1, 1, 30); break;
case SDLK_x: setrange(&brush.size, brush.size + 1, 1, 30); break;
case SDLK_z: setmode(&brush.size, brush.size + (brush.size > 1 ? -1 : 0)); break;
case SDLK_x: setmode(&brush.size, brush.size + (brush.size < 30 ? 1 : 0)); break;
}
}
}
@ -527,8 +520,8 @@ main(int argc, char **argv)
brush.size = 10;
if(!init())
return error("Init", "Failure");
if(argc < 2 || !openchr(argv[1]))
newchr();
if(argc < 2 || !opendoc(&doc, argv[1]))
makedoc(&doc, "untitled.chr");
redraw(pixels);
while(1) {
int tick = SDL_GetTicks();

Loading…
Cancel
Save