From 8a3e4543c2ce76ee588812a6ed85a71ef93ed9ef Mon Sep 17 00:00:00 2001 From: Boris Stefanovic <owldev@bluewin.ch> Date: Sun, 22 May 2022 20:23:10 +0200 Subject: [PATCH] FIX: frame edge space and refresh loop for moving window around --- .gitignore | 1 + src/draw.c | 59 ++++++++++++++++++++++++++------------------------- src/draw.h | 5 ++++- src/field.c | 5 ++--- src/field.h | 2 +- src/main.c | 33 +++++++++++++++++++++------- utils/utils.c | 2 +- 7 files changed, 64 insertions(+), 43 deletions(-) diff --git a/.gitignore b/.gitignore index da8aa77..8a575a9 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ build *.o *.pdf main +*_tests diff --git a/src/draw.c b/src/draw.c index ae3ffd9..491f09a 100644 --- a/src/draw.c +++ b/src/draw.c @@ -1,12 +1,12 @@ #include <stdbool.h> - #include "../utils/gfx/gfx.h" -#include "field.h" +#include "../utils/utils.h" + void gfx_draw_line(struct gfx_context_t *ctxt, coordinates_t p0, coordinates_t p1, uint32_t color) { - int dx = abs((int)p1.column - (int)p0.column); + int dx = abs((int) p1.column - (int) p0.column); int sx = p0.column < p1.column ? 1 : -1; - int dy = -abs((int)p1.row - (int)p0.row); + int dy = -abs((int) p1.row - (int) p0.row); int sy = p0.row < p1.row ? 1 : -1; int error = dx + dy; @@ -34,29 +34,30 @@ void gfx_draw_line(struct gfx_context_t *ctxt, coordinates_t p0, coordinates_t p } } -void gfx_draw_circle(struct gfx_context_t *ctxt, coordinates c, uint32_t r, uint32_t color) { - int x = 0; - int y = r; - int d = r - 1; - while(y >= x){ - gfx_putpixel(ctxt, c.column + x, c.row + y, color); - gfx_putpixel(ctxt, c.column + y, c.row + x, color); - gfx_putpixel(ctxt, c.column - x, c.row + y, color); - gfx_putpixel(ctxt, c.column - y, c.row + x, color); - gfx_putpixel(ctxt, c.column + x, c.row - y, color); - gfx_putpixel(ctxt, c.column + y, c.row - x, color); - gfx_putpixel(ctxt, c.column - x, c.row - y, color); - gfx_putpixel(ctxt, c.column - y, c.row - x, color); - if(d >= 2*x){ - d = d-2*x-1; - x++; - }else if(d < 2 * ((int)r-y)){ - d = d + 2*y-1; - y--; - }else { - d = d + 2 * (y-x-1); - y--; - x++; - } - } + +void gfx_draw_circle(struct gfx_context_t *ctxt, coordinates_t c, uint32_t r, uint32_t color) { + int x = 0; + int y = r; + int d = r - 1; + while (y >= x) { + gfx_putpixel(ctxt, c.column + x, c.row + y, color); + gfx_putpixel(ctxt, c.column + y, c.row + x, color); + gfx_putpixel(ctxt, c.column - x, c.row + y, color); + gfx_putpixel(ctxt, c.column - y, c.row + x, color); + gfx_putpixel(ctxt, c.column + x, c.row - y, color); + gfx_putpixel(ctxt, c.column + y, c.row - x, color); + gfx_putpixel(ctxt, c.column - x, c.row - y, color); + gfx_putpixel(ctxt, c.column - y, c.row - x, color); + if (d >= 2 * x) { + d = d - 2 * x - 1; + x++; + } else if (d < 2 * ((int) r - y)) { + d = d + 2 * y - 1; + y--; + } else { + d = d + 2 * (y - x - 1); + y--; + x++; + } + } } diff --git a/src/draw.h b/src/draw.h index 5ac6b73..af39d21 100644 --- a/src/draw.h +++ b/src/draw.h @@ -3,7 +3,10 @@ #include "field.h" + void gfx_draw_line(struct gfx_context_t *ctxt, coordinates_t p0, coordinates_t p1, uint32_t color); + void gfx_draw_circle(struct gfx_context_t *ctxt, coordinates_t c, uint32_t r, uint32_t color); -#endif \ No newline at end of file + +#endif diff --git a/src/field.c b/src/field.c index b29131a..95f80f0 100644 --- a/src/field.c +++ b/src/field.c @@ -34,8 +34,7 @@ double compute_delta_x() { } bool is_in_screen(coordinates_t pos) { - // TODO: should this not be "<" instead of "<=" ? - return pos.column <= WID && pos.row <= HEI; + return pos.column < WID && pos.row < HEI; } bool draw_field_line_point(struct gfx_context_t *ctxt, charge_t *charges, int num_charges, double x0, double x1, double y0, double y1, vec2 pos, vec2 *pos_next, double delta) { @@ -105,7 +104,7 @@ static bool draw_field_line(struct gfx_context_t *ctxt, charge_t *charges, int n /// A circle with minus sign for negative charges /// A circle with a plus sign for positive charges static void draw_charges(struct gfx_context_t *context, charge_t *charges, int num_charges, double x0, double x1, double y0, double y1) { - for (int i = 0; i < num_charges; i++) { + for (int i = 0; i < num_charges; ++i) { coordinates_t charge_center = position_to_coordinates(WID, HEI, x0, x1, y0, y1, charges[i].pos); gfx_draw_circle(context, charge_center, CHARGE_R, COLOR_WHITE); diff --git a/src/field.h b/src/field.h index 4f7c4bd..4fb8f80 100644 --- a/src/field.h +++ b/src/field.h @@ -2,9 +2,9 @@ #define _PHYSIQUE_H_ #include <stdio.h> -#include "../utils/utils.h" #include "../utils/gfx/gfx.h" #include "../utils/vec2/vec2.h" +#include "../utils/utils.h" #define SIDE_LEN 1000 #define WID SIDE_LEN diff --git a/src/main.c b/src/main.c index 636c039..9c5e5a1 100644 --- a/src/main.c +++ b/src/main.c @@ -1,20 +1,17 @@ #include <stdlib.h> -#include <time.h> - -#include "draw.h" -#include "field.h" -#include "../utils/utils.h" #include "field.h" + #define NCHARGES 2 +#define NC 3 #define DX 25 #define NLINES 50 -int main() { +void main_old() { charge_t charges[NCHARGES] = { - charge_create(0.25, vec2_create(0.25, 0.5)), - charge_create(-0.25, vec2_create(0.75, 0.5)) + charge_create(0.25, vec2_create(0.25, 0.5)), + charge_create(-0.25, vec2_create(0.75, 0.5)) }; struct gfx_context_t *ctxt = gfx_create("elec", WID, HEI); @@ -23,5 +20,25 @@ int main() { while (gfx_keypressed() != SDLK_ESCAPE); gfx_destroy(ctxt); +} + + +void main_new() { + charge_t charges[NC]; + for (int i = 0; i < NC; ++i) + charges[i] = charge_create(i % 2 == 0 ? rand_one() : -rand_one(), vec2_create(rand_one(), rand_one())); + struct gfx_context_t *ctxt = gfx_create("elec", WID, HEI); + + draw_everything(ctxt, charges, NC, NLINES, DX, 0.0, 1.0, 0.0, 1.0); + gfx_present(ctxt); + while (gfx_keypressed() != SDLK_ESCAPE) { + gfx_present(ctxt); + } + gfx_destroy(ctxt); +} + + +int main() { + main_new(); return EXIT_SUCCESS; } diff --git a/utils/utils.c b/utils/utils.c index 24feb7a..2b2d974 100644 --- a/utils/utils.c +++ b/utils/utils.c @@ -1,8 +1,8 @@ #include <math.h> #include <stdlib.h> #include <time.h> -#include "utils.h" #include "vec2/vec2.h" +#include "utils.h" coordinates_t coordinates_create(int row_, int column_) { -- GitLab