From 9fd87b7f2920ed6a2f7fb484488b41df1137eef9 Mon Sep 17 00:00:00 2001 From: Karma Riuk Date: Tue, 14 Mar 2023 21:46:36 +0100 Subject: [PATCH] Simple implementation of polygons, you can create them in the init and draw them --- Makefile | 4 ++-- polygons.cc | 31 +++++++++++++++++++++++++++++++ polygons.h | 28 ++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 polygons.cc create mode 100644 polygons.h diff --git a/Makefile b/Makefile index 541b388..6ae2c69 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,7 @@ CXXFLAGS=-Wall -g -O2 $(PROFILING_CFLAGS) $(GTK_CFLAGS) LIBS=$(GTK_LIBS) -lm PROGS=balls -OBJS=balls.o c_index.o game.o gravity.o spaceship.o main.o +OBJS=balls.o c_index.o game.o gravity.o spaceship.o main.o polygons.o # dependencies (gcc -MM *.cc) balls.o: balls.cc game.h balls.h vec2d.h gravity.h @@ -24,7 +24,7 @@ stats.o: stats.cc .PHONY: run run: balls - ./balls + ./balls n=0 .PHONY: all all: $(PROGS) diff --git a/polygons.cc b/polygons.cc new file mode 100644 index 0000000..eddf463 --- /dev/null +++ b/polygons.cc @@ -0,0 +1,31 @@ +#include "polygons.h" + +#include + +polygon* polygons = nullptr; +uint n_polygons = 1; + +void polygons_init_state() { + polygons = new polygon[n_polygons]; + + polygon poly = + polygon{{400, 400}, 0, {{0, 0}, {0, 100}, {100, 100}, {100, 0}}}; + polygons[0] = poly; +} + +void polygon::draw(cairo_t* cr) const { + polygon& poly = polygons[0]; + vec2d& center = poly.center; + cairo_set_source_rgb(cr, 1, 1, 1); + + cairo_move_to(cr, center.x, center.y); + for (auto& point : poly.points) + cairo_line_to(cr, center.x + point.x, center.y + point.y); + cairo_line_to(cr, center.x, center.y); + cairo_stroke(cr); +} + +void polygons_draw(cairo_t* cr) { + for (const polygon* p = polygons; p != polygons + n_polygons; ++p) + p->draw(cr); +} diff --git a/polygons.h b/polygons.h new file mode 100644 index 0000000..471cdf9 --- /dev/null +++ b/polygons.h @@ -0,0 +1,28 @@ +#ifndef POLYGONS_H_INCLUDED +#define POLYGONS_H_INCLUDED + +// extern ball spaceship; + +#include "vec2d.h" + +#include +#include + +class polygon { + public: + vec2d center; + double angle; + std::vector points; + + void draw(cairo_t* cr) const; +}; + +extern polygon* polygons; +extern uint n_polygons; + +extern void polygons_init_state(); +extern void polygons_update_state(); +// extern void polygon_control (double dx, double dy); +extern void polygons_draw(cairo_t* cr); + +#endif