This commit is contained in:
neauoire 2021-01-04 17:26:36 -08:00
commit 3eedc8228f
7 changed files with 410 additions and 0 deletions

19
.clang-format Normal file
View File

@ -0,0 +1,19 @@
AlignAfterOpenBracket: DontAlign
AlignEscapedNewlines: DontAlign
AllowShortBlocksOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: true
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: TopLevel
BinPackArguments: false
BinPackParameters: false
BreakBeforeBraces: WebKit
IndentCaseLabels: false
TabWidth: 4
IndentWidth: 4
ContinuationIndentWidth: 4
UseTab: ForContinuationAndIndentation
ColumnLimit: 0
ReflowComments: false
SortIncludes: false
SpaceBeforeParens: false

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
.DS*
*jpg~
*png~
*gif~
theme.svg
left

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) Devine Lu Linvega
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

36
README.md Normal file
View File

@ -0,0 +1,36 @@
# Left
A minimal text editor, written in ANSI C.
## Build
To build left, you must have [SDL2](https://wiki.libsdl.org/).
```
cc left.c -std=c89 -Os -DNDEBUG -g0 -s -Wall -L/usr/local/lib -lSDL2 -o left
```
## Controls
### Generics
- `+` Zoom In
- `-` Zoom Out
### IO
- `E` Export(export.chr)
- `R` Render(render.bmp)
- `Z` Length Out
- `X` Length In
### General
- `WASD` Orbit(rotate)
- `Shift+WASD` Orbit(move)
- `Shift+ZX` Orbit(zoom)
- `TAB` Toggle projection
## TODOs
- Add lathe effect.

26
build.sh Executable file
View File

@ -0,0 +1,26 @@
#!/bin/bash
# format code
clang-format -i left.c
# remove old
rm -f ./left
# debug(slow)
cc -std=c99 -DDEBUG -Wall -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined left.c -L/usr/local/lib -lSDL2 -lm -o left
# build(fast)
# cc left.c -std=c89 -Os -DNDEBUG -g0 -s -Wall -L/usr/local/lib -lSDL2 -lm -o left
# Size
echo "Size: $(du -sk ./left)"
# Install
if [ -d "$HOME/bin" ] && [ -e ./left ]
then
cp ./left $HOME/bin
echo "Installed: $HOME/bin"
fi
# run
./left

302
left.c Normal file
View File

@ -0,0 +1,302 @@
#include <SDL2/SDL.h>
#include <stdio.h>
/*
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 8
#define SZ (HOR * VER * 16)
int WIDTH = 8 * HOR + PAD * 2;
int HEIGHT = 8 * (VER + 2) + PAD * 2;
int FPS = 30, GUIDES = 1, ZOOM = 2;
/* interface */
Uint32 theme[] = {
0x000000,
0xFFFFFF,
0x72DEC2,
0x666666,
0x222222};
Uint8 icons[][8] = {
{0x10, 0x28, 0x44, 0x92, 0x44, 0x28, 0x10, 0x00},
{0x10, 0x28, 0x44, 0x82, 0x82, 0x82, 0xd6, 0x00},
{0x1e, 0x22, 0x40, 0x82, 0x40, 0x22, 0x1e, 0x00},
{0x04, 0x08, 0x50, 0xa4, 0x50, 0x08, 0x04, 0x00},
{0x1e, 0x20, 0x40, 0xa2, 0x40, 0x20, 0x1e, 0x00},
{0x00, 0x00, 0x00, 0x82, 0x44, 0x38, 0x00, 0x00}, /* eye open */
{0x00, 0x38, 0x44, 0x92, 0x28, 0x10, 0x00, 0x00} /* eye closed */
};
Uint8 chrbuf[SZ];
SDL_Window *gWindow = NULL;
SDL_Renderer *gRenderer = NULL;
SDL_Texture *gTexture = NULL;
Uint32 *pixels;
int text_len = 0;
char text[300];
/* 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];
}
int
getpixel(Uint32 *dst, int x, int y)
{
return dst[(y + PAD) * WIDTH + (x + PAD)];
}
void
putpixel(Uint32 *dst, int x, int y, int color)
{
if(x >= 0 && x < WIDTH - 8 && y >= 0 && y < HEIGHT - 8)
dst[(y + PAD) * WIDTH + (x + PAD)] = theme[color];
}
void
drawchr(Uint32 *dst, int x, int y, int id)
{
int v, h, offset = id * 16;
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 guides = GUIDES && !clr && (x + y) % 2;
putpixel(dst, px, py, guides ? 4 : clr);
}
}
void
drawicon(Uint32 *dst, int x, int y, Uint8 *icon, int color)
{
int v, h;
for(v = 0; v < 8; v++)
for(h = 0; h < 8; h++) {
int c = (icon[v] >> (8 - h)) & 0x1;
putpixel(dst, x + h, y + v, c ? color : 0);
}
}
void
drawletter(Uint32 *dst, int x, int y, int id)
{
int v, h, offset = id * 16;
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);
putpixel(dst, px, py, clr);
}
}
void
drawui(Uint32 *dst)
{
int bottom = VER * 8 + 8;
drawicon(dst, 0 * 8, bottom, icons[0], 2);
drawicon(dst, 1 * 8, bottom, icons[1], 2);
drawicon(dst, 2 * 8, bottom, icons[2], 2);
drawicon(dst, 4 * 8, bottom, icons[3], 1);
drawicon(dst, 5 * 8, bottom, icons[GUIDES ? 6 : 5], GUIDES ? 1 : 2);
}
void
drawbody(Uint32 *dst)
{
int i = 0;
char c;
while((c = text[i++])) {
drawletter(dst, i % 24, i / 24, (int)c);
printf("%c\n", c);
}
}
void
redraw(Uint32 *dst)
{
int i, j;
clear(dst);
drawbody(dst);
drawui(dst);
SDL_UpdateTexture(gTexture, NULL, dst, WIDTH * sizeof(Uint32));
SDL_RenderClear(gRenderer);
SDL_RenderCopy(gRenderer, gTexture, NULL, NULL);
SDL_RenderPresent(gRenderer);
}
/* options */
int
error(char *msg, const char *err)
{
printf("Error %s: %s\n", msg, err);
return 0;
}
void
setguides(int v)
{
GUIDES = v;
redraw(pixels);
}
void
selectoption(int option)
{
switch(option) {
case 5: setguides(!GUIDES); 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);
}
void
domouse(SDL_Event *event)
{
switch(event->type) {
case SDL_MOUSEBUTTONUP:
break;
case SDL_MOUSEBUTTONDOWN:
if(event->motion.y / ZOOM / 8 == VER + 2)
selectoption(event->motion.x / ZOOM / 8 - 1);
break;
case SDL_MOUSEMOTION:
break;
}
}
void
dokey(SDL_Event *event)
{
int shift = SDL_GetModState() & KMOD_LSHIFT || SDL_GetModState() & KMOD_RSHIFT;
switch(event->key.keysym.sym) {
}
redraw(pixels);
}
void
dotext(SDL_Event *event)
{
int i;
for(i = 0; i < SDL_TEXTINPUTEVENT_TEXT_SIZE; ++i) {
char c = event->text.text[i];
if(c < ' ' || c > '~')
break;
printf("%c\n", c);
text[text_len++] = c;
text[text_len] = '\0';
}
}
int
loadchr(FILE *f)
{
if(!f)
return error("Load", "Invalid input file");
if(!fread(chrbuf, sizeof(chrbuf), 1, f))
return error("Load", "Invalid input size");
puts("Load: Complete");
fclose(f);
return 1;
}
int
init(void)
{
if(SDL_Init(SDL_INIT_VIDEO) < 0)
return error("Init", SDL_GetError());
gWindow = SDL_CreateWindow("Moogle",
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(void)
{
int ticknext = 0;
if(!init())
return error("Init", "Failure");
loadchr(fopen("nasu-export.chr", "r"));
redraw(pixels);
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_TEXTINPUT: dotext(&event); break;
case SDL_KEYDOWN: dokey(&event); break;
case SDL_WINDOWEVENT:
if(event.window.event == SDL_WINDOWEVENT_EXPOSED)
redraw(pixels);
break;
}
}
}
quit();
return 0;
}

BIN
nasu-export.chr Normal file

Binary file not shown.