From e517bec509e981550f88c24d656e50fad02a2ccd Mon Sep 17 00:00:00 2001 From: Rob Rohan Date: Sat, 1 Jun 2024 08:26:04 +1200 Subject: [PATCH] Fixing warnings in wefx code moving to c11 --- Makefile | 13 ++++++++---- README.md | 52 +++++++++++++++++++++++++++++---------------- examples/example0.c | 2 +- examples/example1.c | 2 +- src/events.c | 2 +- src/events.h | 2 +- src/wefx.c | 10 ++++----- src/wefx.h | 8 +++---- 8 files changed, 56 insertions(+), 35 deletions(-) diff --git a/Makefile b/Makefile index 8990b20..7d0103a 100644 --- a/Makefile +++ b/Makefile @@ -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 \ @@ -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 \ @@ -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 diff --git a/README.md b/README.md index 9bf6e0a..824d50a 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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: @@ -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: @@ -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 @@ -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 | | ---------| -----| @@ -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 diff --git a/examples/example0.c b/examples/example0.c index 32eee66..fa9117e 100644 --- a/examples/example0.c +++ b/examples/example0.c @@ -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"); diff --git a/examples/example1.c b/examples/example1.c index 11b9d53..b2b6fa6 100644 --- a/examples/example1.c +++ b/examples/example1.c @@ -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) diff --git a/src/events.c b/src/events.c index f13b70d..022ff8c 100644 --- a/src/events.c +++ b/src/events.c @@ -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 }; diff --git a/src/events.h b/src/events.h index ede995e..18d3f2c 100644 --- a/src/events.h +++ b/src/events.h @@ -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); diff --git a/src/wefx.c b/src/wefx.c index c716f07..b0f077c 100644 --- a/src/wefx.c +++ b/src/wefx.c @@ -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; @@ -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]; } /* @@ -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; } diff --git a/src/wefx.h b/src/wefx.h index f2d7d19..f676442 100644 --- a/src/wefx.h +++ b/src/wefx.h @@ -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); @@ -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 \ No newline at end of file +#endif // WEFX__H