Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions sem2/OlarMA/Dinos/AssetManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "AssetManager.h"

AssetManager *AssetManager::sInstance = nullptr;

AssetManager::AssetManager() {
// Разрешить существование только одного AssetManager
// В противном случае сгенерировать исключение
assert(sInstance == nullptr);
sInstance = this;
}

sf::Texture &AssetManager::GetTexture(std::string const &filename) {
auto &texMap = sInstance->m_Textures;

auto pairFound = texMap.find(filename);

if (pairFound != texMap.end()) {
return pairFound->second;
} else {
auto &texture = texMap[filename];
texture.loadFromFile(filename);
return texture;
}
}
23 changes: 23 additions & 0 deletions sem2/OlarMA/Dinos/AssetManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef AssetManager_hpp
#define AssetManager_hpp

#include "includes.h"

class AssetManager {

public:
AssetManager();

static sf::Texture &GetTexture(std::string const &filename);

static sf::Font &GetFont(std::string const &filename);

private:

std::map<std::string, sf::Texture> m_Textures;
std::map<std::string, sf::Font> m_Fonts;

static AssetManager *sInstance;
};

#endif
11 changes: 11 additions & 0 deletions sem2/OlarMA/Dinos/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.23)
project(Dinosaur)
include(FetchContent)
set(BUILD_SHARED_LIBS OFF)
FETCHCONTENT_DECLARE(SFML GIT_REPOSITORY https://github.com/SFML/SFML.git GIT_TAG 2.6.1)
FETCHCONTENT_MAKEAVAILABLE(SFML)
set(CMAKE_CXX_STANDARD 17)

add_executable(Dinosaur main.cpp AssetManager.h AssetManager.cpp DatabaseFunc.cpp DatabaseFunc.h dino.h Graphics.h includes.h LooserMenu.cpp Menu.cpp ProfileMenu.cpp RegisterMenu.cpp RegisterMenu.h Textbox.cpp Textbox.h WinnerMenu.cpp StartMenu.cpp StartBattleMenu.cpp ShopMenu.cpp sqlite3.h sqlite3ext.h sqlite3.c)

target_link_libraries(Dinosaur sfml-graphics)
224 changes: 224 additions & 0 deletions sem2/OlarMA/Dinos/DatabaseFunc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
#include "DatabaseFunc.h"

template<typename Out>

void split(const std::string &s, char delim, Out result) {
std::istringstream iss(s);
std::string item;
while (std::getline(iss, item, delim)) {
*result++ = item;
}
}

std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
split(s, delim, std::back_inserter(elems));
return elems;
}

static int callback(void *pString, int argc, char **argv, char **azColName) {
if (argc > 0) {
std::string *str = static_cast<std::string *>(pString);
str->assign(argv[0]);
}
return 0;
}

void insertUser(Textbox textbox) {
std::string enteringText = textbox.getString();
// Pointer to SQLite connection
sqlite3 *db;

// Save any error messages
char *zErrMsg = nullptr;

// Save the result of opening the file
int rc;

// Save any SQL
std::string sql;

// Save the result of opening the file
rc = sqlite3_open("C:/Users/mikhail/CLionProjects/Dinosaur/DB/dbUser.db", &db);

if (rc) {
// Show an error message
std::cout << "DB Error: " << sqlite3_errmsg(db) << std::endl;
// Close the connection
sqlite3_close(db);
// Return an error
return;
}
sql = "INSERT INTO User( id, namespace, balance, inventory) VALUES ('1', '" + enteringText +
"', '300', '') ON CONFLICT (id) DO UPDATE SET namespace = '" + enteringText +
"', balance = '300', inventory = '' WHERE id ='1';";

// Run the SQL (convert the string to a C-String with c_str() )
rc = sqlite3_exec(db, sql.c_str(), callback, nullptr, &zErrMsg);
sqlite3_close(db);
}

void selectInventory(std::string &outInventory) {
// Pointer to SQLite connection
sqlite3 *db;

// Save any error messages
char *zErrMsg = nullptr;

// Save the result of opening the file
int rc;

// Save any SQL
std::string sql;

// Save the result of opening the file
rc = sqlite3_open("C:/Users/mikhail/CLionProjects/Dinosaur/DB/dbUser.db", &db);

if (rc) {
// Show an error message
std::cout << "DB Error: " << sqlite3_errmsg(db) << std::endl;
// Close the connection
sqlite3_close(db);
// Return an error
return;
}
sql = "Select inventory FROM User WHERE id ='1';";

// Run the SQL (convert the string to a C-String with c_str() )

rc = sqlite3_exec(db, sql.c_str(), callback, &outInventory, &zErrMsg);

std::vector<std::string> splittedInventory = split(outInventory, ' ');
sqlite3_close(db);

}

void selectBalance(sf::Font font, std::string &outStr) {
// Pointer to SQLite connection
sqlite3 *db;

// Save any error messages
char *zErrMsg = nullptr;

// Save the result of opening the file
int rc;

// Save any SQL
std::string sql;

// Save the result of opening the file
rc = sqlite3_open("C:/Users/mikhail/CLionProjects/Dinosaur/DB/dbUser.db", &db);

if (rc) {
// Show an error message
std::cout << "DB Error: " << sqlite3_errmsg(db) << std::endl;
// Close the connection
sqlite3_close(db);
// Return an error
return;
}

sql = "Select balance FROM User WHERE id ='1';";

// Run the SQL (convert the string to a C-String with c_str() )
rc = sqlite3_exec(db, sql.c_str(), callback, &outStr, &zErrMsg);
sqlite3_close(db);
}

void selectNamespace(std::string &outStrNamespace) {
// Pointer to SQLite connection
sqlite3 *db;

// Save any error messages
char *zErrMsg = nullptr;

// Save the result of opening the file
int rc;

// Save any SQL
std::string sql;

// Save the result of opening the file
rc = sqlite3_open("C:/Users/mikhail/CLionProjects/Dinosaur/DB/dbUser.db", &db);

if (rc) {
// Show an error message
std::cout << "DB Error: " << sqlite3_errmsg(db) << std::endl;
// Close the connection
sqlite3_close(db);
// Return an error
return;
}
sql = "Select namespace FROM User WHERE id ='1';";

// Run the SQL (convert the string to a C-String with c_str() )
rc = sqlite3_exec(db, sql.c_str(), callback, &outStrNamespace, &zErrMsg);
sqlite3_close(db);

}

void updateBalance(std::string outStr, std::string name) {
// Pointer to SQLite connection
sqlite3 *db;

// Save any error messages
char *zErrMsg = nullptr;

// Save the result of opening the file
int rc;

// Save any SQL
std::string sql;

// Save the result of opening the file
rc = sqlite3_open("C:/Users/mikhail/CLionProjects/Dinosaur/DB/dbUser.db", &db);

if (rc) {
// Show an error message
std::cout << "DB Error: " << sqlite3_errmsg(db) << std::endl;
// Close the connection
sqlite3_close(db);
// Return an error
return;
}

sql = "UPDATE User SET balance = (balance - 100) WHERE id = '1';";
rc = sqlite3_exec(db, sql.c_str(), callback, nullptr, &zErrMsg);
sql = "Select inventory from User";
rc = sqlite3_exec(db, sql.c_str(), callback, &outStr, &zErrMsg);

sql = "Update User SET inventory = '" + outStr + name + " ' WHERE id='1';";
rc = sqlite3_exec(db, sql.c_str(), callback, nullptr, &zErrMsg);
sqlite3_close(db);
}

void updateBalanceAfterWin() {
// Pointer to SQLite connection
sqlite3 *db;

// Save any error messages
char *zErrMsg = nullptr;

// Save the result of opening the file
int rc;

// Save any SQL
std::string sql;

// Save the result of opening the file
rc = sqlite3_open("C:/Users/mikhail/CLionProjects/Dinosaur/DB/dbUser.db", &db);

if (rc) {
// Show an error message
std::cout << "DB Error: " << sqlite3_errmsg(db) << std::endl;
// Close the connection
sqlite3_close(db);
// Return an error
return;
}

sql = "UPDATE User SET balance = (balance + 25) WHERE id = '1';";
rc = sqlite3_exec(db, sql.c_str(), callback, nullptr, &zErrMsg);

sqlite3_close(db);
}
18 changes: 18 additions & 0 deletions sem2/OlarMA/Dinos/DatabaseFunc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef DatabaseFunc_hpp
#define DatabaseFunc_hpp

#include "includes.h"

void insertUser(Textbox textbox);

void selectInventory(std::string &outInventory);

void selectBalance(sf::Font font, std::string &outStr);

void selectNamespace(std::string &outStrNamespace);

void updateBalance(std::string outStr, std::string name);

void updateBalanceAfterWin();

#endif
28 changes: 28 additions & 0 deletions sem2/OlarMA/Dinos/Graphics.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef Graphics_h
#define Graphics_h
#define SCRWIDTH 800
#define SCRHEIGHT 300

#include "includes.h"

using namespace std::this_thread; // sleep_for, sleep_until
using namespace std::chrono; // nanoseconds, system_clock, seconds
void menuWindowPrint();

void shopWindowPrint();

void registrationWindowPrint();

void startWindowPrint();

void profileWindowPrint();

void startingBattleWindowPrint();

void winnerWindowPrint();

void looserWindowPrint();

void idleAnimation(sf::Sprite &sprite, std::string output);

#endif
Loading