Skip to content

Commit

Permalink
Fixing warnings in wefx code moving to c11
Browse files Browse the repository at this point in the history
  • Loading branch information
robrohan committed May 31, 2024
1 parent 2875b18 commit e517bec
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 35 deletions.
13 changes: 9 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ PANDOC?=pandoc
NARRATIVE?=narrative
MAIN?=examples/example0.c

C_ERRS += -Wall -Wextra -Wpedantic \
-Wformat=2 -Wno-unused-parameter -Wshadow \
-Wwrite-strings -Wstrict-prototypes -Wold-style-definition \
-Wredundant-decls -Wnested-externs -Wmissing-include-dirs \
-Wno-unused

NO_BUILT_INS=-fno-builtin-sin -fno-builtin-cos -fno-builtin-tan \
-fno-builtin-ceil -fno-builtin-floor \
-fno-builtin-pow -fno-builtin-round \
Expand Down Expand Up @@ -35,10 +41,9 @@ clean:

build: clean
mkdir -p build
$(CC) \
$(CC) $(C_ERRS) \
--target=wasm32 \
-std=c99 \
-Wall \
-std=c11 \
-g \
-nostdlib \
-Os -flto \
Expand Down Expand Up @@ -82,7 +87,7 @@ test: clean_test

mkdir -p build
# add -lm if you want to test against built in math.h
$(CC) -std=c99 -m32 -g \
$(CC) $(C_ERRS) -std=c11 -m32 -g \
$(NO_BUILT_INS) \
src/math.c src/wefx.c src/test.c \
-o build/test
Expand Down
52 changes: 34 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
# wefx

Wefx is a simple graphics library for drawing using C, WASM ([Web Assembly][webassembly]), and an [HTML canvas][htmlcanvas]. It aims to serve a similar purpose as [gfx][gfx], but provide an introduction to using C and WASM. Wefx is meant to be a teaching / learning tool for C and graphics. Wefx is not using OpenGL /
WebGL or anything like that. It is doing very basic pixel manipulation and
has very simple functions to draw pixels and lines.
Wefx is a simple graphics library for drawing using C11, WASM
([Web Assembly][webassembly]), and an [HTML canvas][htmlcanvas].
It aims to serve a similar purpose as [gfx][gfx], but provide an introduction
to using C and WASM. Wefx is meant to be a teaching / learning tool for C and
graphics. Wefx is not using OpenGL / WebGL or anything like that. It is doing
very basic pixel manipulation and has very simple functions to draw pixels and
lines.

You can also [download the documentation](https://raw.githubusercontent.com/robrohan/wefx/main/docs/manual.pdf)
You can also download the
[documentation](https://raw.githubusercontent.com/robrohan/wefx/main/docs/manual.pdf)

![Example Screenshot](https://raw.githubusercontent.com/robrohan/wefx/main/docs/wefx_shot.png)
![Screenshot](https://raw.githubusercontent.com/robrohan/wefx/main/docs/wefx_shot.png)

## Quick Start

If you are using Ubuntu, you can run `make init` to install the correct version of clang and tools. If you are on another system, you will have to install `clang` yourself.
If you are using Ubuntu, you can run `make init` to install the correct
version of clang and tools. If you are on another system, you will have to
install `clang` yourself.

Once installed, `make build` should compile `examples/example0.c` into WASM (if there are no errors). If successful, and if you have python installed, you can run `make serve` to start a simple HTTP server and browse to http://localhost:8000 to view wasm output.
Once installed, `make build` should compile `examples/example0.c` into WASM
(if there are no errors). If successful, and if you have python installed, you
can run `make serve` to start a simple HTTP server and browse to
http://localhost:8000 to view wasm output.

## Using The Project

Expand Down Expand Up @@ -41,8 +51,9 @@ You'll need the following programs installed:
- make (optional - MacOS and Linux)
- (optional) python3

On MacOS or Linux these tools should be available already, or easily
installed with homebrew (`brew install`), or Apt (`apt install`), or your local package manager.
On MacOS or Linux these tools should be available already, or easily installed
with homebrew (`brew install`), or Apt (`apt install`), or your local package
manager.

### Compiling

Expand Down Expand Up @@ -71,7 +82,9 @@ directly from your file system (this is just how wasm loading works).
If you try to open the `index.html` file directly you will get an error
like:

> Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at file:///xxxxx/build/wefx.wasm. (Reason: CORS request not http).
> Cross-Origin Request Blocked: The Same Origin Policy disallows reading the
> remote resource at file:///xxxxx/build/wefx.wasm. (Reason: CORS request not
> http).
A basic http server comes with python3, and the make file will run that
server if you run:
Expand All @@ -91,9 +104,9 @@ to see the compiled code.
---

If you already have a favorite server (for example I use
[busboy](https://github.com/robrohan/busboy)), you can use that serve to
serve the `build` directory instead, and then run the `make build` command
to replace the wasm file as you play around.
[busboy](https://github.com/robrohan/busboy)), you can use that serve to serve
the `build` directory instead, and then run the `make build` command to replace
the wasm file as you play around.

For example in one shell I run:

Expand All @@ -112,15 +125,16 @@ And then simply refresh the browser to see changes.
### Writing Code

If just teaching / learning about graphics, you'll only need to edit the
[./examples/example0.c](./examples/example0.c) file. There are two entry
points into that file:
[./examples/example0.c](./examples/example0.c) file. There are two entry points
into that file:

| Function | Usage |
| ---------| -----|
|init()| Called once at the start of the app|
|main_loop(time)| Called every frame with time being time since app start|

You can also add your own entry files in the examples directory, and then pass them to the build script using the _MAIN_ variable. For example:
You can also add your own entry files in the examples directory, and then pass
them to the build script using the _MAIN_ variable. For example:

```{sh}
make build MAIN=examples/example1.c
Expand All @@ -132,7 +146,8 @@ This will build the WASM file using `example1.c` as the entry point.

#### API

The API calls try to emulate [gfx][gfx] as much as possible. Here are a few currently supported functions (see the documentation for a full reference):
The API calls try to emulate [gfx][gfx] as much as possible. Here are a few
currently supported functions (see the documentation for a full reference):

| Function | Does |
| ---------| -----|
Expand All @@ -145,7 +160,8 @@ The API calls try to emulate [gfx][gfx] as much as possible. Here are a few curr

#### Coordinate System

The coordinate system in newer versions has changed to reflect most other drawing styles. The system works thusly:
The coordinate system in newer versions has changed to reflect most other
drawing styles. The system works thusly:

```
+Y
Expand Down
2 changes: 1 addition & 1 deletion examples/example0.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

// Called once at startup
// note: memory not initialized yet so 'print' will not work
EXPORT int init()
EXPORT int init(void)
{
// Open a "window"
int err = wefx_open(W, H, "Logo Window");
Expand Down
2 changes: 1 addition & 1 deletion examples/example1.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#define W 1024
#define H 768

EXPORT int init()
EXPORT int init(void)
{
int err = wefx_open(W, H, "Test Window");
if (err)
Expand Down
2 changes: 1 addition & 1 deletion src/events.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Similar to how _wefx\_open_ created screen memory, the _wefx_open_events_
function allocates memory for the event queue.
*/
wefx_event_queue *wefx_open_events()
wefx_event_queue *wefx_open_events(void)
{
wefx_q = malloc(sizeof(struct wefx_event_queue));
// wefx_q = &(const struct wefx_event_queue){ 0 };
Expand Down
2 changes: 1 addition & 1 deletion src/events.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ typedef struct wefx_event_queue

//////////////////////////////////////////////

wefx_event_queue *wefx_open_events();
wefx_event_queue *wefx_open_events(void);

void wefx_init_queue(wefx_event_queue *q);

Expand Down
10 changes: 5 additions & 5 deletions src/wefx.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ This is often called at the top of the render loop to reset to a blank slate bef
doing any drawing.
*/
void wefx_clear()
void wefx_clear(void)
{
for (int q = 0; q < w * h; q++)
buffer[q] = bg_color;
Expand Down Expand Up @@ -237,10 +237,10 @@ what it considers to be the screen.
---
*/
EXPORT void wefx_draw(unsigned int *screen)
EXPORT void wefx_draw(unsigned int *iscreen)
{
for (int q = 0; q < w * h; q++)
screen[q] = buffer[q];
iscreen[q] = buffer[q];
}
/*
Expand All @@ -251,11 +251,11 @@ size to be. These methods are exposed to Javascript to get the X
and Y dimensions of the buffer / screen.
*/
EXPORT int wefx_xsize()
EXPORT int wefx_xsize(void)
{
return w;
}
EXPORT int wefx_ysize()
EXPORT int wefx_ysize(void)
{
return h;
}
8 changes: 4 additions & 4 deletions src/wefx.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void wefx_line(int x1, int y1, int x2, int y2);
void wefx_color(unsigned int red, unsigned int green, unsigned int blue);

// Clear the graphics window to the background color.
void wefx_clear();
void wefx_clear(void);

// Change the current background color.
void wefx_clear_color(unsigned int red, unsigned int green, unsigned int blue);
Expand All @@ -32,11 +32,11 @@ void wefx_clear_color(unsigned int red, unsigned int green, unsigned int blue);
// int wefx_ypos();

// Return the X and Y dimensions of the window.
int wefx_xsize();
int wefx_ysize();
int wefx_xsize(void);
int wefx_ysize(void);

extern void print(const char *);

void wefx_draw(unsigned int *screen);

#endif // WEFX__H
#endif // WEFX__H

0 comments on commit e517bec

Please sign in to comment.