#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <vector>
#include <string>
#include <iostream>
using namespace std;
using namespace sf;
// Item structure
struct Item {
string name;
RectangleShape shape;
};
// Function to draw items
void drawItems(RenderWindow& window, const vector<Item>& items) {
window.clear(Color::White);
for (const auto& item : items) {
window.draw(item.shape);
}
window.display();
}
int main() {
RenderWindow window(VideoMode(800, 600), "Simple CRUD Application");
vector<Item> items;
Font font;
if (!font.loadFromFile("arial.ttf")) {
cout << "Error loading font" << endl;
return -1;
}
Text text;
text.setFont(font);
text.setCharacterSize(24);
text.setFillColor(Color::Black);
bool running = true;
while (running) {
Event event;
while (window.pollEvent(event)) {
if (event.type == Event::Closed) {
running = false;
}
}
if (Keyboard::isKeyPressed(Keyboard::A)) {
// Add item
Item newItem;
newItem.name = "Item " + to_string(items.size() + 1);
newItem.shape.setSize(Vector2f(100, 50));
newItem.shape.setFillColor(Color::Green);
newItem.shape.setPosition(Vector2f(10, 10 + 60 * items.size()));
items.push_back(newItem);
drawItems(window, items);
}
if (Keyboard::isKeyPressed(Keyboard::R) && !items.empty()) {
// Remove item
items.pop_back();
drawItems(window, items);
}
if (Keyboard::isKeyPressed(Keyboard::U) && !items.empty()) {
// Update item
items.back().shape.setFillColor(Color::Red);
drawItems(window, items);
}
if (Keyboard::isKeyPressed(Keyboard::D) && !items.empty()) {
// Display items
string itemText;
for (const auto& item : items) {
itemText += item.name + "\n";
}
text.setString(itemText);
window.clear(Color::White);
window.draw(text);
window.display();
}
}
return 0;
}