Refactoring.
This commit is contained in:
parent
692e9de011
commit
647ca6084c
6 changed files with 193 additions and 127 deletions
103
src/GameMap.cpp
103
src/GameMap.cpp
|
@ -1,123 +1,140 @@
|
||||||
#include "GameMap.hpp"
|
#include "GameMap.hpp"
|
||||||
|
|
||||||
bool operator==(Field left, Field right) {
|
bool operator==(Field left, Field right)
|
||||||
|
{
|
||||||
return left.x == right.x && left.y == right.y;
|
return left.x == right.x && left.y == right.y;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator!=(Field left, Field right) {
|
bool operator!=(Field left, Field right)
|
||||||
|
{
|
||||||
return left == right;
|
return left == right;
|
||||||
}
|
}
|
||||||
|
|
||||||
Field operator+(Field left, Field right) {
|
Field operator+(Field left, Field right)
|
||||||
|
{
|
||||||
return Field(left.x + right.x, left.y + right.y, left.z + right.z);
|
return Field(left.x + right.x, left.y + right.y, left.z + right.z);
|
||||||
}
|
}
|
||||||
|
|
||||||
Field operator-(Field left, Field right) {
|
Field operator-(Field left, Field right)
|
||||||
|
{
|
||||||
return Field(left.x - right.x, left.y - right.y, left.z - right.z);
|
return Field(left.x - right.x, left.y - right.y, left.z - right.z);
|
||||||
}
|
}
|
||||||
|
|
||||||
Field operator*(Field left, Field right) {
|
Field operator*(Field left, Field right)
|
||||||
|
{
|
||||||
return Field(left.x * right.x, left.y * right.y, left.z * right.z);
|
return Field(left.x * right.x, left.y * right.y, left.z * right.z);
|
||||||
}
|
}
|
||||||
|
|
||||||
Field operator/(Field left, Field right) {
|
Field operator/(Field left, Field right)
|
||||||
|
{
|
||||||
return cubic_round(left.x / right.x, left.y / right.y, right.z / left.z);
|
return cubic_round(left.x / right.x, left.y / right.y, right.z / left.z);
|
||||||
}
|
}
|
||||||
|
|
||||||
Field cubic_round(double x, double y, double z) {
|
Field cubic_round(double x, double y, double z)
|
||||||
|
{
|
||||||
int8_t round_x = (int8_t) std::round(x);
|
int8_t round_x = (int8_t) std::round(x);
|
||||||
int8_t round_y = (int8_t) std::round(y);
|
int8_t round_y = (int8_t) std::round(y);
|
||||||
int8_t round_z = (int8_t) std::round(z);
|
int8_t round_z = (int8_t) std::round(z);
|
||||||
double x_err = std::abs(round_x - x);
|
double x_err = std::abs(round_x - x);
|
||||||
double y_err = std::abs(round_y - y);
|
double y_err = std::abs(round_y - y);
|
||||||
double z_err = std::abs(round_z - z);
|
double z_err = std::abs(round_z - z);
|
||||||
if (x_err > y_err && x_err > z_err) {
|
if (x_err > y_err && x_err > z_err)
|
||||||
|
{
|
||||||
round_x = -round_y - round_z;
|
round_x = -round_y - round_z;
|
||||||
} else if (y_err > z_err) {
|
} else if (y_err > z_err)
|
||||||
|
{
|
||||||
round_y = -round_x - round_z;
|
round_y = -round_x - round_z;
|
||||||
} else {
|
} else
|
||||||
|
{
|
||||||
round_z = -round_x - round_y;
|
round_z = -round_x - round_y;
|
||||||
}
|
}
|
||||||
return Field(round_x, round_y, round_z);
|
return Field(round_x, round_y, round_z);
|
||||||
}
|
}
|
||||||
|
|
||||||
int cubic_distance(Field a, Field b) {
|
int cubic_distance(Field a, Field b)
|
||||||
|
{
|
||||||
return (abs(a.x - b.x) + abs(a.y - b.y) + abs(a.z - b.z)) / 2;
|
return (abs(a.x - b.x) + abs(a.y - b.y) + abs(a.z - b.z)) / 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
Field hex_direction(int direction) {
|
Field hex_direction(int direction)
|
||||||
|
{
|
||||||
assert (0 <= direction && direction <= 5);
|
assert (0 <= direction && direction <= 5);
|
||||||
return hex_directions[direction];
|
return hex_directions[direction];
|
||||||
}
|
}
|
||||||
|
|
||||||
Field hex_neighbor(int8_t direction, Field f) {
|
Field hex_neighbor(int8_t direction, Field f)
|
||||||
|
{
|
||||||
return hex_direction(direction) + f;
|
return hex_direction(direction) + f;
|
||||||
}
|
}
|
||||||
|
|
||||||
Point field_to_point(const Field *f, const Layout *layout) {
|
Point field_to_point(const Field f, const Layout layout)
|
||||||
const Orientation m = layout->orientation;
|
{
|
||||||
double x = (m.f0 * f->x + m.f1 * f->y) * layout->size;
|
const Orientation m = layout.orientation;
|
||||||
double y = (m.f2 * f->x + m.f3 * f->y) * layout->size;
|
double x = (m.f0 * f.x + m.f1 * f.y) * layout.size;
|
||||||
return Point(x + layout->origin.x, y + layout->origin.y);
|
double y = (m.f2 * f.x + m.f3 * f.y) * layout.size;
|
||||||
|
return Point(x + layout.origin.x, y + layout.origin.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
Field point_to_field(Point point, const Layout *layout) {
|
Field point_to_field(Point point, const Layout layout)
|
||||||
const Orientation m = layout->orientation;
|
{
|
||||||
double rel_x = (point.x - layout->origin.x) / layout->size;
|
const Orientation m = layout.orientation;
|
||||||
double rel_y = (point.y - layout->origin.y) / layout->size;
|
double rel_x = (point.x - layout.origin.x) / layout.size;
|
||||||
|
double rel_y = (point.y - layout.origin.y) / layout.size;
|
||||||
double x = m.b0 * rel_x + m.b1 * rel_y;
|
double x = m.b0 * rel_x + m.b1 * rel_y;
|
||||||
double y = m.b2 * rel_x + m.b3 * rel_y;
|
double y = m.b2 * rel_x + m.b3 * rel_y;
|
||||||
return cubic_round(x, y, -x - y);
|
return cubic_round(x, y, -x - y);
|
||||||
}
|
}
|
||||||
|
|
||||||
Point field_corner_offset(uint8_t corner, const Layout *layout) {
|
Point field_corner_offset(uint8_t corner, const Layout layout)
|
||||||
double angle = 2.0 * M_PI * (corner + layout->orientation.start_angle) / 6;
|
{
|
||||||
return Point(layout->size * cos(angle), layout->size * sin(angle));
|
double angle = 2.0 * M_PI * (corner + layout.orientation.start_angle) / 6;
|
||||||
|
return Point(layout.size * cos(angle), layout.size * sin(angle));
|
||||||
//uint8_t i = (uint8_t) ((corner + layout->orientation.start_corner) % 6);
|
|
||||||
//return Point(layout->size * COS_ANGLES_HEX[i], layout->size * SIN_ANGLES_HEX[i]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<Point> field_to_polygon(const Field *field, const Layout *layout) {
|
std::vector<Point> field_to_polygon(const Field field, const Layout layout)
|
||||||
|
{
|
||||||
std::vector<Point> corners = {};
|
std::vector<Point> corners = {};
|
||||||
Point center = field_to_point(field, layout);
|
Point center = field_to_point(field, layout);
|
||||||
for (uint8_t i = 0; i < 6; i++) {
|
for (uint8_t i = 0; i < 6; i++)
|
||||||
|
{
|
||||||
Point offset = field_corner_offset(i, layout);
|
Point offset = field_corner_offset(i, layout);
|
||||||
corners.push_back(Point(center.x + offset.x, center.y + offset.y));
|
corners.push_back(Point(center.x + offset.x, center.y + offset.y));
|
||||||
}
|
}
|
||||||
return corners;
|
return corners;
|
||||||
}
|
}
|
||||||
|
|
||||||
Grid::Grid() {
|
HexagonGrid::HexagonGrid(Sint16 grid_radius, Layout layout, SDL_Color color)
|
||||||
this->fields = std::unordered_set<Field>();
|
: Grid(layout, color)
|
||||||
}
|
{
|
||||||
|
|
||||||
HexagonGrid::HexagonGrid(Sint16 grid_radius)
|
|
||||||
: Grid() {
|
|
||||||
// first lower half, then upper half
|
// first lower half, then upper half
|
||||||
for (Sint16 x = -grid_radius; x <= grid_radius; x++) {
|
for (Sint16 x = -grid_radius; x <= grid_radius; x++)
|
||||||
|
{
|
||||||
Sint16 y_l = (-grid_radius > -x - grid_radius) ? -grid_radius : -x - grid_radius;
|
Sint16 y_l = (-grid_radius > -x - grid_radius) ? -grid_radius : -x - grid_radius;
|
||||||
Sint16 y_u = (grid_radius < -x + grid_radius) ? grid_radius : -x + grid_radius;
|
Sint16 y_u = (grid_radius < -x + grid_radius) ? grid_radius : -x + grid_radius;
|
||||||
for (Sint16 y = y_l; y <= y_u; y++) {
|
for (Sint16 y = y_l; y <= y_u; y++)
|
||||||
|
{
|
||||||
Sint16 z = -x - y;
|
Sint16 z = -x - y;
|
||||||
this->fields.insert({x, y, z});
|
this->fields.insert({x, y, z});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool HexagonGrid::render(SDL_Renderer *renderer, const SDL_Color color, const Layout *layout) {
|
bool HexagonGrid::render(SDL_Renderer *renderer)
|
||||||
for (const Field &elem : this->fields) {
|
{
|
||||||
std::vector<Point> polygon = field_to_polygon(&elem, layout);
|
for (const Field &elem : this->fields)
|
||||||
|
{
|
||||||
|
std::vector<Point> polygon = field_to_polygon(elem, this->layout);
|
||||||
assert(polygon.size() > 5);
|
assert(polygon.size() > 5);
|
||||||
Sint16 vx[6];
|
Sint16 vx[6];
|
||||||
Sint16 vy[6];
|
Sint16 vy[6];
|
||||||
for (uint8_t i = 0; i < 6; i++) {
|
for (uint8_t i = 0; i < 6; i++)
|
||||||
|
{
|
||||||
vx[i] = (Sint16) polygon[i].x;
|
vx[i] = (Sint16) polygon[i].x;
|
||||||
vy[i] = (Sint16) polygon[i].y;
|
vy[i] = (Sint16) polygon[i].y;
|
||||||
}
|
}
|
||||||
const Sint16 x[6] = {vx[0], vx[1], vx[2], vx[3], vx[4], vx[5]};
|
const Sint16 x[6] = {vx[0], vx[1], vx[2], vx[3], vx[4], vx[5]};
|
||||||
const Sint16 y[6] = {vy[0], vy[1], vy[2], vy[3], vy[4], vy[5]};
|
const Sint16 y[6] = {vy[0], vy[1], vy[2], vy[3], vy[4], vy[5]};
|
||||||
aapolygonRGBA(renderer, x, y, 6, color.r, color.g, color.b, color.a);
|
aapolygonRGBA(renderer, x, y, 6, this->color.r, this->color.g, this->color.b, this->color.a);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,18 +8,21 @@
|
||||||
|
|
||||||
#ifndef Point
|
#ifndef Point
|
||||||
|
|
||||||
struct Point {
|
struct Point
|
||||||
|
{
|
||||||
double x;
|
double x;
|
||||||
double y;
|
double y;
|
||||||
|
|
||||||
Point(double x_, double y_) : x(x_), y(y_) { }
|
Point(double x_, double y_) : x(x_), y(y_)
|
||||||
|
{ }
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef Orientation
|
#ifndef Orientation
|
||||||
|
|
||||||
struct Orientation {
|
struct Orientation
|
||||||
|
{
|
||||||
// cubic to point
|
// cubic to point
|
||||||
const double f0, f1, f2, f3;
|
const double f0, f1, f2, f3;
|
||||||
// point to cubic
|
// point to cubic
|
||||||
|
@ -30,7 +33,8 @@ struct Orientation {
|
||||||
Orientation(double f0_, double f1_, double f2_, double f3_, double b0_, double b1_, double b2_, double b3_,
|
Orientation(double f0_, double f1_, double f2_, double f3_, double b0_, double b1_, double b2_, double b3_,
|
||||||
double start_angle_)
|
double start_angle_)
|
||||||
: f0(f0_), f1(f1_), f2(f2_), f3(f3_), b0(b0_), b1(b1_), b2(b2_), b3(b3_),
|
: f0(f0_), f1(f1_), f2(f2_), f3(f3_), b0(b0_), b1(b1_), b2(b2_), b3(b3_),
|
||||||
start_angle(start_angle_) { }
|
start_angle(start_angle_)
|
||||||
|
{ }
|
||||||
};
|
};
|
||||||
|
|
||||||
const Orientation pointy_orientation = Orientation(sqrt(3.0), sqrt(3.0) / 2.0, 0.0, 3.0 / 2.0,
|
const Orientation pointy_orientation = Orientation(sqrt(3.0), sqrt(3.0) / 2.0, 0.0, 3.0 / 2.0,
|
||||||
|
@ -43,23 +47,27 @@ const Orientation flat_orientation = Orientation(3.0 / 2.0, 0.0, sqrt(3.0) / 2.0
|
||||||
|
|
||||||
#ifndef Layout
|
#ifndef Layout
|
||||||
|
|
||||||
struct Layout {
|
struct Layout
|
||||||
|
{
|
||||||
const Orientation orientation;
|
const Orientation orientation;
|
||||||
const double size;
|
const double size;
|
||||||
const SDL_Point origin;
|
const SDL_Point origin;
|
||||||
|
|
||||||
Layout(Orientation orientation_, double size_, SDL_Point origin_)
|
Layout(Orientation orientation_, double size_, SDL_Point origin_)
|
||||||
: orientation(orientation_), size(size_), origin(origin_) { }
|
: orientation(orientation_), size(size_), origin(origin_)
|
||||||
|
{ }
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef Field
|
#ifndef Field
|
||||||
|
|
||||||
struct Field {
|
struct Field
|
||||||
|
{
|
||||||
const Sint16 x, y, z;
|
const Sint16 x, y, z;
|
||||||
|
|
||||||
Field(Sint16 x_, Sint16 y_, Sint16 z_) : x(x_), y(y_), z(z_) {
|
Field(Sint16 x_, Sint16 y_, Sint16 z_) : x(x_), y(y_), z(z_)
|
||||||
|
{
|
||||||
assert(x + y + z == 0);
|
assert(x + y + z == 0);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -69,10 +77,13 @@ const std::vector<Field> hex_directions = {
|
||||||
Field(1, 0, -1), Field(0, 1, -1), Field(-1, 1, 0), Field(-1, 0, 1), Field(-1, 1, 0)
|
Field(1, 0, -1), Field(0, 1, -1), Field(-1, 1, 0), Field(-1, 0, 1), Field(-1, 1, 0)
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace std {
|
namespace std
|
||||||
|
{
|
||||||
template<>
|
template<>
|
||||||
struct hash<Field> {
|
struct hash<Field>
|
||||||
size_t operator()(const Field &f) const {
|
{
|
||||||
|
size_t operator()(const Field &f) const
|
||||||
|
{
|
||||||
hash<Sint16> int_hash;
|
hash<Sint16> int_hash;
|
||||||
size_t hx = int_hash(f.x);
|
size_t hx = int_hash(f.x);
|
||||||
size_t hy = int_hash(f.y);
|
size_t hy = int_hash(f.y);
|
||||||
|
@ -99,35 +110,43 @@ Field operator*(Field left, Field right);
|
||||||
|
|
||||||
Field operator/(Field left, Field right);
|
Field operator/(Field left, Field right);
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Point field_to_point(const Field *f, const Layout *layout);
|
Point field_to_point(const Field f, const Layout layout);
|
||||||
|
|
||||||
Field point_to_field(Point point, const Layout *layout);
|
Field point_to_field(Point point, const Layout layout);
|
||||||
|
|
||||||
Point field_corner_offset(Uint8 corner, const Layout *layout);
|
Point field_corner_offset(Uint8 corner, const Layout layout);
|
||||||
|
|
||||||
std::vector<Point> field_to_polygon(const Field *field, const Layout *layout);
|
std::vector<Point> field_to_polygon(const Field field, const Layout layout);
|
||||||
|
|
||||||
#ifndef Grid
|
#ifndef Grid
|
||||||
|
|
||||||
class Grid {
|
class Grid
|
||||||
|
{
|
||||||
protected:
|
protected:
|
||||||
std::unordered_set<Field> fields;
|
std::unordered_set<Field> fields;
|
||||||
|
SDL_Color color;
|
||||||
|
Layout layout;
|
||||||
public:
|
public:
|
||||||
Grid();
|
Grid(Layout layout_, SDL_Color color_) : layout(layout_), color(color_)
|
||||||
|
{
|
||||||
|
this->fields = std::unordered_set<Field>();
|
||||||
|
};
|
||||||
|
|
||||||
|
virtual bool render(SDL_Renderer *renderer) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef HexagonGrid
|
#ifndef HexagonGrid
|
||||||
|
|
||||||
class HexagonGrid : public Grid {
|
class HexagonGrid : public Grid
|
||||||
|
{
|
||||||
public:
|
public:
|
||||||
HexagonGrid(Sint16 grid_radius);
|
HexagonGrid(Sint16 grid_radius, Layout layout, SDL_Color color);
|
||||||
|
|
||||||
bool render(SDL_Renderer *renderer, const SDL_Color color, const Layout *layout);
|
bool render(SDL_Renderer *renderer);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
113
src/Main.cpp
113
src/Main.cpp
|
@ -1,15 +1,19 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include "Main.hpp"
|
#include "Main.hpp"
|
||||||
|
|
||||||
void logSDLError(std::ostream &os, const std::string &msg) {
|
|
||||||
|
void logSDLError(std::ostream &os, const std::string &msg)
|
||||||
|
{
|
||||||
os << msg << " error:" << SDL_GetError() << std::endl;
|
os << msg << " error:" << SDL_GetError() << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_Window *init_window() {
|
SDL_Window *init_window()
|
||||||
|
{
|
||||||
SDL_Window *window = SDL_CreateWindow("Hello World!", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
|
SDL_Window *window = SDL_CreateWindow("Hello World!", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
|
||||||
SCREEN_WIDTH,
|
SCREEN_WIDTH,
|
||||||
SCREEN_HEIGHT, SDL_WINDOW_OPENGL);
|
SCREEN_HEIGHT, SDL_WINDOW_OPENGL);
|
||||||
if (window == NULL) {
|
if (window == NULL)
|
||||||
|
{
|
||||||
logSDLError(std::cout, "SDL_CreateWindow");
|
logSDLError(std::cout, "SDL_CreateWindow");
|
||||||
SDL_DestroyWindow(window);
|
SDL_DestroyWindow(window);
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
|
@ -17,88 +21,89 @@ SDL_Window *init_window() {
|
||||||
return window;
|
return window;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_Renderer *init_renderer(SDL_Window *window) {
|
SDL_Renderer *init_renderer(SDL_Window *window)
|
||||||
|
{
|
||||||
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
||||||
if (renderer == NULL) {
|
if (renderer == NULL)
|
||||||
|
{
|
||||||
logSDLError(std::cout, "SDL_CreateRenderer");
|
logSDLError(std::cout, "SDL_CreateRenderer");
|
||||||
|
|
||||||
}
|
}
|
||||||
return renderer;
|
return renderer;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_Surface *init_surface(int width, int height) {
|
SDL_Surface *init_surface(int width, int height)
|
||||||
|
{
|
||||||
|
|
||||||
//SDL_Surface *surface = SDL_CreateRGBSurface(0, width, height, 32, rmask, gmask, bmask, amask);
|
//SDL_Surface *surface = SDL_CreateRGBSurface(0, width, height, 32, rmask, gmask, bmask, amask);
|
||||||
SDL_Surface *surface = SDL_CreateRGBSurface(0, width, height, 32, 0, 0, 0, 0);
|
SDL_Surface *surface = SDL_CreateRGBSurface(0, width, height, 32, 0, 0, 0, 0);
|
||||||
if (surface == NULL) {
|
if (surface == NULL)
|
||||||
|
{
|
||||||
logSDLError(std::cout, "SDL_CreateSurface");
|
logSDLError(std::cout, "SDL_CreateSurface");
|
||||||
}
|
}
|
||||||
return surface;
|
return surface;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_Texture *init_texture(SDL_Renderer *renderer, SDL_Surface *surface) {
|
SDL_Texture *init_texture(SDL_Renderer *renderer, SDL_Surface *surface)
|
||||||
|
{
|
||||||
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
|
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
|
||||||
if (texture == NULL) {
|
if (texture == NULL)
|
||||||
|
{
|
||||||
logSDLError(std::cout, "SDL_CreateTexture");
|
logSDLError(std::cout, "SDL_CreateTexture");
|
||||||
}
|
}
|
||||||
return texture;
|
return texture;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int, char **) {
|
int Game::game_loop()
|
||||||
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
|
{
|
||||||
logSDLError(std::cout, "SDL_init");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
SDL_Window *window = init_window();
|
|
||||||
if (!window) {
|
|
||||||
SDL_DestroyWindow(window);
|
|
||||||
SDL_Quit();
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
SDL_Renderer *renderer = init_renderer(window);
|
|
||||||
if (!renderer) {
|
|
||||||
SDL_DestroyRenderer(renderer);
|
|
||||||
SDL_DestroyWindow(window);
|
|
||||||
SDL_Quit();
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
SDL_Surface *surface = init_surface(SCREEN_WIDTH, SCREEN_HEIGHT);
|
|
||||||
if (!surface) {
|
|
||||||
SDL_FreeSurface(surface);
|
|
||||||
SDL_DestroyRenderer(renderer);
|
|
||||||
SDL_DestroyWindow(window);
|
|
||||||
SDL_Quit();
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
SDL_Texture *texture = init_texture(renderer, surface);
|
|
||||||
if (!texture) {
|
|
||||||
SDL_DestroyTexture(texture);
|
|
||||||
SDL_FreeSurface(surface);
|
|
||||||
SDL_DestroyRenderer(renderer);
|
|
||||||
SDL_DestroyWindow(window);
|
|
||||||
SDL_Quit();
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
const SDL_Point center = {SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2};
|
|
||||||
const Layout standard = Layout(pointy_orientation, 20.0, center);
|
|
||||||
const SDL_Color color = {0, 255, 255, 255};
|
|
||||||
HexagonGrid *grid = new HexagonGrid(4);
|
|
||||||
bool quit = false;
|
bool quit = false;
|
||||||
while (!quit) {
|
while (!quit)
|
||||||
|
{
|
||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
while (SDL_PollEvent(&event)) {
|
while (SDL_PollEvent(&event))
|
||||||
if (event.type == SDL_QUIT) {
|
{
|
||||||
|
if (event.type == SDL_QUIT)
|
||||||
|
{
|
||||||
quit = true;
|
quit = true;
|
||||||
} else if (event.type == SDL_MOUSEBUTTONDOWN) {
|
} else if (event.type == SDL_MOUSEBUTTONDOWN)
|
||||||
|
{
|
||||||
quit = true;
|
quit = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00);
|
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00);
|
||||||
SDL_RenderClear(renderer);
|
SDL_RenderClear(renderer);
|
||||||
SDL_SetRenderDrawColor(renderer, 0xff, 0xff, 0xff, 0xff);
|
SDL_SetRenderDrawColor(renderer, 0xff, 0xff, 0xff, 0xff);
|
||||||
grid->render(renderer, color, &standard);
|
this->grid->render(renderer);
|
||||||
SDL_RenderPresent(renderer);
|
SDL_RenderPresent(renderer);
|
||||||
}
|
}
|
||||||
delete grid;
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int main(int, char **)
|
||||||
|
{
|
||||||
|
if (SDL_Init(SDL_INIT_VIDEO) != 0)
|
||||||
|
{
|
||||||
|
logSDLError(std::cout, "SDL_init");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
SDL_Window *window = init_window();
|
||||||
|
if (!window)
|
||||||
|
{
|
||||||
|
SDL_DestroyWindow(window);
|
||||||
|
SDL_Quit();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
SDL_Renderer *renderer = init_renderer(window);
|
||||||
|
if (!renderer)
|
||||||
|
{
|
||||||
|
SDL_DestroyRenderer(renderer);
|
||||||
|
SDL_DestroyWindow(window);
|
||||||
|
SDL_Quit();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
Game *game = new Game(window, renderer);
|
||||||
|
int exit_status = game->game_loop();
|
||||||
|
delete game;
|
||||||
|
return exit_status;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
31
src/Main.hpp
31
src/Main.hpp
|
@ -3,13 +3,13 @@
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
#include "GameMap.hpp"
|
#include "GameMap.hpp"
|
||||||
|
|
||||||
#ifndef DATA_PATH
|
#ifndef DEFAULTS
|
||||||
#endif
|
|
||||||
#ifndef SCREEN_WIDTH
|
|
||||||
const int SCREEN_WIDTH = 800;
|
const int SCREEN_WIDTH = 800;
|
||||||
#endif
|
|
||||||
#ifndef SCREEN_HEIGHT
|
|
||||||
const int SCREEN_HEIGHT = 800;
|
const int SCREEN_HEIGHT = 800;
|
||||||
|
const SDL_Point CENTER = {SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2};
|
||||||
|
const Layout LAYOUT = Layout(pointy_orientation, 20.0, CENTER);
|
||||||
|
const Sint16 GRID_SIZE = 6;
|
||||||
|
const SDL_Color COLOR = {0, 255, 255, 255};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
|
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
|
||||||
|
@ -25,3 +25,24 @@ Uint32 amask = 0xff000000;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
//template <class T>
|
||||||
|
class Game
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
SDL_Window *window;
|
||||||
|
SDL_Renderer *renderer;
|
||||||
|
HexagonGrid *grid;
|
||||||
|
public:
|
||||||
|
Game(SDL_Window *window_, SDL_Renderer *renderer_)
|
||||||
|
: window(window_), renderer(renderer_)
|
||||||
|
{
|
||||||
|
this->grid = new HexagonGrid(GRID_SIZE, LAYOUT, COLOR);
|
||||||
|
}
|
||||||
|
|
||||||
|
~Game()
|
||||||
|
{
|
||||||
|
delete this->grid;
|
||||||
|
}
|
||||||
|
|
||||||
|
int game_loop();
|
||||||
|
};
|
||||||
|
|
|
@ -2,17 +2,21 @@
|
||||||
|
|
||||||
#ifndef Upgrade
|
#ifndef Upgrade
|
||||||
|
|
||||||
class Upgrade {
|
class Upgrade
|
||||||
|
{
|
||||||
SDL_Color color;
|
SDL_Color color;
|
||||||
public:
|
public:
|
||||||
static Upgrade *Instance();
|
static Upgrade *Instance();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Upgrade() { };
|
Upgrade()
|
||||||
|
{ };
|
||||||
|
|
||||||
Upgrade(Upgrade const &) { };
|
Upgrade(Upgrade const &)
|
||||||
|
{ };
|
||||||
|
|
||||||
Upgrade &operator=(Upgrade const &) { };
|
Upgrade &operator=(Upgrade const &)
|
||||||
|
{ };
|
||||||
static Upgrade *m_pInstance;
|
static Upgrade *m_pInstance;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue