Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added some control additions in the editor, filled(), and polygon() #58

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 17 additions & 1 deletion README.md
Expand Up @@ -50,14 +50,30 @@ PROGRAMMING INTERFACE
Drawing functions:

* fill(r,g,b,alpha): select the drawing color.
* filled(filled): set the filled state (true or false)
* background(r,g,b): paint the whole background with the specified color.
* rect(x,y,width,height): draw a rectangle at x,y (left-bottom corner).
* ellipse(x,y,width,height): draw an ellipse centered at x,y.
* line(x1,y1,x2,y2): draw a line from x1,y1 to x2,y2.
* text(x,y,string): print the specified text at x,y using a bitmap font.
* triangle(x1,y1,x2,y2,x3,y3): draw a triangle with the specified vertex.
* getpixel(x,y): return the red,gree,blue value of the specified pixel.
* sprite(file,x,y,[rotation],[antialiasing]): draw sprite at coordinates with the specified rotation (in degrees, default 0) and antialiasing (default false).
* polygon(xv, yv): draw a polygon using a table of X values and a table of Y values.

Sprite functions:

* sprite(file,[x,y,[rotation],[antialiasing]]): draw sprite at coordinates with the specified rotation (in degrees, default 0) and antialiasing (default false).

Returns a sprite userdata object, with the following functions

* getHeight(): returns the height of the sprite.
* getWidth(): returns the height of the sprite.
* getTiles(): returns x,y for the number of tiles horizontally and vertically.
* setTiles(x,y): set the number of tiles horizontally and vertically.
* getTileSize(): return w,h for the size of a tile, calculated from the width and height of the image divided by the number of tiles horizontally and vertically.
* getTileNum(): returns the number of tiles.
* tile(x,y,tileNum,[rotation],[antialiasing]): draw a tile using tileNum at coordinates with the specified rotation (in degrees, default 0) and antialiasing (default: false).
* draw(x,y,[rotation],[antialiasing]): draw sprite at coordinates with the specified rotation (in degrees, default 0) and antialiasing (default: false).

Control functions:

Expand Down
178 changes: 149 additions & 29 deletions editor.c
Expand Up @@ -374,7 +374,7 @@ void editorDrawCursor(void) {
y -= E.margin_top;
if (!(E.cblink & 0x80)) drawBox(E.fb,x+charmargin,y,
x+charmargin+FONT_KERNING-1,y+FONT_HEIGHT-1,
165,165,255,128);
165,165,255,128,1);
E.cblink += 4;
}

Expand Down Expand Up @@ -412,25 +412,25 @@ void editorDrawChars(void) {
}

void editorDrawPowerOff(int x, int y) {
drawEllipse(E.fb,x,y,12,12,66,66,231,255);
drawEllipse(E.fb,x,y,7,7,165,165,255,255);
drawBox(E.fb,x-4,y,x+4,y+12,165,165,255,255);
drawBox(E.fb,x-2,y,x+2,y+14,66,66,231,255);
drawEllipse(E.fb,x,y,12,12,66,66,231,255,1);
drawEllipse(E.fb,x,y,7,7,165,165,255,255,1);
drawBox(E.fb,x-4,y,x+4,y+12,165,165,255,255,1);
drawBox(E.fb,x-2,y,x+2,y+14,66,66,231,255,1);
}

void editorDrawSaveIcon(int x, int y) {
drawBox(E.fb,x-12,y-12,x+12,y+12,66,66,231,255);
drawBox(E.fb,x-1,y+7,x+1,y+11,165,165,255,255);
drawEllipse(E.fb,x,y,4,4,165,165,255,255);
drawBox(E.fb,x-12,y-12,x+12,y+12,66,66,231,255,1);
drawBox(E.fb,x-1,y+7,x+1,y+11,165,165,255,255,1);
drawEllipse(E.fb,x,y,4,4,165,165,255,255,1);
}

void editorDraw() {
drawBox(E.fb,0,0,E.fb->width-1,E.fb->height-1,165,165,255,255);
drawBox(E.fb,0,0,E.fb->width-1,E.fb->height-1,165,165,255,255,1);
drawBox(E.fb,
E.margin_left,
E.margin_bottom,
E.fb->width-1-E.margin_right,
E.fb->height-1-E.margin_top,66,66,231,255);
E.fb->height-1-E.margin_top,66,66,231,255,1);
editorDrawChars();
editorDrawCursor();
/* Show buttons */
Expand Down Expand Up @@ -473,24 +473,38 @@ void editorMouseClicked(int x, int y, int button) {
} else if (x >= E.margin_left && x <= E.fb->width-1-E.margin_right &&
y >= E.margin_bottom && y <= E.fb->height-1-E.margin_top)
{
int realheight = E.fb->height - E.margin_top - E.margin_bottom;
int realy = y - E.margin_bottom;
int row = (realheight-realy)/FONT_HEIGHT;
int col = (x-E.margin_left)/FONT_KERNING;
int filerow = E.rowoff+row;
int filecol = E.coloff+col;
erow *r = (filerow >= E.numrows) ? NULL : &E.row[filerow];

E.cblink = 0;
if (filerow == E.numrows) {
E.cx = 0;
E.cy = filerow-E.rowoff;
} else if (r) {
if (filecol >= r->size)
E.cx = r->size-E.coloff;
else
E.cx = filecol-E.coloff;
E.cy = filerow-E.rowoff;
if (button == 4) {
if (E.rowoff) {
E.rowoff--;
if (E.cy < E.screenrows - 1) E.cy++;
}
}
else if (button == 5) {
if (E.rowoff + E.screenrows < E.numrows) {
E.rowoff++;
if (E.cy > 0) E.cy--;
}
}
else {
int realheight = E.fb->height - E.margin_top - E.margin_bottom;
int realy = y - E.margin_bottom;
int row = (realheight-realy)/FONT_HEIGHT;
int col = (x-E.margin_left)/FONT_KERNING;
int filerow = E.rowoff+row;
int filecol = E.coloff+col;
erow *r = (filerow >= E.numrows) ? NULL : &E.row[filerow];

E.cblink = 0;
if (filerow == E.numrows) {
E.cx = 0;
E.cy = filerow-E.rowoff;
} else if (r) {
if (filecol >= r->size)
E.cx = r->size-E.coloff;
else
E.cx = filecol-E.coloff;
E.cy = filerow-E.rowoff;
}
}
}
}
Expand All @@ -500,6 +514,7 @@ void editorMoveCursor(int key) {
int filecol = E.coloff+E.cx;
int rowlen;
erow *row = (filerow >= E.numrows) ? NULL : &E.row[filerow];
int temp;

switch(key) {
case SDLK_LEFT:
Expand Down Expand Up @@ -534,6 +549,61 @@ void editorMoveCursor(int key) {
}
}
break;
case SDLK_PAGEUP:
if (E.rowoff) {
E.rowoff -= E.screenrows - 1;
if (E.rowoff < 0) {
E.rowoff = 0;
E.cy = 0;
}
}
else {
if (E.cy > 0) E.cy = 0;
}
break;
case SDLK_PAGEDOWN:
if (E.rowoff + E.screenrows - 1 < E.numrows) {
E.rowoff += E.screenrows - 1;
if (E.rowoff + E.screenrows - 1 > E.numrows) E.cy = E.numrows - E.rowoff - 1;
}
else {
E.cy = E.numrows - E.rowoff - 1;
}
break;
case SDLK_HOME:
if (E.modifiers & CTRL_MASK) {
E.rowoff = E.coloff = E.cy = E.cx = 0;
}
else {
if (row && filecol != 0) {
temp = getFirstNonSpace(row);
if (temp > -1) {
if (filecol > temp) {
E.cx = temp;
E.coloff = 0;
}
else {
E.cx = E.coloff = 0;
}
}
}
}
break;
case SDLK_END:
if (E.modifiers & CTRL_MASK) {
E.rowoff = E.numrows - E.screenrows;
E.cy = E.screenrows - 1;
E.coloff = E.cx = 0;
}
else {
if (row && filecol < row->size) {
if (row->size - E.screencols + 1 > 0) {
E.coloff = row->size - E.screencols + 1;
}
E.cx = row->size - E.coloff;
}
}
break;
}
/* Fix cx if the current line has not enough chars. */
filerow = E.rowoff+E.cy;
Expand All @@ -549,6 +619,16 @@ void editorMoveCursor(int key) {
}
}

int getFirstNonSpace(erow *row) {
int i;
for (i = 0; i < row->size; i++) {
if (row->chars[i] != ' ' && row->chars[i] != '\t') {
return i;
}
}
return -1;
}

int editorEvents(void) {
SDL_Event event;
int j, ksym;
Expand Down Expand Up @@ -578,6 +658,24 @@ int editorEvents(void) {
E.key[ksym].counter = 1;
E.key[ksym].translation = (event.key.keysym.unicode & 0xff);
}
switch(ksym) {
case SDLK_LSHIFT:
case SDLK_RSHIFT:
E.modifiers |= SHIFT_MASK;
break;
case SDLK_LCTRL:
case SDLK_RCTRL:
E.modifiers |= CTRL_MASK;
break;
case SDLK_LALT:
case SDLK_RALT:
E.modifiers |= ALT_MASK;
break;
case SDLK_LMETA:
case SDLK_RMETA:
E.modifiers |= META_MASK;
break;
}
break;
}
break;
Expand All @@ -586,6 +684,24 @@ int editorEvents(void) {
case SDL_KEYUP:
ksym = event.key.keysym.sym;
if (ksym >= 0 && ksym < KEY_MAX) E.key[ksym].counter = 0;
switch(ksym) {
case SDLK_LSHIFT:
case SDLK_RSHIFT:
E.modifiers &= ~SHIFT_MASK;
break;
case SDLK_LCTRL:
case SDLK_RCTRL:
E.modifiers &= ~CTRL_MASK;
break;
case SDLK_LALT:
case SDLK_RALT:
E.modifiers &= ~ALT_MASK;
break;
case SDLK_LMETA:
case SDLK_RMETA:
E.modifiers &= ~META_MASK;
break;
}
break;
/* Mouse click */
case SDL_MOUSEBUTTONDOWN:
Expand All @@ -610,6 +726,10 @@ int editorEvents(void) {
case SDLK_RIGHT:
case SDLK_UP:
case SDLK_DOWN:
case SDLK_PAGEUP:
case SDLK_PAGEDOWN:
case SDLK_HOME:
case SDLK_END:
editorMoveCursor(j);
break;
case SDLK_BACKSPACE:
Expand All @@ -618,7 +738,6 @@ int editorEvents(void) {
case SDLK_RETURN:
editorInsertNewline();
break;
case SDLK_HOME:
case SDLK_LSHIFT:
case SDLK_RSHIFT:
case SDLK_LCTRL:
Expand Down Expand Up @@ -699,4 +818,5 @@ void initEditor(frameBuffer *fb, int mt, int mb, int ml, int mr) {
E.dirty = 0;
E.filename = NULL;
memset(E.key,0,sizeof(E.key));
E.modifiers = 0;
}
9 changes: 9 additions & 0 deletions editor.h
Expand Up @@ -28,6 +28,12 @@
#define HL_FUNCDEF_COLOR {255,255,255}
#define HL_LIB_COLOR {255,0,255}

/* Key Held Modifier Bit Masks */
#define CTRL_MASK (1<<0)
#define SHIFT_MASK (1<<1)
#define ALT_MASK (1<<2)
#define META_MASK (1<<3)

typedef struct erow {
int size; /* Size of the row, excluding the null term. */
char *chars; /* Row content. */
Expand Down Expand Up @@ -56,6 +62,7 @@ struct editorConfig {
erow *row; /* Rows */
time_t lastevent; /* Last event time, so we can go standby */
keyState key[KEY_MAX]; /* Remember if a key is pressed / repeated. */
unsigned int modifiers; /* Key modifiers held. CTRL & SHIFT & ALT & META */
int dirty; /* File modified but not saved. */
char *filename; /* Currently open filename */
frameBuffer *fb; /* Framebuffer */
Expand All @@ -76,4 +83,6 @@ void editorClearError(void);
int editorFileWasModified(void);
void editorRun(void);

int getFirstNonSpace(erow *row);

#endif /* EDITOR_H */
3 changes: 3 additions & 0 deletions examples/flames.lua
Expand Up @@ -10,6 +10,7 @@ function setup()
refreshCount = 0
skipCount = 0
Flames = { }
filled(false)

for i=1,MaxFlames do
x = math.random(WIDTH/3) + (WIDTH/3)
Expand All @@ -26,9 +27,11 @@ function draw()
background(0,0,0)
for i,f in pairs(Flames) do
if f.l > 35 then
filled(true)
fill(255, 255, 255, 0.9)
minMove = 0
elseif f.l > 30 then
filled(false)
fill(255, 255, 192, 0.8)
minMove = 1
elseif f.l > 20 then
Expand Down