From d19cf962cfe19840afab602f5ffaa1abf13c0285 Mon Sep 17 00:00:00 2001 From: Jack Kelly Date: Fri, 19 Oct 2018 20:53:45 +0100 Subject: [PATCH] refactored project name canvas->pixels --- 1_pixel.c => examples/1_pixel.c | 6 +-- 2_line.c => examples/2_line.c | 6 +-- 3_box.c => examples/3_box.c | 6 +-- 4_splash.c => examples/4_splash.c | 6 +-- 5_RGB.c => examples/5_RGB.c | 0 5_rows.c => examples/5_rows.c | 6 +-- 6_avalanche.c => examples/6_avalanche.c | 18 ++++--- examples/Makefile | 23 ++++++++ main.c | 70 ------------------------- {canvas => pixels}/canvas.c | 0 {canvas => pixels}/canvas.h | 0 {canvas => pixels}/draw.c | 0 {canvas => pixels}/draw.h | 0 {canvas => pixels}/screen.c | 0 {canvas => pixels}/screen.h | 0 template.c => pixels/template.c | 0 program.c | 64 ++++++++++++++++++++++ 17 files changed, 113 insertions(+), 92 deletions(-) rename 1_pixel.c => examples/1_pixel.c (85%) rename 2_line.c => examples/2_line.c (86%) rename 3_box.c => examples/3_box.c (87%) rename 4_splash.c => examples/4_splash.c (88%) rename 5_RGB.c => examples/5_RGB.c (100%) rename 5_rows.c => examples/5_rows.c (92%) rename 6_avalanche.c => examples/6_avalanche.c (80%) create mode 100644 examples/Makefile delete mode 100644 main.c rename {canvas => pixels}/canvas.c (100%) rename {canvas => pixels}/canvas.h (100%) rename {canvas => pixels}/draw.c (100%) rename {canvas => pixels}/draw.h (100%) rename {canvas => pixels}/screen.c (100%) rename {canvas => pixels}/screen.h (100%) rename template.c => pixels/template.c (100%) create mode 100644 program.c diff --git a/1_pixel.c b/examples/1_pixel.c similarity index 85% rename from 1_pixel.c rename to examples/1_pixel.c index 3a65c53..bd527d1 100644 --- a/1_pixel.c +++ b/examples/1_pixel.c @@ -4,9 +4,9 @@ #include #include "SDL.h" -#include "canvas/canvas.h" -#include "canvas/screen.h" -#include "canvas/draw.h" +#include "pixels/canvas.h" +#include "pixels/screen.h" +#include "pixels/draw.h" int main(int argc, char *argv[]) diff --git a/2_line.c b/examples/2_line.c similarity index 86% rename from 2_line.c rename to examples/2_line.c index 662a94d..5197916 100644 --- a/2_line.c +++ b/examples/2_line.c @@ -4,9 +4,9 @@ #include #include "SDL.h" -#include "canvas/canvas.h" -#include "canvas/screen.h" -#include "canvas/draw.h" +#include "pixels/canvas.h" +#include "pixels/screen.h" +#include "pixels/draw.h" int main(int argc, char *argv[]) diff --git a/3_box.c b/examples/3_box.c similarity index 87% rename from 3_box.c rename to examples/3_box.c index db96e62..cf956f4 100644 --- a/3_box.c +++ b/examples/3_box.c @@ -4,9 +4,9 @@ #include #include "SDL.h" -#include "canvas/canvas.h" -#include "canvas/screen.h" -#include "canvas/draw.h" +#include "pixels/canvas.h" +#include "pixels/screen.h" +#include "pixels/draw.h" int main(int argc, char *argv[]) diff --git a/4_splash.c b/examples/4_splash.c similarity index 88% rename from 4_splash.c rename to examples/4_splash.c index 9df63b2..c9da183 100644 --- a/4_splash.c +++ b/examples/4_splash.c @@ -4,9 +4,9 @@ #include #include "SDL.h" -#include "canvas/canvas.h" -#include "canvas/screen.h" -#include "canvas/draw.h" +#include "pixels/canvas.h" +#include "pixels/screen.h" +#include "pixels/draw.h" int main(int argc, char *argv[]) diff --git a/5_RGB.c b/examples/5_RGB.c similarity index 100% rename from 5_RGB.c rename to examples/5_RGB.c diff --git a/5_rows.c b/examples/5_rows.c similarity index 92% rename from 5_rows.c rename to examples/5_rows.c index 935b0a8..b57da9a 100644 --- a/5_rows.c +++ b/examples/5_rows.c @@ -4,9 +4,9 @@ #include #include "SDL.h" -#include "canvas/canvas.h" -#include "canvas/screen.h" -#include "canvas/draw.h" +#include "pixels/canvas.h" +#include "pixels/screen.h" +#include "pixels/draw.h" int main(int argc, char *argv[]) diff --git a/6_avalanche.c b/examples/6_avalanche.c similarity index 80% rename from 6_avalanche.c rename to examples/6_avalanche.c index 053e8aa..b7fd655 100644 --- a/6_avalanche.c +++ b/examples/6_avalanche.c @@ -5,17 +5,21 @@ #include #include "SDL.h" -#include "canvas/canvas.h" -#include "canvas/screen.h" -#include "canvas/draw.h" +#include "pixels/canvas.h" +#include "pixels/screen.h" +#include "pixels/draw.h" int main(int argc, char *argv[]) { - int width = 16; - int height = 16; + int width = 128; + int height = 128; + + int avalanche[128]; + int i; + for (i = 0; i < 128; i++) + avalanche[i] = 0; - int avalanche[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; srand(time(NULL)); canvas_create(width, height); @@ -55,7 +59,7 @@ int main(int argc, char *argv[]) } screen_update(); - usleep(41666); + usleep(16777); } /* ------------------------------------ */ diff --git a/examples/Makefile b/examples/Makefile new file mode 100644 index 0000000..1987821 --- /dev/null +++ b/examples/Makefile @@ -0,0 +1,23 @@ +program: program.c + gcc -Wall -o program program.c pixels/canvas.c pixels/screen.c pixels/draw.c -I/usr/include/SDL2 -D_REENTRANT -L/usr/lib/arm-linux-gnueabihf -lSDL2 + +1_pixel: 1_pixel.c + gcc -Wall -o 1_pixel 1_pixel.c pixels/canvas.c pixels/screen.c pixels/draw.c -I/usr/include/SDL2 -D_REENTRANT -L/usr/lib/arm-linux-gnueabihf -lSDL2 + +2_line: 2_line.c + gcc -Wall -o 2_line 2_line.c pixels/canvas.c pixels/screen.c pixels/draw.c -I/usr/include/SDL2 -D_REENTRANT -L/usr/lib/arm-linux-gnueabihf -lSDL2 + +3_box: 3_box.c + gcc -Wall -o 3_box 3_box.c pixels/canvas.c pixels/screen.c pixels/draw.c -I/usr/include/SDL2 -D_REENTRANT -L/usr/lib/arm-linux-gnueabihf -lSDL2 + +4_splash: 4_splash.c + gcc -Wall -o 4_splash 4_splash.c pixels/canvas.c pixels/screen.c pixels/draw.c -I/usr/include/SDL2 -D_REENTRANT -L/usr/lib/arm-linux-gnueabihf -lSDL2 + +5_rows: 5_rows.c + gcc -Wall -o 5_rows 5_rows.c pixels/canvas.c pixels/screen.c pixels/draw.c -I/usr/include/SDL2 -D_REENTRANT -L/usr/lib/arm-linux-gnueabihf -lSDL2 + +6_avalanche: 6_avalanche.c + gcc -Wall -o 6_avalanche 6_avalanche.c pixels/canvas.c pixels/screen.c pixels/draw.c -I/usr/include/SDL2 -D_REENTRANT -L/usr/lib/arm-linux-gnueabihf -lSDL2 + +clean: + rm -f program 1_pixel 2_line 3_box 4_splash 5_rows 6_avalanche diff --git a/main.c b/main.c deleted file mode 100644 index 5e465e8..0000000 --- a/main.c +++ /dev/null @@ -1,70 +0,0 @@ -#include -#include -#include -#include -#include -#include "SDL.h" - -#include "canvas/canvas.h" -#include "canvas/screen.h" -#include "canvas/draw.h" - - -int main(int argc, char *argv[]) -{ - int width = 128; - int height = 128; - - int avalanche[width]; - int i; - for (i = 0; i < width; i++) - avalanche[i] = 0; - - srand(time(NULL)); - - canvas_create(width, height); - - screen_setup(width, height); - - /* ----- Write your program below ----- */ - - bool loop = true; - - while (loop == true) - { - if (screen_closebuttonpressed() == true) - loop = false; - - clear(0, 0, 0); - - int x, y; - - for (x = 0; x < height; x++) - { - //Lower the "avalanche" by a random amount in each column - int random = (rand() % 2) + 1; - avalanche[x] = avalanche[x] + random; - - for (y = 0; y < height; y++) - { - Uint8 r = 255; - Uint8 g = 255; - Uint8 b = 255; - - if (y < avalanche[x]) - draw_pixel(x, y, r, g, b); - else - break; - } - } - - screen_update(); - usleep(41666); - } - - /* ------------------------------------ */ - - screen_cleanup(); - - return 0; -} diff --git a/canvas/canvas.c b/pixels/canvas.c similarity index 100% rename from canvas/canvas.c rename to pixels/canvas.c diff --git a/canvas/canvas.h b/pixels/canvas.h similarity index 100% rename from canvas/canvas.h rename to pixels/canvas.h diff --git a/canvas/draw.c b/pixels/draw.c similarity index 100% rename from canvas/draw.c rename to pixels/draw.c diff --git a/canvas/draw.h b/pixels/draw.h similarity index 100% rename from canvas/draw.h rename to pixels/draw.h diff --git a/canvas/screen.c b/pixels/screen.c similarity index 100% rename from canvas/screen.c rename to pixels/screen.c diff --git a/canvas/screen.h b/pixels/screen.h similarity index 100% rename from canvas/screen.h rename to pixels/screen.h diff --git a/template.c b/pixels/template.c similarity index 100% rename from template.c rename to pixels/template.c diff --git a/program.c b/program.c new file mode 100644 index 0000000..7569b26 --- /dev/null +++ b/program.c @@ -0,0 +1,64 @@ +#include +#include +#include +#include +#include "SDL.h" + +#include "pixels/canvas.h" +#include "pixels/screen.h" +#include "pixels/draw.h" + + +int main(int argc, char *argv[]) +{ + int width = 32; + int height = 32; + + canvas_create(width, height); + + screen_setup(width, height); + + /* ----- Write your program below ----- */ + + int frame = 0; + bool ascending = true; + + bool exit = false; + + while (exit == false) + { + if (screen_closebuttonpressed()) + break; + + int x, y; + + for (y = 0; y < height; y++) + for (x = 0; x < width; x++) + { + Uint8 r = 255 - ((255 / height) * y); + Uint8 g = (255 / width) * x; + Uint8 b = frame % 255; + + draw_pixel(x, y, r, g, b); + } + + if (frame >= 254) + ascending = false; + else if (frame <= 0) + ascending = true; + + if (ascending == true) + frame++; + else + frame--; + + screen_update(); + usleep(16700); + } + + /* ------------------------------------ */ + + screen_cleanup(); + + return 0; +}