Compare commits
2 Commits
main
...
test_sqlit
| Author | SHA1 | Date | |
|---|---|---|---|
| fe17cbda22 | |||
| 6794dada92 |
@@ -33,6 +33,7 @@ add_library(shared STATIC
|
||||
shared/src/values/Similarity.cpp
|
||||
shared/src/TcpSocket.cpp
|
||||
shared/src/sql.cpp
|
||||
shared/src/values/DnaDB.cpp
|
||||
)
|
||||
|
||||
add_executable(app
|
||||
|
||||
104
Readme.md
104
Readme.md
@@ -1,90 +1,26 @@
|
||||
# Treender
|
||||
|
||||
A genetic algorithm visualization tool with interactive evolution simulation.
|
||||
# Code style
|
||||
|
||||
## Overview
|
||||
## Naming Conventions:
|
||||
- PascalCase:
|
||||
- Functions
|
||||
- Class names
|
||||
- Struct names
|
||||
|
||||
Treender is a C++ application that simulates genetic algorithms through an interactive visual interface. Users can observe and influence the evolution of digital organisms represented as visual patterns.
|
||||
- camelCase:
|
||||
- Class attributes
|
||||
- Class methods
|
||||
- Struct attributes
|
||||
- Variables
|
||||
|
||||
## Features
|
||||
## Class Structure:
|
||||
|
||||
- **Interactive Evolution**: Like/dislike organisms to guide evolution
|
||||
- **Genetic Algorithm**: DNA-based reproduction with mutation
|
||||
- **Visual Representation**: Organisms displayed as colorful patterns
|
||||
- **Client-Server Architecture**: Separate app and server components
|
||||
- **Cross-Platform**: Supports Web, Android, and Desktop platforms
|
||||
- Header (.h) Files:
|
||||
- Declare public methods and attributes first, followed by private members.
|
||||
- Implementation (.cpp) Files:
|
||||
- Implement methods in the same order as declared in the corresponding header file.
|
||||
|
||||
## Architecture
|
||||
|
||||
```
|
||||
Treender
|
||||
├── app/ # Main application (Raylib + Dear ImGui)
|
||||
├── server/ # TCP server for data management
|
||||
├── shared/ # Shared code (DNA logic, networking)
|
||||
├── external/ # Third-party libraries
|
||||
└── data.db # SQLite database
|
||||
```
|
||||
|
||||
## Components
|
||||
|
||||
### Application
|
||||
- **Main Entry**: `app/src/main.cpp`
|
||||
- **Core Class**: `App` class handles rendering, input, and evolution logic
|
||||
- **Visualization**: Uses Raylib for graphics and Dear ImGui for UI
|
||||
|
||||
### Server
|
||||
- **TCP Server**: Handles client connections and data persistence
|
||||
- **Database**: SQLite for storing organism data and user preferences
|
||||
- **Block Management**: Tracks warnings and blocked organisms
|
||||
|
||||
### Shared Library
|
||||
- **DNA System**: Genetic algorithm implementation (`Dna.cpp`)
|
||||
- **Networking**: TCP socket communication
|
||||
- **Data Structures**: Shared between app and server
|
||||
|
||||
## Building
|
||||
|
||||
### Prerequisites
|
||||
- CMake 3.10+
|
||||
- C++17 compiler
|
||||
- Raylib
|
||||
- Dear ImGui
|
||||
- SQLite3
|
||||
|
||||
### Build Instructions
|
||||
|
||||
```bash
|
||||
mkdir build
|
||||
cd build
|
||||
cmake ..
|
||||
make
|
||||
```
|
||||
## Running
|
||||
|
||||
1. Start the server:
|
||||
```bash
|
||||
./build/server
|
||||
```
|
||||
|
||||
2. Run the application:
|
||||
```bash
|
||||
./build/app
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
- **Like/Disike**: Click on organisms to influence evolution
|
||||
- **Rotation**: Drag to rotate the view
|
||||
- **Statistics**: View similarity scores and generation information
|
||||
|
||||
## Database
|
||||
|
||||
The application uses SQLite database (`data.db`) to store:
|
||||
- Organism DNA data
|
||||
- User preferences
|
||||
- Blocked/warned organisms
|
||||
|
||||
## Screenshots
|
||||
|
||||

|
||||

|
||||
## Include order
|
||||
- Std
|
||||
- local ( form inc dir )
|
||||
- external raylib
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <array>
|
||||
#include "canvas/Canvas.hpp"
|
||||
#include "DnaStore.hpp"
|
||||
|
||||
class App
|
||||
{
|
||||
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace DnaStore
|
||||
{
|
||||
void init();
|
||||
void deinit();
|
||||
void load(DnaManagerData *data);
|
||||
void saveData(DnaManagerData *data);
|
||||
void saveVec(DnaManagerData *data);
|
||||
|
||||
@@ -64,7 +64,7 @@ void App::init(int screenWidth, int screenHeight)
|
||||
this->screenWidth = screenWidth;
|
||||
this->screenHeight = screenHeight;
|
||||
this->canvas.init(screenWidth);
|
||||
|
||||
DnaStore::init();
|
||||
// int s = MeasureText("GEN 9999: 999/999", 20);
|
||||
// TraceLog(LOG_INFO, "%d", s);
|
||||
|
||||
@@ -239,4 +239,5 @@ void App::deinit()
|
||||
UnloadRenderTexture(canvasTexure[i]);
|
||||
}
|
||||
canvas.deinit();
|
||||
DnaStore::deinit();
|
||||
}
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "sys.hpp"
|
||||
#include "DnaStore.hpp"
|
||||
#include "TcpSocket.hpp"
|
||||
#include "values/DnaDB.hpp"
|
||||
|
||||
#include <raylib.h>
|
||||
|
||||
@@ -17,19 +18,30 @@
|
||||
#define VECTOR_FILE_NAME "VECTOR.bin"
|
||||
#define GEN_FILE_PATTRN "gen/%04d.bin"
|
||||
#define HOST_NAME "localhost"
|
||||
#define DB_NAME "data.db"
|
||||
|
||||
DnaDB dnaDB;
|
||||
|
||||
void DnaStore::init(){
|
||||
const char* filename = sys::transformFilePath(DB_NAME);
|
||||
dnaDB.init(filename);
|
||||
}
|
||||
|
||||
void DnaStore::deinit(){
|
||||
dnaDB.deinit();
|
||||
}
|
||||
|
||||
void DnaStore::load(DnaManagerData *data)
|
||||
{
|
||||
const char *dir = sys::transformFilePath("gen");
|
||||
std::filesystem::create_directory(dir);
|
||||
if (sys::fileExists(ID_FILE_NAME))
|
||||
{
|
||||
sys::loadDataFromFile(ID_FILE_NAME, &data->id, sizeof(int64_t));
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
std::vector<int64_t> ids = dnaDB.getUserIds();
|
||||
if(!ids.empty()){
|
||||
data->id = ids[0];
|
||||
}else{
|
||||
data->id = time(nullptr);
|
||||
sys::saveDataToFile(ID_FILE_NAME, &data->id, sizeof(int64_t));
|
||||
dnaDB.insertUser(data->id, 0);
|
||||
}
|
||||
|
||||
if (sys::fileExists(DATA_FILE_NAME))
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 168 KiB |
@@ -44,12 +44,16 @@ namespace sql
|
||||
void init();
|
||||
void shutdown();
|
||||
int open(const char *filename, sqlite3 **ppDb);
|
||||
int exec(sqlite3 *db, const char *sql, int (*callback)(void *, int, char **, char **), void *first_arg, char **errmsg);
|
||||
int prepare_v2(sqlite3 *db, const char *zSql, int nByte, sqlite3_stmt **pStmt, const char **pzTail);
|
||||
int bind_int64(sqlite3_stmt *pStmt, int column, int64_t value);
|
||||
int bind_blob(sqlite3_stmt*pStmt, int column, const void* data, int size);
|
||||
int step(sqlite3_stmt *pStmt);
|
||||
int column_count(sqlite3_stmt *pStmt);
|
||||
int column_type(sqlite3_stmt *pStmt, int column);
|
||||
int64_t column_int64(sqlite3_stmt *pStmt, int column);
|
||||
const void* column_blob(sqlite3_stmt *pStmt, int column);
|
||||
int64_t column_bytes(sqlite3_stmt* pStmt, int column);
|
||||
int finalize(sqlite3_stmt *pStmt);
|
||||
int reset(sqlite3_stmt *pStmt);
|
||||
int close(sqlite3 *db);
|
||||
|
||||
21
shared/inc/values/DnaDB.hpp
Normal file
21
shared/inc/values/DnaDB.hpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#include <cinttypes>
|
||||
#include <vector>
|
||||
|
||||
struct sqlite3;
|
||||
|
||||
class DnaDB
|
||||
{
|
||||
sqlite3 *db;
|
||||
|
||||
public:
|
||||
void init(const char *db_name = "data.db");
|
||||
void deinit();
|
||||
|
||||
std::vector<int64_t> getUserIds();
|
||||
int insertUser(int64_t id, int64_t max_gen);
|
||||
int updateUser(int64_t id, int64_t max_gen);
|
||||
|
||||
void getGenerations();
|
||||
void getLikesForGeneration();
|
||||
void getVectorForGeneration();
|
||||
};
|
||||
@@ -7,9 +7,9 @@
|
||||
|
||||
enum Liked
|
||||
{
|
||||
tbd,
|
||||
yes,
|
||||
no
|
||||
tbd = 0,
|
||||
yes = 1,
|
||||
no = 2,
|
||||
};
|
||||
|
||||
struct UiUnit
|
||||
|
||||
@@ -78,6 +78,10 @@ namespace sql
|
||||
{
|
||||
return sqlite3_open(filename, ppDb);
|
||||
}
|
||||
int exec(sqlite3 *db, const char *sql, int (*callback)(void *, int, char **, char **), void *first, char **errmsg)
|
||||
{
|
||||
return sqlite3_exec(db, sql, callback, first, errmsg);
|
||||
}
|
||||
int prepare_v2(sqlite3 *db, const char *zSql, int nByte, sqlite3_stmt **pStmt, const char **pzTail)
|
||||
{
|
||||
return sqlite3_prepare_v2(db, zSql, nByte, pStmt, pzTail);
|
||||
@@ -86,6 +90,10 @@ namespace sql
|
||||
{
|
||||
return sqlite3_bind_int64(pStmt, column, value);
|
||||
}
|
||||
int bind_blob(sqlite3_stmt *pStmt, int column, const void *data, int size)
|
||||
{
|
||||
return sqlite3_bind_blob(pStmt, column, data, size, SQLITE_STATIC);
|
||||
}
|
||||
int step(sqlite3_stmt *pStmt)
|
||||
{
|
||||
return sqlite3_step(pStmt);
|
||||
@@ -102,6 +110,14 @@ namespace sql
|
||||
{
|
||||
return sqlite3_column_int64(pStmt, column);
|
||||
}
|
||||
const void *column_blob(sqlite3_stmt *pStmt, int column)
|
||||
{
|
||||
return sqlite3_column_blob(pStmt, column);
|
||||
}
|
||||
int64_t column_bytes(sqlite3_stmt *pStmt, int column)
|
||||
{
|
||||
return sqlite3_column_bytes(pStmt, column);
|
||||
}
|
||||
int finalize(sqlite3_stmt *pStmt)
|
||||
{
|
||||
return sqlite3_finalize(pStmt);
|
||||
|
||||
69
shared/src/values/DnaDB.cpp
Normal file
69
shared/src/values/DnaDB.cpp
Normal file
@@ -0,0 +1,69 @@
|
||||
#include "values/DnaDB.hpp"
|
||||
|
||||
#include <sql.hpp>
|
||||
|
||||
|
||||
|
||||
void DnaDB::init(const char * db_name)
|
||||
{
|
||||
sql::init();
|
||||
int res = sql::open(db_name, &db);
|
||||
|
||||
constexpr char create_user_table[] = "CREATE TABLE IF NOT EXISTS user_table ( ID INTEGER PRIMARY KEY, USER_ID INTEGER, MAX_GENERATION INTEGER);";
|
||||
res = sql::exec(db, create_user_table, nullptr, nullptr, nullptr);
|
||||
}
|
||||
|
||||
void DnaDB::deinit()
|
||||
{
|
||||
sql::close(db);
|
||||
sql::shutdown();
|
||||
}
|
||||
|
||||
std::vector<int64_t> DnaDB::getUserIds()
|
||||
{
|
||||
constexpr char sql[] = "SELECT USER_ID FROM user_table;";
|
||||
sqlite3_stmt *stmt;
|
||||
sql::prepare_v2(db, sql, -1, &stmt, nullptr);
|
||||
std::vector<int64_t> ids;
|
||||
while (sql::step(stmt) != SQL_DONE)
|
||||
{
|
||||
int64_t id = sql::column_int64(stmt, 0);
|
||||
ids.push_back(id);
|
||||
}
|
||||
sql::finalize(stmt);
|
||||
return ids;
|
||||
}
|
||||
|
||||
int DnaDB::insertUser(int64_t id, int64_t max_gen){
|
||||
constexpr char sql[] = "INSERT INTO user_table (USER_ID, MAX_GENERATION) VALUES (?,?);";
|
||||
sqlite3_stmt *stmt;
|
||||
sql::prepare_v2(db, sql, -1, &stmt, nullptr);
|
||||
sql::bind_int64(stmt, 1, id);
|
||||
sql::bind_int64(stmt, 2, max_gen);
|
||||
int ret = sql::step(stmt);
|
||||
sql::finalize(stmt);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int DnaDB::updateUser(int64_t id, int64_t max_gen){
|
||||
constexpr char sql[] = "UPDATE user_table SET MAX_GENERATION = ? WHERE USER_ID = ?;";
|
||||
sqlite3_stmt *stmt;
|
||||
sql::prepare_v2(db, sql, -1, &stmt, nullptr);
|
||||
sql::bind_int64(stmt, 1, max_gen);
|
||||
sql::bind_int64(stmt, 2, id);
|
||||
int ret = sql::step(stmt);
|
||||
sql::finalize(stmt);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void DnaDB::getGenerations()
|
||||
{
|
||||
}
|
||||
|
||||
void DnaDB::getLikesForGeneration()
|
||||
{
|
||||
}
|
||||
|
||||
void DnaDB::getVectorForGeneration()
|
||||
{
|
||||
}
|
||||
|
Before Width: | Height: | Size: 52 KiB After Width: | Height: | Size: 52 KiB |
|
Before Width: | Height: | Size: 3.3 KiB After Width: | Height: | Size: 3.3 KiB |
Reference in New Issue
Block a user