-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoardTextures.cpp
More file actions
132 lines (117 loc) · 4.17 KB
/
Copy pathBoardTextures.cpp
File metadata and controls
132 lines (117 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "BoardTextures.h"
/// \brief Loads all the textures from .png
///
/// \param renderer The rendering context provided by Game class
/// \param boardSize diameter of the board 19, 13 or 9
///
BoardTextures::BoardTextures(SDL_Renderer *const renderer, int boardSize):
renderer(renderer),
boardSurface(NULL),
boardTexture(NULL),
bStoneSurface(NULL),
bStoneTexture(NULL),
wStoneSurface(NULL),
wStoneTexture(NULL),
boardTexSize(0),
stoneTexSize(0)
{
boardSurface = IMG_Load("goBoard.png");
if(boardSurface == NULL) throw GameException("Board image loading error.");
boardTexture = SDL_CreateTextureFromSurface(renderer, boardSurface);
boardTexSize = boardSurface->w;
SDL_FreeSurface(boardSurface);
boardSurface = NULL;
bStoneSurface = IMG_Load("blackstone.png");
if(bStoneSurface == NULL) throw GameException("Black stone image loading error.");
bStoneTexture = SDL_CreateTextureFromSurface(renderer, bStoneSurface);
stoneTexSize = bStoneSurface->w;
SDL_FreeSurface(bStoneSurface);
bStoneSurface = NULL;
wStoneSurface = IMG_Load("whitestone.png");
if(wStoneSurface == NULL) throw GameException("White stone image loading error.");
wStoneTexture = SDL_CreateTextureFromSurface(renderer, wStoneSurface);
SDL_FreeSurface(wStoneSurface);
wStoneSurface = NULL;
SDL_SetTextureBlendMode(boardTexture, SDL_BLENDMODE_BLEND);
SDL_SetTextureBlendMode(bStoneTexture, SDL_BLENDMODE_BLEND);
SDL_SetTextureBlendMode(wStoneTexture, SDL_BLENDMODE_BLEND);
topLeftCorners.reserve(boardSize*boardSize);
int i,j;
for(i = 0, j = 0; i < boardSize; i++)
{
for(j = 0; j < boardSize; j++)
{
topLeftCorners[i*boardSize + j].col = i*stoneTexSize;
topLeftCorners[i*boardSize + j].row = j*stoneTexSize;
}
}
renderBlankBoard();
SDL_RenderPresent(renderer);
return;
}
BoardTextures::~BoardTextures()
{
SDL_DestroyTexture(boardTexture);
SDL_DestroyTexture(bStoneTexture);
SDL_DestroyTexture(wStoneTexture);
}
/// \brief Renders textures according to boardposition.
/// Also, it renders a transparent stone of next player's color to where the mouse points
///
void BoardTextures::render(const Board& board)
{
renderBlankBoard();
SDL_Rect dst;
dst.w = dst.h = stoneTexSize;
const Point * const * const boardPointer = board.getBoard();
unsigned i, j;
for(i = 0, j = 0; i < board.getBoardSize(); i++)
for(j = 0; j < board.getBoardSize(); j++)
{
if(boardPointer[i][j] != EMPTY)
{
dst.x = topLeftCorners[i*board.getBoardSize() + j].col;
dst.y = topLeftCorners[i*board.getBoardSize() + j].row;
if(boardPointer[i][j] == BLACK)
{
SDL_RenderCopy(renderer, bStoneTexture, NULL, &dst);
}
else if(boardPointer[i][j] == WHITE)
{
SDL_RenderCopy(renderer, wStoneTexture, NULL, &dst);
}
}
}
//Rendering mouse pointer
int x, y;
SDL_GetMouseState(&x, &y);
int col = x / stoneTexSize;
int row = y / stoneTexSize;
if(board.isInRange(row, col))
{
dst.x = col * stoneTexSize; // we cut off the part over N*25, to get the top left corner of a square over an intersection
dst.y = row * stoneTexSize;
if(board.getTurn() == BLACK)
{
SDL_SetTextureAlphaMod(bStoneTexture, 0x80); // make it 50% transparent
SDL_RenderCopy(renderer, bStoneTexture, NULL, &dst);
SDL_SetTextureAlphaMod(bStoneTexture, 0xFF);
}
else if(board.getTurn() == WHITE)
{
SDL_SetTextureAlphaMod(wStoneTexture, 0x80);
SDL_RenderCopy(renderer, wStoneTexture, NULL, &dst);
SDL_SetTextureAlphaMod(wStoneTexture, 0xFF);
}
}
SDL_RenderPresent(renderer);
}
/// \brief Renders a board on screen without any stones.
///
void BoardTextures::renderBlankBoard()
{
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE);
SDL_RenderClear(renderer);
SDL_Rect dst = {0, 0, boardTexSize, boardTexSize};
SDL_RenderCopy(renderer, boardTexture, NULL, &dst);
}