Skip to content

Commit 8bb9d0a

Browse files
committed
init
0 parents  commit 8bb9d0a

File tree

198 files changed

+3864
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

198 files changed

+3864
-0
lines changed

CMakeLists.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
3+
project(app)
4+
set(CMAKE_CXX_STANDARD 17)
5+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
6+
7+
SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb -pg")
8+
SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")
9+
10+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
11+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
12+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
13+
14+
INCLUDE_DIRECTORIES(SFML-2.5.1/include)
15+
INCLUDE_DIRECTORIES(thirdparty)
16+
INCLUDE_DIRECTORIES(source)
17+
INCLUDE_DIRECTORIES(framework)
18+
INCLUDE_DIRECTORIES(./)
19+
20+
LINK_DIRECTORIES(SFML-2.5.1/lib)
21+
link_directories(/usr/local/lib)
22+
23+
add_subdirectory(framework)
24+
add_subdirectory(application)
25+
26+
message("finished ......................")

LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
zlib License
2+
3+
(C) 2022 ccsdu2004
4+
5+
This software is provided 'as-is', without any express or implied
6+
warranty. In no event will the authors be held liable for any damages
7+
arising from the use of this software.
8+
9+
Permission is granted to anyone to use this software for any purpose,
10+
including commercial applications, and to alter it and redistribute it
11+
freely, subject to the following restrictions:
12+
13+
1. The origin of this software must not be misrepresented; you must not
14+
claim that you wrote the original software. If you use this software
15+
in a product, an acknowledgment in the product documentation would be
16+
appreciated but is not required.
17+
2. Altered source versions must be plainly marked as such, and must not be
18+
misrepresented as being the original software.
19+
3. This notice may not be removed or altered from any source distribution.

application/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
add_subdirectory(chapter-1)
3+
add_subdirectory(chapter-2)
4+
add_subdirectory(chapter-3)
5+
add_subdirectory(chapter-4)
6+
add_subdirectory(chapter-5)
7+
add_subdirectory(chapter-6)
8+
add_subdirectory(chapter-7)
9+
add_subdirectory(chapter-8)

application/chapter-1/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
add_executable(chapter-1 main.cpp)
3+
target_link_libraries(chapter-1 framework sfml-system sfml-graphics sfml-window)

application/chapter-1/main.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include <iostream>
2+
#include <SFML/Graphics/RenderWindow.hpp>
3+
#include <Application.h>
4+
#include <Entity.h>
5+
6+
using namespace std;
7+
8+
class Unit : public Entity
9+
{
10+
public:
11+
Unit(const sf::Vector2f &size = sf::Vector2f()):
12+
Entity(size)
13+
{
14+
setOutlineColor(sf::Color::Yellow);
15+
setOutlineThickness(1.0f);
16+
setBackgroundColor(sf::Color::Green);
17+
}
18+
private:
19+
void onMouseEnter()
20+
{
21+
setBackgroundColor(sf::Color::Red);
22+
}
23+
24+
void onMouseExit()
25+
{
26+
setBackgroundColor(sf::Color::Green);
27+
}
28+
29+
void onMousePressed(sf::Mouse::Button button)
30+
{
31+
(void)button;
32+
setBackgroundColor(sf::Color::White);
33+
}
34+
35+
void onMouseReleased(sf::Mouse::Button button)
36+
{
37+
(void)button;
38+
setBackgroundColor(sf::Color::Black);
39+
}
40+
41+
void onMouseWheelScroll(float scroll)
42+
{
43+
(void)scroll;
44+
}
45+
46+
void onMouseMoved(int x, int y)
47+
{
48+
(void)x, (void)y;
49+
}
50+
};
51+
52+
int main()
53+
{
54+
auto window = std::make_shared<sf::RenderWindow>(sf::VideoMode(800, 600), "Chapter-1",
55+
sf::Style::Close);
56+
window->setVerticalSyncEnabled(true);
57+
58+
auto app = Application::getInstance();
59+
app->setBackgroundColor(sf::Color::Blue);
60+
app->setWindow(window);
61+
62+
auto object = std::make_shared<Object>();
63+
64+
int xoffset = 60;
65+
int yoffset = 10;
66+
67+
for (int i = 0; i < 10; i++)
68+
for (int j = 0; j < 18; j++) {
69+
auto item = std::make_shared<Unit>(sf::Vector2f(100, 36));
70+
item->setPosition(xoffset + i * 120, yoffset + j * 40);
71+
item->setRotate(30.0f);
72+
object->addChild(item);
73+
}
74+
75+
app->execute(object);
76+
return 0;
77+
}

application/chapter-2/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
add_executable(chapter-2 main.cpp)
3+
target_link_libraries(chapter-2 framework sfml-system sfml-graphics sfml-window)

application/chapter-2/main.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <iostream>
2+
#include <SFML/Graphics/RenderWindow.hpp>
3+
#include <Application.h>
4+
#include <Entity.h>
5+
#include <Text.h>
6+
7+
using namespace std;
8+
9+
std::shared_ptr<Text> createText(std::shared_ptr<sf::Font> font);
10+
11+
int main()
12+
{
13+
auto size = sf::Vector2f(800, 600);
14+
auto window = std::make_shared<sf::RenderWindow>(sf::VideoMode(size.x, size.y), "Chapter-2",
15+
sf::Style::Close);
16+
window->setVerticalSyncEnabled(true);
17+
18+
auto app = Application::getInstance();
19+
app->setBackgroundColor(sf::Color::Blue);
20+
app->setWindow(window);
21+
22+
auto object = std::make_shared<Object>();
23+
24+
auto font = std::make_shared<sf::Font>();
25+
font->loadFromFile("../resource/FZYTK.TTF");
26+
27+
auto text = createText(font);
28+
text->setPosition(400, 48);
29+
text->setText(L"这是一个文本对象", true);
30+
object->addChild(text);
31+
32+
text = createText(font);
33+
text->setStyle(sf::Text::Style::Regular);
34+
text->setPosition(400, 84);
35+
text->setCharacterSize(16);
36+
text->setText(L"又是一个文本对象", false);
37+
object->addChild(text);
38+
39+
app->execute(object);
40+
return 0;
41+
}
42+
43+
std::shared_ptr<Text> createText(std::shared_ptr<sf::Font> font)
44+
{
45+
auto text = std::make_shared<Text>();
46+
text->setFont(font);
47+
text->setCharacterSize(13);
48+
text->setTextColor(sf::Color::White);
49+
text->setSize(150, 32);
50+
text->setOutlineColor(sf::Color::Cyan);
51+
text->setOutlineThickness(1.0f);
52+
text->setBackgroundColor(sf::Color::Black);
53+
return text;
54+
}
55+

application/chapter-3/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
add_executable(chapter-3 main.cpp)
3+
target_link_libraries(chapter-3 framework sfml-system sfml-graphics sfml-window)

application/chapter-3/main.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <SFML/Graphics/RenderWindow.hpp>
2+
#include <Application.h>
3+
#include <Entity.h>
4+
#include <Text.h>
5+
#include <TileMap.h>
6+
7+
using namespace std;
8+
9+
int main()
10+
{
11+
auto size = sf::Vector2f(800, 600);
12+
auto setting = sf::ContextSettings();
13+
setting.antialiasingLevel = 12;
14+
auto window = std::make_shared<sf::RenderWindow>(sf::VideoMode(size.x, size.y), "Chapter-3",
15+
sf::Style::Close, setting);
16+
window->setVerticalSyncEnabled(true);
17+
18+
auto app = Application::getInstance();
19+
app->setBackgroundColor(sf::Color::Blue);
20+
app->setWindow(window);
21+
22+
auto tileMap = TileMap::createTileMap(TileMapType_Hex);
23+
tileMap->init(48, 36, 30);
24+
tileMap->setTextVisible(true);
25+
26+
auto list = tileMap->getAdjacentTileByPosition(8, 5);
27+
for (auto itr = list.begin(); itr != list.end(); itr++)
28+
tileMap->getTileByIndex(itr->x, itr->y)->setVisible(false);
29+
30+
app->execute(tileMap);
31+
return 0;
32+
}
33+
34+

application/chapter-4/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
add_executable(chapter-4 main.cpp)
3+
target_link_libraries(chapter-4 framework sfml-system sfml-graphics sfml-window)

application/chapter-4/main.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <iostream>
2+
#include <SFML/Graphics/RenderWindow.hpp>
3+
#include <Application.h>
4+
#include <Entity.h>
5+
#include <Text.h>
6+
#include <Sprite.h>
7+
#include <TileMap.h>
8+
9+
using namespace std;
10+
11+
std::shared_ptr<TileMap> tileMap;
12+
13+
std::shared_ptr<Object> createSprite(const std::string &image, float x, float y)
14+
{
15+
auto sprite = std::make_shared<Sprite>();
16+
sprite->setBackgroundColor(sf::Color::Red);
17+
sprite->setPosition(x, y);
18+
auto texture = Application::getInstance()->loadTexture(image);
19+
sprite->setTexture(*texture);
20+
return sprite;
21+
}
22+
23+
void clickedTile(int32_t x, int32_t y)
24+
{
25+
std::cout << "clicked tile:" << x << "," << y << std::endl;
26+
tileMap->getTileByIndex(x, y)->setFillColor(sf::Color::Red);
27+
}
28+
29+
int main()
30+
{
31+
auto size = sf::Vector2f(800, 600);
32+
auto window = std::make_shared<sf::RenderWindow>(sf::VideoMode(size.x, size.y), "Chapter-4",
33+
sf::Style::Close);
34+
window->setVerticalSyncEnabled(true);
35+
36+
auto app = Application::getInstance();
37+
app->setBackgroundColor(sf::Color::Black);
38+
app->setWindow(window);
39+
40+
auto object = std::make_shared<Object>();
41+
42+
tileMap = TileMap::createTileMap(TileMapType_Hex);
43+
tileMap->tileClicked.connect(clickedTile);
44+
tileMap->init(48, 36, 32);
45+
tileMap->setMessageReceived(true);
46+
tileMap->setTextVisible(false);
47+
48+
object->addChild(tileMap);
49+
50+
object->addChild(createSprite("../resource/icon/nato/units/infantry.png", 10, 10));
51+
object->addChild(createSprite("../resource/icon/nato/units/aviation_fr.png", 80, 10));
52+
object->addChild(createSprite("../resource/icon/nato/units/armour.png", 150, 10));
53+
54+
app->execute(object);
55+
return 0;
56+
}
57+

application/chapter-5/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
add_executable(chapter-5 main.cpp)
3+
target_link_libraries(chapter-5 framework sfml-system sfml-graphics sfml-window)

application/chapter-5/main.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include <iostream>
2+
#include <SFML/Graphics/RenderWindow.hpp>
3+
#include <Application.h>
4+
#include <Entity.h>
5+
#include <Text.h>
6+
#include <Scene.h>
7+
#include <Sprite.h>
8+
9+
using namespace std;
10+
11+
std::shared_ptr<Sprite> createSprite(const std::string &image, float x, float y)
12+
{
13+
auto sprite = std::make_shared<Sprite>();
14+
sprite->setPosition(x, y);
15+
auto texture = Application::getInstance()->loadTexture(image);
16+
sprite->setTexture(*texture);
17+
sprite->setSize(texture->getSize().x, texture->getSize().y);
18+
return sprite;
19+
}
20+
21+
class SpriteMessageListener : public MessageListener
22+
{
23+
// MessageListener interface
24+
public:
25+
SpriteMessageListener(std::shared_ptr<Sprite> inputSprite):
26+
sprite(inputSprite)
27+
{
28+
}
29+
public:
30+
bool onListener(std::shared_ptr<Message> message) override
31+
{
32+
auto sfml = std::dynamic_pointer_cast<SFMLMessage>(message);
33+
auto event = sfml->getEvent();
34+
if (event.type == sf::Event::KeyPressed) {
35+
if (event.key.code == sf::Keyboard::Key::A) {
36+
if(sprite->getPosition().x > 5)
37+
sprite->move(-5, 0);
38+
return true;
39+
} else if (event.key.code == sf::Keyboard::Key::D) {
40+
if(sprite->getPosition().x + sprite->getSize().x < Application::getInstance()->getWindow()->getSize().x - 5)
41+
sprite->move(5, 0);
42+
return true;
43+
}
44+
}
45+
46+
return false;
47+
}
48+
private:
49+
std::shared_ptr<Sprite> sprite;
50+
};
51+
52+
int main()
53+
{
54+
auto size = sf::Vector2f(960, 640);
55+
auto window = std::make_shared<sf::RenderWindow>(sf::VideoMode(size.x, size.y), "Chapter-5",
56+
sf::Style::Close);
57+
window->setVerticalSyncEnabled(true);
58+
59+
auto app = Application::getInstance();
60+
app->setBackgroundColor(sf::Color::Black);
61+
app->setWindow(window);
62+
63+
auto scene = std::make_shared<Scene>();
64+
scene->setName("scene");
65+
66+
auto background = Application::getInstance()->loadTexture("../resource/images/background.png");
67+
scene->setBackground(*background);
68+
69+
auto sprite = createSprite("../resource/images/plane.png", 390, 532);
70+
scene->addMessageListener(std::make_shared<SpriteMessageListener>(sprite));
71+
scene->addChild(sprite);
72+
73+
auto sceneManager = std::make_shared<SceneManager>();
74+
sceneManager->setInitialScene(scene);
75+
app->execute(sceneManager);
76+
77+
return 0;
78+
}
79+

application/chapter-6/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
add_executable(chapter-6 main.cpp)
3+
target_link_libraries(chapter-6 framework sfml-system sfml-graphics sfml-window)

0 commit comments

Comments
 (0)