Skip to content

Commit 7ed1bae

Browse files
committed
dev
1 parent 3d59e15 commit 7ed1bae

File tree

13 files changed

+169
-55
lines changed

13 files changed

+169
-55
lines changed

src/game-field.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#include "game-field.h"
22
#include <cstring>
33

4-
GameField::GameField(int _fieldSize)
5-
: fieldSize(_fieldSize)
6-
, decks(std::vector< std::vector<bool>>(fieldSize, std::vector<bool>(fieldSize, false)))
4+
GameField::GameField(int _w, int _h)
5+
: w(_w)
6+
, h(_h)
7+
, decks(std::vector< std::vector<bool>>(h, std::vector<bool>(w, false)))
78
{
89
}
910

@@ -17,7 +18,7 @@ void GameField::addShip(int numberOfDecks, int sx, int sy, Direction dir)
1718
for (int i = 0; i < numberOfDecks; i++) {
1819
int x = sx + dx[(int)dir] * i;
1920
int y = sx + dx[(int)dir] * i;
20-
if (x < 0 || x >= fieldSize || y < 0 || y >= fieldSize) {
21+
if (x < 0 || x >= w || y < 0 || y >= h) {
2122
throw "Unknow position for ship adding";
2223
}
2324
decks[y][x] = true;
@@ -26,5 +27,18 @@ void GameField::addShip(int numberOfDecks, int sx, int sy, Direction dir)
2627

2728
bool GameField::isDeckOfShip(int x, int y)
2829
{
30+
if (x < 0 || x >= w || y < 0 || y >= h) {
31+
throw "Unknow position for deck checking";
32+
}
2933
return decks[y][x];
3034
}
35+
36+
int GameField::getWidth() const
37+
{
38+
return w;
39+
}
40+
41+
int GameField::getHeight() const
42+
{
43+
return h;
44+
}

src/game-field.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ enum class Direction {
77
};
88

99
class GameField {
10-
int fieldSize;
10+
int w, h;
1111
std::vector<std::vector<bool>> decks;
1212
public:
13-
GameField(int _fieldSize=10);
13+
GameField(int _w, int _h);
1414
void addShip(int numberOfDecks, int sx, int sy, Direction dir);
1515
bool isDeckOfShip(int x, int y);
16+
int getWidth() const;
17+
int getHeight() const;
1618
};

src/game.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
#include "game.h"
22

33
Game::Game()
4-
: playerField(), aiField()
4+
: playerField(10, 10)
5+
, aiField(10, 10)
56
{
67
}
78

89
void Game::startNewGame()
910
{
10-
playerField = GameField();
11-
aiField = GameField();
11+
playerField = GameField(10, 10);
12+
aiField = GameField(10, 10);
1213
}

src/sdl2/app.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
#include "grid-of-chars.h"
44

55
App::App()
6-
: font(nullptr), window(nullptr), renderer(nullptr)
6+
: font(nullptr)
7+
, window(nullptr)
8+
, renderer(nullptr)
9+
, screen(nullptr)
10+
, game()
11+
, fieldMakerScreen(this, game.playerField)
712
{}
813

914
void App::free() {
@@ -38,28 +43,35 @@ void App::init() {
3843
if (!renderer) {
3944
throw "Renderer creation failed";
4045
}
46+
47+
setScreen(&fieldMakerScreen);
4148
}
4249

4350
void App::run() {
44-
GridOfChars grid(5, 5, '#');
4551
while (true)
4652
{
4753
SDL_Event event;
4854
if (SDL_PollEvent(&event))
4955
{
56+
screen->onEvent(event);
5057
if (event.type == SDL_QUIT)
5158
{
5259
break;
5360
}
5461
}
5562
SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
5663
SDL_RenderClear(renderer);
57-
getDrawContext().drawText("Hello, World!", SDL_Color{255, 0, 255}, 0, 0);
58-
grid.draw(getDrawContext(), 50, 50);
64+
screen->update();
65+
screen->draw();
5966
SDL_RenderPresent(renderer);
6067
}
6168
}
6269

70+
void App::setScreen(Screen* _screen)
71+
{
72+
screen = _screen;
73+
}
74+
6375
DrawContext App::getDrawContext()
6476
{
6577
return DrawContext(this);

src/sdl2/app.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,25 @@
44
#include <SDL_ttf.h>
55
#include "../game.h"
66
#include "../game-field.h"
7+
#include "screens/screen.h"
8+
#include "screens/field-maker-screen.h"
79

810
class DrawContext;
911

1012

1113
class App {
14+
friend DrawContext;
1215
SDL_Renderer* renderer;
1316
TTF_Font* font;
1417
SDL_Window* window;
15-
Game game;
18+
Screen* screen;
1619
public:
17-
friend DrawContext;
20+
Game game;
21+
FieldMakerScreen fieldMakerScreen;
1822
App();
1923
void free();
2024
void init();
2125
void run();
26+
void setScreen(Screen* screen);
2227
DrawContext getDrawContext();
2328
};

src/sdl2/draw-context.cpp

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,47 @@ DrawContext::DrawContext(App* _app)
88

99
SDL_Surface* DrawContext::makeTextSurface(char* text, SDL_Color color)
1010
{
11-
if (!app->font) {
12-
throw "No font for make text surface.";
13-
}
14-
if (!text) {
15-
throw "No text for make text surface.";
16-
}
17-
SDL_Surface* surfaceMessage = TTF_RenderText_Blended(app->font, text, color);
18-
if (!surfaceMessage) {
19-
throw "Surface message creation failed";
20-
}
21-
return surfaceMessage;
11+
if (!app->font) {
12+
throw "No font for make text surface.";
13+
}
14+
if (!text) {
15+
throw "No text for make text surface.";
16+
}
17+
SDL_Surface* surfaceMessage = TTF_RenderText_Blended(app->font, text, color);
18+
if (!surfaceMessage) {
19+
throw "Surface message creation failed";
20+
}
21+
return surfaceMessage;
2222
}
2323

2424
SDL_Texture* DrawContext::createTextureFromSurface(SDL_Surface* surface)
2525
{
26-
if (!surface) {
27-
throw "No surface for making texture";
28-
}
29-
SDL_Texture* texture = SDL_CreateTextureFromSurface(app->renderer, surface);
30-
if (!texture) {
31-
throw "Texture creation failed";
32-
}
33-
return texture;
26+
if (!surface) {
27+
throw "No surface for making texture";
28+
}
29+
SDL_Texture* texture = SDL_CreateTextureFromSurface(app->renderer, surface);
30+
if (!texture) {
31+
throw "Texture creation failed";
32+
}
33+
return texture;
3434
}
3535

3636
SDL_Rect DrawContext::drawText(char* text, SDL_Color color, int x, int y)
3737
{
38-
SDL_Surface* surfaceMessage = makeTextSurface(text, color);
39-
SDL_Texture* Message = createTextureFromSurface(surfaceMessage);
38+
SDL_Surface* surfaceMessage = makeTextSurface(text, color);
39+
SDL_Texture* Message = createTextureFromSurface(surfaceMessage);
4040

41-
SDL_Rect Message_rect;
42-
Message_rect.x = x;
43-
Message_rect.y = y;
44-
Message_rect.w = surfaceMessage->w;
45-
Message_rect.h = surfaceMessage->h;
41+
SDL_Rect Message_rect;
42+
Message_rect.x = x;
43+
Message_rect.y = y;
44+
Message_rect.w = surfaceMessage->w;
45+
Message_rect.h = surfaceMessage->h;
4646

47-
SDL_RenderCopy(renderer, Message, NULL, &Message_rect);
4847

49-
SDL_FreeSurface(surfaceMessage);
50-
SDL_DestroyTexture(Message);
48+
SDL_RenderCopy(app->renderer, Message, NULL, &Message_rect);
5149

52-
return Message_rect;
50+
SDL_FreeSurface(surfaceMessage);
51+
SDL_DestroyTexture(Message);
52+
53+
return Message_rect;
5354
}

src/sdl2/game-field-view.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
#include "game-field-view.h"
22

3-
GameFieldView::GameFieldView(GameField _gameField)
3+
GameFieldView::GameFieldView(GameField* _gameField)
44
: gameField(_gameField)
5+
, grid(gameField->getWidth(), gameField->getHeight(), ' ')
56
{}
67

7-
8+
void GameFieldView::draw(DrawContext dc, int x, int y)
9+
{
10+
int w = gameField->getWidth();
11+
int h = gameField->getHeight();
12+
for (int y = 0; y < h; y++) {
13+
for (int x = 0; x < w; x++) {
14+
if (gameField->isDeckOfShip(x, y)) {
15+
grid.setChar('#');
16+
}
17+
}
18+
}
19+
grid.draw(dc, x, y);
20+
}

src/sdl2/game-field-view.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
#pragma once
22

33
#include "../game-field.h"
4+
#include "grid-of-chars.h"
5+
#include "draw-context.h"
46

57
class GameFieldView {
6-
GameField gameField;
8+
GameField *gameField;
9+
GridOfChars grid;
710
public:
8-
GameFieldView(GameField _gameField);
9-
void draw(int x, int y);
11+
GameFieldView(GameField *_gameField);
12+
void draw(DrawContext dc, int x, int y);
1013
};

src/sdl2/grid-of-chars.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,15 @@ void GridOfChars::draw(DrawContext dc, int sx, int sy)
4343
int maxW = 0, maxH = 0;
4444
for (int y = 0; y < h; y++) {
4545
for (int x = 0; x < w; x++) {
46-
maxW = std::max(maxW, surfaces[y][x]->w);
47-
maxH = std::max(maxH, surfaces[y][x]->h);
46+
maxW = std::max(maxW, surfaces[y][x] ? surfaces[y][x]->w : 0);
47+
maxH = std::max(maxH, surfaces[y][x] ? surfaces[y][x]->h : 0);
4848
}
4949
}
50-
int cellSize = std::max(maxW, maxH);
50+
cellSize = std::max(maxW, maxH);
51+
viewRect.x = sx;
52+
viewRect.y = sy;
53+
viewRect.w = cellSize * w;
54+
viewRect.h = cellSize * h;
5155
SDL_SetRenderDrawColor(dc.renderer, 255, 255, 255, SDL_ALPHA_OPAQUE);
5256
for (int y = 0; y <= h; y++) {
5357
SDL_RenderDrawLine(dc.renderer, sx, sy + cellSize*y, sx + cellSize * w, sy + cellSize * y);
@@ -68,12 +72,17 @@ void GridOfChars::draw(DrawContext dc, int sx, int sy)
6872
}
6973
}
7074

75+
SDL_Rect GridOfChars::getViewRect()
76+
{
77+
return viewRect;
78+
}
79+
7180
void GridOfChars::makeSurfaces(DrawContext dc)
7281
{
7382
for (int y = 0; y < h; y++) {
7483
for (int x = 0; x < w; x++) {
7584
char buf[] = { chars[y][x], '\0' };
76-
if (renderedChars[y][x] == buf[0]) {
85+
if (renderedChars[y][x] == chars[y][x]) {
7786
continue;
7887
}
7988
if (surfaces[y][x]) {
@@ -86,7 +95,7 @@ void GridOfChars::makeSurfaces(DrawContext dc)
8695
}
8796
surfaces[y][x] = dc.makeTextSurface(buf, SDL_Color{ 255, 255,255 });
8897
textures[y][x] = dc.createTextureFromSurface(surfaces[y][x]);
89-
renderedChars[y][x] = buf[0];
98+
renderedChars[y][x] = chars[y][x];
9099
}
91100
}
92101
}

src/sdl2/grid-of-chars.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
class GridOfChars {
99
int w, h;
10+
SDL_Rect viewRect;
1011
int cellSize = 0;
1112
std::vector<std::vector<char>> chars;
1213
std::vector<std::vector<char>> renderedChars;
@@ -19,4 +20,5 @@ class GridOfChars {
1920
void setChar(int x, int y, char ch);
2021
void free();
2122
void draw(DrawContext dc, int x, int y);
23+
SDL_Rect getViewRect();
2224
};

0 commit comments

Comments
 (0)