Added a generator for polygons to make the init easier
This commit is contained in:
parent
48a1b3ade4
commit
e6a3979de1
5
Makefile
5
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 polygons.o
|
||||
OBJS=balls.o c_index.o game.o gravity.o spaceship.o main.o polygons.o polygon_generator.o
|
||||
|
||||
# dependencies (gcc -MM *.cc)
|
||||
balls.o: balls.cc game.h balls.h vec2d.h gravity.h
|
||||
@ -21,6 +21,9 @@ gravity.o: gravity.cc gravity.h balls.h vec2d.h game.h
|
||||
main.o: main.cc game.h balls.h vec2d.h c_index.h gravity.h spaceship.h
|
||||
spaceship.o: spaceship.cc balls.h vec2d.h game.h
|
||||
stats.o: stats.cc
|
||||
polygons.o: polygons.cc polygons.h vec2d.h polygon_generator.h
|
||||
polygon_generator.o: polygon_generator.cc polygon_generator.h
|
||||
|
||||
|
||||
.PHONY: run
|
||||
run: balls
|
||||
|
29
polygon_generator.cc
Normal file
29
polygon_generator.cc
Normal file
@ -0,0 +1,29 @@
|
||||
#include "polygon_generator.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <cmath>
|
||||
|
||||
#define PI 3.141592653589793238462643323
|
||||
|
||||
static double to_rad(double angle_in_deg) {
|
||||
static double PI_180 = PI / 180;
|
||||
return angle_in_deg * PI_180;
|
||||
}
|
||||
|
||||
polygon poly_generate::rectangle(double width, double height) {
|
||||
assert(width > 0);
|
||||
assert(height > 0);
|
||||
return polygon{
|
||||
{0, 0}, 0, {{0, 0}, {width, 0}, {width, height}, {0, height}}};
|
||||
}
|
||||
|
||||
polygon poly_generate::triangle(double side1, double side2, double angle) {
|
||||
assert(side1 > 0);
|
||||
assert(side2 > 0);
|
||||
return polygon{{0, 0},
|
||||
0,
|
||||
{{0, 0},
|
||||
{side1, 0},
|
||||
{side2 * std::cos(to_rad(angle)),
|
||||
side2 * std::sin(to_rad(angle))}}};
|
||||
}
|
20
polygon_generator.h
Normal file
20
polygon_generator.h
Normal file
@ -0,0 +1,20 @@
|
||||
#ifndef POLYGON_GENERATOR_H_INCLUDED
|
||||
#define POLYGON_GENERATOR_H_INCLUDED
|
||||
|
||||
#include "polygons.h"
|
||||
|
||||
#include <cassert>
|
||||
|
||||
namespace poly_generate {
|
||||
|
||||
polygon rectangle(double width, double height);
|
||||
|
||||
inline polygon square(double width) {
|
||||
assert(width > 0);
|
||||
return rectangle(width, width);
|
||||
};
|
||||
|
||||
polygon triangle(double side1, double side2, double angle);
|
||||
|
||||
}; // namespace poly_generate
|
||||
#endif
|
18
polygons.cc
18
polygons.cc
@ -1,26 +1,28 @@
|
||||
#include "polygons.h"
|
||||
|
||||
#include "polygon_generator.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
polygon* polygons = nullptr;
|
||||
uint n_polygons = 1;
|
||||
uint n_polygons = 3;
|
||||
|
||||
void polygons_init_state() {
|
||||
polygons = new polygon[n_polygons];
|
||||
|
||||
polygon poly =
|
||||
polygon{{400, 400}, 0, {{-50, -50}, {50, -50}, {50, 50}, {-50, 50}}};
|
||||
polygons[0] = poly;
|
||||
polygons[0] = poly_generate::triangle(200, 200, 30).set_center({300, 200});
|
||||
polygons[1] = poly_generate::square(200).set_center({100, 400});
|
||||
polygons[2] = poly_generate::rectangle(200, 100).set_center({400, 400});
|
||||
}
|
||||
|
||||
void polygon::draw(cairo_t* cr) const {
|
||||
polygon& poly = polygons[0];
|
||||
vec2d& center = poly.center;
|
||||
const vec2d& center = this->center;
|
||||
cairo_set_source_rgb(cr, 1, 1, 1);
|
||||
|
||||
for (auto& point : poly.points)
|
||||
for (auto& point : this->points)
|
||||
cairo_line_to(cr, center.x + point.x, center.y + point.y);
|
||||
cairo_line_to(cr, center.x + poly.points[0].x, center.y + poly.points[0].y);
|
||||
cairo_line_to(
|
||||
cr, center.x + this->points[0].x, center.y + this->points[0].y);
|
||||
cairo_stroke(cr);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user