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
30 changes: 30 additions & 0 deletions sem2/DogadinNM/DinoGame/AssetManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

#include "AssetManager.hpp"

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;
}

}
Binary file added sem2/DogadinNM/DinoGame/DB/dbUser.db
Binary file not shown.
223 changes: 223 additions & 0 deletions sem2/DogadinNM/DinoGame/DatabaseFunc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
//
// DatabaseFunc.cpp
// DinoGame
//
// Created by Nikitoooooozy) on 16.05.2024.
// Copyright © 2024 Nikitoooooozy). All rights reserved.
//

#include "DatabaseFunc.hpp"
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 = 0;

// 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("/Users/admin/Desktop/Progz/DinoGame/DinoGame/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(1);
}
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, 0, &zErrMsg);
sqlite3_close(db);
}
void selectInventory (std::string &outInventory){
// Pointer to SQLite connection
sqlite3 *db;

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

// 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("/Users/admin/Desktop/Progz/DinoGame/DinoGame/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(1);
}
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 = 0;

// 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("/Users/admin/Desktop/Progz/DinoGame/DinoGame/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 (1);
}

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 = 0;

// 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("/Users/admin/Desktop/Progz/DinoGame/DinoGame/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(1);
}
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 = 0;

// 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("/Users/admin/Desktop/Progz/DinoGame/DinoGame/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(1);
}

sql = "UPDATE User SET balance = (balance - 100) WHERE id = '1';";
rc = sqlite3_exec(db, sql.c_str(), callback, 0, &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, 0, &zErrMsg);
sqlite3_close(db);
}
void updateBalanceAfterWin(){
// Pointer to SQLite connection
sqlite3 *db;

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

// 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("/Users/admin/Desktop/Progz/DinoGame/DinoGame/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(1);
}

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

sqlite3_close(db);
}
68 changes: 68 additions & 0 deletions sem2/DogadinNM/DinoGame/Headers/Animator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//
// Animator.h
// Souls
//
// Created by Nikitoooooozy) on 25.04.2024.
// Copyright © 2024 Nikitoooooozy). All rights reserved.
//

#ifndef Animator_h
#define Animator_h

#include "includes.h"

class Animator
{
public:
struct Animation
{
std::string m_Name;
std::string m_TextureName;
std::vector<sf::IntRect> m_Frames;
sf::Time m_Duration;
bool m_Looping;
Animation(std::string const& name, std::string const& textureName,
sf::Time const& duration, bool looping) :m_Name(name), m_TextureName(textureName),
m_Duration(duration), m_Looping(looping){}
void AddFrames(sf::Vector2i const& startFrom,
sf::Vector2i const& frameSize, unsigned int frames, unsigned int traccia)
{
sf::Vector2i current = startFrom;
for (unsigned int t = 0; t < traccia; t++) {
for (unsigned int i = 0; i < frames; i++)
{
m_Frames.push_back(sf::IntRect(current.x, current.y, frameSize.x, frameSize.y));
current.x += frameSize.x;
}
current.y += frameSize.y;
current.x = startFrom.x;
}
}
};
explicit Animator(sf::Sprite& sprite);

Animator::Animation& CreateAnimation(std::string const& name,
std::string const& textureName, sf::Time const& duration,
bool loop = false );

void Update(sf::Time const& dt);
bool SwitchAnimation(std::string const& name);
std::string GetCurrentAnimationName() const;
void restart();
bool getEndAnim() const
{
return endAnim;
}
bool endAnim = false;

private:

Animator::Animation* FindAnimation(std::string const& name);
void SwitchAnimation(Animator::Animation* animation);
sf::Sprite& m_Sprite;
sf::Time m_CurrentTime;
std::list<Animator::Animation> m_Animations;
Animator::Animation* m_CurrentAnimation;
};

#endif /* Animator_h */
69 changes: 69 additions & 0 deletions sem2/DogadinNM/DinoGame/Headers/Animator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//
// Animator.hpp
// DinoGame
//
// Created by Nikitoooooozy) on 21.05.2024.
// Copyright © 2024 Nikitoooooozy). All rights reserved.
//

#ifndef Animator_hpp
#define Animator_hpp

#include "AssetManager.hpp"
#include "includes.h"

class Animator
{
public:
struct Animation
{
std::string m_Name;
std::string m_TextureName;
std::vector<sf::IntRect> m_Frames;
sf::Time m_Duration;
bool m_Looping;
Animation(std::string const& name, std::string const& textureName,
sf::Time const& duration, bool looping) :m_Name(name), m_TextureName(textureName),
m_Duration(duration), m_Looping(looping){}
void AddFrames(sf::Vector2i const& startFrom,
sf::Vector2i const& frameSize, unsigned int frames, unsigned int traccia)
{
sf::Vector2i current = startFrom;
for (unsigned int t = 0; t < traccia; t++) {
for (unsigned int i = 0; i < frames; i++)
{
m_Frames.push_back(sf::IntRect(current.x, current.y, frameSize.x, frameSize.y));
current.x += frameSize.x;
}
current.y += frameSize.y;
current.x = startFrom.x;
}
}
};
explicit Animator(sf::Sprite& sprite);

Animator::Animation& CreateAnimation(std::string const& name,
std::string const& textureName, sf::Time const& duration,
bool loop = false );

void Update(sf::Time const& dt);
bool SwitchAnimation(std::string const& name);
std::string GetCurrentAnimationName() const;
void restart();
bool getEndAnim() const
{
return endAnim;
}
bool endAnim = false;

private:

Animator::Animation* FindAnimation(std::string const& name);
void SwitchAnimation(Animator::Animation* animation);
sf::Sprite& m_Sprite;
sf::Time m_CurrentTime;
std::list<Animator::Animation> m_Animations;
Animator::Animation* m_CurrentAnimation;
};

#endif /* Animator_hpp */
Loading