Skip to content

Commit

Permalink
Adding rectangle and copying over pixel size from my other tests
Browse files Browse the repository at this point in the history
  • Loading branch information
robrohan committed Dec 31, 2024
1 parent 63c01aa commit bb03c30
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 22 deletions.
1 change: 1 addition & 0 deletions examples/example0.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ void draw(int time)
{
wefx_clear();
wefx_color(rand() % 0xff, rand() % 0xff, rand() & 0xff);
wefx_set_psize(2);

// W
wefx_line(15, 35, 20, 15);
Expand Down
101 changes: 82 additions & 19 deletions src/wefx.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ static color fg_color = 0;
static color bg_color = 0;
static int w = 0;
static int h = 0;
static int psize = 1;
/*
## Opening a Canvas - wefx_open
Expand Down Expand Up @@ -98,6 +99,17 @@ void wefx_color(unsigned int red, unsigned int green, unsigned int blue)
}
/*
## Set the Drawing Pixel Size
Default pixel size is 1
*/
void wefx_set_psize(int size)
{
psize = size;
}
/*
## Draw a Single Point - wefx_point
This function sets one pixel value to a color. It set one of the values in
Expand All @@ -108,17 +120,46 @@ By setting the value at $x + y * w$ we are drawing a point at $(x,y)$ on the scr
*/
void wefx_point(int x, int y)
{
#ifdef WEFX_ORIGIN_TOP_LEFT
int offset = x + y * w;
#elif WEFX_ORIGIN_CENTER
int cx = ((w / 2) + x);
int cy = ((h / 2) + y);
int offset = cx + cy * w;
#else // WEFX_ORIGIN_BOTTOM_LEFT
int inboundx = x-1 < 0 ? 0 : x-1;
int inboundy = y+1 > h ? y : y+1;
// because 0,0 should display at the bottom left
// we need to add 1 to the height or 0 is offscreen
// because 0,0 should display at the bottom left
// we need to add 1 to the height or 0 is offscreen
int offset = inboundx + (((int)abs( (inboundy) - h) * w));
if(offset > w*h || offset < 0) {
return;
}
#endif
buffer[offset] = fg_color;
}
/*
## Draw a Pixel
This function draw's a pixel to the screen. This is similar to "point" but can
have an arbitrary size. This is useful for stylized rendering. For example if
you want a more chucky rendering, you can use a 4x4 pixel size
The start point of the pixel is the top left of the group.
*/
void wefx_pixel(int x0, int y0)
{
y0 = y0 + psize;
for (int r = 0; r <= psize; r++)
{
for (int c = 0; c <= psize; c++)
{
wefx_point(x0 + r, y0 - c);
}
}
}
/*
## Set the Background Color - wefx_clear_color
Expand Down Expand Up @@ -165,7 +206,7 @@ void wefx_line(int x0, int y0, int x1, int y1)

for (;;)
{
wefx_point(x0, y0);
wefx_pixel(x0, y0);
if (x0 == x1 && y0 == y1)
break;
int e2 = 2 * error;
Expand All @@ -187,28 +228,43 @@ void wefx_line(int x0, int y0, int x1, int y1)
}
/*
## Draw a Rectangle - wefx_rect
Draws a rectangle where x0,y0 is the top left of the rectangle and
x1,y1 is the bottom right.
*/
void wefx_rect(int x0, int y0, int x1, int y1)
{
wefx_line(x0, y0, x0, y1);
wefx_line(x0, y1, x1, y1);
wefx_line(x1, y1, x1, y0);
wefx_line(x1, y0, x0, y0);
}
/*
## Draw a Circle - wefx_circle
This function can be called to draw a circle. It also uses the
currently set foreground color. It uses the Midpoint Circle Algorithm [@MidpointCircleAlgorithm_2022_].
*/
void wefx_circle(int x0, int y0, int r0)
void wefx_circle(int x0, int y0, int r)
{
int x = r0;
int x = r;
int y = 0;
int err = 0;
while (x >= y)
{
wefx_point(x0 + x, y0 + y);
wefx_pixel(x0 + x, y0 + y);

wefx_point(x0 + y, y0 + x);
wefx_point(x0 - y, y0 + x);
wefx_point(x0 - x, y0 + y);
wefx_point(x0 - x, y0 - y);
wefx_point(x0 - y, y0 - x);
wefx_point(x0 + y, y0 - x);
wefx_point(x0 + x, y0 - y);
wefx_pixel(x0 + y, y0 + x);
wefx_pixel(x0 - y, y0 + x);
wefx_pixel(x0 - x, y0 + y);
wefx_pixel(x0 - x, y0 - y);
wefx_pixel(x0 - y, y0 - x);
wefx_pixel(x0 + y, y0 - x);
wefx_pixel(x0 + x, y0 - y);

y += 1;
err += 2 * y + 1;
Expand All @@ -230,17 +286,24 @@ drawn to the screen.
This method is called from Javascript and asks us to draw our buffer to
what it considers to be the screen.
---
*/
EXPORT void wefx_draw(unsigned int *iscreen)
{
// TODO: memcopy
for (int q = 0; q < (w * h); ++q)
iscreen[q] = buffer[q];
}
/*
*Note*: there might be a faster / better way to do this.
## Raw Screen Buffer
---
This grabs the raw buffer of pixels. Similar to wefx_draw, but instead
of copying the data to a new array, this gets the raw data.
*/
EXPORT void wefx_draw(unsigned int *iscreen)
unsigned int * wefx_get_buffer(void)
{
for (int q = 0; q < w * h; q++)
iscreen[q] = buffer[q];
return buffer;
}
/*
Expand Down
16 changes: 13 additions & 3 deletions src/wefx.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,18 @@ int wefx_open(unsigned int width, unsigned int height, const char *title);
// Draw a point at (x,y)
void wefx_point(int x, int y);

void wefx_circle(int x0, int y0, int r0);
// Draw a pixel at (x0,y0) with size psize
void wefx_pixel(int x0, int y0);

// Draw a line from (x1,y1) to (x2,y2)
void wefx_line(int x1, int y1, int x2, int y2);
void wefx_circle(int x0, int y0, int r);

// Draw a line from (x0,y0) to (x1,y1)
void wefx_line(int x0, int y0, int x1, int y1);

// Draw a rectangle from top left (x1, y1), to bottom right (x2,y2)
void wefx_rect(int x0, int y0, int x1, int y1);

void wefx_set_psize(int size);

// Change the current drawing color.
void wefx_color(unsigned int red, unsigned int green, unsigned int blue);
Expand All @@ -39,4 +47,6 @@ extern void print(const char *);

void wefx_draw(unsigned int *screen);

unsigned int * wefx_get_buffer(void);

#endif // WEFX__H

0 comments on commit bb03c30

Please sign in to comment.