1
0
mirror of https://github.com/rzzzwilson/pymlac.git synced 2025-06-10 09:32:41 +00:00

Implementing ABS/REL draw with beam ON/OFF/

This commit is contained in:
Ross Wilson
2022-07-16 15:32:01 +07:00
parent fd07d41c29
commit abece56694

View File

@@ -8,69 +8,68 @@
#define SAVEFILE "snapshot.png"
enum BEAM {OFF, ON}; // beam ON/OFF
enum OP {ABS, REL}; // absolute/relative moves
typedef struct
{
int x1;
int y1;
int x2;
int y2;
int dy1;
int dy2;
bool end;
enum OP type;
enum BEAM beam;
int dx;
int dy;
} DLine;
DLine drawlist[] = {{0, 0, WIDTH, HEIGHT, 1, 1, false},
{0, HEIGHT, WIDTH, 0, 1, 1, false},
{0, HEIGHT/2, HEIGHT, HEIGHT/2, -1, -1, true}
};
#define DRAWLIST_SIZE 1000
int len_drawlist = 3;
DLine drawlist[DRAWLIST_SIZE];
// index of drawlist entry AFTER last instruction
unsigned int drawlist_end = 0;
//-------------------------
// Define some characters with relative moves
//-------------------------
DLine Char_I[] = {{REL, OFF, 0, -6},
{REL, ON, 2, 0},
{REL, OFF, -1, 0},
{REL, ON, 0, 6},
{REL, OFF, -1, 0},
{REL, ON, 2, 0},
};
DLine Char_m[] = {{REL, ON, 0, -3},
{REL, ON, 1, -1},
{REL, ON, 0, 1},
{REL, OFF, 0, -1},
{REL, ON, 1, -1},
{REL, ON, 1, 1},
{REL, ON, 0, 3},
};
int Magnification = 5;
DLine *
unsigned int
make_drawlist(void)
{
for (int dindex = 0; dindex < len_drawlist; ++dindex)
{
int x1 = drawlist[dindex].x1;
int y1 = drawlist[dindex].y1;
int x2 = drawlist[dindex].x2;
int y2 = drawlist[dindex].y2;
int dy1 = drawlist[dindex].dy1;
int dy2 = drawlist[dindex].dy2;
drawlist_end = 0;
y1 += dy1;
if (y1 >= HEIGHT)
{
y1 = HEIGHT;
dy1 = -1;
}
if (y1 <= 0)
{
y1 = 0;
dy1 = +1;
}
DLine dline = {ABS, OFF, 100, 100};
memcpy(&drawlist[drawlist_end], &dline, sizeof(DLine));
++drawlist_end;
drawlist[dindex].y1 = y1;
drawlist[dindex].dy1 = dy1;
memcpy(&drawlist[drawlist_end], Char_I, sizeof(Char_I));
drawlist_end += sizeof(Char_I);
y2 += dy2;
if (y2 >= HEIGHT)
{
y2 = HEIGHT;
dy2 = -1;
}
if (y2 <= 0)
{
y2 = 0;
dy2 = +1;
}
drawlist[dindex].y2 = y2;
drawlist[dindex].dy2 = dy2;
}
DLine dline2 = {ABS, OFF, 106, 100};
memcpy(&drawlist[drawlist_end], &dline2, sizeof(DLine));
++drawlist_end;
return drawlist;
memcpy(&drawlist[drawlist_end], Char_m, sizeof(Char_m));
drawlist_end += sizeof(Char_m);
return drawlist_end;
}
@@ -116,7 +115,6 @@ saveScreenshotBMP(char *savefile, SDL_Window* window, SDL_Renderer* renderer)
return false;
}
IMG_SavePNG(save_surface, SAVEFILE);
// SDL_SavePNG(save_surface, savefile);
SDL_FreeSurface(save_surface);
save_surface = NULL;
}
@@ -152,7 +150,7 @@ main(int argc, char **argv)
// handle events
while (!quit)
{
DLine *drawlist = make_drawlist();
drawlist_end = make_drawlist();
SDL_Delay(5);
SDL_PollEvent(&event);
@@ -179,21 +177,42 @@ main(int argc, char **argv)
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
// render the draw list, set draw colour
// set draw colour
SDL_SetRenderDrawColor(renderer, 200, 255, 200, 255);
// set beam at (0, 0)
unsigned int abs_x = 0;
unsigned int abs_y = 0;
// draw the drawlist
for (int dindex = 0; dindex < len_drawlist; ++dindex)
for (int dindex = 0; dindex < drawlist_end; ++dindex)
{
int x1 = drawlist[dindex].x1;
int y1 = drawlist[dindex].y1;
int x2 = drawlist[dindex].x2;
int y2 = drawlist[dindex].y2;
enum OP type = drawlist[dindex].type;
enum BEAM beam = drawlist[dindex].beam;
int dx = drawlist[dindex].dx * Magnification;
int dy = drawlist[dindex].dy * Magnification;
unsigned int new_x;
unsigned int new_y;
SDL_RenderDrawLine(renderer, x1, y1, x2, y2);
if (type == ABS)
{
new_x = dx;
new_y = dy;
if (drawlist[dindex].end)
break;
if (beam == ON)
SDL_RenderDrawLine(renderer, abs_x, abs_y, new_x + 1, new_y + 1);
}
else
{
new_x = abs_x + dx;
new_y = abs_y + dy;
if (beam == ON)
SDL_RenderDrawLine(renderer, abs_x, abs_y, new_x + 1, new_y + 1);
}
abs_x = new_x;
abs_y = new_y;
}
// render window