diff --git a/lib/settings.h b/lib/settings.h index a5dc272..660f5e0 100644 --- a/lib/settings.h +++ b/lib/settings.h @@ -17,7 +17,12 @@ class Settings public: Settings( void ); - + point getUL( void ); + point getUR( void ); + point getBL( void ); + point getBR( void ); + int getWhiteTreshold( void ); + int getBlackTreshold( void ); private: //upper left, upper right etc... diff --git a/lib/state.cpp b/lib/state.cpp index c6455ef..775421b 100644 --- a/lib/state.cpp +++ b/lib/state.cpp @@ -1,13 +1,47 @@ #include "state.h" +#include int State::size = 19; State::State( void ) { - //does nothing yet + board = (type*)malloc(sizeof(type) * size * size); } -State generateState (SDL_Surface *board) +State State::generateState (SDL_Surface *boardImg, Settings settings) { - + State state; + int i, j, x, y, r, sum; + point p; + r = 20; + for (i = 0; i < size; i++) { + for (j = 0; j < size; j++) { + p = positionPoint(i, j, settings); + for (y = p.y-r; y < p.y+r; y++) { + for (x = p.x-r; x < p.x+r; x++) { + uint32_t px = PX(boardImg, x, y); + sum += PXR(px) + PXG(px) + PXB(px); + } + } + type t = BOARD; + if (sum < settings.getBlackTreshold()) t = BLACK; + if (sum > settings.getWhiteTreshold()) t = WHITE; + PX_(state.board, i, j, size, size) = t; + } + } + return state; } + +point State::positionPoint(int i, int j, Settings s) +{ + point ul = s.getUL(), ur = s.getUR(), bl = s.getBL(), br = s.getBR(); + point topp, botp, pp; + topp.x = ul.x + (i*(ur.x-ul.x))/size; + topp.y = ul.y + (i*(ur.y-ul.y))/size; + botp.x = bl.x + (i*(br.x-bl.x))/size; + botp.y = bl.y + (i*(br.y-bl.y))/size; + pp.x = topp.x + (j*(botp.x-topp.x))/size; + pp.y = topp.y + (j*(botp.y-topp.y))/size; + return pp; +} + diff --git a/lib/state.h b/lib/state.h index 8de4dd0..2c5971b 100644 --- a/lib/state.h +++ b/lib/state.h @@ -5,10 +5,18 @@ #include #include +// get pixel/something in array p of size w*h #define PX_(p, x, y, w, h) ((p)[(x)+(y)*(w)]) +// get pixel in SDL_Surface i #define PX(i,x,y) (PX_((uint32_t*)(i)->pixels, (x), (y), (i)->w, (i)->h)) +// get color components of pixel value. should probably be done in a nicer way +#define PXR(px) ((px) & 0xff) +#define PXG(px) (((px) & 0xff00) >> 8) +#define PXB(px) (((px) & 0xff0000) >> 16) +// iterate over SDL_Surface s with variables x, y #define iter_pixels(s,x,y) for (x = 0; x < s->w; x++) for (y = 0; y < s->h; y++) + /** * Describes the current state of a go board. */ @@ -28,7 +36,14 @@ class State private: + /* + * Get the image point corresponding to board position (i, j) using + * settings s. + */ + static point positionPoint(int i, int j, Settings s); + static int size; + type *board; // array describing the board situation };